﻿function DoHcSearch(languageCode, target, affiliateId, domain, brandId, label) {
    
    //initializers
    var city;
    var host = "http://www.hotelscombined.com";
    var checkin;
    var checkout;
    var brandParams = "";
    var affiliateParams = "";
    var redirection;
    var defaultCountryCode = "";
    //conditional default values

    if (getObj("citySearch")) {
        city = getObj("citySearch").value;
    }
    if ((city == null || city == "") && getObj("defaultCityName")) {
        city = getObj("defaultCityName").value;
    }
    city = escape(city);
    if (!getObj("hotelCheckin")) {
        var tempCheckin = new Date();
        tempCheckin.setDate(tempCheckin.getDate() + 7); //If no checkin is supplied, use a date in a weeks time
        checkin = formdateDateToString(tempCheckin);
    }
    else {
        checkin = getObj("hotelCheckin").value;
    }
    if (!getObj("hotelCheckout")) {
        var tempCheckout = new Date();
        tempCheckout.setDate(getDate(checkin).getDate() + 7); //If no checkout is supplied, use a week from checkin
        checkout = formdateDateToString(tempCheckout);
    }
    else {
        checkout = getObj("hotelCheckout").value;
    }
    if (getObj("defaultCountryCode")) {
        defaultCountryCode = "&countryCode=" + getObj("defaultCountryCode").value;
    }
    
    if (!ValidateDates(checkin, checkout)) return false;
    if (domain != null && domain != "") {
        host = "http://" + domain;
    }
    else {
        if (brandId != null && brandId != "")
            brandParams = "&brandId=" + brandId;
        affiliateParams = "&a_aid=" + affiliateId;
    }
    
    if (!getObj("selectedFileName") || getObj("selectedFileName").value == "") {
        if (getObj("selectedHotelFileName")) {
            redirection = "/SearchedHotel.aspx?fileName=" + escape(getObj("selectedHotelFileName").value) + "&checkin=" + checkin + "&checkout=" + checkout + "&languageCode=" + languageCode + affiliateParams + brandParams + defaultCountryCode;
        }
        else if (city == null || city.length < 3 || (defaultCityValue != null && escape(defaultCityValue) == city)) {
            alert("Please enter a city name");
            return false;
        }
        else {
            redirection = "/Search.aspx?search=" + city + "&checkin=" + checkin + "&checkout=" + checkout + "&languageCode=" + languageCode + affiliateParams + brandParams + defaultCountryCode;
        }
        
    }
    else {
        redirection = "/SearchResults.aspx?fileName=" + getObj("selectedFileName").value + "&checkin=" + checkin + "&checkout=" + checkout + "&languageCode=" + languageCode + affiliateParams + brandParams;
    }
    
    if (getObj("guests"))
        redirection += "&adults=" + getObj("guests").value;
    if (getObj("rooms"))
        redirection += "&rooms=" + getObj("rooms").value;
    if (label != null && label != "")
        redirection += "&label=" + label;

    redirection = host + redirection;

    switch (target) {
        case "_blank":
            var newWin = window.open(redirection);
            if (newWin != null) { newWin.focus(); }
            break;
        case "_parent":
            window.parent.location = redirection;
            break;
        case "_top":
            window.top.location = redirection;
            break;
        default:
            window.location = redirection;
    }
    return false;
}


$(document).ready(function () {
    addSearchBoxHandlers();
    var city = $("#citySearch");
    if (city) {
        city.removeAttr("disabled");
    }
});
