
////////////////////////////////
//Créateur : Yves Prince		yprince@odacia-studio.com
//Dernière Mise à jour : 22 novembre 2007
//Par : Yves Prince		yprince@odacia-studio.com
//
//Validation des formulaires
////////////////////////////////

//trims blank spaces
function trim(val)
{
	if (!val) return val;

	while(val.charAt(0) == ' ') val = val.substring(1,val.length);
	while(val.charAt(length)==' ')  val = val.substring(0,val.length-1);
	
	return val;
}

function bug(champ)
{
	if(nobug)
	eval('leForm.'+champ+'.focus()')
	
	nobug=false
}

nobug=false;
bgNormal=bgErreur=bgInvalid='#FFFFFF';
//bgErreur='#FCF38A';
//bgInvalid='#F1B1B1';
bNormal='#7F9DB9';
bErreur='#FF0000';
bInvalid='#FF0000';

trouvLang=true;
function validerGeneral(champ,Reset)
{
	getValue(champ);
	
	if (!Reset && valeur=='')
	{
		if(laLang=='fr')
		{
			msg='Veuillez entrer votre ';
			
			if(champ=='msg')
			msg+='message.';
			else
			msg+=champ+'.';
		}
		else
		{
			msg='Please enter your ';
			
			if(champ=='nom')
			msg+='name.';
			else if(champ=='msg')
			msg+='message.';
		}
		
		document.getElementById('span'+champ).innerHTML = msg;
		leChamp.style.backgroundColor = bgErreur;
		leChamp.style.borderColor = bErreur;
		
		if(nobug)
		bug(champ);
	}
	else
	{
		document.getElementById('span'+champ).innerHTML = '';
		leChamp.style.backgroundColor = bgNormal;
		leChamp.style.borderColor = bNormal;
	}
}

function validerTel(champ,Reset)
{
	getValue(champ);
	
    var stripped = valeur.replace(/[\(\)\.\-\_\ ]/g,'');    
	
	if (!Reset && valeur=='')
	{	
		if(laLang=='fr')
		msg='Veuillez entrer votre numéro.';
		else
		msg='Please enter your number.';
		
		document.getElementById('span'+champ).innerHTML = msg;
		leChamp.style.backgroundColor = bgErreur;
		leChamp.style.borderColor = bErreur;
		
		if(nobug)
		bug(champ);
	}
	else if(!Reset && (isNaN(stripped) || stripped.length<10))
	{
		if(laLang=='fr')
		msg='Votre numéro n\'est pas valide.';
		else
		msg='Your number isn\'t valid.';
		
		document.getElementById('span'+champ).innerHTML = msg;
		leChamp.style.backgroundColor = bgInvalid;
		leChamp.style.borderColor = bInvalid;
		
		if(nobug)
		bug(champ);
	}
	else
	{
		document.getElementById('span'+champ).innerHTML = '';
		leChamp.style.backgroundColor = bgNormal;
		leChamp.style.borderColor = bNormal;
	}
}

function validerEmail(champ,Reset)
{
	getValue(champ);
	
	if (!Reset && valeur=='')
	{
		if(laLang=='fr')
		msg='Veuillez entrer votre courriel.';
		else
		msg='Please enter your email.';
		
		document.getElementById('span'+champ).innerHTML = msg;
		leChamp.style.backgroundColor = bgErreur;
		leChamp.style.borderColor = bErreur;
		
		if(nobug)
		bug(champ);
	}
	else if(!Reset && !validEmail(valeur))
	{
		if(laLang=='fr')
		msg='Votre courriel n\'est pas valide.';
		else
		msg='Your email isn\'t valid.';
		
		document.getElementById('span'+champ).innerHTML = msg;
		leChamp.style.backgroundColor = bgInvalid;
		leChamp.style.borderColor = bInvalid;
		
		if(nobug)
		bug(champ);
	}
	else
	{
		document.getElementById('span'+champ).innerHTML = '';
		leChamp.style.backgroundColor = bgNormal;
		leChamp.style.borderColor = bNormal;
	}
}

function validEmail(email)
{
	if (email.length>=7)
	{
		domaine=false
		if ((email.indexOf(".",email.indexOf("@")))<(email.indexOf("@")+3))
		domaine=true
		
		if (!domaine)
		{
			acom=false
			if ((email.indexOf("@",email.lastIndexOf("."))!=-1) || (email.indexOf("@")!=email.lastIndexOf("@")) || (email.indexOf("@")<1))
			acom=true
			
			if (!acom)
			{
				nbpoints=false
				combien=0
				for (y=email.indexOf("@"); y<email.length; y++)
				{
					if (email.charAt(y)==".")
					combien++
					
					if (combien==3)
					break
				}
				
				if (((combien==2) && (email.indexOf(".",email.indexOf("@"))!=email.lastIndexOf(".")-3) && (email.indexOf(".",email.indexOf("@"))!=email.lastIndexOf(".")-5)) || (combien>3))
				nbpoints=true
				
				if (!nbpoints)
				{
					extension=false
					if (combien==1)
					{
						if ((email.lastIndexOf(".")>(email.length-3)) || (email.lastIndexOf(".")<(email.length-5)))
						extension=true
					}
					else if (combien==2)
					{
						if (email.lastIndexOf(".")!=(email.length-3))
						extension=true
					}
					else if (combien==3)
					{
						if ((email.charAt(email.indexOf(".",email.indexOf("@"))+5)!=".") || (email.charAt(email.lastIndexOf(".")-3)!=".") || (email.lastIndexOf(".")!=(email.length-3)))
						extension=true
					}
					
					if (!extension)
					{
						points=false
						if ((email.charAt(email.indexOf(".")+1)==".") || (email.indexOf(".",email.indexOf("@"))==-1) || (email.indexOf(".")==0) || (email.charAt(email.indexOf("@")-1)=="."))
						points=true
						
						if (!points)
						{
							leschar=false
							charValid="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@._-"
							for (x=0; x<email.length; x++)
							{
								if (charValid.indexOf(email.charAt(x))==-1)
								{
									leschar=true
									break
								}
							}
						}
					}
				}
			}
		}
	}
	
	if ((email.length<7) || domaine || acom || nbpoints || extension || points || leschar)
	return false
	else
	return true
}

function getValue(champ)
{
	if(trouvLang)
	{
		laLang=document.getElementById('langForm').value;
		trouvLang=false;
	}
	
	leChamp = document.getElementById('txt'+champ);
	valeur = leChamp.value = trim(leChamp.value);
}