// JavaScript Document
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}
/*TRIM TEXT*/
function trim( value ) {
	
	return LTrim(RTrim(value));
}

/*TO GET THE URL OF CURRENT PAGE*/
 function getURL(uri) {
	uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
	uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
	uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
	uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
	pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
	pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
	uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
	uri.file = uri.page;
	if (uri.ext != '') uri.file += '.' + uri.ext;
	if (uri.file == '') uri.page = 'index';
	uri.args = location.search.substr(1).split("?");
	return uri;
}
/*CLEAR VALUE*/
/*modified by jaya on Mar 18*/
function clear_txt(id,deftxt){
	id.value=(trim_space(id.value)==deftxt)?'':id.value;
	id.style.color='#000';
	return true;
}
/*modified by jaya on Mar 18*/
function assign_text(id,deftxt){
	//id.value=(trim_space(id.value)=='')?deftxt:id.value;
	if(trim_space(id.value)==''){
		id.value=deftxt;
		id.style.color='#999999';
	}else{
		id.value=id.value;
		id.style.color='#000';
	}
	return true;
}
function trim_space(str) { 

    if(!str || typeof str != 'string')     

	   return '';    

 return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,''); 

}
/*TOGGLE BETWEEN DISPLAY OF TEXT BOX*/
function doShow(combo,topic){
	
	if(isItOk(combo)){
	var sel =combo.options[combo.selectedIndex].value;
	box = 'new_'+topic+'_topic';
	topic=topic.toLowerCase();
	selectbox =topic+'_topic_'+'select';
	var btn1=topic+'_btn_save';
	var btn2=topic+'_btn_close';
	var bt1=document.getElementById(btn1);
	var bt2=document.getElementById(btn2);

	var ld = topic+'_load';
	var speed=500;
	if(sel=='blank'){
		if(topic=='main'){
			document.getElementById('sub_topic_select').disabled='disabled';
			document.getElementById('title_topic_select').disabled='disabled';
			make_default_select(document.getElementById('sub_topic_select'));
			make_default_select(document.getElementById('title_topic_select'));
		}else if(topic=='sub'){
			document.getElementById('main_topic_select').disabled='disabled';
			document.getElementById('title_topic_select').disabled='disabled';
			make_default_select(document.getElementById('title_topic_select'));
		}else if(topic=='title'){
			document.getElementById('main_topic_select').disabled='disabled';
			document.getElementById('sub_topic_select').disabled='disabled';
		}
		bt1.style.display='inline';
		bt2.style.display='inline';
		document.getElementById(box).style.display='inline';
		clear_content('toggle');
		document.getElementById(selectbox).style.display='none';
		document.getElementById(box).value='';
		document.getElementById(box).focus();
		return true;
	}
	else{
		document.getElementById(box).style.display='none';
		bt1.style.display='none';
		bt2.style.display='none';
		document.getElementById(selectbox).style.display='inline';
		return false;
	}
	}
}
/*FORCE TO CHOOSE THE PREVIOUS ONES*/
function validate(topic){
	var	box =topic+'_topic_'+'select';
	var title='';
	var select_box = '';
	if(topic=='main') {title='Main Topic'; select_box=document.getElementById('sub_topic_select'); box=document.getElementById(box);}
	else if(topic=='sub') {
		if(document.getElementById('main_topic_select').value=='default'){
			title='Category'; 
			select_box=document.getElementById('title_topic_select'); 
			box=document.getElementById('main_topic_select');
		}else{
			title = 'Question';
			select_box=document.getElementById('title_topic_select');
			box=document.getElementById(box);
		}
	}
	if(box.options[0].selected==true){
		alert('Please Choose the relevant '+title+' first!');
		select_box.options[0].selected='selected';
		box.focus();
		return false;
	}
	return true;

}
/*VALIDATE UNIQUNESS*/
function isNotUnique(mid,sid,tid,mode,temp_id){
	if(tid!='' && sid!='' && tid!='' && tid!='blank' && sid!='blank' && mid!='blank'){
xmlHttp=GetXmlHttpObject()
	var url="General.php";
	url=url+"?mid="+mid+"&sid="+sid+"&tid="+tid+"&action=validate&mode="+mode+"&temp_id="+temp_id;
	var dt = new Date( ).valueOf(); 
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request")
		 return
	}
	
	xmlHttp.onreadystatechange=function() 
		{ 
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{ 	
				var response=eval(xmlHttp.responseText);
				document.templates.unique.value=response;
			} 
			
			if (xmlHttp.readyState==1 || xmlHttp.readyState!="complete")
			{
				 
				
			} 
	}
		xmlHttp.open("GET",url + "&dt=" + dt,true)
		xmlHttp.send(null);
	}
}
/*GET REFERRENCE TO HTTP REQUEST*/
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
/*POPULATE SUB TOPICS*/
function populate_sub_topic(tid,sel) 
{
	if(tid!='' && tid!='blank'){
	xmlHttp=GetXmlHttpObject()
	var url="General.php";
	url=url+"?topic_id="+tid+"&action=sub";
	var dt = new Date( ).valueOf(); 
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request")
		 return
	}
	
	xmlHttp.onreadystatechange=function() 
		{ 
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{ 	
				var myarray=eval(xmlHttp.responseText);
				//document.getElementById('sub_load').style.display='none';
				//alert(myarray);
				document.getElementById('main_topic_id').value=tid;
				for(j=document.templates.sub_topic_select.options.length-1;j>=0;j--)
				{
					document.templates.sub_topic_select.remove(j);
				}
				
			make_select_box(document.templates.sub_topic_select,1);
			
			
				for (i=0;i<myarray.length;i++)
				{
					var optn = document.createElement("OPTION");
					var op = myarray[i];
					temp = op.split("*");
					var txt = temp[0];
					var val = temp[1];
					optn.text = txt;
					optn.value = val;
					document.templates.sub_topic_select.options.add(optn);
				} 
				make_select_box(document.templates.sub_topic_select,2);
			/*TO MAKE TITLE BOX AS DEFAULT ONE*/
			for(j=document.templates.title_topic_select.options.length-1;j>=0;j--)
					{
						document.templates.title_topic_select.remove(j);
					}
			make_select_box(document.templates.title_topic_select,3);
			/*END TITLE FIX*/
			if(typeof(sel) != 'undefined'){
				select_index(document.templates.sub_topic_select,sel);
			}	
		document.getElementById('sub_load').style.display='none';
		document.getElementById('sub_topic_select').disabled='';
		document.getElementById('title_topic_select').disabled='';


		} 
		//alert(xmlHttp.readyState);
		if (xmlHttp.readyState==1)
		{
			//if(typeof(sel) != 'undefined')
			document.getElementById('sub_load').style.display='block';
			document.getElementById('sub_topic_select').disabled='disabled';
			document.getElementById('title_topic_select').disabled='disabled';

			
		} 
	}
		xmlHttp.open("GET",url + "&dt=" + dt)
		xmlHttp.send(null);
	}
}
/*POPULATE TITLE*/
function populate_title(tid,sel) 
{
	if(tid!='' && tid!='blank'){
	xmlHttp=GetXmlHttpObject()
	var mid=document.getElementById('main_topic_id').value;
	var url="General.php";
	url=url+"?sub_id="+tid+"&action=title&main_id="+mid;
		var dt = new Date( ).valueOf(); 
		if (xmlHttp==null)
		{
			 alert ("Browser does not support HTTP Request")
			 return
		}
		xmlHttp.onreadystatechange=function() 
						{ 	

							if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
							{ 	
								var myarray=eval(xmlHttp.responseText);
								for(j=document.templates.title_topic_select.options.length-1;j>=0;j--)
								{
									document.templates.title_topic_select.remove(j);
								}
								make_select_box(document.templates.title_topic_select,1);
								
								for (i=0;i<myarray.length;i++)
								{
									var optn = document.createElement("OPTION");
									var op = myarray[i];
									temp = op.split("*");
									var txt = temp[0];
									var val = temp[1];
									optn.text = txt;
									optn.value = val;
									document.templates.title_topic_select.options.add(optn);
								} 
							make_select_box(document.templates.title_topic_select,2);
							if(typeof(sel) != 'undefined'){
									select_index(document.templates.title_topic_select,sel);
								}	
								document.getElementById('title_load').style.display='none';
								document.getElementById('main_topic_select').disabled='';
								document.getElementById('title_topic_select').disabled='';

							} 
								
							if (xmlHttp.readyState==1)
							{
								//if(typeof(sel) != 'undefined')
								document.getElementById('title_load').style.display='block';
								document.getElementById('main_topic_select').disabled='disabled';
								document.getElementById('title_topic_select').disabled='disabled';

							} 
						}
		xmlHttp.open("GET",url + "&dt=" + dt)
		xmlHttp.send(null);
	}
}

