var req, qs_sd_last, qs_oc_last; 
var mpo_logged = false;
var d = document;
var num_only_rx = /\D/g;

if (window.XMLHttpRequest){
  req = new XMLHttpRequest();
}

if (window.ActiveXObject){
  req = new ActiveXObject("Microsoft.XMLHTTP");
}

// Loaded onLoad of body - y'know... when loaded.
function init(){

  d.getElementById('mpo_username').onkeypress = checkEnter;
  d.getElementById('mpo_password').onkeypress = checkEnter;

  mpo_content();
}

// BEGIN XMLHTTPREQUEST FUNCTIONS
// PULL Method for GETs
function pull(url) {
  
  req.open('GET', url, false);
  req.send(null);
	//alert(req.responseText);
  return req.responseText;

}
// PUT Method for POSTs
// post = 'var=value&var2=value2'
function put(url, post) {

  req.open('POST', url, false);
  req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  req.send(post);

  return req.responseText;

}
// END XMLHTTPRequest Functions

var newwindow;
function popit(url, name)
{
	
	try
	{
		//for non I.E. browsers
		window.open(url,name,'height=400,width=600');
		
	}
	catch(err)
	{	
		//for I.E. Browser
		window.open(url,null,"height=400,width=600,status=yes,toolbar=no,menubar=no,location=no");			
	}
	return;
	
	//if (window.focus) {newwindow.focus()}
}

// BEGIN Members FUNCTIONS
function doLogin(){

   var user = d.getElementById('mpo_username').value;
   var pass = d.getElementById('mpo_password').value;
   var ret = put('/mpo/process_login.aspx', 'mpo_username=' + user + '&mpo_password=' + pass + '&callBack=1');
  
   d.getElementById('mpo_username').blur(); // DO NOT REMOVE - IE Rendering Bug
   d.getElementById('mpo_password').blur(); // DO NOT REMOVE - IE Rendering Bug
   //ret = "mpo_logged=true;var is_seller='Y';"
   //alert(ret);
   eval(ret);
   
   switch(mpo_logged){
     // case true: mpo_pre('You are now logged in.'); break;
     // default: mpo_pre('We could not log you in.');
   }
      
  // If on search page, change search_top
  if(mpo_logged){
    if(d.URL.indexOf('members_login.asp') != -1){
     d.location = '/members_home.asp';
    }
  }
  else{
	d.location = '/loginerror.asp?err=-7';
	}
   
   mpo_content();
}

function doLogout(){
  pull('/mpo/process_logout.aspx');
  
    mpo_logged = false;
    //mpo_pre('You have been logged out.');
    d.getElementById('mpo_username').value = '';
    d.getElementById('mpo_password').value = '';
    d.location="/";
    mpo_content();

}

function mpo_pre(msg){
  set_id('mpo_pre', msg);
  d.getElementById('mpo_pre').className = '';
  setTimeout('d.getElementById("mpo_pre").className = "hide"', 5000);
}

function mpo_content(){ // Need this function for onLoad - if logged in... etc.
  d.getElementById('mpo_content_logged').className = (mpo_logged) ? '' : 'hide';
  d.getElementById('mpo_content_nologin').className = (mpo_logged) ? 'hide' : '';
  d.getElementById('mpo_content_default').className = 'hide';
}

function checkEnter(e){ 
  if(!e){
    var e = window.event;
  }
  if(e.keyCode == 13){ // 13 is the lucky number
     doLogin();
  }
}

function save_search()
{
  var search_name = prompt('What would you like to name this search?');
  if(search_name)
  {
    pull('/mpo/mposave.aspx?a=add&b=search&name=' + search_name);
    set_id('search_top', 'This search has been saved as \'' + search_name + '\'');
  }
}

function remove_search(search_name)
{
  //var search_name = prompt('enter the the savedsearch name');
  //if(search_name)
  //search_name = ReplaceSpecial(search_name);
  pull('/mpo/mposave.aspx?a=remove&b=search&name=' + search_name);
  parent.location = d.URL;
}

function save_house(uid)
{
  //alert(uid);
  pull('/mpo/mposave.aspx?a=add&b=home&uid=' + uid);
}

function remove_house(uid)
{
  pull('/mpo/mposave.aspx?a=remove&b=home&uid=' + uid);
}

// END Members FUNCTIONS

function vt_open(url, uid, zip){

  var http = (url.indexOf('http') == -1) ? 'http://' : '';
  
  if(url.indexOf('visualtour.com') != -1){
    
    var visual_id = url.substr( url.lastIndexOf('=') + 1 );
    //alert(visual_id);
    var visual_page = 'http://visualtour.com/applets/vt_viewer/vt_router.asp?asr=true&T=' + visual_id + '&ref=';
    var x = (screen.width - 570) / 2;
    var y = (screen.height - 525) / 2;

    //Define Browser Window Style
    stats='toolbar=no,location=no,directories=no,status=no,menubar=no,'; 
    stats += 'scrollbars=no,resize=no,width=570,height=525'; 
    stats += ',top=' + y + ',left=' + x;
   
    //Open a viewer window
    
    var win = window.open(visual_page,'VisualTourViewer',stats);
  } 
  else {
  
    open(http + url, 'vtour', 'status=yes,top=0,left=0,width=760,height=450');
  }
  
  // Do property stat increment
  pull('/mpo/process_vt.aspx?zip=' + zip + '&uid=' + uid);
 
}

// Similar to PHP's in_array()
// str needle
// array haystack
function in_array(needle, haystack){
  var i, count = haystack.length;
  for(i=0; i <= count; i++){
    if(haystack[i] == needle){
      return true;
    }
  }
  return false;
}

