$(document).ready(initSite);

function initSite()
{
    $("#noJs").css('display','none');
    $('body').removeClass('noJs');
}
/*******************************************Fonctions gérant l'envoi de formulaire*******************************/
var idFormulaireTest = "";

function setIdFormTest(idFormNew)
{
    idFormulaireTest = idFormNew;   
}

function ecouteurForm(idFormulaire)
{
    idFormulaireTest = idFormulaire;
    $("#"+idFormulaire+" .row label:has(.obligatory)").each
    (
        function(i)
        {
             $(this).parent().children("input").addClass("obligatoireInput");
             $(this).parent().children("textarea").addClass("obligatoireInput");
             $(this).parent().children("select").addClass("obligatoireInput");
             var valInput = $(this).parent().children("input:not(#mailid)").attr("value");
             var valTextArea = $(this).parent().children("textarea").attr("value");
             var valMail = verifMail($("#mailid").attr("value"));
             var valSelect = $(this).parent().children('select').attr('value');
             
             if (valInput != null && valInput != "")
             {
                $(this).parent().children("input").attr("envoie","true");
             }
             else
             {
                $(this).parent().children("input").attr("envoie","false");
             }
             if (valTextArea != null && valTextArea != "")
             {
               $(this).parent().children("textarea").attr("envoie","true");
             }
             else
             {
               $(this).parent().children("textarea").attr("envoie","false");
             }
             if(valSelect != null && valSelect != "" && valSelect !="0")
             {
                $(this).parent().children("select").attr("envoie","true");
             }
             else
             {
                $(this).parent().children("select").attr("envoie","false");
             }
             if (valMail)
             {
                $("#mailid").attr("envoie","true"); 
             }
             else
             {
                $("#mailid").attr("envoie","false");
             }
        }
    );
    //autorisation exceptionnelle pour quelques inputs
    $("#nbPerspar").attr("envoie","true");
    $("#"+idFormulaire+" .row .obligatoireInput:not(#dateEventid):not(#nbPerspar)").blur(verifChamp);
    $("#"+idFormulaire).submit(envoieFormContact);
}

function verifChamp()
{
   var test = $(this).attr("id");
   var chaine = test.match('mail');
   if (chaine == "mail")
   {
      var addMail = $(this).attr("value");
      var validMail = verifMail(addMail);
      if (validMail)
      {
        $(this).parent().children('.errorInput').hide("slow");
        $(this).css("border","1px solid #a8a8a7");
        $(this).attr("envoie","true");
      }
      else
      {
         $(this).parent().children('.errorInput').show("slow");
         $(this).css("border","1px solid #FBAA1D");
         $(this).attr("envoie","false");
      }
   }
   else
   {
       var testSelect = $(this).attr('id');
       var chaineSelect = testSelect.match('Partyid');
       if (chaineSelect == "Partyid")
       {
            var verifVal = $(this).attr("value");
            if (verifVal == "0")
            {
                 $(this).parent().children('.errorInput').show("slow");
                 $(this).css("border","1px solid #FBAA1D");
                 $(this).attr("envoie","false");        
            }
            else
            {
                $(this).parent().children('.errorInput').hide("slow");
                $(this).css("border","1px solid #a8a8a7");
                $(this).attr("envoie","true");            
            }
       }
       else
       {
              entreeUser = $(this).attr("value");
              if(entreeUser != "" && entreeUser != null)
              {
                  $(this).parent().children('.errorInput').hide("slow");
                  $(this).css("border","1px solid #a8a8a7");
                  $(this).attr("envoie","true");
              }
              else
              {
                 $(this).parent().children('.errorInput').show("slow");
                 $(this).css("border","1px solid #FBAA1D");
                 $(this).attr("envoie","false");
              }
       }
   }
}

function verifDate()
{
  entreeUser = $(this).attr("value");
  if(entreeUser != "" && entreeUser != null)
  {
      $(this).parent().children('.errorInput').hide("slow");
      $(this).css("border","1px solid #a8a8a7");
      $(this).attr("envoie","true");
  }
  else
  {
     $(this).parent().children('.errorInput').show("slow");
     $(this).css("border","1px solid #FBAA1D");
     $(this).attr("envoie","false");
  }
}

