// phone number validation
var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}

ParseForNumber1(object)

if(keycode >= 48){
ValidatePhone(object)
}
}

function backspacerDOWN(object,e) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}
ParseForNumber2(object)
} 

function GetCursorPosition(){

var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i=0; i<t1.length; i++)
{
if (t1.substring(i,1) != t2.substring(i,1)) {
if(!bool) {
cursorposition=i
bool=true
}
}
}
}

function ValidatePhone(object){

var p = phonevalue1

p = p.replace(/[^\d]*/gi,"")

if (p.length < 3) {
object.value=p
} else if(p.length==3){
pp=p;
d4=p.indexOf('(')
d5=p.indexOf(')')
if(d4==-1){
pp="("+pp;
}
if(d5==-1){
pp=pp+")";
}
object.value = pp;
} else if(p.length>3 && p.length < 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

object.value = pp; 

} else if(p.length >= 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

l40 = pp.length;
p40 = pp.substring(0,8);
p40 = p40 + "-"

p41 = pp.substring(8,l40);
ppp = p40 + p41;

object.value = ppp.substring(0, maxphonelength);
}

GetCursorPosition()

if(cursorposition >= 0){
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1=object.value.indexOf(')')
e2=object.value.indexOf('-')
if (e1>-1 && e2>-1){
if (e2-e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}

var txtRange = object.createTextRange();
txtRange.moveStart( "character", cursorposition);
txtRange.moveEnd( "character", cursorposition - object.value.length);
txtRange.select();
}

}

function ParseChar(sStr, sChar)
{
if (sChar.length == null) 
{
zChar = new Array(sChar);
}
else zChar = sChar;

for (i=0; i<zChar.length; i++)
{
sNewStr = "";

var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);

while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

sStr = sNewStr;
}

return sNewStr;
}

// end phone number validation

// zip code validation
function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;

      }

// begin email validation
function echeck(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					
	}
// end email validation

