// Script to cancel the onine rental application process
function confirmCancelRequest(formIdent)
{
    doyou = confirm("By selecting OK, you will be cancelling the online rental process and clearing all entered information.");

    if (doyou == true)
    {
         var formlist = formIdent;
	
	 //alert(formlist);
	 
	 // Do what you want here when the question is answered Yes.
	 // clear all fields
	 clearForm('form_application_step5');
	 clearForm('form_application_step4');
	 clearForm('form_application_step3');
	 clearForm('form_application_step2');
	 
	 // Redirect to rental search page
	 window.location="clear_session_data.php";
    }
    else if (doyou == false)
    {
         alert("Aborting CANCEL request");
	 
	 // Do nothing when the question is answered No.
    }
}

function clearForm(formIdent) { 
    var formname = formIdent;
    var inp = document.getElementsByTagName('input');
    
    for(var i = 0; i < inp.length; i++) {
	if(inp[i].type == 'text') {
	    inp[i].value = '';
	}
    }
    
    var inp = document.getElementsByTagName('select');
    
    for(var i = 0; i < inp.length; i++) {
	inp[i].selectedIndex=0
    }
    
    // document.listsearch.submit();
}

function autoTab(original,destination)
{
    if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
    destination.focus();
}

// Scripts to create a popup on mouseover
function ShowPopup(hoveritem)
{
    hp = document.getElementById("hoverpopup");

    // Set position of hover-over popup
    hp.style.top = hoveritem.offsetTop + 18;
    hp.style.left = hoveritem.offsetLeft + 0;

    // Set popup to visible
    hp.style.visibility = "Visible";
}

function HidePopup()
{
    hp = document.getElementById("hoverpopup");
    hp.style.visibility = "Hidden";
}

function popitup(url) 
{
	newwindow=window.open(url,'privacy','height=490,width=440');
	if (window.focus) {newwindow.focus()}
	return false;
}

// Script to validate valid promotional code, neeed to change to make a call to the database to validate the promo code
function test_promo()
{
    if (document.form_promo.promocode.value=="" || document.form_promo.promocode.value=="Enter promo code") {
	   alert ("Please enter your promo code provided on the CIS postcard mailer");
	   return false;  
    } else if (document.form_promo.promocode.value=="PB08-200902" || document.form_promo.promocode.value=="pb08-200902") {
	   document.form_promo.action = "promo_pb08_200902.html";
	   document.form_promo.submit();
    } else {
	   alert ("If you do not have your CIS promo code, contact us at (858) 495-3344, ask for Rod Taylor.");
	   return false; 
    }
}

// Validates MLS search criteria
function test_MLSsearch()
{
    if (document.form_MLSsearch.community.value=="") {
	   alert ("Please select a community");
	   return false;  
    } else if (document.form_MLSsearch.property_type.value=="") {
	   alert ("Please select a property type");
	   return false;  
    } else {
	   document.form_MLSsearch.action = "";
	   document.form_MLSsearch.submit();
    } 
}

// Validates rental search criteria
function test_search()
{
    if (document.form_rentalsearch.community.value=="") {
	   alert ("Please select a community");
	   return false;  
    } else if (document.form_rentalsearch.community.value=="na") {
	   alert ("There are no vacancies, select a community that is not grayed out");
	   return false;  	   
    } else {
	   document.form_rentalsearch.action = "navbar_menu03_results.php";
	   document.form_rentalsearch.submit();
    } 
}

// Validates real property search criteria
function test_realProperty_search()
{
    if (document.form_realProperty_search.community.value=="") {
	   alert ("Please select a community");
	   return false;  
    } else if (document.form_realProperty_search.community.value=="na") {
	   alert ("There are no available realestate properties, select a community that is not grayed out");
	   return false;  	   
    } else {
	   document.form_realProperty_search.action = "navbar_menu02_results.php";
	   document.form_realProperty_search.submit();
    } 
}

// Validates login values
function test_login()
{
    if (document.form_login.user_id.value=="") {
	   alert ("Please enter your user id (email address)");
	   return false;  
    } else if (document.form_login.password.value=="") {
	   alert ("Please enter your password");
	   return false;  
    } else {
	   document.form_login.action = "login_check.php";
	   document.form_login.submit();
    } 
}

