function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function WM_toggle(id) {
  if (document.all){
    if(document.all[id].style.display == 'none'){
      document.all[id].style.display = '';
    } else {
      document.all[id].style.display = 'none';
    }
  } else if (document.getElementById){
    if(document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == ''){
      document.getElementById(id).style.display = 'block';
    } else {
      document.getElementById(id).style.display = 'none';
    }
  } 
}

  
/* Opens the location in the window that opened this one. If no such window exists, a new window is opened. */
function popInOpener(location) {
  if (window.opener && !window.opener.closed) {
    window.opener.location = location;
    window.opener.focus();
  } else {
    popOrdinary(location, 'nemoves');
  }
}

function pop(loc, name, w, h) {
    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-h)/400 : 0;
    setToolbar = 'yes';
    setLocation = 'yes';
    setDirectories = 'no';
    setStatus = 'yes';
    setMenubar = 'yes';
    setScrollbars = 'yes';
    setResizable = 'yes';
    setCopyhistory = 'no';
    settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',toolbar='+setToolbar+',location='+setLocation+',directories='+setDirectories+',status='+setStatus+',menubar='+setMenubar+',scrollbars='+setScrollbars+',resizable='+setResizable+',copyhistory='+setCopyhistory+''
    var opp = window.open(loc, name, settings);
    opp.focus();
    return opp;
}

function popWin(windowName, loc, w, h, setToolbar, setLocation, setDirectories, setStatus, setMenubar, setScrollbars, setResizable, setCopyhistory) {
    LeftPosition = (screen.availWidth) ? (screen.availWidth-w)/2 : 0;
    TopPosition = (screen.availHeight) ? (screen.availHeight-h)/2 : 0;
    settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',toolbar='+setToolbar+',location='+setLocation+',directories='+setDirectories+',status='+setStatus+',menubar='+setMenubar+',scrollbars='+setScrollbars+',resizable='+setResizable+',copyhistory='+setCopyhistory;
    var opp = window.open(loc, windowName, settings);
    opp.focus();
}

function popOrdinary(location, name) {
  pop(location, name, 820, 600);
}

function popSimple(location, width, height) {
  windowName = 'popup';
  pop(location, windowName, width, height);
}

function popNoDecorations(location, width, height) {
  popWin('popup', location, width, height, 'no', 'no', 'no', 'no', 'no', 'no', 'yes', 'no');
}

function popRequestAppointment(property) { 
	popWin("popRequestAppointment", '/listings/Request_Appointment.asp?id=' + property, 510, 600, "no", "no", "no", "no", "no", "yes", "yes", "no"); 
} 

function popRequestAppointmentWithAgent(property,agent) { 
	popWin("popRequestAppointment", '/listings/Request_Appointment.asp?id=' + property +'&agent=' + agent, 510, 600, "no", "no", "no", "no", "no", "yes", "yes", "no"); 
} 

function popRequestMoreInfo(property) { 
	popWin("popRequestMoreInfo", '/listings/Request_More_Information.asp?id=' + property, 510, 600, "no", "no", "no", "yes", "no", "yes", "yes", "no"); 
}

function popRequestMoreInfoWithAgent(property,agent) { 
	popWin("popRequestMoreInfo", '/listings/Request_More_Information.asp?id=' + property +'&agent=' + agent, 510, 600, "no", "no", "no", "yes", "no", "yes", "yes", "no"); 
}

function openCalculatorWithPurchasePrice(price) { 
	popWin("calc", "/listings/Calculator.asp?price=" + price, 510, 340,  "no", "no", "no", "yes", "no", "yes", "yes", "no"); 
}

function openCalculator() { 
	popWin("calc", "/listings/Calculator.asp", 510, 340,  "no", "no", "no", "yes", "no", "yes", "yes", "no"); 
}

function emailListingToFriendPop(property) { 
	popWin("contactUsShortForm", "/listings/EmailToFriend.asp?id=" + property, 510, 600, "no", "no", "no", "yes", "no", "yes", "yes", "no"); 
}

function popUpPrintWindow(property) { 
	popWin("printerfriendly", "/listings/PrintListing.asp?id=" + property, 740, 560, "no", "no", "no", "yes", "no", "yes", "yes", "no"); 
}

function popUpPrintWindowWithAgent(property,agent) { 
	popWin("printerfriendly", "/listings/PrintListing.asp?id=" + property +'&aid=' + agent, 740, 560, "no", "no", "no", "yes", "no", "yes", "yes", "no"); 
}

function setStatus(msg) { 
	window.status = msg; 
}

