$(document).ready(function(){
   /*** Inscription ***/
    $('#name').blur(function() {
        $('#nameShip').val($(this).val());
        if($(this).val() != '') $('#nameShip').removeClass('required');
        else $('#nameShip').addClass('required');
    });
    $('#surname').blur(function() {
        $('#surnameShip').val($(this).val());
        if($(this).val() != '') $('#surnameShip').removeClass('required');
        else $("#surnameShip").addClass('required');
    });
    $('input[name=civ]').click(function() {
        $('input[name=civShip][value=' + $(this).val() + ']').attr('checked', 'checked');
        $('#civilShip').removeClass('required');
    });
    $(".onlySelect").change(function() {
        $("#birthdate").val($("#date_y").val() + "-" + $("#date_m").val() + "-" + $("#date_d").val());
    });
    $('#deliv').click(function() {
        if(!$(this).attr('checked')) {
            $('#address_bill_form').slideDown('fast');
            $('#address_bill_form').find('input').each(function() {
                if($(this).attr('name') != 'address2Bill') {
                    $(this).addClass('required');
                }
            });
        } else {
            $('#address_bill_form').slideUp('fast');
            $('#address_bill_form').find('input').each(function() {
                if($(this).attr('name') != 'address2Bill') {
                    $(this).removeClass('required');
                }
            });
        }
    });
    $(".onlyRadioShip").change(function() {
        $("#civilShip").val(0);	
        if($("#mrShip").attr("checked") == true){
            $("#civilShip").val(1);
        }
        if($("#mmeShip").attr("checked") == true){
            $("#civilShip").val(2);
        }
        if($("#mlleShip").attr("checked") == true){
            $("#civilShip").val(3);
        }
    });	
    $(".onlyRadioNews").change(function() {
        $("#newsletter").val(0);
        if($("#newsY").attr("checked") == true){
            $("#newsletter").val(1);
        }
        if($("#newsN").attr("checked") == true){
            $("#newsletter").val(2);
        }
    });
    $.validator.addMethod(
        "frenchDate",
        function(value, element) {
            return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
        },
        "Date invalide"
        );
    $("#subscribe_form").validate({
        rules: {
            pass: "required",
            pass2: {
                equalTo: "#pass"
            },
            birthdate: {
                date: true
            },
            civility: {
                range: [1,3]
            },
            civilShip: {
                range: [1,3]
            },
            newsletter: {
                range: [1,2]
            },
			mail: {
				required: true,
				email: true,
				remote: AJAX_PATH + "check_mail_exists.ajax.php"
			}
        }
    });
    $(".onlyRadio").change(function() {
        $("#civility").val(0);
        if($("#mr").attr("checked") == true){
            $("#civility").val(1);
        }
        if($("#mme").attr("checked") == true){
            $("#civility").val(2);
        }
        if($("#mlle").attr("checked") == true){
            $("#civility").val(3);
        }
    });

 
});
