var initHiddenFields = {
    run : function() {
        if(typeof corporateAccountNumber != 'undefined') {
            document.searchForm.corporateAccountNumber.value = corporateAccountNumber;
        } else
        if(typeof promoCode != 'undefined') {
            document.searchForm.promoCode.value = promoCode;
        } else
        if(typeof ratePlanName != 'undefined') {
            document.searchForm.ratePlanName.value = ratePlanName;
        }

        if(typeof iATANumber != 'undefined') {
            document.searchForm.iATANumber.value = iATANumber;
        }

        if(formAction !== "") {
            document.searchForm.action = formAction;
        }
    }
};

yuiEvent.onDOMReady(initHiddenFields.run);

function writeTermsConditions() {
    var win = window.open("","terms","width=375,height=400,screenX=300,screenY=250,scrollbars=yes,resizable=yes");
    var tandc = document.getElementById("termsconditions");
    win.document.writeln('<html><head><title>Terms &amp; Condition</title><style type=\'text/css\'> *{ font-family: arial, helvetica, sans-serif;} p, ul, ul li { font-size: 10px; } b { font-weight: bold; }</style></head><body>');
    win.document.writeln(tandc.innerHTML);
    win.document.writeln('</body></html>');
}
function citySearchForm_onSubmit() {
	searchForm.resetErrors();

    //city_onBlur();
    checkDates();
    locationStatus = getLocationSearchStatus();
    if ((locationStatus =="BLANK")) {
        searchForm.addError("searchCriteriaRequiredError" );
    }
    if ((locationStatus =="INVALID_US_SEARCH") ) {
        searchForm.addError("toGeneralUSSearchError" );
    }
    if ((locationStatus =="MISSING_STATE_US_SEARCH") ) {
        searchForm.addError("stateProvinceRequiredError" );
    }
    if ((locationStatus =="CITY_ONLY") ) {
        searchForm.addError("countryRequiredForCitySearchError" );
    }
    if (searchForm.hasErrors()) {
        searchForm.showErrors();
        return false;
    }
    else {
        return true;
    }
}

SW.domWidget.add({
    id:"sIFRManager",
    /* every param in config is required */
    selectors: [ /* these params are set from CMS content */ ],
    initialize:function(config) {
        this.selectors.push(config);
    },
    load:function() {
        // do load stuff, find elements and setup
        var locale = SW.tools.Cookie.get("LOCALECOOKIE");
        if (typeof sIFR == "function") {
            if (locale == "ja_JP" || locale == "zh_CN") {
                // this is a hack to remove main sifr class - due to sIFR.js containing sIFR.setup()
                yuiDom.getElementsByClassName("sIFR-hasFlash").forEach(function(el) {
                    yuiDom.removeClass(el, "sIFR-hasFlash");
                });
            } else {
                this.isSupported = true;
                //      sIFR.debug();
                //            sIFR.setup();// DL: removed this call, as I do not think it is needed
                this.setup();
            }
        }
    },
    update:function(root) {
        // do update stuff, find elements and setup. root element is passed in.
        if (self.isSupported) {

        }
    },
    setup: function(root) {
        var self = this;
        if (self.isSupported) {
            self.selectors.forEach(function(config) {
                sIFR.replaceElement(named(config));
            });
        }
    }
});
       
SW.domWidget.add({
    id:"iePNGManager",
/* selectors are id's or HTML Objects to provide setup with a root node,
  instead of traversing the entire dom for the given page.
*/
    selectors: [
            "promoHeader",
            "searchFormContainer"
            ],
    load:function() {
        for (var i = 0; i < this.selectors.length; i++) {
            this.setup(this.selectors[i]);
        }
    },
    setup:function(root) {
        var self = this;
        if (YAHOO.env.ua.ie > 0) { // only > 0 when browser is ie...
            yuiDom.getElementsByClassName("iePngReplace", "div", root).forEach(function(container) {
                if (container.currentStyle.backgroundImage.match(/\.png/i)) {
                    self.applyFilter(container);
                }
            });
        }
    },
    applyFilter : function(obj) {
        var bg = obj.currentStyle.backgroundImage;
        var bgSrc = bg.substring(5, bg.length - 2); // strip 'url("' and '")'
        obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bgSrc + "', sizingMethod='scale')";
        obj.style.backgroundImage = "url(/common/images/shim.gif)";
    }
});

SW.domWidget.dhtmlSelect.setEnabled(true);
SW.domWidget.bodyClickHandler.setEnabled(true);
SW.domWidget.iePNGManager.setEnabled(true);
SW.domWidget.sIFRManager.setEnabled(true);