function set_id(id, new_text){
  d.getElementById(id).innerHTML = new_text;
}

function get_value(id){
  return d.getElementById(id).value;
}
////////////////////////
// Validation Routines
////////////////////////
function validate_email(email) {
  var e_rx = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
  var regex = new RegExp(e_rx);
  return regex.test(email);
}

function validate_zipcode(tZip){
  tZip = tZip.replace(num_only_rx, '');
  return (tZip.length == 5);  
}

function validate_phone(pnumber){
  pnumber = pnumber.replace(num_only_rx, '');
  return (pnumber.length == 10);  
}

// ZIP CODES

function get_city_options(county){

  if (county == 'sd'){

	 return '<select name="zipcode" onChange="d.getElementById(\'ss_city\').value = d.getElementById(\'search_select\').options[d.getElementById(\'search_select\').selectedIndex].text;" id="search_select">\
	 <option value="91901,92110,91902,92003,92004,91905,91906,92007,92008,92009,92130,91910,91911,91913,91914,91915,92117,92115,92118,92155,92120,92014,91916,92101,91917,92105,92019,92020,92021,92114,92024,92025,92026,92027,92029,92028,92108,92110,91931,91932,91934,91935,92036,92037,92093,91941,91942,92040,91945,92111,92113,92126,92145,92103,92108,92123,91948,92562,92563,92564,91950,92116,92104,92107,92054,92056,92057,92154,92109,92059,92060,92139,92061,91962,92106,91963,92064,92065,92127,92128,92129,92067,92119,92102,92069,92078,92173,92070,92071,92131,92075,92121,91977,91978,92584,92585,92586,92587,91980,92589,92590,92591,92592,92593,92124,92122,92082,92081,92083,92084,92066,92086">All cities</option>\
    <option value="91901">Alpine</option>\
    <option value="92110">Bay Park</option>\
    <option value="91902">Bonita</option>\
    <option value="92003">Bonsall</option>\
    <option value="92004">Borrego Springs</option>\
    <option value="91905">Boulevard</option>\
    <option value="91906">Campo</option>\
    <option value="92007">Cardiff by the Sea</option>\
    <option value="92008,92009">Carlsbad</option>\
    <option value="92130">Carmel Valley</option>\
    <option value="91910,91911,91913,91914,91915">Chula Vista</option>\
    <option value="92117">Clairemont</option>\
    <option value="92115">College Area</option>\
    <option value="92118,92155">Coronado</option>\
    <option value="92120">Del Cerro</option>\
    <option value="92014">Del Mar</option>\
    <option value="91916">Descanso</option>\
    <option value="92101">Downtown</option>\
    <option value="91917">Dulzura</option>\
    <option value="92105">East San Diego</option>\
    <option value="92019,92020,92021">El Cajon</option>\
    <option value="92114">Encanto</option>\
    <option value="92024">Encinitas</option>\
    <option value="92025,92026,92027,92029">Escondido</option>\
    <option value="92028">Fallbrook</option>\
    <option value="92108,92110">Fashion Valley</option>\
    <option value="91931">Guatay</option>\
    <option value="91932">Imperial Beach</option>\
    <option value="91934">Jacumba</option>\
    <option value="91935">Jamul</option>\
    <option value="92036">Julian</option>\
    <option value="92037,92093">La Jolla</option>\
    <option value="91941,91942">La Mesa</option>\
    <option value="92040">Lakeside</option>\
    <option value="92530">Lake Elsinore</option>\
    <option value="91945">Lemon Grove</option>\
    <option value="92111">Linda Vista</option>\
    <option value="92113">Logan Heights</option>\
    <option value="92126">Mira Mesa</option>\
    <option value="92145">Miramar</option>\
    <option value="92103">Mission Hills</option>\
    <option value="92108,92123">Mission Valley</option>\
    <option value="91948">Mount Laguna</option>\
    <option value="92562,92563,92564">Murietta</option>\
    <option value="91950">National City</option>\
    <option value="92116">Normal Heights</option>\
    <option value="92104">North Park</option>\
    <option value="92107">Ocean Beach</option>\
    <option value="92054,92056,92057">Oceanside</option>\
    <option value="92154">Otay Mesa</option>\
    <option value="92109">Pacific Beach</option>\
    <option value="92059">Pala</option>\
    <option value="92060">Palomar Mountain</option>\
    <option value="92139">Paradise Hills</option>\
    <option value="92061">Pauma Valley</option>\
    <option value="91962">Pine Valley</option>\
    <option value="92106">Point Loma</option>\
    <option value="91963">Potrero</option>\
    <option value="92064">Poway</option>\
    <option value="92065">Ramona</option>\
    <option value="92127,92128">Rancho Bernardo</option>\
    <option value="92129">Rancho Penasquitos</option>\
    <option value="92067">Rancho Santa Fe</option>\
    <option value="92119">San Carlos</option>\
    <option value="92105,92101,92102,92103,92116,92123,92104,92110,92109,92154">San Diego</option>\
    <option value="92069,92078">San Marcos</option>\
    <option value="92173">San Ysidro</option>\
    <option value="92070">Santa Ysabel</option>\
    <option value="92071">Santee</option>\
    <option value="92131">Scripps Miramar</option>\
    <option value="92075">Solana Beach</option>\
    <option value="92121">Sorrento Valley</option>\
    <option value="91977,91978">Spring Valley</option>\
    <option value="92584,92585,92586,92587">Sun City</option>\
    <option value="91980">Tecate</option>\
    <option value="92589,92590,92591,92592,92593">Temecula</option>\
    <option value="92124">Tierrasanta</option>\
    <option value="92122">University City</option>\
    <option value="92082">Valley Center</option>\
    <option value="92081,92083,92084">Vista</option>\
    <option value="92066,92086">Warner Springs</option></select><input type="hidden" name="ss_city" id="ss_city">';

  } else {

		return '<select name="zipcode">\
		<option value="92656,92801,92802,92804,92805,92806,92807,92808,92821,90620,90621,92625,92626,92627,90630,92624,92629,92708,92831,92832,92833,92835,92840,92841,92843,92844,92845,92646,92647,92648,92649,92603,92604,92606,92612,92614,92618,92620,90631,90623,92651,92659,92677,92653,92610,92630,92691,92692,92660,92663,92780,92865,92866,92867,92868,92869,92870,92679,92688,90720,92672,92673,92675,92701,92703,92704,92705,92706,92707,90740,90743,90680,90742,92861,92683,92886,92887">All cities</option>\
		<option value="92656">Aliso Viejo</option>\
		<option value="92801,92802,92804,92805,92806">Anaheim</option>\
		<option value="92807,92808">Anaheim Hills</option>\
		<option value="92821">Brea</option>\
		<option value="90620,90621">Buena Park</option>\
		<option value="92625">Corona Del Mar</option>\
		<option value="92626,92627">Costa Mesa</option>\
	   <option value="90630">Cypress</option>\
		<option value="92624,92629">Dana Point</option>\
		<option value="92708">Fountain Valley</option>\
		<option value="92831,92832,92833,92835">Fullerton</option>\
		<option value="92840,92841,92843,92844,92845">Garden Grove</option>\
		<option value="92646,92647,92648,92649">Huntington Beach</option>\
		<option value="92603,92604,92606,92612,92614,92618,92620">Irvine</option>\
		<option value="90631">La Habra</option>\
		<option value="90623">La Palma</option>\
		<option value="92651,92659">Laguna Beach</option>\
		<option value="92677">Laguna Niguel</option>\
		<option value="92653">Laguna Hills</option>\
		<option value="92610,92630">Lake Forest</option>\
		<option value="92691,92692">Mission Viejo</option>\
		<option value="92660,92663">Newport Beach</option>\
		<option value="92780">North Tustin</option>\
		<option value="92865,92866,92867,92868,92869">Orange</option>\
		<option value="92870">Placentia</option>\
		<option value="92679,92688">Rancho Santa Margarita</option>\
		<option value="90720">Rossmoor</option>\
		<option value="92672,92673">San Clemente</option>\
		<option value="92675">San Juan Capistrano</option>\
		<option value="92701,92703,92704,92705,92706,92707">Santa Ana</option>\
		<option value="90740,90743">Seal Beach</option>\
		<option value="90680">Stanton</option>\
		<option value="90742">Sunset Beach</option>\
		<option value="92861">Villa Park</option>\
		<option value="92683">Westminster</option>\
		<option value="92886,92887">Yorba Linda</option></select>';

  }

}

