/* ======================================================================================*/
/*				 FONCTIONS GÉNÉRIQUES DE VALIDATION DE FORMULAIRE 						 */
/* ======================================================================================*/
//---------------------------------------------
// Empeche la saisie d'un caractere onKeypress
//----------------------------------------------
function empechechar()
{
	if( event.keyCode < 48 || event.keyCode > 57 )
 	{ 
 		alert(get_trad_champ("numeric") );
   		event.returnValue = false;
   	}
}

//-------------------------------------
// Verifie la validite d'un email
//-------------------------------------
function isValidEmail ( _fieldId ) {
	
	var email = document.getElmentById(_fieldId).value;		
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if ( !filter.test(email.value) ) 
	{		
		return false;
	}
	else 
	{
		return true;
	}
}

//-------------------------------------
// Verifie la validite d'une date
//-------------------------------------
function isValidDate ( _fieldId )
{
    if ( _fieldId == "" )
        return true;

    // Récupération de la valeur
    var totalDate    = document.getElementById( _fieldId ).value;
    if ( totalDate == "" )
        return false;
   
    // Découpage de la date récupérée
    var aDate = totalDate.split( "/" );
    if ( aDate.length != 3 )
        return false;

    // Création d'un objet date
    var generatedDate = new Date ( aDate[ 2 ], aDate[ 1 ] - 1, aDate[ 0 ] );
   
    // Test
    if (     generatedDate.getFullYear() == aDate[ 2 ]
        &&    generatedDate.getMonth() + 1 == aDate[ 1 ]
        &&    generatedDate.getDate() == aDate[ 0 ] )
        return true;
   
    return false;
}

//-------------------------------------
// Test criteres form
//-------------------------------------
//---test champ obligatoire
function testObl(fieldValue,fieldName)
{	
	if(fieldValue.trim()=="")
	{	
		inlineMsg(fieldName,get_trad_champ(fieldName),2);
	    return false;
	}
	else
	{		
		return true
	}
}
//--test match expression reg
function testMatchRegex(fieldValue,fieldName,myRegex)
{	
	if(!fieldValue.match(myRegex)) 
  	{
	    inlineMsg(fieldName,get_trad_champ(fieldName+"_incorrect"),2);
	    return false;
  	}  
  	else
  	{
  		return true;
  	}
}

//--test longueur du champ
function testLength(fieldValue,fieldName,fieldLength)
{
	if( parseInt(fieldValue.trim().length) < fieldLength)
	{			
	  	inlineMsg(fieldName,get_trad_champ(fieldLength+"_caracteres_min"),2);
	  	return false;
	}
	else
	{
		return true;
	}
}

/* ======================================================================================*/
/*							 FONCTIONS GÉNÉRIQUES										 */
/* ======================================================================================*/
String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
}
/* ======================================================================================*/
/*				 FONCTIONS D APPELS LIES AU PROJET				 						 */
/* ======================================================================================*/
//--------------------------------------
// Validation form contact 
//--------------------------------------
function validFormContact(form)
{	  
	
  /*var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;*/
  /*var nameRegex = /^[^0-9]+(([\'\,\.\- ][a-zA-Z ])?[^0-9]*)*$/;  */
  var nameRegex = /^[^0-9]*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,10}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  var telRegex = /^[0-9\(\)\+ ]*$/;
  
  //==========================  
  //declaration de variables
  //==========================
  var objet = form.objet.value;
  var nom = form.nom.value;
  var prenom = form.prenom.value;
  var fonction = form.fonction.value;
  var societeNom = form.societeNom.value;
  var secteur = form.secteur.value;
  var societeEffectif = form.societeEffectif.value;
  var zipcode = form.zipcode.value;
  var pays = form.pays.value;
  var email = form.email.value;
  var tel = form.tel.value;
  var message = form.message.value;
//  var connu = form.connu.value;  
  //var code = form.code.value;
  
  var shareInfo = "";
  for(var i=0;i<form.shareInfo.length;i++)
  {
	  if(form.shareInfo[i].checked)
	  {
	  	var shareInfo = form.shareInfo[i].value;	  		  	
	  }
  }
    
  //==========================  
  //controles JS
  //==========================      
    
  //objet du message
  if(!testObl(objet,"objet")) return false;

  //nom-----
  if(!testObl(nom,"nom")) return false;
  if(!testMatchRegex(nom,"nom",nameRegex)) return false;  
  if(!testLength(nom,"nom",3)) return false;
  
  //prenom ------
  if(!testObl(prenom,"prenom")) return false;
  if(!testMatchRegex(prenom,"prenom",nameRegex)) return false;
  if(!testLength(prenom,"prenom",3)) return false;
  
  //fonction-----
  if(!testObl(fonction,"fonction")) return false;
  if(!testMatchRegex(fonction,"fonction",nameRegex)) return false;
  if(!testLength(fonction,"fonction",3)) return false;
  
  //societeNom-----
  if(!testObl(societeNom,"societeNom")) return false;  
  if(!testLength(societeNom,"societeNom",3)) return false;
    
  //secteur-----
  //if(!testObl(secteur,"secteur")) return false;
  //if(!testMatchRegex(secteur,"secteur",nameRegex)) return false;
  //if(!testLength(secteur,"secteur",3)) return false;
  
  //societeEffectif-----
  if(!testObl(societeEffectif,"societeEffectif")) return false;
  
  //pays-----
  //if(!testObl(pays,"pays")) return false;  
  
  //tel-----
  if(!testObl(tel,"tel")) return false;
  if(!testMatchRegex(tel,"tel",telRegex)) return false;
  
  //email-----
  if(!testMatchRegex(email,"email",emailRegex)) return false;
  
  //vous avez connu INES ---
//   if(!testObl(connu,"connu")) return false;   
   
   //message --
  // if(!testObl(message,"message")) return false;
   //if(!testLength(message,"message",3)) return false;
   
   //code
   //if(!testObl(code,"code")) return false;
   
   //shareInfos   
   if(!testObl(shareInfo,"shareInfo_2")) return false;   
      
  return true;
}

function submitFormContact()
{		
	if(validFormContact(document.getElementById('myContactForm'))) 
	{	
		document.getElementById('myContactForm').submit();
	}
	else
	{			
		return false;
	}
}