// only accept numbers in selected field
function checkIt(e){
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	if (charCode > 31 && (charCode < 48 || charCode > 57)){
		alert("Please make sure entries are numbers only.");
		return false;
	}
	return true;
}

function showSection(oSection){
	document.getElementById(oSection).style.display = (document.getElementById(oSection).style.display == "none") ? "" : "none";
}

// --------------------------------------------------------------------------------------------------------
// Search Options functions
// --------------------------------------------------------------------------------------------------------
function checkForm(frm) {
	var retVal = true
	var minPriceValue = frm.minprice.options[frm.minprice.selectedIndex].value;
	var maxPriceValue = frm.maxprice.options[frm.maxprice.selectedIndex].value;

	if (frm.mlsid.value != '' && frm.mlsid.value != 'Enter MLS ID') {
		// -- MLS Listing ID field occupied - other fields and submit MLS Listing ID search --
		// -- clear any default values --
		if (frm.street.value == ' - Enter a Street Name - ') frm.street.value='';
		if (frm.subdivision.value == ' - Enter a Subdivision - ') frm.subdivision.value='';
		if (frm.city.value == ' - Enter a City - ') frm.city.value='';
		if (frm.zip.value == ' - Enter a Zip/Postal Code - ') frm.zip.value='';

		frm.frm.value = 'bymlsid';
		frm.action = 'PropListing.asp';
		retVal =  true;	
	}
	else {
		// -- Price range check and alert --
		if (minPriceValue != '' && maxPriceValue != '') {
	   	var minPrice = parseInt(minPriceValue);
			var maxPrice = parseInt(maxPriceValue);
			
			if(minPrice > maxPrice  &&  maxPrice > 0) {
				alert('You have selected a minimum price ($' + minPrice + ') greater than your maximum price ($' + maxPrice + ').\n\nMaximum price should be greater than minimum price in order to find listings.\nSee "Price Range" on this page.\n\nPlease correct this before continuing.');
				frm.minprice.focus();
				retVal = false;
			}
			else {
				if(minPrice == maxPrice  &&  maxPrice > 0) {
					retVal = confirm('You have selected a minimum price that is equal to your maximum price ($' + maxPrice + ').\nThis will cause your search to find ONLY listings with a current list price of EXACTLY $' + maxPrice + '.\n\nIs this okay?');
				}
			}
		}
		// -- clear any default values --
		if (frm.street.value == ' - Enter a Street Name - ') frm.street.value='';
		if (frm.subdivision.value == ' - Enter a Subdivision - ') frm.subdivision.value='';
		if (frm.city.value == ' - Enter a City - ') frm.city.value='';
		if (frm.zip.value == ' - Enter a Zip/Postal Code - ') frm.zip.value='';
		if (frm.elem_school.value == ' - Select an Elementary School - ') frm.elem_school.value='';
		if (frm.middle_school.value == ' - Select a Middle School - ') frm.middle_school.value='';
		if (frm.high_school.value == ' - Select a High School - ') frm.high_school.value='';
		if (frm.mlsid.value == 'Enter MLS ID') frm.mlsid.value ='';
	}
	return retVal;
}

function ChangeText(blnChecked, divName){
	//alert(divName);
	if(blnChecked){
		document.getElementById(divName).className = 'cbHighlight';
	}
	else{
		document.getElementById(divName).className = 'lightText';
	}
}
// --------------------------------------------------------------------------------------------------------
		

// --------------------------------------------------------------------------------------------------------
// Google map code
// --------------------------------------------------------------------------------------------------------
var infowindows = [];
var marker = [];
var point = [];

var icon_other_listing = new GIcon();
icon_other_listing.image = "/listings_local/images/white-home.png";
icon_other_listing.iconSize = new GSize(25, 25);
icon_other_listing.iconAnchor = new GPoint(6, 20);
icon_other_listing.infoWindowAnchor = new GPoint(15, 1);

var icon_selected_listing = new GIcon();
icon_selected_listing.image = "/listings_local/images/green-home.png";
icon_selected_listing.iconSize = new GSize(25, 25);
icon_selected_listing.iconAnchor = new GPoint(6, 20);
icon_selected_listing.infoWindowAnchor = new GPoint(15, 1);

var icon_c21_listing = new GIcon();
icon_c21_listing.image = "/listings_local/images/yellow-home.png";
icon_c21_listing.iconSize = new GSize(25, 25);
icon_c21_listing.iconAnchor = new GPoint(6, 20);
icon_c21_listing.infoWindowAnchor = new GPoint(15, 1);

