/*
Copyright 2006 Sell It Yourself Homes, Inc.
Usage of these scripts is limited to their original intent as developed and deployed
for Sell It Yourself Homes Copying, selling or distributing of these scripts, 
in whole or in part, is strictly prohibited.
*/


	///////////////////////////////////////
	// Frame Buster
	///////////////////////////////////////

if (window != window.top)
  top.location.href = location.href;
  


	///////////////////////////////////////
	// On-Click-Pop-Center
	///////////////////////////////////////
	// Requires "onClick="NewWindow(this.href,'title','400','300','no');return false;"" in the A HREF tag
	// Example: <a href="doc.html" onClick="NewWindow(this.href,'title','400','300','no');return false;">LINK</a>

function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes,,toolbar=no,menubar=no'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}




	///////////////////////////////////////
	// Auto-Select Field
	///////////////////////////////////////
	// Requires "onLoad=ready()" in the BODY tag
	// Example: <body onLoad=ready()>
	// Requires form name to match where it reads "form" in script
	// Example: <form name="form">
	// Requires form field name to match where it reads "field" in script
	// Example: <input name="field">

function ready(){document.form.field.focus();}



	///////////////////////////////////////
	// Form Validation
	///////////////////////////////////////

function validateMailMe(form) {
	return (
				 checkSelect(form.elements["mailme[recipient]"],"V","","Please enter Your Friend or Neighbor's Name.",false) &&
				 checkSelect(form.elements["mailme[to]"],"V","","Please enter Your Friend or Neighbor's E-mail Address.",false) &&
				 checkEmail(form.elements["mailme[to]"],false) &&
				 checkSelect(form.elements["mailme[to]"],"V","joe@example.com","Please enter a valid E-mail Address.",false) &&
				 checkSelect(form.elements["mailme[sender]"],"V","","Please enter Your Name.",false) &&
				 checkSelect(form.elements["mailme[from]"],"V","","Please enter Your E-mail Address.",false) &&
				 checkEmail(form.elements["mailme[from]"],false) &&
				 checkSelect(form.elements["mailme[from]"],"V","joe@example.com","Please enter a valid E-mail Address.",false));
}

function validateEmailSide( form ) {
	return (
				 checkSelect(form.elements["email"],"V","","Please enter your E-mail Address.",false) &&
				 checkEmail(form.elements["email"],false) &&
				 checkSelect(form.elements["email"],"V","joe@example.com","Please supply valid E-mail Address.",false));	
}

function validateContactUs(form) {
	return (
				 checkSelect(form.elements["fullname"],"V","","Please enter Your Name.",false) &&
				 checkSelect(form.elements["email"],"V","","Please enter your E-mail Address.",false) &&
				 checkEmail(form.elements["email"],false) &&
				 checkSelect(form.elements["email"],"V","joe@example.com","Please supply valid E-mail Address.",false) &&
				 checkSelect(form.elements["phone"],"V","","Please enter a Phone Number where you can be reached.",false) &&
				 checkSelect(form.elements["phonetype"],"V","","Please tell us what Phone Number this is by selecting from Home Phone, Work Phone or Mobile Phone.",false) &&
				 checkSelect(form.elements["message"],"V","","Please provide a Message with your inquirey.",false));
}

function validateContactLister(form) {
	return (
				 checkSelect(form.elements["fullname"],"V","","Please enter Your Name.",false) &&
				 checkSelect(form.elements["email"],"V","","Please enter your E-mail Address.",false) &&
				 checkEmail(form.elements["email"],false) &&
				 checkSelect(form.elements["email"],"V","joe@example.com","Please supply valid E-mail Address.",false) &&
				 checkSelect(form.elements["phone"],"V","","Please enter a Phone Number where you can be reached.",false) &&
				 checkSelect(form.elements["phonetype"],"V","","Please indicate what Phone Number this is by selecting from Home Phone, Work Phone or Mobile Phone.",false) &&
				 checkSelect(form.elements["message"],"V","","Please provide a Message with your inquirey.",false));
}

function validateNewAccount(form) {
	return (
				 checkSelect(form.elements["fullname"],"V","","Please enter your Full Name for your account.",false) &&
				 checkSelect(form.elements["email"],"V","","Please enter your E-mail Address. This will serve as your Login ID.",false) &&
				 checkEmail(form.elements["email"],false) &&
				 checkSelect(form.elements["email"],"V","joe@example.com","Please supply valid E-mail Address.",false) &&
				 checkSelect(form.elements["password1"],"V","","Please enter something to Create a Password for your new account.",false) &&
				 checkSelect(form.elements["password2"],"V","","Please confirm your password by re-typing it in the Confirm Password field.",false));
}

function validateNewForSale(form) {
	return (
				 checkSelect(form.elements["address"],"V","","Please enter the Property Address of your listing.",false) &&
				 checkSelect(form.elements["zip"],"V","","Please enter the Zip Code of your listing.",false) &&
				 checkSelect(form.elements["price"],"V","$","Please enter your Asking Price for this listing.",false) &&
				 checkSelect(form.elements["price"],"V","","Please enter your Asking Price for this listing.",false));
}

