var findReserveModule = {
    config: null,
    language: null,
    mainContainer: null,
    initCtiiesJA: false,
    initialize: function (config) {
        this.config = config || {hidden: true, toggle:true};
        this.language = config.language;
        this.mainContainer = yuiDom.get("findReserveModule");
        /* initial setup */
        if (this.config.toggle) {
            yuiDom.addClass(this.mainContainer, "animating");
            this.main.animation.initialize();
        }
        this.searchType.animation.initialize();
        this.main.date.initialize();
        this.main.initialize(this.config.loadCountries);
        yuiDom.setStyle(this.mainContainer, "display", "block");

        if (!this.config.hidden && this.config.toggle) {
            setTimeout(findReserveModule.main.animation.toggleBridge, 100);
        }
        // hack for chinese form
        yuiDom.get("frm_destinationSectionContainer").removeChild(this.searchType.animation.allSearchContainer)
        if (this.config.searchType == "all") {
            this.searchType.animation.showAllSearch();
            if (this.config.locale == "ja") {
                initCities();
                this.initCtiiesJA = true;
            }
        }
        else {
            yuiDom.get("frSearchType").value = "dest";
        }
    },
    main: {
        countrySelectContainer: null,
        countrySelect: null,
        stateSelectContainer: null,
        stateSelect: null,
        citySelectContainer: null,
        citySelect: null,
        countriesLoaded: false,
        initialize: function (loadCountries) {
            this.toggleTab = yuiDom.get("findReserveTab");
            this.countrySelectContainer = yuiDom.get("frm_countrySelectContainer");
            this.countrySelect = yuiDom.get("frm_country");
            this.stateSelectContainer = yuiDom.get("frm_stateSelectContainer");
            this.stateSelect = yuiDom.get("frm_state");
            this.citySelectContainer = yuiDom.get("frm_citySelectContainer");
            this.citySelect = yuiDom.get("frm_city");

            yuiEvent.addListener(this.countrySelect, "change", this.getStateOptionsBridge)
            yuiEvent.addListener(this.stateSelect, "change", this.getCityOptionsBridge)

            if (loadCountries) {
                this.getCountryOptions();
            }
            else {
                yuiEvent.addListener(this.toggleTab, "click", this.getCountryOptionsBridge);
            }
        },

        populateSelect: function (xhr) {
            var selectOptions = xhr.responseXML.getElementsByTagName("entry");
            var selectBox = xhr.argument.select;
            selectBox.options.length = 1;
            selectBox.selectedIndex = 0;
            selectBox.options[0].text = xhr.argument.defaultText;
            if (selectOptions.length > 0) {
                selectBox.disabled = false;
                var matched = false;
                for (var i = 0; i < selectOptions.length; i++) {
                    var selected = false;
                    if (xhr.argument.selectedValue && findReserveModule.config.selectedValues[xhr.argument.selectedValue] != "") {
                        selected = (selectOptions[i].getAttribute("key") == findReserveModule.config.selectedValues[xhr.argument.selectedValue]);
                        if (selected) {
                            matched = true;
                        }
                    }
                    var option = new Option(selectOptions[i].firstChild.nodeValue, selectOptions[i].getAttribute("key"), selected);
                    selectBox.options[selectBox.options.length] = option;
                    if (selected) {
                        //                        selectBox.selectedIndex = selectBox.options.length;
                        selectBox.value = selectOptions[i].getAttribute("key");
                    }
                }
                if (matched) {
                    findReserveModule.config.selectedValues[xhr.argument.selectedValue] = "";
                    if (xhr.argument.next) {
                        xhr.argument.next.apply(findReserveModule.main);
                    }
                }
            }
            else if (xhr.argument.next) {
                selectBox.disabled = true;
                xhr.argument.next.apply(findReserveModule.main);
            }
        },
        getCountryOptionsBridge: function () {
            findReserveModule.main.getCountryOptions();
        },
        getCountryOptions: function () {
            if (!this.countriesLoaded) {
                yuiConnect.asyncRequest("GET", "/ajax/countryandstatelocations?localeCode=" + findReserveModule.config.locale + "&brandCode=ST&requestType=COUNTRY&propStatus=Active|Pre-Active",
                {
                    success: findReserveModule.main.populateSelect,
                    argument: {
                        select: this.countrySelect,
                        defaultText: findReserveModule.language.country,
                        selectedValue: "country",
                        next:  findReserveModule.main.getStateOptions
                    }
                }
                    );
                this.countrySelect.options[0].text = findReserveModule.language.loading;
                this.countriesLoaded = true;
            }
        },
        getStateOptionsBridge: function() {
            findReserveModule.main.getStateOptions(this);
        },
        getStateOptions: function () {
            this.stateSelect.disabled = true;
            this.citySelect.disabled = true;
            yuiConnect.asyncRequest("GET", "/ajax/countryandstatelocations?localeCode=" + findReserveModule.config.locale + "&brandCode=ST&requestType=STATE&propStatus=Active|Pre-Active&countryCode=" + this.countrySelect.value,
            {
                success: findReserveModule.main.populateSelect,
                argument: {
                    select: this.stateSelect,
                    defaultText: findReserveModule.language.state,
                    selectedValue: "state",
                    next:  findReserveModule.main.getCityOptions
                }
            }
                );
            this.stateSelect.options[0].text = findReserveModule.language.loading;
        },
        getCityOptionsBridge: function() {
            findReserveModule.main.getCityOptions(this);
        },
        getCityOptions: function () {
            this.citySelect.disabled = true;
            yuiConnect.asyncRequest("GET", "/ajax/countryandstatelocations?localeCode=" + findReserveModule.config.locale + "&brandCode=ST&requestType=CITY&propStatus=Active|Pre-Active&countryCode=" + this.countrySelect.value + "&stateCode=" + (this.stateSelect.selectedIndex > 0 ? this.stateSelect.value : ""),
            {
                success: findReserveModule.main.populateSelect,
                argument: {
                    select: this.citySelect,
                    defaultText: findReserveModule.language.city,
                    selectedValue: "city"
                }
            }
                );
            this.citySelect.options[0].text = findReserveModule.language.loading;
        },
        validateForm: function(e) {
            searchForm.resetErrors();
            if (findReserveModule.searchType.animation.currentHandler == findReserveModule.searchType.animation.allSearchClickHandler) {
                if(findReserveModule.config.locale=="ja"||findReserveModule.config.locale=="zh"){
                     if (!document.simpleForm.country.disabled && document.simpleForm.country.value == "") {
                        searchForm.addError("errorBlock", "findReserve_searchCriteriaRequiredError", "ysearchinputLabel");
                     } else if(!document.simpleForm.country.disabled && document.simpleForm.country.value == "US" && document.simpleForm.stateProvince.value == ""){
                        searchForm.addError("errorBlock", "findReserve_toGeneralUSSearchError", "ysearchinputLabel");
                     }
                }else{
                    if (document.simpleForm.complexSearchField && !document.simpleForm.complexSearchField.disabled && (document.simpleForm.complexSearchField.value == "" || document.simpleForm.complexSearchField.value == document.simpleForm.complexSearchField.getAttribute("label"))) {
                        searchForm.addError("errorBlock", "findReserve_searchCriteriaRequiredError", "ysearchinputLabel");
                    }
                }
            }
            else {
                if (!findReserveModule.main.countrySelect.disabled && findReserveModule.main.countrySelect.value == "") {
                    searchForm.addError("errorBlock", "findReserve_searchCriteriaRequiredError", "ysearchinputLabel");
                }
                else if(!findReserveModule.main.countrySelect.disabled && findReserveModule.main.countrySelect.value == "US" && findReserveModule.main.stateSelect.value == ""){
                    searchForm.addError("errorBlock", "findReserve_toGeneralUSSearchError", "ysearchinputLabel");
                }
            }
            if ((findReserveModule.main.date.arrivalDate.value != "" && findReserveModule.main.date.departureDate.value != "") && (findReserveModule.main.date.arrivalDate.value != dateFormatString || findReserveModule.main.date.departureDate.value != dateFormatString)) {
                var checkIn = new validDate(findReserveModule.main.date.arrivalDate);
                var checkOut = new validDate(findReserveModule.main.date.departureDate);
                var datesDelta = checkIn.diffDate(checkOut.d);
                if (!(checkIn.valid) && (!checkOut.valid)) {
                    searchForm.addError("errorBlock", "findReserve_ciAnddepartureDatesInvalidError", "frm_arrivalDateLabel", "frm_departureDateLabel")
                }
                else if (!checkIn.valid) {
                    searchForm.addError("errorBlock", "findReserve_arrivalDateInvalidError", "frm_arrivalDateLabel")
                }
                else if (!checkOut.valid) {
                    searchForm.addError("errorBlock", "findReserve_departureDateInvalidError", "frm_departureDateLabel")
                }
                else if ((checkIn.valid) & (checkOut.valid)) {
                    var datesDelta = checkIn.diffDate(checkOut.d);
                    var nowDelta = checkIn.diffDate(new Date());
                    if (nowDelta > 0) {

                    }
                    else if (nowDelta < -551) {
                        searchForm.addError("errorBlock", "findReserve_bookToFarInFutureError", "frm_arrivalDateLabel");
                        searchForm.addError()
                    }
                    else if (datesDelta < 0) {
                        searchForm.addError("errorBlock", "findReserve_departureBeforeArrivalError", "frm_departureDateLabel");
                    }
                    else if (datesDelta > 90) {
                        searchForm.addError("errorBlock", "findReserve_maximumLengthStayExceededError", "frm_arrivalDateLabel", "frm_departureDateLabel");
                    }
                    else if (datesDelta < 1) {
                        searchForm.addError("errorBlock", "findReserve_arrivalEqualsDepartureError", "frm_arrivalDateLabel", "frm_departureDateLabel");
                    }
                }
            }
            if (searchForm.hasErrors()) {
                searchForm.showErrors();
                yuiDom.setStyle(yuiDom.get("findReserveErrorBlock"), "display", "block");
                yuiEvent.stopPropagation(e);
                return false
            }
            return true;
        },
        closeError: function(e) {
            yuiDom.setStyle(yuiDom.get("findReserveErrorBlock"), "display", "none");
        },
        date: {
            calendar: null,
            calendarIcon: null,
            locale: null,
            arrivalDate: null,
            departureDate: null,
            initialize: function () {
                this.localeCode = findReserveModule.config.locale;
                this.calendarIcon = yuiDom.get("frm_calendarIcon");
                this.arrivalDate = yuiDom.get("frm_arrivalDate");
                this.departureDate = yuiDom.get("frm_departureDate");
                findReserveModule.main.date.calendar = new multiDisplayCalendar("findReserveCalendar_1", findReserveModule.main.date.getArrivalDateBridge, findReserveModule.main.date.getDepartureDateBridge, findReserveModule.main.date.setDatesBridge, null);
                yuiEvent.addListener(this.arrivalDate, "focus", this.checkArrivalDateFocusBridge);
                yuiEvent.addListener(this.arrivalDate, "blur", this.checkArrivalDateBlurBridge);
                yuiEvent.addListener(this.departureDate, "focus", this.checkDepartureDateFocusBridge);
                yuiEvent.addListener(this.departureDate, "blur", this.checkDepartureDateBlurBridge);
                yuiEvent.addListener(this.calendarIcon, "click", this.toggleCalendarBridge);
            },
            setDatesBridge: function(arrivalDate, departureDate, fromContinue) {
                findReserveModule.main.date.setDates(arrivalDate, departureDate, fromContinue);
            },
            setDates: function(arrivalDate, departureDate, fromContinue) {
                this.arrivalDate.value = getFormatedDate(this.localeCode, arrivalDate);
                yuiDom.removeClass(this.arrivalDate, "hasDefaultText");
                this.departureDate.value = getFormatedDate(this.localeCode, departureDate);
                yuiDom.removeClass(this.departureDate, "hasDefaultText");
                if (!fromContinue) {
                    this.closeCalendar();
                }
            },
            getArrivalDateBridge: function() {
                return findReserveModule.main.date.getArrivalDate();
            },
            getDepartureDateBridge: function() {
                return findReserveModule.main.date.getDepartureDate();
            },
            getArrivalDate: function() {
                return setDateFromString(this.localeCode, this.arrivalDate.value);
            },
            getDepartureDate: function() {
                return setDateFromString(this.localeCode, this.departureDate.value);
            },
            checkDatesBridge: function(e) {
                findReserveModule.main.date.checkDates(this);
            },
            checkDates: function(form) {
                if ((this.arrivalDate.value == this.dateFormatString) && (this.departureDate.value == this.dateFormatString)) {
                    return "DATES_NOT_SET";
                }

                var checkIn = new validDate(this.arrivalDate);
                var checkOut = new validDate(this.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 "";
            },
            checkArrivalDateFocusBridge: function(e) {
                findReserveModule.main.date.checkArrivalDateFocus(e);
            },
            checkArrivalDateFocus: function(e) {
                if (this.arrivalDate.value == dateFormatString) {
                    this.arrivalDate.value = "";
                    return;
                }
                var checkIn = new validDate(this.arrivalDate);
                if (!checkIn.valid) {
                    this.arrivalDate.value = "";
                }
                return;
            },
            checkArrivalDateBlurBridge: function(e) {
                findReserveModule.main.date.checkArrivalDateBlur(this);
            },
            checkArrivalDateBlur: function(f) {
                var checkIn = new validDate(this.arrivalDate);
                if (checkIn.valid) {
                    checkIn.setField();
                    var isValid = true;
                    if (this.departureDate.value == dateFormatString) {
                        isValid = false;
                    }
                    var checkOut = validDate(this.departureDate);
                    var before;
                    if (!isValid) {
                        this.departureDate.value = dateFormatString;
                    }
                    if (checkOut.valid) {
                        before = (checkIn.diffDate(checkOut.d) <= 0);
                    }

                    if (this.departureDate.value == dateFormatString) {
                        checkOut.valid = false;
                    }
                    if ((! (checkOut.valid)) || before) {
                        checkOut.setD(checkIn.d);
                        checkOut.nextDay();
                        checkOut.setField();
                        yuiDom.removeClass(this.departureDate, "hasDefaultText");
                    }
                } else {
                    this.arrivalDate.value = dateFormatString;
                }
            },
            checkDepartureDateFocusBridge: function (e) {
                findReserveModule.main.date.checkDepartureDateFocus(this);
            },
            checkDepartureDateFocus: function(input) {
                if (this.departureDate.value == dateFormatString) {
                    this.departureDate.value = "";
                    return;
                }
                var checkOut = new validDate(this.departureDate);
                if (!checkOut.valid) {
                    this.departureDate.value = "";
                }
            },
            departureDate_onBlur: function(f) {
                var checkOut = new validDate(this.departureDate);
                if (checkOut.valid) {
                    checkOut.setField();
                } else {
                    this.departureDate.value = dateFormatString;
                }
            },

            toggleCalendarBridge: function(e) {
                findReserveModule.main.date.toggleCalendar(e, this)
            },
            toggleCalendar: function(e, button) {
                var IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
                var coords = yuiDom.getXY(button);
                var calendarPos = (coords[0] + 20) + "," + (coords[1] - 300);
                if (!IE7 && findReserveModule.config.calendarPos && findReserveModule.config.calendarPos != "") {
                    calendarPos = findReserveModule.config.calendarPos;
                }
                //alert(calendarPos);
                this.calendar.show(button, calendarPos);
                var e = e || window.event;
                e.cancelBubble = true;
                if (e.stopPropagation) e.stopPropagation();
            },
            closeCalendar: function() {
                if (this.calendar) {
                    if (this.calendar.display == "block") {
                        this.calendar.toggleDisplay();
                    }
                }
            }

        },
        animation: {
            formAnimIn: null,
            formAnimOut: null,
            logoAnimIn: null,
            logoAnimOut: null,
            toggleTab: null,
            formHidden: null,
            animating: false,
            logoTopPosition: null,
            initialize: function (formHidden) {
                this.formHidden = formHidden || true;

                this.toggleTab = yuiDom.get("findReserveTab");
                yuiEvent.addListener(this.toggleTab, "click", this.toggleBridge);
                this.logoTopPosition = yuiDom.getStyle(yuiDom.get("logoContainer"), "top").split("px")[0];
                /* animation to show the find & reserver form while shrink the site logo*/
                this.formAnimIn = new YAHOO.util.Anim("findReserveModule", {height: {to:110}}, .6, YAHOO.util.Easing.easeIn);
                this.logoAnimOut = new YAHOO.util.Anim("logoContainer", {top: {to:-132}}, .2, YAHOO.util.Easing.easeIn);
                this.formAnimIn.onStart.subscribe(function() {
                    yuiDom.addClass(findReserveModule.mainContainer, "frm_findReserveBorder");
                    yuiDom.addClass(findReserveModule.main.animation.toggleTab, "hideBorderBottom");
                });
                this.logoAnimOut.onComplete.subscribe(function() {
                    findReserveModule.main.animation.formAnimIn.animate();
                });
                this.formAnimIn.onComplete.subscribe(function() {
                    yuiDom.removeClass(findReserveModule.mainContainer, "animating");
                    findReserveModule.main.animation.animating = false;
                });

                /* animation to show the find & reserver form while expand the site logo*/
                this.formAnimOut = new YAHOO.util.Anim("findReserveModule", {height: {to:0}}, 1, YAHOO.util.Easing.easeIn);
                this.logoAnimIn = new YAHOO.util.Anim("logoContainer", {top: {to:findReserveModule.main.animation.logoTopPosition}}, 1, YAHOO.util.Easing.easeIn);
                this.formAnimOut.onStart.subscribe(function() {
                    yuiDom.addClass(findReserveModule.mainContainer, "animating");
                });
                this.formAnimOut.onComplete.subscribe(function() {
                    findReserveModule.main.animation.logoAnimIn.animate();
                });
                this.formAnimOut.onComplete.subscribe(function() {
                    yuiDom.removeClass(findReserveModule.main.animation.toggleTab, "hideBorderBottom");
                    yuiDom.removeClass(findReserveModule.mainContainer, "frm_findReserveBorder");
                });
                this.logoAnimIn.onComplete.subscribe(function() {
                    findReserveModule.main.animation.animating = false;
                });
            },
            toggleBridge: function(el) {
                findReserveModule.main.animation.toggle();
            },
            toggle: function () {
                if (this.formHidden) {
                    this.show();
                }
                else {
                    this.hide();
                }
            },
            show: function() {
                if (!this.animating) {
                    this.animating = true;
                    this.logoAnimOut.animate();
                    this.formHidden = false;
                }
            },
            hide: function() {
                if (!this.animating) {
                    this.animating = true;
                    this.formAnimOut.animate();
                    this.formHidden = true;
                }
            }
        }

    },
    searchType: {
        animation: {
            fadeDivAnimIn: null,
            fadeDivAnimOut: null,
            lcSearchContainer: null,
            lcSearchClickHandler: null,
            allSearchContainer: null,
            allSearchClickHandler: null,
            lcSearchAnimIn: null,
            lcSearchAnimOut: null,
            lcClickHandlerAnimIn: null,
            lcClickHandlerAnimOut: null,
            allSearchAnimIn: null,
            allSearchAnimOut: null,
            allClickHandlerAnimIn: null,
            allClickHandlerAnimOut: null,
            form: null,
            initialize: function () {
                this.form = yuiDom.get("frm_findReserveForm");
                /* handler animation setup */
                this.lcSearchContainer = yuiDom.get("frm_LCSearch");
                this.lcSearchClickHandler = yuiDom.get("frm_lcSearchHandler");
                this.currentHandler = this.lcSearchClickHandler;
                yuiEvent.addListener(this.lcSearchClickHandler, "click", this.toggleBridge);

                this.allSearchClickHandler = yuiDom.get("frm_allSearchHandler");
                this.allSearchContainer = yuiDom.get("frm_AllSWSearch");
                yuiDom.setStyle(this.allSearchContainer, "opacity", 0);
                yuiDom.setStyle(this.allSearchContainer, "display", "none");
                yuiEvent.addListener(this.allSearchClickHandler, "click", this.toggleBridge);
                
                this.lcClickHandlerAnimIn = new YAHOO.util.ColorAnim("frm_lcSearchHandler", {color: { to: "#333333" }}, .5, YAHOO.util.Easing.easeIn);
                this.allClickHandlerAnimOut = new YAHOO.util.ColorAnim("frm_allSearchHandler", {color: { to: "#666666" }}, .5, YAHOO.util.Easing.easeOut);
                this.lcClickHandlerAnimIn.onStart.subscribe(function() {
                    findReserveModule.searchType.animation.allClickHandlerAnimOut.animate();
                });

                this.allClickHandlerAnimIn = new YAHOO.util.ColorAnim("frm_allSearchHandler", {color: { to: "#333333" }}, .5, YAHOO.util.Easing.easeIn);
                this.lcClickHandlerAnimOut = new YAHOO.util.ColorAnim("frm_lcSearchHandler", {color: { to: "#666666" }}, .5, YAHOO.util.Easing.easeOut);
                this.allClickHandlerAnimIn.onStart.subscribe(function() {
                    findReserveModule.searchType.animation.lcClickHandlerAnimOut.animate();
                });

                /** end handler animation setup **/

                /* form animation setup */
                this.fadeDiv = yuiDom.get("frm_fadeDiv");
                this.fadeFrame = yuiDom.get("frm_fadeFrame");
                yuiDom.setStyle(this.fadeDiv, "display", "none");
                yuiDom.setStyle(this.fadeFrame, "display", "none");
                this.fadeDivAnimIn = new YAHOO.util.Anim("frm_fadeDiv", {height: {to:63}}, .5, YAHOO.util.Easing.easeIn);
                this.fadeDivAnimOut = new YAHOO.util.Anim("frm_fadeDiv", {height: {to: 0}}, .5, YAHOO.util.Easing.easeIn);
                this.fadeFrameAnimIn = new YAHOO.util.Anim("frm_fadeFrame", {height: {to:63}}, .5, YAHOO.util.Easing.easeIn);
                this.fadeFrameAnimOut = new YAHOO.util.Anim("frm_fadeFrame", {height: {to: 0}}, .5, YAHOO.util.Easing.easeIn);


                /* searchtype animation setup */
                this.lcSearchAnimIn = new YAHOO.util.Anim("frm_LCSearch", {opacity: {to:1}}, .5, YAHOO.util.Easing.easeIn);
                this.allSearchAnimOut = new YAHOO.util.Anim("frm_AllSWSearch", {opacity: {to:0}}, .5, YAHOO.util.Easing.easeOut);
                this.lcSearchAnimIn.onStart.subscribe(function() {
                    yuiDom.setStyle(findReserveModule.searchType.animation.allSearchContainer, "display", "none");
                    yuiDom.setStyle(findReserveModule.searchType.animation.lcSearchContainer, "display", "block");
                });
                this.lcSearchAnimIn.onTween.subscribe(function() {
                    yuiDom.addClass(findReserveModule.searchType.animation.lcSearchClickHandler, "frm_FormOpened");
                    yuiDom.removeClass(findReserveModule.searchType.animation.lcSearchClickHandler, "frm_FormClosed");
                    yuiDom.removeClass(findReserveModule.searchType.animation.allSearchClickHandler, "frm_FormOpened");
                    yuiDom.addClass(findReserveModule.searchType.animation.allSearchClickHandler, "frm_FormClosed");
                    findReserveModule.searchType.animation.allSearchAnimOut.animate();
                });

                this.allSearchAnimIn = new YAHOO.util.Anim("frm_AllSWSearch", {opacity: {to:1}}, .5, YAHOO.util.Easing.easeIn);
                this.lcSearchAnimOut = new YAHOO.util.Anim("frm_LCSearch", {opacity: {to:0}}, .5, YAHOO.util.Easing.easeOut);
                this.allSearchAnimIn.onStart.subscribe(function() {
                    yuiDom.setStyle(findReserveModule.searchType.animation.allSearchContainer, "display", "block");
                    yuiDom.setStyle(findReserveModule.searchType.animation.lcSearchContainer, "display", "none");
                });
                this.allSearchAnimIn.onTween.subscribe(function() {
                    yuiDom.addClass(findReserveModule.searchType.animation.lcSearchClickHandler, "frm_FormClosed");
                    yuiDom.removeClass(findReserveModule.searchType.animation.lcSearchClickHandler, "frm_FormOpened")
                    yuiDom.removeClass(findReserveModule.searchType.animation.allSearchClickHandler, "frm_FormClosed");
                    yuiDom.addClass(findReserveModule.searchType.animation.allSearchClickHandler, "frm_FormOpened");
                    findReserveModule.searchType.animation.lcSearchAnimOut.animate();
                });
                /* end form animation setup */
                this.fadeDivAnimIn.onStart.subscribe(function() {
                    yuiDom.setStyle(findReserveModule.searchType.animation.fadeDiv, "display", "block");
                });
                this.fadeDivAnimOut.onComplete.subscribe(function() {
                    yuiDom.setStyle(findReserveModule.searchType.animation.fadeDiv, "display", "none");
                });

                this.fadeDivAnimIn.onComplete.subscribe(function() {
                    if (findReserveModule.searchType.animation.currentHandler == findReserveModule.searchType.animation.lcSearchClickHandler) {
                        findReserveModule.searchType.animation.lcSearchAnimIn.animate();
                    }
                    else {
                        findReserveModule.searchType.animation.allSearchAnimIn.animate();
                    }
                    findReserveModule.searchType.animation.fadeDivAnimOut.animate();
                });

                this.fadeFrameAnimIn.onStart.subscribe(function() {
                    yuiDom.setStyle(findReserveModule.searchType.animation.fadeFrame, "display", "block");
                });
                this.fadeFrameAnimIn.onComplete.subscribe(function() {                    
                    findReserveModule.searchType.animation.fadeFrameAnimOut.animate();
                });
                this.fadeFrameAnimOut.onComplete.subscribe(function() {
                    yuiDom.setStyle(findReserveModule.searchType.animation.fadeDiv, "display", "none");
                });
            },
            toggleBridge: function () {
                findReserveModule.searchType.animation.toggle(this);
            },
            toggle: function(handler) {
                if (handler == this.lcSearchClickHandler && this.currentHandler != this.lcSearchClickHandler) {
                    this.showLCSearch();
                }
                else if (handler == this.allSearchClickHandler && this.allSearchClickHandler != this.currentHandler) {
                    this.showAllSearch();
                }
            },
            showLCSearch: function() {
            	this.currentHandler = this.lcSearchClickHandler;
                this.form.action = findReserveModule.config.formAction.lc;
                this.fadeFrameAnimIn.animate();
                this.fadeDivAnimIn.animate();
                this.lcClickHandlerAnimIn.animate();
                this.allClickHandlerAnimOut.animate();
                yuiDom.get("frm_destinationSectionContainer").replaceChild(this.lcSearchContainer, this.allSearchContainer)
                yuiDom.get("frSearchType").value = "dest";
                findReserveModule.main.closeError();
            },
            showAllSearch: function() {                
                this.currentHandler = this.allSearchClickHandler;
                this.form.action = findReserveModule.config.formAction.spg;
                this.fadeFrameAnimIn.animate();
                this.fadeDivAnimIn.animate();
                this.allClickHandlerAnimIn.animate();
                this.lcClickHandlerAnimOut.animate();
                yuiDom.get("frm_destinationSectionContainer").replaceChild(this.allSearchContainer, this.lcSearchContainer)
                yuiDom.get("frSearchType").value = "all";
                if (findReserveModule.config.locale == "ja" && !findReserveModule.initCtiiesJA) {
                    initCities();
                    findReserveModule.initCtiiesJA = true;
                }
                findReserveModule.main.closeError();
            }
        }
    }
}

var stateProvinceAutoSet = true;
var countryAutoSet = true;
var searchForm = new WebForm();

function searchForm_onSubmit() {
    city_onBlur();
    searchForm.resetErrors();
    dateStatus = checkDates();

    if (document.searchForm.searchType.value != "favorite") {
        checkLocationFields();
        searchForm.collapseErrors("multipleError");
    } else if (document.searchForm.searchType.value == "favorite") {
        checkFavoriteSearch(dateStatus);
        searchForm.collapseErrors("datesAndPropertyRquiredError");
    }

    if (searchForm.hasErrors()) {
        searchForm.showErrors();
        return false;
    } else {
        return true;
    }
}
function checkFavoriteSearch(dateStatus) {
    if (document.simpleForm.favoritePropertyID.value < 1) {
        searchForm.addError("~favoritePropertyNotSelectedError", "favoriteHotelLabel");
    }
    if (dateStatus == "DATES_NOT_SET") {
        searchForm.addError("~datesAreRequiredError", "arrivalDateLabel", "departureDateLabel");
    }
}
function checkLocationFields() {
    locationStatus = getLocationStatus();
    zipCodeStatus = getZipCodeStatus();
    addressEntered = (document.simpleForm.address.value != "")
    isAddressSearch = (document.simpleForm.searchType.value == "address")
    if (!isAddressSearch) {
        if (locationStatus == "BLANK") {
            searchForm.addError("~searchCriteriaRequiredError", "cityLabel", "stateProvinceError", "countryError");
            return;
        }
        if (locationStatus == "CITY_ONLY") {
            searchForm.addError("~countryRequiredForCitySearchError", "countryError");
            return;
        }
        if (locationStatus == "INVALID_US_SEARCH") {
            searchForm.addError("~toGeneralUSSearchError", "stateProvinceError");
            return;
        }
    } else {
        if (zipCodeStatus == "INVALID") {
            searchForm.addError("~invalidZipCodeError", "zipLabel");
            return;
        }
        if (zipCodeStatus == "VALID") {
            return;
        }
        if ((locationStatus == "INVALID_US_SEARCH") && (!addressEntered)) {
            searchForm.addError("~searchCriteriaRequiredError", "cityLabel", "stateProvinceError", "zipLabel", "addressLabel");
            return;
        }
        if (addressEntered) {
            if ((document.simpleForm.city.value == "") && (document.simpleForm.stateProvince.value == "")) {
                searchForm.addError("~cityAndStateRequiredError", "cityLabel", "stateProvinceError");
                return;
            } else if (locationStatus == "STATE_SEARCH") {
                searchForm.addError("~cityRequiredError", "cityLabel");
                return;
            } else if (locationStatus == "INVALID_US_SEARCH") {
                searchForm.addError("~stateRequiredError", "stateProvinceError");
                return;
            }
        }

    }
}
function getLocationStatus() {
    cityEmpty = (document.simpleForm.city.value == "");
    stateProvinceEmpty = (document.simpleForm.stateProvince.value == "");
    countryEmpty = (document.simpleForm.country.value == "");
    countryUS = (document.simpleForm.country.value == "US");

    if ((!cityEmpty) && (!stateProvinceEmpty) && (!countryEmpty)) {
        return "COMPLETE_SEARCH";
    }
    if ((!stateProvinceEmpty) && (!countryEmpty)) {
        return "STATE_SEARCH";
    }
    if ((!cityEmpty) && (!countryUS) && (!countryEmpty)) {
        return "NON_US_CITY_SEARCH";
    }
    if ((!countryEmpty) && (!countryUS)) {
        return "NON_US_COUNTRY_SEARCH";
    }
    if ((stateProvinceEmpty) && (countryUS)) {
        return "INVALID_US_SEARCH";
    }
    if ((!cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) {
        return "CITY_ONLY";
    }
    if ((cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) {
        return "BLANK";
    }
    return "INCOMPLETE";
}
function getZipCodeStatus() {
    var objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
    if (document.simpleForm.postalCode.value == '') {
        return "BLANK";
    } else if (!objRegExp.test(document.simpleForm.postalCode.value)) {
        return "INVALID";
    } else {
        return "VALID"
    }
}

function checkDates() {
    if ((document.simpleForm.arrivalDate.value == dateFormatString) && (document.simpleForm.departureDate.value == dateFormatString)) {
        return "DATES_NOT_SET";
    }

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

    if (!(checkIn.valid) && (!checkOut.valid)) {
        searchForm.addError("~ciAnddepartureDatesInvalidError", "arrivalDateLabel", "departureDateLabel")
    } else if (!checkIn.valid) {
        searchForm.addError("~arrivalDateInvalidError", "arrivalDateLabel")
    } else if (!checkOut.valid) {
        searchForm.addError("~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("~checkInEarlierThanTodayError", "arrivalDateLabel");
        } else if (nowDelta < -551) {
            searchForm.addError("~bookToFarInFutureError", "arrivalDateLabel");
            searchForm.addError()
        } else if (datesDelta < 0) {
            searchForm.addError("~departureBeforeArrivalError", "departureDateLabel");
        } else if (datesDelta > 90) {
            searchForm.addError("~maximumLengthStayExceededError", "arrivalDateLabel", "departureDateLabel");
        } else if (datesDelta < 1) {
            searchForm.addError("~arrivalEqualsDepartureError", "arrivalDateLabel", "departureDateLabel");
        }
    }
    return "";
}


function city_onFocus() {
    document.simpleForm.city.value = "";
    if (stateProvinceAutoSet) {
        f.stateProvince.selectedIndex = 0;
    }
    if (countryAutoSet) {
        f.country.selectedIndex = 0;
    }
}

function city_onBlur() {
    a = isIn(document.simpleForm.city.value.toLowerCase(), topCities)
    if (a != false) {
        setDropDown(a[1], document.simpleForm.stateProvince);
        setDropDown(a[2], document.simpleForm.country);
        country_onChange();
        stateProvinceAutoSet = true;
        countryAutoSet = true;
    }
}

function stateProvince_onChange() {
    c = false;
    if (isIn(document.simpleForm.stateProvince.value, us_states)) {
        c = "US";
    } else if (isIn(document.simpleForm.stateProvince.value, ca_prov)) {
        c = "CA";
    } else if (isIn(document.simpleForm.stateProvince.value, misc)) {
        c = "US";
    }
    if (c != false) {
        setDropDown(c, document.simpleForm.country);
    }
    country_onChange();
    stateProvinceAutoSet = false;
}
function country_onChange() {
    var f = document.simpleForm;
    if (f.searchType.value == "address") {
        setDropDown("US", document.simpleForm.country);
    }
    if ((f.country[f.country.selectedIndex].value != "US") && (f.country[f.country.selectedIndex].value != "CA")) {
        setDropDown("", document.simpleForm.stateProvince);
    }
    //	if (f.country[f.country.selectedIndex].value == "US") {
    //		f.rateRange.disabled = false;
    //	} else {
    //		f.rateRange.disabled = true;
    //		f.rateRange.selectedIndex = 0;
    //	}
    stateProvinceAutoSet = false;
    countryAutoSet = false;
}
function rateRange_onChange() {
    var f = document.simpleForm;
    if (f.country[f.country.selectedIndex].value == "US") {
        f.rateRange.disabled = false;
    } else {
        f.rateRange.disabled = true;
        f.rateRange.selectedIndex = 0;
    }
}

function setDropDown(v, dd) {
    for (i = 0; i < dd.options.length - 1; i++) {
        if (dd.options[i].value == v) {
            dd.selectedIndex = i;
            return true
        }
    }
    return false;
}

function isIn(elt, list) {
    for (var i = 0; i < list.length; i++) {
        var tempArray = list[i].split(":")
        if (tempArray[0] == elt) {
            return tempArray;
        }
    }
    return false;
}

function searchTab_onClick(searchAction, isLoggedIn) {
    searchForm.resetErrors();
    document.simpleForm.searchAction.value = searchAction;
    var t = (searchAction == 'findRooms')
    document.getElementById('rightSearchTab').className = t ? "faded" : "selected";
    document.getElementById('leftSearchTab').className = t ? "selected" : "faded";
    document.getElementById('roomInfo').style.display = t ? "" : "none";

    document.getElementById('redeemLoggedOut').style.display = (!isLoggedIn && (!t)) ? "block" : "none";
    document.getElementById('standardForm').style.display = (! (!isLoggedIn && (!t))) ? "block" : "none";
}

function arrivalDate_onFocus() {
    var checkIn = new validDate(document.simpleForm.arrivalDate);
    if (!checkIn.valid) {
        document.simpleForm.arrivalDate.value = "";
    }
}

function arrivalDate_onBlur() {
    var checkIn = new validDate(document.simpleForm.arrivalDate);
    if (checkIn.valid) {
        checkIn.setField();
        var checkOut = validDate(document.simpleForm.departureDate);
        if (checkOut.valid) {
            before = (checkIn.diffDate(checkOut.d) <= 0);
        }
        if ((! (checkOut.valid)) || before) {
            checkOut.setD(checkIn.d);
            checkOut.nextDay();
            checkOut.setField();
        }
    } else {
        document.simpleForm.arrivalDate.value = dateFormatString;
    }
}
function departureDate_onFocus() {
    var checkOut = new validDate(document.simpleForm.departureDate);
    if (!checkOut.valid) {
        document.simpleForm.departureDate.value = "";
    }
}
function departureDate_onBlur() {
    var checkOut = new validDate(document.simpleForm.departureDate);
    if (checkOut.valid) {
        checkOut.setField();
    } else {
        document.simpleForm.departureDate.value = dateFormatString;
    }
}

function favorites_onBlur() {
}

function favorites_onClick(id, t) {
    document.simpleForm.favoritePropertyID.value = id;
    var nodes = document.getElementById('favoriteHotels').getElementsByTagName('A');
    for (var x = 0; x < nodes.length; x++) {
        nodes[x].className = "";
    }
    t.className = "selectedFavorite";
    document.simpleForm.arrivalDate.focus();
}

function searchType_onClick(searchType) {
    searchForm.resetErrors();
    document.simpleForm.searchType.value = searchType;
    document.getElementById('cityToggle').className = (searchType == "city") ? "toggleHREFSelected" : "toggleHREF";
    document.getElementById('addressToggle').className = (searchType == "address") ? "toggleHREFSelected" : "toggleHREF";
    document.getElementById('favoritesToggle').className = (searchType == "favorite") ? "toggleHREFSelected" : "toggleHREF";
    var byF = (searchType == "favorite")
    document.getElementById('byLocationBlock').style.display = byF ? "none" : "block";
    document.getElementById('byFavoriteBlock').style.display = byF ? "block" : "none";
    if (!byF) {
        var t = (searchType == "city");
        document.getElementById('byLocationBlock').style.display = "none";
        document.getElementById('addressBlock').style.display = t ? "none" : "block";
        document.getElementById('city').style.width = t ? "200px" : "60px";
        document.getElementById('zipCode').style.display = t ? "none" : "block";
        if (!t) {
            document.simpleForm.country.selectedIndex = 1;
        }
        document.getElementById('byLocationBlock').style.display = "block";
        rateRange_onChange();
    } else {
        f.rateRange.disabled = true;
        f.rateRange.selectedIndex = 0;
    }
}
function advancedSearch_onClick() {
    document.simpleForm.searchAction.value = "advanced";
    if (document.simpleForm.sendRedirect.value != null) {
        document.simpleForm.sendRedirect.value = "true";
    }
    f.submit();
}


