function usernameck()
{
    var theText=document.po.username.value;
    //document.po.usernameok.value=0;
    document.po.usernamemessage.value="Username ! Available";
    if(theText.length > 3)
    {
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
    		document.po.usernameok.value=0;
    		document.po.usernamemessage.value=">>Username ! Available";
			return;
        } 

        var url="../inc/checkusername.asp";
        var params = "user_nick=" +theText;
        //alert("params: "+params);
        xmlHttp.open("POST",url,true);
        xmlHttp.onreadystatechange = function() 
        {
         	//alert("entered call back function. readstate value is: "+xmlHttp.readyState+". Response Text is: "+xmlHttp.responseText);
        	if (xmlHttp.readyState==4)
        	{ 
            	//alert(xmlHttp.responseText);
				if (xmlHttp.responseText == "1") 
				{	
            		document.po.usernamemessage.value="Username Available";
            		document.po.usernameok.value=1;
					buttonok();
				}
				else
				{
					document.po.usernameok.value=0;
    				document.po.usernamemessage.value="Username ! Available";
				}			
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);

	}
    buttonok();
}

function ref_usernameck()
{
    var theText=document.po.username.value;
    document.po.usernameok.value=0;
    document.po.usernamemessage.value="Username Not Available";
    if(theText.length > 3)
    {
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
    		document.po.usernameok.value=0;
    		document.po.usernamemessage.value=">>Username Not Available";
			return;
        } 

        var url="../inc/checkusername.asp";
        var params = "user_nick=" +theText;
        //alert("params: "+params);
        xmlHttp.open("POST",url,true);
        xmlHttp.onreadystatechange = function() 
        {
         	//alert("entered call back function. readstate value is: "+xmlHttp.readyState+". Response Text is: "+xmlHttp.responseText);
        	if (xmlHttp.readyState==4)
        	{ 
            	//alert(xmlHttp.responseText);
				if (xmlHttp.responseText == "1") 
				{	
            		document.po.usernamemessage.value="Username Available";
            		document.po.usernameok.value=1;
					buttonok();
				}
				else
				{
					document.po.usernameok.value=0;
    				document.po.usernamemessage.value="Username ! Available";
				}			
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);

	}
    buttonok();
}


function findR1Area()
{
    var theText = document.po.r1area.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.r1areastate.value = xmlHttp.responseText;
					document.po.r1areaok.value=1;
					buttonok();
				}
				else
				{
					document.po.r1areastate.value = "BadArea";				
					document.po.r1areaok.value=0;
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.r1areastate.value != "BadArea")
	{
		document.po.r1phone1.focus() ;
		document.po.r1phone1.select();
		document.po.r1areaok.value=1;
    }
	else
	{
		document.po.r1areaok.value=0;
 	}
	buttonok();
}

function findR2Area()
{
    var theText = document.po.r2area.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.r2areastate.value = xmlHttp.responseText;
					document.po.r2areaok.value=1;
					buttonok();
				}
				else
				{
					document.po.r2areastate.value = "BadArea";				
					document.po.r2areaok.value=0;
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.r2areastate.value != "BadArea")
	{
		document.po.r2phone1.focus() ;
		document.po.r2phone1.select();
		document.po.r2areaok.value=1;
    }
	else
	{
		document.po.r2areaok.value=0;
 	}

	buttonok();
}

function findWArea()
{

    var theText = document.po.warea.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.wareastate.value = xmlHttp.responseText;
					vwarea=1;
					buttonok();
				}
				else
				{
					document.po.wareastate.value = "BadArea";				
					vwarea=0;
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.wareastate.value != "BadArea")
	{
		vwarea=1;
		document.po.wphone1.focus() ;
		document.po.wphone1.select()  ;
    }
	else
	{
		vwarea=0;
 	}
    buttonok();
}

function findSArea()
{
    var theText = document.po.sarea.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.sareastate.value = xmlHttp.responseText;
					vsarea=1;
					buttonok();
				}
				else
				{
					document.po.sareastate.value = "BadArea";				
					vsarea=0;
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.sareastate.value != "BadArea")
	{
		vsarea=1;
		document.po.sphone1.focus() ;
		document.po.sphone1.select()  ;
    }
	else
	{
		vsarea=0;
 	}

	buttonok();
}

function findBArea()
{
    var theText = document.po.barea.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.bareastate.value = xmlHttp.responseText;
					vbarea=1;
					buttonok();
				}
				else
				{
					document.po.bareastate.value = "BadArea";				
					vbarea=0;
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.bareastate.value != "BadArea")
	{
		vbarea=1;
		document.po.bphone1.focus() ;
		document.po.bphone1.select()  ;
    }
	else
	{
		vbarea=0;
 	}
	buttonok();
}

