// set default pop-up window dimensions.
var popupWidth = 500;
var popupHeight = 400;

//
// 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){
	window.open(strLocation,strWindowName,strParameters);
}
// END POP UP A NEW WINDOW ==================================================

// BEGIN FORM FIELD VALIDATION ================================================
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
	}
	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 += '&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=570');
}