function populate_main_topic(tid) 
{
	if(tid!='' && tid!='blank'){
	xmlHttp=GetXmlHttpObject()
	var mid=document.getElementById('main_topic_id').value;
	var url="General.php";
	url=url+"?topic_id="+tid+"&action=select_main";
	var dt = new Date( ).valueOf(); 
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request")
		 return
	}
	xmlHttp.onreadystatechange=function() 
	{ 	

		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 	
			var myarray=eval(xmlHttp.responseText);
			for(j=document.templates.title_topic_select.options.length-1;j>=0;j--)
			{
				document.templates.title_topic_select.remove(j);
			}
			make_select_box(document.templates.title_topic_select,1);
			
			for (i=0;i<myarray.length;i++)
			{
				var optn = document.createElement("OPTION");
				var op = myarray[i];
				temp = op.split("*");
				var txt = temp[0];
				var val = temp[1];
				optn.text = txt;
				optn.value = val;
				document.templates.title_topic_select.options.add(optn);
			} 
					make_select_box(document.templates.title_topic_select,2);
				document.getElementById('main_load').style.display='none';
				document.getElementById('sub_topic_select').disabled='';
				document.getElementById('title_topic_select').disabled='';

		} 
			
		if (xmlHttp.readyState==1)
		{
			 document.getElementById('main_load').style.display='block';
			document.getElementById('sub_topic_select').disabled='';
			document.getElementById('title_topic_select').disabled='';

			
		} 

	}
		xmlHttp.open("GET",url + "&dt=" + dt)
		xmlHttp.send(null);
	}
}
function select_index(selectObj,idex){
	for(var i = 0; i < selectObj.length; i++) {
		//alert(selectObj.options[i].value);
		if(selectObj.options[i].value == idex)
		selectObj.selectedIndex = i;
		}
}
/*PROPER CASE*/
function toProperCase(str) {
var elem = str;
var val = elem.toLowerCase().replace(/^(.)|\s(.)/g,function($1) { return $1.toUpperCase(); });
return val;
}