// Validate Property Management request form
function test_pmr()
{
    if (document.form_pmr.property_type.value=="") {
	   alert ("Please select a property type");
	   return false;  
    } else if (document.form_pmr.apt_street.value=="" || document.form_pmr.apt_street.value=="Enter street") {
	   alert ("Please enter the income property street address");
	   return false; 
    } else if (document.form_pmr.apt_city.value=="" || document.form_pmr.apt_city.value=="Enter city") {
	   alert ("Please enter the income property city name");
	   return false; 
    } else if (document.form_pmr.apt_state.value=="" || document.form_pmr.apt_state.value=="select state") {
	   alert ("Please select the income property state");
	   return false; 		   
    } else if (document.form_pmr.apt_zip.value=="" || document.form_pmr.apt_zip.value=="Enter zip") {
	   alert ("Please enter the income property zip code");
	   return false; 	  
    } else if (document.form_pmr.apt_units.value=="" || document.form_pmr.apt_units.value=="Enter number of units") {
	   alert ("Please enter the number of units for your income property");
	   return false; 	   
    } else if (document.form_pmr.fname.value=="" || document.form_pmr.fname.value=="Enter first name") {
	   alert ("Please enter your first name");
	   return false; 
    } else if (document.form_pmr.lname.value=="" || document.form_pmr.lname.value=="Enter last name") {
	   alert ("Please enter last name");
	   return false; 		   
    } else if (document.form_pmr.home_street.value=="" || document.form_pmr.home_street.value=="Enter street") {
	   alert ("Please enter your home street address");
	   return false; 
    } else if (document.form_pmr.home_city.value=="" || document.form_pmr.home_city.value=="Enter city") {
	   alert ("Please enter your home city name");
	   return false; 
    } else if (document.form_pmr.home_state.value=="" || document.form_pmr.home_state.value=="select state") {
	   alert ("Please select state");
	   return false; 	   
    } else if (document.form_pmr.home_zip.value=="" || document.form_pmr.home_zip.value=="Enter zip") {
	   alert ("Please enter your home zip code");
	   return false; 				      	   
    } else if (document.form_pmr.phone.value=="" || document.form_pmr.phone.value=="Enter phone number") {
	   alert ("Please enter your contact phone number");
	   return false; 
    } else if (document.form_pmr.emailid.value=="" || document.form_pmr.emailid.value=="Enter email address") {
	   alert ("Please enter your email address");
	   return false; 
    } else {
	   document.form_pmr.action = "sendemail_pmquote.php";
	   document.form_pmr.submit();
    } 
}

// Validate HOQ request form
function test_HOA()
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = document.form_HOA.emailAddress.value;
    var re3digit=/^\d{3}$/ //regular expression defining a 3 digit number
    var re4digit=/^\d{4}$/ //regular expression defining a 4 digit number
    var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number

    if (document.form_HOA.streetAddress.value=="" || document.form_HOA.streetAddress.value=="Enter street") {
	   alert ("Please enter the HOA property street address");
	   return false; 
    } else if (document.form_HOA.city.value=="" || document.form_HOA.city.value=="Enter city") {
	   alert ("Please enter the HOA property city name");
	   return false; 
    } else if (document.form_HOA.state.value=="" || document.form_HOA.state.value=="select state") {
	   alert ("Please select the HOA property state");
	   return false; 		   
    } else if (document.form_HOA.zipCode.value.search(re5digit)==-1) {
	   alert ("Please enter a valid zip code");
	   return false; 	  
    } else if (document.form_HOA.associationName.value=="" || document.form_HOA.associationName.value=="Enter association name") {
	   alert ("Please enter the association name");
	   return false; 	
    } else if (document.form_HOA.boardPosition.value=="" || document.form_HOA.boardPosition.value=="Enter board position") {
	   alert ("Please enter your position on the board");
	   return false;
    } else if (document.form_HOA.associationDetails.value=="" || document.form_HOA.associationDetails.value==null) {
	   alert ("Please enter association details");
	   return false;	   
    } else if (document.form_HOA.firstName.value=="" || document.form_HOA.firstName.value=="Enter first name") {
	   alert ("Please enter your first name");
	   return false; 
    } else if (document.form_HOA.lastName.value=="" || document.form_HOA.lastName.value=="Enter last name") {
	   alert ("Please enter last name");
	   return false; 
    } else if (document.form_HOA.phoneAreacode.value.search(re3digit)==-1) {
	   alert ("Please enter a valid area code");
	   return false;
    } else if (document.form_HOA.phonePrefix.value.search(re3digit)==-1) {
	alert ("Please enter your complete phone number");
	return false; 
    } else if (document.form_HOA.phoneLinenumber.value.search(re4digit)==-1) {
	alert ("Please enter your complete phone number");
	return false; 
    } else if (reg.test(address) == false) {
	   alert ("Please enter avalid email address");
	   return false; 
    } else {
	   document.form_HOA.action = "sendemail_HOAquote.php";
	   document.form_HOA.submit();
    } 
}