function get_cities(frm, county){
	set_id(frm + '_zipcode', get_city_options(county))
}

function selectCounty( ddCounty, doRedirect )
{
	//county drop-down options value format: <countyID>_<0|1 -- redirectFlag>_<redirectUrl>
	if( ddCounty.selectedIndex == null || ddCounty.options == null ) 
		return;
	
	//get selected county-value
	var selCounty = new String( ddCounty.options[ ddCounty.selectedIndex ].value );
	
	var strAry = selCounty.split("_")
	if( strAry.length < 2 ) return;
	var countyID = parseInt(strAry[0]);
	var countyRedirect = parseInt(strAry[1]);
	if( countyRedirect == 0 || (doRedirect==0))
	{
		var str = "countyID: " + countyID + "\noptions";
		for( var i=0; i<ddCounty.options.length; i++ )
		{
			var optVal = new String( ddCounty.options[i].value );
			str += "\noptVal: " + ddCounty.options[i].value; 
			strAry = optVal.split("_");
			if( strAry.length > 1 )
			{
				var ddCity = document.quick_search[ "zipcode" + strAry[0] ];
				if( ddCity != null ) 
				{
				 str += "  found: " +	"zipcode" + strAry[0] ;
					if( parseInt(strAry[0]) == countyID )
						ddCity.style.display = "";		
					else
						ddCity.style.display = "none";					
						
				}			
				else
					str += "  not found: " +	"zipcode" + strAry[0];
			}	
			
		}
		//alert( str );
	}
	else if( strAry.length > 2)
		document.location.href = "http://www.ipayone.com/redirect.php?URL=" + strAry[2];	
}
function validateFrpt( theForm )
{
	//alert("validateFrpt");
	//return false;
	
	var isValid = true;
	var outStr = "";
	
	if( theForm.fname.value == "" )
	{
		isValid = false;
		outStr += "Please enter your First Name.\n";
	}
	if( theForm.lname.value == "" )
	{
		isValid = false;
		outStr += "Please enter your Last Name.\n";
	}
		
	if(!validate_email(theForm.email.value))
	{
		isValid = false;
		outStr += "Please enter valid Email Address.\n";
	}
	
	if(!validate_phone(theForm.phone.value) )
	{
		isValid = false;
		outStr += "Please enter valid Home Phone.\n";
	}
	var checked = false;
	if (theForm.frpt1 != null && theForm.frpt1.checked)
		checked = true;
	if(theForm.frpt2 != null && theForm.frpt2.checked)
		checked = true;
	if(theForm.frpt3 != null && theForm.frpt3.checked)
		checked = true;
	if(theForm.frpt4 != null && theForm.frpt4.checked)
		checked = true;
	if(theForm.frpt5 != null && theForm.frpt5.checked)
		checked = true;
	if(theForm.frpt6 != null && theForm.frpt6.checked)
		checked = true;
	if(theForm.frpt7 != null && theForm.frpt7.checked)
		checked = true;
	if(theForm.frpt8 != null && theForm.frpt8.checked)
		checked = true;
	if(theForm.frpt9 != null && theForm.frpt9.checked)
		checked = true;
	if(theForm.frpt10 != null && theForm.frpt10.checked)
		checked = true;
	
	if(!checked)
	{
		isValid = false;
		outStr += "You have not selected any reports.\n";
	}
	if( !isValid ) alert( outStr );
	
	return isValid;
	
}

