var regularExpressions = {
	txt		: /^[a-zA-Zàáâãäåçèéêëìíîïðòóôõöùúûüýÿ\s]{2,}$/, // texte uniquement
	txt2	: /^[0-9a-zA-Zàáâãäåçèéêëìíîïðòóôõöùúûüýÿ\s]{2,}$/, // texte uniquement
	num		: /^[0-9\s]+$/, // chiffres et nombres uniquement
	num2	: /^[1-9]{1}|[0-9]{2,16}$/, // chiffres et nombres uniquement
	tel		: /^[ /\()+.0-9]{10,20}$/, // No tél
	cp		: /^[0-9]{5,5}$/, // code postal
	heure	: /^[0-9]{2,2}:[0-9]{2,2}$/, // date
	date	: /^[0-9]{2,2}\/[0-9]{2,2}\/[0-9]{4,4}$/, // date
	mail	: /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/, // email
	url		: /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/, // adresse url
	rew		: /^[0-9a-z-]{1,50}$/ // rewriting
}

function verifieForm(elmt){
	val = elmt.val();
	val = $.trim(val); // val.replace(/^\s*/,'').replace(/\s*$/,'');
	elmt.val(val);
	
	var testdonnee = true;
	var classes = elmt.attr('class').split(' ');
	var oblig = classes[$.inArray('controle', classes) + 1] == 'oblig' || val != '';
	var typeDonnee = classes[$.inArray('controle', classes) + 2];
	
	if (elmt.hasClass('verification')) {
		$('.verification').not(elmt).each(function(){
			if ($(this).val() != val)
				testdonnee = false;
		});
	}
	
	if(testdonnee && oblig && typeDonnee != ''){
		if(typeDonnee == 'mix' || typeDonnee == 'file')
			testdonnee = val.length > 2;
		else {
			if(typeDonnee == 'notnul')
				testdonnee = (val != '' && val != '0');
			else{
				expcourrante = regularExpressions[typeDonnee];
				testdonnee = expcourrante.test(val);
			}
		}
	}
	
	if(testdonnee == false){
		elmt.parent().addClass('error');
		return false;
	}
	
	elmt.parent().removeClass('error');
	return true;
}

/**
 * Gestion des evenements
 */
$(function(){
	//envoi normal.
	$('form').live('submit', function(){
		var ok = true;
		
		$('.controle:visible', $(this)).each(function(){
			if(!verifieForm($(this))){
				var $this = $(this);
				if(ok){
					ok = false;
					var haut = $(this).parent().position().top-10;
					$('html, body').animate({scrollTop : haut}, 'fast', function(){
						$this.focus();
					});
				}
			}
		});
		
		if (ok)
			return true;

		return false;
	});
	
	$('#professionnel').click(function(){
		if ($(this).is(':checked'))		$('.form-pro:hidden').slideDown(500);
		else							$('.form-pro:visible').slideUp(500);
	});

	$('#particulier').click(function(){
		if (!$(this).is(':checked'))	$('.form-pro:hidden').slideDown(500);
		else							$('.form-pro:visible').slideUp(500);
	})
});
