function verifieForm(elmt){
	val = elmt.val().replace(/^\s*/,'').replace(/\s*$/,'');
	elmt.val(val);

	var testdonnee = false;
	var classes = elmt.attr('class').split(' ');
	var oblig = classes[classes.indexOf('controle')+1]=='oblig' || val!='';
	var typeDonnee = classes[classes.indexOf('controle')+2];

	var expville = /^[àéèâôîïùêëûa-zA-Z-\'\s]{2,}$/; // ville
	var exptxt = /^[àéèâôîïùêëûa-zA-Z-\'\s]{2,}$/; // texte uniquement 
	//var expmix = /^.{2,}$/; // texte et nombres (ex. adresses)
	var expnum = /^[0-9]{2,16}$/; // chiffres et nombres uniquement
	var expnum2 = /^[1-9]{1}|[0-9]{2,16}$/; // chiffres et nombres uniquement
	var exptel = /^[ /\()+.0-9]{10,20}$/; // No tél
	var expcp = /^[0-9]{5,5}$/; // code postal
	var expdate = /^[0-9]{2,2}\/[0-9]{2,2}\/[0-9]{4,4}$/; //date
	var expmail = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/; //email
	var expurl = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;//adresse url
	var exprew = /^[0-9a-zA-Z-]{2,50}$/;//rewriting

	if(oblig){
		if(typeDonnee!=''){
			if(typeDonnee == 'mix')
				testdonnee = val.length>2;
			else{
				if(typeDonnee == 'notnul')
					testdonnee = val!='' && val!='0';
				else{
					expcourrante = eval('exp'+typeDonnee);
					testdonnee = expcourrante.test(val);
					//console.log('exp' + typeDonnee + '.test("'+elmt.val()+'")');
				}
			}
		}

		//if(elmt.is('textarea')) console.log('exp' + typeDonnee + '.test("'+elmt.val()+'")');

		if(typeDonnee!='' && !testdonnee){
			elmt.parent().addClass('error');
			return false;
		}
	}
	elmt.parent().removeClass('error');
	return true;
}

/**
 * Gestion des evenements
 */
$(function(){
	$('.controle').live('focusout', function(){
		verifieForm($(this));
	});

	//envoi en ajax.
	$('.formsubmit').live('click', function(){
		var formjson = {ajax : true};
		var formu = $(this).parents('form:first');
		var $link = formu.attr('action');
		var ok = true;
		
		formu.find('.controle').each(function(){
			if(!verifieForm($(this))){
				if(ok){
					ok = false;
					$(this).focus();
				}
			}
			else
				formjson[$(this).attr('name')] = $(this).val();
		});

		if(ok){
			$('#contact-form').load($link, formjson, function(){
				try{
					_gaq.push(['_setAccount', gaIdCode]);
					_gaq.push(["_trackPageview", "/"+$link ]);
				}
				catch(err){}
			});
			setTimeout("$('.popup').fadeOut('slow');", 4000);
		}
		
		return false;
	});

	//envoi normal.
	$('.formsubmit2').live('click', function(){
		var nompage = $(this).attr('id');
		var formu = $(this).parents('form:first');
		var ok = true;
		var $link = $(this).attr('href');
		
		formu.find('.controle').each(function(){
			if(!verifieForm($(this))){
				if(ok){
					ok = false;
					$(this).focus();
				}
			}
		});
		
		if(ok){
			if(formu.find('input[name=page]').length>0)
				formu.find('input[name=page]').val(nompage);
			formu.attr('action', $link);
			formu.removeAttr('target');

			formu.submit();
		}

		return false;
	});

	$('input.autocomp').each(function(){
		var input = $(this).attr('name');
		$(this).autocomplete({
			source : "contact/autocomp.html?input="+input
		});
	});
});
