$(document).ready(function(){

	var email =    /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; //Adresses Mail
	var number = /^[-]?\d*\.?\d*$/; // Nombre
	var length5 = /\b.{5}\b/; // Longueur de 5 caractères
	$('input.donnees').blur(function()
	{
		testFormat(this);
	});
	$('textarea.donnees').blur(function()
	{
		testFormat(this);
	});
	$('select.donnees').blur(function()
	{
		testFormat(this);
	});
	$('select[multiple]').change(function()
	{
		testFormat(this);
	});
	$(':radio').change(function()
	{
		testFormat(this);
	});
	$(':checkbox').change(function()
	{
		testFormat(this);
	});
});

function testFormat(id){

	var email =    /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; //Adresses Mail
	var number = /^[-]?\d*\.?\d*$/; // Nombre
	var length5 = /\b.{5}\b/; // Longueur de 5 caractères
	var test_ok = false;
	var test_format = true;

	var type = $(id).attr("type");
	if($(id).attr("obligatoire") == 'oui')
	{
		if(type=="radio" || type=="checkbox"){
			if($(id).attr("checked")==true)
			{
				test_ok = true;
			}
		}else {
			if($(id).attr("format") == 'email' && !$(id).val().match(email))
			{
				//test_ok = true;
				test_format = false;
			}
			if($(id).attr("format") == 'number' && !$(id).val().match(number))
			{
				//test_ok = true;
				test_format = false;
			}
			if($(id).attr("format") == 'length5' && !$(id).val().match(length5))
			{
				//test_ok = true;
				test_format = false;
			}
			if(($(id).val() != '' && $(id).val() != null && $(id).val() != "NULL") && test_format)
			{
				test_ok = true;
			}
		}
		if(test_ok)
		{
			$(id).removeClass("field_error");
			var name = $(id).attr("name");
			name=name.replace("[","").replace("]","");
			$("#lbl_"+name).removeClass("label_error");
		}
	}
}

function alerte(id,name)
{
	if($(id).css("display")!="none")
	{
		$("#"+name).addClass("field_error");

	}
	name=name.replace("[","").replace("]","");
	$("#lbl_"+name).addClass("label_error");
}

function check_formular(frm,typeMsg,onglet)
{
	var champs = new Array;
	var champsControle = new Array;
	var nbChamps = 0;
	var champfocus="";

	var email =    /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; //Adresses Mail
	var number = /^[-]?\d*\.?\d*$/; // Nombre
	var length5 = /\b.{5}\b/; // Longueur de 5 caractères
	var trigger = true;

	$(":input",frm).each(function()
	{

		//		if($(this).css("display")!="none")
		//		{
		var valeur = $(this).val();
		var estObligatoire = $(this).attr("obligatoire");
		var format = $(this).attr("format");
		var nom = $(this).attr("name");
		var type = $(this).attr("type");
		var estControle = false;
		if(estObligatoire == "oui")
		{

			if(valeur){
				if(type=="radio" || type=="checkbox"){
					if(in_array(champsControle,nom)===false){
						estControle = false;
						$(":input[name="+nom+"]",frm).each(function()
						{
							if($(this).attr("checked")==true)
							{
									estControle = true;
							}
						});
						if(estControle==false){
							alerte(this,nom);

							champs[nbChamps] = $(this).attr("texteObligatoire");
							nbChamps++;
							if(champfocus==""){
								champfocus = this;
							}

							trigger = false;
							estControle = true;
						}
					}

					champsControle[nbChamps] = nom;

				}else if(valeur == '' || valeur == "NULL")
				{
					alerte(this,nom);

					champs[nbChamps] = $(this).attr("texteObligatoire");
					nbChamps++;
					if(champfocus==""){
						champfocus = this;
					}

					trigger = false;
				}
				if(type!="select-multiple" && trigger){
					if(format == 'email' && !valeur.match(email))
					{
						alerte(this,nom);

						champs[nbChamps] = $(this).attr("texteObligatoire");
						nbChamps++;
						if(champfocus==""){
							champfocus = this;
						}

						trigger = false;
					}
					if(format == 'number' && !valeur.match(number))
					{
						alerte(this,nom);

						champs[nbChamps] = $(this).attr("texteObligatoire");
						nbChamps++;
						if(champfocus==""){
							champfocus = this;
						}

						trigger = false;
					}
					if(format == 'length5' && !valeur.match(length5))
					{
						alerte(this,nom);

						champs[nbChamps] = $(this).attr("texteObligatoire");
						nbChamps++;
						if(champfocus==""){
							champfocus = this;
						}

						trigger = false;
					}
				}
			}else{
				alerte(this,nom);

				champs[nbChamps] = $(this).attr("texteObligatoire");
				nbChamps++;
				if(champfocus==""){
					champfocus = this;
				}

				trigger = false;
			}

		}
		//		}
	});

	if(trigger){
		$("#error_div").slideUp("fast");
	} else {

		$("#error_div").css("text-align","center");
		var chaine = "Un ou plusieurs champs du formulaire ne sont pas remplis.<br/>Merci de corriger ce(s) erreur(s) puis de valider &agrave; nouveau votre formulaire.";
		if(typeMsg=="champ"){

			if(nbChamps == 1){
				chaine = "Le champ " + champs[0] + " est obligatoire.";
			} else {
				$("#error_div").css("text-align","left");
				chaine = "Les champs suivants sont obligatoires ou ont un format incorrect :<br/>";
				for(champ in champs){
					chaine = chaine + "   - " + champs[champ] + "<br/>";
				}
			}
		}
			if(onglet){
				var activeOnglet = $(champfocus).attr("onglet");
				$('#accordion').accordion( 'activate' , $('#'+activeOnglet) );
			}
		if($(champfocus).attr("type")!="select-multiple"){
			$(champfocus).focus();
		}
		$("#error_div").html(chaine);
		$("#error_div").slideDown("fast");
		$("#ok_div").slideUp("fast");
	}
	return trigger;
}

function in_array(array, p_val) {
	for(var i = 0, l = array.length; i < l; i++) {
		//    	alert(array[i]);
		if(array[i] == p_val) {
			rowid = i;
			return true;
		}
	}
	return false;
}
