// Get base url
url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);

var  http_req;

function fnValAgentReg(frmName)
{
	with(document.forms[frmName])
	{
		if(trimAll(txtAgentFName.value) == ""){
			alert("Please enter your first name!");
			txtAgentFName.focus();
			return false;
		}else if(trimAll(txtAgentEmail.value) == ""){
			alert("Please enter agent email address!");
			txtAgentEmail.focus();
			return false;			
		}else 	if(!validateEmail( trimAll(txtAgentEmail.value))) {
			alert("Please enter agent valid email address!");
			txtAgentEmail.focus();
			return false;			
		}else if(trimAll(txtPassword.value) == ""){		
			alert("Please enter agent password.");
			txtPassword.focus();
			return false;
		}else if(trimAll(txtPassword.value).length < 6){		
			alert("Please enter agent password minimum 6 characters.");
			txtPassword.focus();
			return false;
		}else if(trimAll(txtCPassword.value) == ""){
			alert("Please enter agent confirm password.");
			txtCPassword.focus();
			return false;
		}
		else if(trimAll(txtPassword.value) != trimAll(txtCPassword.value) )
		{
			alert("Confirm password doesn't match with the password.");
			txtCPassword.focus();
			return false;
		}		
		else if(trimAll(ddlSecQues.value) == ""){
			alert("Please select security question!");
			ddlSecQues.focus();
			return false;
		}else if(trimAll(txtSecAns.value) == "") {	
			alert("Please enter security answer!");
			txtSecAns.focus();
			return false;			
		}else if(trimAll(txtAddress1.value) == ""){
			alert("Please enter agent address !");
			txtAddress1.focus();
			return false;
		}else if(trimAll(txtCity.value) == ""){
			alert("Please enter agent city !");
			txtCity.focus();
			return false;
		}else if(trimAll(txtState.value) == ""){
			alert("Please enter agent state !");
			txtState.focus();
			return false;
		}else if(trimAll(txtPinCode.value) == ""){
			alert("Please enter agent postal code !");
			txtPinCode.focus();
			return false;
		}else if(trimAll(txtCountry.value) == ""){
			alert("Please enter agent country !");
			txtCountry.focus();
			return false;
		}else if( trimAll(txtPhone.value) == ""  ){
			alert("Please enter agent phone number !");
			txtPhone.focus();
			return false;
		}else{
			return true;
		}
	}
}

function agentLoginVal(frmName)
{
    //check for valid email
    var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  
  	if(trimAll(document.getElementById('txtEmail').value) == "")
	{
		alert("Please enter email address!");
		document.getElementById('txtEmail').value="";
		document.getElementById('txtEmail').focus();
		return false;
	}
	else if(!(objRegExp.test(trimAll(document.getElementById('txtEmail').value))))
    {
        alert("Please enter valid email address!");
        document.getElementById('txtEmail').focus();
        return false;
    }
	else if(trimAll(document.getElementById('txtPassword').value) == "")
	{
		alert("Please enter password!");
		document.getElementById('txtPassword').value="";
		document.getElementById('txtPassword').focus();
		return false;			
	}
	else
	{
		return true;
	}
}

function findAgent(frmName)
{
    with(document.forms[frmName])
	{
	    if((trimAll(ddstate.value) == "") && (trimAll(ddcity.value) == "")){
		    alert("Please select state or city to find agent!");
		    ddstate.focus();
		    return false;
	    }
	    else{
	        return true;
	    }
	}
}
function funRemoveSubCat(formname)
{
    with(document.forms[formname])
    {   
        if(ddcity.options.length>0)
        {
            ddcity.removeChild(ddcity.firstChild);
            funRemoveSubCat(formname);
        }
    }
}

function funCallStateAjax(formname)
{
    with(document.forms[formname])
	{
	    var DrpDwnVal = ddstate.value; 
	    hdnCityValue.value = "";  
	    funRemoveSubCat(formname);
		if(DrpDwnVal!="")
		{      
		  url="ajax_find_agentcity.aspx?state="+DrpDwnVal;
		  
		  //Does URL begin with http?
		  if(url.substring(0, 4) != 'http') 
		  {
			url = base_url + url;
		  }

		  http_req = null;

		  if (window.XMLHttpRequest)     http_req = new XMLHttpRequest();
		  else if (window.ActiveXObject) http_req = new ActiveXObject("Microsoft.XMLHTTP");
 
		  if (http_req)
		  {     
     			http_req.onreadystatechange = funInsrtCity;
				
				http_req.open("GET", url, true);
				http_req.send("");		   
		  }
		}
		else
		{
			var optn = document.createElement("OPTION");
			optn.text = "--- Select City ---";
			optn.value = "";
			ddcity.options.add(optn); 
		}
	}
}	
function funInsrtCity()
{
    if(http_req.readyState == 4)
    {	 
        txt = http_req.responseText;

        f = document.frmagentLogin;
        var optn = document.createElement("OPTION");
        optn.text = "--- Select City ---";
        optn.value = "";
        f.ddcity.options.add(optn);

        var arrTxt = txt.split("@");    
        if(trimAll(arrTxt[0])!="")
        {
            var arrSubcatIdsAndNames = arrTxt[0].split("#");
            for(var i=0;i<arrSubcatIdsAndNames.length;i++)
            {  
                if(arrSubcatIdsAndNames[i]!="")
                {
                    var arrNames = arrSubcatIdsAndNames[i].split("^");
                    var strId = arrNames[0];
                    var strNames = arrNames[1];

                    optn = document.createElement("OPTION");
                    optn.text = strNames;
                    optn.value = strId;
                    f.ddcity.options.add(optn);
                }
            }//for loop ends
        }
    }
}

function funCallCityAjax(formname)
{
	with(document.forms[formname])
	{   
		var DrpDwnVal = ddcity.value; 
	    hdnCityValue.value=  DrpDwnVal;
    }
}