//////////Script from HTML page starts here//////////////////
function validate_registrationSignup()
{
  var zip = get_value('reg_zipcode');
  var email = get_value('reg_email');
  var home_phone = get_value('reg_home_phone');

  var fname = get_value('reg_first_name');
  if(!fname || fname != Trim(fname))
  {
    alert('You must enter your first name');
    document.register.reg_first_name.focus();
    return false;
  }
  
  var lname = get_value('reg_last_name');
  if(!lname || lname != Trim(lname))
  {
    alert('You must enter your last name');
    document.register.reg_last_name.focus();
    return false;
  }
  
  if(!validate_phone(home_phone))
  {
    alert('Please enter valid home phone number, including 3-digit area code');
    document.register.reg_home_phone.focus();
    return false;
  }
  
  if(document.register.reg_work_phone.value != "" && !validate_phone(document.register.reg_work_phone.value))
  {
    alert('Please enter valid work phone number, including 3-digit area code');
    document.register.reg_work_phone.focus();
    return false;
  }
  
  if(document.register.reg_cell_phone.value != "" && !validate_phone(document.register.reg_cell_phone.value))
  {
    alert('Please enter valid cell phone number, including 3-digit area code');
    document.register.reg_cell_phone.focus();
    return false;
  }
  
  if(!validate_zipcode(zip))
  {
    alert('You must enter your 5-digit zip code');
    document.register.reg_zipcode.focus();
    return false;
  }
  
  
  if(!validate_email(email))
  {
    alert('Please enter a valid email address');
    document.register.reg_email.focus();
    return false;
  }
  
  var pwd = get_value('reg_password');
  
  if(!pwd || pwd != Trim(pwd))
  {
    alert('You must enter password');
    document.register.reg_password.focus();
    return false;
  }
   
  if(get_value('reg_password') != get_value('reg_vpassword'))
  {
    alert('Your passwords did not match');
    document.register.reg_vpassword.focus();
    return false;
  }

  return true;

}

function validate_registration()
{
  var zip = get_value('reg_zipcode');
  var email = get_value('reg_email');
  var home_phone = get_value('reg_home_phone');

  if(!get_value('reg_first_name'))
  {
    alert('You must enter your first name');
    return false;
  }
  
  if(!get_value('reg_last_name'))
  {
    alert('You must enter your last name');
    return false;
  }
  
  if(!validate_phone(home_phone))
  {
    alert('Please enter your home phone number, including 3-digit area code');
    return false;
  }
  
  if(!validate_zipcode(zip))
  {
    alert('You must enter your 5-digit zip code');
    return false;
  }


  
  if(!validate_email(email))
  {
    alert('Please enter a valid email address');
    return false;
  }

  return true;

}

function validate_pwd()
{
  if(get_value('mpo_pwd') != get_value('mpo_verpwd'))
  {
    alert('Your passwords did not match');
    return false;
  }
}
  
