var httpRequest;

function createRequestObject() {
   var ro;
   if (window.XMLHttpRequest) {
      ro = new XMLHttpRequest();
   } else {
      if (window.ActiveXObject) {
         ro = new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   return ro;
}

function checkReadyState() {
   if(httpRequest.readyState < 4) {
      AdvSearchAJAX.msgBox.style.display = "block";
   } else if(httpRequest.readyState == 4) {
      AdvSearchAJAX.msgBox.style.display = "none";
      return (httpRequest.status == 200);
   }
}

var AdvSearchAJAX = {
   msgBox: null,
   detailsObj: null,
   buildObj: null,
   ctsRgnsObj: null,
   qrtrsVlgsObj: null,
   init: function() {
      this.msgBox       = document.getElementById('msgArea');
      this.detailsObj   = document.getElementById('tpDtlsDv');
      this.buildObj     = document.getElementById('bldngDtlsDv');
      this.ctsRgnsObj   = document.getElementById('ctsRgns');
      this.qrtrsVlgsObj = document.getElementById('quartersVilages');
   },
   selectAllDistricts: function() {
      i = 0;
      while(document.getElementById('dstrct'+i)) {
         document.getElementById('dstrct'+i).checked = true;
         i++;
      }
   },
   deselectAllDistricts: function() {
      i = 0;
      while(document.getElementById('dstrct'+i)) {
         document.getElementById('dstrct'+i).checked = false;
         i++;
      }
   },
   showDistricts: function() {
      document.getElementById('citiesDistricts').style.display = "block";
   },
   hideDistricts: function() {
      document.getElementById('citiesDistricts').style.display = "none";
      this.deselectAllDistricts();
   },
   hideQuartersVilages: function() {
      this.qrtrsVlgsObj.innerHTML = '';
      this.qrtrsVlgsObj.style.display = "none";
   },
   selectAllQV: function(optn) {
      i = 0;
      while(document.getElementById('cvqIDs'+i)) {
         document.getElementById('cvqIDs'+i).checked = optn;
         i++;
      }
   },
   choosedEstateType: function(eID) {
      if(eID > 0) {
         this.getEstateDetails(eID, 1);
         document.getElementById('sRealEstates').focus();
      } else {
         this.detailsObj.innerHTML = '';
         this.buildObj.innerHTML = '';
      }
   },
   /*== Method for getting Estate & Building Details ==*/
   getEstateDetails: function(chsdVl, cs) {
      if((chsdVl > 0) || (chsdVl == -1)) {
         this.msgBox.style.display = "block";
         httpRequest = createRequestObject();
         var aParams = new Array();
         var sParam = encodeURIComponent("chsdVl");
         sParam += "=";
         sParam += encodeURIComponent(chsdVl);
         aParams.push(sParam);

         sParam = encodeURIComponent("cs");
         sParam += "=";
         sParam += encodeURIComponent(cs);
         aParams.push(sParam);

         sBody = aParams.join("&");
         url = "get_details_properties.php";
         httpRequest.open("POST", url, true);
         httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         httpRequest.onreadystatechange = this.handleEstateDetails;
         httpRequest.send(sBody);
      }
   },
   /*== Method for handle Estate & Building Details ==*/
   handleEstateDetails: function() {
      if(checkReadyState()) {
         if(httpRequest.responseText) {
            tmpArr = httpRequest.responseText.split('<!-- %% spliter %% -->');
            AdvSearchAJAX.detailsObj.innerHTML = tmpArr[0];
            if(tmpArr[1].length > 0)
               AdvSearchAJAX.buildObj.innerHTML = tmpArr[1];
            else
               AdvSearchAJAX.buildObj.innerHTML = '';
         }
      }
   },
   /*== Method for getting Cities or Regions ==*/
   getCitiesRegions: function(tp) {
      this.msgBox.style.display = "block";
      (tp == 2) ? this.hideDistricts() : 0;
      this.hideQuartersVilages();
      document.itemform.citiesOrRegions.value = tp;
      httpRequest = createRequestObject();
      var aParams = new Array();
      var sParam = encodeURIComponent("tp");
      sParam += "=";
      sParam += encodeURIComponent(tp);
      aParams.push(sParam);
      sBody = aParams.join("&");
      url = "get_cities_regions.php";
      httpRequest.open("POST", url, true);
      httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      httpRequest.onreadystatechange = this.handleCitiesRegions;
      httpRequest.send(sBody);
   },
   /*== Method for handle Cities or Regions ==*/
   handleCitiesRegions: function() {
      if(checkReadyState()) {
         if(httpRequest.responseText) {
            AdvSearchAJAX.ctsRgnsObj.innerHTML = httpRequest.responseText;
         }
      }
   },
   choosedCity: function(cID) {
   	//alert(cID);
      if(cID > 0) {
         this.deselectAllDistricts();
         this.getQuartersVilages(1, cID, '');
      } else {
         this.hideQuartersVilages();
      }
   },
   choosedRegion: function(rID) {
      if(rID > 0) {
         this.getQuartersVilages(2, rID, '');
      } else {
         this.hideQuartersVilages();
      }
   },
   getSelectedDistricts: function() {
      i = 0;
      j = 0;
      slctd = new Array();
      while(document.getElementById('dstrct'+i)) {
         if(document.getElementById('dstrct'+i).checked) {
            slctd[j] = document.getElementById('dstrct'+i).value;
            j++
         }
         i++;
      }

      if(slctd.length > 0)
         return slctd.join(",");
      else
         return "";
   },
   choosedDistrict: function() {
      //this.getQuartersVilages(1, document.getElementById('ctsRgnsID')[document.getElementById('ctsRgnsID').selectedIndex].value, this.getSelectedDistricts());
	  this.getQuartersVilages(1, document.getElementById('ctsRgnsID').value, this.getSelectedDistricts());
	  //alert(document.getElementById('ctsRgnsID').value + "\n" + this.getSelectedDistricts());
   },
   getQuartersVilages: function(tp, chsdVl, dIDs) {
   	//alert(tp + "\n" + chsdVl + "\n" + dIDs);
      if((chsdVl > 0) || (chsdVl == -1)) {
         (tp == 1) ? this.showDistricts() : this.hideDistricts();
         this.msgBox.style.display = "block";
         httpRequest = createRequestObject();
         var aParams = new Array();
         var sParam = encodeURIComponent("tp");
         sParam += "=";
         sParam += encodeURIComponent(tp);
         aParams.push(sParam);

         sParam = encodeURIComponent("pID");
         sParam += "=";
         sParam += encodeURIComponent(chsdVl);
         aParams.push(sParam);

         sParam = encodeURIComponent("dIDs");
         sParam += "=";
         sParam += encodeURIComponent(dIDs);
         aParams.push(sParam);

         sBody = aParams.join("&");
         url = "get_vilages_quarters.php";
         httpRequest.open("POST", url, true);
         httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         httpRequest.onreadystatechange = this.handleQuartersVilages;
         httpRequest.send(sBody);
      }
   },
   /*== Method for handle Towns/Villages ==*/
   handleQuartersVilages: function() {
      if(checkReadyState()) {
         if(httpRequest.responseText) {
            AdvSearchAJAX.qrtrsVlgsObj.innerHTML = httpRequest.responseText;
            AdvSearchAJAX.qrtrsVlgsObj.style.display = "block";
         }
      }
   }
     

};
/*== END CLASS AdvSearchAJAX ==*/