/*ADD DEFAULT ENTRIES TO SELECT BOXES*/
function make_select_box(combo,status){
	var cb=combo.name;
	var arr = cb.split("_");
	var text = toProperCase(arr[0]);
	if(text!='Title') text = text+ ' Topic';
	if (status==1 || status==3){
		var optn = document.createElement("OPTION");
		optn.text = '<-- Choose '+text+' -->';
		optn.value ='default';
		combo.options.add(optn);
	}
	if(status==2 || status==3){
		var optn = document.createElement("OPTION");
		optn.text = '<-- Enter Some Text -->';
		optn.value ='blank';
		combo.options.add(optn);
	}
}
function make_default_select(combo){
	for(j=combo.options.length-1;j>=0;j--)
		combo.remove(j);
	make_select_box(combo,3);
}
function make_editor(state){
	var is_on= document.getElementById('fck_enable').value;
	 if(state=='off') document.getElementById('fck_enable').value='0';
	else if(state=='on') document.getElementById('fck_enable').value='1';	

	var oEditor1 = FCKeditorAPI.GetInstance( 'objFCK' ) ;
	var st = true;
	if(state=='off') st=false;
	enable_items(oEditor1,st);
	
}
/*ESCAPE TAB KEY FOR SELECT BOX FOR FIXING FCKEDITOR*/
function stopTab(e){
	var KeyID = (window.event) ? event.keyCode : e.keyCode;	
	if(KeyID==9) return false;
}
/*SAVE ENTRIES RELEVANTLY*/
function KeyCheck(txt,text,e)