function newImage(arg) 
{
	if (document.images) 
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

var preloadFlag = false;
function changeImages() 
{
	if (document.images && (preloadFlag == true)) 
	{
		for (var i=0; i<changeImages.arguments.length; i+=2) 
		{
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}
function preloadImages() 
{
	if (document.images) 
	{
		cov2_11_over = newImage("{%templatePath%}/images/cov2_11-over.gif");
		cov2_12_over = newImage("{%templatePath%}/images/cov2_12-over.gif");
		cov2_13_over = newImage("{%templatePath%}/images/cov2_13-over.gif");
		cov2_14_over = newImage("{%templatePath%}/images/cov2_14-over.gif");
		cov2_15_over = newImage("{%templatePath%}/images/cov2_15-over.gif");
		cov2_16_over = newImage("{%templatePath%}/images/cov2_16-over.gif");
		cov2_17_over = newImage("{%templatePath%}/images/cov2_17-over.gif");
		preloadFlag = true;
	}
}
function getQueryVariable(variable) 
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) 
  {
    var pair = vars[i].split("=");
    if (pair[0] == variable) 
    {
      return pair[1];
    }
  } 
  alert('Query Variable ' + variable + ' not found');
}
function validate_form()
{
	
	if(!get_value('pwd') || !get_value('verPwd'))
	{
		alert('Enter "password" and "verify password"');
		return false;
	}
	
	if(get_value('pwd') != get_value('verPwd'))
	{
		alert('Your passwords do not match');
		return false;
	}
	else 
	{
		
		var urlID = document.getElementById('urlID');
		
		urlID.value = getQueryVariable('id');
		
	 	return true;
	 }
}
function FrontPage_Form1_Validator(theForm)
{

	/*if (theForm.ddAgents.options[0].selected)
	{
		alert("Please select your Broker.");
		theForm.ddAgents.focus();
		return (false);
	}*/
	
  if (theForm.qform_name.value == "")
  {
    alert("Please enter a value for the \"name\" field.");
    theForm.qform_name.focus();
    return (false);
  }
  if (theForm.qform_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.qform_email.focus();
    return (false);
  }
  if (theForm.qform_email.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"email\" field.");
    theForm.qform_email.focus();
    return (false);
  }
  
  var app_email = theForm.qform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.qform_email.focus();
		return false;
	}
	
	if(theForm.qform_phone1.value != "" && !validate_phone(theForm.qform_phone1.value))
	{
		alert('Please provide valid "Day Telephone"');
		theForm.qform_phone1.focus();
		return false;
	}
	
	if(theForm.qform_phone2.value != "" && !validate_phone(theForm.qform_phone2.value))
	{
		alert('Please provide valid "Evening Telephone"');
		theForm.qform_phone2.focus();
		return false;
	}
  
  return (true);
}
function Form_Validator(theForm)
{
  if (theForm.zform_fName.value == "" || theForm.zform_fName.value!=Trim(theForm.zform_fName.value) )
  {
    alert("Please enter a value for the \"first name\" field.");
    theForm.zform_fName.focus();
    return (false);
  }
 
  if (theForm.zform_lName.value == "" || theForm.zform_lName.value!=Trim(theForm.zform_lName.value))
  {
    alert("Please enter a value for the \"last name\" field.");
    theForm.zform_lName.focus();
    return (false);
  }
  
  if(!validate_phone(theForm.zform_phone.value))
	{
		alert('Please enter your phone number');
		theForm.zform_phone.focus();
		return false;
	}
	
  if (theForm.zform_email.value == "")
  {
    alert("Please enter a value for the \"email address\" field.");
    theForm.zform_email.focus();
    return (false);
  }
  
   var app_email = theForm.zform_email.value;
   
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.zform_email.focus();
		return false;
	}
	
  if (theForm.zform_p_addr.value == "")
  {
    alert("Please enter a value for the \"property address\" field.");
    theForm.zform_p_addr.focus();
    return (false);
  }
  
  if (theForm.zform_p_city.value == "")
  {
    alert("Please enter a value for the \"property city\" field.");
    theForm.zform_p_city.focus();
    return (false);
  }
  
  if (theForm.zform_p_state.value == "")
  {
    alert("Please enter a value for the \"property state\" field.");
    theForm.zform_p_state.focus();
    return (false);
  }

	if(!validate_zipcode(theForm.zform_zip.value))
	{
		alert('Please provide valid zip code');
		theForm.zform_zip.focus();
		return false;
	}
	
	if(!IsNumeric(theForm.zform_p_footage.value))
	{
		alert('Please provide numeric value for "Approx. Square Footage"');
		theForm.zform_p_footage.focus();
		return false;
	}
	
	if(theForm.zform_p_sDate.value == "Choose One")
	{
		alert('Please provide input for "Selling Date"');
		theForm.zform_p_sDate.focus();
		return false;
	}
  return (true);
}
function homeSearch_Validator()
{	
		
	if(!document.bform1.bform_name.value)
	{
		alert('Please enter your Name');
		document.bform1.bform_name.focus();
		return false;
	}
	
	if(!document.bform1.bform_email.value)
	{
		alert('Please enter your email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_email = document.bform1.bform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		document.bform1.bform_email.focus();
		return false;
	}
		
	if(!validate_phone(document.bform1.bform_phone.value))
	{
		alert('Please enter valid phone number');
		document.bform1.bform_phone.focus();
		return false;
	}
	
}
	
	
function search_Validator()
{	
		
	if(!document.bform1.bform_name.value)
	{
		alert('Please enter your Name');
		document.bform1.bform_name.focus();
		return false;
	}
	
	if(!document.bform1.bform_email.value)
	{
		alert('Please enter your email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_email = document.bform1.bform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_phone = document.bform1.bform_phone.value;
	
	if(!validate_phone(app_phone))
	{
		alert('Please enter your phone number');
		document.bform1.bform_phone.focus();
		return false;
	}
	
	if(document.bform1.bform_own.options[0].selected)
	{
		alert('Please answer for "Do you currently own a home?"');
		document.bform1.bform_own.focus();
		return false;
	}
	
	if(document.bform1.bform_own.value == "1" && document.bform1.bform_selling.options[0].selected)
	{
		alert('Please answer for "Are you selling your current home?"');
		document.bform1.bform_selling.focus();
		return false;
	}
	
	if( document.bform1.bform_move.options[0].selected)
	{
		alert('Please answer for "When do you plan to buy or move?"');
		document.bform1.bform_move.focus();
		return false;
	}
		
	if(toNumber(document.bform1.bform_priceHigh.value) < toNumber(document.bform1.bform_priceLow.value))
	{
		alert('In Price range, "Low price" must be less than "High price"');
		document.bform1.bform_priceLow.focus();
		return false;		
	}
	
	return true;
}

function login_validate()
{
	
	if(!document.frmLogin.mpo_username.value)
	{
		alert('Please enter user name');
		document.frmLogin.mpo_username.focus();
		return false;
	}
	
	if(!document.frmLogin.mpo_password.value)
	{
		alert('Please enter password');
		document.frmLogin.mpo_password.focus();
		return false;
	}
	return true;
	
}

function toNumber(stringValue)
{
	var numVal = stringValue * 1;
	return numVal;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var Char;
   var DecimalOccured = false;
   var IsNumber = true;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
            
      if(Char=='.')
      {
		if(DecimalOccured)
		{
			IsNumber = false;
		}
		else
		{
			DecimalOccured = true;
		}
      }
      
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;
   
}

function ReplaceSpecial(sText)
{
   
   alert(sText);
   var SpecialChars = "'\"";
   var Char;
   var resultString = "";

 
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i);            
            
      if (SpecialChars.indexOf(Char) == -1) 
      {
         resultString = resultString + Char;
      }
      else
      {
		resultString = resultString + "\\" + Char;
      }
   }
   return resultString;
   alert(resultString);
}

function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return"";
	}
	
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	
	if(TRIM_VALUE=="")
	{
		return "";
	}
	else
	{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	
	if(v_length < 0)
	{
		return"";
	}

	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space){}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	
	return strTemp;

} //End Function

