// JavaScript Document
	
	
	function CheckForm (frm,confirmSave) {			
		SetPhoneNumbers();						
		if(Trim(frm.S2FirstName.value)==""){
			alert("Please fill up the First Name field in section 2 of this form.")
			frm.S2FirstName.focus();
			return false;
		}
		if(Trim(frm.S2LastName.value)==""){
			alert("Please fill up the Last Name field in section 2 of this form.")
			frm.S2LastName.focus();
			return false;
		}		
		
		if(frm.S1NeedHousing.selectedIndex==3 || frm.S1NeedHousing.selectedIndex==4 || frm.S1NeedHousing.selectedIndex==5){
			if(Trim(frm.S2CompanyName.value)==""){
				alert("Please fill up the Company Name field in section 2 of this form.")
				frm.S2CompanyName.focus();
				return false;
			}
		}	
		
		//check phone fields validity....
		if(Trim(frm.txtS2PPhoneStateCode.value).length!=0 || Trim(frm.txtS2PPhoneAreaCode.value).length!=0 || Trim(frm.txtS2PPhoneNo.value).length!=0){
				if(Trim(frm.txtS2PPhoneStateCode.value).length<3 || Trim(frm.txtS2PPhoneAreaCode.value).length<3 || Trim(frm.txtS2PPhoneNo.value).length<4){
						if(Trim(frm.S2InternationalPhone.value)!=""){
							alert("If you wish to fill Primary Phone Field along with International Phone field, please fill the Primary Phone completely.");
							frm.txtS2PPhoneStateCode.focus();
							return false;
						}
			
				}						
		}
		
		if(Trim(frm.S2PPhone.value)=="" && Trim(frm.S2InternationalPhone.value)==""){
			alert("Please fill up atleast one of the Primary Phone OR International Phone field in section 2 of this form.")
			frm.txtS2PPhoneStateCode.focus();
			return false;
		}

		
		if(Trim(frm.S2Email.value)==""){
			alert("Please fill up the Email field in section 2 of this form.");
			frm.S2Email.focus();
			return false;
		}		
		if(!CheckEmail(frm.S2Email.value)){
			alert("Please fill up a valid Email Address in section 2 of this form.");
			frm.S2Email.focus();
			return false;
		}
		if(Trim(frm.S2City.value)==""){
			alert("Please fill up the City field in section 2 of this form.");
			frm.S2City.focus();
			return false;
		}
		if(frm.S2State.selectedIndex==0){
			alert("Please select the State field in section 2 of this form.")
			frm.S2State.focus();
			return false;
		}
		
		if(frm.S2State.selectedIndex==1 ){
			if(Trim(frm.S2StateOutSide.value)==""){
				alert("Please provide the State Outside US/Canada field in section 2 of this form.")
				frm.S2StateOutSide.focus();
				return false;
			}
			
		}		

		
		if(Trim(frm.S4Location.value)==""){
			alert("Please fill up the Location field in section 3 of this form.");
			frm.S4Location.focus();
			return false;
		}
		if(!isValidLocationFormat(Trim(frm.S4Location.value),frm.S4Location))
			return false;
		
		

		//if(frm.S4PreferredArea.value == "")
			//{
         //		alert("Please enter Preferred Area.");
				//frm.S4PreferredArea.focus();
				//return false;
       //		}

		if(frm.S4PreferredArea.value.length>1000){
			alert("The maximum characters limit for the Preferred Area description in section 3 of this form is 1000 characters.");
			frm.S4PreferredArea.focus();
			return false;
		}
	

  if(Trim(frm.S4Location.value)=="Others,Others")
			{
				//alert("Please fill up the Location field in section 3 of this form.");
				//frm.S4Location.focus();
				//return false;
			
			if(frm.S4OtherLocation.value == "")
			{
          		alert("Please enter desired location not listed above.");
				frm.S4OtherLocation.focus();
				return false;
       		}

		}

		if(frm.S4OtherLocation.value.length>1000){
			alert("The maximum characters limit for the Desired Location description in section 3 of this form is 1000 characters.");
			frm.S4OtherLocation.focus();
			return false;
		}
		
		/*
		if(!frm.S5IsStudio.checked && !frm.S5IsOneBedOneBath.checked && !frm.S5IsTwoBedTwoBath.checked && !frm.S5IsThreeBedTwoBath.checked){
			if(Trim(frm.S5OtherDetail.value) == "" ){
				alert("Please select atleast one option in section 4 of this form, or indicate your requirements in the 'Other' textbox");
				frm.S5IsStudio.focus();
				return false;
			}
		}
		*/
		
		if(frm.S5IsStudio.checked) {
			if (Trim(frm.S5StudioCount.value)==""){
				alert("If you have checked on the \"A Studio\" option in section 4 of this form, then you must specfiy a value.");
				frm.S5StudioCount.focus();
				return false;
			}			
			if (isNaN(frm.S5StudioCount.value)){
				alert("Please fill in a numeric value for \"A Studio\" option in section 4 of this form.")
				frm.S5StudioCount.focus()
				return false;
			}else if(parseInt(frm.S5StudioCount.value)<=0){
				alert("Please fill in a value greater than zero or uncheck your selection for \"A Studio\" option in section 4 of this form.");
				frm.S5StudioCount.focus()
				return false;
			}else if(parseInt(frm.S5StudioCount.value)>255){
				alert("Max. value allowed for \"A Studio\" option in section 4 of this form is 255.");
				frm.S5StudioCount.focus()
				return false;
			}
		}
		if(frm.S5IsOneBedOneBath.checked) {
			if (Trim(frm.S5OneBedOneBathCount.value)==""){
				alert("If you have checked on the \"One bedroom/one bath\" option in section 4 of this form, then you must specfiy a value.");
				frm.S5OneBedOneBathCount.focus();
				return false;
				}
				if (isNaN(frm.S5OneBedOneBathCount.value)){
				alert("Please fill in a numeric value for \"One bedroom/one bath\" option in section 4 of this form.")
				frm.S5OneBedOneBathCount.focus()
				return false;
			}else if(parseInt(frm.S5OneBedOneBathCount.value)<=0){
				alert("Please fill in a value greater than zero or uncheck your selection for  \"One bedroom/one bath\" option in section 4 of this form.");
				frm.S5OneBedOneBathCount.focus()
				return false;
			}else if(parseInt(frm.S5OneBedOneBathCount.value)>255){
				alert("Max. value allowed for  \"One bedroom/one bath\" option in section 4 of this form is 255.");
				frm.S5OneBedOneBathCount.focus()
				return false;
			}
		}
		if(frm.S5IsTwoBedTwoBath.checked) {
			if (Trim(frm.S5TwoBedTwoBathCount.value)==""){
				alert("If you have checked on the \"Two bedroom/two baths\" option in section 4 of this form, then you must specfiy a value.");
				frm.S5TwoBedTwoBathCount.focus();
				return false;
				}
				
			if (isNaN(frm.S5TwoBedTwoBathCount.value)){
				alert("Please fill in a numeric value for \"Two bedroom/two baths\" option in section 4 of this form.")
				frm.S5TwoBedTwoBathCount.focus()
				return false;
			}else if(parseInt(frm.S5TwoBedTwoBathCount.value)<=0){
				alert("Please fill in a value greater than zero or uncheck your selection for \"Two bedroom/two baths\" option in section 4 of this form.")
				frm.S5TwoBedTwoBathCount.focus()
				return false;
			}else if(parseInt(frm.S5TwoBedTwoBathCount.value)>255){
				alert("Max. value allowed for \"Two bedroom/two baths\" option in section 4 of this form 255.")
				frm.S5TwoBedTwoBathCount.focus()
				return false;
			}
		}
		if(frm.S5IsThreeBedTwoBath.checked) {
			if (Trim(frm.S5ThreeBedTwoBath.value)==""){
				alert("If you have checked on the \"Three bedrooms/two baths\" option in section 4 of this form, then you must specfiy a value.");
				frm.S5ThreeBedTwoBath.focus();
				return false;
				}

			if (isNaN(frm.S5ThreeBedTwoBath.value)){
				alert("Please fill in a numeric value for \"Three bedrooms/two baths\" option in section 4 of this form.")
				frm.S5ThreeBedTwoBath.focus()
				return false;
			}else if(parseInt(frm.S5ThreeBedTwoBath.value)<=0){
				alert("Please fill in a value greater than zero or uncheck your selection for \"Three bedrooms/two baths\" option in section 4 of this form.")
				frm.S5ThreeBedTwoBath.focus()
				return false;
			}else if(parseInt(frm.S5ThreeBedTwoBath.value)>255){
				alert("Max. value allowed for \"Three bedrooms/two baths\" option in section 4 of this form is 255.")
				frm.S5ThreeBedTwoBath.focus()
				return false;
			}
		}		
		
		if(frm.S5OtherDetail.value.length>100){
			alert("The maximum characters limit for the Other Requirements of furnished appartment in section 4 of this form is 100 characters.");
			frm.S5OtherDetail.focus();
			return false;
		}
//_______________________________________________________________________
		
 if(Trim(frm.S6NumAdults.value)==""){
			alert("Please fill up atleast one Adults field in section 5 of this form.")
			frm.S6NumAdults.focus();
			return false;
		}
		 if (isNaN(frm.S6NumAdults.value))
			{
						alert("Please fill in a numeric value for Adults field in section 5 of this form.");
						frm.S6NumAdults.focus();
						return false;
			}
 if(Trim(frm.S6NumChildren.value)==""){
			alert("Please fill up the Children field in section 5 of this form.")
			frm.S6NumChildren.focus();
			return false;
		}
	 if (isNaN(frm.S6NumChildren.value))
			{
						alert("Please fill in a numeric value for Children field in section 5 of this form.");
						frm.S6NumChildren.focus();
						return false;
			}
//____________________________________________________________________________________

	if(frm.S7Period.selectedIndex<=0){
			alert("Please select the \"Duration\" in section 6 of this form");
			frm.S7Period.focus();
			return false;			
	}
	
//____________________________________________________________________________________
									
//____________________________________________________________________________________
		if(checkMoveInDt()==false) //check the movein date
		{
		alert("Move-In date in section 6 of this form, should occur on or after today's date.");			
		frm.S7MoveInDate.focus();
		return false;
		}		
		GetDateAfter30Days();  //function to get the date after 30 days from the user selected date
		if(checkMoveOutDt()==false) //check the moveout date
		{
			alert("Move-Out date in section 6 of this form, should be atleast 30 days after the selected Move-In date.");
			frm.S7MoveOutDate.focus();
			return false;
			}
//____________________________________________________________________________________

		if (frm.S8Range.selectedIndex == 0 ) {
			window.alert ("Please select the \"Price Range\" in Section 7 of this form.");
			frm.S8Range.focus();
			return false;
		}
		if(frm.S9Pets[1].checked)
		{
			if (frm.S9IsDogs.checked==false && frm.S9IsCats.checked==false )
			{
				alert("Please select and give information about atleast one pet in section 8 of this form.");
				return false;
			}				
		 }

		if (frm.S9Pets[1].checked) {
			if (frm.S9IsDogs.checked) {			
				if (Trim(frm.S9NumDogs.value)=="" ) {
					window.alert ("Please enter the number of Dogs in Section 8 of this form.");
					frm.S9NumDogs.focus();
					return false;
				}
				
				if (isNaN(frm.S9NumDogs.value) ) {
					window.alert ("Please enter the number of Dogs in Section 8 of this form, in numeric form.");
					frm.S9NumDogs.focus();
					return false;
				}
				
				if (parseInt(frm.S9NumDogs.value)>255 ) {
					window.alert ("Max. value for number of Dogs in Section 8 of this form is 255.");
					frm.S9NumDogs.focus();
					return false;
				}
				
				if (Trim(frm.S9DogBreeds.value) == "") {
					window.alert ("Please enter the Breed(s) of your Dog(s) in Section 8 of this form.");
					frm.S9DogBreeds.focus();
					return false;
				}
				if (Trim(frm.S9DogWeights.value) == "") {
					window.alert ("Please enter the Weights of your Dog(s) in Section 8 of this form.");
					frm.S9DogWeights.focus();
					return false;
				}
			}
			if (frm.S9IsCats.checked) {
				if (Trim(frm.S9NumCats.value)=="" ) {
					window.alert("Please enter the number of Cats in Section 8 of this form.");
					frm.S9NumCats.focus();
					return false;
				}
				if (isNaN(frm.S9NumCats.value)) {
					window.alert("Please enter the number of Cats in Section 8 of this form, in numeric form.");
					frm.S9NumCats.focus();
					return false;
				}
				if (parseInt(frm.S9NumCats.value)>255) {
					window.alert ("Max. value for number of Cats in Section 8 of this form is 255.");
					frm.S9NumCats.focus();
					return false;
				}

			}
		}		


		if(frm.S10Special.value.length>3000){
			alert("The maximum characters limit for the Other Special Requirements in section 9 of this form is 3000 characters.");
			frm.S10Special.focus();
			return false;
		}

		if(confirmSave==true){
				if(window.confirm("Do you want to save this lead ?"))
					frm.submit();	
		}
		else{
			frm.submit();
		}
	}

