// File: validation.js
// Author: Sean Ryan ( sryan@evercrack.com )
// Synopsis: Form validation routines.  Attempted to make these independant 
//           of which site was being worked on (so that they can be shared).
// Last Modified: 20031215

//===================================================================
// Standard check for an email address
// Args: addr -- the email address in question
// Returns: true on valid, false on fail
function validate_email(addr) {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(addr);	
}
//===================================================================
//===================================================================
// validate_element
// Args: element -- the selection element either a dropdown or checkbox
// return: true/false
// Note: basically takes a single element or array, checks to see at least 
//       is selected.
function validate_areas(areas) {

   // if the .value is null ... means that it's not a discrete value( ie array )
   // it's flawed ... but anyway at 2am I don't care much
   if ( areas.value == null ) {

	// do array checking for checkboxes ... at least one is needed
	var area_checked = false;
	for( var i=0; i<areas.length; i++ ) {
	  if ( areas[i].checked ) { area_checked = true; }
	}
	if ( ! area_checked ) { return false; }

   } else {

	// do value checking ... single element tag
	// nothing was selected
	if(areas.value=="" ){ return false; } 

   } // end if chi_areas

   return true;

}
//===================================================================
// createRange()
// Synopsis: creates a range str val from a min/max pair
// fmin - form element that has the minimum value
// fmax - form element that has the maximum value
// lp   - form element that will contain the resulting range
function createRange(fmin,fmax,lp) {

	// Combine price into range
        strMin = fmin.value;
        strMax = fmax.value;
	strVal = "";

        if (strMax == "") {
	  // only min selected
          strVal = strMin + "+";
        } else {
	  // max was selected.
          if  (strMin == "") {
	    // only max was selected
            strVal = strMax + "-";
          } else {
	    // both min and max were selected.
            strVal = strMin + "-" + strMax;
          }
        }
	lp.value = strVal;

}// end createRange
//===================================================================
//===================================================================
// checkform_VEGAS
// Args: inform -- the form for dreamtown submissions
function checkform_VEGAS( inform ) {

   // we want a first name
   if(inform.first_name.value=="") {alert("Please fill out first name");inform.first_name.focus();return false;}

   // we want a last name
   if(inform.last_name.value==""){alert("Please fill out last name");inform.last_name.focus();return false;}

   // we want an email addy
   if(inform.email.value==""){alert("Please fill out email");inform.email.focus();return false;}

   // test for valid email address
   if( ! validate_email(inform.email.value) ) { alert("Email Address is formatted incorrectly"); inform.email.focus(); return false; }

   // if they searched ... and all the data passed our checks.
   var today = new Date();
   var expiry = new Date(today.getYear() + 1, today.getMonth(), today.getDate()); // plus 1 year
   setCookie("vegas_member",inform.email.value,expiry);

   return true;

}
//===================================================================