{
/*for text box key press*/
if(typeof(e)!='undefined'){
	var KeyID = (window.event) ? event.keyCode : e.keyCode;	
	if (KeyID==27) txt='close';
	else if(KeyID==13) txt='save';
	else if(KeyID==9) return false;
	else return;
}
	/* End text box key press*/
	var box = "new_"+text+"_topic";
	var textbox=document.getElementById(box);
	var sel=text+"_topic_select";
	var selectbox=document.getElementById(sel);
	var btn1=text+'_btn_save';
	var btn2=text+'_btn_close';
	var bt1=document.getElementById(btn1);
	var bt2=document.getElementById(btn2);
	var main_topic=document.templates.main_topic_select;
	var mid=main_topic.options[main_topic.selectedIndex].value;
	var sub_topic=document.templates.sub_topic_select;
	var sid=sub_topic.options[sub_topic.selectedIndex].value;

	//alert(KeyID);
	switch(txt)
	
	{
	
		case 'close':
			textbox.style.display='none';
			bt1.style.display='none';
			bt2.style.display='none';
			selectbox.style.display='inline';
			selectbox.options[0].selected='selected';
			document.getElementById('sub_topic_select').disabled='';
			document.getElementById('title_topic_select').disabled='';
			document.getElementById('main_topic_select').disabled='';
			//make_editor('off');
			break;
		case 'save':
			var val = trim(textbox.value);
			var is_ok = (val.indexOf("?") ==0);
			if(val=='' ||val.indexOf("+") > -1|| is_ok) { alert('Please enter a valid value');textbox.focus();return false;}
			if(text=='main'){
				if(NotExists(textbox.value,'main')){
					save_main_topic(textbox.value);
					//document.getElementById('sub_topic_select').disabled='';
					//document.getElementById('title_topic_select').disabled='';
				}else{
					alert('Category Already Exists!');textbox.focus();
					return false;
				}
			}else if(text=='sub'){
				if(NotExists(textbox.value,'sub'))
					{save_sub_topic(textbox.value,mid);
					//document.getElementById('main_topic_select').disabled='';
					//document.getElementById('title_topic_select').disabled='';
					}
				else {alert('Question Already Exists!');textbox.focus();return false;}
			}else if(text=='title'){
				if(NotExists(textbox.value,'title'))
					{save_title(textbox.value,mid,sid);
					//document.getElementById('sub_topic_select').disabled='';
					//document.getElementById('main_topic_select').disabled='';
					}
				else {alert('Title Already Exists!');textbox.focus();return false;}
			}
			textbox.style.display='none';
			bt1.style.display='none';
			bt2.style.display='none';
			selectbox.style.display='inline';
			return false;
			break; 
	
	}

}
/*VALIDATE DUPLICATES*/
function NotExists(topic,text){
	/*remove trial and leading spaces , also in between spaces*/
	topic = trim(topic);
	topic = topic.replace(/\s+/g," ");
	switch(text){
		case 'main':
            var Target=document.templates.main_topic_select;
			break;
		case 'sub':
			var Target=document.templates.sub_topic_select;
			break;
		case 'title':
			var Target=document.templates.title_topic_select;
			break;
	}
            var TIx1, TValue0, TValue1, TText0, TText1;
            for (TIx1=1; TIx1<Target.options.length; TIx1++) {
              TText1 =Target.options[TIx1].text;
			  TText1 = TText1.toLowerCase();
			  topic = topic.toLowerCase();
              if (TText1 == topic) {
                return false;
              }
            }
          	
	return true;
	
}
/*SAVE MAIN TOPIC*/
function save_main_topic(topic){
	if(document.getElementById('sub_load').style.display=='inline') document.getElementById('sub_load').style.display='none';
	xmlHttp=GetXmlHttpObject()
	var mid=document.getElementById('main_topic_id').value;
	var url="General.php";
	topic = escape(topic);
	url=url+"?topic="+trim(topic)+"&action=save_main";
	var dt = new Date( ).valueOf(); 
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request")
		 return
	}
	xmlHttp.onreadystatechange=function() 
	{ 	

		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 	
			var response=xmlHttp.responseText;
			var optn = document.createElement("OPTION");
			optn.text = unescape(topic);
			optn.value =response;
			var length=document.getElementById('main_topic_select').options.length;
			document.getElementById('main_topic_select').options.add(optn,length-1);
			optn.selected='selected';
			store_selected();
			document.getElementById('main_topic_id').value=response;
			document.getElementById('main_load').style.display='none';
			document.getElementById('sub_topic_select').disabled='';
			document.getElementById('title_topic_select').disabled='';
			document.getElementById('main_topic_select').disabled='';
			document.getElementById('new_fck').value='1';
			
		}
		if (xmlHttp.readyState==1 && xmlHttp.readyState!="complete")
		{
			document.getElementById('main_load').style.display='inline';
			document.getElementById('sub_topic_select').disabled='disabled';
			document.getElementById('title_topic_select').disabled='disabled';
			document.getElementById('main_topic_select').disabled='disabled';
			
		} 
	}

		xmlHttp.open("GET",url + "&dt=" + dt)
		xmlHttp.send(null);
}
/*SAVE SUB TOPIC*/
function save_sub_topic(topic,mid){
if(document.getElementById('title_load').style.display=='inline') document.getElementById('title_load').style.display='none';

	xmlHttp=GetXmlHttpObject()
	//var mid=document.getElementById('main_topic_id').value;
	var url="General.php";
	topic = escape(topic);
	url=url+"?topic="+trim(topic)+"&action=save_sub&mid="+mid;
	var dt = new Date( ).valueOf(); 
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request")
		 return
	}
	xmlHttp.onreadystatechange=function() 
	{ 	
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 	
			var response=xmlHttp.responseText;
			var optn = document.createElement("OPTION");
			topic = unescape(topic);
			optn.text = topic;
			optn.value =response;
			var length=document.getElementById('sub_topic_select').options.length;
			document.getElementById('sub_topic_select').options.add(optn,length-1);
			optn.selected='selected';
			store_selected();
			 document.getElementById('sub_load').style.display='none';
			 document.getElementById('main_topic_select').disabled='';
			document.getElementById('title_topic_select').disabled='';
			document.getElementById('sub_topic_select').disabled='';
			
		}
		if (xmlHttp.readyState==1 && xmlHttp.readyState!="complete")
		{
			  document.getElementById('sub_load').style.display='inline';
			document.getElementById('main_topic_select').disabled='disabled';
			document.getElementById('title_topic_select').disabled='disabled';
			document.getElementById('sub_topic_select').disabled='disabled';
			
		} 
	}
		xmlHttp.open("GET",url + "&dt=" + dt)
		xmlHttp.send(null);
}