// Validate Market Analysis request form
function test_cma()
{
    if (document.form_cma.name.value=="" || document.form_cma.name.value=="Enter name") {
	   alert ("Please enter your full name");
	   return false;  
    } else if (document.form_cma.phone.value=="" || document.form_cma.phone.value=="Enter phone number") {
	   alert ("Please enter your contact phone number");
	   return false; 
    } else if (document.form_cma.emailid.value=="" || document.form_cma.emailid.value=="Enter email address") {
	   alert ("Please enter your email address");
	   return false; 
    } else if (document.form_cma.apt_street.value=="" || document.form_cma.apt_street.value=="Enter street") {
	   alert ("Please enter the income property street address");
	   return false; 
    } else if (document.form_cma.apt_city.value=="" || document.form_cma.apt_city.value=="Enter city") {
	   alert ("Please enter the income property city name");
	   return false; 
    } else if (document.form_cma.apt_state.value=="" || document.form_cma.apt_state.value=="select state") {
	   alert ("Please select the income property state");
	   return false; 		   
    } else if (document.form_cma.apt_zip.value=="" || document.form_cma.apt_zip.value=="Enter zip") {
	   alert ("Please enter the income property zip code");
	   return false; 	  
    } else if (document.form_cma.apt_units.value=="" || document.form_cma.apt_units.value=="Enter number of units") {
	   alert ("Please enter the number of units for your income property");
	   return false; 
    } else if (document.form_cma.home_street.value=="" || document.form_cma.home_street.value=="Enter street") {
	   alert ("Please enter your home street address");
	   return false; 
    } else if (document.form_cma.home_city.value=="" || document.form_cma.home_city.value=="Enter city") {
	   alert ("Please enter your home city name");
	   return false; 
    } else if (document.form_cma.home_state.value=="" || document.form_cma.home_state.value=="select state") {
	   alert ("Please select state");
	   return false; 	   
    } else if (document.form_cma.home_zip.value=="" || document.form_cma.home_zip.value=="Enter zip") {
	   alert ("Please enter your home zip code");
	   return false; 		   	    
    } else {
	   document.form_cma.action = "sendemail_cmarequest.php";
	   document.form_cma.submit();
    } 
}

// Validates waitlist request form
function test_waitist_form()
{
    if (document.form_waitlist.name.value=="" || document.form_waitlist.name.value=="Enter name" ) {
	   alert ("Please enter your name");
	   return false;  
       } else if (document.form_waitlist.emailid.value=="" || document.form_waitlist.emailid.value=="Enter email address") {
	   alert ("Please enter your email address");
	   return false;  
       } else if (document.form_waitlist.community.value=="") {
	   alert ("Please select a community");
	   return false; 
       } else if (document.form_waitlist.property_type.value=="") {
	   alert ("Please select a property type");
	   return false; 
       } else {
	   document.form_waitlist.action = "sendemail_waitingistrequest.php";
	   document.form_waitlist.submit();
    } 
}

// Validates rental application step 1 login
function test_application_step1_login()
{   
    if (document.form_application_step1_login.user_ip.value=="" || document.form_application_step1_login.user_ip.value==null) {
	   alert ("Please enter your User ID (email address)");
	   return false;  
       } else if (document.form_application_step1_login.password.value=="" ) {
	   alert ("Please enter your password");
	   return false; 
       } else {
	   document.form_application_step1_login.action = "rental_application_s2.php";
	   document.form_application_step1_login.submit();
       }
       
}

