addOnload(initBooking);

var ready = false;

function initBooking() {
    var html = "";

    html += "<span class='lodgingDateArea'>" + renderCalendar() + "</span>";
    html += "<span class='lodgingNightsArea'>" + renderNights() + "</span>";
    html += '<span id="lodgingSubmit"><button id="lodgingButton" type="button" onclick="findHotels()" class="icon"><img src="/ui/media/find-hotels.png"></span>';

    var lodgingArea = document.getElementById("guideLodgingArea");
    // if no form, abort
    if (!lodgingArea)
        return;

    lodgingArea.innerHTML = lodgingArea.innerHTML + html;

    ready = true;
}

var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

function renderCalendar() {
    var html = "";

    // day
    html += "<select name='day'>";
    html += "<option value=''>day</option>";
    for (var j=1; j <= 31; j++) 
        html += "<option value='" + j + "'>" + j + "</option>";
    html += "</select>&nbsp;";


/*
    // month
    html += "<select name='month'>";
    html += "<option value=''>month</option>";
    for (var j=0; j < 12; j++) 
        html += "<option value='" + (j+1) + "'>" + months[j] + "</option>";
    html += "</select>&nbsp;";

    // year
    html += "<select name='year'>";
    html += "<option value=''>year</option>";
    html += "<option value='2008'>2008</option>";
    html += "<option value='2009'>2009</option>";
    html += "</select>";
*/

    html += "<select name='month'>";
    var d = new Date();
    var mon = d.getMonth();
    var year = d.getFullYear();
    for (var j=0; j < 12; j++) {
        var m = (mon + j) % 12;
        var y = year + Math.floor((mon+j) / 12);
        html += "<option value='" + (m+1) + "-" + y + "'>" + months[m] + " " + y + "</option>";
    }
    html += "</select>";

    return html; 
}

function renderNights() {
    var html = "<select name='nights'>";
    html += "<option value=''>- nights -</option>";
    html += "<option value='1'>1 night</option>";
    for (var j=2; j <= 31; j++) 
        html += "<option value='" + j + "'>" + j + " nights</option>";
    html += "</select>";
    return html;
}

function makeHotelUrl(locationId, month, day, year, nights) {
    var checkin = year + "-" + month + "-" + day;
    var d = new Date();
    d.setYear(year);
    d.setMonth(month-1);
    d.setDate(day + nights);

    var checkout = d.getFullYear() + "-" + (d.getMonth()+1) + "-" + d.getDate();
    if (locationId) {
        if (document.form.locationType.value == 4)
            return "http://b2b.hotelclub.net/Enter.asp?id=47221&ru=directcity%2Easp%3Fid%3D" + locationId + "%26Checkin=" + checkin + "%26Checkout=" + checkout;
            //return "http://b2b.hotelclub.net/Enter.asp?id=47221&ru=SearchResults%2Easp%3Fid%3D" + locationId + "%26Checkin=" + checkin + "%26Checkout=" + checkout;
        else 
            return "http://b2b.hotelclub.net/Enter.asp?id=47221&ru=%2F%3Fid%3D" + locationId + "%26Checkin=" + checkin + "%26Checkout=" + checkout;
    } else {
        return "http://b2b.hotelclub.net/Enter.asp?id=47221&ru=%2F%3FCheckin=" + checkin + "%26Checkout=" + checkout;
    }
//http://b2b.hotelclub.net/?Checkin=2008-7-6&Checkout=2008-7-13
}

function findHotels() {
    var city = document.form.affiliateId.value;
    var nights = parseInt(document.form.nights.value);
    var day = parseInt(document.form.day.value);

    var month = document.form.month.value;
    if (month.match(/(\d+)-(\d+)/)) {
        month = parseInt(RegExp.$1);
        year = parseInt(RegExp.$2);
    } else {
        alert("Please select the month you'll arrive."); 
        return;
    }
        

    if (!day) { alert("Please select the day you'll arrive."); return; }
    if (!nights) { alert("Please select the nights you'll stay."); return; }

    window.location = makeHotelUrl(city, month, day, year, nights);
}