/*SAVE TITLE*/
function save_title(topic,mid,sid){
	xmlHttp=GetXmlHttpObject()
	//var mid=document.getElementById('main_topic_id').value;
	topic = escape(topic);
	var url="General.php";
	url=url+"?topic="+trim(topic)+"&action=save_title&mid="+mid+"&sid="+sid;
	var dt = new Date( ).valueOf(); 
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request")
		 return
	}
	xmlHttp.onreadystatechange=function() 
	{ 	
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 	
			var response=xmlHttp.responseText;
			var optn = document.createElement("OPTION");
			topic = unescape(topic);
			optn.text = topic;
			optn.value =response;
			var length=document.getElementById('title_topic_select').options.length;
			document.getElementById('title_topic_select').options.add(optn,length-1);
			optn.selected='selected';
			
			store_selected();

			 document.getElementById('title_load').style.display='none';
			 document.getElementById('main_topic_select').disabled='';
				document.getElementById('sub_topic_select').disabled='';
				document.getElementById('title_topic_select').disabled='';
			
		}
		if (xmlHttp.readyState==1 && xmlHttp.readyState!="complete")
		{
			   document.getElementById('title_load').style.display='inline';
				document.getElementById('main_topic_select').disabled='disabled';
				document.getElementById('sub_topic_select').disabled='disabled';
				document.getElementById('title_topic_select').disabled='disabled';
			
		} 
	}
		xmlHttp.open("GET",url + "&dt=" + dt)
		xmlHttp.send(null);
}

/*SHOW FLASH*/
function flashit(msg){
	var sp=document.getElementById? document.getElementById(msg) : document.all? document.all.myexample : "";
	if (sp){
	if (sp.style.borderColor.indexOf("green")!=-1)
	sp.style.borderColor="grey";
	else
	sp.style.borderColor="green";
	}
}

function goLocation(loc,combo){
	var uri = new Object();
	var obj=getURL(uri);
	var sel =combo.options[combo.selectedIndex].value;
	if(loc=='sub')
		window.location.href=obj.file+'?choose=subtopic&mid='+sel;
	else if(loc=='title')
		window.location.href=obj.file+'?choose=title&sid='+sel;
		
}

