/* ======================================================================================*/
/*				 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 = /^[^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 zipcode = form.zipcode.value;
  var email = form.email.value;
  var tel = form.tel.value;
  var message = form.message.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;
  
  //tel-----
  if(!testObl(tel,"tel")) return false;
  if(!testMatchRegex(tel,"tel",telRegex)) return false;
  
  //email-----
  if(!testMatchRegex(email,"email",emailRegex)) return false;
 
      
  return true;
}

function submitFormContact()
{		
	if(validFormContact(document.getElementById('myContactForm'))) 
	{			
		document.getElementById('myContactForm').submit();
	}
	else
	{			
		return false;
	}
}

