// JavaScript Document


// This will re-direct the user back to the Home Page if they choose to cancel the account creation
function confirmCancel() {
var x=window.confirm("This will cancel the user account you are setting up. Do you wish to continue?");
	if (x) {
		window.location = "./index.php";
	}
}



// Begin Phone Number Validation and Input Masking - Auto enters the proper characters for phone number format consistency and does not allow certain characters
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 VALIDATION

// Do not allow entry of characters other than numbers in the zip code field
function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;

      }
// This function is called throughout the validation code above.  This makes the "required" image appear or dissappear on the browser screen
// to help guide the user to the proper fields where their inputted data did not pass our validation requirements.
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
}
}


// This function is used to validate an email address to ensure that it "seems to be" an email address by looking for the "@" and the "."
// and also for checking the position of these characters when they do appear so as to reduce the ability to trick our 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					
	}
	
	
// Validate form information prior to submitting it to the database.  Detailed comments within.
function continueOption() {
	valid = false;
	
	// Set variable to a confirmation window with a message.
	var x=window.confirm("This will create your new account, do you wish to continue?");
	
	// Set Variable for minimum password length
	var psswd = document.getElementById('textPassword1').value;
	
	// Continue to validation if the user clicks ok on the confirmation window.
	if (x) {
	
	valid = true;
	
	
		// Check to ensure the user has marked themself as an "agent" or "owner".  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		if (document.getElementById('textAgent').value =="2") 
		{
			valid = false;
			alert("You must specify whether you are an agent, or an owner.");
			document.getElementById('textAgent').focus();
		} 
						
		// Check to ensure the user has entered their First Name.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textFirstName').value =="") 
		{
			valid = false;
			alert("Please enter your FIRST NAME.");
			toggleDiv('val1',1);
			document.getElementById('textFirstName').focus();
		}
				
		// Check to ensure the user has entered their Last Name.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textLastName').value =="") 
		{
			toggleDiv('val1',0);
			valid = false;
			alert("Please enter your LAST NAME.");
			toggleDiv('val2',1);
			document.getElementById('textLastName').focus();
		}
		
		// Check to ensure the user has entered their Address.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textAddress1').value =="") 
		{
			toggleDiv('val2',0);
			valid = false;
			alert("Please enter your address in ADDRESS 1.")
			toggleDiv('val3',1);
			document.getElementById('textAddress1').focus();
		} 
		
		// Check to ensure the user has entered their City.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textCity').value =="") 
		{
			toggleDiv('val3',0);
			valid = false;
			alert("Please enter your CITY.")
			toggleDiv('val4',1);
			document.getElementById('textCity').focus();
		} 
		
		// Check to ensure the user has entered their State.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textState').value =="") 
		{
			toggleDiv('val4',0);
			valid = false;
			alert("Please enter your STATE.")
			toggleDiv('val5',1);
			document.getElementById('textState').focus();
		} 
		
		// Check to ensure the user has entered their Zip Code.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textZipCode').value =="") 
		{
			toggleDiv('val4',0);
			toggleDiv('val5',0);
			valid = false;
			alert("Please enter your ZIP CODE.")
			toggleDiv('val6',1);
			document.getElementById('textZipCode').focus();
		} 
	
		// Check to ensure the user has entered their Preferred Contact Option.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textContactOption').value =="") 
		{
			toggleDiv('val6',0);
			valid = false;
			alert("Please enter your PREFERRED CONTACT OPTION.  This information shows up on your property website.")
			toggleDiv('val7',1);
			document.getElementById('textContactOption').focus();
		}
	
	
		// Check to ensure that there is at least one contact number if the value is not equal to "None".
		else if (document.getElementById('textContactOption').value !="None" && document.getElementById('textOfficePhone').value =="" && document.getElementById('textMobilePhone').value =="" && document.getElementById('textHomePhone').value =="" && document.getElementById('textPager').value =="")
		{
			alert("You must enter at least one phone number OR select \"None\" for CONTACT OPTION.");
			// Not sure that this is the best way to handle this.
			return false;
		}
	
		// Check to ensure the user has entered their Email Address and that it is valid.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textEmailAddress').value =="") 
		{
			toggleDiv('val7',0);
			valid = false;
			alert("Please enter your EMAIL ADDRESS.")
			toggleDiv('val8',1);
			document.getElementById('textEmailAddress').focus();
		} 
		
		// Reference valid email address code above.  Function called "echeck"
		else if (echeck(document.getElementById('textEmailAddress').value)==false)
		{
			valid = false;
			alert("You must enter a valid email address.");
			document.getElementById('textEmailAddress').focus();
		} 
		
		// Check to ensure the user has entered their Password.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textPassword1').value =="") 
		{
			toggleDiv('val8',0);
			valid = false;
			alert("Please enter your PASSWORD.")
			toggleDiv('val9',1);
			document.getElementById('textPassword1').focus();
		} 
		
		// Check to ensure the user has confirmed their Password.  If left blank, prompt box appears and "required" image in DIV tag on html page is displayed by browser.
		else if (document.getElementById('textPassword2').value =="") 
		{
			toggleDiv('val9',0);
			valid = false;
			alert("Please Re-Enter your PASSWORD.")
			toggleDiv('val10',1);
			document.getElementById('textPassword2').focus();
		}
		
		// VALIDATE password by checking if they match and if they meet minimum length requirements.		
		
		// Check to make sure passwords match up.
		else if (document.getElementById('textPassword1').value != document.getElementById('textPassword2').value) 
		{
			toggleDiv('val10',0);
			alert("Your passwords do not match. Please re-enter.");
			document.getElementById('textPassword1').value = "";
			document.getElementById('textPassword2').value = "";
			document.getElementById('textPassword1').focus();
			valid = false;
		}  
		
		// Check to make sure password is not less than minimum length.
		else if (psswd.length < 5) 
		{
				toggleDiv('val10',0);
				alert("Your password must be at least 5 characters long.");
				document.getElementById('textPassword1').focus();
				valid = false;
		} 
		
		
		// Check to make sure password does not exceed maximum length.
		else if (psswd.length > 10) 
		{
				toggleDiv('val10',0);
				alert("Your password may not be longer than 10 characters.");
				document.getElementById('textPassword1').focus();
				valid = false;
		} 
		
		
		// Check to ensure the user has Agreed to the Terms and Conditions.
		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();
			}
		
	}
	
	// If all data checks out against the above vaildation, Then the data is okay to proceed and create the record in the database.
	return valid;
}