//new functions---------------------------------------------------------------->		
	//function to check a valid location format: city,state	
	//atleast one comma, with some text after comma. 
	function isValidLocationFormat(val,ctl){
		if(val.indexOf(",",0)==-1){
			alert("The Location field in section 3 of this form should be in a comma seprated form. Like: city, state");
			ctl.focus();
			return false;
		}
		
		if(val.indexOf(",",0)==(val.length-1) || val.indexOf(",",0)==0){
			alert("The Location field in section 3 of this form should be in a comma seprated form. Like: city, state");
			ctl.focus();
			return false;
		}
		
		if(val.indexOf(",",0)==val.lastIndexOf(",",0)){
			alert("The Location field in section 3 of this form should be in a comma seprated form. Like: city, state");
			ctl.focus();
			return false;
		}
		return true;
	}

	function checkMoveInDt()
	{
 		var todayDt=new Date(frm.moveInDt.value);	
		var selectedDt=new Date(frm.S7MoveInDate.value);
		var selectedYear= selectedDt.getFullYear();//frm. CF_DATEINPUT_YEAR_1.value
		var selectedDate=selectedDt.getDate();//frm.CF_DATEINPUT_DAY_1.value
		var selectedMonth=selectedDt.getMonth();//eval(frm.CF_DATEINPUT_MONTH_1.value)
		var isvalid=false;
		var validDate

		if(selectedYear>todayDt.getFullYear()){
			isvalid=true;
			
			}
			else if(selectedYear==todayDt.getFullYear()){
				if (selectedMonth > todayDt.getMonth()){
					isvalid=true;
				}else if(selectedMonth==todayDt.getMonth()){
					if(selectedDate>=todayDt.getDate()){
						isvalid=true;
					}
					else
						isvalid= false;
					}
				else{	
					isvalid=false; 
					}				
			 } 
			 else {
			 isvalid=false; 
			 }			  
		return isvalid;
	 }
