var xmlHttp;
var ajax = new sack();
var url;

var myGlobalHandlers = {
	onCreate: function(){
		Element.show('loadingBox');
	},
	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.hide('loadingBox');
		}
	}
};


function resultCount()
{
  url = "searchBackend.php?" + Form.serialize("listingSearch")
  xmlHttp=GetXmlHttpObject(stateChanged)
  xmlHttp.open("GET", url , true)
  xmlHttp.send(null)
}   

function feedbackWindow()
{
   win1 = new Window('1', {className: "alphacube", title: "Highland Real Estate: User Feedback", width:600, height:600, top:40, left:200}); win1.setZIndex(9999); win1.setURL('feedback.php');
   win1.setDestroyOnClose();
   win1.show();
}

function inquiryWindow(mlsId)
{
   inquiryWin = new Window('2', {className: "alphacube", title: "Highland Real Estate: Request for Information", width:600, height:630, top:40, left:200}); inquiryWin.setZIndex(9999); inquiryWin.setURL('inquiry.php?id=' + mlsId);
   inquiryWin.setDestroyOnClose();
   inquiryWin.show();
}

function chatWindow()
{
   chatWin = new Window('3', {className: "alphacube", title: "Highland Real Estate: Agent Chat", width:350, height:430, top:40, left:200}); chatWin.setZIndex(9999); chatWin.setURL('chat/chat.html');
   chatWin.setDestroyOnClose();
   chatWin.show();
}

function lakeAreaResultCount()
{
  url = "searchBackend.php?" + Form.serialize("lakeAreaSearch")
  xmlHttp=GetXmlHttpObject(stateChanged)
  xmlHttp.open("GET", url , true)
  xmlHttp.send(null)
}   

function propertyClassResultCount()
{
  url = "searchBackend.php?" + Form.serialize("propertyClassSearch")
  xmlHttp=GetXmlHttpObject(stateChanged)
  xmlHttp.open("GET", url , true)
  xmlHttp.send(null)
}   

function citySearchResultCount()
{
  url = "searchBackend.php?" + Form.serialize("citySearch")
  xmlHttp=GetXmlHttpObject(stateChanged)
  xmlHttp.open("GET", url , true)
  xmlHttp.send(null)
}  

function getPropertyTypes()
{
  var propertyClass = $F('t');
  document.propertyClassSearch.ty.options.length = 0;	// Empty property type select box
  document.propertyClassSearch.co.options.length = 0;	// Empty property type select box
  document.propertyClassSearch.ci.options.length = 0;	// Empty property type select box
  if(propertyClass.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=pt&t=' + propertyClass;	// Specifying which file to get
    ajax.onCompletion = createPropertyTypes;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  } 
}

function getCounties()
{
  var countyClass = $F('t');
  document.propertyClassSearch.co.options.length = 0;	// Empty property type select box
  document.propertyClassSearch.ci.options.length = 0;	// Empty property type select box
  if(countyClass.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=co&t=' + countyClass;	// Specifying which file to get
    ajax.onCompletion = createCounties;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  }
}

function getSearchCities()
{
  var countyClass = $F('t');
  document.getElementById('ci').options.length = 0;	// Empty city select box
  document.getElementById('ty').options.length = 0;	// Empty city select box
  if(countyClass.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=cci&t=' + countyClass;	// Specifying which file to get
    ajax.onCompletion = createCities;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  }
}

function citySquareFootage()
{
  var type = Form.Element.serialize('t');
  var city = Form.Element.serialize('ci');
  var propType = Form.Element.serialize('ty');
  var startPrice = Form.Element.serialize('bp');
  var endPrice = Form.Element.serialize('ep');
  document.getElementById('sq').options.length = 0;	// Empty city select box
  if(type.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=csq&' + type + '&' + city + '&' + propType + '&' + startPrice + '&' + endPrice;	// Specifying which file to get
    ajax.onCompletion = createSquareFootage;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  } 
}

function getLakeCounties()
{
  var countyClass = $F('t');
  var lakeArea = Form.Element.serialize('la');
  var lakeType = Form.Element.serialize('v');
  var startPrice = Form.Element.serialize('bp');
  var endPrice = Form.Element.serialize('ep');
  document.getElementById('co').options.length = 0;	// Empty city select box
  document.getElementById('ci').options.length = 0;	// Empty city select box
  if(countyClass.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=lco&t=' + countyClass + '&' + lakeArea + '&' + lakeType + '&' + startPrice + '&' + endPrice;	// Specifying which file to get
    ajax.onCompletion = createCounties;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  }
}

function getLakeCities()
{
  var countyClass = $F('t');
  var lakeArea = Form.Element.serialize('la');
  var lakeType = Form.Element.serialize('v');
  var lakeCounty = Form.Element.serialize('co');
  var startPrice = Form.Element.serialize('bp');
  var endPrice = Form.Element.serialize('ep');
  document.getElementById('ci').options.length = 0;	// Empty city select box
  if(countyClass.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=lci&t=' + countyClass + '&' + lakeArea + '&' + lakeType + '&' + lakeCounty + '&' + startPrice + '&' + endPrice;	// Specifying which file to get
    ajax.onCompletion = createCities;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  } 
}

function getCities()
{
  var cityClass = $F('t');
  var cityCounty = Form.Element.serialize('co');
  var propType = Form.Element.serialize('ty');
  document.getElementById('ci').options.length = 0;	// Empty city select box
  if(cityCounty.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=ci&t=' + cityClass + '&' + cityCounty + '&' + propType;	// Specifying which file to get
    ajax.onCompletion = createCities;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  }
}

