///////repload images
//window.onload = preLoad('contactus','video');

function preLoad()
{
	//// preload images function
	var imgroot_nav = "http://66.153.118.118/mountainhouse/media/common/";
	//var iPath = '_images/';
	var iExt = 'gif';
	var suffix = new Array('','_over');
	
	if(document.images)
	{
		//alert("in");
	  var argLen = arguments.length;
	  
	  //alert("arglen:" + argLen);
	  
	  for(i=0; i<argLen; i++)
	  {
	  	//alert("in for loop");
	    var arg = arguments[i];
	    var sufLen = suffix.length;
	    
	   	//alert("current arg:" + arg);
	    //alert("suflen:" + sufLen);
	    
	    for(j = 0; j < sufLen; j++)
	    {
	      var suf = suffix[j];
	      
	      self[arg + suf] = new Image();
	      self[arg + suf].src = imgroot_nav + arg + suf + "." + iExt;
	    } //end for suffix
	  } // end for arglen
	} // end if documents
} // end function


///// image roll over

function rollOver(iName,iState)
{
  if(document.images)
  {
    document.images[iName].src = eval(iName + iState + ".src");
    //alert(eval(iName + iState + ".src"));
  }
}

/// this function is used in the printer friendly floorplan pages to toggle between first and second floor

////the object you want to show and hide needs to be surrounded by this tag
///<div id="paymentoption" style="display:none;">---content---</div>
///
///this is using a drop down or radio button
///the actuator of can be
// onclick="show_hide('paymentoption','on');"
//
// when using check box, use:
// onclick="show_hide('paymentoption',this);"
// if statement change as so:
//
// if(state.checked)

/* by Colins Lam */
function select_min_dropdown()
{
	if ( document.findahome.min_sq.value == "" && document.findahome.min_bed.value == "" && document.findahome.min_bath.value == "" && document.findahome.base_price.value == "" && document.findahome.to_price.value == "" && document.findahome.builder.value == "" )
	{
		alert("Please select at least on search criteria in order to search.");
		return false;
	}
	else
	{
		status = price_compare(document.findahome.base_price.value,document.findahome.to_price.value);
		return status;
	}
}



/* by Colins Lam */
function price_compare(base_price,max_price)
{
	b_price = parseInt(base_price);
	m_price = parseInt(max_price);
		
	if ( b_price >= m_price )
	{
		alert("Please adjust your \'From\' Price to be Less than your \'To\' Price");
		return false;
	}
	else
	{
		return true;
	}
}

function show_hide(obj,state) {

	var elem = document.getElementById(obj);
	
	if(state == "on")
	{
		elem.style.display = "block";
	}
	else
	{
		elem.style.display = "none";
	}
	
}

function enableOption()
{
	var agree = confirm("Please be aware that by changing your selection, your preferences will be cleared.");
	if (agree)
	{
		document.form.action = "join.php";
		document.form.selection.disabled=false;
	}
}

function enableOther()
{
	if (document.form.Other.checked == true)
	{
		document.form.OtherActivities.disabled=false;
		document.form.OtherActivities.focus();
	}
	else
	{
		document.form.OtherActivities.value = '';
		document.form.OtherActivities.disabled=true;
	}
}

function enableBroker(val)
{
	if (val == 'yes')
	{
		document.form.brokernameOne.disabled=false;
		document.form.brokernameTwo.disabled=false;
		document.form.brokernameOne.focus();
	}
	else
	{
		document.form.brokernameOne.value = '';
		document.form.brokernameTwo.value = '';
		
		document.form.brokernameOne.disabled=true;
		document.form.brokernameTwo.disabled=true;
	}
} 

/////
///onKeyPress="return numbersonly(this, event)"
/////

function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;
	if (window.event){
		key = window.event.keyCode;
	}else if (e){
		key = e.which;
	}else{
		return true;
	}
	keychar = String.fromCharCode(key);
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){
		return true;
	}else if ((("+1234567890").indexOf(keychar) > -1)){
		return true;
	}else if (dec && (keychar == ".")){
		myfield.form.elements[dec].focus();
		return false;
	}else{
		return false;
	}
}



//// enable disable the other box in the interest list form
//// when the other option is selected

function enable_other(val)
{
	if (val == "Other")
	{
		form.other_feature.disabled = false;
		form.other_feature.focus();
	}
	else
	{
		form.other_feature.disabled = true;
	}
}


/// jump phone number fields
//// onkeyup="numTyped(this.value,phone2,3);"

function numTyped(current, next,maxlen) {
  var len = current.length;
  if (len==maxlen) {
    next.focus();
  }
}
/// popup window function
function popup(URL,w,h) {

day = new Date();
id = day.getTime();

var width = w;
var height = h;

var left = ((screen.width-width)/2);
var top = ((screen.height-height)/2);

eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + '');");
}

