/** 
 * @package formsUtility
 * @version 1.1
 * @author Frédéric LECOINTRE<frederic.lecointre@burnweb.net>
 * 
 */

// Tableau des formulaires
var __forms = Array();



var FormsUtility = new Object();

FormsUtility.SUCCESS 	= 1;
FormsUtility.FAILED 	= 0x2;
FormsUtility.EMPTY 		= 0x4;
FormsUtility.UNKNOWN 	= 0x8;

FormsUtility.OPT_REQUIRED 	= 1;
FormsUtility.OPT_NUMBER 	= 0x2;
FormsUtility.OPT_STRING 	= 0x4;
FormsUtility.OPT_EMAIL 		= 0x8;
FormsUtility.OPT_REGEXP = 0x10;/*
FormsUtility.OPT_ = 0x20;
FormsUtility.OPT_ = 0x40;
FormsUtility.OPT_ = 0x80;
FormsUtility.OPT_ = 0x100;
FormsUtility.OPT_ = 0x200;
FormsUtility.OPT_ = 0x400;
FormsUtility.OPT_ = 0x800;
*/
/**
 * @param string sForm
 * @return FormsUtility.FormParser
 */
FormsUtility.registerForm = function(sForm){
	
	if(__forms[sForm] == undefined){
		__forms[sForm] = new FormsUtility.FormParser(sForm);
	}//end if
	
	return __forms[sForm];
	
}//end function

/**
 * @param string sForm
 * @return FormsUtility.FormParser
 */
FormsUtility.getFormParser = function(sForm){
	
	if(__forms[sForm] == undefined){
		return __forms[sForm];
	}//end if
	else{
		return null;
	}//end else
}//end function

/**
 *
 * @param string sEmail
 * @return boolean
 */
FormsUtility.checkEmailAddress = function(sEmail) {
	return sEmail.match(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9]{2}[a-z0-9-.]*\.[a-z]{2,4}$/i);
}//end function

/**
 * @param Event e
 * @return void
 */
FormsUtility.onfocus = function(e){
	this.className = this.className.replace(/ +error$/, '');
}//end function

/**
 * @param Event e
 * @return void
 */
FormsUtility.onerror = function(e){
	this.className += ' error';
}//end function

/* -----------------------------------------------------------------------------*/

/** 
 * @class FormParser
 * @package FormsUtility
 * @version 1.1
 * @author: Frédéric LECOINTRE<frederic.lecointre@burnweb.net>
 * 
 */
FormsUtility.FormParser = function(sName){
	this.name = sName;
	this.domObj = null;
	this.fields = Array();
	this.result = FormsUtility.FAILED;
	this.message = '';
	this.currentField = '';
}//end function

/**
 * @param string sName
 * @param integer iOptions
 * @param string sMessage
 * @param array aArgs
 * @return string
 */
FormsUtility.FormParser.prototype.registerField = function(sName, iOptions, sMessage, aArgs){
	if(this.fields[sName] == undefined){
		this.fields[sName] = Array();
		this.fields[sName]['options'] = iOptions;
		this.fields[sName]['message'] = sMessage;
		this.fields[sName]['result'] = FormsUtility.SUCCESS;
		if(aArgs == undefined){
			aArgs = Array();
		}//end if
		this.fields[sName]['args'] = aArgs;
	}//end if
}//end function

/**
 * @param boolean bAfterAll
 * @return string
 */
FormsUtility.FormParser.prototype.check = function(bAfterAll){

	if(bAfterAll == undefined){
		bAfterAll = false;
	}//end if
	
	this.message = '';
	this.currentField = '';
	this.domObj = window.document.getElementById(this.name);
	this.result = FormsUtility.SUCCESS;
	
	if(this.domObj == null){
			alert('"' + this.name + '" does not exists');
			return null;
	}//end if
	
	for(var i in this.fields){
	
		var oField = this.domObj[i];
		var iOptions = this.fields[i]['options'];
		var sMessage = this.fields[i]['message'];
		var aArgs = this.fields[i]['args'];
		
		this.fields[i]['result'] = FormsUtility.SUCCESS;
		this.currentField = i;
		
		if(oField == null){
			alert('"' + i + '" does not exists in "' + this.name + '" form');
			continue;
		}//end if

		oField.onfocus = FormsUtility.onfocus;
		oField.onerror = FormsUtility.onerror;

		switch(oField.type){
		
			case 'text':
			case 'textarea':
				
				// Field empty test
				if(iOptions & FormsUtility.OPT_REQUIRED){
					if(oField.value.length == 0){
						this.fields[i]['result'] = FormsUtility.EMPTY + FormsUtility.FAILED;
						this.result = FormsUtility.FAILED;
						break;
					}//end if
				}//end if
				
				// Field match regular expression test
				if(iOptions & FormsUtility.OPT_REGEXP){
					if(!oField.value.match(aArgs[0])){
						this.fields[i]['result'] = FormsUtility.FAILED;
						this.result = FormsUtility.FAILED;
						break;
					}//end if
				}//end if
				
				// Field is email test
				if(iOptions & FormsUtility.OPT_EMAIL){
					if(!FormsUtility.checkEmailAddress(oField.value)){
						this.fields[i]['result'] = FormsUtility.FAILED;
						this.result = FormsUtility.FAILED;
						break;
					}//end if
				}//end if
				
			break;

			case 'select-one':
			
			break;
			
		}//end switch
		
		if(!bAfterAll && this.result == FormsUtility.FAILED){
			this.message = sMessage;
			oField.onerror();
			break;
		}//end if
		else if(this.result == FormsUtility.FAILED && this.fields[i]['result'] & FormsUtility.FAILED ){
			this.message += '- ' + sMessage + "\n";
			oField.onerror();
		}//end else
		
	}//end for
	
	return this.result;
	
}//end function