function findBankName()
{
    var theText = document.po.aba.value;
    if(theText.length==9)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotebank.asp";
        var params = "rout_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadRoute") 
				{	
		    		document.po.bankname.value = xmlHttp.responseText;
					vbarea=1;
					buttonok();
				}
				else
				{
					document.po.bankname.value = "BadRoute";				
					vbarea=0;
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
	buttonok();
}


function ref_findparea()
{
    var theText = document.po.parea.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.pareastate.value = xmlHttp.responseText;
					buttonok();
				}
				else
				{
					document.po.pareastate.value = "BadArea";				
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.pareastate.value != "BadArea")
	{
    	document.po.pphone1.focus();
    	document.po.phonemessage.value="Good Area Code"  ;
    }
	else
	{
    	document.po.phonemessage.value="Need Good Area Code";
 	}
    buttonok();
}

function findparea()
{
    var theText = document.po.parea.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.pareastate.value = xmlHttp.responseText;
					buttonok();
				}
				else
				{
					document.po.pareastate.value = "BadArea";				
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.pareastate.value != "BadArea")
	{
    	document.po.pphone1.focus();
    	document.po.phonemessage.value="Good Area Code"  ;
    }
	else
	{
    	document.po.phonemessage.value="Need Good Area Code";
 	}
    buttonok();
}

function findharea()
{
    var theText = document.po.harea.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.hpareastate.value = xmlHttp.responseText;
					document.po.hareaok.value=1;
				    buttonok();
				}
				else
				{
					document.po.hpareastate.value = "BadArea";				
					document.po.hareaok.value=1;
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.hpareastate.value != "BadArea")
	{
		document.po.hareaok.value=1;
  		document.po.hphone1.select();
  	}
	else
	{  
  		document.po.hareaok.value=0  ;
 	}
    buttonok();
}


function findcarea()
{
    var theText = document.po.carea.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.cpareastate.value = xmlHttp.responseText;
					document.po.careaok.value=1;
				    buttonok();
				}
				else
				{
					document.po.cpareastate.value = "BadArea";				
					document.po.careaok.value=0;
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	}
    if(theText.length ==3 && document.po.cpareastate.value != "BadArea")
	{
		document.po.careaok.value=1;
  		document.po.cphone1.select();
  	}
	else
	{  
  		document.po.careaok.value=0  ;
 	}
    buttonok();
}

function findCityState()
{
  	theText = document.po.zipcode.value;
  	if(theText.length==5)
	{
   		xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotecity.asp";
        var params = "zipcode=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ //alert(xmlHttp.responseText);
				if (xmlHttp.responseText != "BadZip") 
				{	
		    		document.po.city.value = xmlHttp.responseText;
   					document.po.cityok.value=1;
			   		document.po.zipcodeok.value=1;
				    buttonok();
				}
				else
				{
					document.po.city.value = "BadZip";				
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	
   		xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null)
        {
			return;
        } 

        var url2="../inc/remotestate.asp";
        var params2 = "zipcode=" +theText;
        xmlHttp2.open("POST",url2,true);

        xmlHttp2.onreadystatechange = function() 
        {

        	if (xmlHttp2.readyState==4)
        	{ //alert(xmlHttp2.responseText);
				if (xmlHttp2.responseText != "BadZip") 
				{	
		    		document.po.state.value  = xmlHttp2.responseText;
			   		document.po.stateok.value=1;
			   		document.po.zipcodeok.value=1;
				    buttonok();
				}
				else
				{
					document.po.state.value  = "BadZip";				
				}
        	}
        }
        xmlHttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp2.send(params2);
   	}
	else
	{
    	document.po.city.value = "BadZip";
  		document.po.state.value = "BadZip";
  		document.po.cityok.value=0;
     	document.po.stateok.value=0;
     	document.po.zipcodeok.value=0;
  	}

  	if(theText.length == 5 && document.po.city.value != "BadZip" &&  document.po.state.value != "BadZip")
	{
   		document.po.cityok.value=1;
   		document.po.stateok.value=1;
   		document.po.zipcodeok.value=1;
  	}
	else
	{
   		document.po.cityok.value=0;
   		document.po.stateok.value=0;
   		document.po.zipcodeok.value=0;
	}
  	buttonok();
}


function findBankCityState()
{

  	theText = document.po.wzipcode.value;
  	if(theText.length==5)
	{
   		xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
			return;
        } 

        var url="../inc/remotecity.asp";
        var params = "zipcode=" +theText;
        xmlHttp.open("POST",url,true);

        xmlHttp.onreadystatechange = function() 
        {
        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadZip") 
				{	
		    		document.po.wcity.value = xmlHttp.responseText;
				    buttonok();
				}
				else
				{
					document.po.wcity.value = "BadZip";				
				}
        	}
        }
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);
	


   		xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null)
        {
			return;
        } 

        var url2="../inc/remotestate.asp";
        var params2 = "zipcode=" +theText;
        xmlHttp2.open("POST",url2,true);

        xmlHttp2.onreadystatechange = function() 
        {

        	if (xmlHttp2.readyState==4)
        	{ //alert(xmlHttp2.responseText);
				if (xmlHttp2.responseText != "BadZip") 
				{	
		    		document.po.wstate.value  = xmlHttp2.responseText;
				    buttonok();
				}
				else
				{
					document.po.wstate.value  = "BadZip";				
				}
        	}
        }
        xmlHttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp2.send(params2);
   	}
	else
	{
    	document.po.wcity.value = "BadZip";
  		document.po.wstate.value = "BadZip";
  	}
  	buttonok();
}