function validateNewForRent(form) {
	return (
				 checkSelect(form.elements["property_name"],"V","","Please enter a Property Name for your rental listing.",false) &&
				 checkSelect(form.elements["address"],"V","","Please enter your rental property's Address.",false) &&
				 checkSelect(form.elements["zip"],"V","","Please enter the Zip Code of your listing.",false));
}

function validateNewOpen(form) {
	return (
				 checkSelect(form.elements["address"],"V","","Please enter the Property Address of your listing.",false) &&
				 checkSelect(form.elements["zip"],"V","","Please enter the Zip Code of your listing.",false) &&
				 checkSelect(form.elements["price"],"V","$","Please enter your Asking Price for this listing.",false) &&
				 checkSelect(form.elements["price"],"V","","Please enter your Asking Price for this listing.",false));
}


function validatePayment(form) {
	return (
				 checkSelect(form.elements["address"],"V","","Please enter your Billing Address.",false) &&
				 checkSelect(form.elements["city"],"V","","Please enter the City for your billing address.",false) &&
				 checkSelect(form.elements["state"],"V","","Please select the State for your billing address.",false) &&
				 checkSelect(form.elements["zip"],"V","","Please enter the Zip Code for your billing address.",false) &&
				 checkSelect(form.elements["cc_type"],"V","","Please select your Credit Card Type (Visa, MasterCard, etc.).",false) &&
				 checkSelect(form.elements["cc_first_name"],"V","","Please enter the First Name exactly how it appears on your credit card.",false) &&
				 checkSelect(form.elements["cc_last_name"],"V","","Please enter the Last Name exactly how it appears on your credit card.",false) &&
				 checkSelect(form.elements["cc_num"],"V","","Please enter your Credit Card Number.",false) &&
				 checkSelect(form.elements["cc_expires_month"],"V","00","Please select your credit card's Expiration Month",false) &&
				 checkSelect(form.elements["cc_expires_year"],"V","00","Please select your credit card's Expiration Year",false) &&
				 checkSelect(form.elements["cc_ccv"],"V","","Please enter your credit card's 3-digit CCV Number.",false) &&
				 checkAgreement(form.elements['agreeterms'].checked));
}




function validateEmailSignup(form) {
	return (
				 checkSelect(form.elements["firstname"],"V","","Please enter your First Name.",false) &&
				 checkSelect(form.elements["firstname"],"V","First Name","Please enter your real First Name.",false) &&
				 checkSelect(form.elements["lastname"],"V","","Please enter your Last Name.",false) &&
				 checkSelect(form.elements["lastname"],"V","First Name","Please enter your real Last Name.",false) &&
				 checkSelect(form.elements["email"],"V","","Please enter your E-mail.",false) &&
				 checkEmail(form.elements["email"],false) &&
				 checkSelect(form.elements["email"],"V","joe@example.com","Please supply a valid E-mail Address.",false));
}

function validateUnsub(form) {
	return (
				 checkSelect(form.elements["email"],"V","","Please enter your E-mail.",false) &&
				 checkEmail(form.elements["email"],false) &&
				 checkSelect(form.elements["email"],"V","joe@example.com","Please supply a valid E-mail Address.",false));
}

function validateLogin(form) {
	return (
				 checkSelect(form.elements["email"],"V","","Please enter your E-mail.",false) &&
				 checkEmail(form.elements["email"],false) &&
				 checkSelect(form.elements["email"],"V","joe@example.com","Please supply a valid E-mail Address.",false) &&
				 checkSelect(form.elements["password"],"V","","Please enter your Password.",false));
}


function validateSignin(form) {
	return (
				 checkSelect(form.elements["loginemail"],"V","","Please enter your E-mail.",false) &&
				 checkEmail(form.elements["loginemail"],false) &&
				 checkSelect(form.elements["loginemail"],"V","joe@example.com","Please supply valid E-mail Address.",false) &&				 
				 checkSelect(form.elements["password"],"V","","Please enter your Password.",false));
}


function checkPasswords(form) {
	
	if ( form.elements["password1"].value != form.elements["password1"].value ) {
		alert("Your passwords does not match.  Please enter re-enter your passwords. REMEMBER, passwords are cAsE sEnSiTiVe.");
		return false;
	} else {
		return true;
	}
	
}




