//Copyright EXPERT-LINE / JEAN-PHILIPPE LORENZO / Last Update : November 2005
//Tested on firefox 1.0.4,1.0.7 Internet Explorer 6.0, Netscape 8.0.4
var ie= (navigator.appName=="Microsoft Internet Explorer")?1:0;
var ns= (navigator.appName=="Netscape")?1:0;
function Formulaire(form){
		this.name=form.name;
		this.elements=form.elements;
		this.form = form;
}
function formChecker(formName,_submit){
	object=new formValidation(formName,_submit);
	return object.valid;
}
function formValidation(formName,_submit){
	if(_submit==null){
		this._submit=true;	
	}else{
		this._submit=_submit;	
	}
	this.getForm = getForm;
	this.checkDateType = checkDateType;
	this.checkDateEnglishType = checkDateEnglishType;
	this.checkEmailType = checkEmailType;
	this.checkRequired= checkRequired;
	this.checkFloatGrammar=checkFloatGrammar;
	this.checkIntGrammar=checkIntGrammar;
	this.getFieldLabel = getFieldLabel;
	this.formulaire=this.getForm(formName);
	this.errors=0;
	this.errorMessage="";
	this.formNumber=0;
	if(ie){
		for(i=0;i<this.formulaire.elements.length;i++){
			if(this.formulaire.elements[i].required=="true"){
				//alert('ok'+this.formulaire.elements[i].name);
				this.currentElement=this.formulaire.elements[i];
				this.checkRequired();
			}
			if(this.formulaire.elements[i].datatype=="float"){
				//alert('ok'+this.formulaire.elements[i].name);
				this.currentElement=this.formulaire.elements[i];
				this.checkFloatGrammar();
			}
			if(this.formulaire.elements[i].datatype=="integer"){
				//alert('ok'+this.formulaire.elements[i].name);
				this.currentElement=this.formulaire.elements[i];
				this.checkIntGrammar();
			}
			if(this.formulaire.elements[i].datatype=="date"){
				//alert('ok'+this.formulaire.elements[i].name);
				this.currentElement=this.formulaire.elements[i];
				this.checkDateType();
			}
			if(this.formulaire.elements[i].datatype=="dateEnglish"){
				//alert('ok'+this.formulaire.elements[i].name);
				this.currentElement=this.formulaire.elements[i];
				this.checkDateEnglishType();
			}
			if(this.formulaire.elements[i].datatype=="email"){
				//alert('ok'+this.formulaire.elements[i].name);
				this.currentElement=this.formulaire.elements[i];
				this.checkEmailType();
			}
		}
	}else{
		for(i=0;i<this.formulaire.elements.length;i++){
			for(j=0;j<this.formulaire.elements[i].attributes.length;j++){
					if(this.formulaire.elements[i].attributes[j].name=="required" && this.formulaire.elements[i].attributes[j].value=="true"){
						//alert('ok '+this.formulaire.elements[i].attributes[j].name+' '+this.formulaire.elements[i].name);
						this.currentElement=this.formulaire.elements[i];
						this.checkRequired();
					}
					if(this.formulaire.elements[i].attributes[j].name=="datatype" && this.formulaire.elements[i].attributes[j].value=="float"){
						//alert('ok '+this.formulaire.elements[i].attributes[j].name+' '+this.formulaire.elements[i].name);
						this.currentElement=this.formulaire.elements[i];
						this.checkFloatGrammar();
					}
					if(this.formulaire.elements[i].attributes[j].name=="datatype" && this.formulaire.elements[i].attributes[j].value=="integer"){
						//alert('ok '+this.formulaire.elements[i].attributes[j].name+' '+this.formulaire.elements[i].name);
						this.currentElement=this.formulaire.elements[i];
						this.checkIntGrammar();
					}
					if(this.formulaire.elements[i].attributes[j].name=="datatype" && this.formulaire.elements[i].attributes[j].value=="date"){
						//alert('ok '+this.formulaire.elements[i].attributes[j].name+' '+this.formulaire.elements[i].name);
						this.currentElement=this.formulaire.elements[i];
						this.checkDateType();
					}
					if(this.formulaire.elements[i].attributes[j].name=="datatype" && this.formulaire.elements[i].attributes[j].value=="dateEnglish"){
						//alert('ok '+this.formulaire.elements[i].attributes[j].name+' '+this.formulaire.elements[i].name);
						this.currentElement=this.formulaire.elements[i];
						this.checkDateEnglishType();
					}
					if(this.formulaire.elements[i].attributes[j].name=="datatype" && this.formulaire.elements[i].attributes[j].value=="email"){
						//alert('ok '+this.formulaire.elements[i].attributes[j].name+' '+this.formulaire.elements[i].name);
						this.currentElement=this.formulaire.elements[i];
						this.checkEmailType();
					}
			}
		}	
	}
	try{
		specificValidation(this,this.formulaire.form);
	}catch(e){
	
	}
	
	if(this.errors>0){
		alert(this.errorMessage);
		this.valid = false;
	}else{
		if(this._submit){
			this.valid = true;
			try{
				document.forms[this.formNumber].submit();
			}catch(e){
				try{
					this.formulaire.form.submit();
				}catch(e){
					//alert(e.message);
				}
			}
		}else{
			this.valid = true;
		}	
	}
	return this.valid;
}
function getForm(formName){
	
	for(i=0;i<document.forms.length;i++){
		//alert(document.forms.length +":" +i+ ":"+document.forms[i].name);
		//for(j=0;j<document.forms[i].elements.length;j++){
		//	alert(document.forms[i].elements[j].name);
		//}
		if(document.forms[i].name==formName){
			formulaire= new Formulaire(document.forms[i]);
			this.formNumber=i;
			//alert('Formulaire retenu: '+i+'\n Nom: '+ formulaire.name);
		}
	}
	if(this.formulaire=null){
		alert('erreur développeur: le formulaire appelé n existe pas dans la page'); 	
	}else{
		return formulaire;	
	}
}
function checkRequired(){
	if(!this.currentElement.disabled)	{
		if(this.currentElement.value=="" || this.currentElement.value==0 ){
			fieldLabel=this.getFieldLabel(this.currentElement);
			try{
				this.errorMessage=this.errorMessage + getMessageRequired(fieldLabel);
			}catch(e){
				this.errorMessage=this.errorMessage + 'Field -'+fieldLabel+'- is required !\n';	
			}
			this.errors=this.errors+1;
		}
	}
}
function getFieldLabel(element){
	fieldLabel="";
	if(ie){
		fieldLabel=	element.fieldlabel;
	}else{
		for(e=0;e<element.attributes.length;e++){
				if(element.attributes[e].name=="fieldlabel"){
					fieldLabel=element.attributes[e].value;
				}
		}
	}
	if(fieldLabel=="" || fieldLabel==null ){
		return element.name;	
	}else{
		return fieldLabel;	
	}
}
function checkFloatGrammar(){
	Expression= new RegExp("^[0-9]+[.]{0,1}[0-9]{0,2}$","i");
	if(!Expression.test(this.currentElement.value)){
		fieldLabel=this.getFieldLabel(this.currentElement);
		this.errorMessage=this.errorMessage + 'The field -'+fieldLabel+'- format must be like 999.99 !\n';	
		this.errors=this.errors+1;
	}
}
function checkIntGrammar(){
	Expression= new RegExp("^[0-9]*$","i");
	if(!Expression.test(this.currentElement.value)){
		fieldLabel=this.getFieldLabel(this.currentElement);
		this.errorMessage=this.errorMessage + 'The field -'+fieldLabel+'- must contain only number !\n';	
		this.errors=this.errors+1;
	}
}
function checkDateType(){
	Expression= new RegExp("^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$","i");
	if(!Expression.test(this.currentElement.value)){
		fieldLabel=this.getFieldLabel(this.currentElement);
		this.errorMessage=this.errorMessage + 'The field -'+fieldLabel+'- format must be jj/mm/aaaa !\n';	
		this.errors=this.errors+1;
	}
}
function checkDateEnglishType(){
	Expression= new RegExp("^[0-9]{4}\/[0-9]{2}\/[0-9]{2}$","i");
	if(!Expression.test(this.currentElement.value)){
		fieldLabel=this.getFieldLabel(this.currentElement);
		this.errorMessage=this.errorMessage + 'The field -'+fieldLabel+'- format must be yyyy/mm/dd !\n';	
		this.errors=this.errors+1;
	}else{
		if(!isValidDate(this.currentElement.value.substr(8,2),this.currentElement.value.substr(5,2),this.currentElement.value.substr(0,4))){
					this.errorMessage=this.errorMessage + this.currentElement.value+' is not a valid date !\n';	
					this.errors=this.errors+1;	
			}		
	
	}
}
function checkEmailType(){
	if(this.currentElement.value!=null && this.currentElement.value!=""){
	//Contrôle général de la forme 
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var charFilter=/[^a-zA-Z_\-@\.0-9]/i;
	fieldLabel=this.getFieldLabel(this.currentElement);
	if (!filter.test(this.currentElement.value)){
		try{
			this.errorMessage=this.errorMessage +getMessageEmailIncorrect(fieldLabel);
		}catch(e){	
			this.errorMessage=this.errorMessage + fieldLabel +'- Unproper email adresse !\n';	
		}
		this.errors=this.errors+1;
	}else{
	// Contrôle des caractères interdits
		if(charFilter.test(this.currentElement.value)){
			this.errorMessage=this.errorMessage + fieldLabel + '-There\'s forbidden character in the email adress !\n';	
			this.errors=this.errors+1;	
		}
	}
	}
}
function isValidDate(jj,mm,aaaa){
	var DateControl = new Date(aaaa,parseInt(mm,10)-1,jj);
	var jjC = DateControl.getDate();
	var mmC = DateControl.getMonth();
	var aaaaC = DateControl.getFullYear();
	var isValid=false;
	
	if( (jjC!=jj) || ((mmC+1)%12!=mm%12) || (aaaaC!=aaaa) ){
		isValid = false;
	}else{
		isValid= true;
	}
	return isValid;	
}
function exists(obj) {
return (typeof obj != 'undefined');
}