
function getContacts(id, lang, preselect_id)
{
  $.ajax({
    type: "GET",
    url: "/ctrl_front.php",
    data: "fct=getContacts&id="+id+"&lang="+lang,
    success: function(msg){
      $('select[name="contact_id"]').html(msg);
      if(preselect_id != '')
      {
        $('select[name="contact_id"] option[value="'+preselect_id+'"]').attr('selected', 'selected');
      }
    }
  });
}

function checkMail(mail)
{
  //pour tester email
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  return reg.test(mail);
}

function recharge()
{
    window.location.reload();
}


/* function init_formulaire
 * Initialise les controlles js du formulaire
 * @param : integer : id_service - Id du service préselectionnée
 * @param : integer : id_personne - Id de la personne préselectionnée 
 *  
 */
function init_formulaire( id_service, id_personne )
{
  highlight_form();

  var lang = $('input[name="lang"]').val();
  
  if( id_service != '' &&  id_personne != '')
  {
    $('#service_id option[value='+id_service+']').attr("selected", "selected");
    getContacts(id_service, lang, id_personne);
  }
  
  //récupère les contacts quand le service change
  $('select[name="service_id"]').change(function(){
    getContacts($(this).val(), lang, '');
  });
  
  /** AJAX FORM **/
  var options = {
    dataType:  'json',
    beforeSubmit:  function() {
      
      //Vérification standard
      v_std = verif_chp_formulaire("form_contact", ['nom', 'societe', 'service_id', 'contact_id', 'objet', 'message'] );
      //Vérification des champs spéciaux
      v_email = verif_email("email");      
     
      //Si toutes les vérifications sont bonnes (ont retournées true)
      if(v_std && v_email)
        return true;
      else 
        return false;
    },  // pre-submit callback
    success:       function(data) {
      if(data.success)
      { 
        $.nyroModalManual({content : "<h4 class=\"success\">"+data.success+"</h4>"});
        $( "#form_contact" ).resetForm();
         setTimeout($.nyroModalRemove, 2000);
         
      }
      else
      {
        $.nyroModalManual({content : '<h4 class="error">'+data.error+'</h4>'});
        setTimeout($.nyroModalRemove, 2000);
      }
    }
  };

  $('#form_contact').ajaxForm(options);
}     