function checkEmailSignup() {

	var frm = document.emailSignup;
	
	if ( (frm.firstname.value == "" ) || (frm.firstname.value == " " ) ) {
		
		alert("First name is required");
		return false;			
	}
	
	if ( (frm.lastname.value == "" ) || (frm.lastname.value == " " ) ) {
		alert("Last name is required");
		return false;	
	}	
	
	if ( (frm.email.value == "") || (frm.email.value == " ") ) {
		
		alert("E-mail is required");
		return false;	
	}
	
	return true;
	
}
function LoginOptionCheck(myself) {
	frm = document.forms['new_login'];
	// if the new Customer item is checked
	var elem_pass_new = document.getElementById('newcustomerpassword');
	var elem_pass_old = document.getElementById('returningcustomerpassword');
	
	if ( myself.value == "new" ) {
		elem_pass_old.disabled = true;
		elem_pass_old.style.display = "none";
		elem_pass_new.disabled = true;
		elem_pass_new.style.display = "";
		frm.elements['form_action'].value = "account_new";
		//alert(frm.action);
		frm.action = "new_listing.php";
		
	} else {
	
		elem_pass_old.disabled = false;
		elem_pass_old.style.display = "";
		elem_pass_new.disabled = true;
		elem_pass_new.style.display = "none";
        frm.elements['loginemail'].focus();
		frm.elements['form_action'].value = "account";
		//alert(frm.action);
		frm.action = "login.php";
	}
	
	frm.elements['loginemail'].focus();
}


function focusLoginField() {
	if ( typeof(document.new_login) != "undefined" ) {
		document.new_login.loginemail.focus();
	}
}

function loadPreview() {

	var caption_value = window.opener.document.getElementById("caption").value;
	var headline_obj = window.opener.document.getElementById("headline");
	var body_obj = window.opener.document.getElementById("body");
	var child_head = document.getElementById("headline");
	var child_body = document.getElementById("body");
	var child_caption = document.getElementById("img_caption");
	
	if ( caption_value != '' && child_caption != null ) {
		
		child_caption.innerHTML = caption_value;
	}
	child_head.innerHTML = headline_obj.value;
	child_body.innerHTML = body_obj.value;
	
}

function doProfileChange() {
	document.getElementById('update_profile').style.display = "inline";
}

function fadeout() {
	for (var iterator = 0; iterator < arguments.length; iterator++) {
		arguments[iterator].value = '';
		arguments[iterator].disabled = true;
	}
}

function fadein() {
	for (var iterator = 0; iterator < arguments.length; iterator++) {
		arguments[iterator].disabled = false;
	}
	arguments[1].focus();
	arguments[1].select();
}


function validateInteger( strValue ) 
{	
  var objRegExp  = /(^-?\d\d*$)/;

  return objRegExp.test(strValue);
}

function $() 
{
	var elements = new Array();
	
	for ( var i=0; i < arguments.length; i++ ) {
		var element = arguments[i];
		if ( typeof element == 'string' ) {
			element = document.getElementById( element );
		}
		
		if ( arguments.length == 1 ) {
			return element;
		}
		
		elements.push( element );
	}
	
	return elements;
}


function doSavingsForm()
{
	var uri = "LiveUpdater.php";
	var data = "";
	
	data += nh.compressData('action', 'handleSavings');
	data += nh.compressData('selling_price', $( 'selling_price' ).value);
	
	LiveUpdater( uri, data );	
}

function resetSavingsForm()
{	
	var uri = "LiveUpdater.php";
	var data = "";
	
	data += nh.compressData('action', 'resetSavings');
	
	LiveUpdater( uri, data );		
}

function num2money(n_value) 
{
	// validate input
	if (isNaN(Number(n_value)))
	return 'ERROR';
	
	// save the sign
	var b_negative = Boolean(n_value < 0);
	n_value = Math.abs(n_value);
	
	// round to 1/100 precision, add ending zeroes if needed
	var s_result = String(Math.round(n_value*1e2)%1e2 + '00').substring(0,2);
	
	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value > 1) {
		s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value%1e3);
		s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
		b_first = false;
		n_value = n_value/1e3;
	}
	// add at least one integer digit
	if (b_first)
		s_result = '0.' + s_result;
	
	// apply formatting and return
	return b_negative ? '($' + s_result + ')' : '$' + s_result;
}

function fillAddress(state)
{
	var uri = "LiveUpdater.php";
	var data = "";
	
	data += nh.compressData('action', 'fillAddress');
	data += nh.compressData('state', state);
	
	LiveUpdater( uri, data );
}

function fillOpenHouseAddress()
{
	var uri = "LiveUpdater.php";
	var data = "";
	
	var state = document.getElementById('prefill').checked;
	var id = document.getElementById('current_listing').value;
	
	data += nh.compressData('action', 'fillOpenHouseAddress');
	data += nh.compressData('state', state);
	data += nh.compressData('id', id);
	
	LiveUpdater( uri, data );
}

function swapImage( img, url, width, height, zoom_url, caption )
{
	$( img ).width = width;	
	$( img ).height = height;	
	$( img ).src = url;	
	$( 'img_caption' ).innerHTML = caption;		
	$( 'img_zoom1' ).href="pop_zoom.php?uri="+zoom_url;
	$( 'img_zoom2' ).href="pop_zoom.php?uri="+zoom_url;
	$( 'img_zoom3' ).href="pop_zoom.php?uri="+zoom_url;
}

function checkAgreement( checked )
{
	if ( !checked ) {
		alert("You must agree to the Service Agreement before checking out!");
		return false;
	} else {
		return true;
	}	
}

function handleEnter(field, event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		field.form.submit();
	}
}