var offerSearchForm = new WebForm();
var offerSearch = {
    form: null,
    stateProvinceAutoSet: false,
    countryAutoSet: false,
    initialize: function () {
        offerSearch.form = document.offerSearchForm;

        // location
        if (yuiDom.get("offerSearchByCity")) yuiDom.get("offerSearchByCity").checked = true;
        if (yuiDom.get("offerSearchByRegion")) yuiDom.get("offerSearchByRegion").checked = false;

        yuiEvent.addListener(yuiDom.get("offerSearchByCity"), "click", offerSearch.location.toggleSearchType, "cityOfferSearch");
        yuiEvent.addListener(yuiDom.get("offerSearchByRegion"), "click", offerSearch.location.toggleSearchType, "regionOfferSearch");
        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);
        if (offerSearch.locationSearchType) {
            offerSearch.location.toggleSearchType(null, offerSearch.locationSearchType + "OfferSearch");
            if (offerSearch.locationSearchType == "region") {
                yuiDom.get("offerSearchByCity").checked = false;
                yuiDom.get("offerSearchByRegion").checked = true;
            }
        }
    },
    location: {
        toggleSearchType: function(e, elementId) {
            yuiDom.setStyle(yuiDom.get("cityOfferSearch"), "display", "none");
            yuiDom.setStyle(yuiDom.get("regionOfferSearch"), "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;
        },
        validateLocation: function () {
            if (yuiDom.get("offerSearchByCity").checked) {
                if ((yuiDom.get("offerCity").value == "" || yuiDom.get("offerCity").value == yuiDom.get("offerCity").getAttribute("label")) && yuiDom.get("offerCountry").value == "" && yuiDom.get("offerStateProvince").value == "") {
                    return false;
                }
                return true;
            }
            else {
                var regions = yuiDom.getElementsByClassName("regionCheckbox", "input", yuiDom.get("regionOfferSearchCheckboxes"));
                var regionsChecked = false;
                regions.forEach(function(region) {
                    if (region.checked) {
                        regionsChecked = true;
                    }
                });
                return regionsChecked;
            }
        }
    },
    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, "bottomLeft");
            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();
                }
            }
        },

        validateDates: function(f) {
            if ((offerSearch.arrivalDate.value == offerSearch.dateFormatString) && (offerSearch.departureDate.value == offerSearch.dateFormatString) ||
                (offerSearch.arrivalDate.value == "") && (offerSearch.departureDate.value == "")) {
                return false;
            }
            return true;
        },
        checkDates: function () {
            var checkIn = new validDate(offerSearch.arrivalDate);
            var checkOut = new validDate(offerSearch.departureDate);

            if (!(checkIn.valid) && (!checkOut.valid)) {
                offerSearchForm.addError("errorBlock", "~ciAnddepartureDatesInvalidError", "arrivalDateLabel", "departureDateLabel")

            } else if (!checkIn.valid) {
                offerSearchForm.addError("errorBlock", "~arrivalDateInvalidError", "arrivalDateLabel")
            } else if (!checkOut.valid) {
                offerSearchForm.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) {
                    offerSearchForm.addError("errorBlock", "~checkInEarlierThanTodayError", "arrivalDateLabel");
                } else if (nowDelta < -561) {
                    offerSearchForm.addError("errorBlock", "~bookToFarInFutureError", "arrivalDateLabel");
                    offerSearchForm.addError()
                } else if (datesDelta < 0) {
                    offerSearchForm.addError("errorBlock", "~departureBeforeArrivalError", "departureDateLabel");
                } else if (datesDelta > 31) {
                    offerSearchForm.addError("errorBlock", "~maximumLengthStayExceededError", "arrivalDateLabel", "departureDateLabel");
                } else if (datesDelta < 1) {
                    offerSearchForm.addError("errorBlock", "~arrivalEqualsDepartureError", "arrivalDateLabel", "departureDateLabel");
                }
            }
        },
        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;
            }
        }
    },
    validateInterest: function() {
        var interestCheckboxes = yuiDom.getElementsByClassName("offerInterest", "input");
        var interestChecked = false
        interestCheckboxes.forEach(function(checkbox) {
            if (checkbox.checked) {
                interestChecked = true;
            }
        });
        return interestChecked;
    },
    clearAll: function () {
        if (yuiDom.get("offerCity")) yuiDom.get("offerCity").value = "";
        if (yuiDom.get("offerPromotionCode")) yuiDom.get("offerPromotionCode").value = "";
        if (yuiDom.get("offerStateProvince")) yuiDom.get("offerStateProvince").selectedIndex = 0;
        if (yuiDom.get("offerCountry")) 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;
        });
    },
    validate: function() {
        offerSearchForm.resetErrors();
        if (!offerSearch.location.validateLocation() && !offerSearch.dates.validateDates() && !offerSearch.validateInterest()) {
            offerSearchForm.addError("errorBlock", "searchCriteriaRequiredError");
        }
        if (offerSearch.dates.validateDates()) {
            offerSearch.dates.checkDates();
        }
        if (offerSearchForm.hasErrors()) {
            offerSearchForm.showErrors();
            yuiEvent.stopPropagation(e);
            return false;
        }
        return true;
    }
}
