function open_new_window(url) {new_window = window.open(url,'content_window','toolbar=0,menubar=0,resizable=0,dependent=0,status=0,scrollbars=1,width=600,height=600,left=25,top=25');}
function open_energysmart_window(url) {new_window = window.open(url,'energysmart_window','toolbar=0,menubar=0,resizable=0,dependent=0,status=0,scrollbars=0,width=662,height=437,left=25,top=25');}
function open_large_image(url) {new_window = window.open(url,'large_image_window','toolbar=0,menubar=0,resizable=0,dependent=0,status=0,scrollbars=0,width=700,height=350,left=25,top=25');}
function rollover(imgid, newsrc) {var img = document.getElementById(imgid); img.src=newsrc;}
function open_emc_color_window(url) {new_window = window.open(url,'emc_color_window','toolbar=0,menubar=0,resizable=1,dependent=0,status=0,scrollbars=1,width=750,height=880,left=25,top=25');}
function open_emc_template_window(url,w,h) {new_window = window.open(url,'emc_template_window','toolbar=0,menubar=0,resizable=1,dependent=0,status=0,scrollbars=1,width='+w+',height='+h+',left=25,top=25');}
function confirmDelete() {
  var agree = confirm("Are you sure you wish to delete this entry?");
  if(agree) { return true; } else { return false; }
}
function confirmEdit() {
  var agree = confirm("Are you sure you wish to edit this entry?");
  if(agree) { return true; } else { return false; }
}
function isEmpty(s) { return ((s == null) || (s.length == 0)); }
function isNotEmpty(s) { return ((s != null) || (s.length > 0)); }
function isNumeric(s)
{
  var strValidChars = "0123456789.-";
  var c;
  var r = true;
  if(s.length == 0) return false;
  
  for(i = 0; i < s.length && r == true; i++) {
    c = s.charAt(i);
    if(strValidChars.indexOf(c) == -1) { r = false; }
  }
  return r;
}

function changeClass(id, newClass) {
  //alert("Changing class of "+id+" to "+newClass);
  object = document.getElementById(id);
  object.className = newClass;
}


// VALIDATION

function valEmail(field) {
  var str = field.value;
  if (str.length==0) { field.className=""; return true; }
  if (emailIsValid(str)) { field.className=""; return true; }
  else { field.className="error"; return false; }
}
function emailIsValid(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
    //alert("Invalid E-mail ID")
    return false
  }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
    //alert("Invalid E-mail ID")
    return false
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
     //alert("Invalid E-mail ID")
     return false
  }
   if (str.indexOf(at,(lat+1))!=-1){
     //alert("Invalid E-mail ID")
     return false
   }
   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
     //alert("Invalid E-mail ID")
     return false
   }
   if (str.indexOf(dot,(lat+2))==-1){
     //alert("Invalid E-mail ID")
     return false
   }
   if (str.indexOf(" ")!=-1){
     //alert("Invalid E-mail ID")
     return false
   }
  return true         
}
function valZip(field) {
  var str = field.value;
  if (str.length==0) {return true;}
  str = str.replace(new RegExp("-","g"),"");
  if (str.length==5) { field.value=str; field.className=""; return true; }
  else { field.className="error"; return false; }
}
function zipIsValid(str) {
  var i;
  var c;
  if (str.length != 5) {return false;}
  for (i = 0; i < str.length; i++) {
    c = str.charAt(i);
    if (((c < "0") || (c > "9"))) return false;
  }
  return true;
}
function valPhone(field) {
  var str = field.value;
  if (str.length==0) {return true;}
  str = str.replace(new RegExp(" ","g"),"");
  str = str.replace(/\(/g,"");
  str = str.replace(/\)/g,"");
  str = str.replace(new RegExp("-","g"),"");
  if (str.length==10) { str=str.substr(0,3)+"-"+str.substr(3,3)+"-"+str.substr(6,4); field.value=str; field.className=""; return true; }
  else { field.className="error"; return false; }
}
function phoneIsValid(str) {
  var i;
  var c;
  str = str.replace(new RegExp("-","g"),"");
  if (str.length != 10) {return false;}
  for (i = 0; i < str.length; i++) {
    c = str.charAt(i);
    if (((c < "0") || (c > "9"))) return false;
  }
  return true;
}
function valTimestamp(field) {
  var str = field.value;
  var yyyy = "";
  var mm = "";
  var dd = "";
  if (str.length == 8) {
    yyyy = str.substr(0,4);
    mm = str.substr(4,2);
    dd = str.substr(6,2);
  }
  else if (str.length == 10) {
    if ((str.charAt(2) < "0" || str.charAt(2) > "9") && (str.charAt(5) < "0" || str.charAt(5) > "9")) {
      mm = str.substr(0,2);
      dd = str.substr(3,2);
      yyyy = str.substr(6,4);
    } else if ((str.charAt(4) < "0" || str.charAt(4) > "9") && (str.charAt(7) < "0" || str.charAt(7) > "9")) {
      yyyy = str.substr(0,4);
      mm = str.substr(5,2);
      dd = str.substr(8,2);
    } else {field.className = "error"; return false;}
  }
  else {field.className = "error"; return false;}
  if (isNumeric(yyyy) && isNumeric(mm) && isNumeric(dd)) {
    field.value = mm+"-"+dd+"-"+yyyy;
    field.className = "";
    return true;
  } else {field.className = "error"; return false;}
}
function timestampIsValid(str) {
  if (isNumeric(str.substr(0,2)) && isNumeric(str.substr(3,2)) && isNumeric(str.substr(6,4))) {return true;} else {return false;}
}
function gotoSelected(selname) {
  var sel = document.getElementById(selname);
  window.location.href = sel.options[sel.selectedIndex].value;
}

