// set default pop-up window dimensions.
var popupWidth = 500;
var popupHeight = 400;

//
//	showSection - Advanced search zone locations
//

function setCheckedItems(bChecked) {

	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	var bEdit = false;

	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			
			if(argname == "edit")	bEdit = true;
			
			if(argname.substring(0, 1) == "L" && argname != "living")
			{
				var elem = document.getElementById(String(argname));
				
				if(elem != null) {
					if(elem.tagName == "INPUT" && elem.type == "checkbox") {
						if(bChecked) {
							elem.checked = true;
						}
						else {
							var elems = document.frmSearch.elements;
							for(var j = 0; j < elems.length; j++) {
								var elemName = elems[j].name;
								if(elemName.substring(0, 1) == "L" && elemName != "living") {
									if(elems[j].tagName == "INPUT" && elems[j].type == "checkbox") {
										elems[j].checked = false;
									}
								}
							}
						}
					}
				}
			}	
		}
	}
	
	if(!bEdit) {
		var elems = document.getElementsByTagName("INPUT");
		for(var i = 0; i < elems.length; i++) {
			var elemName = elems[i].name;
			if(elems[i].type == "checkbox" && elemName.substring(0, 1) == "L") {
				elems[i].checked = false;
			}
		}
	}
}

function displaySection(aValue, bDoCheck)
{
	//
	// page variables intRegionGroupCount and arrRegionGroups
	// are intitialized dynamically in the <head> section of the page.
	//
	for (var i = 0; i < intRegionGroupCount; i++) {
		var txtID = (i+1);
		arrRegionGroups[i] = document.getElementById(txtID);
	}

	if(bDoCheck) {
		setCheckedItems(true);
	} else {
		setCheckedItems(false);
	}

	for (var i = 0; i < intRegionGroupCount; i++) {
		if (aValue == (i+1))
			arrRegionGroups[i].style.display = "block";
		else
			arrRegionGroups[i].style.display = "none";
	}
}

//
// gotoURL - allows a URL to be coded into an HTML block
// append vid to the URL.
//

function gotoURL(theURL, newWindow) {
	var newURL = theURL;
	var blnVID = false;
	//
	// make sure local variable vid is available
	//
	if (typeof(vid) == 'string') {
		blnVID = true;
	}

	if (theURL.match(/[?]/)) {
		//
		// there are already query string variables
		// append vid
		//
		if (blnVID) newURL = newURL + "&vid=" + vid;
	} else {
		//
		// there are no query string variables. assume this
		// is a link to external site.
		//
		//if (blnVID) newURL = newURL + "?vid=" + vid;
	}

	if (typeof(newWindow) != 'undefined') {
		if (newWindow == 'new') {
			window.open(newURL);
		} else {
			window.location.href = newURL;
		}
	} else {
		window.location.href = newURL;
	}
	return;
}


// BEGIN PRIMARY NAV ROLLOVER ==========================
// wrapper functions to swap both "background-color style" on cell and image
function PNavOver(imgID,newImageSRC,navID,cellID){
	if ( newImageSRC.length != 0 )
		over(imgID,newImageSRC);
	setHighlightOn(navID,cellID);
}
function PNavOut(imgID,newImageSRC,navID,cellID){
	if ( newImageSRC.length != 0 )
		out(imgID,newImageSRC);
	setHighlightOff(navID,cellID);
}
// END PRIMARY NAV ROLLOVER ==========================

// BEGIN IMAGE ROLLOVER CODE ======================
// swaps image source file onmouseover, onmouseout
function over(imgID,newImageSRC){
	document.images["pnav_" + imgID].src = newImageSRC;
}
function out(imgID,newImageSRC){
	document.images["pnav_" + imgID].src = newImageSRC;
}
function SwapImage(imgID,newImageSRC){
	document.getElementById(imgID).src = newImageSRC; 
}
// END IMAGE ROLLOVER CODE =======================

// BEGIN ROLLOVER CLASSNAME SWAP CODE USED BY NAV =================
// creates dom object from table cell to be altered.
function setObjectForBrowser(cellID){
	if(document.all && ! (document.getElementById)){
		objElement = document.all[cellID];
	}
	else if (document.getElementById){
		objElement = document.getElementById(cellID);
	}
	return objElement;
}
// changes css classname on specified cell
function setHighlightOn(navid, cellID){
	if(!document.layers){
		var elementToChange = setObjectForBrowser(cellID);
		elementToChange.className = navid + "_on";
	}
}
// changes css classname on specified cell
function setHighlightOff(navid, cellID){
	if(!document.layers){
		var elementToChange = setObjectForBrowser(cellID);
		elementToChange.className = navid + "_off";
	}
}