// disclaimer footer
function footer()
{
var root = 'http://66.153.118.118/mountainhouse/';

var footer_code = '<table width="778" border="0" cellspacing="0" cellpadding="0">';
var footer_code = footer_code + '<tr>';
var footer_code = footer_code + '<td bgcolor="#681b00"><img src="' + root + 'media/common/spacer.gif" alt="" width="1" height="1" border="0"></td>';
var footer_code = footer_code + '</tr>';
var footer_code = footer_code + '<tr>';
var today = new Date(); var footer_code = footer_code + '<td class="disc" align="center"><img src="' + root + 'media/common/eho_17.gif" alt="" width="15" height="14" align="absmiddle" border="0" hspace="5">&copy;'+today.getFullYear()+' <img src="' + root + 'media/common/trimark_disc_32.gif" alt="" width="240" height="21" align="absmiddle" border="0" hspace="5" vspace="5">All Rights Reserved. <a class="disc" href="javascript:void(0);" onclick="popup(\'' + root + 'disclaimer.html\',\'550\',\'600\');">Privacy&nbsp;&amp;&nbsp;Disclaimer.</a>&nbsp;Site Created By <a href="Http://www.westonmason.com" target="_blank">Weston | Mason</a> Marketing</td>';
var footer_code = footer_code + '</tr>';
var footer_code = footer_code + '</table>';

return footer_code;
}


//// print function testing for pc or mac
function printPage() 
{
	if (window.print)
	{
		window.print();
	} 
	else
	{
		alert('To print on Macs, use Command + P.');
	}
}

/// form validation starts here

var new_fieldname = ""

//this is the onclick that calls this function
//
// onClick="return check(form,form.elements.length)"
//

function check(form,x)

{

	var message = ""
	var more_message = new Array //(4)
	var showmsg = "no"
	var doItNow = "yes"

	var required = new Array (12)

	if (doItNow == "yes") 	{

			var i = 0;

						if (form.first_name.value == "")
							{more_message [i++] = "Please Enter Your First Name"}
						
						if (form.last_name.value == "")
							{more_message [i++] = "\nPlease Enter Your Last Name"}
						
						if (form.zip.value != '' && form.zip.value.length < 5)
							{more_message [i++] = "\nPlease Enter A Valid Zip code"}
						
						
						// phone validation
						if ( count_phoneboxes('phone[]') >= 1 && count_phoneboxes('phone[]') < 3)
						{
							more_message [i++] = "\nPlease Enter Your Phone Number Completely"
						}						
						else if ( count_num_boxes('phone[]') <= 3 && count_phoneboxes('phone[]') == 3)
						{
							if ((box_length('phone[]') > 0 && box_length('phone[]') <= 3) || document.getElementById('p3').value.length < 4)
							{
								more_message [i++] = "\nPlease Enter Your Phone Number Completely"	
							}
						}
						
						// fax validation
						if ( count_phoneboxes('fax[]') >= 1 && count_phoneboxes('fax[]') < 3)
						{
							more_message [i++] = "\nPlease Enter Your Fax Number Completely"
						}						
						else if ( count_num_boxes('fax[]') <= 3 && count_phoneboxes('fax[]') == 3)
						{
							if ((box_length('fax[]') > 0 && box_length('fax[]') <= 3) || document.getElementById('f3').value.length < 4)
							{
								more_message [i++] = "\nPlease Enter Your Fax Number Completely"	
							}
						}
						
						
						/// email validation
						
						if (form.email.value == "" )
						{
							more_message [i++] = "\nPlease Your E-mail Address"
						}
						else if ( form.email.value != '' )
						{
							if ( checkEmail(form.email.value) == false )
							{
								more_message [i++] = "\nPlease Enter A Valid E-mail Address"
							}
							else if ( form.email.value != form.confirm_email.value )
							{
								more_message [i++] = "\nPlease Verify That Both E-mail Address Fields Are Identical"
							}
						}
						
						/// selection
						if (form.selection.value == '')
						{
							more_message [i++] = "\nIn Order To Continue This Form, Please Select The Best Option That Applies To You"
						}
						
						/// end construct messgaes
						if (more_message != "") {

								if (message == "") {message = more_message}

									else

									{message = message + "\n" + more_message}

				        }

						if (message > "")

								{showmsg = "yes"}																

						if (showmsg == "yes")

			{
				alert("The following form field(s) were incomplete or incorrect:\n\n" + message + "\n\n Please complete or correct the form and submit again.")
				return false;
			}
			else
			{ 
				form.submit()
				return true;
			}

		}

}

/// functions for form validation script

//// phone funtions
function box_length(elem)
{
	len_short = 0;
	with(document.form)
	{
		for (var i=0; i <elements.length; i++)
		{
			if (elements[i].name == elem)
			{
				if (elements[i].value.length < 3)
				{
					len_short = len_short + 1;
				}
			}
		}
	}
	return len_short;
}

function count_phoneboxes(elem) { 
	box_checked = 0;
	with (document.form) { 
		for (var i=0; i < elements.length; i++) { 
			if (elements[i].name == elem)
				if (  elements[i].value != "" )
				{ 
						box_checked = box_checked + 1;
				}
		}
	}
	return box_checked;
}

function count_num_boxes(elem) {
	box_checked = 0;
	with (document.form) { 
		for (var i=0; i < elements.length; i++) { 
			if (elements[i].name == elem)
				//num = elements[i].value;
				if (elements[i].value.match(/^\d{3,4}$/))
				{ 
					box_checked = box_checked + 1;
				}
		}
	}
	return box_checked;
}

/////////////////

function count_comm_boxes() { 
	box_checked = 0;
	with (document.form) { 
		for (var i=0; i < elements.length; i++) { 
			if (elements[i].name == 'community[]')
				if (  elements[i].value != "" )
				{ 
						box_checked = box_checked + 1;
				}
		}
	}
	return box_checked;
}

function checkEmail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
	{
		return (true)
	}
	else
	{
		return (false)
	}
}