var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = '';
var fullVersion  = 0; 
var majorVersion = 0;

// In Internet Explorer, the true version is after "MSIE" in userAgent
if((verOffset=nAgt.indexOf("MSIE"))!=-1) {
  browserName  = "IE";
  fullVersion  = parseFloat(nAgt.substring(verOffset+5));
  majorVersion = parseInt(''+fullVersion);
}
// In Opera, the true version is after "Opera"
else if((verOffset=nAgt.indexOf("Opera"))!=-1) {
  browserName  = "Opera";
  fullVersion  = parseFloat(nAgt.substring(verOffset+6));
  majorVersion = parseInt(''+fullVersion);
}
// In most other browsers, "name/version" is at the end of userAgent
else if((nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/'))) {
  browserName  = nAgt.substring(nameOffset,verOffset);
  fullVersion  = parseFloat(nAgt.substring(verOffset+1));
  if(!isNaN(fullVersion)) { majorVersion = parseInt(''+fullVersion); }
  else { fullVersion  = 0; majorVersion = 0; }
}
// Finally, if no name and/or no version detected from userAgent...
if(browserName.toLowerCase()==browserName.toUpperCase() || fullVersion==0 || majorVersion == 0) {
  browserName  = navigator.appName;
  fullVersion  = parseFloat(nVer);
  majorVersion = parseInt(nVer);
}

PositionX = 100;
PositionY = 100;
defaultWidth  = 500;
defaultHeight = 500;
var AutoClose = true;
if(browserName == "IE" || browserName == "Opera") {
  windowOptions = 'scrollbars=no,toolbar=no,directories=no,resizable=yes,location=no,status=no,width=150,height=100,left='+PositionX+',top='+PositionY;
}
else {
  windowOptions = 'scrollbars=no,toolbar=no,directories=no,resizable=yes,location=no,status=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
}
function popImage(imageURL,imageTitle)
{
  imgWin=window.open('about:blank','',windowOptions);
  with(imgWin.document) {
    writeln('<html><head><title>Loading...</title><style>body{background-color:#FFFFFF;margin:0px;}</style>');
    writeln('<script>');
    writeln('  function reSizeToImage() {');
    if(browserName == "IE" || browserName == "Opera") {
      writeln('    window.resizeTo(300,300);');
      writeln('    width=300-(document.body.clientWidth-document.images[0].width);');
      writeln('    height=300-(document.body.clientHeight-document.images[0].height);');
      writeln('    window.resizeTo(width,height);');
    }
    else if(browserName == "Safari") {
      writeln('    window.innerWidth=document.images["theimage"].width;');
      writeln('    window.innerHeight=document.images["theimage"].height;');
    }
    else {
      writeln('    window.innerWidth=document.images["theimage"].width;');
      writeln('    window.innerHeight=document.images["theimage"].height;');
    }
    writeln('  }');
    writeln('  function doTitle() { document.title="'+imageTitle+'"; }');
    writeln('</script>');
    if(!AutoClose) { writeln('</head><body bgcolor="#FFFFFF" scroll="no" onload="reSizeToImage();doTitle();self.focus()">') }
    else { writeln('</head><body bgcolor="#FFFFFF" scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">'); }
    writeln('<img id="theimage" name="theimage" src='+imageURL+' style="display:block"></body></html>');
    close();		
  }
}

function getAjax(target, url, pars)
{
  var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onSuccess: function(){}, onFailure: function(){ alert('There was an error processing. Please change your selection.');}, onComplete: function(){ new Effect.Highlight(target);}, asynchronous: true});
}
function getAjaxNoHighlight(target, url, pars)
{
  var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onSuccess: function(){}, onFailure: function(){ alert('There was an error processing. Please change your selection.');}, onComplete: function(){}, asynchronous: true});
}
function getAjaxReset(target, url, pars)
{
  var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onSuccess: function(){}, onFailure: function(){ alert('There was an error processing. Please change your selection.');}, onComplete: function(){ new Effect.Appear(target);}, asynchronous: true});
}