// BEGIN POP UP A NEW WINDOW ================================================
function doPopWin(strLocation, strWindowName, strParameters) {
	var newURL = strLocation;
	//
	// make sure local variable vid is available
	//
	if (typeof(vid) == 'string') {
		if (strLocation.indexOf("default.asp?") >= 0) {
			if (strLocation.indexOf("vid=") < 0) {
				newURL = newURL + "&vid=" + vid;
			}
		}
	}

	window.open(newURL, strWindowName, strParameters);
}
// END POP UP A NEW WINDOW ==================================================

// BEGIN FORM FIELD VALIDATION ================================================
function chkPostcardForm() {

	var senderEmail = document.frmPostcard.senderEmail;
	if ((senderEmail.value==null) || (senderEmail.value=="")) {
		alert("Your E-mail address is a required field.");
		senderEmail.focus();
		return false;
	}

	if (validEmail(senderEmail.value)==false) {
		senderEmail.focus();
		return false;
	}

	var senderName = document.frmPostcard.senderName;
	if (senderName != null) {
		if ((senderName.value==null) || (senderName.value=="")) {
			alert("Your name is a required field.");
			senderName.focus();
			return false;
		}
	}

	var sendeeEmail = document.frmPostcard.sendeeEmail;
	if ((sendeeEmail.value==null) || (sendeeEmail.value=="")) {
		alert("Send To E-mail address is a required field.");
		sendeeEmail.focus();
		return false;
	}

	if (validEmail(sendeeEmail.value)==false) {
		sendeeEmail.focus();
		return false;
	}

	var sendeeName = document.frmPostcard.sendeeName;
	if (sendeeName != null) {
		if ((sendeeName.value==null) || (sendeeName.value=="")) {
			alert("Send To name is a required field.");
			sendeeName.focus();
			return false;
		}
	}

	var subject = document.frmPostcard.subject;
	if ((subject != null) && (subject.value != null) && (subject.value.length != null) && (subject.value.length > 50)) {
		alert("Subject is 50 characters maximum.");
		subject.focus();
		return false;
	}

	var message = document.frmPostcard.message;
	if ((message != null) && (message.value != null) && (message.value.length != null) && (message.value.length > 300)) {
		alert("Message is 300 characters maximum.");
		message.focus();
		return false;
	}

}

function chkWarrantyForm() {
	var firstname = document.frmWarranty.firstname;
	if (firstname != null) {
		if ((firstname.value==null) || (firstname.value=="")) {
			alert("First Name is a required field.");
			firstname.focus();
			return false;
		}
	}

	var lastname = document.frmWarranty.lastname;
	if (lastname != null) {
		if ((lastname.value==null) || (lastname.value=="")) {
			alert("Last Name is a required field.");
			lastname.focus();
			return false;
		}
	}

	var address = document.frmWarranty.address;
	if (address != null) {
		if ((address.value==null) || (address.value=="")) {
			alert("Address is a required field.");
			address.focus();
			return false;
		}
	}

	var city = document.frmWarranty.city;
	if (city != null) {
		if ((city.value==null) || (city.value=="")) {
			alert("City is a required field.");
			city.focus();
			return false;
		}
	}

	var state = document.frmWarranty.state;
	if (state != null) {
		if ((state.value==null) || (state.value=="")) {
			alert("State is a required field.");
			state.focus();
			return false;
		}
	}

	var zip = document.frmWarranty.zip;
	if (zip != null) {
		if ((zip.value==null) || (zip.value=="")) {
			alert("Zip is a required field.");
			zip.focus();
			return false;
		}
	}

	var email = document.frmWarranty.email;
	if ((email.value==null) || (email.value=="")) {
		alert("E-mail Address is a required field.");
		email.focus();
		return false;
	}

	if (validEmail(email.value)==false) {
		email.focus();
		return false;
	}

	var phone = document.frmWarranty.phone;
	if (phone != null) {
		if ((phone.value==null) || (phone.value=="")) {
			alert("Phone is a required field.");
			phone.focus();
			return false;
		}
	}

	var bestnumber = document.frmWarranty.bestnumber;
	if (bestnumber != null) {
		if ((bestnumber.value==null) || (bestnumber.value=="")) {
			alert("Best # is a required field.");
			bestnumber.focus();
			return false;
		}
	}

	return true;
}

