// Global Variables
var bool = false;
var globalErrorString = "";
var globalDivID = "";
var xmlHttp;

//------------ ajax portion (start) -------------//
function GetXmlHttpObject(handler) {
var objXmlHttp=null;

if (navigator.userAgent.indexOf("Opera")>=0) {
	alert("This example doesn't work in Opera");
	return;
}

if (navigator.userAgent.indexOf("MSIE")>=0) {

	var strName="Msxml2.XMLHTTP";

	if (navigator.appVersion.indexOf("MSIE 5.5")>=0) {
		strName="Microsoft.XMLHTTP";;
	}

	try {
		objXmlHttp=new ActiveXObject(strName);
		objXmlHttp.onreadystatechange=handler;
		return objXmlHttp;
	}
	catch(e) {
		alert("Error. Scripting for ActiveX might be disabled");
		return;
	}
}

if (navigator.userAgent.indexOf("Mozilla")>=0) {
	objXmlHttp=new XMLHttpRequest();
	objXmlHttp.onload=handler;
	objXmlHttp.onerror=handler;
	return objXmlHttp;
	}
}

//------------ ajax portion (end) -------------//
function valid(f){

	var i=0, q = true, t;
	var errorString = "";

	while(t=f.elements[i++]){
	var str = t.name+"_label";
	if((t.type == 'text' || t.type == 'password') && !str.match(/phonenumber/) && t.name != "address2") {
		try {
			document.getElementById(str).className = "profileStyleLabel";
		} catch(e) {
			alert(str+": "+e);
		}
	}

	 	if((t.type == 'text' || t.type == 'password') && t.value.length < 1 && t.name != "address2") {
			q=false;

		 	if(!str.match(/phonenumber/)) {
		 		if(document.getElementById(str).className == "profileStyleLabel") {
					document.getElementById(str).className = "profileStyleLabel_lighted";
				}
			}

				//alert(str+": "+document.getElementById(str).className);
				errorString = '<div class="boxcontent">Please enter values on the required fields</div>';
			}
		}

	if(validate_email(document.agentRegister_form.emailAddress)) {
	 	document.getElementById("emailAddress_label").className = "profileStyleLabel";
	} else {
		q = false;
	 	document.getElementById("emailAddress_label").className = "profileStyleLabel_lighted";
		errorString += '<div class="boxcontent">Invalid Email Address</div>';
	}

	if(document.agentRegister_form.c_password.value != document.agentRegister_form.password.value) {
		errorString += '<div class="boxcontent">Password Fields are not identical</div>';
		document.agentRegister_form.c_password.value = "";
		document.agentRegister_form.password.value = "";
		document.getElementById("c_password_label").className = "profileStyleLabel_lighted";
		document.getElementById("password_label").className = "profileStyleLabel_lighted";
		q = false;
	} else {
		 if(document.agentRegister_form.c_password.value.length > 0 && document.agentRegister_form.password.value.length > 0) {
			document.getElementById("c_password_label").className = "profileStyleLabel";
			document.getElementById("password_label").className = "profileStyleLabel";
		}

	}

	if(document.agentRegister_form.d_phonenumber1.value.length < 1 || document.agentRegister_form.d_phonenumber2.value.length < 1 || document.agentRegister_form.d_phonenumber3.value.length < 1) {
		document.getElementById("d_phonenumber_label").className = "profileStyleLabel_lighted";
		q = false;
	} else {
		document.getElementById("d_phonenumber_label").className = "profileStyleLabel";
	}



	//alert(q);
	if(q){
		//alert("CORRECT");
		return true;
	} else {
		document.getElementById("errorbox").innerHTML = '<div class="headerError">Error:</div>'+errorString;
		document.getElementById("errorbox").style.backgroundColor = "#435e20";
		return false;
	}
}


function validate_email(field){

	with (field) {
	 	if(value==null || value=="") {
			return false;
		}
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");

		if (apos<1||dotpos-apos<2) {
		 	return false;
		 } else {
		  	return true;
		}
	}
}

