﻿function addDaysToDate(old_date, delta_days) {
    var split_date = old_date.split('/');
    var new_date = new Date(split_date[2], split_date[1] * 1 - 1, split_date[0] * 1 + delta_days);
    var new_day = new_date.getDate();
    new_day = ((new_day < 10) ? '0' : '') + new_day;
    var new_month = new_date.getMonth() + 1;
    new_month = ((new_month < 10) ? '0' : '') + new_month;
    var new_year = new_date.getYear();
    new_year = ((new_year < 200) ? 1900 : 0) + new_year;
    var new_date_text = new_day + '/' + new_month + '/' + new_year;
    return new_date_text;
}

function reservation_init() {

    $("#date_debut").datepicker({ minDate: +3, maxDate: '+2Y',
        numberOfMonths: 2,
        showOn: "both",
		buttonImage: "/_images/calendar.gif",
		buttonImageOnly: true,
        onSelect: function (date) {
            $('#date_fin').val(addDaysToDate(date, 7));
        }
    });

    $("#date_fin").datepicker({ minDate: +4, maxDate: '+2Y', numberOfMonths: 2, showOn: "both",
		buttonImage: "/_images/calendar.gif",
		buttonImageOnly: true });
    $("#ui-datepicker-div").hide();

    $("#agence_depart").change(function () {
        $("#agence_retour").val($(this).val());
        reservation_hours_get($("input[name='departement']:checked").val(), $(this).val(), '1');       
    });
    $("#agence_retour").change(function () {
        reservation_hours_get($("input[name='departement']:checked").val(), $(this).val(), '');
    });

    $("#search_resa input[type='radio']").click(change_region);
    $("#search_resa input[type='radio']").attr('checked', false);
    $("#div_retour_agence").hide();
    $("#retour_agence_link").hide();
    
}

function reservation_valid() {
    $("#search_alerte").hide();
    reservation_control();
}

function reservation_control() {

    $.ajax({
        type: "POST",
        url: "/_ajax/location.asmx/reservation_control",
        data: '{"agence_depart":"' + $("#agence_depart").val() + '", "agence_retour":"' + $("#agence_retour").val() + '", "heure_debut":"' + $("#heure_debut").val() + '", "heure_fin":"' + $("#heure_fin").val() + '", "date_debut":"' + $("#date_debut").val() + '", "date_fin":"' + $("#date_fin").val() + '", "departement":"' + $("input[name='departement']:checked").val() + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            if (msg.d == "") {
                $('#aspnetForm').submit()
            } else {
                $("#search_alerte").show();
                $("#search_alerte").html(msg.d);
            }

        }
    });

}

function reservation_ag_retour_show() {
    $("#div_retour_agence").show();
    $("#retour_agence_link").hide();
}

function change_region() {
    var thedepartement = $(this).val();
    reservation_agences_get(thedepartement);
    reservation_hours_get(thedepartement, 0, '1');
    $("#retour_agence_link").show();
}

function reservation_agences_get(departement) {

    $.ajax({
        type: "POST",
        url: "/_ajax/location.asmx/reservation_agences_get",
        data: '{"departement":"' + departement + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            $("#agence_depart").html('');
            $("#agence_retour").html('');

            var j = msg.d;
            var options = '';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i]._id + '">' + j[i]._item + '</option>';
            }

            $("#agence_depart").html(options);
            $("#agence_retour").html(options);

        }
    });

}

function reservation_hours_get(departement, id_agence, depart) {
    if (depart == '1') {
        $("#heure_debut").attr('disabled', 'true');
        $("#heure_debut").html('<option value="">Chargement...</option>');
    }
    $("#heure_fin").attr('disabled', 'true');
    $("#heure_fin").html('<option value="">Chargement...</option>');

    $.ajax({
        type: "POST",
        url: "/_ajax/location.asmx/reservation_horaires_get",
        data: '{"id_agence":"' + id_agence + '", "departement":"' + departement + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (depart == '1') { $("#heure_debut").html(''); }
            $("#heure_fin").html('');
            var options = '';
            var j = msg.d;
            for (var i = 0; i < j.length; i++) {
                if (j[i]._id == '16:00' && departement != 'REU') {
                    options += '<option value="' + j[i]._id + '" selected="selected">' + j[i]._id + '</option>';
                } else if (j[i]._id == '08:00' && departement == 'REU') {
                    options += '<option value="' + j[i]._id + '" selected="selected">' + j[i]._id + '</option>';
                } else                 {
                    options += '<option value="' + j[i]._id + '">' + j[i]._id + '</option>';
                }
            }
            if (depart == '1') {
                $("#heure_debut").html(options);
                $("#heure_debut").removeAttr('disabled');
            }
            $("#heure_fin").html(options);
            $("#heure_fin").removeAttr('disabled');
        }
    });
}

