
function zTrim(q) {
    return q.replace(/^[\s\n\r]+/g,'').replace(/[\s\n\r]+$/g,'');
}

function ImprimeZF(f) {
  f.find('.zf_print_remove').css('display','none');
  content=f.parent().html();
  f.find('.zf_print_remove').css('display','block');
  var pwin=window.open('','print_content','width=720,height=100');
  pwin.document.open();
  pwin.document.write('<html><head><link href="Style.css" rel="stylesheet" type="text/css" /><link href="themes/jquery-ui-common.css" rel="stylesheet" type="text/css" /><body style="background:#ffffff; width:720px;" position:relative; onload="window.print()"><div class="zf_form">'+content+'</div></body></html>');
  pwin.document.close();
  setTimeout(function(){pwin.close();},1000);
}

function ValideZF(f) {
     //désactive le formulaire au besoin, vide les messages...
     $('.zf_form').append('<div class="zf_form_overlay">Validation et traitement...</div>');
     $('.zf_form_overlay').css('opacity','0.70');
     $('.zf_msg_etat').text('').stop(true,true).hide();
     $('.zf_elem_msg').removeClass('zf_elem_msg');
     $('.zf_msg').html('').stop(true,true).fadeTo('fast',0.1);
     valide = true;
     
     // vérifier la présence du captcha (longueur exacte)...
     msgcc = "";
     if (!(est_admin && f[0].date_code)) {
      cc = f.find('.zf_captcha');
      cc.css('background-color','#ffffff');
      ccl = cc[0].value.length
      if (ccl == 0) msgcc = '<b style="#ff3333;">Captcha manquant.</b><br />';
      else if (ccl != 5) msgcc = '<b style="#ff3333;">Le captcha doit avoir exactement 5 caractères.</b><br />';
      if (msgcc != "") {
         valide = false;
         cc.css('background-color','#ff6600');
      }
     }
     //[zf_type] hidden dans le formulaire
     // Traitements spéciaux si nécessaires .. ?

/*  exemple.. test de boutons radios.. lorsqu'on a le nom...
        test = f.pi_loc; test_radio = true;
        for (i in test) if (test[i].checked==true) test_radio = false;
        if (test_radio) message += "Localisation\n";
 */

//pour chaque classes freq... (requises) reste à faire...
//zfreq_lien (débute par http ou https... pas de caractères spéciaux (comme la vérification dans admin pages)

     $('.zfreq_txt').each(function() {
        msg = ""; this.value = zTrim(this.value); len = this.value.length;
        if ($(this).parent().find('.zfreq_min')[0]) {
          m = parseInt($(this).parent().find('.zfreq_min').text());
          if (len < m) {
            valide = false;
            if (len == 0) msg += "Ce champ ne doit pas être vide.";
                else msg += "Trop court.";
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
            }  
        }
        else if (len == 0) { // ATTENTIÒN ne vérifie pas car si le min existe et min == 0, VALIDE !
            valide = false; msg += "Ce champ ne doit pas être vide.";
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
        }
        if ($(this).parent().find('.zfreq_max')[0]) {
          m = parseInt($(this).parent().find('.zfreq_max').text());
          if (len > m) {
            valide = false; msg += "Trop long ! "+len+' / '+m+' caractères max.';
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
            }
        }
     });
     $('.zfreq_date').each(function() {
        msg = ""; this.value = zTrim(this.value);
        len = this.value.length; test = this.value;                 
        if ((len != 10
            || isNaN(parseInt(test.substring(0,4)))
            || isNaN(parseInt(test.substring(5,7)))
            || isNaN(parseInt(test.substring(8,10))))) {
            valide = false;
            msg += "Format de date attendu : YYYY-MM-JJ";
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
        }
        else this.value = test.substring(0,4)+'-'+test.substring(5,7)+'-'+test.substring(8,10);
     });
     $('.zfreq_num').each(function() {
        msg = ""; this.value = zTrim(this.value);
        len = this.value.length; test = this.value;
        if (len == 0 || isNaN(test*1)) {
            valide = false;
            if (len==0) msg += "Ce champ doit contenir un chiffre.";
            else msg += "Ce champ n'accepte que les chiffres.";
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
        }
        else {
         if ($(this).parent().find('.zfreq_max')[0]) {
          m = parseInt($(this).parent().find('.zfreq_max').text());
          if (test > m) {
            valide = false; msg += "Trop grand ! Maximum : "+m;
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
            }
         }
         if ($(this).parent().find('.zfreq_min')[0]) {
          m = parseInt($(this).parent().find('.zfreq_min').text());
          if (test < m) {
            valide = false; msg += "Trop petit ! Minimum : "+m;
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
            }
         }
        }
     });
     $('.zfreq_mail').each(function() {
        msg = ""; testc = this.value;        
        if (/[^\w@\.-]/.test(testc)) { msg += "Caractères invalides. "; valide = false; }
        testl = testc.length; at_pos = testc.indexOf("@"); dot_pos = testc.lastIndexOf(".");
        if ((testl < 8) || at_pos == null || dot_pos == null || (at_pos < 1) || (dot_pos < (at_pos+2)) || (dot_pos > testl-3) || (dot_pos < testl-5)) {            
            msg += "Format du courriel invalide."; valide = false;
        }
        if (msg != "") {
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
        }
     });
     $('.zfreq_tel').each(function() {
        msg = ""; tel = this.value;
        len = tel.replace(/[^0-9]/gi,'').length;
        if (len != 10) {
            valide = false;
            msg += "Le numéro doit avoir 10 chiffres.";
            $(this).parent().find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).parent().parent().addClass('zf_elem_msg ui-corner-all');
        }
     });
     $('.zf_radios').each(function() { // vérifie seulement si zfreq_radio présent dans zf_radios
        msg = "";  test_radio = true;
        $(this).find('.zfreq_radio').each(function() { if (this.checked===true) test_radio = false;})
        if (test_radio && $(this).find('.zfreq_radio')[0]) {
            valide = false; msg += "Choisir une option.";
            $(this).find('.zf_msg').html(msg).stop(true,true).fadeTo('fast',1);
            $(this).addClass('zf_elem_msg ui-corner-all');
        }
     });
     if (valide) {
         $.post('zen_traite_formulaires.php',f.serialize(),function(rep) {
             if (rep.resultat===true) {
                 $('.zf_form_overlay').html('<span class="ui-state-highlight">Le formulaire a été envoyé avec succès !</span>');                 
                 if (est_admin && f[0].date_code) {$('.zf_form_overlay').remove();
                    $('.zf_msg_etat').html('<div class="zenbox ui-state-highlight ui-corner-all">'+rep.msg+'</div>').stop(true,true).fadeIn();
                 }else if (rep.msg != "") $('.zf_form').before(rep.msg)
             }
             else {
                 $('.zf_msg_etat').html('<div class="zenbox ui-state-highlight ui-corner-all">'+rep.msg+'</div>').stop(true,true).fadeIn();
                 $('.zf_form_overlay').remove();
                 $('.captcha').attr('src','securimage_show.php?rien='+(Math.random()*10000000));
                 $('.zf_captcha').css('background-color','#ff6600').val('');
             }
         }, "json");
     } else {
         $('.zf_msg_etat').html('<div class="zenbox ui-state-highlight ui-corner-all">'+msgcc+'Certains champs sont incorrects ou manquants,<br /> vérifiez et essayez à nouveau.</div>').stop(true,true).fadeIn();
         setTimeout("$('.zf_form_overlay').remove();",300);
     }

    
}


function EffaceZF(t,dc,o) {
  if (CheckEdition()){
    ZenConfirm('<div class="avertissement">Êtes-vous certain de vouloir effacer ce formulaire ?</div>',
    function() {
      $.get('zen_traite_formulaires.php?zf_type='+t+'&efface_dc='+dc, function (rep) {
        if (rep.resultat === true) $(o).parent().parent().fadeOut();
        if (rep.msg) MsgAdmin(rep.msg);
        edition_active = false;
      },'json');
    },function() {edition_active = false;});
  }
}