// Validates rental application step 1
function test_application_step1()
{          
       var invalid = " "; // Invalid character is a space
       var minLength = 5; // Minimum length
       var pw1 = document.form_application_step1.password.value;
       var pw2 = document.form_application_step1.passwordconfirmation.value;
       
       if (document.form_application_step1.user_ip.value=="" || document.form_application_step1.user_ip.value==null) {
	   alert ("Please enter a valid email address");
	   return false; 
       } else if (document.form_application_step1.password.value < minLength) {
	   alert('Your password must be at least ' + minLength + ' characters long. Try again.');
	   return false;
       } else if (document.form_application_step1.password.value.indexOf(invalid) > -1) {
	   alert("Sorry, spaces are not allowed.");
	   return false;
       } else if (pw1 != pw2) {
	   alert ("You did not enter the same new password twice. Please re-enter your password.");
	   return false; 	       
       } else {
	   document.form_application_step1.action = "rental_application_s2.php";
	   document.form_application_step1.submit();
       } 
}

// Validates rental application step 2
function test_application_step2()
{   
    if (document.form_application_step2.fname.value=="" || document.form_application_step2.fname.value==null) {
	   alert ("Please enter your first name");
	   return false;  
       } else if (document.form_application_step2.lname.value=="") {
	   alert ("Please enter your last name");
	   return false; 
       } else if (document.form_application_step2.mm_dob.value=="") {
	   alert ("Please select DOB month");
	   return false; 	   
       } else if (document.form_application_step2.dd_dob.value=="") {
	   alert ("Please select DOB day");	
	   return false; 	   
       } else if (document.form_application_step2.yyyy_dob.value=="") {
	   alert ("Please select DOB year");
	   return false; 
       } else if (document.form_application_step2.phone1.value=="") {
	   alert ("Please enter your complete contact phone number");
	   return false; 
       } else if (document.form_application_step2.phone2.value=="") {
	   alert ("Please enter your complete contact phone number");
	   return false; 
       } else if (document.form_application_step2.phone3.value=="") {
	   alert ("Please enter your complete contact phone number");
	   return false; 	   
       } else {
	   document.form_application_step2.action = "rental_application_s3.php";
	   document.form_application_step2.submit();
    } 
}

// Validates rental application step 3
function test_application_step3()
{   
    if (document.form_application_step3.movein_date_mm.value=="" || document.form_application_step3.movein_date_dd.value=="" || document.form_application_step3.movein_date_yyyy.value=="") {
	   alert ("Please select a valid move-in date");
	   return false;  
       } else if (document.form_application_step3.lease_term.checked == false) {
	   alert ("You did not check the lease term agreement box");
	   return false;  
       } else {
	   document.form_application_step3.action = "rental_application_s4.php";
	   document.form_application_step3.submit();
    } 
}

// Validates rental application step 4
function test_application_step4()
{   
    if (document.form_application_step4.occupant1_fname.value=="") {
	   alert ("Please enter your first name");
	   return false;  
       } else if (document.form_application_step4.occupant1_lname.value=="") {
	   alert ("Please enter your last name");
	   return false;  	   
       } else {
	   document.form_application_step4.action = "rental_application_s5.php";
	   document.form_application_step4.submit();
    } 
}