//check the moveout date................
function checkMoveOutDt(){
		var moveOutDt=new Date(frm.moveOutDt.value);
		var moSelectedDt=new Date(frm.S7MoveOutDate.value);
		var moselectedYear=moSelectedDt.getFullYear(); 
		var moselectedDate=moSelectedDt.getDate(); 
		var moselectedMonth=moSelectedDt.getMonth();
		var valid=false
		var validDate
		if(moselectedYear>moveOutDt.getFullYear())	
		{
			valid=true;
				}
		else if(moselectedYear==moveOutDt.getFullYear())
		{
			if (moselectedMonth > moveOutDt.getMonth())
			{	
				valid=true;
						}				
				else if(moselectedMonth==moveOutDt.getMonth())
				{
					if(moselectedDate>=moveOutDt.getDate())	
						valid=true;
					else
						valid= false;
					}
				else
				{	
					valid=false; 
					}				
		 }
		 else
		 { 	
		 	valid=false;
			 }
		 if (valid==true)  
		 {
		 	//validDate=(moselectedMonth+1)+"/"+moselectedDate+"/"+moselectedYear			 
			//frm.moveOutDt.value=validDate;	 //set the hidden value
			  }			  
		return valid;
	 }
	 
//function to get date after 30days from user selected date
 function GetDateAfter30Days()
  {
  	var startDate=new Date(frm.S7MoveInDate.value);
	var s_day=startDate.getDate();
	var s_month=startDate.getMonth();
	var s_year=startDate.getFullYear();		
	var d_count=1;
	var leap_month_days=new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	var month_days=new Array(31,28,31,30,31,30,31,31,30,31,30,31);	  	
		while(d_count<30)
		{		
			if((s_year%4==0) && ((s_year % 100 != 0) || (s_year % 400 == 0))) //leap year
			{
				if(s_day==leap_month_days[s_month])
			   {
			   		s_day=0
					s_month++;					
					if(s_month==12)
					{
						s_month=0
						s_year++;
					}	
			     }		
			 }
			 else   //not leap year--------------------------------------------------------
			 {			 
			   if(s_day==month_days[s_month])
			   {
			   		s_day=0
					s_month++;
					if(s_month==12)
					{
						s_month=0					
						s_year++;
						}
			     }
			  }//////////////////////////////////////////////////////////////////			  			  
		s_day++;
		d_count++;			  
		 }	//loop over
		 //set the hidden alue
		 frm.moveOutDt.value= (s_month+1)+"/"+s_day+"/"+s_year;
	}//function over...	 