function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space){}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	
	return strTemp;
} //End Function

function validate_freeReport( theForm )
{
	//alert("validateFrpt");
	//return false;
	
	var isValid = true;
	var outStr = "";
	
	if( theForm.fname.value == "" )
	{
		isValid = false;
		outStr += "Please enter your First Name.\n";
	}
	if( theForm.lname.value == "" )
	{
		isValid = false;
		outStr += "Please enter your Last Name.\n";
	}
		
	if(!validate_email(theForm.email.value))
	{
		isValid = false;
		outStr += "Please enter valid Email Address.\n";
	}
	
	if(!validate_phone(theForm.phone.value) )
	{
		isValid = false;
		outStr += "Please enter valid Home Phone.\n";
	}
	var checked = false;
	if (theForm.frpt1 != null && theForm.frpt1.checked)
		checked = true;
	if(theForm.frpt2 != null && theForm.frpt2.checked)
		checked = true;
	if(theForm.frpt3 != null && theForm.frpt3.checked)
		checked = true;
	if(theForm.frpt4 != null && theForm.frpt4.checked)
		checked = true;
	if(theForm.frpt5 != null && theForm.frpt5.checked)
		checked = true;
	if(theForm.frpt6 != null && theForm.frpt6.checked)
		checked = true;
	if(theForm.frpt7 != null && theForm.frpt7.checked)
		checked = true;
	if(theForm.frpt8 != null && theForm.frpt8.checked)
		checked = true;
	if(theForm.frpt9 != null && theForm.frpt9.checked)
		checked = true;
	if(theForm.frpt10 != null && theForm.frpt10.checked)
		checked = true;
	
	if(!checked)
	{
		isValid = false;
		outStr += "You have not selected any reports.\n";
	}
	if( !isValid ) alert( outStr );
	
	return isValid;
	
}

function validate_membersSignup()
{
  var zip = get_value('reg_zipcode');
  var email = get_value('reg_email');
  var home_phone = get_value('reg_home_phone');

  var fname = get_value('reg_first_name');
  if(!fname || fname != Trim(fname))
  {
    alert('You must enter your first name');
    document.register.reg_first_name.focus();
    return false;
  }
  
  var lname = get_value('reg_last_name');
  if(!lname || lname != Trim(lname))
  {
    alert('You must enter your last name');
    document.register.reg_last_name.focus();
    return false;
  }
  
  if(!validate_phone(home_phone))
  {
    alert('Please enter valid home phone number, including 3-digit area code');
    document.register.reg_home_phone.focus();
    return false;
  }
  
  if(document.register.reg_work_phone.value != "" && !validate_phone(document.register.reg_work_phone.value))
  {
    alert('Please enter valid work phone number, including 3-digit area code');
    document.register.reg_work_phone.focus();
    return false;
  }
  
  if(document.register.reg_cell_phone.value != "" && !validate_phone(document.register.reg_cell_phone.value))
  {
    alert('Please enter valid cell phone number, including 3-digit area code');
    document.register.reg_cell_phone.focus();
    return false;
  }
  
  if(!validate_zipcode(zip))
  {
    alert('You must enter your 5-digit zip code');
    document.register.reg_zipcode.focus();
    return false;
  }
  
  
  if(!validate_email(email))
  {
    alert('Please enter a valid email address');
    document.register.reg_email.focus();
    return false;
  }
  
  var pwd = get_value('reg_password');
  
  if(!pwd || pwd != Trim(pwd))
  {
    alert('You must enter password');
    document.register.reg_password.focus();
    return false;
  }
   
  if(get_value('reg_password') != get_value('reg_vpassword'))
  {
    alert('Your passwords did not match');
    document.register.reg_vpassword.focus();
    return false;
  }

  return true;
}

function validate_membersRegistration()
{
  var zip = get_value('reg_zipcode');
  var email = get_value('reg_email');
  var home_phone = get_value('reg_home_phone');

  if(!get_value('reg_first_name'))
  {
    alert('You must enter your first name');
    return false;
  }
  
  if(!get_value('reg_last_name'))
  {
    alert('You must enter your last name');
    return false;
  }
  
  if(!validate_phone(home_phone))
  {
    alert('Please enter your home phone number, including 3-digit area code');
    return false;
  }
  
  if(!validate_zipcode(zip))
  {
    alert('You must enter your 5-digit zip code');
    return false;
  }


  
  if(!validate_email(email))
  {
    alert('Please enter a valid email address');
    return false;
  }

  return true;

}

function validate_pwd()
{
  if(get_value('mpo_pwd') != get_value('mpo_verpwd'))
  {
    alert('Your passwords did not match');
    return false;
  }
}
  
function getQueryVariable(variable) 
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) 
  {
    var pair = vars[i].split("=");
    if (pair[0] == variable) 
    {
      return pair[1];
    }
  } 
  alert('Query Variable ' + variable + ' not found');
}


