﻿/// <reference path="jquery-1.5.1.min.js" />
/// <reference path="~/JS/jquery-1.5.2.min.js" />


//Extention message de validation
$.extend(jQuery.validator.messages, {
    calendarPeriodic: "Veuillez sélectionner au moins deux plages horaires.",
    calendarFixe: "Veuillez sélectionner une plage horaire."    
});




//Validation des champs de parrainages
function ValidFilleul() {

    var v = $("form:first").validate({
        errorClass: 'ecv-onerror',
        errorElement: 'span',
        showErrors: function (erroMap, errorList) {
            $(errorList).each(function myfunction(index) {
                this.message = '<span class=\'ecv-error-picto\'></span>';

            });
            this.defaultShowErrors();
        }
    });

    return (v.form())
}

//Validation inscription
function ValidInscription() {
    var v = $("form:first").validate({

        errorClass: 'ecv-onerror',
        errorElement: 'span',

        showErrors: function (erroMap, errorList) {

            $(errorList).each(function myfunction(index) {
                this.message = '<span class=\'ecv-error-picto\'></span>';

            });
            this.defaultShowErrors();
        }
    });

    return (v.form())
}


//Rajoute les champs obligatoires lorsque la case Appart est coché
function checktypehabitation() {
    $("#logement_appart:checked").each(function(){
        $("#etage").addClass('required');
    });


    $("#logement_maison:checked").each(function () {
        $("#etage").removeClass('required');
    });
    
}

//rajoute les champs digicode
function ajoutdigode() {

    $(".txtDigi").fadeIn(800);
    $(".ecv_ajout_digicode").fadeOut(800);
    return false;

}


//Validation du txtUnzone
function ValidateTxtUnzonedUserJS(source, args) {

    args.IsValid = (jQuery('#txtMail').val().length > 0) || (jQuery('#txtTel').val().length > 0);
}

//Validation du code postal
function ValidCp() {
    jQuery('p#error').empty();
    var v = $("form:first").validate({
        errorClass: 'ecv-onerror',
        errorElement: 'span',
        showErrors: function (erroMap, errorList) {
            $(errorList).each(function myfunction(index) {
                jQuery('p#error').show().css("visibility", "visible").append(this.message + '<br>');
                this.message = '<span class=\'ecv-error-picto\'></span>';

            });
            this.defaultShowErrors();
        }
    });

    return (v.form())
}


function GetNewPassword() {
    jQuery.ajax({
        async: false,
        type: "POST",
        contentType: "application/json;charset=uft-8",
        url: '/Services/EcvWebService.asmx/GeneratePasswordUser',
        data: "{'userId':'" + $('#txtMailForget').val() + "'}",
        dataType: "html",
        cache: false,
       // success: function (msg) { alert('changed'); },
        error: function (error) { alert('Echec : ' + error.value); }
    });

}

/*************** Validation calendrier de livraison fréquence*************************/

// Validation calendrier de livraison
function ValidDeliveryCalendar() {
    var isok=false;
    $('.ecv-encart-container').find('input[@type=radio]:checked').each(function () {
        $('#crvModeDelevery').show().css("display", "none");
        isok = true;
    });
 

    if(isok ==false) {
        $('#crvModeDelevery').show().css("display", "inline");
    }
        
    $.validator.addMethod('validationCheckTable', function (val, el) {
        var checkcase = $(el).parents('table:first').find('input[type="checkbox"]:checked').length;
        return checkcase >= 2;
    }, $.validator.messages.calendarPeriodic)

    $.validator.addMethod('validationRadioTable', function (val, el) {
        var checkcase = $(el).parents('table:first').find('input[type="radio"]:checked').length;
        return checkcase == 1;
    }, $.validator.messages.calendarFixe)

    var v = $("form:first").validate({
        errorClass: 'ecv-onerror',
        errorElement: 'span',
        showErrors: function (erroMap, errorList) {
            $('p.error').hide().css("visibility", "collapse").empty();
            $(errorList).each(function myfunction(index) {
                if (this.message == $.validator.messages.calendarPeriodic) {                 
                        $('p.error').show().css("visibility", "visible").empty().append(this.message + '<br>');
                    return true;
                }
                else if (this.message == $.validator.messages.calendarFixe) {
                    $('p.error').show().css("visibility", "visible").empty().append(this.message + '<br>');
                    return true;
                }
            });
            //this.defaultShowErrors();
        }
    });

    return (v.form());
}

///
/// Test
$(document).ready(function () {
    
    $('.validationCalendar').live('click', function () {
        // Injection des class de validation sur les checkbox
        var checkboxValidationClass = "validationCheckTable";
            $('span.' + (checkboxValidationClass) + ' > input[type="checkbox"]').each(function (index) {
                $(this).addClass(checkboxValidationClass);
                $(this).parent().removeClass(checkboxValidationClass);
            });

        // Injection des class de validation sur les radios
        var radioValidationClass = "validationRadioTable";
        $('span.' + (radioValidationClass) + ' > input[type="radio"]').each(function (index) {
            $(this).addClass(radioValidationClass);
            $(this).parent().removeClass(radioValidationClass);
        });
        return ValidDeliveryCalendar();
    });
});

/*************** Fin Validation calendrier de livraison fréquence*************************/
