	/**
	 * 
	 * Javascript for tracking HTML eventse.
	 * 
	 * @name classe de validation de formulaire
	 * @author Vincent Cantin Bellemare
	 * @since 2006-07-06
	 * @version 1.0.0
	 * @package reptileframework
	 * 
	 * 
	 */




	 function Form_validator(formulaire,static_div_suffixe){

		if(!static_div_suffixe)
		{
			this.static_div_suffixe = 'divInvalid';
		}
		else
		{
			this.static_div_suffixe = static_div_suffixe;
		}
		
		this.form 					= formulaire;
		this.input 					= new Array()
		this.input_validator 		= new Array()
		this.token_div_error 		= new Array()
		this.token_error			= new Array()
		this.input_error			= new Array()
		this.noErrors				= false
		
		this.validate				= validate;
	 	this.addElement				= addElement;
	 	this.showErrors 			= showErrors;
	 }





	function addElement(input,input_validator,token_error, token_div_error)
	{
		if(! token_div_error)
		{
			token_div_error = '*'	
		}
		
		this.token_div_error.push(token_div_error)
		this.input.push(input)
		this.input_validator.push(input_validator)
		this.token_error.push(token_error)
		this.input_error.push(false)
	}
	
	
	function validate()
	{

		ers = 0
		
		for(i in this.input)
		{
			result = eval( this.input_validator[i] );
			this.input_error[i] = result;
			(result != false) ? '' : (ers++);	
		}
	
		if(ers == 0)
		{
			this.noErrors = true
			this.form.submit()
		}
		else
		{	
			return (false)	
		}
		
	}


	function showErrors(token,is_alert)
	{
		token += '\n\n';
		if(this.noErrors == false)
		{
			for(i in this.input_error)
			{
				if(this.input_error[i] == false)
				{
					if(this.token_error[i])
					token += '	' + this.token_error[i] + '\n';
				}

					
					if(document.getElementById(this.static_div_suffixe + this.input[i] ) )
					{
						innerHTML = (this.input_error[i]) ? '' : this.token_div_error[i]; 
						document.getElementById(this.static_div_suffixe + this.input[i] ).innerHTML = innerHTML	
					}
					

				
			}
				
			if(is_alert == true)
			{
				alert(token)	
			}
			else
			{
				return token				
			}

		}
	}
	
	function inputWatcher(formInputField)
	{
		
		
		if(is_array_field(formInputField.id))
		{
			msgErrorDiv = getInputErrorDiv(formInputField,$$(formInputField.id));
			
		
		}else
		{
			msgErrorDiv = $(formInputField.id + "msgError");
			
			if(formInputField.attributes['divError'])
			{
				msgErrorDiv = $(formInputField.attributes['divError']);
			}
			
		}
		
		validator = formInputField.attributes['validator'];
			
		if(validator==null)
		{
			return true;
		}
		
		if(eval(validator.value + "('" + formInputField.value + "')"))
		{	
			if(msgErrorDiv!=null)
			{
				msgErrorDiv.style.display = "none";
			}
			return true;
		}else
		{	if(msgErrorDiv!=null){
				msgErrorDiv.style.display = "inline";
			}
			return false;
		}
		
	}
	
	
	
	function getInputErrorDiv(objInput,arrInput)
	{
		for(var i=0;i<arrInput.length;i++)
		{	
			if(arrInput[i]==objInput)
			{
				name = objInput.id;
				name = name.substr(0,name.length - 2);
				arrDivError = $$(name+'msgError[]');
				return arrDivError[i];
				
			}
		}
	}
	
	
	
	function getInputField(objInput,arrInput)
	{
		for(var i=0;i<arrInput.length;i++)
		{	
			if(arrInput[i]==objInput)
			{
				name = objInput.id;
				name = name.substr(0,name.length - 2);
				arrDivError = $$(name+'msgError[]');
				return arrDivError[i];
				
			}
		}
	}
	
	function is_array_field(id)
	{
		var reg = new RegExp("^\w+[/[/]]$"); 
		
		return reg.test(id);
	}
	
		
	
	function validateForm()
	{
		boolError = true;
		for(var i=0;i<document.forms[0].elements.length;i++)
		{
			if(!inputWatcher(document.forms[0].elements[i]))
			{	
				boolError = false;
			}
			
		}
		if(!boolError)
		{
			alert('Champs requis non valide');	
			
		}
		return boolError;
		
		
	}
	
	

	