function showMarker(index) {
	marker[index].openInfoWindowHtml(infowindows[index]);
}

function update_map(){
	//if (document.nearbylistings.radius.options[document.nearbylistings.radius.selectedIndex].value != "") {
		location = document.nearbylistings.radius.options[document.nearbylistings.radius.selectedIndex].value
	//}
}
// --------------------------------------------------------------------------------------------------------


function validate_form(FORM){
	// validation variables
	var bgBad = "#FFE78C";
	var bgGood = "#FFFFFF";
	var valid = true;
	var missingFields = "The following fields are required:\n\n";            
	var numElements = FORM.elements.length;             

	// check all fields to ensure that they are not empty       
	var i;
	for(i=0;i<numElements;i++){
		// alert(FORM.elements[i].type);
		if (FORM.elements[i].type != "hidden") {
         FORM.elements[i].style.backgroundColor = bgGood;
			if(FORM.elements[i].getAttribute("required") == "true"){
				if(!FORM.elements[i].value){
					valid = false;
					FORM.elements[i].style.backgroundColor = bgBad;
					missingFields += FORM.elements[i].getAttribute("errormsg") + " \n";
				}
				else{
					FORM.elements[i].style.backgroundColor = bgGood;
				}
			}
		}
	}
	
	// return proper validation status
	if(!valid) {
		alert(missingFields);
		return valid;
	}        
	
	if(FORM.name == "frmRegister"){
		var email = FORM.email;
		var password = FORM.password;
		var passwordconf = FORM.passwordconfirmation;
		//var homephone = FORM.homephone;
		//var workphone = FORM.workphone;
		//var zip = FORM.zipcode;

		// check email for @ symbol
		if((email.value.indexOf("@") == -1) && (email.value != "")){
			valid = false;
			email.style.backgroundColor = bgBad; 
			alert("Please enter a valid e-mail address.");
			email.focus();
			email.blur();
			email.select();
			return valid;
		}			

		// check email for . symbol
		if((email.value.indexOf(".") == -1) && (email.value != "")){
			valid = false;
			email.style.backgroundColor = bgBad; 
			alert("Please enter a valid e-mail address.");
			email.focus();
			email.blur();
			email.select();
			return valid;
		}			

		// check password length
		if (password.value.length != 0 && password.value.length < 6) {
			valid = false;
			password.style.backgroundColor = bgBad;
			alert("Password must be at least 6 characters.");
			password.focus();
			password.blur();
			password.select();
			return valid;
		}
	
		// check password confirmation
		if (password.value != passwordconf.value) {
			valid = false;
			passwordconf.style.backgroundColor = bgBad;
			alert("Your password does not match your password confirmation.");
			passwordconf.focus();
			passwordconf.blur();
			passwordconf.select();
			return valid;
		}

		// confirm we got at least one phone
		//if (homephone.value.length == 0 && workphone.value.length == 0) {
		//	valid = false;
		//	homephone.style.backgroundColor = bgBad;
		//	workphone.style.backgroundColor = bgBad;
		//	alert("At least one phone number is required.");
		//	homephone.focus();
		//	homephone.blur();
		//	homephone.select();
		//	return valid;
		//}

		// check zip code
		//if (zip.value.length != 0 && zip.value.length < 5) {
		//	valid = false;
		//	zip.style.backgroundColor = bgBad;
		//	alert("Zip Code must consist of five digits.");
		//	zip.focus();
		//	zip.blur();
		//	zip.select();
		//	return valid;
		//} else {
		//	if (isNaN(zip.value)) {
		//		valid = false;
		//		zip.style.backgroundColor = bgBad;
		//		alert("Zip Code must be numeric characters.");
		//		zip.focus();
		//		zip.blur();
		//		zip.select();
		//		return valid;
		//	}
		//}
	}
	
	if(FORM.name == "frmSchedule"){
		var homephone = FORM.homephone;
		var workphone = FORM.workphone;
		var day = FORM.day;
		var time = FORM.time;
		var comments = FORM.comments;
		
		// confirm we got at least one phone
		if (homephone.value.length == 0 && workphone.value.length == 0) {
			valid = false;
			homephone.style.backgroundColor = bgBad;
			workphone.style.backgroundColor = bgBad;
			alert("At least one phone number is required.");
			homephone.focus();
			homephone.blur();
			homephone.select();
			return valid;
		}

		// confirm we got a day
		if (day.value == "") {
			valid = false;
			day.style.backgroundColor = bgBad;
			alert("Please select your desired date for this appointment.");
			day.focus();
			return valid;
		}
		
		// was 'other? selected? then we need some comments
		if (day.value == "Other (specify below)" && comments.value.length == 0) {
			valid = false;
			comments.style.backgroundColor = bgBad;
			alert("Please let us know your desired date for this appointment.");
			comments.focus();
			return valid;
		}
		
		// confirm we got a time
		for (var j=0; j < time.length; j++){
			if (time[j].checked){
				valid = true;
				break;
			}
			else{
				valid = false;
			}
		}
		if(!valid) {
			alert("Please select your desired time for this appointment.");
			for (var j=0; j < time.length; j++){
				time[j].style.backgroundColor = bgBad;
			}
			return valid;
		}
		else{
			for (var j=0; j < time.length; j++){
				time[j].style.backgroundColor = bgGood;
			}
		}
	}
	
	if(FORM.name == "frmMoreInfo"){
		var homephone = FORM.homephone;
		var workphone = FORM.workphone;
		
		// confirm we got at least one phone
		if (homephone.value.length == 0 && workphone.value.length == 0) {
			valid = false;
			homephone.style.backgroundColor = bgBad;
			workphone.style.backgroundColor = bgBad;
			alert("At least one phone number is required.");
			homephone.focus();
			homephone.blur();
			homephone.select();
			return valid;
		}
	}
	
	if(FORM.name == "frmEditProfile"){
		var homephone = FORM.homephone;
		var workphone = FORM.workphone;
		
		// confirm we got at least one phone
		if (homephone.value.length == 0 && workphone.value.length == 0) {
			valid = false;
			homephone.style.backgroundColor = bgBad;
			workphone.style.backgroundColor = bgBad;
			alert("At least one phone number is required.");
			homephone.focus();
			homephone.blur();
			homephone.select();
			return valid;
		}
	}
	
	if(FORM.name == "frmChangePW"){
		var current_pw = FORM.current_password;
		var password = FORM.password;
		var passwordconf = FORM.password_confirmation;

		// check password length
		if (password.value.length != 0 && password.value.length < 6) {
			valid = false;
			password.style.backgroundColor = bgBad;
			alert("Password must be at least 6 characters.");
			password.focus();
			password.blur();
			password.select();
			return valid;
		}
	
		// check password confirmation
		if (password.value != passwordconf.value) {
			valid = false;
			passwordconf.style.backgroundColor = bgBad;
			alert("Your password does not match your password confirmation.");
			passwordconf.focus();
			passwordconf.blur();
			passwordconf.select();
			return valid;
		}
	}
	

	// return proper validation status
	if(!valid) {
		alert(missingFields);
	}

	// return proper validation status
	return valid;
}

