/**
 * The radClient Object is used to manager client-side validation
 *
 * @author			Frederic Naslis
 * @version 		0.1
 * @see 			attributs.js
 * @constant		OID 				oValidation.OID = 11001
 * @constant 		OCME_INVALIDDATA	oValidation.OCME_INVALIDDATA = 20013 - Invalid Data (used for general Outcome message)
 * @constant		OCME_MINLEN			oValidation.OCME_MINLEN = Please enter a value at least of &MINLEN characters
 * @constant		OCME_MAXLEN			oValidation.OCME_MAXLEN = Please enter a value less than of &MAXLEN characters
 * @constant		OCME_FORMAT			oValidation.OCME_FORMAT = Wrong date format : &FORMAT
 * @constant		OCME_FLOAT			oValidation.OCME_FLOAT = Please enter a valid floating point
 * @constant		OCME_MIN 			oValidation.OCME_MIN = Please enter a value greater than or equal to &MIN
 * @constant		OCME_MAX 			oValidation.OCME_MAX = Please enter a value less than or equal to &MAX
 * @constant		OCME_INTEGER		oValidation.OCME_INTEGER = Please enter a valid integer
 * @constant		OCME_REQUIRED		oValidation.OCME_REQUIRED = Please enter a value
 * @constant		OCME_REGEXP			oValidation.OCME_REGEXP = Please enter correct regular expression
 * @constant		OCME_MASK			oValidation.OCME_MASK = Please enter mask like &MASK
 * @constant		MODE				oValidation.MODE = "client"
 * @constant		BTN_VALIDATE_NAME	oValidation.BTN_VALIDATE_NAME = "_a"
 * @lastmodified	16/05/2002
 * @modified		25/09/2002			Use getElementsByName instead of an index for error message
 */
 
/**
 * Custom object constructor
 *
 * @property	browserName		browser name
 * @property	browserVer		browser version
 */
function Validation()
{
	this.OID				= 11001;
	this.OCME_INVALIDDATA	= 20013;
	this.OCME_MINLEN		= "Please enter a value at least of &MINLEN characters";
	this.OCME_MAXLEN 		= "Please enter a value less than of &MAXLEN characters";
	this.OCME_FORMAT		= "Wrong date format : &FORMAT";
	this.OCME_FLOAT			= "Please enter a valid floating point";
	this.OCME_MIN			= "Please enter a value greater than or equal to &MIN";
	this.OCME_MAX			= "Please enter a value less than or equal to &MAX";
	this.OCME_INTEGER		= "Please enter a valid integer";
	this.OCME_REQUIRED		= "Please enter a value";
	this.OCME_REGEXP 		= "Please enter correct regular expression";
	this.OCME_MASK			= "Please enter mask like &MASK";
	this.MODE				= "client"; 
	this.BTN_VALIDATE_NAME	= "_a";
	this.browserName 		= navigator.appName; 
	this.browserVer			= parseInt(navigator.appVersion); 
}

/**
 * Set Form Properties
 *
 * @param		sName				form name
 * @param		sType				form type("file,grid")
 * @param		sCol				Columns Names
 * @param		iRow				Rows Number
 */
Validation.prototype.initForm=function (sName,sType,sCol){
	this.FormPointer = sName;
	var oForm = document.forms[sName];
	oForm.Type = sType;
	oForm.buttons = new Array();
	if (sType == "grid") {
		var oElement = eval("document.forms[this.FormPointer]." + sCol.split(",")[1]);
		oForm.Row = oElement.length - 1;
		oForm.Col = sCol.split(",");
	}
	oForm.Action = "";
}

/**
 * Set Element Properties
 *
 * @param		sElement			element name
 * @param		sAttribut			element attribut
 * @param		sValue				attribut value
 */
Validation.prototype.setProperty=function(sElement,sAttribut,sValue){
	if (typeof(sValue) == "string" && sAttribut != "regexp" ) sValue = "'" + sValue + "'";
	var oElement = eval("document.forms[this.FormPointer]." + sElement);
	var iLenght = oElement.length;
	if (document.forms[this.FormPointer].Type == "grid") {
		for(i=0; i < iLenght; i++){
			
			eval("oElement[" + i + "]."  + sAttribut + "=" + sValue);	
			 			
			oElement[i].index = i;			
		}			
	}
	else {
		if (iLenght > 0) {
			var sType =  new String(oElement.type);
			if (sType == "undefined") {
				eval("oElement[0]." + sAttribut + "=" + sValue);
			}	
			else {
				eval("oElement." + sAttribut + "=" + sValue);
			}	
		}
		else {
			eval("oElement." + sAttribut + "=" + sValue);
		}
	}
}

/**
 * Set Expression
 *
 * @param		sExpression			String Expression
 */
Validation.prototype.setExpression = function(sExpression){
	document.forms[this.FormPointer].expression = sExpression;
}

