﻿NRT.Property.Search.QuickSearch = function() {
  return {

    performSearch: function() {
      var oSearchCriteria = null;
      var oCriteria = null;
      var srchID = 0;
      var rbStatesArr = null;
      var stateID = 0;
      var stateName = null;
      var ddlLocation = null;
      var locationValue = '';
      var locationName = null;
      var locationInfo = null;
      var locationID = '';
      var locationType = '';
      var locationXML = '<Locations><location id="{LOCID}" name="{LOCNAME}" isParent="" type="{LOCTYPE}" /></Locations>';
      var ddlPropCategoryType = null;
      var propCategoryTypeValue = '';
      var propTypeID = '';
      var propCategoryID = '';
      var propCategoryDisplay = null;
      var propTypeDisplay = null;
      var propInfo = null;

      try {
        if (!this.validate(NRT.Property.Validation.TYPE_QUICKSEARCH)) {
          return;
        }

        oSearchCriteria = eval('(' + NRT.Utility.getElementByTagNameAndID('hdnSrchCriteria', 'INPUT').value + ')');

        if (NRT.Utility.objectExists(oSearchCriteria)) {
          rbStatesArr = NRT.Utility.getElementsByTagNameAndID('rblState', 'INPUT');
          if (NRT.Utility.objectExists(rbStatesArr) && rbStatesArr.length > 0) {
            for (i = 0; i < rbStatesArr.length; i += 1) {
              if (rbStatesArr[i].checked) {
                if (_oUtility.browserType() === 'ie') {
                  stateName = rbStatesArr[i].nextSibling.innerText.toString();
                } else {
                  stateName = rbStatesArr[i].nextSibling.textContent.toString();
                }
                stateID = rbStatesArr[i].value;
                oSearchCriteria.StateID = stateID;
                oSearchCriteria.StateName = stateName;
                i = rbStatesArr.length;
              }
            }
          }

          ddlLocation = NRT.Utility.getElementByTagNameAndID('ddlLocation', 'SELECT');
          if (NRT.Utility.objectExists(ddlLocation)) {
            locationName = ddlLocation.options[ddlLocation.selectedIndex].innerHTML;
            locationValue = ddlLocation.options[ddlLocation.selectedIndex].value;
            if (stateName !== null && stateName !== "" && NRT.Property.Search.Utility.getIsSingleStateMetro() === false) {
              locationName = locationName + ", " + stateName;
            }
            locationInfo = locationValue.split('|');
            if (locationInfo.length > 0) {
              // LocationID|LocationType
              locationID = locationInfo[0];
              locationType = locationInfo[1];
            }
            locationXML = locationXML.replace("{LOCID}", locationID);
            locationXML = locationXML.replace("{LOCNAME}", locationName);
            locationXML = locationXML.replace("{LOCTYPE}", locationType);
            oSearchCriteria.LocationXML = locationXML;
          }

          ddlPropCategoryType = NRT.Utility.getElementByTagNameAndID('ddlPropCategoryType', 'SELECT');
          if (NRT.Utility.objectExists(ddlPropCategoryType)) {
            propCategoryTypeValue = ddlPropCategoryType.options[ddlPropCategoryType.selectedIndex].value;
            propInfo = propCategoryTypeValue.split('|');
            if (propInfo.length > 0) {
              // PropertyCategoryID|CategoryDisplayName|PropertyTypeIDCSV|TypeDisplayNameCSV
              propCategoryID = propInfo[0];
              propCategoryDisplay = propInfo[1];
              propTypeID = propInfo[2];
              propTypeDisplay = propInfo[3];
            }
          }

          // Iterate through the criteria collection and fill their values
          for (i = 0; i < oSearchCriteria.CustomCriterias.length; i += 1) {
            oCriteria = oSearchCriteria.CustomCriterias[i];

            if (oCriteria.DBParamName === '@PropertyCategoryType') {
              oCriteria.ControlValue = propCategoryID;
              oCriteria.ControlDisplayValue = propCategoryDisplay;
            }
            else if (oCriteria.DBParamName === '@PropertyType') {
              if (propTypeID.length > 0) {
                propTypeID += ',';
              }
              if (propTypeDisplay.length > 0) {
                propTypeDisplay += ',';
              }
              oCriteria.ControlValue = propTypeID;
              oCriteria.ControlDisplayValue = propTypeDisplay;
            }
            else {
              NRT.Property.Search.Utility._setControlValue(oCriteria);
            }
          }

          srchID = SearchController.PersistSearch(oSearchCriteria);

          window.location = '/property/PropertyResults.as' + 'px?SearchID=' + srchID.value + NRT.Utility.Branding.getBranding(oSearchCriteria.BrandingID);
        }
      }
      catch (err) {
        _oErrorHandler.Error('NRT.Property.Search.QuickSearch.performSearch', _oErrorHandler.ERRORTYPE_JS, err);
        return;
      }
    },

    //TFS# 19852 - 06/21/2011 - Rajesh Kalidindi
    //Added for MoreOptions link from Detail Quick Search Control
    DetailQuickSearchMoreOptions: function() {
      var propCategoryTypeValue = '';
      var propTypeID = '';
      var propCategoryID = '';
      var propCategoryDisplay = null;
      var propTypeDisplay = null;
      var propInfo = null;
      var oSearchCriteria = null;
      
      oSearchCriteria = eval('(' + NRT.Utility.getElementByTagNameAndID('hdnSrchCriteria', 'INPUT').value + ')');
      
      ddlPropCategoryType = NRT.Utility.getElementByTagNameAndID('ddlPropCategoryType', 'SELECT');
      if (NRT.Utility.objectExists(ddlPropCategoryType)) {
        propCategoryTypeValue = ddlPropCategoryType.options[ddlPropCategoryType.selectedIndex].value;
        propInfo = propCategoryTypeValue.split('|');
        if (propInfo.length > 0) {
          // PropertyCategoryID|CategoryDisplayName|PropertyTypeIDCSV|TypeDisplayNameCSV
          propCategoryID = propInfo[0];
          propCategoryDisplay = propInfo[1];
          propTypeID = propInfo[2];
          propTypeDisplay = propInfo[3];
        }
      }
      
      // Iterate through the criteria collection and fill their values
      for (i = 0; i < oSearchCriteria.CustomCriterias.length; i += 1) {
        oCriteria = oSearchCriteria.CustomCriterias[i];

        if (oCriteria.DBParamName === '@PropertyCategoryType') {
          oCriteria.ControlValue = propCategoryID;
          oCriteria.ControlDisplayValue = propCategoryDisplay;
        }
        else if (oCriteria.DBParamName === '@PropertyType') {
          if (propTypeID.length > 0) {
            propTypeID += ',';
          }
          if (propTypeDisplay.length > 0) {
            propTypeDisplay += ',';
          }
          oCriteria.ControlValue = propTypeID;
          oCriteria.ControlDisplayValue = propTypeDisplay;
        }
        else {
          NRT.Property.Search.Utility._setControlValue(oCriteria);
        }
      }
      srchID = SearchController.PersistSearch(oSearchCriteria);
      window.location = '/property/propertysearch.as' + 'px?SearchID=' + srchID.value + NRT.Utility.Branding.getBranding(oSearchCriteria.BrandingID);
    },

    getCities: function(rblState) {
      var stateID = 0;
      var regionID = -1;
      var countyID = -1;
      var aCities = null;
      var response = null;
      var arrStates = null;
      var oLabel = null;

      try {
        if (NRT.Utility.objectExists(rblState)) {
          arrStates = NRT.Utility.getElementsByTagNameAndID('rblState', 'INPUT');

          if (arrStates.length > 0) {
            for (i = 0; i < arrStates.length; i += 1) {
              oLabel = arrStates[i].parentNode;
              if (arrStates[i].checked === true) {
                stateID = arrStates[i].value;
                oLabel.style.fontWeight = 'bold';
              } else {
                oLabel.style.fontWeight = '';
              }
            }
          }

          if (stateID === 0) {
            stateID = NRT.Property.Search.State.getStateID();
          }
          NRT.Property.Search.State.setStateID(stateID);
          response = SearchController.GetLocations(stateID, regionID, countyID);
          aCities = response.value.Locations;
          NRT.Property.Search.QuickSearch.fillCityList(aCities);
        }
      }
      catch (err) {
        _oErrorHandler.Error('NRT.Property.Search.QuickSearch.getCities', _oErrorHandler.ERRORTYPE_JS, err);
        return;
      }
    },

    fillCityList: function(aCities) {
      var lstCities = null;
      var i;

      try {
        if (aCities !== null) {
          lstCities = NRT.Utility.getElementByTagNameAndID('ddlLocation', 'SELECT');
          if (lstCities !== null && typeof lstCities !== 'undefined') {
            lstCities.innerHTML = '';

            for (i = 0; i < aCities.length; i += 1) {
              lstCities.options[lstCities.length] = new Option(aCities[i].DisplayName1, aCities[i].LocationID, false, false);
            }
          }
        }
      }
      catch (err) {
        _oErrorHandler.Error('NRT.Property.Search.QuickSearch.fillCityList', _oErrorHandler.ERRORTYPE_JS, err);
        return;
      }
    },

    updateDisplay: function(ctl1) {
      var ddlPropCategoryType = null;
      var propCategoryTypeValue = '';
      var ddlMinPrice = null;
      var ddlMaxPrice = null;
      var ddlRentalMinPrice = null;
      var ddlRentalMaxPrice = null;
      var ddlBeds = null;
      var ddlBaths = null;
      var lblBathrooms = null;
      var lblBedrooms = null;

      ddlBaths = NRT.Utility.getElementByTagNameAndID('ddlBaths', 'SELECT');
      ddlBeds = NRT.Utility.getElementByTagNameAndID('ddlBeds', 'SELECT');
      ddlMaxPrice = NRT.Utility.getElementByTagNameAndID('ddlMaxPrice', 'SELECT');
      ddlMinPrice = NRT.Utility.getElementByTagNameAndID('ddlMinPrice', 'SELECT');
      ddlPropCategoryType = NRT.Utility.getElementByTagNameAndID('ddlPropCategoryType', 'SELECT');
      ddlRentalMaxPrice = NRT.Utility.getElementByTagNameAndID('ddlRentalMaxPrice', 'SELECT');
      ddlRentalMinPrice = NRT.Utility.getElementByTagNameAndID('ddlRentalMinPrice', 'SELECT');
      lblBathrooms = NRT.Utility.getElementByTagNameAndID('lblBathrooms', 'SPAN');
      lblBedrooms = NRT.Utility.getElementByTagNameAndID('lblBedrooms', 'SPAN');

      if (NRT.Utility.objectExists(ddlPropCategoryType)) {
        propCategoryTypeValue = ddlPropCategoryType.options[ddlPropCategoryType.selectedIndex].value;
        if (propCategoryTypeValue.length > 1) {
          propCategoryTypeValue = propCategoryTypeValue.substring(0, 1);
        }

        if (propCategoryTypeValue == '1') {
          if (NRT.Utility.objectExists(ddlBaths)) {
            ddlBaths.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlBeds)) {
            ddlBeds.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlMaxPrice)) {
            ddlMaxPrice.style.display = 'inline';
            ddlMaxPrice.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlMinPrice)) {
            ddlMinPrice.style.display = 'inline';
            ddlMinPrice.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlRentalMaxPrice)) {
            ddlRentalMaxPrice.style.display = 'none';
            ddlRentalMaxPrice.disabled = true;
          }
          if (NRT.Utility.objectExists(ddlRentalMinPrice)) {
            ddlRentalMinPrice.style.display = 'none';
            ddlRentalMinPrice.disabled = true;
          }
          if (NRT.Utility.objectExists(lblBathrooms)) {
            lblBathrooms.disabled = false;
          }
          if (NRT.Utility.objectExists(lblBedrooms)) {
            lblBedrooms.disabled = false;
          }
        } else if (propCategoryTypeValue == '4') {
          if (NRT.Utility.objectExists(ddlBaths)) {
            ddlBaths.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlBeds)) {
            ddlBeds.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlMaxPrice)) {
            ddlMaxPrice.style.display = 'none';
            ddlMaxPrice.disabled = true;
          }
          if (NRT.Utility.objectExists(ddlMinPrice)) {
            ddlMinPrice.style.display = 'none';
            ddlMinPrice.disabled = true;
          }
          if (NRT.Utility.objectExists(ddlRentalMaxPrice)) {
            ddlRentalMaxPrice.style.display = 'inline';
            ddlRentalMaxPrice.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlRentalMinPrice)) {
            ddlRentalMinPrice.style.display = 'inline';
            ddlRentalMinPrice.disabled = false;
          }
          if (NRT.Utility.objectExists(lblBathrooms)) {
            lblBathrooms.disabled = false;
          }
          if (NRT.Utility.objectExists(lblBedrooms)) {
            lblBedrooms.disabled = false;
          }
        } else {
          if (NRT.Utility.objectExists(ddlBaths)) {
            ddlBaths.disabled = true;
          }
          if (NRT.Utility.objectExists(ddlBeds)) {
            ddlBeds.disabled = true;
          }
          if (NRT.Utility.objectExists(ddlMaxPrice)) {
            ddlMaxPrice.style.display = 'inline';
            ddlMaxPrice.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlMinPrice)) {
            ddlMinPrice.style.display = 'inline';
            ddlMinPrice.disabled = false;
          }
          if (NRT.Utility.objectExists(ddlRentalMaxPrice)) {
            ddlRentalMaxPrice.style.display = 'none';
            ddlRentalMaxPrice.disabled = true;
          }
          if (NRT.Utility.objectExists(ddlRentalMinPrice)) {
            ddlRentalMinPrice.style.display = 'none';
            ddlRentalMinPrice.disabled = true;
          }
          if (NRT.Utility.objectExists(lblBathrooms)) {
            lblBathrooms.disabled = true;
          }
          if (NRT.Utility.objectExists(lblBedrooms)) {
            lblBedrooms.disabled = true;
          }
        }
      }
    },

    trapEnter: function(evt) {
      try {
        var charCode = (evt.which) ? evt.which : event.keyCode

        if (charCode == 13) {
          var btnSearch = document.getElementById('btnSearch');
          if (NRT.Utility.objectExists(btnSearch)) {
            NRT.Property.Search.QuickSearch.performSearch();
            return true;
          }
        }
        return false;
      }
      catch (err) {
        _oErrorHandler.Error('NRT.Property.Search.QuickSearch.trapEnter', _oErrorHandler.ERRORTYPE_JS, err);
        return;
      }
    },

    validate: function(validationType) {
      var bResult = true;
      var ddlMinPrice;
      var ddlMaxPrice;
      var ddlRentalMinPrice;
      var ddlRentalMaxPrice;
      var nStateID;
      var divValidation = null;

      try {

        _oValidationXML.clear();

        NRT.Property.Validation.UI.showInvalidMaxPrice(false);
        NRT.Property.Validation.UI.showInvalidMaxRentalPrice(false);

        ddlMinPrice = NRT.Utility.getElementByTagNameAndID('ddlMinPrice', 'SELECT');
        ddlMaxPrice = NRT.Utility.getElementByTagNameAndID('ddlMaxPrice', 'SELECT');
        ddlRentalMinPrice = NRT.Utility.getElementByTagNameAndID('ddlRentalMinPrice', 'SELECT');
        ddlRentalMaxPrice = NRT.Utility.getElementByTagNameAndID('ddlRentalMaxPrice', 'SELECT');

        // Validate standard price range - if min is greater than max, switch the values
        if (NRT.Utility.objectExists(ddlMinPrice) && NRT.Utility.objectExists(ddlMaxPrice) && !ddlMinPrice.disabled) {
          if (parseInt(ddlMaxPrice.value, 10) < parseInt(ddlMinPrice.value, 10)) {
            var maxValue = ddlMaxPrice.value;
            var minValue = ddlMinPrice.value;
            ddlMinPrice.value = maxValue;
            ddlMaxPrice.value = minValue;
          }
        }

        // Validate rental price range - if min is greater than max, switch the values
        if (NRT.Utility.objectExists(ddlRentalMinPrice) && NRT.Utility.objectExists(ddlRentalMaxPrice) && !ddlRentalMinPrice.disabled) {
          if (parseInt(ddlRentalMaxPrice.value, 10) < parseInt(ddlRentalMinPrice.value, 10)) {
            var maxValue = ddlRentalMaxPrice.value;
            var minValue = ddlRentalMinPrice.value;
            ddlRentalMinPrice.value = maxValue;
            ddlRentalMaxPrice.value = minValue;
          }
        }

        divValidation = _oUtility.getElementByTagNameAndID('div_QuickSearch_ValidSummary', 'DIV');

        if (_oValidationXML.count() !== 0) {
          NRT.Property.Validation.Summary.show();
        } else {
          NRT.Property.Validation.Summary.hide(NRT.Property.Validation.TYPE_QUICKSEARCH);
        }

        return bResult;
      }
      catch (err) {
        _oErrorHandler.Error('NRT.Property.Search.QuickSearch.validate', _oErrorHandler.ERRORTYPE_JS, err);
        return;
      }
    }
  };
} ();
 
