﻿var MailInformation = {
    "titre": "",
    "type": 0,
    "contact": "Webmaster",
    "subject": "NC",
    "message": "NC",
    "name": "---",
    "mail": "---",
    "propTitre": function (Titre) { this.titre = Titre; },
    "ajoutHTML": function (Contact) {
        var html = [];
        var i = 0;
        // Entete
        html[i++] = '<div id="mailPopup2" >';
        html[i++] = '<form>';
        //Contenu de la Popup
        html[i++] = '<div id="body">';
        html[i++] = '<div>';
        html[i++] = '<label for="mail">Votre email *</label>';
        html[i++] = '<input type="text" id="mail" class="obligatoire email" />';
        html[i++] = '</div>';
        html[i++] = '<div>';
        html[i++] = '<label for="nom">Votre nom</label>';
        html[i++] = '<input type="text" id="nom" />';
        html[i++] = '</div>	';
        html[i++] = '<div>';
        html[i++] = '<label for="sujet">Sujet *';
        if (Contact) html[i++] = ' (Contact pour ' + Contact + ')';
        html[i++] = '</label>';
        html[i++] = '<input type="text" id="sujet" data-minLenght="5" />';
        html[i++] = '</div>';
        html[i++] = '<div>';
        html[i++] = '<label for="msg">Message *</label>';
        html[i++] = '<textarea id="msg" data-minLenght="10" ></textarea>';
        html[i++] = '</div>	';
        html[i++] = '</div>';
        html[i++] = '</form>';
        html[i++] = '</div>';
        $('body').append(html.join(''));
        $('body').append('<div id="overlay"></div>');
    },
    "Inscription3heuresVTT": function () {
        // Type formulaire N°1
        var html = [];
        var i = 0;
        // Entete
        html[i++] = '<div id="mailPopup2" >';
        html[i++] = '<form>';
        //Contenu de la Popup
        html[i++] = '<div id="body">';
        html[i++] = '<div>';
        html[i++] = '<label for="mail">Votre email *</label>';
        html[i++] = '<input type="text" id="mail" class="obligatoire email" />';
        html[i++] = '</div>';
        html[i++] = '<div>';
        html[i++] = '<label for="nom">N° adhérent *</label>';
        html[i++] = '<input type="text" id="nom"  class="obligatoire numerique"/>';
        html[i++] = '</div>	';
        html[i++] = '<div>';
        html[i++] = '<label for="sujet">Sujet *';
        html[i++] = '</label>';
        html[i++] = MailInformation.titre;
        html[i++] = '</div>';
        html[i++] = '<div>';
        html[i++] = '<label for="msg">Présent </label>';
        html[i++] = '<input id="presentOui" type="radio" checked="checked" value="Oui" name="present" >OUI</input>';
        html[i++] = '<input id="presentNon" type="radio" name="present" value="Non" >NON</input>';
        html[i++] = '</div>';
        html[i++] = '<div>';
        html[i++] = '<label for="maillot">Taille du Maillot *</label>';
        html[i++] = '<input type="text" id="maillot"  class="obligatoire"/>';
        html[i++] = '</div>	';
        html[i++] = '<div>';
        html[i++] = '<label for="msg">Message</label>';
        html[i++] = '<textarea id="msg" ></textarea>';
        html[i++] = '</div>	';
        html[i++] = '</div>';
        html[i++] = '</form>';
        html[i++] = '</div>';
        $('body').append(html.join(''));
        $('body').append('<div id="overlay"></div>');
    },
    "Recrutement": function () {
        // Type formulaire N°2
        var html = [];
        var i = 0;
        // Entete
        html[i++] = '<div id="mailPopup2" >';
        html[i++] = '<form>';
        //Contenu de la Popup
        html[i++] = '<div id="body">';
        html[i++] = '<div>																											';															
        html[i++] = '    <label for="nom">Nom *</label>                                                                             ';
        html[i++] = '    <input type="text" id="nom" class="obligatoire" />                                                         ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="prenom">Prénom *</label>                                                                       ';
        html[i++] = '    <input type="text" id="prenom" class="obligatoire" />                                                      ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="dtenaissance">Date de naissance *</label>                                                      ';
        html[i++] = '    <input type="text" id="dtenaissance" class="obligatoire" />                                                ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="lieunaissance">Lieu de naissance *</label>                                                     ';
        html[i++] = '    <input type="text" id="lieunaissance" class="obligatoire" />                                               ';
        html[i++] = '</div>						                                                                                    ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="adresse">Adresse Domicile *</label>                                                            ';
        html[i++] = '    <textarea id="adresse" data-minLenght="10" ></textarea>                                                    ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="telpers">Tél personnel</label>                                                                 ';
        html[i++] = '    <input type="text" id="telpers" class="numerique" />                                                       ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="telport">Tél portable</label>                                                                  ';
        html[i++] = '    <input type="text" id="telport" class="numerique" />                                                       ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="mail">Votre email *</label>                                                                    ';
        html[i++] = '    <input type="text" id="mail" class="obligatoire email" />                                                  ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<hr />                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="numpermis">N° permis </label>                                                                  ';
        html[i++] = '    <input type="text" id="numpermis" class="numerique" />                                                     ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="datepermis">Date </label>                                                                      ';
        html[i++] = '    <input type="text" id="datepermis" class="" />                                                             ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="delpermis">Délivré par</label>                                                                 ';
        html[i++] = '    <input type="text" id="delpermis" class="" />                                                              ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="lieupermis">Lieu de délivrance</label>                                                         ';
        html[i++] = '    <input type="text" id="lieupermis" class="" />                                                             ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<hr />                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="grpsanguin">Groupe sanguin</label>                                                             ';
        html[i++] = '    <input type="text" id="Text1" class="" />                                                                  ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="formsecouriste">Formation secouriste</label>                                                   ';
        html[i++] = '    <input id="secouristeOui" type="radio"  name="formsecouriste" value="Oui"/> Oui                            ';
        html[i++] = '    <input id="secouristeNon" type="radio"  name="formsecouriste" value="non"/> Non                            ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<div>                                                                                                          ';
        html[i++] = '    <label for="adresse">Personne à prévenir en cas d\'accident</label>                                         ';
        html[i++] = '    <textarea id="Textarea1" data-minLenght="10" ></textarea>                                                  ';
        html[i++] = '</div>                                                                                                         ';
        html[i++] = '<hr />                                                                                                         ';
        html[i++] = '<p>Les données ne sont pas sauvegardées sur nos serveurs. Elles sont utilisées pour l\'élaboration du mail</p>  ';
        html[i++] = '</div>';
        html[i++] = '</form>';
        html[i++] = '</div>';
        $('body').append(html.join(''));
        $('body').append('<div id="overlay"></div>');
    },
    "afficher": function (Contact) {
        $('#mailPopup2').remove();
        Marquage("/FORMULAIRE/" + this.type + "_" + this.titre + "_" + Contact, 'Formulaire', 1)
        if ($('#mailPopup2').size() == 0) {
            switch (this.type) {
                case "1":
                case 1:
                    this.Inscription3heuresVTT();
                    break;
                case "2":
                case 2:
                    this.Recrutement();
                    break;
                default:
                    this.ajoutHTML(Contact);
            }
        }
        if (this.titre == '') this.titre = 'Formulaire de contact';
        if ($('#nom').val() == '') MailInformation.name = $('#nom').val(); else MailInformation.name = "---";
        if ($('#mail').val() == '') MailInformation.mail = $('#mail').val(); else MailInformation.mail = "PostMaster";

        $('#mailPopup2').dialog({
            title: this.titre,
            autoOpen: true,
            show: "blind",
            hide: "explode",
            modal: true,
            close: function (event, ui) {
                $('#mailPopup2').remove();
                MailInformation.titre = '';
                MailInformation.type = 0;
            },
            buttons: {
                "envoyer": function () {
                    if ($('#mailPopup2').children('form').trigger('validerFormulaire').data('isFormValid')) {
                        if (Contact) {
                            MailInformation.subject = 'Contact pour ' + Contact + ': ' + $('#sujet').val();
                        } else {
                            MailInformation.subject = $('#sujet').val()
                        }
                        switch (MailInformation.type) {
                            case "1":
                            case 1:
                                MailInformation.subject = $('#nom').val() + ' - ' + MailInformation.titre;
                                MailInformation.message = ' - N° ADHERENT :' + $('#nom').val() + '\n';
                                MailInformation.message += ' - Présent :';
                                MailInformation.message += $('input[type=radio][name=present]:checked').attr('value') + '\n';
                                MailInformation.message += ' - plage horaire :' + $('#msg').val();
                                break;
                            case "2":
                            case 2:
                                MailInformation.subject = $('#nom').val() + ' - ' + MailInformation.titre;
                                MailInformation.message = ' - Nom :' + $('#nom').val() + '\n';
                                MailInformation.message += ' - Présent :';
                                MailInformation.message += $('input[type=radio][name=present]:checked').attr('value') + '\n';
                                MailInformation.message += ' - Message :' + $('#msg').val();
                                break;
                            default:
                                MailInformation.message = $('#msg').val();
                        }
                        if ($('#nom').val() != '') MailInformation.name = $('#nom').val(); else MailInformation.name = "---";
                        if ($('#mail').val() != '') MailInformation.mail = $('#mail').val(); else MailInformation.mail = "PostMaster";
                        if (MailInformation.message == '') MailInformation.message = "--";
                        if (MailInformation.subject == '') MailInformation.subject = "--";
                        $.ajax({
                            "url": "php/send_email_auto_response_hex.php",
                            "type": "POST",
                            "context": this,
                             "data": {
                                "email": MailInformation.mail,
                                "name": MailInformation.name,
                                "subject": MailInformation.subject,
                                "message": MailInformation.message
                            },
                            "dataType": "json",
                            "success": function (obj) {
                                MailInformation.afficherMsgSoumission(
                                'Email Envoyé',
                                    'submitSuccess',
                                    $.proxy(
                                        function () {
                                            $('#mailPopup2').remove();
                                            MailInformation.type = 0;
                                            $(this).dialog("close")
                                        }, this));
                            },
                            "error": function (xhr, textStatus, errorThrown) {
                                if (xhr.status == 400) {
                                    MailInformation.afficherMsgSoumission(xhr.responseText, 'submitError');
                                } else if (xhr.status == 0) {
                                    MailInformation.afficherMsgSoumission(
                                    'Email Envoyé',
                                    'submitSuccess',
                                    $.proxy(
                                        function () {
                                            $('#mailPopup2').remove();
                                            MailInformation.type = 0;
                                            $(this).dialog("close")
                                        }, this)
                                    );
                                } else {
                                    MailInformation.afficherMsgSoumission('Echec de l\'envoi de mail dû à une erreur interne du serveur', 'submitError');
                                }
                            },
                            "complete": function () {
                                $('#mailPopup').find(':submit').attr('disabled', false);
                            }
                        });
                    } else {
                        MailInformation.afficherMsgSoumission('Formulaire non complet', 'submitError');
                    }
                }
            }
        });
    },
    "envoyerformulaire": function (Contact, Type) { },
    "afficherMsgSoumission": function (msg, css, callback) {
        $('#msg')
        .after('<div class="' + css + '">' + msg + '</div>')
        .next()
        .fadeOut(5000, function () {
            $(this).remove();

            if ($.isFunction(callback)) {
                callback.call();
            }

        });
    }
};

