﻿

function selectRadio(isVisible) {
    if (isVisible) {
        $('#MiseEnService').show();
    } else {
        $('#MiseEnService').hide();
    }    
 }



var lastId = 0;
$(document).ready(function() {
   
    $('#code_postal_id').keypress(function(e) {
        //if the letter is not digit then display error and don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });

    $('#code_postal_id').keyup(function() {
        if (($('.countryId :selected').text().toLowerCase() == "france" || $('.countryId :selected').val() == 84 || $('.countryId :selected').val() == 91
            || $('.countryId :selected').val() == 135 || $('.countryId :selected').val() == 180)
            && $('#code_postal_id').val().length == 5
            && lastId != $('#code_postal_id').val()) {
            lastId = $('#code_postal_id').val();
            $.ajax({
                url: sitePath + "ajax/ContactGetVilles/" + lastId,
                dataType: "json",
                cache: false,
                success: function(obj) {
                    $('.ville').children().remove();
                    $.each(obj, function() {
                        $('.ville').append('<option value="' + this.Localite + '">' + this.Localite + '</option>');
                    });
                    if ($('#ville_select select').children().length > 0)
                        $('.ville').removeAttr('disabled');
                },
                error: function() { alert('Erreur'); }
            });
        }
        else if (($('.countryId :selected').text().toLowerCase() == "france" || $('.countryId :selected').val() == 84 || $('.countryId :selected').val() == 91
            || $('.countryId :selected').val() == 135 || $('.countryId :selected').val() == 180) && lastId != $('#code_postal_id').val()) {
            $('.ville').children().remove();
            $('.ville').attr('disabled', 'disabled');
            lastId = $('#code_postal_id').val();
        }
    });

    $('.countryId').change(function() {
        if ($('.countryId :selected').text().toLowerCase() == "france" || $('.countryId :selected').val() == 84 || $('.countryId :selected').val() == 91
            || $('.countryId :selected').val() == 135 || $('.countryId :selected').val() == 180) {
            $('input[name=City]').attr("disabled", "disabled");
            $('#ville_txt').hide();
            $('#ville_select').show();
            if ($('#ville_select select').children().length > 0)
                $('#ville_select select').removeAttr("disabled");
        }
        else {
            $('#ville_select select').attr("disabled", "disabled");
            $('#ville_select').hide();
            $('#ville_txt').show();
            $('input[name=City]').removeAttr("disabled");
        }
        $('#code_postal_id').keyup();
    });

    $('.countryId').change();

    /*
    ** FORMULAIRE BON DE GARANTIE
    */



    $('#InstallerZipCode').keypress(function(e) {
        //if the letter is not digit then display error and don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });

    $('#InstallerZipCode').keyup(function() {
        if (($('.installerCountryId :selected').text().toLowerCase() == "france" || $('.countryId :selected').val() == 84 || $('.countryId :selected').val() == 91
            || $('.countryId :selected').val() == 135 || $('.countryId :selected').val() == 180)
            && $('#InstallerZipCode').val().length == 5
            && lastId != $('#InstallerZipCode').val()) {
            lastId = $('#InstallerZipCode').val();
            $.ajax({
                url: sitePath + "ajax/ContactGetVilles/" + lastId,
                dataType: "json",
                cache: false,
                success: function(obj) {
                    $('.installerVille').children().remove();
                    $.each(obj, function() {
                        $('.installerVille').append('<option value="' + this.Localite + '">' + this.Localite + '</option>');
                    });

                    if (($('#InstallerCity')[0]).hasChildNodes())
                        $('.installerVille').removeAttr('disabled');
                },
                error: function() { alert('Erreur'); }
            });
        }
        else {
            if (lastId != $('#InstallerZipCode').val()) {
                $('.installerVille').children().remove();
                $('.installerVille').attr('disabled', 'disabled');
                lastId = $('#InstallerZipCode').val();
            }
        }
    });


    $('.installerCountryId').change(function() {
        if ($('.installerCountryId :selected').text().toLowerCase() == "france" || $('.installerCountryId :selected').val() == 84 || $('.installerCountryId :selected').val() == 91
            || $('.installerCountryId :selected').val() == 135 || $('.installerCountryId :selected').val() == 180) {
            $('input[name=InstallerCity]').attr("disabled", "disabled");
            $('#installer_ville_txt').hide();
            $('#installer_ville_select').show();
            if ($('#installer_ville_select select').children().length > 0)
                $('#installer_ville_select select').removeAttr("disabled");
        }
        else {
            $('#installer_ville_select select').attr("disabled", "disabled");
            $('#installer_ville_select').hide();
            $('#installer_ville_txt').show();
            $('input[name=InstallerCity]').removeAttr("disabled");
        }
        $('#InstallerZipCode').keyup();
    });

    $('.installerCountryId').change();

});