/**
 * Set Element Predefined Attributs
 *
 * @param		sElement			element name
 * @param		sFunction			predefined function name
 */
Validation.prototype.setPredefined = function(sElement,sFunction){
	eval("this." + sFunction + "('" + sElement + "');");
}

/**
 * Set Validate Buttons
 *
 * @param		sButton				button value
 * @param		sAction				button action
 */
Validation.prototype.setButton = function(sButton,sAction){
	var oButton = new Object();
	oButton.value = sButton;
	if (document.forms[this.FormPointer].Type == "grid") {
		oButton.action = sAction;
	}
	else {
		oButton.action = "validate";
	}	
	var iLenght = document.forms[this.FormPointer].buttons.length;
	document.forms[this.FormPointer].buttons[iLenght++] = oButton;
	document.forms[this.FormPointer].buttons[sButton] = oButton;
}

/**
 * Check <form>.expression (combinaison of expressions)
 *
 * @return							boolean
 */
Validation.prototype.CheckExp=function(){
	return true;
}

/**
 * Set up methods and event handlers for all forms and elements
 */
Validation.prototype.init=function(){	 

	// Fan through forms on page to perform initializations
	var i,iForms=document.forms.length;
	for(i=0; i<iForms; i++){
		var oForm=document.forms[i];		
		if(!oForm.bProcessed)
			this.initSubmit(oForm);
	
		// Create Input methods
		var j, iElements=oForm.elements.length;
		for(j=0; j<iElements; j++){
			var oElement=oForm.elements[j];
			if(!oElement.bProcessed)
				this.initElement(oElement) 
		}
	}	
}
	
/**
 * Set Up onsubmit handler on Form object
 *
 * @param		oForm				form object
 */
Validation.prototype.initSubmit=function(oForm){
	var fnSubmit=oForm.onsubmit;

	// Create new event handlers
	oForm.onsubmit=function (){
		if (this.Action == "validate" || this.Action == "create" || this.Action == "update" || this.Action == "image"){

			// Define Form as valid by default
			oValidation.elements = this.elements;
			this.isValid = true;
							
			// Check Form Expression
			oValidation.expression = this.expression;
			if (!oValidation.CheckExp())
				this.isValid = false;
		
			// Execute onbeforevalidate processing
			if(typeof this.onbeforevalidate=="function" && this.onbeforevalidate()==false)
				return false;
						
			// Check Form Type (grid/file file is a default value)
			if (this.Type == "grid") {
				oValidation.initGrid(this)
			}
			else {	
				oValidation.initFile(this)
			}	
						
			// Check if form is correct
			if (!this.isValid){
				oValidation.Err.displayDiv();
				this.Action = "";
				return false;					
			}
		
			// Execute onaftervalidate processing
			if(typeof this.onaftervalidate=="function" && this.onaftervalidate()==false)
				return false;
							
			// Perform original onsubmit event handler
			if (fnSubmit && fnSubmit()==false)
				return false;
		}
		else {
			return true;	
		}			
	}
	oForm.bProcessed=true;
}	

/**
 * Set value for Grid
 *
 * @param		oForm				form object
 */
Validation.prototype.initGrid=function(oForm){
	switch (oForm.Action) {
		case "create" :
			oForm.startIndex	= eval("oForm." + oForm.Col[1]).length - 1;
			oForm.endIndex		= Number(eval("oForm." + oForm.Col[1]).length - 1) + 1;
			break;
		case "update" :
			oForm.startIndex	= 0
			oForm.endIndex		= eval("oForm." + oForm.Col[1]).length - 1;
			break;
		default :  
			oForm.startIndex	= -1;
			oForm.endIndex		= -2;
	}

	// Loop in all rows of this table
	var indRow;
	for(indRow = oForm.startIndex; indRow<oForm.endIndex; indRow++){
		var indCol;
		for(indCol=0; indCol<oForm.Col.length; indCol++){
							
			// Validate individual elements
			oElement=oForm.elements[oForm.Col[indCol]][indRow];
			oElement.gridIndex = indRow;
				
			// Perform default validation for element
			if (!oElement.valid())
				oForm.isValid = false;
		}
	}
}

/**
 * Set value for File
 *
 * @param		oForm				form object
 */
Validation.prototype.initFile=function(oForm){
	
	// Validate individual elements
	var i, oElement, iElements=oForm.elements.length;
	for(i=0;i<iElements;i++){
		oElement=oForm.elements[i];

		// Perform default validation for element
		if (!oElement.valid()) 
			oForm.isValid = false;
	}
}
	
/**
 * Return the value of a form field as seen at server
 *
 * @param		oSrc				element object
 * @return							sValue
 */
