//******************************************************************************
// Advanced Search AJAX String Builder                    LJG PARTNERS 5/27/2008
// -----------------------------------------------------------------------------
// Class that joins together all of the selected search options to produce
// correct AJAX-populated search options.
//******************************************************************************
g_ieRe = /^(\<SELECT.*?\>)\<.*\>\<\/.*?\>$/;
g_ieReTest = /^\<SELECT/;

// el  - DOM Element ID
// qc  - Is a selection field (default true, false means checkbox)
var PHAS_Element = function(op)
{
  this.el  = op.el;
  this.qc  = (typeof(op.qc)  != "undefined") ? op.qc  : true;
};

var PHAS = 
{
  url: "/search/data/phAdvancedSearch.php",

  elements:
  [
    new PHAS_Element({ el: "city_region"               }), // city_region will always have the same options.
    new PHAS_Element({ el: "min_price"                 }),
    new PHAS_Element({ el: "max_price"                 }),
    new PHAS_Element({ el: "min_bedrooms"              }),
    new PHAS_Element({ el: "max_bedrooms"              }),
    new PHAS_Element({ el: "max_sqft"                  }),
    new PHAS_Element({ el: "level"                     }),
    new PHAS_Element({ el: "garage"                    }),
    new PHAS_Element({ el: "am_master",      qc: false }), 
    new PHAS_Element({ el: "am_gated",       qc: false }),
    new PHAS_Element({ el: "am_enerysmart",  qc: false }),
    new PHAS_Element({ el: "am_golf",        qc: false }),
    new PHAS_Element({ el: "am_park",        qc: false }),
    new PHAS_Element({ el: "am_rec",         qc: false }),
    new PHAS_Element({ el: "am_pool",        qc: false }),
    new PHAS_Element({ el: "am_playground",  qc: false }),
    new PHAS_Element({ el: "am_award",       qc: false }),
    new PHAS_Element({ el: "st_nowsell",     qc: false }), 
    new PHAS_Element({ el: "st_comingsoon",  qc: false }),
    new PHAS_Element({ el: "st_finalsale",   qc: false }),
		new PHAS_Element({ el: "st_newreduced",   qc: false })
  ],
  
  queryString: "",

  doUpdate: function()
  {
    LJG_Ajax(this.queryString, function(response, parent)
    {
      try { response = response.responseText; } catch(e) { alert("Ajax Error"); return; }
      response = response.split("\n");
      for (var i = 0; i < response.length; i++)
      {
        var r = response[i].split("\|\|");
        if (r[0] == "") { continue; }
        try { parent.highlight(r[0], false); } catch (e) { }

        try {
          if (g_ieReTest.test(document.getElementById(r[0]).outerHTML))
          { 
            var t = g_ieRe.exec(document.getElementById(r[0]).outerHTML)[1];
            document.getElementById(r[0]).outerHTML = t + r[1] + "</SELECT>";
          }
          else
          {
            document.getElementById(r[0]).innerHTML = r[1];
          }
        } catch (e) { }
      }
    }, this);
  },

  // Creates the query string, then calls Ajax Update.
  build: function(id)
  {
    this.queryString = [];

    for (var i = 0; i < this.elements.length; i++)
    {
      var el  = this.elements[i];
      var val = (el.qc) ? document.getElementById(el.el).value : document.getElementById(el.el + "_cb").checked;
      if (el.qc && el.el != "city_region" && el.el != id) { this.highlight(el.el); }

      this.queryString.push(el.el + "=" + val);
    }

    this.queryString = this.queryString.join("&") + "&getfor=" + id;
    this.doUpdate();
  },

  highlight: function(id, ison)
  {
    ison = (typeof(ison) == "undefined") ? true : false;
    document.getElementById(id).style.backgroundColor = (ison) ? "#FFFFA8" : "#fff";    
  }
};

function LJG_Ajax(parameters, responseHandler, scope)
{
   if (typeof(parameters) == "undefined") { parameters = ''; }

   // Build the request.
   var ajaxRequest = ((window.XMLHttpRequest) 
                      ? new XMLHttpRequest() 
                      : ((window.ActiveXObject)
                         ? new ActiveXObject("Microsoft.XMLHTTP")
                         : false )); 

   // Create the callback
   function BindAjaxCallback() 
   {
     if (ajaxRequest.readyState == 4) 
     { if (typeof(responseHandler) == "function") { responseHandler(ajaxRequest, scope); } }
   }

   // Send the request
   if (ajaxRequest)
   {
      var m_now = new Date();
      parameters += "&nocachetimestamp=" + m_now.getTime();
      ajaxRequest.onreadystatechange = BindAjaxCallback;
      ajaxRequest.open("POST", "/search/data/phAdvancedSearch.php", true);
      ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      ajaxRequest.setRequestHeader('Connection', 'close');
      ajaxRequest.send(parameters);
   }
}