//new validation functions over------------------------------------------------------------>

 
//-----------pets validations----------------------------
function displayPetsTable()
{
	if(frm.S9Pets[1].checked)
		document.all.petsTable.style.display=""
	else
		document.all.petsTable.style.display=""
 }
function togglePetsTable(val)
{
var tab=document.getElementById("petsTable");
if(eval(val)==1) 
	tab.style.display=""
else
	tab.style.display="none"

if (eval(val)==0)
{
 frm.S9IsDogs.checked=false;
 frm.S9NumDogs.value="";
 frm.S9DogBreeds.value="";
 frm.S9DogWeights.value="";
 frm.S9IsCats.checked=false;
 frm.S9NumCats.value=""
 }
}

//--------------------------------------------------------------			  
 function ClearDogDetails()
  {
	if(frm.S9IsDogs.checked==false)
	{			   
		 frm.S9NumDogs.value="";
		 frm.S9DogBreeds.value="";
		 frm.S9DogWeights.value="";	
	 }
   }
//------------------------------------------------------------------
  function ClearCatDetails()
  {
	if (frm.S9IsCats.checked==false)
		 frm.S9NumCats.value="";
   }
   
   function clearAppartmentDetail(chk,target){
	   if(!chk.checked)
		   	target.value=""
		target.readOnly=!chk.checked
   }
   