$(':text,textarea').live(
    'validerChampTexte',
    function () {
        var errMessage = []
        var val = $.trim($(this).val());

        // Champs Obligatoire
        if ($(this).hasClass('obligatoire') && val == '') {
            errMessage.push('Champs obligatoire');
        }

        //Expression réguliere:  Email
        var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
        var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
        var regex = "^" + usr + "\@" + domain + "$";
        var regexEmail = new RegExp(regex, 'i');

        // Champs Email
        if ($(this).hasClass('email') && !regexEmail.test(val)) {
            errMessage.push('Format invalide');
        }

        //Expression réguliere:  Numérique
        regex = "^[0-9]+$";
        var regexNumerique = new RegExp(regex, 'i');
        // Champs Numérique
        if ($(this).hasClass('numerique') && !regexNumerique.test(val)) {
            errMessage.push('Format invalide');
        }

        // Longueur du Champs
        var minLength = $(this).attr('data-minLenght') == null ? 0 : parseInt($(this).attr('data-minLenght'))
        var maxLength = $(this).attr('data-maxLenght') == null ? 0 : parseInt($(this).attr('data-maxLenght'))
        if (minLength > 0 && val.length < minLength) {
            errMessage.push('Caractères minimum');
        }
        if (maxLength != 0 && val.length > maxLength) {
            errMessage.push('Caractères maximun');
        }

        // Flag
        var isValid = errMessage.length == 0;
        // Le message à afficher à coté du champ
        var msg = '<div class="valid" ></div>';
        if (!isValid) {
            msg = '<div class="error">' + errMessage.join('<br />') + '</div>';
        }

        //Retire le message précédent
        if ($(this).next().is('div.error,div.valid')) {
            $(this).next().remove();
        }
        // affiche le nouveau message d'alerte
        $(this).after(msg);

        //Sauvegarde la validé du champs
        $(this).data('isChampValid', isValid);
    }
 );

// Attache les évènement à la zone de saisie
$(':text,textarea').live('keyup change',
        function () {
            $(this).trigger('validerChampTexte');
        });

// Valide le formulaire
$('form').live('validerFormulaire',
    function () {
        var isValid = true;

        $(this)
        .find(':text,textarea')
        .each(function () {
            $(this).trigger('validerChampTexte');
            if ($(this).data('isChampValid') == false) {
                isValid = false;
            }
        })
        .end()
        .data('isFormValid', isValid);
    }
);
//Efface les données du formulaire
$('form').live('reset', function () {
    $(this).find('div.error,div.valid').remove();
    $(this).get(0).reset();
});

