var a=0;
var errorMessage="";

function validate(formData, jqForm, options) {

	errorMessage="";
	var aEError="";
	var verifString="";
	b="";
	var oT=new Array;
	var toT=new Array;
	i=0;
	$('.lang').each( function() {
		a=$(this).attr("id");
		a=a.toString();
		c=$(this).css("display");
		c=c.toString();
		if(c!="block") {
			a=$('div[id="'+a+'"] input[id^="toText"]').attr("id");
			oT[i]=a;
			i++;
		}
	});
	$('.lang2').each( function() {
		a=$(this).attr("id");
		a=a.toString();
		c=$(this).css("display");
		c=c.toString();
		if(c!="block") {
			a=$('div[id="'+a+'"] input[id^="toText"]').attr("id");
			oT[i]=a;
			i++;
		}
	});
	$('.lang2').each( function() {
		a=$(this).attr("id");
		a=a.toString();
		c=$(this).css("display");
		c=c.toString();
		if(c!="block") {
			a=$('div[id="'+a+'"] input[id^="title_"]').attr("id");
			oT[i]=a;
			i++;
		}
	});
	//Passage des values des éditeurs de texte aux champs cachés 
	tV();
	
	//Verification des boutons radios 
	$("input:radio").each(function() {
								   
		if($(this).attr("valid")=="1"){
			if(($(this).attr("cName"))&&($(this).attr("cName")!="0")){
				cNames=$(this).attr("cName");
				cNames=cNames.split(/;/g);
				fieldName=cNames[0];
			} else if(($(this).attr("cName"))&&($(this).attr("cName")=="0")){
				fieldName="";
			} else {
				fieldName=$(this).attr("name");
			}
			a=$(this).attr("name");
			a=a.toString();
			if($("input:radio[name='"+a+"']").fieldValue()) {value=$("input:radio[name='"+a+"']").fieldValue();} else {value="";}
			value=value.toString();
			if((value=="") && (a!=b) && (fieldName!="")){
				errorMessage+=simpleText("case")+fieldName+simpleText("coche");
				if(cNames[1] && cNames[1]!="") {errorMessage+=" ("+cNames[1]+")";}
				errorMessage+="\n";
			}
			b=a;
		}
	});

	//Vérification des cases à cocher
	previousCBox="";
	previousMessage="";
	$("input:checkbox").each(function() {
		if(($(this).attr("cName"))&&($(this).attr("cName")!="0")){
			cNames=$(this).attr("cName");
			cNames=cNames.split(/;/g);
			fieldName=cNames[0];
		} else if(($(this).attr("cName"))&&($(this).attr("cName")=="0")){
			fieldName="";
		} else {
			fieldName=$(this).attr("name");
		}
		name=$(this).attr("name");
		if(previousCBox!=fieldName){
			if($(this).attr("valid")=="1"){
				coll=$('input:checkbox[name="'+name+'"]');
				conf=0;
				for(w=0;w<coll.length;w++){
					a=$('input:checkbox[name="'+name+'"]:eq('+w+')')
					if(a.attr("checked")){conf++;}
				}
				if(conf==0){
					errorMessage+=simpleText("case")+fieldName+simpleText("coche");
					if(cNames[1] && cNames[1]!="") {errorMessage+=" ("+cNames[1]+")";}
					errorMessage+="\n";
				}
			}
		}
		if($(this).attr("valid")=="mbc"){
			if(!$(this).attr("checked") && (fieldName!="")){
				if(previousMessage!=simpleText("case")+fieldName+simpleText("coche")){
					errorMessage+=simpleText("case")+fieldName+simpleText("coche");
					previousMessage=simpleText("case")+fieldName+simpleText("coche");
					if(cNames[1] && cNames[1]!="") {errorMessage+=" ("+cNames[1]+")";}
					errorMessage+="\n";
					previousMessage=simpleText("case")+fieldName+simpleText("coche");
				}
			}
		}
		previousCBox=fieldName;
	});
	//Verification des champs de texte
	verifString=0;
	previousMessage="";
	$("input").each(function() {
			if(($(this).attr("cName"))&&($(this).attr("cName")!="0")){
				cNames=$(this).attr("cName");
				cNames=cNames.split(/;/g);
				fieldName=cNames[0];
			} else if(($(this).attr("cName"))&&($(this).attr("cName")=="0")){
				fieldName="";
			} else {
				fieldName=$(this).attr("name");
			}
		if(((!$(this).attr("disabled"))||($(this).attr("disabled")=="")) && (($(this).attr("type")=="text")||($(this).attr("type")=="password")||($(this).attr("type")=="hidden"))){
			if($(this).attr("aExists")){
				c=0;
				rqt=($(this).attr("aExists")).toString()+";"+($(this).fieldValue()).toString();
				c=alreadyExists(rqt);
				if(c>0) {
					aEError+=simpleText("champVal")+fieldName+simpleText("existe");
					if(cNames[1] && cNames[1]!="") {aEError+=" ("+cNames[1]+")";}
					aEError+="\n";
				}
			}
			if($(this).attr("valid")=="reg"){
				verifReg=new RegExp("[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]([a-zA-Z0-9])*", "g");
			}
			if($(this).attr("valid")=="1"){
				verifReg=/.+/g;
			}
			if($(this).attr("valid")=="n"){
				verifReg=/[0-9]+/g;
			}
			if($(this).attr("valid")=="m"){
				verifReg=new RegExp("[a-zA-Z0-9._%+-]([a-zA-Z0-9._%+-])*@[a-zA-Z0-9]([a-zA-Z0-9._%+-])*\.([a-z]{2,4})", "g");
			}
			if($(this).attr("valid")=="mo"){
				verifReg=new RegExp("[a-zA-Z0-9._%+-]([a-zA-Z0-9._%+-])*@[a-zA-Z0-9]([a-zA-Z0-9._%+-])*\.([a-z]{2,4})", "g");
			}
			if($(this).attr("valid")=="d"){
				$.ajax({
					type: "POST",
					url: "/phpInc/return_session_value.php",
					dataType: "html",
					data: "_=session_language",
					async: false,
					success: function(returnData) {
						lang=returnData;
					}});
				if(lang!="en") {
					verifReg=/(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d/g;
				} else {
					verifReg=/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/(19|20)\d\d/g;
				}
			}
			voT=0;
			for(i=0;i<oT.length;i++){
				if ($(this).attr("id")==oT[i]) { voT++; }
			}
			
			if(($(this).attr("valid")) && ($(this).attr("valid")!="0")){
				compareString=$(this).fieldValue();
				compareString=compareString.toString();
				compareString=noHtml(compareString);
				if((voT==0) && (! compareString.match(verifReg)) && (fieldName!="") && (previousMessage!=simpleText("champ")+fieldName+simpleText("rempli")) && (!(($(this).attr("valid")=="mo") && ($(this).fieldValue()=="")))) {
					errorMessage+=simpleText("champ")+fieldName+simpleText("rempli");
					if(cNames[1] && cNames[1]!="") {errorMessage+=" ("+cNames[1]+")";}
					errorMessage+="\n";
					previousMessage=simpleText("champ")+fieldName+simpleText("rempli");
				}
			}
			
			// Valeurs interdites
			forbiddenValues=new Array("http://","mailto:")
			u=0;
			while(forbiddenValues[u]){
				if((fieldName!="")&&(forbiddenValues[u]==$(this).fieldValue())){
					errorMessage+=simpleText("champ")+fieldName+simpleText("rempli");
					if(cNames[1] && cNames[1]!="") {errorMessage+=" ("+cNames[1]+")";}
					errorMessage+="\n";
				}
				u++;
			}
			
			if($(this).attr("valid")=="r"){
				prevValue=$("input:eq("+(verifString-1)+")").fieldValue();
				prevValue=prevValue.toString();
				nextValue=$(this).fieldValue();
				nextValue=nextValue.toString();
				if(prevValue!=nextValue) {
					if($("input:eq("+(verifString-1)+")").attr("cName")){
						fieldName_old=$("input:eq("+(verifString-1)+")").attr("cName");
					} else {
						fieldName_old=$("input:eq("+(verifString-1)+")").attr("name");
					}
					errorMessage+=simpleText("champ")+fieldName+simpleText("correspond")+fieldName_old+"\".\n";
					if((cNames[1] && cNames[1]!="") && (fieldName!="")) {errorMessage+=" ("+cNames[1]+")";}
					errorMessage+=".\n";
				}
			}
		}
		verifString++;
	});
	
	$("textarea").each(function() {
			if(($(this).attr("cName"))&&($(this).attr("cName")!="0")){
				cNames=$(this).attr("cName");
				cNames=cNames.split(/;/g);
				fieldName=cNames[0];
			} else if(($(this).attr("cName"))&&($(this).attr("cName")=="0")){
				fieldName="";
			} else {
				fieldName=$(this).attr("name");
			}
		if((!$(this).attr("disabled"))||($(this).attr("disabled")=="")){
			if($(this).attr("aExists")){
				c=0;
				rqt=($(this).attr("aExists")).toString()+";"+($(this).fieldValue()).toString();
				c=alreadyExists(rqt);
				if(c>0) {
					aEError+=simpleText("champVal")+fieldName+simpleText("existe");
					if(cNames[1] && cNames[1]!="") {aEError+=" ("+cNames[1]+")";}
					aEError+="\n";
				}
			}
			if($(this).attr("valid")=="reg"){
				verifReg=new RegExp("[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]([a-zA-Z0-9])*", "g");
			}
			if($(this).attr("valid")=="1"){
				verifReg=/.+/g;
			}
			if($(this).attr("valid")=="n"){
				verifReg=/[0-9]+/g;
			}
			if($(this).attr("valid")=="m"){
				verifReg=new RegExp("[a-zA-Z0-9._%+-]([a-zA-Z0-9._%+-])*@[a-zA-Z0-9]([a-zA-Z0-9._%+-])*\.([a-z]{2,4})", "g");
			}
			if($(this).attr("valid")=="mo"){
				verifReg=new RegExp("[a-zA-Z0-9._%+-]([a-zA-Z0-9._%+-])*@[a-zA-Z0-9]([a-zA-Z0-9._%+-])*\.([a-z]{2,4})", "g");
			}
			if($(this).attr("valid")=="d"){
				$.ajax({
					type: "POST",
					url: "/phpInc/return_session_value.php",
					dataType: "html",
					data: "_=session_language",
					async: false,
					success: function(returnData) {
						lang=returnData;
					}});
				if(lang!="en") {
					verifReg=/(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d/g;
				} else {
					verifReg=/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/(19|20)\d\d/g;
				}
			}
			voT=0;
			for(i=0;i<oT.length;i++){
				if ($(this).attr("id")==oT[i]) { voT++; }
			}
			
			if(($(this).attr("valid")) && ($(this).attr("valid")!="0")){
				compareString=$(this).fieldValue();
				compareString=compareString.toString();
				compareString=noHtml(compareString);
				if((voT==0) && (! compareString.match(verifReg)) && (fieldName!="") && (previousMessage!=simpleText("champ")+fieldName+simpleText("rempli")) && (!(($(this).attr("valid")=="mo") && ($(this).fieldValue()=="")))) {
					errorMessage+=simpleText("champ")+fieldName+simpleText("rempli");
					if(cNames[1] && cNames[1]!="") {errorMessage+=" ("+cNames[1]+")";}
					errorMessage+="\n";
					previousMessage=simpleText("champ")+fieldName+simpleText("rempli");
				}
			}
			
			// Valeurs interdites
			forbiddenValues=new Array("http://","mailto:")
			u=0;
			while(forbiddenValues[u]){
				if((fieldName!="")&&(forbiddenValues[u]==$(this).fieldValue())){
					errorMessage+=simpleText("champ")+fieldName+simpleText("rempli");
					if(cNames[1] && cNames[1]!="") {errorMessage+=" ("+cNames[1]+")";}
					errorMessage+="\n";
				}
				u++;
			}
			
			if($(this).attr("valid")=="r"){
				prevValue=$("input:eq("+(verifString-1)+")").fieldValue();
				prevValue=prevValue.toString();
				nextValue=$(this).fieldValue();
				nextValue=nextValue.toString();
				if(prevValue!=nextValue) {
					if($("input:eq("+(verifString-1)+")").attr("cName")){
						fieldName_old=$("input:eq("+(verifString-1)+")").attr("cName");
					} else {
						fieldName_old=$("input:eq("+(verifString-1)+")").attr("name");
					}
					errorMessage+=simpleText("champ")+fieldName+simpleText("correspond")+fieldName_old+"\".\n";
					if((cNames[1] && cNames[1]!="") && (fieldName!="")) {errorMessage+=" ("+cNames[1]+")";}
					errorMessage+=".\n";
				}
			}
		}
		verifString++;
	});
	
	//Vérification des listes et menus
	$("select").each(function() {	
			if(($(this).attr("cName"))&&($(this).attr("cName")!="0")){
				cNames=$(this).attr("cName");
				cNames=cNames.split(/;/g);
				fieldName=cNames[0];
			} else if(($(this).attr("cName"))&&($(this).attr("cName")=="0")){
				fieldName="";
			} else {
				fieldName=$(this).attr("name");
			}
		if($(this).attr("valid")=="1"){
			if(((!$(this).fieldValue()) || ($(this).fieldValue()=="")) && (fieldName!="")) {
				errorMessage+=simpleText("champ")+fieldName+simpleText("rempli");
				if(cNames[1] && cNames[1]!="") {errorMessage+=" ("+cNames[1]+")";}
				errorMessage+="\n";
	}}});
	errorMessage+=aEError;
	if(errorMessage!="") {
		alert(simpleText("erreurValid")+"\n\n"+errorMessage);
		return false;
	} else {
		return true;
	}
	
}

function alreadyExists(valueField) {
	$.ajax({
		type: "POST",
		url: "/phpInc/data_xmlHttpCounter.php",
		dataType: "html",
		data: "_="+valueField,
		async: false,
		success: function(returnValue) {
			c=parseInt(returnValue);
		}});
		return(c);
}