function checkDog()
{
	if(Trim(frm.S9NumDogs.value)=="" && Trim(frm.S9DogBreeds.value)=="" && Trim(frm.S9DogWeights.value)=="" )
		frm.S9IsDogs.checked=false
	else
		frm.S9IsDogs.checked=true					
 }
//--------------------------------------------------------------	 
 function checkCat()
 {
	if(Trim(frm.S9NumCats.value)=="")
		frm.S9IsCats.checked=false
	else
		frm.S9IsCats.checked=true
  }
//-----------pets validations over----------------------------
 

	function nextFocus(fromctl,targetCtl)
	{
		if(fromctl.value.length==fromctl.maxLength)
			targetCtl.focus();			
	 }
	 
	 function SetPhoneNumbers() {		 
		if(Trim(frm.txtS2PPhoneStateCode.value).length==3 && Trim(frm.txtS2PPhoneAreaCode.value).length==3 && Trim(frm.txtS2PPhoneNo.value).length==4)
		{
			frm.S2PPhone.value=frm.txtS2PPhoneStateCode.value+"-"+frm.txtS2PPhoneAreaCode.value+"-"+frm.txtS2PPhoneNo.value
			if(Trim(frm.S2PPhoneExt.value)!="")
				frm.S2PPhone.value+= "-"+frm.S2PPhoneExt.value
		}else
			frm.S2PPhone.value=""

		if(Trim(frm.txtS2OPhoneStateCode.value).length==3 && Trim(frm.txtS2OPhoneAreaCode.value).length==3 && Trim(frm.txtS2OPhoneNo.value).length==4){
	  		frm.S2OPhone.value=frm.txtS2OPhoneStateCode.value+"-"+frm.txtS2OPhoneAreaCode.value+"-"+frm.txtS2OPhoneNo.value
			if(Trim(frm.S2OPhoneExt.value)!="")
				frm.S2OPhone.value+= "-"+frm.S2OPhoneExt.value
		}else
			frm.S2OPhone.value=""
		
		//fax numbers
		if(Trim(frm.txtS2FaxStateCode.value).length==3 && Trim(frm.txtS2FaxAreaCode.value).length==3 && Trim(frm.txtS2FaxNo.value).length==4){
	  		frm.S2Fax.value=frm.txtS2FaxStateCode.value+"-"+frm.txtS2FaxAreaCode.value+"-"+frm.txtS2FaxNo.value
			if(Trim(frm.S2FaxExt.value)!="")
				frm.S2Fax.value+= "-"+frm.S2FaxExt.value
		}else
			frm.S2Fax.value=""
		} 