function findareastate()
{
    var theText = document.po.parea.value;
    if(theText.length==3)
	{
       	xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
		  	document.po.pareaok.value=0  ;
    		document.po.pareastate.value = "BadArea";
			return;
        } 

        var url="../inc/remotearea.asp";
        var params = "area_code=" +theText;
        xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlHttp.send(params);

        xmlHttp.onreadystatechange = function() 
        {

        	if (xmlHttp.readyState==4)
        	{ 
				if (xmlHttp.responseText != "BadArea") 
				{	
		    		document.po.pareastate.value = xmlHttp.responseText;
			  		document.po.pareaok.value=1;
				    buttonok();
				}
				else
				{
					document.po.pareastate.value = "BadArea";
			  		document.po.pareaok.value=0;
				}
        	}
        }
		//xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        //xmlHttp.send(params);
	}
	else
	{
		document.po.pareastate.value = "BadArea";
	}

    if(theText.length == 3 && document.po.pareastate.value != "BadArea")
	{
   		document.po.phone1.select();
  		document.po.pareaok.value=1;
  		//document.po.phone1.focus();
    }
	else
	{
  		document.po.pareaok.value=0  ;
 	}
    buttonok();
}

function ssnck()
{
  	ssn=document.po.ssn.value ;
  	own_ssn=document.po.useraccountsid.value ;
  	document.po.ssnok.value=0;

  	if(ssn.length ==9)
	{
   		var ssn1=Left(ssn,3);
   		var ssn2=Mid(ssn,4,2);
   		var ssn3=Right(ssn,4);

   		if(parseFloat(ssn1) > 0 && parseFloat(ssn1) < 773 && parseFloat(ssn1) != 666 && parseFloat(ssn2) > 0 && parseFloat(ssn3)>0)
		{
			ssnpass=0;
       		xmlHttp=GetXmlHttpObject();
        	if (xmlHttp==null)
        	{
				return;
        	} 

        	var url="../inc/SSNUsedTest.asp";
        	var params = "ssn=" + ssn + "&own_ssn=" + own_ssn ;
        	xmlHttp.open("POST",url,true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        	xmlHttp.send(params);

        	xmlHttp.onreadystatechange = function() 
        	{
        		if (xmlHttp.readyState==4)
        		{ 
					if (xmlHttp.responseText == "1") 
					{	     					
						document.po.ssnok.value=1;
	     				ssnpass=1;
					    buttonok();										
					}
					else if (xmlHttp.responseText == "2") 
					{
						ssnpass=0;
     					alert("An Account already exists for that SSN. Please Log In to that account from the Home Page.");
					}
					else
					{
 						ssnpass=0;
					}
        		}
        	}
   		}
  	}
 	vssnck() ;
}
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;
}


function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function Mid(str, start, len)
{

    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}

function InStr(strSearch, charSearchFor)
{
	for (i=0; i < strSearch.length; i++)
   	{
    	if (charSearchFor == Mid(strSearch, i, 1))
        {
        	return i;
      	}
  	}
    return -1;
}

function isDate(dateStr) 
{

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) 
	{
		alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) 
	{ // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) 
	{
		alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}

	if (month == 2)
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) 
		{
			alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}
function daysBetween(date1, date2){
if (date1.indexOf("-") != -1) { date1 = date1.split("-"); } else if (date1.indexOf("/") != -1) { date1 = date1.split("/"); } else { return 0; }
if (date2.indexOf("-") != -1) { date2 = date2.split("-"); } else if (date2.indexOf("/") != -1) { date2 = date2.split("/"); } else { return 0; }
if (parseInt(date1[0], 10) >= 1000) {
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
} else if (parseInt(date1[2], 10) >= 1000) {
var sDate = new Date(date1[2]+"/"+date1[0]+"/"+date1[1]);
} else {
return 0;
}
if (parseInt(date2[0], 10) >= 1000) {
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
} else if (parseInt(date2[2], 10) >= 1000) {
var eDate = new Date(date2[2]+"/"+date2[0]+"/"+date2[1]);
} else {
return 0;
}
var one_day = 1000*60*60*24;
var daysApart = Math.abs(Math.ceil((sDate.getTime()-eDate.getTime())/one_day));
return daysApart;
}