function fnChangeCmd(frm,sCmd){
	// validation variables
	var bgBad = "#FFE78C";
	var valid = true;
	var email = document.forms[frm].elements['email']

	if(!email.value){
		valid = false;
		email.style.backgroundColor = bgBad; 
		alert("Please enter the e-mail address you used to regsiter.");
		email.focus();
		email.blur();
		email.select();
	}	else {
		// check email for @ symbol
		if((email.value.indexOf("@") == -1) || (email.value.indexOf(".") == -1)){
			valid = false;
			email.style.backgroundColor = bgBad; 
			alert("Please enter a valid e-mail address.");
			email.focus();
			email.blur();
			email.select();
		}
	}

	if(valid) {
		document.forms[frm].elements['cmd'].value = sCmd; 
		document.forms[frm].submit();
	}
}

function isEmail(str) {
	var foundOffset;
	foundOffset = str.indexOf("@",1)
	if (foundOffset == -1)
		return false;
	foundOffset = str.indexOf(".",3)
	if (foundOffset == -1)
		return false;
	foundOffset = str.lastIndexOf(".")
	if (foundOffset == (str.length-1))
		return false;
	return true;
}


// ------------------------------------------------------------------------------------------------
// Cookie functions
// ------------------------------------------------------------------------------------------------
function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	  x=x.replace(/^\s+|\s+$/g,"");
	  if (x==c_name)
	    {
	    return unescape(y);
	    }
	}
}
// ------------------------------------------------------------------------------------------------


// ------------------------------------------------------------------------------------------------
// Registration functions
// ------------------------------------------------------------------------------------------------
function checkVisited() {
	var visited = getCookie("Visited");

	if (visited==null || visited=="") {
		var ctr = getCookie("prop_ctr");
		if (ctr!=null && ctr!="") {
			if (ctr >= 3) {
				showColorBox('sign_up');
				document.cookie = "prop_ctr=1";
			} else {
		  		ctr++;
				document.cookie = "prop_ctr=" + ctr;
			}
	   } else {
	     setCookie("prop_ctr",1);
	   }
   }
}
// ------------------------------------------------------------------------------------------------