function removeHTMLTags(content){
 	if(content){
 		var strInputCode = content;
 		/* 
  			This line is optional, it replaces escaped brackets with real ones, 
  			i.e. < is replaced with < and > is replaced with >
 		*/	
 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
		//IF THE CONTENT HAS IMAGE OR FLASH ONLY
		// RISK:- will be a pbm when counting the length in validation
		if((strInputCode.indexOf("<img") > -1) || (strInputCode.indexOf("<embed") > -1) || (strInputCode.indexOf("<input") > -1)|| (strInputCode.indexOf("<textarea") > -1) || (strInputCode.indexOf("<select") > -1)|| (strInputCode.indexOf("<object") > -1)){
			var txt = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
			if (txt.length==0) var strTagStrippedText = strInputCode;
			else var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");

		}
		else{	
			//OTHERWISE
			var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
		}
		//alert('befpre  '+strTagStrippedText);
 		 strTagStrippedText = trim(strTagStrippedText);
		strTagStrippedText = strTagStrippedText.replace('\r\n','');
		strTagStrippedText = strTagStrippedText.replace('\n','');
		strTagStrippedText = strTagStrippedText.replace('\t','');
		strTagStrippedText = strTagStrippedText.replace('\r','');
		strTagStrippedText = strTagStrippedText.replace(/\n/g, "");
		strTagStrippedText = strTagStrippedText.replace(/\r|\n|\r\n/g, "");
		strTagStrippedText = strTagStrippedText.replace(/&nbsp;/gi,'');
			
	/*	strTagStrippedText = strTagStrippedText.replace('&nbsp;','');
*/
 		//alert("Output text:" + strTagStrippedText);	
		return strTagStrippedText.length;
 	}	
}
/*validate*/
function validate_values(){
	var main = document.getElementById('main_topic_select');
	var subt = document.getElementById('sub_topic_select');
	var title = document.getElementById('title');
	var oEditor = FCKeditorAPI.GetInstance( 'objFCK' ) ;
	var cont = oEditor.GetXHTML(true) ;
	var mc=trim(main.value);var sc=trim(subt.value);var tc=trim(title.value);

	if(trim(mc)=='default' || trim(mc)=='blank')
	{
		alert('Category Required!');
		main.focus();
		return false;
	}
	if(trim(sc)=='default' || trim(sc)=='blank')
	{
		alert('Question Required!');
		subt.focus();
		return false;
	}
	
	if(trim(tc)=='default' || trim(tc)=='blank')
	{
		alert('Title Required!');
		title.focus();
		return false;
	}
	
	//var x=document.getElementById('unique').value;
	var unique=document.getElementById('unique').value;
	if(unique>0){
		alert('The Template With The Same Title Already Exists in this Topic.\n Please Choose Another Topic or Title!');
		title.focus();
		return false;
	}
	
	var oEditor = FCKeditorAPI.GetInstance( 'objFCK' ) ;
	var cont = oEditor.GetXHTML(true) ;
	var len=removeHTMLTags(cont);
	
	if(len==0 || typeof(len)=='undefined'){
		alert('Content Required!');
		return false;
	}
	if(len>10000){
		alert('The Maximum allowed number of characters is 10000. Please reduce your content!');
		return false;
	}
	return true;
	
	
}
function validate_search(){
	var val=document.getElementById('Title').value;
	val=trim(val);
	if(val=='Please Enter your answer here...' || val==''){
		alert('Please enter the answer!');
		document.getElementById('Title').value='';
		document.getElementById('Title').focus();
		return false;
	}
	return true;
}
function reloadMe(combo,mid){
	//alert('reload');
	var sel =combo.options[combo.selectedIndex].value;
	if(sel=='blank') return false;
if(isItOk(combo)){
	var nme = combo.name;
	switch(nme){
		case 'main_topic_select':
			location.href='index.php?mid='+sel;
			break;
		case 'sub_topic_select':
			location.href='index.php?mid='+mid+"&sid="+sel;
			break;
	}
	return true;
	}return true;
}
function store_selected(){
	document.templates.sel_index.value=document.templates.title_topic_select.value;
	document.templates.sel_main.value=document.templates.main_topic_select.value;
	document.templates.sel_sub.value= document.templates.sub_topic_select.value;
}
function isItOk(combo){
	
	var page1 = document.getElementById('page1').value;
	//alert(page1);
	//if(page1!='' && page2!='' && page3!=''){
		var oEditor1 = FCKeditorAPI.GetInstance( 'objFCK' ) ;
		var cont1 = oEditor1.GetXHTML(true) ;
		var len1=removeHTMLTags(cont1);
							
		var now_sel = document.templates.title_topic_select.value;
		var mid = document.templates.main_topic_select.value;
		var sid = document.templates.sub_topic_select.value;

		
		if(typeof(len1)=='undefined') len1=0;if(page1=='undefined' || page1=='') page1=0; 
		//alert(page1 +' ' +len1 +'\n' +page2 +' '+ len2+'\n'+page3+' '+len3);
	
		if(len1!=page1){
				if(confirm('Do you want to save the changes?')){
					document.getElementById('isok').value=1;
					var which ='';
					if(typeof(combo)!='undefined') {
						if(combo.name=='sub_topic_select') which='sub';
						else if(combo.name=='main_topic_select') which='main';
						else which='title';
					}

					var prev =document.getElementById('sel_index').value;
					var prev_main =document.getElementById('sel_main').value;
					var prev_sub =document.getElementById('sel_sub').value;

					document.templates.sel_index.value=prev;
					document.templates.sel_main.value=prev_main;
					document.templates.sel_sub.value=prev_sub;
					//document.templates.title_topic_select.options[prev].selected='selected';
					select_index(document.templates.title_topic_select,prev);
					select_index(document.templates.main_topic_select,prev_main);
					select_index(document.templates.sub_topic_select,prev_sub);
					
					var uri = new Object();
					var obj=getURL(uri);
					document.templates.reload_url.value=obj.file+"?mid="+mid+"&sid="+sid+"&tid="+now_sel;
					if (which=='sub'){
						document.templates.reload_url.value=obj.file+"?mid="+mid+"&sid="+sid;
					}else if(which=='main'){
						document.templates.reload_url.value=obj.file+"?mid="+mid;
					}
					document.templates.Save.click();return false;
				}
		}
		return true;
	//}
	//return true;
				
}
function show_content(tid,raw){
	//alert('showcon');
	if(isItOk()){
	xmlHttp=GetXmlHttpObject()
	var url="General.php";
	url=url+"?tid="+tid+"&action=showContent";
	var dt = new Date( ).valueOf(); 
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request")
		 return
	}
	xmlHttp.onreadystatechange=function() 
	{ 	
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 	
			var response=xmlHttp.responseText;
			
			var str_arr = response.split("###DATA###");
			var oEditor1 = FCKeditorAPI.GetInstance( 'objFCK' ) ;
			
			

				//alert('idex '+document.templates.title_topic_select.selectedIndex);
			document.getElementById('sel_index').value=document.templates.title_topic_select.value;
			document.getElementById('sel_main').value=document.templates.main_topic_select.value;
			document.getElementById('sel_sub').value=document.templates.sub_topic_select.value;
			
			if(str_arr[0]!='' && str_arr[0]!=0 && typeof(str_arr[0])!='undefined'){
				document.getElementById('mode').value='edit';
				document.getElementById('temp_id').value=str_arr[0];
				
				var body1 = str_arr[2];
			

				//body1=unescape(body1);body2=unescape(body2);body3=unescape(body3);
				oEditor1.SetHTML(body1) ;
				var len1=removeHTMLTags(body1);
				
				/*SET LENGTHS*/
				document.getElementById('page1').value=len1;

			}else{
				
				clear_content('toggle');

			}

	document.getElementById('loading').style.display='none';
			//alert(body2);
			
		}
		if (xmlHttp.readyState==1 &&  xmlHttp.readyState!="complete")
		{
			document.getElementById('loading').style.display='block';
		} 
	}
		xmlHttp.open("GET",url + "&dt=" + dt)
		xmlHttp.send(null);
	}
}
/*CLEAR EDITOR*/
function clear_content(toggle,make){
	var oEditor1 = FCKeditorAPI.GetInstance( 'objFCK' ) ;

	oEditor1.SetHTML('') ;

			
	document.getElementById('mode').value='';
	document.getElementById('temp_id').value='';
	
	document.getElementById('page1').value=0;
	document.getElementById('disable_fck').value=0;
	
if(typeof(toggle)!='undefined') document.getElementById('Fck1').style.display='inline';
	
}
/*modified by jaya on Mar 18*/
function hide_div()
{
  document.getElementById('loading').style.display='none';
}
function delete_main(mid){
	  if(document.getElementById('msg'))
	    document.getElementById('msg').style.display='none';
	xmlHttp=GetXmlHttpObject()
	var url="../General.php";
	url=url+"?mid="+mid+"&action=del_main";
	var dt = new Date( ).valueOf(); 
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request")
		 return
	}
	xmlHttp.onreadystatechange=function() 
	{ 	
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 	
			var response=xmlHttp.responseText;
			document.getElementById('msgs').style.display = 'inline';
			document.getElementById('show_list').innerHTML=response;
		}
		if (xmlHttp.readyState==1 &&  xmlHttp.readyState!="complete")
		{} 
	}
		xmlHttp.open("GET",url + "&dt=" + dt)
		xmlHttp.send(null);
}
/*by JEYA KUMAR*/
//script for signin form
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
function signin_val(){
	var minLength = 8; // Minimum length
	var returnval=emailfilter.test(document.sign.uname.value);
	var pass = document.sign.pwd.value;
	var cpass = document.sign.cpwd.value;

	if (returnval==false){
		alert("Please Enter a Valid Email!");
		document.sign.uname.select();
		return false;
	}
	if(document.sign.uname.value == "")
	{
		alert("Please Enter Email!");
		document.sign.uname.focus();
		return false;
	}
	if(document.sign.pwd.value == "")
	{
		alert("Please Enter Password!");
		document.sign.pwd.focus();
		return false;
	}
	if (document.sign.pwd.value.length < minLength) 
	{
		alert('Your password must be at least ' + minLength + ' characters long. Try again.');
		document.sign.pwd.focus();
		return false;
	}
	if(document.sign.cpwd.value == "")
	{
		alert("please Enter Confirm Password!");
		document.sign.cpwd.focus();
		return false;
	}
	if(pass != cpass)
	{
		document.getElementById('errmsg').style.display = 'inline';
		document.getElementById('errmsg').innerHTML= 'Passwords not matching';
		document.sign.cpwd.focus();
		return false;
	}else if(pass==cpass) document.getElementById('errmsg').style.display = 'none';
	if(document.getElementById('exists').value!='0'){
			alert('This User Already Exists!');
			document.getElementById('uname').focus();
			return false;
		}
	return true;
}
//script onclicking cancel button
function cancel_val(){
location.reload(true);
}

