////////////////// VALIDATOR FUNCTIONS //////////////////

/****** Check required fields in forms script *******/
function emailFriendCheck(formobj){
if (formobj.name.value == "" || formobj.name.value == null){ alert("Please enter your name."); formobj.name.focus(); return false;}
if (formobj.email.value == "" || formobj.email.value == null){ alert("Please enter your email address."); formobj.email.focus(); return false;}
if (formobj.recipient.value == "" || formobj.recipient.value == null){ alert("Please enter your friend's email address."); formobj.recipient.focus(); return false;}
}


function pageCheckhva(formobj){
	if (formobj.firstName.value == "" || formobj.firstName.value == null){ alert("Please enter your first name."); formobj.firstName.focus(); return false;}
	if (formobj.lastName.value == "" || formobj.lastName.value == null){ alert("Please enter your last name."); formobj.lastName.focus(); return false;}
	if (formobj.email.value == "" || formobj.email.value == null){ alert("Please enter your email address."); formobj.email.focus(); return false;}
	if (formobj.street.value == "" || formobj.street.value == null){ alert("Please enter your street."); formobj.street.focus(); return false;}
	if (formobj.zip.value == "" || formobj.zip.value == null){ alert("Please enter your zip code."); formobj.zip.focus(); return false;}
	if (formobj.propertyType.value == "" || formobj.propertyType.value == null){ alert("Please enter your property type."); formobj.propertyType.focus(); return false;}
	if (formobj.beds.value == "" || formobj.beds.value == null){ alert("Please enter the number of bedrooms."); formobj.beds.focus(); return false;}
	if (formobj.baths.value == "" || formobj.baths.value == null){ alert("Please enter number of bathrooms."); formobj.baths.focus(); return false;}
	if (formobj.hasRealtor.value == "" || formobj.hasRealtor.value == null){ alert("Please tell us if you are working with a realtor."); formobj.hasRealtor.focus(); return false;}
	if ( (formobj.dayPhone.value == "") && (formobj.nightPhone.value == "") ){
	alert("Please enter a valid phone number.");
	formobj.dayPhone.focus();
	return false;
	}
}

function pageCheckrbk(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("firstName", "lastName", "email", "street", "state", "zip", "propertyType", "neighborhoods", "minPrice", "maxPrice", "beds", "baths","moveDate", "dayPhone", "nightPhone");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("First Name", "Last Name", "Email Address", "Address", "State", "Zip", "Home Type", "Nieghborhood", "Min Price", "Max Price", "Bedrooms", "Bathrooms", "MoveDate", "Home Phone", "Night Phone");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";

	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].value == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}


function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("FirstName", "LastName", "email", "propertyType", "minPrice", "maxPrice", "neighborhoods", "beds", "baths", "halfBaths", "parkingType", "hasRealtor", "moveDate", "nightPhone", "street", "city", "state", "zip");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("First Name", "Last Name", "Email Address", "Property Type", "Min Price", "Max Price", "Neighborhoods", "Bedrooms", "Bathrooms", "Half Bathrooms", "Parking Type", "Are you working with a Realtor?", "Estimated Move Date", "Night Phone", "Street Address", "City", "State", "ZIP Code");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].value == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

function relocateformCheck(formobj) {
if (formobj.firstName.value == "" || formobj.firstName.value == null){
	alert("Please enter your  first name.");
	formobj.firstName.focus();
	return false;
}

if (formobj.lastName.value == "" || formobj.lastName.value == null){
	alert("Please enter your last name.");
	formobj.lastName.focus();
	return false;
}

if (formobj.email.value == "" || formobj.email.value == null){
	alert("Please enter your email.");
	formobj.email.focus();
	return false;
}

if ( (formobj.dayPhone.value == "") && (formobj.nightPhone.value == "") ){
	alert("Please enter a valid phone number.");
	formobj.dayPhone.focus();
	return false;
} 

if (formobj.hasRealtor.value == "" || formobj.hasRealtor.value == null){
	alert("Please enter if you are working with a realtor.");
	formobj.hasRealtor.focus();
	return false;
}
}

function contactformCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("name","phone","city","state","email","howheard","comments");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Name","Phone Number","City","State","Email Address","How did you hear about us?","Questions/Comments?");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].value == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

/******* Validate Member Login box on left navigation *******/

    var bCancel = false; 

    function validateLogonForm(form) {                                                                   
        if (bCancel) 
	return true; 
	else 
	return validateRequired(form) && validateMinLength(form); 
	}
    function required () { 
	this.aa = new Array("username", "Please enter a valid username.", new Function ("varName", " return this[varName];"));
	this.ab = new Array("password", "Please enter a valid password.", new Function ("varName", "this.minlength='3';  return this[varName];"));
	}
    function minlength () { 
	this.aa = new Array("password", "Passwords cannot be less than 3 characters.", new Function ("varName", "this.minlength='3';  return this[varName];"));
	}
	
/******** Email Check *******/

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					
	}

function ValidateForm(formobj){
	var emailID=formobj.email
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 
 //testimonial pop up
function testimonialPopup(whereTo, winWidth, winHeight){
  var basepage = "http://www.dreamtown.com/testimonials/";
  var currentpage = whereTo;
  var goto = basepage + currentpage;
  remote = window.open("goto","testimonial","width=650,height=350,menubar=0,toolbar=no,scrollbars=yes,statusbars=no");
  remote.location.href = goto; 
  remote.focus();
}