﻿function miseenforme() {
            $("#msgoptins").hide();
            $("#msgoptins").fadeIn(1500);
            $("#msgoptins").parent().css("height", "194px");
            $("#coupon").parent().css("padding-top", "10px");
            $("#getresa").css("margin-top", "15px");

    $("#date_debut").focus(
        function () {
            $("#msgoptins").fadeOut(500);
            $("#erreurs").fadeOut(500);
            $("#erreurs").parent().css("height", "");
            $("#msgoptins").parent().css("height", "");
            $("#coupon").parent().css("padding-top", "20px");
            $("#getresa").css("margin-top", "20px");
        }
    );

    $("#date_fin").focus(
        function () {
            $("#msgoptins").fadeOut(500);
            $("#erreurs").fadeOut(500);
            $("#erreurs").parent().css("height", "");
            $("#msgoptins").parent().css("height", "");
            $("#coupon").parent().css("padding-top", "20px");
            $("#getresa").css("margin-top", "20px");
        }
    );
}

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() {

    $("#agence_depart").change(function () {
        $("#agence_retour").val($(this).val());
        reservation_hours_get($(this).val());
    });
    $("#agence_retour").change(function () {
        reservation_hours_get($(this).val());
    });

    reservation_agences_get();
    reservation_hours_get(1);
    $("#div_retour_agence").hide();

}

function reservation_valid() {
    $("#erreurs").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() + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            if (msg.d == "") {
                $('#aspnetForm').submit()
            } else {
                $("#erreurs").html(msg.d);

                if ($("#erreurs").height() == 16) {
                    $("#erreurs").parent().css("height", "194px");
                    $("#coupon").parent().css("padding-top", "10px");
                    $("#getresa").css("margin-top", "15px");
                } else if ($("#erreurs").height() == 32) {
                    $("#erreurs").parent().css("height", "194px");
                    $("#coupon").parent().css("padding-top", "0px");
                    $("#getresa").css("margin-top", "10px");
                }
                $("#erreurs").fadeIn(1500);
            }
        }
    });

}

function reservation_ag_retour_show() {
    $("#div_agence_retour").show();
    $("#get_agence_retour").hide();
}

function reservation_agences_get() {

    $.ajax({
        type: "POST",
        url: "/_ajax/location.asmx/GetListAgence",
        cache:true,
        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(id_agence) {
    $.ajax({
        type: "POST",
        url: "/_ajax/location.asmx/GetHoraires",
        data: '{"id_agence":"' + id_agence + '"}',
        cache:true,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var options = '';
            var j = msg.d;
            for (var i = 0; i < j.length; i++) {
                if (j[i].ho == '16:00') {
                    options += '<option value="' + j[i].ho + '" selected="selected">' + j[i].ho + '</option>';
                } else {
                    options += '<option value="' + j[i].ho + '">' + j[i].ho + '</option>';
                }
            }
                $("#heure_debut").html(options);
                $("#heure_debut").removeAttr('disabled');
            $("#heure_fin").html(options);
            $("#heure_fin").removeAttr('disabled');
        }
    });
}

$(function () {

    miseenforme();

    $("#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
    })
})

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 gethours(id_agence, first) {

    if (first == '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/GetHoraires",
        data: '{"id_agence":"' + id_agence + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            if (first == '1') { $("#heure_debut").removeOption(/./); }
            $("#heure_fin").removeOption(/./);

            var options = '';
            var j = msg.d;

            for (var i = 0; i < j.length; i++) {
                if (j[i].ho == '16:00') {
                    options += '<option value="' + j[i].ho + '" selected="selected">' + j[i].ho + '</option>';
                } else {
                    options += '<option value="' + j[i].ho + '">' + j[i].ho + '</option>';
                }
            }
            if (first == '1') {
                $("#heure_debut").html(options);
                $("#heure_debut").removeAttr('disabled');
            }
            $("#heure_fin").html(options);
            $("#heure_fin").removeAttr('disabled');
        }
    });

}
