// Global array.
paramsArray = { };

// Reservation function.
function Reserve()
{
    if ((arguments.length > 0) && (parseInt(arguments[0]) > 0))
    {
        var url = 'https://mingus.info/hotelloweb4/php/index.php?h=' + arguments[0];
        url += '&lang=' + ((arguments[1] != null) ? arguments[1] : 'en' ); //la langue.

        if (arguments[2] != null)
        {
            var paramsArray = arguments[2];
            for (var param in paramsArray)
                url += (paramsArray[param] != null) ? '&' + param + '=' + paramsArray[param] : '';
        } //fin du if (arguments[2] != null)

        fenetreDetail = window.open(url, '', 'menubar=0, location=0, resizable=1, scrollbars=1, toolbar=0, status=0, directories=0');
        fenetreDetail.focus();
    }
}

// Function that sets the calendar start date.
function getStartDate(st)
{
    var realDate;
    if (today.getTime() < st.getTime())
        realDate = st;
    else
        realDate = today;

    return realDate;
}

// Function that verifies the selected date.
function dateIsOk(d, m, y, nbj, md)
{
    var selectedDate = $('#' + y).val() + '/' + $('#' + m).val() + '/' + $('#' + d).val();
    var dateDebut = new Date(selectedDate);
    var stdt = new Date();

    if ((dateDebut.getTime() < stdt.getTime()) && loadpage > 1)
    {
        if (langue == "fr")
            alert("La date selectioné ne doit pas etre inferieur à la date du jour!");
        else
            alert("The selected date must be lower than today date!");

        $('#'+d+' option[value=' + stdt.getDate() + ']').attr('selected', 'selected');
        $('#'+m+' option[value=' + (stdt.getMonth()+1) + ']').attr('selected', 'selected');
        $('#'+y+' option[value=' + (stdt.getFullYear()) + ']').attr('selected', 'selected');
    }
    else
        loadpage++;

    VerifMaxdate(d, m, y, nbj, md);
}

// Function that verifies the maximum date.
function VerifMaxdate(d, m, y, nbj, md)
{
    var selectedDate = $('#' + y).val() + '/' + $('#' + m).val() + '/' + $('#' + d).val();
    var maxd = new Date(md);
    var nt = new Date(selectedDate);

    nt.addDays(document.getElementById(nbj).value);

    if (nt.getTime() > md.getTime() && loadpage > 1)
    {
        if (langue == "fr")
            alert("La date selectionée ne doit pas être supèrieure à la date maximale !");
        else
            alert("The selected date cannot be later than the maximum date!");

        document.getElementById(nbj).value = 0;
    }
}

// Function that fills the global array.
function fillParam(d, m, y, nt, na, nd, ne)
{
    var selectedDate = $('#' + y).val() + '/' + $('#' + m).val() + '/' + $('#' + d).val();
    var dateDebut = (new Date(selectedDate)).asString('yyyy/mm/dd');
    var dateFin = new Date(selectedDate);
    dateFin.addDays(document.getElementById(nt).value);

    paramsArray = {
        ckIn:dateDebut,
        ckOut:dateFin.asString('yyyy/mm/dd'),
        /*r:'CH LUXE',*/
        ad:document.getElementById(na).value,
        te:document.getElementById(nd).value,
        ch:document.getElementById(ne).value
    };
}

// Common initialization function.
function InitializeMingusObjects(reserveDiv, calendarButtonImage, reserveNowImage, daySelect, monthSelect, yearSelect, startDate, maxDate)
{
    // Hide reservation DIV object.
    $("#" + reserveDiv).hide();

    $(function()
    {
        // Initialize the calendar button image.
        $('#' + calendarButtonImage)
            .datePicker(
                /* Associate the link with a date picker */
                {
                    createButton:false,
                    startDate:startDate.asString('dd/mm/yyyy'),
                    endDate:maxDate
                }
            ).bind(
                /* When the link is clicked display the date picker. */
                'click',
                function()
                {
                    updateSelects($(this).dpGetSelected()[0]);
                    $(this).dpDisplay();
                    return false;
                }
            ).bind(
                /* When a date is selected update the SELECTs. */
                'dateSelected',
                function(e, selectedDate, $td, state)
                {
                    updateSelects(selectedDate);
                }
            ).bind(
                'dpClosed',
                function(e, selected)
                {
                    updateSelects(selected[0]);
                }
            );

        var updateSelects = function(selectedDate)
        {
            var selectedDate = new Date(selectedDate);
            $('#' + daySelect +   ' option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
            $('#' + monthSelect + ' option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
            $('#' + yearSelect +  ' option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
        }

        // Listen for when the selects are changed and update the picker.
        $('#' + daySelect + ', #' + monthSelect + ', #' + yearSelect)
            .bind(
                'change',
                function()
                {
                    var d = new Date($('#' + yearSelect).val(), $('#' + monthSelect).val()-1, $('#' + daySelect).val());
                    $('#' + calendarButtonImage).dpSetSelected(d.asString());
                }
            );

        // Default the position of the selects to today...
        updateSelects(getStartDate(startDate));

        // and update the datePicker to reflect it.
        $('#' + daySelect).trigger('change');
    });

    $("a#" + reserveNowImage)
        .click(
            function()
            {
                $(this).next().fadeIn();
                return false;
            }
        );

    $("#" + reserveDiv + " a.fermer")
        .click(
            function()
            {
                $(this).parent().fadeOut();
                return false;
            }
        );
}