//===================================================================
// checkform_SOAR
// Args: inform -- the form for dreamtown submissions
function checkform_SOAR( inform ) {

   // we want a first name
   if(inform.TempFirstName.value=="") {alert("Please fill out first name");inform.TempFirstName.focus();return false;}

   // we want a last name
   if(inform.TempLastName.value==""){alert("Please fill out last name");inform.TempLastName.focus();return false;}

   // we want an email addy
   if(inform.TempEMail.value==""){alert("Please fill out email");inform.TempEMail.focus();return false;}

   // test for valid email address
   if( ! validate_email(inform.TempEMail.value) ) { alert("Email Address is formatted incorrectly"); inform.TempEMail.focus(); return false; }

   // check that at least one area is present
   if ( ! validate_areas(inform.Areas) ) { alert("You must select at least one area to search in"); return false; }

   // if they searched ... and all the data passed our checks.
   setCookie("VegasHomeBuyer","1");

   return true;

}
//===================================================================
//===================================================================
// checkform_SOAR2
// Args: inform -- the form for dreamtown submissions
function checkform_SOAR2( inform ) {

   // we want a first name
   if(inform.TempFirstName.value=="") {alert("Please fill out first name");inform.TempFirstName.focus();return false;}

   // we want a last name
   if(inform.TempLastName.value==""){alert("Please fill out last name");inform.TempLastName.focus();return false;}

   // we want an email addy
   if(inform.TempEMail.value==""){alert("Please fill out email");inform.TempEMail.focus();return false;}
   
   // we want an email addy
   if(inform.TempEMail2.value==""){alert("Please confirm your email");inform.TempEMail2.focus();return false;}

   // we must check if they are identical
   if(inform.TempEMail.value!=inform.TempEMail2.value) {alert("Email and Confirmation Email do not match!");inform.TempEMail.focus();return false; }

   // test for valid email address
   if( ! validate_email(inform.TempEMail.value) ) { alert("Email Address is formatted incorrectly"); inform.TempEMail.focus(); return false; }

   // check that at least one area is present
   if ( ! validate_areas(inform.Areas) ) { alert("You must select at least one area to search in"); return false; }

   // if they searched ... and all the data passed our checks.
   setCookie("VegasHomeBuyer","1");

   return true;

}
//===================================================================

//===================================================================
// checkform_DT ( dreamtown )
// Args: inform -- the form for dreamtown submissions
// Note: for DT, important things are fname, lname, email, chi_areas
function checkform_DT(inform,popup) {

   FName=inform.fname.value;
   LName=inform.lname.value;
   EMail=inform.email.value;
   STTarget=inform.STTarget.value;
   STType=inform.STType.value;

   // we want a first name
   if(inform.fname.value=="") {alert("Please fill out first name");inform.fname.focus();return false;}

   // we want a last name
   if(inform.lname.value==""){alert("Please fill out last name");inform.lname.focus();return false;}

   // we want an email addy
   if(inform.email.value==""){alert("Please fill out email");inform.email.focus();return false;}

   // test for valid email address
   if( ! validate_email(inform.email.value) ) { alert("Email Address is formatted incorrectly"); inform.email.focus(); return false; }

   // check that at least one area is present
   if ( ! validate_areas(inform.chi_areas) ) { alert("You must select at least one area to search in"); return false; }


   // if they searched ... and all the data passed our checks.
   setCookie("ChicagoRealEstateCustomer","1");

   // if we passed in a popup, format vars and pop it up 
   if ( popup != null ) {

	var urlvars="?FName="+FName+"&LName="+LName+"&EMail="+
		     EMail+"&STTarget="+STTarget+"&STType="+STType;
	popcatch = popupwindow(popup+urlvars,"popcatch",275,400) 
   	popcatch.focus();
   }



   return true;

}
//===================================================================
//===================================================================
// checkmini_DT
// form
function checkmini_DT(inform,popup) {

   // collect info
   FName=inform.fname.value;
   LName=inform.lname.value;
   EMail=inform.email.value;
   STTarget=inform.STTarget.value;
   STType=inform.STType.value;

   // we want a first name
   if(inform.fname.value=="") {alert("Please fill out first name");inform.fname.focus();return false;}

   // we want a last name
   if(inform.lname.value==""){alert("Please fill out last name");inform.lname.focus();return false;}

   // we want an email addy
   if(inform.email.value==""){alert("Please fill out email");inform.email.focus();return false;}

   if( ! validate_email(inform.email.value) ) { alert("Email Address is formatted incorrectly"); inform.email.focus(); return false; }

   // setCookie
   setCookie("ChicagoRealEstateCustomer","1");

   // if we passed in a popup, format vars and pop it up
   if ( popup != null ) {

        var urlvars="?FName="+FName+"&LName="+LName+"&EMail="+
                     EMail+"&STTarget="+STTarget+"&STType="+STType;
        popcatch = popupwindow(popup+urlvars,"popcatch",275,400)
        popcatch.focus();
   }


   return true;
}
//===================================================================
