/******************************
 * 
 * Class NewsLetterValidator  *
 * 
 *****************************/
(function(){
  
  NewsLetterValidator = function(){
    this.openBtn = $("#nl_validate");
    this.inputLibelle = $("#nl_subscribe_libelle");
    this.defaultLibelle = "S'abonner à la newsletter";
    this.userDatas = [];
    this.initListeners(this);
  };
  
  NewsLetterValidator.prototype.initListeners = function(obj){
    obj.openBtn.click(function(evt){
      evt.preventDefault();
      obj.openNlForm(obj);
    });
    
    this.inputLibelle.focus(function(evt){
      if($(this).val() == obj.defaultLibelle){
        $(this).val(""); 
      }
    });
    
    this.inputLibelle.blur(function(evt){
      if($.trim($(this).val()) == ""){
        $(this).val(obj.defaultLibelle); 
      }
    });
    
  };
  
  
  NewsLetterValidator.prototype.openNlForm = function(obj){
    var close2 = $("#close_popup").clone();
    $("#close_popup").remove();
    $("#form_nl").append(close2);
     var contblk = $('<div class="contact_page">');
     contblk.load(ROOT_URL + "form_template.php");
     
     MyLightBox.create({
       content:contblk,
       parentElt:"form_nl",
       onClose:{
         func_name:obj.closeLb,
         args:obj
       },
       onOpenAction:{
         func_name:obj.initValiderForm,
         args:obj
       }
     });
     MyLightBox.open();
  };
  
  
  NewsLetterValidator.prototype.initValiderForm = function(obj){
   obj.requieredField = $(".required_field");
   var email = obj.inputLibelle.val();
   if($.trim(email) != obj.defaultLibelle){
     $("#contact_mail").val(email);
   }
   
    $("#form_valider").click(function(evt){
      evt.preventDefault();
      var result = obj.checkDatas(obj);
      
      if(result){
        var url = ROOT_URL +"informations.php";
        var datas = obj.userDatas;
        datas.push("action=nl_subscribe");
        
        $.ajax({
          url:url,
          data:datas.join('&'),
          type:'post',
          dataType:'json',
          success:function(responseJSON, responseDATA, responseXML){
            if(responseJSON.success){
              $("#form_content").remove();
              $("#form_success_msg").text("Votre inscription a été enregistrée");
            }else{
               obj.addErrorMessage(responseJSON.error_msg);
            }
          },
          error:function(){
            
          }
        });
      }
    });
  };
  
  
 
  
  NewsLetterValidator.prototype.checkDatas = function(obj){
    obj.removeErrorMessage();
    var error = [];
    obj.userDatas = [];
    
    obj.requieredField.each(function(index, elt){
      if($.trim($(this).val()) == ""){
        error.push(index);
      }
      obj.userDatas.push($(this).attr("name")+"="+$.trim($(this).val()));
    });
    
    
    if(error.length){ 
      obj.addErrorMessage("Vous devez renseigner tous les champs");
      return false;
    }
    
    if($("#optin_aviva").attr("checked")== "checked"){
      obj.userDatas.push("nl_optin_aviva=1");
    }
    
    if($("#optin_partenaires").attr("checked")== "checked"){
      obj.userDatas.push("nl_optin_partenaires=1");
    }
    
    if($("#projet_cuisine").attr("checked")== "checked" ){
      obj.userDatas.push("nl_projet_cuisine=1");
    }
    var foundCiv = false;
    $(".form_civ").each(function(index, elt){
      if($(this).attr("checked") == "checked"){
        obj.userDatas.push("nl_civilite="+$(this).val());
        foundCiv = true;
      }
    });
    
    if(!foundCiv){
      error.push("Vous devez indiquer votre civilité");
      obj.addErrorMessage("Vous devez indiquer votre civilité");
      return false;
    }
    
    return true;
  };
  
  
  
  
  NewsLetterValidator.prototype.removeErrorMessage = function(){
    $("#candidature_form_error").children().remove();
  };
  
  
  
  NewsLetterValidator.prototype.addErrorMessage = function(msg){
    $("#candidature_form_error").append('<span>'+msg+'</span>');
  };
  
  
  NewsLetterValidator.prototype.closeLb = function(obj){
    $("#form_valider").unbind("click");
    MyLightBox.close(MyLightBox.destroy);
  };
  
})();
// end Class NewsLetterValidator