/*Login form validation*/
function login_val(){
	if(document.userin.uname.value == "")
	{
		alert("Please Enter Email!");
		document.userin.uname.focus();
		return false;
	}
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var returnval=emailfilter.test(document.userin.uname.value);
	if(returnval==false){
		alert("Please Enter a Valid Email!");
		document.userin.uname.focus();
		return false;
	}
	if(document.userin.pwd.value == "")
	{
		alert("Please Enter Password!");
		document.userin.pwd.focus();
		return false;
	}
	return true;
}


/*Profile form validation*/
function profile_validate(){
	
	//Email validation.
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var returnval=emailfilter.test(document.profilefrm.uid.value);
	if (returnval==false){
		alert("Please enter a valid email address.");
		document.profilefrm.uid.select();
		return false;
	}
	
		var invalid = " ";
		var minLength = 8;
		
		//Added By Monimekalai June 29-09
		var pass = document.profilefrm.pwd.value;
		var cpass = document.profilefrm.cpwd.value;
		//End Monimekalai.
		var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

		  for (var i = 0; i < document.profilefrm.fname.value.length; i++) {
			if (iChars.indexOf(document.profilefrm.fname.value.charAt(i)) != -1) {
			alert ("Your First Name has special characters. These are not allowed.\n Please remove them and try again.");
			return false;
			}
		  }
		    if(document.profilefrm.fname.value == "")
			{
			alert("please enter firstname!");
			document.profilefrm.fname.focus();
			return false;
		}

		
		/*Apr-14-Jeyakumar*/
		if (document.profilefrm.fname.value.indexOf(invalid) > -1) {
			alert("Sorry,Initially spaces are not allowed for Firstname.");
			document.profilefrm.fname.focus();
			return false;
			}
			/*------------*/
		 if(document.profilefrm.pwd.value!=''){
			 if (document.profilefrm.pwd.value.indexOf(invalid) > -1) {
			alert("Sorry, spaces are not allowed for password.");
			document.profilefrm.pwd.focus();
			return false;
			}
			else if(document.profilefrm.pwd.value.length < 8 ){
			alert("Password must be atleast 8 characters long!");
			document.profilefrm.pwd.focus();
			return false;
			}
		}
		//Added By Monimekalai June23-09
		if(pass!=cpass)
		{
			alert("Password Not Match...");
			document.profilefrm.cpwd.focus();
			return false;
		}
		//End Monimekalai.
			/*if (document.profilefrm.pwd.value.indexOf(invalid) > -1) {
			alert("Sorry, spaces are not allowed for Password.");
			document.profilefrm.pwd.focus();
			return false;
			}*/
	return true;
}
	/*END JEYA KUMAR*/