function checkEmail()
{
  var e = $('user_email').value;
  
  getAjax('email_check', '/register/data/', 'e='+e);
}

function getPrices(resetSqFt)
{
  var cr = $('city_region').value;
  
  getAjaxReset('max_price', '/search/data/','t=prr');
  getAjaxReset('max_bedrooms', '/search/data/','t=brr');
  if(resetSqFt) {
    getAjaxReset('max_sqft', '/search/data/','t=sfr');
  }
  
  getAjax('max_price', '/search/data/', 't=pr&cr='+cr);
}

function getBedrooms(resetSqFt)
{
  var cr = $('city_region').value;
  var mp = $('max_price').value;
  
  getAjaxReset('max_bedrooms', '/search/data/','t=brr');
  if(resetSqFt) {
    getAjaxReset('max_sqft', '/search/data/','t=sfr');
  }
  
  getAjax('max_bedrooms', '/search/data/','t=br&cr='+cr+'&mp='+mp);
}

function getSqft()
{
  var cr = $('city_region').value;
  var mp = $('max_price').value;
  var mb = $('max_bedrooms').value;
  
  getAjaxReset('max_sqft', '/search/data/','t=sfr');
  getAjax('max_sqft', '/search/data/','t=sf&cr='+cr+'&mp='+mp+'&mb='+mb);
}

function getCommNeigh()
{
  var cr = $('city_region2').value;
  
  //if(cr.length > 0) {
    getAjax('comm_neigh', '/search/data/', 't=cn&cr='+cr);
  //}
}

function getCommNeigh2()
{
  var cr = $('city_region2').value;
  
  //if(cr.length > 0) {
    getAjax('comm_neigh2', '/search/data/', 't=cn&cr='+cr);
  //}
}

