window.onload = function() {

	preloadImages();

}



function clickEktronFormSubmit(e){

	return clickButton(e, 'EktronFormSubmit');

}



// DEFAULT BUTTON ON 'ENTER' KEY

function clickButton(e, buttonid){

	var bt = document.getElementById(buttonid);

	// check that the event is a keypress and the button is in fact an input

	if ( (e.type == 'keypress') && (bt.tagName == 'INPUT') ) {

		// create generic reference to keyCode or charCode, depending on used object

		var kCode;

		if (e.keyCode) {

			kCode = e.keyCode;

		} else if (e.charCode) {

			kCode = e.charCode;

		} else {

			// niether object is supported, so do nothing

			kCode = "";

			return false;

		}

		

		// mac uses 13 for 'return' and 3 for 'enter'

		if ( (kCode == 13) || (kCode == 3) ) {

			bt.click();

			return false;

		} else {

			// if not the desired key code, the user is typing. must be set to true so typing works

			return true;

		}

	}

}







// FIND VALIDATION ======================================================

// used by the find box througout the site

function checkState(val) {
	if (val > 0) {
		return true;
	} else {
		alert('Please select a state.');
		return false;
	}	
}

function checkZip(val) {

	val = val.toString();

	var regex = /\d{5}/i.test(val);

	var pass = false;

	if ( (val.length == 5) && ( regex == true) ) {

		pass = true;

	} else {

		alert('Please enter a 5 digit zip code. For example, 98006.');

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkAdvanced(last_name, area_code) {

	var pass = false;

	var msg = "";



	area_code = area_code.toString();

	if (area_code == 'Enter Area Code')

		return true;

		

	var r_area_code = /\d{3}/i.test(area_code);



	if ( (area_code.length == 3) && ( r_area_code == true) ) {

		pass = true;

	} else {

		msg += "Please enter a 3 digit area code. For example, 425.";

		pass = false;

	}



	if (pass == true) {

		return true;

	} else {

		alert(msg);

		return false

	}

}

function getElement(id) {

	return document.getElementById(id);

}

// END FIND VALIDATION ==================================================



// Schedule Inspection Agent Form

function checkScheduleInspectionAgentForm(

OrderPlacedByFirstName,

OrderPlacedByLastName,

OrderPlacedByPhone,

OrderPlacedByEmail,

OrderPlacedByIAmThe,

OrderPlacedByAccessProvidedBy,

OrderPlacedByClientAttend, 

OrderPlacedByWINCoordinate, 

InspectionAddressLine1, 

InspectionAddressCity, 

InspectionAddressState, 

InspectionAddressZip, 

InspectionAddressSquareFootage, 

InspectionAddressPoolSpa, 

InspectionAddressPropertyOccupied, 

InspectionAddressUtilitiesOn, 

ClientName, 

ClientState, 

ClientZip, 

ClientPhone, 

ClientEmail, 

BuyerAgentOfficePhone, 

BuyerAgentCellPhone, 

BuyerAgentFax, 

BuyerAgentEmail, 

ListingAgentOfficePhone, 

ListingAgentCellPhone, 

ListingAgentFax, 

ListingAgentEmail, 

TitleCompanyOfficePhone, 

TitleCompanyFax

) {

    if (!checkTextRequired(OrderPlacedByFirstName, 'Please enter your first name.'))

		return false;

		

	if (!checkTextRequired(OrderPlacedByLastName, 'Please enter your last name.'))

		return false;

		

	if (!checkPhoneRequired(OrderPlacedByPhone))

		return false;

		

	if (!checkEmailRequired(OrderPlacedByEmail))

		return false;

		

	if (!checkSelectionMade(OrderPlacedByIAmThe))

		return false;

		

	if (!checkTextRequired(OrderPlacedByAccessProvidedBy, 'Please specify who will provide access to the property.'))

		return false;

		

	if (!checkSelectionMade(OrderPlacedByClientAttend))

		return false;

		

	if (!checkSelectionMade(OrderPlacedByWINCoordinate))

		return false;

		

	if (!checkTextRequired(InspectionAddressLine1, 'Please enter address line 1.'))

		return false;

		

	if (!checkTextRequired(InspectionAddressCity, 'Please enter city.'))

		return false;

		

	if (!checkUSStateCodeRequired(InspectionAddressState, 'Please enter 2 character state code. For example, CA.'))

		return false;

		

	if (!check5DigitZipOrBlank(InspectionAddressZip))

		return false;

		

	if (!checkTextRequired(InspectionAddressSquareFootage, 'Please enter the square footage of the property.'))

		return false;

		

	if (!checkSelectionMade(InspectionAddressPoolSpa))

		return false;

		

	if (!checkSelectionMade(InspectionAddressPropertyOccupied))

		return false;

		

	if (!checkSelectionMade(InspectionAddressUtilitiesOn))

		return false;

		

	if (!checkTextRequired(ClientName, 'Please enter the client name.'))

		return false;

		

	if (!checkUSStateCodeOrBlank(ClientState, 'Please enter 2 character state code. For example, CA.'))

		return false;

		

	if (!check5DigitZipOrBlank(ClientZip))

		return false;

		

	if (!checkPhoneRequired(ClientPhone))

		return false;

		

	if (!checkEmailOrBlank(ClientEmail))

		return false;

		

	if (!checkPhoneOrBlank(BuyerAgentOfficePhone))

		return false;

		

	if (!checkPhoneOrBlank(BuyerAgentCellPhone))

		return false;

		

	if (!checkPhoneOrBlank(BuyerAgentFax))

		return false;

		

	if (!checkEmailOrBlank(BuyerAgentEmail))

		return false;

		

	if (!checkPhoneOrBlank(ListingAgentOfficePhone))

		return false;

		

	if (!checkPhoneOrBlank(ListingAgentCellPhone))

		return false;

		

	if (!checkPhoneOrBlank(ListingAgentFax))

		return false;

		

	if (!checkEmailOrBlank(ListingAgentEmail))

		return false;

		

	if (!checkPhoneOrBlank(TitleCompanyOfficePhone))

		return false;

		

	if (!checkPhoneOrBlank(TitleCompanyFax))

		return false;

		

	// Prep Inspection Services Needed hidden field.

	// Also, confirm at least one selected.

	document.Form1.SelectedServices.value = "";

	var bOneSelected = false;

	var aInputs = document.getElementsByTagName("input");

    for (var s in aInputs) {

		if (aInputs[s].type == "checkbox" && aInputs[s].checked == true && aInputs[s].id.substring(0, 8) == "Services") {

			bOneSelected = true;

			var label = aInputs[s].nextSibling;

  			document.Form1.SelectedServices.value += label.firstChild.data + ",";

		}

    }

    

    if (bOneSelected == false) {

		alert('Please select at least one Inspection Service Needed.');

		return false;

    }

    

    document.getElementById('EktronFormSubmitHidden').value = 'Y';

    

	return true;

}

function checkUSStateCodeRequired(id, msg) {

	var val = id.value.toString().toUpperCase();

	

	var pass = false;

	

	var arrState =  new Array('AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY');

	

	for (var s in arrState) {

		if (val == arrState[s]) {

			pass = true;

			id.value = val; // Just in case it was entered as lower case.

			break;

		}

	}

	

	if (pass == true) {

		return true;

	} else {

		alert(msg);

		id.focus();

		return false

	}

}

function checkUSStateCodeOrBlank(id, msg) {

	var val = id.value.toString().toUpperCase();

	if ( val.length == 0 ) {

		return true;

	}

	

	var pass = false;

	

	var arrState =  new Array('AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY');

	

	for (var s in arrState) {

		if (val == arrState[s]) {

			pass = true;

			id.value = val; // Just in case it was entered as lower case.

			break;

		}

	}

	

	if (pass == true) {

		return true;

	} else {

		alert(msg);

		id.focus();

		return false

	}

}

function checkTextRequired(id, msg) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert(msg);

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkPhoneRequired(id) {

	var val = id.value.toString();

	var regex = /^[1-9]\d{9}$/i.test(val);

	var pass = false;

	if ( (val.length == 10) && ( regex == true) ) {

		pass = true;

	} else {

		alert('Please enter a 10 digit phone number. For example, 4255551212.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkSelectionMade(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val != "(Select)" ) {

		pass = true;

	} else {

		alert('Please make a selection.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function check5DigitZipOrBlank(id) {

	var val = id.value.toString();

	var regex = /^\d{5}$/i.test(val);

	var pass = false;

	if ( (val.length == 5) && ( regex == true) ) {

		pass = true;

	} else if (val.length == 0) {

		pass = true;

	} else {

		alert('Please enter a 5 digit zip code. For example, 98006.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}











function checkContactUsForm(first, last, email, message) {

    var ret = false;

	if (checkFirstNameRequired(first)) {

		if (checkLastNameRequired(last)) {

			if (checkEmailRequired(email)) {

				if (checkMessageRequired(message)) {

					ret = true;

				}

			}

		}

	}

	return ret;

}

function checkFirstNameRequired(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter your first name.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkLastNameRequired(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter your last name.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkEmailRequired(id) {

	var val = id.value.toString();

	var regex = /^.+@.+\..+$/i.test(val);

	var pass = false;

	if ( regex == true ) {

		pass = true;

	} else {

		alert('Please enter a valid e-mail address. For example, me@something.com');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkEmailOrBlank(id) {

	var val = id.value.toString();

	var regex = /^.+@.+\..+$/i.test(val);

	var pass = false;

	if ( regex == true ) {

		pass = true;

	} else if (val.length == 0) {

		pass = true;

	} else {

		alert('Please enter a valid e-mail address. For example, me@something.com');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkPhoneOrBlank(id) {

	var val = id.value.toString();

	var regex = /^[1-9]\d{9}$/i.test(val);

	var pass = false;

	if ( (val.length == 10) && ( regex == true) ) {

		pass = true;

	} else if (val.length == 0) {

		pass = true;

	} else {

		alert('Please enter a 10 digit phone number. For example, 4255551212.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkMessageRequired(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter your message.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}



// PDF CUSTOMIZE FORM VALIDATION

function checkPDFCustomizeForm(first, last, agency, address1, city, state, zip, officephone, email) {

    var ret = false;

	if (checkFirstNameRequired(first)) {

		if (checkLastNameRequired(last)) {

			if (checkAgencyNonBlank(agency)) {

				if (checkAddress1NonBlank(address1)) {

					if (checkCityNonBlank(city)) {

						if (checkStateNonBlank(state)) {

							if (checkZipNonBlank(zip)) {

								if (checkOfficePhoneNonBlank(officephone)) {

									if (checkEmailRequired(email)) {

										ret = true;

									}

								}

							}

						}

					}

				}

			}

		}

	}

	return ret;

}

function checkAgencyNonBlank(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter your agency name.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkAddress1NonBlank(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter address line 1.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkCityNonBlank(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter your city.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkStateNonBlank(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter your state.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkZipNonBlank(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter your zip.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

function checkOfficePhoneNonBlank(id) {

	var val = id.value.toString();

	var pass = false;

	if ( val.length > 0 ) {

		pass = true;

	} else {

		alert('Please enter your office phone.');

		id.focus();

		pass = false;

	}

	

	if (pass == true) {

		return true;

	} else {

		return false

	}

}

// END PDF CUSTOMIZE FORM VALIDATION



// Agent Schedule Inspection Form - special validation

function checkInspectionServicesNeededRequired() {

	var pass = false;

	var obj = document.getElementById('InspectionInformationInspectionServicesNeeded');

	if (obj == null) {

		pass = true;

	}

	else {

		if (obj.selectedIndex > -1) {

			pass = true;

		} else {

			alert('Please select one or more Inspection Services Needed.');

			obj.focus();

			pass = false;

		}

	}

	return pass;

}





/* IMAGE PRELOADER

------------------------------------------------------------------------- */



// imagePath: generic path from RootVirtual to image folder

// NOTE: dependent on settng the js RootVirtual variable in the head of each document

// just before the link to the js.js file. The js version of this variable just takes

// the C# version as it's value.

/* <script language="javascript" type="text/javascript">

//	var RootVirtual = "<%=RootVirtual %>";

// </script> */

var imagePath = RootVirtual + "/_img";



// theImages: Array object of images whos path starts WITHIN the generic image folder

var imgURLs = new Array(

	"/home/guide/hgAgent_mo.gif",

	"/home/guide/hgAgent_on.gif",

	"/home/guide/hgSeller_mo.gif",

	"/home/guide/hgSeller_on.gif",

	"/home/guide/hgOwner_mo.gif",

	"/home/guide/hgOwner_on.gif",

	"/home/guide/hgBuyer_mo.gif",

	"/home/guide/hgBuyer_on.gif",

	"/find/findTabAdvanced.gif",

	"/find/findTabAdvanced_mo.gif",

	"/find/findTabAdvanced_on.gif",

	"/find/findTabState.gif",

	"/find/findTabState_mo.gif",

	"/find/findTabState_on.gif",

	"/find/findTabZip.gif",

	"/find/findTabZip_mo.gif",

	"/find/findTabZip_on.gif",

	"/nav/about.gif",

	"/nav/about_mo.gif",

	"/nav/about_in.gif",

	"/nav/articles.gif",

	"/nav/articles_mo.gif",

	"/nav/articles_in.gif",

	"/nav/find.gif",

	"/nav/find_mo.gif",

	"/nav/find_in.gif",

	"/nav/home.gif",

	"/nav/home_mo.gif",

	"/nav/home_in.gif",

	"/nav/services.gif",

	"/nav/services_mo.gif",

	"/nav/services_in.gif",

	"/guide/agent.gif",

	"/guide/agent_mo.gif",

	"/guide/buyer.gif",

	"/guide/buyer_mo.gif",

	"/guide/owner.gif",

	"/guide/owner_mo.gif",

	"/guide/seller.gif",

	"/guide/seller_mo.gif",

	"/util/contact.gif",

	"/util/contact_mo.gif",

	"/util/glossary.gif",

	"/util/glossary_mo.gif",

	"/util/login.gif",

	"/util/login_mo.gif",

	"/util/map.gif",

	"/util/map_mo.gif"

);



// preloadImages: called within the window.onload = init; function, starts the image loading process;

function preloadImages() {

	loadImages(imgURLs, imagePath);

}



// loadImages: loops through the image source array, instantiating and loading image files

// I don't know what the indexOf() call is doing there. It seems pointless to me.

function loadImages(imgURLs, imagePath) {

	var urls = imgURLs;

	var path = imagePath;

	if (document.images) {

		document.imageArray = new Array();

		var i, j=document.imageArray.length;

		for (i=0; i<urls.length; i++) {

			if (urls[i].indexOf("#")!=0) { 

				document.imageArray[j] = new Image; 

				document.imageArray[j++].src = path + urls[i];

			}

		}

		return true;

	} else {

		return false;

	}

}

// END Image PreLoader ==================================================





/* this script had some errors that are I've, with comments appended -dd */

/* added "var" declaration */

var HOVER_X = "_mo";



function swap(i) {

	/* changed reference from deprecated 'arguments[]' array to 'arguments' object */

	var args = arguments;

	if (document.images && i.childNodes) {

		/* changed reference to 'childNodes[0]' to 'firstChild' */

		var a = args[1], s = i.firstChild;

		if (a) s.src = a;

		else {

			var x = s.src.lastIndexOf('.'), xl = HOVER_X.length;

			if (s.src.substring(x-xl, x) == HOVER_X)

				s.src = s.src.substring(0,x-xl)+s.src.substring(x,s.src.length);

			else s.src = s.src.substring(0,x)+HOVER_X+s.src.substring(x,s.src.length);

		}

	}

}



function printPage(){if (window.print){window.print();}}









// TABCONTROL OBJECT ====================================================

// FUNCTIONS TO BE USED BY OBJECT

function registerTabs(tabUL) {

	// only if requested element exists on page

	if (document.getElementById(tabUL)) {

		// initialize array for TABS (li elements)

		this.tabs = new Array();

		// loop through children of supplied UL

		for (var j=0;j<document.getElementById(tabUL).childNodes.length;j++) {

			// determine current LI in loop

			var k = document.getElementById(tabUL).childNodes[j];

			// make sure we're looking at an LI (will have content)

			if (k.id) {

				var l = this.tabs.length;

				// put current LI into next open position in array

				this.tabs[l] = k;

				// set it to OFF

				k.className = "off";

			}

		}

		// designate currently selected tab
		if (tabUL == "find_tabs")
			this.tabs[1].className = "on";
		else this.tabs[0].className = "on";

	}

}

function registerPanels(panelWrapper) {

	// only if requested element exists on page

	if (document.getElementById(panelWrapper)) {

		// initialize array for panels (div elements)

		this.panels = new Array();

		// loop though all children of wrapper, add all with an id to Panels Array

		for (var j=0;j<document.getElementById(panelWrapper).childNodes.length;j++) {

			var k = document.getElementById(panelWrapper).childNodes[j];

			// only add children with an ID

			if (k.id) {

				var l = this.panels.length;

				this.panels[l] = document.getElementById(k.id);

				// turn recognized children OFF

				this.panels[l].className = "off";

			}

		}

		// designate select panel
		if (panelWrapper == "find_panels")
			this.panels[1].className = "on";
		else this.panels[0].className = "on";

	}

}

function selectTab(selected) {

	// loop through list of tabs

	for (var i=0;i<this.tabs.length;i++) {

		// if this is the selected tab, display correct panel and update tab class

		if (this.tabs[i].id == selected.id) {

			this.tabs[i].className = "on";

			this.panels[i].className = "on";

		} else {

		// else, do the opposite;

			this.tabs[i].className = "off";

			this.panels[i].className = "off";

		}

	}

}

// !END FUNCTIONS TO BE USED BY OBJECT



// CONSTRUCTOR

function tabControl() {

	// register functions as belonging to this object

	this.registerTabs = registerTabs;

	this.registerPanels = registerPanels;

	this.selectTab = selectTab;

}

// END TABCONTROL OBJECT ====================================================





// New and Better popup



// 	usage: popuplink(['js-only url',] this[, w[, h[, scroll[, extras]]]])

// 	basic usage: <a href="popup.html" target="_blank" onclick="return(popuplink(this));">new pop</a>

// 	advanced usage: <a href="popup_nojs.html" target="_blank" onclick="return(popuplink('popup_yesjs.html', this, 200, 100, false));">new pop</a>

// 	site-wide defaults:

var popup_w = 600;

var popup_h = 450;

var popup_scroll = true;

var popup_extras = 'location=0,status=0,menubar=0,resizable=0';

//	POPUP_FANCY = 'location=yes,toolbar=yes,menubar=yes,directories=yes,status=yes,resizable=yes';

// 	NS4 placement:   	screenX and screenY

//	IE placement:		left and top

function popuplink() {

	var undef, i=0, args = arguments;

	var url = (typeof(args[i])=='string') ? args[i++] : args[i].getAttribute('href');

	var target = args[i++].getAttribute('target') || '_blank';

	var w = args[i++];

	var h = args[i++];

	var s = (args[i]===undef) ? popup_scroll : args[i++];



	//	centering: determine screen center

	var scrw=(screen.width/2)-((w || popup_w)/2);

	var scrh=(screen.height/2)-((h || popup_w)/2);

	 	if (scrh > 100) { scrh = scrh - 40; } // bump vertical pos. just above center, but only if it fits



	var features = 'width=' + (w || popup_w)

		 + ',height=' + (h || popup_h)

		 + ',scrollbars=' + (s ? 'yes,' : 'no,')

		 + (args[i] || popup_extras)

		 + ',left='+scrw+',top='+scrh+',screenX='+scrw+',screenY='+scrh; // positioning for IE and NS

	var win = window.open(url, target, features);

	win.focus();

	return false;

}

// END POP UP