function validate_ChangePwd()
{
	if(!get_value('pwd') || !get_value('verPwd'))
	{
		alert('Enter "password" and "verify password"');
		return false;
	}
	
	if(get_value('pwd') != get_value('verPwd'))
	{
		alert('Your passwords do not match');
		return false;
	}
	else 
	{		
		var urlID = document.getElementById('urlID');	
		urlID.value = getQueryVariable('id');		
	 	return true;
	 }
}
function validate_nlSignup(theForm)
{	
  if (theForm.nlSubscriber_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.nlSubscriber_email.focus();
    return (false);
  }
  if (theForm.nlSubscriber_email.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"email\" field.");
    theForm.nlSubscriber_email.focus();
    return (false);
  }
  
  var app_email = theForm.nlSubscriber_email.value;
	if(!validate_email(nlSubscriber_email))
	{
		alert('Please provide valid email address');
		theForm.nlSubscriber_email.focus();
		return false;
	}  
  return (true);
}
function validate_QuestionForm(theForm)
{	
  if (theForm.qform_name.value == "")
  {
    alert("Please enter a value for the \"name\" field.");
    theForm.qform_name.focus();
    return (false);
  }
  if (theForm.qform_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.qform_email.focus();
    return (false);
  }
  if (theForm.qform_email.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"email\" field.");
    theForm.qform_email.focus();
    return (false);
  }
  
  var app_email = theForm.qform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.qform_email.focus();
		return false;
	}
	
	if(theForm.qform_phone1.value != "" && !validate_phone(theForm.qform_phone1.value))
	{
		alert('Please provide valid "Phone"');
		theForm.qform_phone1.focus();
		return false;
	}
	
	if(theForm.qform_phone2.value != "" && !validate_phone(theForm.qform_phone2.value))
	{
		alert('Please provide valid "Evening Telephone"');
		theForm.qform_phone2.focus();
		return false;
	}
  
  return (true);
}

function validate_rateFinder(theForm)
{	
  if (theForm.rate_fName.value == "")
  {
    alert("Please enter a value for the First Name field.");
    theForm.rate_fName.focus();
    return (false);
  }
   if (theForm.rate_lName.value == "")
  {
    alert("Please enter a value for the Last Name field.");
    theForm.rate_lName.focus();
    return (false);
  }
  if (theForm.rate_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.rate_email.focus();
    return (false);
  }
  if (theForm.rate_email.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"email\" field.");
    theForm.rate_email.focus();
    return (false);
  }
  
  var app_email = theForm.rate_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.rate_email.focus();
		return false;
	}
	
	if(theForm.rate_phone1.value != "" && !validate_phone(theForm.rate_phone1.value))
	{
		alert('Please provide valid "Home Telephone"');
		theForm.rate_phone1.focus();
		return false;
	}
	 
  return (true);
}
function validate_QuickApp(theForm)
{
  if (theForm.qApp_fName.value == "")
  {
    alert("Please enter a value for the \"first name\" field.");
    theForm.qApp_fName.focus();
    return (false);
  }
  if (theForm.qApp_lName.value == "")
  {
    alert("Please enter a value for the \"last name\" field.");
    theForm.qApp_lName.focus();
    return (false);
  }
  if (theForm.qApp_lName.value == theForm.qApp_fName.value)
  {
    alert("Please enter a real values for the \"first name\" and \"last name\" fields.");
    theForm.qApp_fName.focus();
    return (false);
  }
  if (theForm.qApp_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.qApp_email.focus();
    return (false);
  }

  
  var app_email = theForm.qApp_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.qApp_email.focus();
		return false;
	}
	
	if(theForm.qApp_phone.value == "" || !validate_phone(theForm.qApp_phone.value))
	{
		alert('Please provide valid "Phone"');
		theForm.qApp_phone.focus();
		return false;
	}
	
	if(theForm.qApp_lAmt.value == "" || !IsNumeric(theForm.qApp_lAmt.value))
	{
		alert('Please provide valid "Loan Amount"');
		theForm.qApp_lAmt.focus();
		return false;
	}
  	if(theForm.qApp_expense.value == "" || !IsNumeric(theForm.qApp_expense.value))
	{
		alert('Please provide valid "Total Monthly Expenses"');
		theForm.qApp_expense.focus();
		return false;
	}
		if(theForm.qApp_income.value == "" || !IsNumeric(theForm.qApp_income.value))
	{
		alert('Please provide valid "Total Monthly Income"');
		theForm.qApp_income.focus();
		return false;
	}
  return (true);
}
function validate_ZipForm(theForm)
{
  if (theForm.zform_fName.value == "" || theForm.zform_fName.value!=Trim(theForm.zform_fName.value) )
  {
    alert("Please enter a value for the \"first name\" field.");
    theForm.zform_fName.focus();
    return (false);
  }
 
  if (theForm.zform_lName.value == "" || theForm.zform_lName.value!=Trim(theForm.zform_lName.value))
  {
    alert("Please enter a value for the \"last name\" field.");
    theForm.zform_lName.focus();
    return (false);
  }
  
  if(!validate_phone(theForm.zform_phone.value))
	{
		alert('Please enter your phone number');
		theForm.zform_phone.focus();
		return false;
	}
	
  if (theForm.zform_email.value == "")
  {
    alert("Please enter a value for the \"email address\" field.");
    theForm.zform_email.focus();
    return (false);
  }
  
   var app_email = theForm.zform_email.value;
   
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.zform_email.focus();
		return false;
	}
	
  if (theForm.zform_p_addr.value == "")
  {
    alert("Please enter a value for the \"property address\" field.");
    theForm.zform_p_addr.focus();
    return (false);
  }
  
  if (theForm.zform_p_city.value == "")
  {
    alert("Please enter a value for the \"property city\" field.");
    theForm.zform_p_city.focus();
    return (false);
  }
  
  if (theForm.zform_p_state.value == "")
  {
    alert("Please enter a value for the \"property state\" field.");
    theForm.zform_p_state.focus();
    return (false);
  }

	if(!validate_zipcode(theForm.zform_zip.value))
	{
		alert('Please provide valid zip code');
		theForm.zform_zip.focus();
		return false;
	}
	
	if(!IsNumeric(theForm.zform_p_footage.value))
	{
		alert('Please provide numeric value for "Approx. Square Footage"');
		theForm.zform_p_footage.focus();
		return false;
	}
	
	if(theForm.zform_p_sDate.value == "Choose One")
	{
		alert('Please provide input for "Selling Date"');
		theForm.zform_p_sDate.focus();
		return false;
	}
  return (true);
}


