var searchForm = new WebForm();
var offerSearch = {
  form: null,
  stateProvinceAutoSet: false,
  countryAutoSet: false,
  initialize: function () {
    offerSearch.form = document.offerSearchForm;
    // location
    yuiDom.get("offerSearchByCity").checked=false;
    yuiDom.get("offerSearchByRegion").checked=false;

    yuiEvent.addListener(yuiDom.get("offerSearchByCity"), "click", offerSearch.location.toggleSearchType, "cityOfferSearchDropdowns");
    yuiEvent.addListener(yuiDom.get("offerSearchByRegion"), "click", offerSearch.location.toggleSearchType, "regionOfferSearchCheckboxes");
    yuiEvent.addListener(yuiDom.get("offerCity"), "focus", offerSearch.location.clearCity);
    yuiEvent.addListener(yuiDom.get("offerCity"), "blur", offerSearch.location.selectStateCountry);
    yuiEvent.addListener(yuiDom.get("offerStateProvince"), "change", offerSearch.location.setCountry);
    yuiEvent.addListener(yuiDom.get("offerCountry"), "change", offerSearch.location.clearState);
    // dates
    mcToday = new Date();
    mcToday = new Date(mcToday.getFullYear(),mcToday.getMonth(),mcToday.getDate());
    mcMaxDisplay = new Date(mcToday.getFullYear()+1,mcToday.getMonth(),mcToday.getDate());
    mcMaxDisplay    = mcMaxDisplay.setDate(mcToday.getDate());

    offerSearch.dateFormatString = dateFormatString;

    offerSearch.arrivalDate = yuiDom.get("offerArrivalDate");
    offerSearch.departureDate = yuiDom.get("offerDepartureDate");

    offerSearch.dates.createCalendar();

    yuiEvent.addListener(yuiDom.get("offerCalendarIcon"), "click", offerSearch.dates.toggleCalendar,yuiDom.get("offerCalendarIcon"));
    yuiEvent.addListener(offerSearch.arrivalDate,'blur',offerSearch.dates.arrivalDate_onBlur);
    yuiEvent.addListener(offerSearch.arrivalDate,'focus',offerSearch.dates.arrivalDate_onFocus);
    yuiEvent.addListener(offerSearch.departureDate,'blur',offerSearch.dates.departureDate_onBlur);
    yuiEvent.addListener(offerSearch.departureDate,'focus',offerSearch.dates.departureDate_onFocus);
    yuiEvent.addListener(yuiDom.get("clearOfferSearch"),'click',offerSearch.clearAll);
  },
  location: {
    toggleSearchType: function(e, elementId) {
      yuiDom.setStyle(yuiDom.get("cityOfferSearchDropdowns"), "display", "none");
      yuiDom.setStyle(yuiDom.get("regionOfferSearchCheckboxes"), "display", "none");
      yuiDom.setStyle(yuiDom.get(elementId), "display", "block");
    },
    clearCity: function(e) {
      yuiDom.get("offerCity").value = "";
      if (offerSearch.stateProvinceAutoSet)
        yuiDom.get("offerStateProvince").selectedIndex = 0;
      if (offerSearch.countryAutoSet)
        yuiDom.get("offerCountry").selectedIndex = 0;
    },
    selectStateCountry: function (e) {
      var a = offerSearch.location.isIn(yuiDom.get("offerCity").value.toLowerCase(), topCities);
      if (a) {
        offerSearch.location.setDropDown(a[1], yuiDom.get("offerStateProvince"));
        offerSearch.location.setDropDown(a[2], yuiDom.get("offerCountry"));
        offerSearch.location.clearState(e);
        offerSearch.stateProvinceAutoSet = true;
        offerSearch.countryAutoSet = true;
      }
    },
    setCountry: function () {
      var c = false;
      if (offerSearch.location.isIn(yuiDom.get("offerStateProvince").value, us_states)) {
        c = "US";
      } else if (offerSearch.location.isIn(yuiDom.get("offerStateProvince").value, ca_prov)) {
        c = "CA";
      } else if (offerSearch.location.isIn(yuiDom.get("offerStateProvince").value, misc)) {
        c = "US";
      }
      if (c) {
        offerSearch.location.setDropDown(c, yuiDom.get("offerCountry"));
      }
      offerSearch.location.clearState();
      offerSearch.stateProvinceAutoSet = false;
    },
    clearState: function (e) {

      if ((yuiDom.get("offerCountry")[yuiDom.get("offerCountry").selectedIndex].value != "US") && (yuiDom.get("offerCountry")[yuiDom.get("offerCountry").selectedIndex].value != "CA")) {
        offerSearch.location.setDropDown("", yuiDom.get("offerStateProvince"));
      }
      offerSearch.stateProvinceAutoSet = false;
      offerSearch.countryAutoSet = false;
    },
    setDropDown: function (v, dd) {
      for (i = 0; i < dd.options.length - 1; i++) {
        if (dd.options[i].value == v) {
          dd.selectedIndex = i;
          return true
        }
      }
      return false;
    },
    isIn: function (value, list) {
      for (var i = 0; i < list.length; i++) {
        var tempArray = list[i].split(":")
        if (tempArray[0] == value) {
          return tempArray;
        }
      }
      return false;
    }
  },
  dates: {
    createCalendar: function() {
      offerSearch.calendar = new multiDisplayCalendar("offerSearchDates", offerSearch.dates.getArrivalDate, offerSearch.dates.getDepartureDate, offerSearch.dates.setDates);
    },
    setDates: function(arrivalDate, departureDate, fromContinue) {
      offerSearch.arrivalDate.value = getFormatedDate(offerSearch.localeCode, arrivalDate);
      offerSearch.departureDate.value = getFormatedDate(offerSearch.localeCode, departureDate);
      if(!fromContinue) {
        offerSearch.dates.closeCalendar();
      }
    },
    getArrivalDate: function() {
      return setDateFromString(offerSearch.localeCode,offerSearch.arrivalDate.value);
    },
    getDepartureDate: function() {
      return setDateFromString(offerSearch.localeCode,offerSearch.departureDate.value);
    },
    toggleCalendar: function(e,button) {
      offerSearch.calendar.show(button,"bottomRight");
      var e = e || window.event;
      e.cancelBubble = true;
      if (e.stopPropagation) e.stopPropagation();
    },
    closeCalendar: function(){
      if(offerSearch.calendar){
        if(offerSearch.calendar.display == "block"){
          offerSearch.calendar.toggleDisplay();
        }
      }
    },

    checkDates: function(f) {	
      if ((offerSearch.arrivalDate.value == offerSearch.dateFormatString) && (offerSearch.departureDate.value == offerSearch.dateFormatString)) {
          return "DATES_NOT_SET";
      }

      var checkIn = new validDate(offerSearch.arrivalDate);
      var checkOut = new validDate(offerSearch.departureDate);

      if (!(checkIn.valid) && (!checkOut.valid) ) {
          searchForm.addError("errorBlock","~ciAnddepartureDatesInvalidError", "arrivalDateLabel","departureDateLabel")
      } else if (!checkIn.valid ) {
          searchForm.addError("errorBlock","~arrivalDateInvalidError","arrivalDateLabel")
      } else if (!checkOut.valid ) {
          searchForm.addError("errorBlock","~departureDateInvalidError","departureDateLabel")
      } else 	if ( (checkIn.valid) & (checkOut.valid)  ) {
          var datesDelta = checkIn.diffDate( checkOut.d);
          var nowDelta = checkIn.diffDate(new Date());
          if (nowDelta > 0 ) {
              searchForm.addError("errorBlock","~checkInEarlierThanTodayError","arrivalDateLabel");
          } else if (nowDelta < -551) {
              searchForm.addError("errorBlock","~bookToFarInFutureError","arrivalDateLabel");
              searchForm.addError()
          } else if (datesDelta < 0) {
              searchForm.addError("errorBlock","~departureBeforeArrivalError","departureDateLabel");
          } else if (datesDelta > 90) {
              searchForm.addError("errorBlock","~maximumLengthStayExceededError", "arrivalDateLabel","departureDateLabel");
          } else if (datesDelta < 1) {
              searchForm.addError("errorBlock","~arrivalEqualsDepartureError", "arrivalDateLabel","departureDateLabel");
          }
      }
      return "";
    },
    arrivalDate_onFocus: function(f) {
        var checkIn = new validDate(offerSearch.arrivalDate);
        if ( !checkIn.valid ) { offerSearch.arrivalDate.value="";}
    },

    arrivalDate_onBlur: function(f) {
      var checkIn = new validDate(offerSearch.arrivalDate);
      if ( checkIn.valid ) {
          checkIn.setField();
          var checkOut = validDate(offerSearch.departureDate);
          if (checkOut.valid ) { before = (checkIn.diffDate(checkOut.d) <= 0);}
          if ( (! (checkOut.valid)) || before) {
              checkOut.setD(checkIn.d);
              checkOut.nextDay();
              checkOut.setField();
          }
      } else {
          offerSearch.arrivalDate.value=dateFormatString;
      }
    },
    departureDate_onFocus: function(f) {
      var checkOut = new validDate(offerSearch.departureDate);
      if ( !checkOut.valid ) { offerSearch.departureDate.value="";}
    },
    departureDate_onBlur: function(f) {
      var checkOut = new validDate(offerSearch.departureDate);
      if ( checkOut.valid ) { checkOut.setField(); } else {offerSearch.departureDate.value=dateFormatString;}
    }
  },
  clearAll: function () {
    yuiDom.get("offerCity").value = "";
    yuiDom.get("offerPromotionCode").value = "";
    yuiDom.get("offerStateProvince").selectedIndex = 0;
    yuiDom.get("offerCountry").selectedIndex = 0;

    offerSearch.arrivalDate.value = offerSearch.dateFormatString;
    offerSearch.departureDate.value = offerSearch.dateFormatString;
    
    var regionCheckboxes = yuiDom.getElementsByClassName("regionCheckbox","input");
    regionCheckboxes.forEach(function(checkbox) {
      checkbox.checked=false;
    });
    var interestCheckboxes = yuiDom.getElementsByClassName("offerInterest","input");
    interestCheckboxes.forEach(function(checkbox) {
      checkbox.checked=false;
    });
  }
}

function validateOffersDates(){
	searchForm.resetErrors();
	offerSearch.dates.checkDates();
	if (searchForm.hasErrors()) {
        searchForm.showErrors();		
        return false;
    } else {
        return true;
    }	
}
