// JavaScript Document file for common js functions
var cp = new cpaint();

cp.set_transfer_mode('get');
cp.set_response_type('text');
cp.set_async(true);
cp.set_debug(0);

//////////HandlesOtherFieldOption
function checkSelectOther(field1,filed2)
{	
		field1=$(field1).options[$(field1).selectedIndex].text;
		if (field1 == "Others")
		{
			$(filed2).style.display = 'block';
		}
		else
		{
			$(filed2).style.display = 'none';
		}
}

function showWait(layername){

   //////////show the loading message/////////////////////////////////////////////////////////////////////////////////////////// 
   document.getElementById(layername).innerHTML="<img src='../images/indicator.gif' width='16' height='16' /> Processing data...";
   document.getElementById(layername).style.display = 'block';
   /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

function hideWait(layername){

	document.getElementById(layername).style.display = 'none';
}
////////////////////////to open a url via ajax request/////////////////////////////////////////////////////////
function opUrl(url,target,msg) {

$(target).innerHTML = "<div style='height:100px; margin-top:50px;'><img src='images/progressbar.gif' width='195' height='20'><br /><span class='maroon12'>"+msg+"</span></div>";

if ($(target).style.display='none')
$(target).style.display='';
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = function() {openUrlDone(target);};
req.open("GET", url, true);
req.send(null);
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = function() {openUrlDone(target);};
req.open("GET", url, true);
req.send();
}
}
}   

function openUrlDone(target) {
if (req.readyState == 4) {        
if (req.status == 200) {
var retStr = req.responseText;
if (retStr == "LOGGEDUSER NOT FOUND")
document.forSess.submit();
else
{
$(target).innerHTML = retStr;
}
} else {
$(target).innerHTML="openUrl error:\n" +
req.statusText;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////$ sign used for document.getElementById///////////////////////////////////////////////////////////////////////
function $(id){
var o = document.getElementById(id);
if (!o){
alert("Error in $(): "+id+" Not Found!", false);
return false;
}

return o;	
}
///////////////////////////////////////////////////////////
/////////////////hide the control passed///////////////////
function hide(ctrl){
	
document.getElementById(ctrl).style.display='none';
	
}
//////////////////////////////////////////////////////////

// Charactor Count Function

function textKey(form,element)
{
	supportsKeys = true
	calcCharLeft(form,element)
}
function calcCharLeft(form,element)
{
		clipped = false
		maxLength = 600
        if (element.value.length > maxLength) { 
	        element.value = element.value.substring(0,maxLength)
		    charleft = 0
		    clipped = true
        } 
		else 
		{
			charleft = maxLength - element.value.length
		}
        form.charsLeft.value = charleft
        return clipped
}

////////////////////////////////////////////////////////////////////////////
function get_city(value,pageUrl,combo,spanLoading,spanResult)
{
	showWait(spanLoading);
	var countryid = value;
  	cp.call(pageUrl, 'get_city', city_results, countryid, combo, spanLoading, spanResult);
}
function city_results(result) {

	var arrResult = result.split('||');
	
	var output = arrResult[0];
	var spanLoader = arrResult[1];
	var spanResult = arrResult[2];
	
	/////////hide the loading message/////////////////////
	document.getElementById(spanLoader).innerHTML="Done...";
	document.getElementById(spanLoader).style.display = 'none';
	//////////////////////////////////////////////////////
	//var x=result.documentElement; //store the result in var
	//var text=x.childNodes[0].childNodes[0].nodeValue;
	document.getElementById(spanResult).innerHTML = output;

}


function get_city_name(value,pageUrl,combo,spanLoading,spanResult)
{
	showWait(spanLoading);
	var countryid = value;
  	cp.call(pageUrl, 'get_city_name', city_results_name, countryid, combo, spanLoading, spanResult);
}
function city_results_name(result) {

	var arrResult = result.split('||');
	
	var output = arrResult[0];
	var spanLoader = arrResult[1];
	var spanResult = arrResult[2];
	
	/////////hide the loading message/////////////////////
	document.getElementById(spanLoader).innerHTML="Done...";
	document.getElementById(spanLoader).style.display = 'none';
	//////////////////////////////////////////////////////
	//var x=result.documentElement; //store the result in var
	//var text=x.childNodes[0].childNodes[0].nodeValue;
	document.getElementById(spanResult).innerHTML = output;

}
/////////////////////open new browser window////////////////////////////////////////////////
function openWindow(theURL,winName)
{
window.open(theURL,winName,"scrollbars=yes,maximize=no,menubar=no,resizeable=no,height=500,width=740'");
}
/////////////////////////////search section///////////////////////////////////////////
function doSearch(searchMode){
	
	if (searchMode=='simple')///simple mode
	{
		if (document.getElementById('searchadvance').style.display='block')
			document.getElementById('searchadvance').style.display='none'
		
		document.getElementById('searchsimple').style.display='block';
	}
	else////advance mode
	{
		if (document.getElementById('searchsimple').style.display='block')
			document.getElementById('searchsimple').style.display='none'
		
		document.getElementById('searchadvance').style.display='block';
	
	}
}
/////////////////////////////////////////////////////////////////////////////////
function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}

//////////////////////email validation check///////////////////////////////////
function checkemail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		  // alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   // alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   // alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   // alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   // alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   // alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
////////////////////Show error in span///////////////////////////////
function showError(str){
	
	$('messageStep1').style.background = "#FF0000";
	$('messageStep1').style.color = "#FFFFFF";
	$('messageStep1').style.display = "block";
	$('messageStep1').innerHTML = '&nbsp;'+str;
	
}
///////////////email validity check/////////////////////
function checkemail(emailadd){
	var testresults
	
	var str = emailadd;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		
	if (filter.test(str))
		testresults=true
	else{
		testresults=false
	}
	return (testresults)
}

function emailSpecific(emailadd){
	
	var extcheck = true;
	
	var invalidaddress=new Array()
	invalidaddress[0]="hotmail"
	invalidaddress[1]="gmail"
	invalidaddress[2]="yahoo"
	
	var ext=new Array()
	ext[0]="com"
	ext[1]="com"
	ext[2]="com"
	
	var tempstring=emailadd.split("@")
	tempstring=tempstring[1].split(".")
	
	
	for (i=0;i<invalidaddress.length;i++){
	
	if (tempstring[0]==invalidaddress[i]){ //if its hotmail or yahoo or gmail add then do next step

		if ((tempstring[0]==invalidaddress[i]) && (tempstring[1]==ext[i]))
		{	
			extcheck=true;
			break;
		}
		else
			extcheck=false;
		}
	}
	return extcheck;	
}