var xmlHttp;

var typeArray = new Array(1);
var type = "";
var floorsArray = new Array(1);
var floors = ""
var bedroomsArray = new Array(1);
var bedrooms = "";
var bathroomsArray = new Array(1);
var bathrooms = "";
var garageArray = new Array(1);
var garage = "";
var lottypeArray = new Array(1);
var lottype = "";
var garageorientationArray = new Array(1);
var garageorientation = "";
var specialfeaturesArray = new Array(1);
var specialfeatures = "";
var bedroomstyleArray = new Array(1);
var bedroomstyle = "";
var planstyleArray = new Array(1);
var planstyle = "";

var livingareamin = "";
var livingareamax = "";
var buildingwidthmin = "";
var buildingwidthmax = "";
var buildingdepthmin = "";
var buildingdepthmax = "";
var narrowmin = "&narrowmin=";
var narrowmax = "&narrowmax=";
var newplans = "";

var bedroomsmin = "";
var bedroomsmax = "";
var bathroomsmin = "";
var bathroomsmax = "";
var floorsmin = "";
var floorsmax = "";
var planname = "&planname=";
var browsestyles = "&browsestyles=nostyle";

function QuickSearch(name, value) {
  // livingareamin
  if (name == "livingareamin") {
    livingareamin = "&livingareamin=" + value;
  }

  // livingareamax
  if (name == "livingareamax") {
    livingareamax = "&livingareamax=" + value;
  }

  // bedroomsmin
  if (name == "bedroomsmin") {
    bedroomsmin = "&bedroomsmin=" + value;
  }

  // bedroomsmax
  if (name == "bedroomsmax") {
    bedroomsmax = "&bedroomsmax=" + value;
  }

  // bathroomsmin
  if (name == "bathroomsmin") {
    bathroomsmin = "&bathroomsmin=" + value;
  }

  // bathroomsmax
  if (name == "bathroomsmax") {
    bathroomsmax = "&bathroomsmax=" + value;
  }

  // floorsmin
  if (name == "floorsmin") {
    floorsmin = "&floorsmin=" + value;
  }

  // floorsmax
  if (name == "floorsmax") {
    floorsmax = "&floorsmax=" + value;
  }

  // set query string
  var queryString = "?searchForm=quicksearch";

  if (livingareamin == "") {
    queryString = queryString + "&livingareamin=";
  } else {
    queryString = queryString + livingareamin;
  }
  if (livingareamax == "") {
    queryString = queryString + "&livingareamax=";
  } else {
    queryString = queryString + livingareamax;
  }
  if (bedroomsmin == "") {
    queryString = queryString + "&bedroomsmin=nomin";
  } else {
    queryString = queryString + bedroomsmin;
  }
  if (bedroomsmax == "") {
    queryString = queryString + "&bedroomsmax=nomax";
  } else {
    queryString = queryString + bedroomsmax;
  }
  if (bathroomsmin == "") {
    queryString = queryString + "&bathroomsmin=nomin";
  } else {
    queryString = queryString + bathroomsmin;
  }
  if (bathroomsmax == "") {
    queryString = queryString + "&bathroomsmax=nomax";
  } else {
    queryString = queryString + bathroomsmax;
  }
  if (floorsmin == "") {
    queryString = queryString + "&floorsmin=nomin";
  } else {
    queryString = queryString + floorsmin;
  }
  if (floorsmax == "") {
    queryString = queryString + "&floorsmax=nomax";
  } else {
    queryString = queryString + floorsmax;
  }

  // add narrowmin, narrowmax, planname and browsestyles parameters to query string (always blank)
  queryString = queryString + narrowmin + narrowmax + planname + browsestyles;

  xmlHttp=GetXmlHttpObject();
  if (xmlHttp == null) {
    alert ("Your browser does not support AJAX!");
    return;
  }

  url = "AjaxQuickSearch.cfm"
  url = url + queryString;
  url = url + "&sid=" + Math.random();
  xmlHttp.onreadystatechange = stateChangedQS;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

function Search2(name, value, url) {
  // type
  if (name == "type") {
    // IE doesn't support array.indexOf... wow!
    if (!Array.indexOf) {
      Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
          if (this[i] == obj) {
            return i;
          }
        }
        return -1;
      }
    }
    if (typeArray.indexOf("&type=" + value) == -1) {
      typeArray.push("&type=" + value);
    } else {
      typeArray.splice(typeArray.indexOf("&type=" + value), 1);
    }
    type = typeArray.join("");
  }

  // floors
  for (i = 1; i <= 3; i++) {
    if (name == "floors" + i) {
      // IE doesn't support array.indexOf... wow!
      if (!Array.indexOf) {
        Array.prototype.indexOf = function(obj) {
          for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
              return i;
            }
          }
          return -1;
        }
      }
      if (floorsArray.indexOf("&floors" + i + "=" + value) == -1) {
        floorsArray.push("&floors" + i + "=" + value);
      } else {
        floorsArray.splice(floorsArray.indexOf("&floors" + i + "=" + value), 1);
      }
      floors = floorsArray.join("");
    }
  }

  // bedrooms
  for (i = 1; i <= 5; i++) {
    if (name == "bedrooms" + i) {
      // IE doesn't support array.indexOf... wow!
      if (!Array.indexOf) {
        Array.prototype.indexOf = function(obj) {
          for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
              return i;
            }
          }
          return -1;
        }
      }
      if (bedroomsArray.indexOf("&bedrooms" + i + "=" + value) == -1) {
        bedroomsArray.push("&bedrooms" + i + "=" + value);
      } else {
        bedroomsArray.splice(bedroomsArray.indexOf("&bedrooms" + i + "=" + value), 1);
      }
      bedrooms = bedroomsArray.join("");
    }
  }

  // bathrooms
  for (i = 1; i <= 4; i++) {
    if (name == "bathrooms" + i) {
      // IE doesn't support array.indexOf... wow!
      if (!Array.indexOf) {
        Array.prototype.indexOf = function(obj) {
          for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
              return i;
            }
          }
          return -1;
        }
      }
      if (bathroomsArray.indexOf("&bathrooms" + i + "=" + value) == -1) {
        bathroomsArray.push("&bathrooms" + i + "=" + value);
      } else {
        bathroomsArray.splice(bathroomsArray.indexOf("&bathrooms" + i + "=" + value), 1);
      }
      bathrooms = bathroomsArray.join("");
    }
  }

  // garage
  for (i = 1; i <= 5; i++) {
    if (name == "garage" + i) {
      // IE doesn't support array.indexOf... wow!
      if (!Array.indexOf) {
        Array.prototype.indexOf = function(obj) {
          for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
              return i;
            }
          }
          return -1;
        }
      }
      if (garageArray.indexOf("&garage" + i + "=" + value) == -1) {
        garageArray.push("&garage" + i + "=" + value);
      } else {
        garageArray.splice(garageArray.indexOf("&garage" + i + "=" + value), 1);
      }
      garage = garageArray.join("");
    }
  }

  // lottype
  if (name == "lottype") {
    // IE doesn't support array.indexOf... wow!
    if (!Array.indexOf) {
      Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
          if (this[i] == obj) {
            return i;
          }
        }
        return -1;
      }
    }
    if (lottypeArray.indexOf("&lottype=" + value) == -1) {
      lottypeArray.push("&lottype=" + value);
    } else {
      lottypeArray.splice(lottypeArray.indexOf("&lottype=" + value), 1);
    }
    lottype = lottypeArray.join("");
  }

  // garageorientation
  if (name == "garageorientation") {
    // IE doesn't support array.indexOf... wow!
    if (!Array.indexOf) {
      Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
          if (this[i] == obj) {
            return i;
          }
        }
        return -1;
      }
    }
    if (garageorientationArray.indexOf("&garageorientation=" + value) == -1) {
      garageorientationArray.push("&garageorientation=" + value);
    } else {
      garageorientationArray.splice(garageorientationArray.indexOf("&garageorientation=" + value), 1);
    }
    garageorientation = garageorientationArray.join("");
  }

  // specialfeatures
  if (name == "specialfeatures") {
    // IE doesn't support array.indexOf... wow!
    if (!Array.indexOf) {
      Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
          if (this[i] == obj) {
            return i;
          }
        }
        return -1;
      }
    }
    if (specialfeaturesArray.indexOf("&specialfeatures=" + value) == -1) {
      specialfeaturesArray.push("&specialfeatures=" + value);
    } else {
      specialfeaturesArray.splice(specialfeaturesArray.indexOf("&specialfeatures=" + value), 1);
    }
    specialfeatures = specialfeaturesArray.join("");
  }

  // bedroomstyle
  if (name == "bedroomstyle") {
    // IE doesn't support array.indexOf... wow!
    if (!Array.indexOf) {
      Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
          if (this[i] == obj) {
            return i;
          }
        }
        return -1;
      }
    }
    if (bedroomstyleArray.indexOf("&bedroomstyle=" + value) == -1) {
      bedroomstyleArray.push("&bedroomstyle=" + value);
    } else {
      bedroomstyleArray.splice(bedroomstyleArray.indexOf("&bedroomstyle=" + value), 1);
    }
    bedroomstyle = bedroomstyleArray.join("");
  }

  // planstyle
  if (name == "planstyle") {
    // IE doesn't support array.indexOf... wow!
    if (!Array.indexOf) {
      Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
          if (this[i] == obj) {
            return i;
          }
        }
        return -1;
      }
    }
    if (planstyleArray.indexOf("&planstyle=" + value) == -1) {
      planstyleArray.push("&planstyle=" + value);
    } else {
      planstyleArray.splice(planstyleArray.indexOf("&planstyle=" + value), 1);
    }
    planstyle = planstyleArray.join("");
  }

  // livingareamin
  if (name == "livingareamin") {
    livingareamin = "&livingareamin=" + value;
  }

  // livingareamax
  if (name == "livingareamax") {
    livingareamax = "&livingareamax=" + value;
  }

  // buildingwidthmin
  if (name == "buildingwidthmin") {
    buildingwidthmin = "&buildingwidthmin=" + value;
  }

  // buildingwidthmax
  if (name == "buildingwidthmax") {
    buildingwidthmax = "&buildingwidthmax=" + value;
  }

  // buildingdepthmin
  if (name == "buildingdepthmin") {
    buildingdepthmin = "&buildingdepthmin=" + value;
  }

  // buildingdepthmax
  if (name == "buildingdepthmax") {
    buildingdepthmax = "&buildingdepthmax=" + value;
  }

  // newplans
  if (name == "newplans") {
    newplans = "&newplans=" + value;
  }

  // set query string
  var queryString = "?searchForm=search2";

  // build up query string parameters
  if (type != "") {
    queryString = queryString + type;
  }
  if (floors != "") {
    queryString = queryString + floors;
  }
  if (bedrooms != "") {
    queryString = queryString + bedrooms;
  }
  if (bathrooms != "") {
    queryString = queryString + bathrooms;
  }
  if (garage != "") {
    queryString = queryString + garage;
  }
  if (lottype != "") {
    queryString = queryString + lottype;
  }
  if (garageorientation != "") {
    queryString = queryString + garageorientation;
  }
  if (specialfeatures != "") {
    queryString = queryString + specialfeatures;
  }
  if (bedroomstyle != "") {
    queryString = queryString + bedroomstyle;
  }
  if (planstyle != "") {
    queryString = queryString + planstyle;
  }
  if (livingareamin == "") {
    queryString = queryString + "&livingareamin=";
  } else {
    queryString = queryString + livingareamin;
  }
  if (livingareamax == "") {
    queryString = queryString + "&livingareamax=";
  } else {
    queryString = queryString + livingareamax;
  }
  if (buildingwidthmin == "") {
    queryString = queryString + "&buildingwidthmin=";
  } else {
    queryString = queryString + buildingwidthmin;
  }
  if (buildingwidthmax == "") {
    queryString = queryString + "&buildingwidthmax=";
  } else {
    queryString = queryString + buildingwidthmax;
  }
  if (buildingdepthmin == "") {
    queryString = queryString + "&buildingdepthmin=";
  } else {
    queryString = queryString + buildingdepthmin;
  }
  if (buildingdepthmax == "") {
    queryString = queryString + "&buildingdepthmax=";
  } else {
    queryString = queryString + buildingdepthmax;
  }
  if (newplans != "") {
    queryString = queryString + newplans;
  }

  // add narrowmin and narrowmax parameters to query string (always blank)
  queryString = queryString + narrowmin + narrowmax;

  xmlHttp=GetXmlHttpObject();
  if (xmlHttp == null) {
    alert ("Your browser does not support AJAX!");
    return;
  }

  url = url + queryString;
  url = url + "&sid=" + Math.random();
  xmlHttp.onreadystatechange = stateChanged;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

function stateChanged() {
  if (xmlHttp.readyState == 4) {
    document.getElementById("matchingPlans").innerHTML = "Matching Plans: " + xmlHttp.responseText;
  }
}

function stateChangedQS() {
  if (xmlHttp.readyState == 4) {
    document.getElementById("matchingPlansQS").innerHTML = "Matching Plans: " + xmlHttp.responseText;
  }
}

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;
}