function addCustomSection(deftitle, deftext){
	csecs++; ftswitches++; helps++; if(deftitle=='') deftitle = 'Title';
	content = '<table cellpadding="0" cellspacing="0" align="center" border="0"><tr valign="bottom"><td align="left">&nbsp;&nbsp;&nbsp;&nbsp;<input class="textbox" type="text" id="ctitle'+csecs+'" name="ctitle[]" value="'+deftitle+'" size="60" /></td><td class="help" align="left" id="help'+helps+'"></td><td align="right"><a class="ftclosebtn" onclick="removeCustomSection('+csecs+')" title="Remove Section">&nbsp;</a></td></tr><tr valign="top"><td  align="center" style="padding-left:10px" colspan="3"><textarea class="cketa" id="ctext'+csecs+'" name="ctext[]">'+deftext+'</textarea></td></tr></table>';
	var tbody = document.getElementById("ctxttable").getElementsByTagName("TBODY")[0];
	var row = document.createElement("TR"); var td = document.createElement("TD"); 
	td.appendChild(document.createTextNode(''));
	td.id = "ctxtcell"+csecs;
	row.appendChild(td);
	tbody.appendChild(row);
	document.getElementById("ctxtcell"+csecs).innerHTML = content;
	loadFormDiv('div.ftdiv');
	getCKEditor('ctext'+csecs,'1',640,160,false);
	hideFTE();
}
function removeCustomSection(cid){
	document.getElementById("ctitle"+cid).value = "";
	document.getElementById("ctext"+cid).value = "";
	document.getElementById("ctxtcell"+cid).style.display = "none";
	hideFTE();
}
function invalidateMe(){
	if(document.getElementById("successbox")) document.getElementById("successbox").style.display = "none";
	if(document.getElementById("failurebox")) document.getElementById("failurebox").style.display = "none";
	if(document.getElementById("invalidmsg")) document.getElementById("invalidmsg").style.display = "block";
}
function showFTE(refid, msg, tb){
	clearer(0); invalidateMe(); 
	if(document.getElementById(refid) && document.getElementById("ftetable"))
   	{
		myPos = findPos(document.getElementById(refid)); x = myPos[0];  y = myPos[1];
		document.getElementById("ftetable").style.left = x+"px"; document.getElementById("ftetable").style.top = y+"px";
		document.getElementById("ftebody").innerHTML = msg; 
		document.getElementById("ftetable").style.display = "block"; 
		if(tb!="" && tb!= null && tb != "undefined")
			document.getElementById(tb).className = "tberror";
   }
   else alert(msg); 
   // do not add control.focus() fucntion here coz, it hides the FTE bubble
}
function hideFTE(){
	if(document.getElementById("ftetable")) document.getElementById("ftetable").style.display = "none";
}
function toggleFtSwitch(c) {
	if(document.getElementById("ftsdiv"+c).style.display == "block")
	{ document.getElementById("ftswitch"+c).className = 'ftswitchoff'; document.getElementById("ftsdiv"+c).style.display = "none"; }
	else 
	{ document.getElementById("ftswitch"+c).className = 'ftswitchon'; document.getElementById("ftsdiv"+c).style.display = "block"; }
	hideFTE();
}
function extClearer(ch,count){
	hideFTE();
	if(ch==0) for(i=1;i<=count;i++) { document.getElementById("help"+i).innerHTML = ""; }
	else { document.getElementById("help"+ch).innerHTML = "";}		
}
/******** client side validations ***********/
function isEmailId(control,isnotnull,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str=='') { showFTE(helpid,'Email-Id cannot be empty',control); return false; } }
	if(str!='')
	{
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!(str.match(emailRegEx))) { invalidateMe(); showFTE(helpid,'Email-Id is not valid',control); return false; }
		if(isBadJack(str)==true) { invalidateMe(); showFTE(helpid,'Email-Id is not appropriate',control); return false; }
	}
	return true;
}
function isPassword(pwd,pwd1,helpid) {
	p = document.getElementById(pwd).value; p1 = document.getElementById(pwd1).value;	
	if(p == ""){ showFTE(helpid,'Password cannot be empty',pwd); return false; }
	if(p.length<6){ showFTE(helpid,'Password needs minimum 6 characters',pwd); return false;}
	for(i=0;i<=p.length;i++) if(p.charAt(i)==' '){ showFTE(helpid,'Password should not contain spaces',pwd); return false; }	
	if(p!=p1){ showFTE(helpid,'Passwords did not match',pwd); return false; }	
	return true;
}
function isFirstName(control,isnotnull,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str.length<3){ showFTE(helpid,'First name should have minimum 3 letters',control); return false; } }
	if(isBadJack(str)==true) { showFTE(helpid,'First name is inappropriate', control); return false; }
	for(i=0;i<str.length;i++)
		if(!((str.charAt(i)>="a" && str.charAt(i)<="z")||(str.charAt(i)>="A" && str.charAt(i)<="Z")||str.charAt(i)==" "))
	{ showFTE(helpid,'First name allows alphabets and spaces only',control); return false; break; } 
	return true;
}
function isLastName(control,isnotnull,helpid) {
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str=="") { showFTE(helpid,'Last name cannot be empty',control); return false;} }
	if(isBadJack(str)==true) { showFTE(helpid,'Last name is inappropriate',control); return false; }
	for(i=0;i<str.length;i++)
	if(!((str.charAt(i)>="a" && str.charAt(i)<="z")||(str.charAt(i)>="A" && str.charAt(i)<="Z")||str.charAt(i)==" "))
	{ showFTE(helpid,'Last name allows alphabets and spaces only',control); return false; }
	return true;
}
function isMobile(control,isnotnull,helpid){
	num = trim(document.getElementById(control).value); document.getElementById(control).value = num;
	if(isnotnull==1) { if(num=="") { showFTE(helpid,'Mobile number cannot be empty',control); return false;} }
	if(parseInt(num)<=0) { showFTE(helpid,'Mobile number is not valid',control); return false; }		
	if(num!="") { if(num.length<10) { showFTE(helpid,'Mobile number should have 10 digits',control); return false; } }
	for(var i=0;i<num.length;i++)	
	if(!((num.charAt(i)>="0" && num.charAt(i)<="9"))) {showFTE(helpid,'Mobile number accepts numbers only',control); return false; } 
	return true;
}
function isIName(control,isnotnull,ins,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str.length<5){ showFTE(helpid, ins+' name cannot be empty or too small',control); return false; } }
	if(isBadJack(str)==true) { showFTE(helpid, ins+' name is inappropriate', control); return false; }
	return true;
}
function isFullName(control,isnotnull,label,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str.length<3){ showFTE(helpid,label+' name cannot be empty or too small',control); return false; } }
	if(isBadJack(str)==true) { showFTE(helpid,label+' name is inappropriate', control); return false; }
	for(i=0;i<str.length;i++) 
		if(!((str.charAt(i)>="a" && str.charAt(i)<="z")||(str.charAt(i)>="A" && str.charAt(i)<="Z")||str.charAt(i)==" "||str.charAt(i)=="."))
	  		{ showFTE(helpid,label+' allows alphabets, spaces, and "." only',control); return false; }
	
	return true;
}
function isAddress(control,isnotnull,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str.length<5){ showFTE(helpid,'Address cannot be empty or too small',control); return false; } }
	if(isBadJack(str)==true) { showFTE(helpid,'Address is inappropriate', control); return false; }
	return true;
}
function isInteger(control,isnotnull,minc,label,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) if(str.length=='' || parseInt(str)==0) { showFTE(helpid,label+' cannot be empty or null',control); return false; } 
	if(minc>0) if(str.length<minc){ showFTE(helpid,label+' cannot be less than '+minc+' digits',control); return false; } 		
	for(i=0;i<str.length;i++) if(!(str.charAt(i)>="0" && str.charAt(i)<="9")) { showFTE(helpid,label+' allows numbers only',control); return false; }
	return true;
}
function isPhones(control,isnotnull,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str.length<10){ showFTE(helpid,'Telephone(s) cannot be empty or less than 10 digits',control); return false; } }
	for(var i=0;i<str.length;i++) if(!((str.charAt(i)>="0" && str.charAt(i)<="9")||str.charAt(i)=="-"||str.charAt(i)==" "||str.charAt(i)==",")) 
	{ showFTE(helpid,'Telephone(s) allows numbers, spaces, "-" and "," only',control); return false; }
	return true;
}
function isWebsite(control,isnotnull,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str.length<6){ showFTE(helpid,'Webiste cannot be empty or less than 6 letters',control); return false; } }
	if(str!='')
	{
		filter = /^(((www.){1}))[-a-zA-Z0-9@:%_\+.~#?&]+\.([A-Za-z]{2,5})$/;
		if(!filter.test(str)) { showFTE(helpid,'Webiste name is not valid',control); return false; } 
	}
	return true;
}	
function isDesc(control,label,isnotnull,helpid,maxlen){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str=='') { showFTE(helpid,label+' cannot be empty',control); return false; } }
	if(isBadJack(str)==true) { showFTE(helpid,label+' is inappropriate', control); return false; }
	if(maxlen>0) { if(str.length>maxlen) { showFTE(helpid,label+' allows '+maxlen+' letters only', control); return false; } }
	return true;
}
function isMoney(control,isnotnull,label,size,helpid){
	str = trim(document.getElementById(control).value); document.getElementById(control).value = str;
	if(isnotnull==1) { if(str=='' || parseFloat(str)<=0) { showFTE(helpid,label+' cannot be empty or less than zero',control); return false; } }
	if(str!='')
	{
		var curRegEx = /^\d+(?:\.\d{0,2})?$/ ;
		if(!(str.match(curRegEx))) { showFTE(helpid,label+' is not valid',control); return false; }
	}
	return true;
}	
function taMaxLen(Object, MaxLen) { return (Object.value.length <= MaxLen); }
function trim(str, chars) { return ltrim(rtrim(str, chars), chars); }
function ltrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); }
function rtrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("[" + chars + "]+$", "g"), ""); }
/******** this script is for form text boxes events ***********/
function tbEvents(frm) {
	for (i=0;i<frm.elements.length;i++)
	{
		obj = frm.elements[i]; 
		if (obj.type == "text" || obj.type == "password" || obj.type == "file" || obj.type == "textarea") 
		{
			obj.className = "textbox";
			if (obj.addEventListener) { obj.addEventListener("focus",tbFocus,true); obj.addEventListener("blur",tbBlur,true); obj.pos = i; }
			else { obj.attachEvent("onfocus",tbFocus); obj.attachEvent("onblur",tbBlur); obj.pos = i; }
		}
	}
}
function tbFocus(e){ if(!e) e=window.event; ref=(e.target) ? e.target : e.srcElement; ref.className = "tbfocus"; hideFTE(); } //helper(ref.pos);
function tbBlur(e){ if(!e) e=window.event; ref=(e.target) ? e.target : e.srcElement; ref.className = "textbox"; }
/******** swap SUI Controls ***********/

