/* AJAX functions for grabbing XML data */

var portletReqBuilder;
var portletReqCommunity;

function asynchGetBuilder(updateURL){
    if (window.XMLHttpRequest) {
        portletReqBuilder = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        portletReqBuilder = new ActiveXObject("Microsoft.XMLHTTP");
    }
    portletReqBuilder.onreadystatechange = processReqChangeBuilder;
    portletReqBuilder.open("GET", updateURL, true);
    portletReqBuilder.send(null);
}

function asynchGetCommunity(updateURL){
    if (window.XMLHttpRequest) {
        portletReqCommunity= new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        portletReqCommunity= new ActiveXObject("Microsoft.XMLHTTP");
    }
    portletReqCommunity.onreadystatechange = processReqChangeCommunity;
    portletReqCommunity.open("GET", updateURL, true);
    portletReqCommunity.send(null);
}

function processReqChangeBuilder() {
    if (portletReqBuilder.readyState == 4) {
        if (portletReqBuilder.status == 200) {
            // process response
            populateBuilderSelect();
        }
    }
}

function processReqChangeCommunity() {
    if (portletReqCommunity.readyState == 4) {
        if (portletReqCommunity.status == 200) {
            // process response
            populateCommunitySelect();
        }
    }
}

function populateBuilderSelect() {
  var strBuilder=portletReqBuilder.responseText;
  strBuilder=strBuilder.toLowerCase().replace(/\b[a-z]/g, convertToUpper)
  strBuilder=strBuilder.replace(/\x3cbuilderlist\x3e/gi, "");
  strBuilder=strBuilder.replace(/\x3cbuilder\x3e/gi, "<a href='http://homes.ajchomefinder.com/new-homes/GA-USA/");
  strBuilder=strBuilder.replace(/\x3ccompanyid\x3e/gi, "");
  strBuilder=strBuilder.replace(/\x3c\x2fcompanyid\x3e/gi, "-companyid/Price_Sales,1-ns'>");
  strBuilder=strBuilder.replace(/\x3cbuildername\x3e/gi, "");
  strBuilder=strBuilder.replace(/\x3c\x2fbuildername\x3e/gi, "");
  strBuilder=strBuilder.replace(/&amp;/gi, "&");
  strBuilder=strBuilder.replace(/builder\x3e/gi, "</a>");
  strBuilder=strBuilder.replace(/\x3c\x2foption/gi, "");

  // first builder dropdown
document.getElementById("builders").innerHTML=strBuilder;
 
  }


function populateCommunitySelect() {
  var strCommunity=portletReqCommunity.responseText;
  strCommunity=strCommunity.toLowerCase().replace(/\b[a-z]/g, convertToUpper)
  strCommunity=strCommunity.replace(/ - /gi, " ");
  strCommunity=strCommunity.replace(/-_/gi, " ");
  strCommunity=strCommunity.replace(/y-m/gi, "y M");
  strCommunity=strCommunity.replace(/y- m/gi, "y M");
  strCommunity=strCommunity.replace(/\d{4}-/gi, " href='http://homes.ajchomefinder.com/new-homes/id/");
  strCommunity=strCommunity.replace(/\x3cbuilderlist\x3e/gi, "");
  strCommunity=strCommunity.replace(/\x3ccommunity\x3e/gi, "<a ");
  strCommunity=strCommunity.replace(/\x3ccommunityid\x3e/gi, "");
  strCommunity=strCommunity.replace(/\x3c\x2fcommunityid\x3e/gi, "'>");
  strCommunity=strCommunity.replace(/\x3ccommunityname\x3e/gi, "");
  strCommunity=strCommunity.replace(/\x3c\x2fcommunityname\x3e/gi, "");
  strCommunity=strCommunity.replace(/&amp;/gi, "&");
  strCommunity=strCommunity.replace(/community\x3e/gi, "</a>");
  strCommunity=strCommunity.replace(/\x3c\x2foption/gi, "");


  // community dropdown
	 document.getElementById("communities").innerHTML=strCommunity;


  }


  function convertToUpper()
  {
     return arguments[0].toUpperCase();
  }



/* Search input scripts */

function prepareInputs() {	
	$("#searchterm input").attr("id", "txtSearch");
	$("#searchterm input").val("Enter a city, state or ZIP code");	

	
    $("#searchterm input").focus( function() {
        if ($("#searchterm input").attr("value") == "Enter a city, state or ZIP code") {
            $("#searchterm input").attr("value", "");	
        }        
  });

}

function prepareValidateSubmit() {
    $("#location-submit").click( function() {
     if ($("#searchterm input").attr("value") == "Enter a city, state or ZIP code" || $("#searchterm input").attr("value") == "") {
	     $("#searchterm input").attr("value", "ATLANTA, GA");	
	        }
    });
}

function navFix() {
	if ($.browser.mozilla && $.browser.version < "1.9" || $.browser.msie) {
	$("li.hoverHide").mouseover(function(){
	  $(".ajc-rr-bigbox embed").css("display","none");	
		$(".ajc-rr-bigbox iframe").css("display","none");
		$(".ajc-rr-bigbox table").css("display","none");		
	    }).mouseout(function(){
			  $(".ajc-rr-bigbox embed").css("display","block");	
				$(".ajc-rr-bigbox iframe").css("display","block");
				$(".ajc-rr-bigbox table").css("display","block");
			    });
}


	if ($.browser.mozilla && $.browser.version < "1.9" || $.browser.msie) {	
	$("li.hoverHidePencil").mouseover(function(){
	  $(".ajc-pencil-ad  embed").css("display","none");	
		$(".ajc-pencil-ad").css("position","static");
		}).mouseout(function(){	
			$(".ajc-pencil-ad embed").css("display","block");	
		   });
}
	

			
	if ($.browser.msie) {
		$("li.hoverHideIE").mouseover(function(){
		    $("#quick-search select").css("visibility","hidden");
		    }).mouseout(function(){
				      $("#quick-search select").css("visibility","visible");			
				    });
					 }


}



/* onload actions */
window.onload = function() {
prepareInputs();
prepareValidateSubmit();
asynchGetBuilder('/gen/ajchomefinder/builders.xml');
asynchGetCommunity('/gen/ajchomefinder/NewHomesCommunity.xml');
navFix();
}