function validate_HomeSearch()
{	
		
	if(!document.bform1.bform_name.value)
	{
		alert('Please enter your Name');
		document.bform1.bform_name.focus();
		return false;
	}
	
	if(!document.bform1.bform_email.value)
	{
		alert('Please enter your email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_email = document.bform1.bform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		document.bform1.bform_email.focus();
		return false;
	}
		
	if(!validate_phone(document.bform1.bform_phone.value))
	{
		alert('Please enter valid phone number');
		document.bform1.bform_phone.focus();
		return false;
	}
	
}
	
	
function validate_HomeSearchExtended()
{	
		
	if(!document.bform1.bform_name.value)
	{
		alert('Please enter your Name');
		document.bform1.bform_name.focus();
		return false;
	}
	
	if(!document.bform1.bform_email.value)
	{
		alert('Please enter your email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_email = document.bform1.bform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_phone = document.bform1.bform_phone.value;
	
	if(!validate_phone(app_phone))
	{
		alert('Please enter your phone number');
		document.bform1.bform_phone.focus();
		return false;
	}
	
	if(document.bform1.bform_own.options[0].selected)
	{
		alert('Please answer for "Do you currently own a home?"');
		document.bform1.bform_own.focus();
		return false;
	}
	
	if(document.bform1.bform_own.value == "1" && document.bform1.bform_selling.options[0].selected)
	{
		alert('Please answer for "Are you selling your current home?"');
		document.bform1.bform_selling.focus();
		return false;
	}
	
	if( document.bform1.bform_move.options[0].selected)
	{
		alert('Please answer for "When do you plan to buy or move?"');
		document.bform1.bform_move.focus();
		return false;
	}
		
	if(toNumber(document.bform1.bform_priceHigh.value) < toNumber(document.bform1.bform_priceLow.value))
	{
		alert('In Price range, "Low price" must be less than "High price"');
		document.bform1.bform_priceLow.focus();
		return false;		
	}
	
	return true;
}

function validate_Login()
{
	
	if(!document.frmLogin.mpo_username.value)
	{
		alert('Please enter user name');
		document.frmLogin.mpo_username.focus();
		return false;
	}
	
	if(!document.frmLogin.mpo_password.value)
	{
		alert('Please enter password');
		document.frmLogin.mpo_password.focus();
		return false;
	}
	return true;
	
}

function validate_Appt(theForm){
	var app_name = theForm.appointment.name.value;
	var app_phone = theForm.appointment.phone.value;
	var app_email = theForm.appointment.email.value;
	if(!app_name || app_name == 'My Name'){
		alert('Please enter your name');
		return false;
		}
	if(!validate_phone(app_phone) || app_phone == '619-555-1234'){
		alert('Please enter your phone number');
		return false;
		}
	if(!validate_email(app_email) || app_email == 'name@example.org'){
		alert('Please enter your email address');
		return false
		}
	return true;
}

//////////////////////////////
//////HELPER FUNCTIONS//////
////////////////////////////
function get_value(id){
  return d.getElementById(id).value;
}
function validate_email(email) {
  var e_rx = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
  var regex = new RegExp(e_rx);
  return regex.test(email);
}

function validate_zipcode(tZip){
  tZip = tZip.replace(num_only_rx, '');
  return (tZip.length == 5);  
}

function validate_phone(pnumber){
  pnumber = pnumber.replace(num_only_rx, '');
  return (pnumber.length == 10);  
}

function toNumber(stringValue)
{
	var numVal = stringValue * 1;
	return numVal;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var Char;
   var DecimalOccured = false;
   var IsNumber = true;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
            
      if(Char=='.')
      {
		if(DecimalOccured)
		{
			IsNumber = false;
		}
		else
		{
			DecimalOccured = true;
		}
      }
      
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;
   
}

function ReplaceSpecial(sText)
{
   
   alert(sText);
   var SpecialChars = "'\"";
   var Char;
   var resultString = "";

 
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i);            
            
      if (SpecialChars.indexOf(Char) == -1) 
      {
         resultString = resultString + Char;
      }
      else
      {
		resultString = resultString + "\\" + Char;
      }
   }
   return resultString;
   alert(resultString);
}

function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return"";
	}
	
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	
	if(TRIM_VALUE=="")
	{
		return "";
	}
	else
	{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	
	if(v_length < 0)
	{
		return"";
	}

	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space){}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	
	return strTemp;

} //End Function

function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space){}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	
	return strTemp;
} //End Function

/** REDIRECT VALIDATION FUNCTIONS **/
// All of the old templates call validation routines that no longer exist.  They have been replaced by the new functions
// We want to point the old function names to the new function names if the old functions are defined on the page.
if(typeof(qkaValidate) != 'function')
{
	qkaValidate = validate_QuickApp;
}

if(typeof(rtfValidate) != 'function')
{
	rtfValidate = validate_rateFinder;
}

if(typeof(qfmValidate) != 'function')
{
	qfmValidate = validate_QuestionForm;
}

