/****************************************************************************************************/
/* Created Date    Author           Comments ********************************************************/
/****************************************************************************************************/
/* 10/17/2007      Muni Mahimaluru  As part of the clean up this file is created for contactus.aspx*/
/* 02/04/2008      Harish S. Ram    Included the Validation and other scripts as per Enhancements/
/****************************************************************************************************/	
	/* Added by Harish */
		
		ToggleRealtor() ;
		ToggleMovingOther() ;
		//ToggleCurrentOther() ;
		ToggleCountry() ;
		
		/*This is to Alert the User */
		var msgFlag = document.getElementById("msgFlag")
		var message = document.getElementById("message")
		var Status = document.getElementById("msgStatus")
			
			if(msgFlag.value == "Y")
			{
				function messagedisplay(msgvalue)
				{
					alert(msgvalue)
					return false;
				}
				messagedisplay(message.value)
				msgFlag.value="N"
				
				//To check whether the values are saved and Mail is sent
				if(Status.value == "Y")
				{
					var omniturevalue = document.getElementById("hidOmniture").value ;
					
					//Call the Omniture function to send the values to Omniture
					LoadOmniture(omniturevalue);
					
					//Redirecting to the Contact Us Main Page
					location.replace("contactUsmain.aspx"); 
				}
			}
			
		
		function GetParentElement(elementID)
		{
			var divElements = document.getElementsByTagName('DIV');
			var elem;
			var elem2;
			var parent = document;
				
			for (elem=0; (parent == document) && (elem < divElements.length); elem++)
			{
				if (divElements[elem].hasChildNodes())
				{
					for (elem2=0; (parent == document) && (elem2 < divElements[elem].childNodes.length); elem2++)
					{
						if (divElements[elem].childNodes[elem2].id == elementID)
						{
							parent = divElements[elem];
						}	
					}		
				}		
			}			
			return parent;
		}	
			
			
		function DivisionOnClick(evnt)
		{
			var imgElement = (document.all) ? window.event.srcElement : evnt.target;
			var divElementID = "div" + imgElement.id.substring(3);
			var divElement = document.getElementById(divElementID);
			
			divElement.style.display = (imgElement.src.search(/expand\.gif/i) > 0) ? "block" : "none";
			imgElement.src = (imgElement.src.search(/expand\.gif/i) > 0) ? "/images/collapse.gif" : "/images/expand.gif";
			
			return false;
		}
		
		
		//Added by Harish
		function ToggleCollapse(evnt)
		{
			var chkElement = (document.all) ? window.event.srcElement : evnt.target;
			var divElementID = "div" + chkElement.id.substring(3)
			var divElement = document.getElementById(divElementID);
			var imgElementID = "img" + chkElement.id.substring(3)
			var imgElement = document.getElementById(imgElementID);
		
			divElement.style.display =  "block" ;
			imgElement.src =  "/images/collapse.gif" ;
		}
		
		function ToggleExpand(evnt)
		{
			
			var chkElement = (document.all) ? window.event.srcElement : evnt.target;
			var divElementID = "div" + chkElement.id.substring(3)
			var divElement = document.getElementById(divElementID);
			var imgElementID = "img" + chkElement.id.substring(3)
			var imgElement = document.getElementById(imgElementID);
		
			divElement.style.display =  "none" ;
			imgElement.src =  "/images/expand.gif" ;
		}


		function DivisionCheckOnClick(evnt)
		{	
			var chkElement = (document.all) ? window.event.srcElement : evnt.target;
			var hidElementID = "hid" + chkElement.id.substring(3)
			var hidElement = document.getElementById(hidElementID);
			var divElementID = "div" + chkElement.id.substring(3)
			var divElement = document.getElementById(divElementID);
			
			var elem;
			var elem2;
			
			if (document.all)
			{
				
				for (elem in divElement.all)
				{
					if (elem.substring(0,3) == "chk") 
					{
						
						if (chkElement.checked) 
						{
							divElement.all[elem].checked = true ;
							ToggleCollapse(divElement)
						}
						else
						{
							divElement.all[elem].checked = false ;
							ToggleExpand(divElement)
							
						}
						/*
						if (divElement.all[elem].checked)
						{
							window.event.returnValue = false;
							return false;
						} */
					}
				}
			}				
			else
			{				
				for (elem=0; elem < divElement.childNodes.length; elem++)
				{
					
					elem2 = divElement.childNodes[elem];
					if (elem2.id)
					{
						if (elem2.id.substring(0,3) == "chk") 
						{
							if (elem2.checked)
							{
								chkElement.checked = true;
								return false;
							}	
						}	
					}	
				}	
			}	
			hidElement.value = chkElement.checked ? "1" : "0";
			return true;
		}


		function NeighborhoodOnClick(evnt)
		{
			var srcElement = (document.all) ? window.event.srcElement : evnt.target;
			var divElement = (document.all) ? srcElement.parentElement : GetParentElement(srcElement.id);
			var chkElementID = "chk" + divElement.id.substring(3)
			var chkElement = document.getElementById(chkElementID);
			var hidElementID = "hid" + divElement.id.substring(3)
			var hidElement = document.getElementById(hidElementID);
			var elem;
			var checked = srcElement.checked;
			
			if (!checked)
			{
				
				for (i = 0; i < divElement.childNodes.length; i++)
				{
					
					elem = divElement.childNodes[i];
					if (elem.nodeType == 1)
					{
						if (elem.id.substring(0,3) == "chk") 
						{
							if (document.getElementById(elem.id).checked)
							{
								checked = true;
								break;
							}	
						}	
					}	
				}
			}	
			if (checked)
			{
				
				chkElement.checked = true;
			}	
			else
			{
				//chkElement.checked = (hidElement.value == "1");
				chkElement.checked = false ;
			}	
			return false;
		}
		
		
		//To Validate the Form fields before saving them
		function ValidateForm(event, obj, objID)
		{
			var sId;
			var ctl;
			var ct2;
			
			
			//The form data will be validated and Saved 
			//only if the user selects the ContactMe Check box
		
			sId = obj.id;
			sId = sId.replace(objID, "chkContactMe")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				if (!ctl.checked)
				{
					alert("Your request to join the interest list cannot be processed without your permission to contact you via e-mail, telephone, or other means.");
					return false;
				}
			}
			
			//Validate First Name
			sId = obj.id;
			sId = sId.replace(objID, "txtFirstName")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				if (ctl.value == "")
				{
					alert("Please enter your First Name.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}
			
			//Validate Last Name
			sId = obj.id;
			sId = sId.replace(objID, "txtLastName")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				if (ctl.value == "")
				{
					alert("Please enter your Last Name.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}
			
			//To validate the email address		
			sId = obj.id;
			sId = sId.replace(objID, "txtEmail")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				if (ctl.value == "")
				{
					alert("Please enter Your Email Address.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
				if (!ctl.value.match(regEmail))
				{
					alert('Please enter a valid Email Address.');
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}		
			}
			
			//Validate confirm Email Address
			sId = obj.id;
			sId = sId.replace(objID, "txtEmailConfirm")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				sEmail2 = ctl.value;
				sId = obj.id;
				sId = sId.replace(objID, "txtEmail")
				ctl = document.getElementById(sId);
				if (ctl)
				{
					while (ctl.value.search(/^\s+/) >= 0)		
						ctl.value = ctl.value.replace(/^\s+/, "");
					while (ctl.value.search(/\s+$/) >= 0)		
						ctl.value = ctl.value.replace(/\s+$/, "");	
					sEmail = ctl.value;
					if (sEmail != sEmail2) 
					{
						alert("Email and Confirmation of Email do not match.");
						event.cancelBubble = true;
						event.returnValue = false;
						ctl.focus();
						return false;
					}		
				}
			}		
			
			// To Validate the Secondary email			
			sId = obj.id;
			sId = sId.replace(objID, "txtSecondaryEmail")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				
				if ((ctl.value != "") && (!ctl.value.match(regEmail)))
				{
					alert('Please enter a valid Secondary Email Address.');
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}		
			}
			
			//to validate the Cell phone number
			sId = obj.id;
			sId = sId.replace(objID, "txtCellPhone")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				
				/*
				if (ctl.value == "")
				{
					alert("Please enter your 10 digit Cell phone number, starting with the area code.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
				*/
				if ((ctl.value != "") && (!ctl.value.match(regPhone)))
				{
					alert("Please enter your 10 digit Cell phone number, starting with the area code.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}

						
			// To validate the Work Phone Number
			sId = obj.id;
			sId = sId.replace(objID, "txtWorkPhone")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				if ((ctl.value != "") && (!ctl.value.match(regPhone)))
				{
					alert("Please enter your 10 digit Work phone number, starting with the area code.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}
			
			//To validate the Home phone number
			sId = obj.id;
			sId = sId.replace(objID, "txtHomePhone")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				if ((ctl.value != "") && (!ctl.value.match(regPhone)))
				{
					alert("Please enter your 10 digit Home phone number, starting with the area code.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}
			
			
			// To check whether user has provided the Prefered contact
			sId = obj.id;
			sId = sId.replace(objID, "ddlPreferedContact")
			ctl = document.getElementById(sId);
			var Contact = ctl.options[ctl.selectedIndex].value
			var Email = document.getElementById("txtEmail")
			var SecondaryEmail = document.getElementById("txtSecondaryemail");
			var CellPhone = document.getElementById("txtCellPhone");
			var HomePhone = document.getElementById("txtHomePhone");
			var WorkPhone = document.getElementById("txtWorkPhone");
			
						
			if (ctl)
			{
				if ((Contact == 1) && Email.value == "")
				{
					alert("You have selected Email as your Preferred contact. \n So please enter your Email");
					event.cancelBubble = true;
					event.returnValue = false;
					Email.focus();
					return false;
				} 
				
				if ((Contact == 2) && SecondaryEmail.value =="")
				{
					alert("You have selected Secondary Email as your Preferred contact.\n So please enter your Secondary Email");
					event.cancelBubble = true;
					event.returnValue = false;
					SecondaryEmail.focus();
					return false;
				} 
				
				if ((Contact == 3) && CellPhone.value =="")
				{
					alert("You have selected Cell Phone as your Preferred contact.\n So please enter your Cell Phone No.");
					event.cancelBubble = true;
					event.returnValue = false;
					CellPhone.focus();
					return false;
				} 
				
				if ((Contact == 4) && HomePhone.value =="")
				{
					alert("You have selected Home Phone as your Preferred contact.\n So please enter your Home Phone No.");
					event.cancelBubble = true;
					event.returnValue = false;
					HomePhone.focus();
					return false;
				} 
				
				if ((Contact == 5) && WorkPhone.value =="")
				{
					alert("You have selected Work Phone as your Preferred contact.\n So please enter your Work Phone No.");
					event.cancelBubble = true;
					event.returnValue = false;
					WorkPhone.focus();
					return false;
				} 
				
			}	
			
			// To validate the State and Zip based on the Selected county
			var selectedCountry = document.getElementById("ddlCountry").value
			
			sId = obj.id;
			sId = sId.replace(objID, "ddlCountry")
			ctl = document.getElementById(sId);
			var State = document.getElementById("ddlState");
			
				
			if (ctl)
			{
				
				//If the selected country is "United states"
				if (ctl.options[ctl.selectedIndex].value == 233)
				{
					//Check for the Zip code field
					ct2 = document.getElementById("txtZip");
					if (ct2)
					{
						while (ct2.value.search(/^\s+/) >= 0)		
							ct2.value = ct2.value.replace(/^\s+/, "");
						while (ctl.value.search(/\s+$/) >= 0)		
							ct2.value = ct2.value.replace(/\s+$/, "");	
						
						if ((ct2.value != "") && (!ct2.value.match(regZip)))
						{
							alert("Please enter a valid Zip Code.");
							event.cancelBubble = true;
							event.returnValue = false;
							ct2.focus();
							return false;
						}
					}	
				}
				
			}
						
			
			sId = obj.id;
			sId = sId.replace(objID, "txtRealtorZip")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				
				if ((ctl.value != "") && (!ctl.value.match(regZip)))
				{
					alert("Please enter a valid REaltor Zip Code.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}		
			
			sId = obj.id;
			sId = sId.replace(objID, "txtRealtorCellPhone")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				if ((ctl.value != "") && (!ctl.value.match(regPhone)))
				{
					alert("Please enter your 10 digit Realtor Cell Phone number, starting with the area code.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}
			
			sId = obj.id;
			sId = sId.replace(objID, "txtRealtorWorkPhone")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				if ((ctl.value != "") && (!ctl.value.match(regPhone)))
				{
					alert("Please enter your 10 digit Realtor Work Phone number, starting with the area code.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}
				
			sId = obj.id;
			sId = sId.replace(objID, "txtRealtorEmail")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				
				if (!ctl.value.match(regEmail))
				{
					alert('Please enter a valid Realtor Email Address.');
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}		
			}
			
			
			sId = obj.id;
			sId = sId.replace(objID, "hidMode")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
				if (ctl.value == "Mortgage")
				{
					var checkboxes = document.getElementsByTagName("input");
					
					for (i = 0; i < checkboxes.length; i++)
					{
						ctl = checkboxes[i];

						if (ctl.id.substring(0,3) == "chk")
						{
							if(ctl.checked)
							{
								return true;
							}
						}		
					}
					alert("Please select at least one Division or Neighborhood.");
					event.cancelBubble = true;
					event.returnValue = false;
					return false;	 
				}
			}
			
			sId = obj.id;
			sId = sId.replace(objID, "txtComments")
			ctl = document.getElementById(sId);
			if (ctl)
			{
				while (ctl.value.search(/^\s+/) >= 0)		
					ctl.value = ctl.value.replace(/^\s+/, "");
				while (ctl.value.search(/\s+$/) >= 0)		
					ctl.value = ctl.value.replace(/\s+$/, "");	
								
				if (ctl.value.length > 1024 )
				{
					alert("Please shorten your Comment.");
					event.cancelBubble = true;
					event.returnValue = false;
					ctl.focus();
					return false;
				}
			}
			return true;
        }  

		
	
	
	
		function ToggleRealtor()
		{
			try
			{
				
				var realtorSection = document.getElementById("tbRealtor");
				var selectedvalue = document.getElementById("ddlFoundThrough").value
				//var foundThroughOther = document.getElementById("txtFoundThroughOther");
				
				
				
				if (selectedvalue == 12)
				{
					realtorSection.style.display = "inline";
					//foundThroughOther.style.display = "none";
					//foundThroughOther.value="" ;
				}
				/*else if (selectedvalue == 16)
				{
					ClearValues();
					foundThroughOther.style.display = "inline";
					realtorSection.style.display = "none";
				}*/
				else
				{
					ClearValues();
					//foundThroughOther.value="" ;
					//foundThroughOther.style.display = "none";
					realtorSection.style.display = "none";
				}
			}
			catch(err)
			{
					alert(err.description);
			}
		
		}
		
		
		function ToggleMovingOther()
		{
			try
			{
			
				var movingOther = document.getElementById("txtMovingReasonOther");
				var selectedValue = document.getElementById("ddlMovingreason").value
				
				
				
				if ( selectedValue == 7)
				{
					
					movingOther.style.display = "inline";
				}
				else
				{
					movingOther.value = "";
					movingOther.style.display = "none";
				}
			}
			catch(err)
			{
				alert(err.description);
			}
		}
		
		
		function ToggleCurrentOther()
		{
			try
			{
				var currentOther = document.getElementById("txtCurrentSituationOther");
				var selectedValue = document.getElementById("ddlCurrentSituation").value
				
				
				if ( selectedValue == 5)
				{
					
					currentOther.style.display = "inline";
					
				}
				else
				{
					currentOther.value = "" ;
					currentOther.style.display = "none";
				}
			}
			catch(err)
			{
				alert(err.description);
			}
		
		}
		
		function ToggleCountry()
		{
			try
			{
				var State = document.getElementById("ddlState");
				var Zip = document.getElementById("txtZip");
				var OtherState = document.getElementById("txtOtherState");
				var OtherZip = document.getElementById("txtOtherZip");
				var selectedCountry = document.getElementById("ddlCountry").value
				
				
				//If selected country is "United States"
				if (selectedCountry == 233)
				{
					OtherState.style.display = "none";
					OtherZip.style.display = "none";
					OtherState.value = "" ;
					OtherZip.value = "" ;
						
					State.style.display = "inline";
					Zip.style.display = "inline";
				}
				else
				{
					OtherState.style.display = "inline";
					OtherZip.style.display = "inline";
					
					State.style.display = "none";
					Zip.style.display = "none";
					State.selectedIndex = 0 ;
					Zip.value = "" ;
				}
			}
			catch(err)
			{
				alert(err.description);
			}
		}
		
		function PopContact()
		{
			//The form data will be validated and Saved 
			//only if the user selects the ContactMe Check box
			ctl = document.getElementById("chkContactMe");
			
			if (!ctl.checked)
			{
				alert("Your request to join the interest list cannot be processed without your permission to contact you via e-mail, telephone, or other means.");
				return false;
			}
		}
		
		
		function ClearValues()
		{
			var realtorFirstName = document.getElementById("txtRealtorFirstName");
			var realtorLastName = document.getElementById("txtRealtorLastname");
			var realtorStreet = document.getElementById("txtRealtorStreet");
			var realtorCity = document.getElementById("txtRealtorCity");
			var realtorState = document.getElementById("ddlRealtorState");
			var realtorZip = document.getElementById("txtRealtorZip");
			var realtorEmail = document.getElementById("txtRealtorEmail");
			var realtorCellPhone = document.getElementById("txtRealtorCellPhone");
			var realtorWorkPhone = document.getElementById("txtRealtorWorkPhone");
	
			realtorFirstName.value = "" ;
			realtorLastName.value = "" ;
			realtorStreet.value="" ;		
			realtorCity.value="" ;
			realtorState.selectedIndex = 0 ;
			realtorZip.value="" ;
			realtorEmail.value="" ;
			realtorCellPhone.value="" ;
			realtorWorkPhone.value="" ;
		}
		
		function ResetFields()
		{
			document.Form1.reset() ;
			UncheckTree()
			ToggleCountry() ;
			ToggleRealtor() ;
			ToggleMovingOther() ;
			return false ;
		}
		
			function UncheckTree()
			{
			  var x = document.getElementById("tvMarketAreas").childnodes;
				
				x.nodes.clear();		
			}
		
		// Show / Hide Layer [ With Image ]
		
function miniMax(whichElement) {
	imageSource = document.images['minMax'+whichElement].src;
	imageSource_array = imageSource.split('/images/');
	imageSource = imageSource_array[1];
	imageSource_array2 = imageSource.split('.gif');
	whichAction = imageSource_array2[0];
	
	if (whichAction == 'sectionCollapse') {
		document.getElementById(whichElement).style.display = 'none';
		document.images['minMax'+whichElement].src = imageSource_array[0] + '/images/sectionExpand.gif';
		document.getElementById(whichElement).title = 'Expand Statistics Details';
	}
	if (whichAction == 'sectionExpand') {
		document.getElementById(whichElement).style.display = 'inline';
		document.images['minMax'+whichElement].src = imageSource_array[0] + '/images/sectionCollapse.gif';
		document.getElementById(whichElement).title = 'Collapse Statistics Details';
	}
}