/*JEYA KUMAR*/

//Script for Forget Password-Mar26
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
function forget_val(){
	var returnval=emailfilter.test(document.forget.uname.value);
	if (returnval==false){
		alert("Please enter a valid email address.");
		document.forget.uname.select();
		return false;
	}
	if(document.forget.uname.value == "")
	{
		alert("please enter emailid!");
		document.forget.uname.focus();
		return false;
	}
	return true;
}
	/*END*/
	function show_tip(text){
		document.getElementById(text).style.display='inline';
	}
	function hide_tip(text){
		document.getElementById(text).style.display='none';
	}
	
//Added by jeyakumar
//script for ContactUs form

function contact_val(){
	var returnval=emailfilter.test(document.contact.email.value);
	if (returnval==false){
		alert("Please enter a valid email address.");
		document.contact.email.select();
		return false;
	}
	if(document.contact.email.value == "")
	{
		alert("please enter emailid!");
		document.contact.email.focus();
		return false;
	}
	var feed = trim(document.contact.feedback.value);
 	if (feed == "" )
	{
		alert("please enter feedback!");
		document.contact.feedback.focus();
		return false;
	}
	return true;
}

//Textfield Limit-Jeyakumar-Apr03
function CountLeft(field, count, max) {
if (field.value.length > max)
field.value = field.value.substring(0, max);
else
count.value = max - field.value.length;
}

function CountLeft(field, count, max) {
if (field.value.length > max)
field.value = field.value.substring(0, max);
else
count.value = max - field.value.length;
}
//script for Hide&Display-Jeyakumar-Apr09
function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}
//script for Hide&Display-Jeyakumar-Apr09
function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none") 
{ document.getElementById(d).style.display = "block"; }
else 
{ document.getElementById(d).style.display = "none"; }
}
/*Jeyakumar-Apr16*/
/*For password*/
function changeBox()
 {
    document.getElementById('div1').style.display='none';
    document.getElementById('div2').style.display='';
    document.getElementById('pwd').focus();
 }
 function restoreBox()
 {
    if(document.getElementById('pwd').value=='')
    {
      document.getElementById('div1').style.display='';
      document.getElementById('div2').style.display='none';
    }
 }
/*For Confirm password*/
function changeCBox()
 {
    document.getElementById('div3').style.display='none';
    document.getElementById('div4').style.display='';
    document.getElementById('cpwd').focus();
 }
 function restoreCBox()
 {
    if(document.getElementById('cpwd').value=='')
    {
      document.getElementById('div3').style.display='';
      document.getElementById('div4').style.display='none';
    }
 }
/*Jeyakumar-Apr16-End*/
//script onclicking cancel button-Apr14-Jeyakumar
function cancel_val(){
//location.reload(true);
window.location = "index.php";
}