// Validates rental application step 5
function test_application_step5()
{   
    if (document.form_application_step5.gmi.value=="") {
	alert ("Please enter your gross income");
	return false;   
    } else if (document.form_application_step5.ssn1.value=="") {
	alert ("Please enter your social security number");
	return false;  
    } else if (document.form_application_step5.ssn2.value=="") {
	alert ("Please enter your social security number");
	return false;  
    } else if (document.form_application_step5.ssn3.value=="") {
	alert ("Please enter your social security number");
	return false;  	
    } else if (document.form_application_step5.licensenumber.value=="") {
	alert ("Please enter your driver license number");
	return false;  	 
    } else if (document.form_application_step5.id_state.value=="") {
	alert ("Please select your driver license state");
	return false;  
    } else if (document.form_application_step5.address1_line1.value=="") {
	alert ("Please enter your current street addresss");
	return false;
    } else if (document.form_application_step5.city1.value=="") {
	alert ("Please enter your current city addresss");
	return false;
    } else if (document.form_application_step5.state1.value=="") {
	alert ("Please enter your current state addresss");	 
	return false;
    } else if (document.form_application_step5.zip1.value=="") {
	alert ("Please enter your current zip code");
	return false;
    } else if (document.form_application_step5.time1.value=="") {
	alert ("Please select time at your currrent address");	   
	return false; 
    } else if (document.form_application_step5.move1.value=="") {
	alert ("Please enter reason for moving from current address");	   
	return false; 	   
    } else if (document.form_application_step5.address2_line1.value=="") {
	alert ("Please enter your previous street addresss");
	return false;
    } else if (document.form_application_step5.city2.value=="") {
	alert ("Please enter your previous city addresss");
	return false;
    } else if (document.form_application_step5.state2.value=="") {
	alert ("Please enter your previous state addresss");	 
	return false;
    } else if (document.form_application_step5.zip2.value=="") {
	alert ("Please enter your previous zip code");
	return false;
    } else if (document.form_application_step5.time2.value=="") {
	alert ("Please select time at your previous address");	   
	return false; 
    } else if (document.form_application_step5.move2.value=="") {
	alert ("Please enter reason for moving from previous address");	   
	return false; 	   
    } else {
	document.form_application_step5.action = "rental_application_s6.php";
	document.form_application_step5.submit();
    } 
}

// Validates rental application step 6
function test_application_step6()
{   
    if (document.form_application_step6.application_terms.checked == false) {
	   alert ("You did not check the I Agree check box.  It is required to complete the online lease application.");
	   return false; 	   
    } 
    else 
    {
	if (document.form_application_step6.sandbox.value == "1" || document.form_application_step6.sandbox.value == 1)
	{
		 document.form_application_step6.action = "https://www.sandbox.paypal.com/cgi-bin/webscr";
	     document.form_application_step6.submit();
	 }
	 else
	 {
		 document.form_application_step6.action = "https://www.paypal.com/cgi-bin/webscr";
	     document.form_application_step6.submit();
	 }
	 
    } 
}


// Validates contact form
function test_contact_form()
{
    if (document.form_contact.name.value=="" || document.form_contact.name.value=="Enter name" ) {
	   alert ("Please enter your name");
	   return false;  
       } else if (document.form_contact.phone.value=="" || document.form_contact.phone.value=="Enter phone number" ) {
	   alert ("Please enter your phone");
	   return false;  
       } else if (document.form_contact.emailid.value=="" || document.form_contact.emailid.value=="Enter email address") {
	   alert ("Please enter your user id (email address)");
	   return false;  
       } else if (document.form_contact.regarding.value=="") {
	   alert ("Please select a contact reason");
	   return false; 
       } else if (document.form_contact.contactTime.value=="") {
	   alert ("Please select a prefered contact time");
	   return false; 
       } else {
	   document.form_contact.action = "sendemail_contactus.php";
	   document.form_contact.submit();
    } 
}

// Fucntion to validate email addres format is correct befor making db call
// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
function ValidateEmail()
{
    var emailID = document.form_forgotpassword.emailaddress.value;
    
    if ((emailID==null)||(emailID==""))
    {
	alert("Please enter an email address");
	emailID.focus();
	return false;
    } else if (echeck(emailID)==false){
	emailID.value="";
	emailID.focus();
	return false;
    } else {
	document.form_forgotpassword.action = "forgotpassword_check.php";
	document.form_forgotpassword.submit();
    } 
}

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("Please enter a valid email ID");
	return false;
    }
    
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	alert("Please enter a valid email ID");
	return false;
    }
    
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	alert("Please enter a valid email ID");
	return false;
    }
    
    if (str.indexOf(at,(lat+1))!=-1){
	alert("Please enter a valid email ID");
	return false;
    }
    
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	alert("Please enter a valid email ID");
	return false;
    }
    
    if (str.indexOf(dot,(lat+2))==-1){
	alert("Please enter a valid email ID");
	return false;
    }
    
    if (str.indexOf(" ")!=-1){
	alert("Please enter a valid email ID");
	return false;
    }
    
    return true;					
}