  
  /**
  * Organic SIV Ajax Functions
  *
  * Gordon Brown
  *
  * 24/11/2009
  *
  * gordon.brown@infoserve.com
  *
  */
  
// AJAX XML Function
// The purpose of the function is to solve the problem of 
// creating different XMLHTTP objects for different browsers
function GetXmlHttpObject() {
  
  var xmlHttp = null;
  
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  
  catch (e){
    //Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
 }
 
 return xmlHttp; 
 
}  
  
function getFreeListings(vPostCodeArea, vSiv, vPostCodeDistrict, vTown, vCounty, vOffset, vRecordQuantity) {    
  
  if(vSiv != undefined && ((vPostCodeDistrict != undefined && vPostCodeArea != undefined) || vTown != undefined || vCounty != undefined)) {
  
    //Display The Progress Indicator
    showProgressIndicator();
    document.getElementById('freeListings').className = 'faded';
    ////////////////////////////////
  
    //Prepare Optional Parameters  
    if(vOffset == undefined) {
      vOffset = 0;
    }
    
    if(vRecordQuantity == undefined) {
      vRecordQuantity = 10;
    }
    /////////////////////////////
    
    //Prepare The URL Variable        
    var url = 'http://www.flooringlocal.co.uk/get_free_listings.php';
    var paramString = '';
    //////////////////////////
    
    //Add The Parameters To The URL
    if(vPostCodeArea != undefined) {
      if(paramString != '') paramString += '&'; else paramString += '?';
      paramString += 'postCodeArea=' + escape(vPostCodeArea);
    }
    
    if(vPostCodeDistrict != undefined) {
      if(paramString != '') paramString += '&'; else paramString += '?';
      paramString += 'postCodeDistrict=' + escape(vPostCodeDistrict);
    }
    
    if(vTown != undefined) {
      if(paramString != '') paramString += '&'; else paramString += '?';
      paramString += 'town=' + escape(vTown);
    }
    
    if(vCounty != undefined) {
      if(paramString != '') paramString += '&'; else paramString += '?';
      paramString += 'county=' + escape(vCounty);
    }
    
    paramString += '&siv=' + escape(vSiv);    
    paramString += '&offset=' + escape(vOffset);
    paramString += '&recordQuantity=' + escape(vRecordQuantity);
    
    url += paramString;
    
    //Create An Instance Of The XML HTTP Object
    xmlHttp = GetXmlHttpObject();
    ///////////////////////////////////////////
    
    //Ensure That The Current Browser Supports
    //The XML HTTP Object
    if(xmlHttp == null) {
      alert ("Browser does not support HTTP Request");
      return;
    }
    //////////////////////////////////////////
    
    //Prepare The URL And Then
    //Append A Random ID To The URL String To Ensure 
    //That The Page Returned Is Not A Cached One  
    url=url+"&sid="+Math.random();
    //////////////////////////////////////////////
   
    //Make The Request      
    xmlHttp.open("POST",url,true);
    //xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
    //////////////////////////////
    
    //Change The Destination Div On Receiving
    //A Valid Ready State
    xmlHttp.onreadystatechange = function () {
      if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200 || xmlHttp.status == 304) {
          var response = xmlHttp.responseText.split('^');
          //Hide The Progress Indicator
          hideProgressIndicator();
          document.getElementById('freeListings').className = '';
          /////////////////////////////
          document.getElementById('freeListings').innerHTML = response[0];
          document.getElementById('pagination').innerHTML = response[1];          
        }
      }
    }
    
    xmlHttp.send(null);  
    ///////////////////////////    
    
  }else {
  
    //Perform Action When Not Enough Parameters Were Passed
    //..............................
    
    alert("Not Enough Params..." + vPostCodeArea + " | " + vPostCodeDistrict + " | " + vTown);
    return false;
      
  } 

}