function getAdvForm()
{
  var cr = $('city_region').value;
  
  getAjaxReset('min_price', '/search/data/','t=mnprr');
  getAjaxReset('max_price', '/search/data/','t=mxprr');
  getAjaxReset('min_bedrooms', '/search/data/','t=mnbrr');
  getAjaxReset('max_bedrooms', '/search/data/','t=mxbrr');
  getAjaxReset('max_sqft', '/search/data/','t=sfr');
  
  getAjax('min_price', '/search/data/', 't=mnpr&cr='+cr);
  getAjax('max_price', '/search/data/', 't=mxpr&cr='+cr);
  getAjax('min_bedrooms', '/search/data/', 't=mnbr&cr='+cr);
  getAjax('max_bedrooms', '/search/data/', 't=mxbr&cr='+cr);
  getAjax('max_sqft', '/search/data/', 't=sfa&cr='+cr);
  getAjax('level', '/search/data/', 't=lv&cr='+cr);
  getAjax('garage', '/search/data/', 't=gr&cr='+cr);
  
  getAjaxNoHighlight('am_master', '/search/data/', 't=am&aa=am_master&ti=7&cr='+cr);
  getAjaxNoHighlight('am_gated', '/search/data/', 't=am&aa=am_gated&ti=8&cr='+cr);
  getAjaxNoHighlight('am_enerysmart', '/search/data/', 't=am&aa=am_enerysmart&ti=9&cr='+cr);
  
  getAjaxNoHighlight('am_golf', '/search/data/', 't=am&aa=am_golf&ti=10&cr='+cr);
  getAjaxNoHighlight('am_park', '/search/data/', 't=am&aa=am_park&ti=11&cr='+cr);
  getAjaxNoHighlight('am_rec', '/search/data/', 't=am&aa=am_rec&ti=12&cr='+cr);
  
  getAjaxNoHighlight('am_pool', '/search/data/', 't=am&aa=am_pool&ti=13&cr='+cr);
  getAjaxNoHighlight('am_playground', '/search/data/', 't=am&aa=am_playground&ti=14&cr='+cr);
  getAjaxNoHighlight('am_award', '/search/data/', 't=am&aa=am_award&ti=15&cr='+cr);
  
  getAjaxNoHighlight('st_nowsell', '/search/data/', 't=st&sa=st_nowsell&ti=16&cr='+cr);
  getAjaxNoHighlight('st_comingsoon', '/search/data/', 't=st&sa=st_comingsoon&ti=17&cr='+cr);
  getAjaxNoHighlight('st_finalsale', '/search/data/', 't=st&sa=st_finalsale&ti=18&cr='+cr);
	getAjaxNoHighlight('st_newreduced', '/search/data/', 't=st&sa=st_newreduced&ti=19&cr='+cr);
}

function validateAdminLogin()
{
  var error = false;
  var error_msg = "Please fix the following errors:\n";
  var email = document.form_login.email.value;
  email = email.replace(/^\s*|\s*$/g,"");
  var password = document.form_login.password.value;
  password = password.replace(/^\s*|\s*$/g,"");
  
  if(isEmpty(email)) {
    error = true;
    error_msg += " - Email is required\n";
    changeClass("login_email", "error");
  } else {
    changeClass("login_email", "");
  }
  if(isEmpty(password)) {
    error = true;
    error_msg += " - Password is required\n";
    changeClass("login_password", "error");
  } else {
    changeClass("login_password", "");
  }
  
  if (error==true) {alert(error_msg);return false;}
  return true;
}

function focusSelect(id)
{
  document.getElementById(id).focus();
  document.getElementById(id).select();
}

function clearText(id, textFilter) {
  var textfld = document.getElementById(id);
  if (textfld.value.indexOf(textFilter) != -1) {
    textfld.value = "";
  }
}

function checkText(id, textFilter) {
  var textfld = document.getElementById(id);
  if (textfld.value == '') {
    textfld.value = textFilter;
  }
}

function validateQuickJump()
{
  var error = false;
  var error_msg = "Please fix the following errors:\n";
  var comm_neigh = document.quick_jump.comm_neigh.value;
  comm_neigh = comm_neigh.replace(/^\s*|\s*$/g,"");
  
  if(isEmpty(comm_neigh)) {
    error = true;
    error_msg += " - Neighborhood Quick Jump is required\n";
    document.quick_jump.comm_neigh.value = 'Neighborhood Quick Jump';
    focusSelect("comm_neigh")
  } else {
  }
  
  if (error==true) {alert(error_msg);return false;}
  return true;
}