function chkForm() {

	var email = document.frmEmail.email;
	if ((email.value==null) || (email.value=="")) {
		alert("E-mail Address is a required field.");
		email.focus();
		return false;
	}

	if (validEmail(email.value)==false) {
		email.focus();
		return false;
	}

	var firstname = document.frmEmail.firstname;
	if (firstname != null) {
		if ((firstname.value==null) || (firstname.value=="")) {
			alert("First Name is a required field.");
			firstname.focus();
			return false;
		}
	}

	var lastname = document.frmEmail.lastname;
	if (lastname != null) {
		if ((lastname.value==null) || (lastname.value=="")) {
			alert("Last Name is a required field.");
			lastname.focus();
			return false;
		}
	}

	var subject = document.frmEmail.subject;
	if ((subject != null) && (subject.value != null) && (subject.value.length != null) && (subject.value.length > 100)) {
		alert("Subject is 100 characters maximum.");
		subject.focus();
		return false;
	}

	if (typeof(document.frmEmail.action == 'string')) {
		if (document.frmEmail.action.indexOf("vid=",1) == -1) {
			if (typeof(vid) == 'string') {
				document.frmEmail.action += '&vid=' + vid;
			}
		}
	}
	return true;
}

function validEmail(email) {
	invalidChars = " /:,;"
	for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			alert("Please enter a valid e-mail address.")
			return false
		}
	}
	atPos = email.indexOf("@",1) // there must be one "@" symbol
	if (atPos == -1) {
		alert("Please enter a valid e-mail address.")
		return false
	}

	if (email.indexOf("@",atPos+1) != -1) { // and only one "@" symbol
		alert("Please enter a valid e-mail address.")	
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) { // and at least one "." after the "@"
		alert("Please enter a valid e-mail address.")
		return false
	}
	if (periodPos+3 > email.length) { // must be at least 2 characters after the "."
		alert("Please enter a valid e-mail address.")
		return false
	}
	return true
}

function chkAdvancedSearch() {

	var theForm = document.frmSearch;
	var newURL = 'default.asp?pageID=list_view_search&siteID=' + siteID + '&vid=' + vid + '&type=1';
	//
	// Test min and max values
	//
	var minValue = theForm.min.options[theForm.min.selectedIndex].value;
	var maxValue = theForm.max.options[theForm.max.selectedIndex].value;

	if ((Math.floor(minValue) > Math.floor(maxValue)) && (Math.floor(maxValue) > 0)) {
		alert("Minimum price can not be greater than maximum price.");
		return false;
	}

	newURL += '&rg=' + theForm.rg.options[theForm.rg.selectedIndex].value + '&min=' + minValue + '&max=' + maxValue;

	for (var i = 0; i < theForm.length; i++) {
		var el = theForm.elements[i];
		if (el.type == 'checkbox') {
			if (el.checked) {
				newURL += '&' + el.name + '=1';
			} else {
				newURL += '&' + el.name + '=0';
			}
		}
	}
	window.location.href = newURL;
	//
	// always return false otherwise form submit will override window.location.href
	//
	return false;
}

function jsFavoritesLogin() {
	if (document.frmFavorites.email.value.length == 0) {
		alert("Please enter an email address.");
		document.frmFavorites.email.focus();
		return;
	}

	document.frmFavorites.hidType.value = 'login';
	document.frmFavorites.submit();
}

function jsSubmitFavorites() {
	if (document.frmFavorites.email.value.length == 0) {
		alert("Please enter an email address.");
		document.frmFavorites.email.focus();
		return;
	}

	if (validEmail(document.frmFavorites.email.value)==false) {
		document.frmFavorites.email.focus();
		return;
	}

	document.frmFavorites.hidType.value = 'submit';
	document.frmFavorites.submit();
}

function jsFavoritesPopup() {
	alert("If you would like more information on this property please proceed to Request Information or call us at 1-800-WCI-2290.");
}

function jsSubmitModelForm() {
	if (document.frmModel.email.value.length == 0) {
		alert("Email is a required field.");
		document.frmModel.email.focus();
		return;
	}

	if (validEmail(document.frmModel.email.value)==false) {
		document.frmModel.email.focus();
		return;
	}

	if (document.frmModel.fname.value.length == 0) {
		alert("First name is a required field.");
		document.frmModel.fname.focus();
		return;
	}

	if (document.frmModel.lname.value.length == 0) {
		alert("Last name is a required field.");
		document.frmModel.lname.focus();
		return;
	}
	document.frmModel.submit();
}

// END FORM FIELD VALIDATION ================================================

function jsModelAvailability() {
	//
	// modelID and vid were in query string and were assigned to local page variables
	//
	window.open('ModelAvailability.asp?modelID=' + modelID + '&vid=' + vid, '_blank', 'resizeable=no,scrollbars=no,toolbar=no,width=476,height=670');
}