// begin account validation
function continueOption() {
	valid = false;
	
	var x=window.confirm("This will create your new account, do you wish to continue?");
	
	var psswd = document.getElementById('password').value;
	
	if (x) {
	
	valid = true;
	
	
		if (document.getElementById('agent').value =="none") 
		{
			valid = false;
			alert("You must specify whether you are an Agent, or an Owner.");
			toggleDiv('val1',1);
			document.getElementById('agent').focus();
		} 
		
		else if (document.getElementById('first_name').value =="") 
		{
			toggleDiv('val1',0);
			valid = false;
			alert("Please enter your First Name.");
			toggleDiv('val2',1);
			document.getElementById('first_name').focus();
		}
				
		else if (document.getElementById('last_name').value =="") 
		{
			toggleDiv('val2',0);
			valid = false;
			alert("Please enter your Last Name.");
			toggleDiv('val3',1);
			document.getElementById('last_name').focus();
		}
		
		else if (document.getElementById('address').value =="") 
		{
			toggleDiv('val3',0);
			valid = false;
			alert("Please enter your Address.")
			toggleDiv('val4',1);
			document.getElementById('address').focus();
		} 
		
		else if (document.getElementById('city').value =="") 
		{
			toggleDiv('val4',0);
			valid = false;
			alert("Please enter your City.")
			toggleDiv('val5',1);
			document.getElementById('city').focus();
		} 
		
		else if (document.getElementById('state').value =="") 
		{
			toggleDiv('val5',0);
			valid = false;
			alert("Please enter your State.")
			toggleDiv('val6',1);
			document.getElementById('state').focus();
		} 
		
		else if (document.getElementById('zip').value =="") 
		{
			toggleDiv('val6',0);
			valid = false;
			alert("Please enter your Zip Code.")
			toggleDiv('val7',1);
			document.getElementById('zip').focus();
		} 
	
		else if (document.getElementById('phone').value =="") 
		{
			toggleDiv('val7',0);
			valid = false;
			alert("Please enter your Phone.")
			toggleDiv('val8',1);
			document.getElementById('phone').focus();
		} 
		
		else if (document.getElementById('email_address').value =="") 
		{
			toggleDiv('val8',0);
			valid = false;
			alert("Please enter your Email Address.")
			toggleDiv('val9',1);
			document.getElementById('email_address').focus();
		} 
		
		else if (echeck(document.getElementById('email_address').value)==false)
		{
			valid = false;
			alert("You must enter a valid Email Address.");
			document.getElementById('email_address').focus();
		} 
		
		else if (document.getElementById('email_address_again').value =="") 
		{
			toggleDiv('val9',0);
			valid = false;
			alert("Please enter your Email Address again.")
			toggleDiv('val10',1);
			document.getElementById('email_address_again').focus();
		}
		
		else if (echeck(document.getElementById('email_address_again').value)==false)
		{
			valid = false;
			alert("You must enter a valid Email Address again.");
			document.getElementById('email_address_again').focus();
		} 
		
		else if (document.getElementById('email_address').value != document.getElementById('email_address_again').value) 
		{
			toggleDiv('val10',0);
			alert("Your Email Addresses do not match. Please re-enter.");
			toggleDiv('val9',1);
			toggleDiv('val10',1);
			document.getElementById('email_address').value = "";
			document.getElementById('email_address_again').value = "";
			document.getElementById('email_address').focus();
			valid = false;
		} 
		
		else if (document.getElementById('password').value =="") 
		{
			toggleDiv('val9',0);
			toggleDiv('val10',0);
			valid = false;
			alert("Please enter your Password.")
			toggleDiv('val11',1);
			document.getElementById('password').focus();
		} 
		
		else if (document.getElementById('password_again').value =="") 
		{
			toggleDiv('val11',0);
			valid = false;
			alert("Please Re-Enter your Password.")
			toggleDiv('val12',1);
			document.getElementById('password_again').focus();
		}
		
		else if (document.getElementById('password').value != document.getElementById('password_again').value) 
		{
			toggleDiv('val12',0);
			alert("Your Passwords do not match. Please re-enter.");
			toggleDiv('val11',1);
			toggleDiv('val12',1);
			document.getElementById('password').value = "";
			document.getElementById('password_again').value = "";
			document.getElementById('password').focus();
			valid = false;
		}  
		
		else if (psswd.length < 5) 
		{
				toggleDiv('val12',0);
				alert("Your Password must be at least 5 characters long.");
				toggleDiv('val11',1);
				toggleDiv('val12',1);
				document.getElementById('password').value = "";
				document.getElementById('password_again').value = "";
				document.getElementById('password').focus();
				valid = false;
		} 
		
		
		// Check to make sure password does not exceed maximum length.
		else if (psswd.length > 10) 
		{
				toggleDiv('val12',0);
				alert("Your Password may not be longer than 10 characters.");
				toggleDiv('val11',1);
				toggleDiv('val12',1);
				document.getElementById('password').value = "";
				document.getElementById('password_again').value = "";
				document.getElementById('password').focus();
				valid = false;
		} 
		
		else if (document.getElementById('code').value =="") 
		{
			valid = false;
			toggleDiv('val11',0);
			toggleDiv('val12',0);
			alert("Please enter the Captcha code.")
			toggleDiv('val13',1);
			document.getElementById('code').focus();
		} 
		/*else if (document.getElementById('chkAgree').checked) 
		{
			toggleDiv('val10',0);
		} 
			
			else 
			{
				valid = false;
				alert("You must agree to the terms and conditions before your account will be created.");
				document.getElementById('chkAgree').focus();
			}*/
		
	}
	
	return valid;
}
//end account validation

// validation script
function toggleDiv(szDivID, iState) // 1 visible, 0 hidden
{
try {
    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";
    }
}
catch(err) {
	//Do nothing
}
}