function envoieFormContact()
{
    //alert("IdForm envoie :"+idFormulaireTest);
   var nbChampObli = $("#"+idFormulaireTest+".row .obligatoireInput").length;
   authEnvoie = null;
   $("#"+idFormulaireTest+" .row .obligatoireInput").each
   (
      function()
      {
        //alert("Avant Verif :"+authEnvoie+" sur l'element :"+$(this).attr("id"));
        var valeurInput = $(this).attr("envoie");
        if (valeurInput == "true")
        {
           if (authEnvoie != false || authEnvoie == null){authEnvoie = true;}
        }
        else
        {
           authEnvoie = false;
        }
        //alert("Apres Verif :"+authEnvoie);
      }
   );
   if(authEnvoie == true)
   {
      envoieForm();
      //alert("envoieMAil");
   }
   else
   {
      alert("on envoie pas");
      $("#"+idFormulaireTest).parent().parent().children(".msgUser").show("slow");
   }
   return false;
}

function envoieForm()
{
    if(idFormulaireTest == "formulaireContact")
    {
        //nom
        var inputNom = $('#nomid').attr('value');
        //prenom
        var inputPrenom = $("#prenomid").attr('value');
        //tel
        var inputTel = $("#telid").attr('value');
        //mail
        var inputMail = $("#mailid").attr('value');
        //selectPresta
        var inputPresta = $("#selectPartyid").attr('value');
        //nombre de personnes
        var inputNbPers = $("#nbPersid").attr('value');
        //lieu
        var inputLieu = $("#lieuEventid").attr('value');
        //dateEvent
        var inputEvent = $("#dateEventid").attr('value');
        //msg
        var inputMsg = $("#msgid").attr('value');
        $.post('ajax/envoieForm.php',
        {idForm:"formulaireContact",nom:inputNom,prenom:inputPrenom,tel:inputTel,mail:inputMail,presta:inputPresta,nbPers:inputNbPers,lieu:inputLieu,dateEvent:inputEvent,msgForm:inputMsg},
        function(data)
        {
            if(data == "ok")
            {
                $("#contact form").slideUp('slow',
                function()
                {
                    $("#contact #formulaire").css('display','none');
                    $("#contact .blocContact").css('width','100%');
                    $('#contact .msgConfirmContact').show('slow');
                }
                );
            }
            else
            {
                alert(data);
            }
        }
        );
    }
    else
    {
        //nom
        var nomPar = $('#nompar').attr('value');
        //prenom
        var prenomPar = $('#prenompar').attr('value');
        //telephone
        var telPar = $('#telpar').attr('value');
        //mail
        var mailPar = $('#mailpar').attr('value');
        //site web
        var webPar = $('#webpar').attr('value');
        //capacité salle
        var nbPersPar = $('#nbPerspar').attr('value');
        if (nbPersPar == "" || nbPersPar == null)
        {
            nbPersPar = "nonIndic";
        }
        //typePartenariat
        var typePar = $('#typepar').attr('value');
        //département
        var departementPar = $('#lieuEventpar').attr('value');
        //msgPartenaria
        var msgPar = $('#msgpar').attr('value');
         $.post('ajax/envoieForm.php',
        {idForm:"formulairePartena",nom:nomPar,prenom:prenomPar,tel:telPar,mail:mailPar,siteweb:webPar,partenariat:typePar,nbPersSalle:nbPersPar,departement:departementPar,msgPart:msgPar},
        function(data)
        {
            if(data == "ok")
            {
                $('#contactPartenaire form').slideUp('slow',
                function()
                {
                    $("#formulairePartenariat").css('display','none');
                    $("#contactPartenaire .blocContact").css('width','100%'); 
                    $('#contactPartenaire .msgConfirmContact').show('slow');                  
                }
                );
                
            }
            else
            {
                alert(data);
            }
        }
        );
    }
}
/******************************************Fonctions utilées pour les formulaires*******************************/
function verifMail(mailteste)

{
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');

	if(reg.test(mailteste))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}