// ------------------------------------------------------------------------------------------------
// Property Compare functions
// ------------------------------------------------------------------------------------------------
function checkCompares(id) {
	var compare = getCookie("compare");
	var exists = false;

	if (compare!=null && compare!="") {
		var arrID=compare.split(",");
		for (i=0;i<arrID.length;i++) {
			if(id==arrID[i]) {
				exists = true;
				break;
			}
		}
	}
	return exists;
}

function updateCompareButton(id) {
	if (checkCompares(id)) {
		document.getElementById("property_compare").src="/listings_local/images/remove-from-prop.jpg"
	} else {
		document.getElementById("property_compare").src="/listings_local/images/add-to-prop.jpg"
	}
}

function Add_Remove_Listing(id) {
	var compare = getCookie("compare");
	var arrID = new Array();
	var exists = false;

	if (compare!=null && compare!="") {
		arrID = compare.split(",");
		for (i=0;i<arrID.length;i++) {
			if(id==arrID[i]) {
				exists = true;
				break;
			}
		}
	}

	if (exists) {
		// listing in array so remove it
		arrID.splice(i,1);
		// update button
		if (document.getElementById("property_compare")!=null) {
			// detail page
			document.getElementById("property_compare").src="/listings_local/images/add-to-prop.jpg";
		} else {
			// summary page
			document.getElementById(id).innerHTML = "ADD TO PROPERTY COMPARE";
			document.getElementById(id).style.backgroundColor = "#fcf9f0";
			document.getElementById(id).style.color = "#5B646D";
		}
	} else {	
		if (arrID.length<5) {
			// listing not in array so add it
			arrID.push(id);
			// update button
			if (document.getElementById("property_compare")!=null) {
				// detail page
				document.getElementById("property_compare").src="/listings_local/images/remove-from-prop.jpg";
			} else {
				// summary page
				document.getElementById(id).innerHTML = "REMOVE FROM PROPERTY COMPARE";
				document.getElementById(id).style.backgroundColor = "#E8584D";
				document.getElementById(id).style.color = "#ffffff";
			}
		} else {
			alert("You can have a maximum of 5 properties in the Property Compare.");
			return false;
		}
	}
	setCookie("compare",arrID.join());
}

function Add_Remove_Listing2(id) {
	var compare = getCookie("compare");
	var arrID = new Array();
	var exists = false;

	if (compare!=null && compare!="") {
		arrID = compare.split(",");
		for (i=0;i<arrID.length;i++) {
			if(id==arrID[i]) {
				exists = true;
				break;
			}
		}
	}

	if (exists) {
		// listing in array so remove it
		arrID.splice(i,1);
	} else {	
		if (arrID.length<5) {
			// listing not in array so add it
			arrID.push(id);
		} else {
			alert("You can have a maximum of 5 properties in the Property Compare.");
			return false;
		}
	}
	setCookie("compare",arrID.join());
	window.location.reload();
}


function CompareListings() {
	var compare = getCookie("compare");

	if (compare!=null && compare!="") {
		arrID = compare.split(",");
		if (arrID.length > 1) {
			document.location.href='comparelistings.asp';
		} else {
			alert("Please select more than one property to compare.");
		}
	} else {
		alert("Please select more than one property to compare.");
	}

}
// ------------------------------------------------------------------------------------------------


// ------------------------------------------------------------------------------------------------
// ColorBox functions
// ------------------------------------------------------------------------------------------------
function showColorBox(pgName,qs,reload) {
	var page = pgName + ".asp" + ((qs==null) ? "" : "?" + qs);
	$.colorbox({
		width:"530", 
		height:"470", 
		iframe:true, 
		href:"/listings/" + page	,
		onCleanup:function(){((reload==null) ? "" : window.parent.location.reload());} 
	});
}

function checkLogin() {
	var visited = getCookie("Visited");

	// check if user is logged in; if we return here then they closed window without signing in/up so redirect to search page
	if (visited==null || visited=="") {
		$.colorbox({
			width:"530", 
			height:"290", 
			iframe:true, 
			href:"/listings/sign_in.asp",
			onCleanup:function(){window.parent.location.href="/listings/searchoptions.asp"} 
		});
   }
}
// ------------------------------------------------------------------------------------------------


