//function for all search boxes without autocomplete city field
function DoSearch(containerId, options) {
    
    var calendar1Id = containerId + '_Checkin';
    var calendar2Id = containerId + '_Checkout';
    var checkin = document.getElementById(calendar1Id).value
    var checkout = document.getElementById(calendar2Id).value

    var guestsEl = document.getElementById(containerId + "_GuestsValue");
    var roomsEl = document.getElementById(containerId + "_RoomsValue");
    
    if (gSearching) return false;
    options = options || {};

    //validate date
//    if (checkin == 0 || checkout == 0) {
//        alert(typeof (JavaScriptEnterCheckinCheckout) == 'undefined' ? 'Please enter your checkin and checkout date.' : JavaScriptEnterCheckinCheckout)
//        return false
//    }
    if (!ValidateCalendarDates(calendar1Id, calendar2Id))
        return false;
   
     if (!ValidateGuestsRooms(guestsEl, roomsEl))
       return false;

   if (typeof(hotelId) != "undefined" && hotelId != 0 && hotelId != null) {
      //legacy global variable
       options.hotelId = hotelId;
   }
    if (options.hotelId == null )
        options.hotelId = 0;

    var redirection = ''; 
    //when entered city is not from autocomplete and doesn't match file name - go to search
    if (options.city != null && options.city != options.cityFileName) {
        redirection = "/Search.aspx?search=" + encodeURIComponent(options.city);
    }
    else {
        if (options.cityFileName.length < 3) {
            alert(typeof (JavaScriptEnterCityName) == 'undefined' ? 'Please enter a city name that is at least 3 characters in length' : JavaScriptEnterCityName);
            return false;
        }
        options.locationId = encodeURIComponent(options.locationId);
        redirection = "http://www.book.iwax.com/SearchResults.aspx?fileName=" + options.cityFileName;
    }
    
    if (options.changeClass && options.searchButton) {
        $(options.searchButton).removeClass(options.changeClass[0]).addClass(options.changeClass[1]);
    }

    checkinValue = formatDate(Date.fromString(document.getElementById(calendar1Id + "Value").value, ShortDatePatternVariable), DefaultShortDatePatternVariable);
    checkoutValue = formatDate(Date.fromString(document.getElementById(calendar2Id + "Value").value, ShortDatePatternVariable), DefaultShortDatePatternVariable);
 
    redirection += "&checkin=" + checkinValue;
    redirection += "&checkout=" + checkoutValue;
    redirection += "&currencyCode=" + options.currencyCode;
    redirection += "&languageCode=" + options.languageCode;
    redirection += "&Adults=" + guestsEl.value;
    redirection += "&Rooms=" + roomsEl.value;
    redirection += "&HotelID=" + options.hotelId;
    // rebel mod
    redirection += "&Label=" + options.Label;
    if (options.redirect == "1") {
        redirection += "&redirect=1";
    }
      
    gSearching = true;

    //window.setTimeout(function() {window.location = redirection;}, 10);
    HC.Common.Navigation.gotoPage(redirection);

    return false;
}

function setGuestValue(containerId, select) {
    document.getElementById(containerId + '_GuestsValue').value = select.options[select.selectedIndex].value;
    return false;
}

function setRoomValue(containerId, select) {

    if (select.value == "5") {
        select.selectedIndex = 0;
        var r = confirm(typeof (JavaScriptSearchMoreRoom) == 'undefined' ? 'We only support searching for up to four rooms at once, would you like to use our partner site Hotelplanner.com to search for more rooms?' : JavaScriptSearchMoreRoom);
        if (r == true) {
            window.open("/ProviderRedirect.aspx?key=" + gHotelPlannerLink, "_self");
            return false;
        }
    }
    else {
        document.getElementById(containerId + '_RoomsValue').value = select.options[select.selectedIndex].value;
    }
    return false;
}

// validate dates
function ValidateCalendarDates(calendar1Id, calendar2Id, allowEmptyDates) {
    var value1 = document.getElementById(calendar1Id + 'Value').value;
    var value2 = document.getElementById(calendar2Id + 'Value').value;

    var valueInInput1 = document.getElementById(calendar1Id ).value;
    var valueInInput2 = document.getElementById(calendar2Id ).value;

    var atLeastOneDateDisplayed = (valueInInput1  != ''  || valueInInput2 != '');
    var atLeastOneValueSet = ((value1 != 0 || value1 != '') || (value2 != 0 || value2 != ''));

    var anyDateEmpty = (value1 == 0 || value1 == '' || value2 == 0 || value2 == '');
    if (allowEmptyDates && atLeastOneDateDisplayed && anyDateEmpty) {
        alert(typeof (JavaScriptEnterCheckinCheckout) == 'undefined' ? 'Please enter your checkin and checkout date.' : JavaScriptEnterCheckinCheckout)
        return false
    }
    else if (!allowEmptyDates && anyDateEmpty) {
        alert(typeof (JavaScriptEnterCheckinCheckout) == 'undefined' ? 'Please enter your checkin and checkout date.' : JavaScriptEnterCheckinCheckout)
        return false
    }
    else if (allowEmptyDates && !atLeastOneDateDisplayed ) {
        return true;
    }
    var inDate = Date.fromString(value1, ShortDatePatternVariable);
    var outDate = Date.fromString(value2, ShortDatePatternVariable);
    var currentDate = new Date();

    //validate checkin - checkout difference (date range too big)
    if ((outDate - inDate) / 86400000 >= 31) {  //86400000 is one days in milliseconds
        alert(typeof (JavaScriptPeriodOfStay) == 'undefined' ? 'Your period of stay should be no longer than 30 nights.' : JavaScriptPeriodOfStay)
        return false
    }

    // validate checkout <= checkin
    if (outDate - inDate <= 0) {
        alert(typeof (JavaScriptEnsureCheckoutAfterCheckin) == 'undefined' ? 'Please ensure that the check-out date is after the check-in date.' : JavaScriptEnsureCheckoutAfterCheckin)
        return false
    }

    //validate checkin/checkout is less than one year in advance
    if ((outDate - currentDate) / 86400000 >= 364) {
        alert(typeof (JavaScriptBookWithinOneYear) == 'undefined' ? 'You cannot book more than 1 year in advance.' : JavaScriptBookWithinOneYear)
        return false;
    }
    return true
}
function ValidateGuestsRooms(guests,rooms) {
    
    //Please select the number of guests and rooms
    if (!guests && !rooms) {
        alert(typeof (JavaScriptSelectNoRoomsNoGuests) == 'undefined' ? 'Please select the number of guests and the number of rooms.' : JavaScriptSelectNoRoomsNoGuests);
        return false;
    } else if (!guests) {
        alert(typeof (JavaScriptSelectNoGuests) == 'undefined' ? 'Please select the number of guests.' : JavaScriptSelectNoGuests);
        return false;
    } else if (!rooms) {
        alert(typeof (JavaScriptSelectNoRooms) == 'undefined' ? 'Please select the number of rooms.' : JavaScriptSelectNoRooms);        
        return false;
    }
    return true;
}

function PopulateFromAutocomplete(inputBox) {

    var query = null;
    var selCityName = document.getElementById("selectedCityName");
    if (inputBox && selCityName) {
        if (inputBox.value && selCityName.value) {
            if (inputBox.value == selCityName.value) {
                query = "fileName=" + document.getElementById("selectedFileName").value;
                var locationID = document.getElementById("selectedLocationID").value;
                if (locationID && !isNaN(locationID)) {
                    query += "&locationId=" + locationID + "&sort=Distance-asc";
                }
            }
        }
    }
    return query;
}