function getLakeTypes()
{
  var lakeCategory = $F('t');
  var lakeArea = Form.Element.serialize('la');
  document.getElementById('v').options.length = 0;	// Empty city select box
  document.getElementById('ci').options.length = 0;	// Empty city select box
  document.getElementById('co').options.length = 0;	// Empty city select box
  if(lakeCategory.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=gv&t=' + lakeCategory + '&' + lakeArea;	// Specifying which file to get
    ajax.onCompletion = createLakeTypes;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  } 
}

function getCityPropertyTypes()
{
  var type = Form.Element.serialize('t');
  var city = Form.Element.serialize('ci');
  var startPrice = Form.Element.serialize('bp');
  var endPrice = Form.Element.serialize('ep');
  document.getElementById('ty').options.length = 0;	// Empty city select box
  document.getElementById('sq').options.length = 0;	// Empty city select box
  if(type.length>0)
  {
    ajax.requestFile = 'searchBackend.php?f=cpt&' + type + '&' + city + '&' + startPrice + '&' + endPrice;	// Specifying which file to get
    ajax.onCompletion = createPropertyTypes;	// Specify function that will be executed after file has been found
    ajax.runAJAX();		// Execute AJAX function
  } 
}

function buildFeedbackForm()
{
   var url = 'ajaxProcessor.php?f=buildFeedback'
   var myAjax = new Ajax.Updater(
        'feedbackResponse',
   	url,
   	{
   		method: 'get'
   	}
   )   				
}

function submitFeedback()
{
  if(Form.Element.serialize('eAddress'))
  {
     var email = Form.Element.serialize('eAddress')
  }
  else
  {
     var email = 'Not Specified'
  };
  var type = Form.Element.serialize('ftype');
  var details = Form.Element.serialize('fdetails');
  var entered = Form.Element.serialize('valid');
  var IP = Form.Element.serialize('submittedIP');
  var referrer = Form.Element.serialize('referrer');
  var url = 'ajaxProcessor.php'
  var pars = 'f=feedback&' + email + '&' + type + '&' + details + '&' + IP + '&' + referrer
  var myAjax = new Ajax.Updater(
        'feedbackResponse',
   	url,
   	{
   		method: 'get',
   		parameters: pars
   	}
   )
}

function submitInquiry()
{
  var email = Form.Element.serialize('emailAddress')
  var fname = Form.Element.serialize('firstName');
  var lname = Form.Element.serialize('lastName');
  var IP = Form.Element.serialize('submittedIP');
  var referrer = Form.Element.serialize('referrer');
  var phoneNumber = Form.Element.serialize('phoneNumber');
  var details = Form.Element.serialize('inquiryDetails');
  var mlsId = Form.Element.serialize('mlsId');
  var url = 'ajaxProcessor.php'
  var pars = 'f=inquiry&' + mlsId + '&' + fname + '&' + lname + '&' + email + '&' + phoneNumber + '&' + details + '&' + IP + '&' + referrer
  var myAjax = new Ajax.Updater(
        'feedbackResponse',
   	url,
   	{
   		method: 'get',
   		parameters: pars
   	}
   )
}

function resetLakeValues()
{
  document.getElementById('v').options.length = 0;	// Empty city select box
  document.getElementById('co').options.length = 0;	// Empty city select box
  document.getElementById('ci').options.length = 0;	// Empty city select box
  document.getElementById('la').options.selected = false;		// Unselect choices
}

function stateChanged() 
{ 
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  { 
    document.getElementById("listingCount").innerHTML=xmlHttp.responseText 
  } 
} 

function createPropertyTypes()
{
  var obj = document.getElementById('ty');
  eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createCounties()
{
  var obj = document.getElementById('co');
  eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createCities()
{
  var obj = document.getElementById('ci');
  eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createLakeTypes()
{
  var obj = document.getElementById('v');
  eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createSquareFootage()
{
  var obj = document.getElementById('sq');
  eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function GetXmlHttpObject(handler)
{ 
  var objXmlHttp=null
  if (navigator.userAgent.indexOf("Opera")>=0)
  {
    alert("This example doesn't work in Opera") 
    return 
  }
  if (navigator.userAgent.indexOf("MSIE")>=0)
  { 
    var strName="Msxml2.XMLHTTP"
    if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
    {
      strName="Microsoft.XMLHTTP"
    } 
    try
    { 
       objXmlHttp=new ActiveXObject(strName)
       objXmlHttp.onreadystatechange=handler 
       return objXmlHttp
    } 
    catch(e)
    { 
       alert("Error. Scripting for ActiveX might be disabled") 
       return 
    } 
  } 
  if (navigator.userAgent.indexOf("Mozilla")>=0)
  {
    objXmlHttp=new XMLHttpRequest()
    objXmlHttp.onload=handler
    objXmlHttp.onerror=handler 
    return objXmlHttp
  }

}

function getWebServiceCity()
{
  var zipCode = Form.Element.serialize('z');
  if(zipCode.length>0)
  {
     url = "searchBackend.php?f=wsc&" + zipCode;
     xmlHttp=GetXmlHttpObject(createWebServiceCity);
     xmlHttp.open("GET", url , true);
     xmlHttp.send(null);
  }
}

function createWebServiceCity()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  { 
    document.getElementById("resultCity").innerHTML=xmlHttp.responseText 
  }
}