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; }
}
  
/* 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){
	oSection.style.display = (oSection.style.display == "none") ? "" : "none";
}

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") {
			if(FORM.elements[i].required){
				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;
}