function toggleSUISelect(sno,hide) {
	var lst = document.getElementById('suiselst'+sno); if(hide=='undefined') hide = '';
	if(hide==1) { document.getElementById('suiselst'+sno).style.display = "none"; return true; }
	if(lst.style.display == 'none' || lst.style.display == '')
	{
	
		var otherdivs = document.getElementsByTagName('div'); 
		for(i=0;i<otherdivs.length;i++) { if(otherdivs[i].className == 'suiselst') otherdivs[i].style.display = 'none'; }
	
		lst.style.width = (parseInt(document.getElementById('suisel'+sno).offsetWidth) - 2) + 'px';
		lst.style.left = document.getElementById('suisel'+sno).style.left;
		lst.style.top = document.getElementById('suisel'+sno).style.top;
		
		if(!document.getElementById('suiclose'+sno))
		{
			footer = document.getElementById("suiselstfoot"+sno);
			closer = '<a title="Close" class="suiselclose" id="suiclose'+sno+'" onclick="toggleSUISelect('+sno+',\''+hide+'\')">&nbsp;X&nbsp;</a>';
			if(footer)
			{
				
				footer.innerHTML = footer.innerHTML + closer;
			}
			else 
			{
				str = '<div class="suiselstfoot" id="suiselstfoot'+sno+'">'+closer+'</div>';
				lst.innerHTML = lst.innerHTML + str;
			}
		}
		lst.style.display = 'block';	
		
	}
	else 
	{
		document.getElementById('suiselst'+sno).style.display = "none";	
	}
	hideFTE();
}
function selectSUIItem(sno,control,selitem,txt,sel) {
	document.getElementById('suisel'+sno).innerHTML = txt;
	document.getElementById('suiselst'+sno).style.display = "none";
	document.getElementById(control).value = selitem;
	try { document.getElementById(control).onclick(); document.getElementById(control).click(); } catch(err) { }
}
function selectSUIAllItems(sno,tctrl,sctrl,icount,selitem,txt) {
	for(i=1;i<=icount;i++) { document.getElementById(sctrl+i).checked = true; } selectSUIItem(sno,tctrl,String(selitem),txt);
}
function toggleSUIItem(itemctrl,sno) { var c = document.getElementById(itemctrl+sno); if(c) { if(c.checked == 0) c.checked = 1; else c.checked = 0; } }
function doneSUIItems(tctrl,sctrl,sno,icount,hide) {
	toggleSUISelect(sno,hide); var comma = false;
	var c = document.getElementById(sctrl); var str=''; var vals='';
	
	for(i=1;i<icount;i++)
	{
		if(document.getElementById(sctrl+i).checked==1)
		{
			if(comma) { vals = vals + ','; str = str + ', '; } else comma = true;		
			vals = vals + (document.getElementById(sctrl+i).value);
			str = str + (document.getElementById(sctrl+i).title);
		}
	}
	if(str=='') str = '-- '+document.getElementById('suisel'+sno).title+' --';	
	document.getElementById(tctrl).value = vals;
	document.getElementById('suisel'+sno).innerHTML = str.substring(0,80);
}