function getRegistrationInfo() {
  var infoString = "";

  if(document.agentRegister_form.firstname.value.length > 0) {
		infoString += "firstname="+document.agentRegister_form.firstname.value+"&";
	}
  if(document.agentRegister_form.lastname.value.length > 0) {
		infoString += "lastname="+document.agentRegister_form.lastname.value+"&";
	}
  if(document.agentRegister_form.password.value.length > 0) {
		infoString += "password="+document.agentRegister_form.password.value+"&";
	}
  if(document.agentRegister_form.emailAddress.value.length > 0) {
		infoString += "emailAddress="+document.agentRegister_form.emailAddress.value+"&";
	}
   if(document.agentRegister_form.companyname.value.length > 0) {
		infoString += "companyname="+document.agentRegister_form.companyname.value+"&";
	}	
  if(document.agentRegister_form.country.value.length > 0) {
   		infoString += "country="+document.agentRegister_form.country.value+"&";
	}
	
	if(document.agentRegister_form.stateOthers && (document.agentRegister_form.stateOthers.value.length > 0)) {
		infoString += "state="+document.agentRegister_form.stateOthers.value+"&";
	} else {
		if(document.agentRegister_form.state.value.length > 0) {
	   		infoString += "state="+document.agentRegister_form.state.options[document.agentRegister_form.state.selectedIndex].value+"&";
		}		
	}
 	
	if(document.agentRegister_form.cityOthers && (document.agentRegister_form.cityOthers.value.length > 0)) {
		infoString += "city="+document.agentRegister_form.cityOthers.value+"&";
	} else {
		infoString += "city="+document.agentRegister_form.city.options[document.agentRegister_form.city.selectedIndex].value+"&";
	}
	
 
  if(document.agentRegister_form.address1.value.length > 0) {
   		var addressStr = document.agentRegister_form.address1.value+" "+document.agentRegister_form.address2.value;
		infoString += "address="+addressStr+"&";
	}
  if(document.agentRegister_form.zipcode.value.length > 0) {
		infoString += "zipcode="+document.agentRegister_form.zipcode.value+"&";
	}

  if(document.agentRegister_form.imageBox.value.length > 0) {
		infoString += "imageBox="+document.agentRegister_form.imageBox.value+"&";
	}

	var profession_var = "profession=";	
	for(var i = 0; i < document.agentRegister_form.profession.length; i++) {
	  	if(document.agentRegister_form.profession[i].checked) {
		 	profession_var += document.agentRegister_form.profession[i].value+",";
		}
	}
  
  infoString += profession_var+"&";
  	
  var d_phoneNumber = document.agentRegister_form.d_phonenumber1.value+"-"+document.agentRegister_form.d_phonenumber2.value+"-"+document.agentRegister_form.d_phonenumber3.value;
  infoString += "d_phoneNumber="+d_phoneNumber+"&";
  var n_phoneNumber = document.agentRegister_form.n_phonenumber1.value+"-"+document.agentRegister_form.n_phonenumber2.value+"-"+document.agentRegister_form.n_phonenumber3.value;
  infoString += "n_phoneNumber="+n_phoneNumber;

  return infoString;
}

function submitInformation(strInput, strURL, divName) {
	var url=strURL+"?"+strInput;
	document.getElementById(divName).innerHTML="";
	document.getElementById("errorbox").innerHTML='<span style="font-size:8pt">Please wait...</span><br /><br /><br /><br /><br /><br /><br /><br /><br /><img name="loading" id="loading" src="images/ajax-loader.gif"><br /><br /><br /><br /><br />';
	globalDivID = "errorbox";
	xmlHttp=GetXmlHttpObject(processInfo);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}

function processInfo() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById(globalDivID).innerHTML=xmlHttp.responseText;
	}
}

function patternCheck(e){
	if (e == null) e = window.event;
	var key = (e.which)?e.which:e.keyCode

	if ((key >=48 && key <=57)||(key == 9)||(key == 8)){
		//alert("good" + key);
		return true;
	}else{
	//alert("You can only enter valid characters in this field:\n\n Numbers: 0-9");
		return false;
	}

}

function advance(currentField,nextField) {
    if(currentField == 'd_phonenumber3' || currentField == 'n_phonenumber3'){
		if(document.getElementById(currentField).value.length==4){
			document.getElementById(nextField).focus();
		}
	}else{
		if(document.getElementById(currentField).value.length==3){
			document.getElementById(nextField).focus();
		}
	}

}

function addOption(selectbox, value, text, is_selected) {
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	optn.id = value+"ID";
	if (is_selected == 1)
		optn.selected = true;

	selectbox.options.add(optn);
}

function populateDropdown(strInput, strURL, divname) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    try {
		document.getElementById(divname).innerHTML = '<center><div style="text-align: center; vertical-align: middle; background: #D3DEF4; color: black;">please wait... <img name="loading" id="loading" src="images/ajax-loader-radar.gif"></div></center>';
 	} catch(e) {
	 	alert(e);
 	}

    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {

	        document.getElementById(divname).innerHTML = self.xmlHttpReq.responseText;
        }
    }

    self.xmlHttpReq.send(strInput);

}

function handleAjax(strInput, strURL, divname) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    try {
		document.getElementById(divname).innerHTML = '<center><div style="text-align: center; vertical-align: middle; background: #D3DEF4; color: black;">please wait... <img name="loading" id="loading" src="images/ajax-loader-radar.gif"></div></center>';
 	} catch(e) {
	 	alert(e);
 	}

    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {

	        document.getElementById(divname).innerHTML = self.xmlHttpReq.responseText;
        }
    }

    self.xmlHttpReq.send(strInput);

}

function detectOnChange(dropdown, divID, nameVar) {

	if(dropdown.value == 0) {
		document.getElementById(divID).innerHTML="<span style='font-size: 10px!important; color: white;'>Specify:</span> <input type='text' name='"+nameVar+"' id='"+nameVar+"' style='width: 100px;' />";		
		dropdown.disabled="true";
	} else {
		document.getElementById(divID).value = "";
		document.getElementById(divID).innerHTML="";
	}
		
}

function checkEmailField(strInput, strURL, divname) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    try {		
		document.getElementById(divname).innerHTML = '<img name="loading" id="loading" src="images/ajax-loader-gallery.gif">';    
 	} catch(e) {
	 	alert(e);
 	}
    

    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {

	        document.getElementById(divname).innerHTML = self.xmlHttpReq.responseText;
        }
    }
    self.xmlHttpReq.send("add="+strInput);    
}
