//lre_seosearch.js
//
  var seoForm = document.forms.seoSearch;
  
  function initSeoFields() {
    seoForm.city.disabled = true;
    seoForm.county.disabled = true;
    seoForm.city.value = "";
    seoForm.county.value = "";
    seoForm.city.style.background = "#e2e2e2";
    seoForm.county.style.background = "#e2e2e2";
    seoForm.state.value = "__none__";
  }
  
  function enableTextFields() {
    seoForm.city.disabled = false;
    seoForm.county.disabled = false;
    seoForm.city.style.background = "#fff";
    seoForm.county.style.background = "#fff";
  } 
  
  function submitSeoSearch(){
    if (seoForm.city.value != '' && seoForm.county.value == '') {
      getSeoUrl();
    } else if (seoForm.city.value == '' && seoForm.county.value != '') {       
      getSeoUrl();
    } else if (seoForm.city.value != '' && seoForm.county.value != '') {
      //initSeoFields();
      document.getElementById("errorDiv").innerHTML = "You may only Search by State, State and City, or State and County.";
      return;
    } else if (seoForm.state.value != '__none__') {
      getSeoUrl();
    } else {
      //initSeoFields();    
      document.getElementById("errorDiv").innerHTML = "Please select a State.";
      return;
    }        
  }
  
  function checkStateStatus() {
    if (seoForm.state.value == "__none__") {
      initSeoFields();
    } else {
      enableTextFields();
      seoForm.city.focus();
    }
  }

  function makeAjaxRequest(){
	try{
		  request = new XMLHttpRequest();
  	} catch(tryMS){
		  try{
			  request = new ActiveXObject("Msxml2.XMLHTTP");
		  } catch(otherMS){
			  try{
				  request = new ActiveXObject("Microsoft.XMLHTTP");
			  } catch(failed){
				  request = null;
			  }
		  }
	  }
	return request;
}
  
  function getSeoUrl(){
    rSeoUrl = makeAjaxRequest();
    fState = seoForm.state.value;
    fCity = seoForm.city.value;
    fCounty = seoForm.county.value;
    if(typeof(rSeoUrl) == null) alert("Unable to create request.");
    else {    
      // generate url string
      var rUrl = "/local_real_estate/search_state_city_county?state=";
      rUrl += fState;
      rUrl += "&city=";
      rUrl += fCity;
      rUrl += "&county=";
      rUrl += fCounty;
      rSeoUrl.onreadystatechange = renderSeoUrl;            
      rSeoUrl.open("GET", rUrl, true);
      rSeoUrl.send(null);      
    }
  }  
  
  function renderSeoUrl() {          
    if(rSeoUrl.readyState == 4){      
      if(rSeoUrl.status == 200){
	 var xmlResults = rSeoUrl.responseXML;        
	 var tmpSearchItems = xmlResults.getElementsByTagName("search_result");     
	 var tmpValid = tmpSearchItems[0].getElementsByTagName("valid")[0].firstChild.nodeValue;
	 if ( tmpValid == 1 ) {
          var tmpUrl = tmpSearchItems[0].getElementsByTagName("url")[0].firstChild.nodeValue;
         }
         if ( tmpValid == 1 ) {
          window.parent.location = tmpUrl;
          } else {
          //initSeoFields();
          document.getElementById("errorDiv").innerHTML = "We don't have the city you entered. Please try another city.";          
          }
         } else {
          document.getElementById("errorDiv").innerHTML = "Error loading XML feed.";        
         }
      }
  }  
  
  function submitOnEnter(e, eval_code) {
    var intKeyCode;
    var intKeyCharCode;
    var bEnterCheck;
    var intEnterCode = 13;  // "Enter" button is key code 13.
        
    if ( window.event ) { //IE
        intKeyCode = e.keyCode;
    }
    else if ( e.which ) { // Netscape/Firefox/Opera
        intKeyCode = e.which;
    }
    
    bEnterCheck = ( intKeyCode == intEnterCode );
    
    if ( bEnterCheck ) {
        eval(eval_code);
    }
    
    return bEnterCheck;
  }

  initSeoFields();