Validation.prototype.getValueOf=function (oElement) {
	var sReturnValue = null;
	switch (oElement.type)
	{
		case "text" : case "textarea" : case "file" : case "password" : case "hidden" :
			sReturnValue = oElement.value;
			break;
		case "select-one" :
			sReturnValue = oElement.options[oElement.selectedIndex].value;
			break;
		case "select-multiple" :
			var i, iOptions = oElement.options.length;
			for(i=0; i<iOptions; i++)
				if(oElement.options[i].selected && oElement.options[i].value.toString().trim()) {
					sReturnValue = true;
					break;
				}
			break;
		case "radio" : case "checkbox" :
			if(oElement.checked){
				sReturnValue = oElement.value ? oElement.value : true;
			}	
			break;
	}
	return sReturnValue;
}	

/**
 * Set Up onclick & onkeypress handler on element object
 *
 * @param		oElement				element object
 */
 Validation.prototype.initElement=function(oElement){	

	// Custom onclick for action management
	if (oElement.name == this.BTN_VALIDATE_NAME) this.initButton(oElement);
						
	// Custom onkeypress filtering for text fields
	if(oElement.onkeypress)
		oElement.fKeypress=oElement.onkeypress;
	
	if (this.browserName == "Netscape") {
		if (this.browserVer <= 4){	
			this.initElementKeyNet4(oElement);
		}
		else {
			this.initElementKeyNet6(oElement);
		}
	}	
	else {
		this.initElementKeyIE(oElement);
	}	
	this.initValid(oElement);
}

/**
 * Set Up onclick handler on submit button
 *
 * @param		oElement				element object
 */
Validation.prototype.initButton=function(oElement){
	var iLength = oElement.form.buttons.length
	var indBtn;
	for(indBtn=0; indBtn < iLength ; indBtn++){
		var oButton = oElement.form.buttons[indBtn];
		if (oButton.value == oElement.value) {
			if(oElement.onclick)
				oElement.fOnClick=oElement.onclick;
			oElement.onclick=function(){
				this.form.Action = this.form.buttons[this.value].action;
			}
			break;
		}	
	}
}

/**
 * Set Up onkeypress handler on element object for Netscape 4
 *
 * @param		oElement				element object
 */
Validation.prototype.initElementKeyNet4=function(oElement){
	oElement.onkeypress=function (netscape_event){
		var sFilter=this.filter;
		if(this.fKeypress && this.fKeypress()==false)
			return false;
						
		if(sFilter){
			var sKey=String.fromCharCode(netscape_event.which);
			var re=new RegExp(sFilter);
	
			// Do not filter out ENTER!
			if(sKey!="\r" && !re.test(sKey))
				return false;
		}
	}	
}
	
/**
 * Set Up onkeypress handler on element object for Netscape 6
 *
 * @param		oElement				element object
 */
Validation.prototype.initElementKeyNet6=function(oElement){
	oElement.onkeypress=function (netscape_event){
		var sFilter=this.filter;
		if(this.fKeypress && this.fKeypress()==false)
			netscape_event.returnValue=false;
								
		if (netscape_event.which !=0 && netscape_event.which !=8 && netscape_event.which !=13) {
			if(sFilter){
				var sKey=String.fromCharCode(netscape_event.which);
				var re=new RegExp(sFilter);
		
				// Do not filter out ENTER!
				if(!re.test(sKey)) {
					var oldValue = new String(this.value);
					this.value = oldValue.replace(sKey,'');  
					return false;
				}	
			}	
		}
	}
}
	
/**
 * Set Up onkeypress handler on element object for Internet Explorer
 *
 * @param		oElement				element object
 */
Validation.prototype.initElementKeyIE=function(oElement){
	oElement.onkeypress=function (){
		var sFilter=this.filter;
		if(this.fKeypress && this.fKeypress()==false)
			event.returnValue=false;

		if(sFilter){
			var sKey=String.fromCharCode(event.keyCode);
			var re=new RegExp(sFilter);

			// Do not filter out ENTER!
			if(sKey!="\r" && !re.test(sKey))
				event.returnValue=false;
				
			event.keyCode=sKey.charCodeAt(0);
		}
	}
}

/**
 * Validate an element based on the attributes provided in the HTML text
 *
 * @param		oElement				element object
 */
Validation.prototype.initValid=function(oElement){
	oElement.valid=function (){
						
		// Trim leading and trailing spaces
		if(this.value)
			this.value = this.value.trim();
						
		var sValue = oValidation.getValueOf(this);
		if (!oValidation.checkAttributes(this,sValue)) 
			return false;		

		// Perform onvalidate event handler
		if(typeof this.onvalidate=="function" && this.onvalidate()==false)
			return false;

		return true;
	}
	oElement.bProcessed=true;
}

/**
 * Trim all space in a string
 *
 */
String.prototype.trim=function(){
	return this.replace(/^\s+|\s+$/g,"");
}


