/**
*		FormValidator class
*
*	A collection of validation methods for form fields. Each method defines
*	a validation type that can be applied to a field type, more than one validator
*	can be defined for a form field. These validators coorespond to the WebWork 
*	validators, and can be defined in the webwork validator xml file for the page 
*	action class that serves the page that contains the form.
*	Each validation function is passed the form field, and a parameters object
*	that contains the parameters defined in the validator xml file, which are
*	needed to configure the validation.
*
*	To Add New Validations using WebWork:
*	Simply create a function named FormValidator_validate_XXX, where XXX is the name 
*	of the validation to perform.  Make the function accept two parameters, a
*	field object and an object that contains all of the parameters needed for the
*	validation, and which returns true if the field passes, false if it has errors.  
*	Then define the validation object in the WebWork validation xml file,
*	any <param>'s defined in the file will be added to the parameter map passed to the
*	function.  
*	Then add the function to this object as a method by adding the line:
*		this.validate_XXX=FormValidator_validate_XXX;
*	The validation will then automatically be passed to the method of the
*	same name in this object by the FormHandlerClass.
*	When you create the validator class, pass a reference to the parent object, and
*	implement the function getFieldValue(formName,fieldName) that returns the values
*	of any other fields in the form.  This reference function will be used by 
*	validator functions that need to compare the field value to other field values. 
*/

function FormValidator(parent){
	this.parent = parent;
	
	//	Define the validations as methods of this object
	
	this.validate_required=FormValidator_validate_required;
	this.validate_requiredstring=FormValidator_validate_requiredstring;
	this.validate_stringlength=FormValidator_validate_stringlength;
	this.validate_regex=FormValidator_validate_regex;
	this.validate_stringregex=FormValidator_validate_stringregex;
	this.validate_email=FormValidator_validate_email;
	this.validate_url=FormValidator_validate_url;
	this.validate_int=FormValidator_validate_int;
	this.validate_double=FormValidator_validate_double;
	this.validate_fieldcompare=FormValidator_validate_fieldcompare;
	this.validate_Date=FormValidator_validate_Date;
	//	Helper functions
	this.getFieldValue=FormValidator_getFieldValue;
	}
	
	function FormValidator_validate_required(field,params){
		var value=field.value;
		//this.parent.log.debug("Validating field required");
		if(field.tagName=="select"){
			value=field.options[field.selectedItem].value;
			}
		if((value===null)||(value=="")||(value==" ")){return false;}
		else{return true;}
	}
	
	function FormValidator_validate_requiredstring(field,params){
        if (field.value != null && (field.value == "" || field.value.replace(/^\s+|\s+$/g,"").length == 0)) {
        	return false;
        	}
        else return true;
    }
	function FormValidator_validate_stringlength(field,params){
		if(!params||typeof params!="object")return;
		if (field.value != null) {
		    var value = field.value;
		    if(params.trim){
		        //trim field value
		        while (value.substring(0,1) == ' ')value = value.substring(1, value.length);
		        while (value.substring(value.length-1, value.length) == ' ')value = value.substring(0, value.length-1);
		        }
		    if(((params.minLength > -1) && (value.length < params.minLength)) ||
		            ((params.maxLength > -1) && (value.length > params.maxLength))) {
		        return false;
		    }
		}
		return true;
	}
    function FormValidator_validate_regex(field,params){
		if(!params||typeof params!="string")return;
		if (field.value != null && !field.value.match(params.expression)) {
		    return false;
		}
		return true;
    }
    function FormValidator_validate_stringregex(field,params){
    	if(!params||typeof params!="string")return;
		if (field.value != null && !field.value.match(/params.regex/)) {
			return false;
		}
		return true;
		}
    function FormValidator_validate_email(field,params){
		if (field.value != null && field.value.length > 0 && field.value.match(/^\S+@\S+\.(com|net|org|info|edu|mil|gov|biz|ws|us|tv|cc|aero|arpa|coop|int|jobs|museum|name|pro|travel|nato|.{2,2})$/gi) == null) {
		    return false;
		}
		return true;
	}
    function FormValidator_validate_url(field,params){
		if (field.value != null && field.value.length > 0 && field.value.match(/^((file:\/\/\S+)|(ftp|http|https):\/\/\S+\.(com|net|org|info|edu|mil|gov|biz|ws|us|tv|cc|aero|arpa|coop|int|jobs|museum|name|pro|travel|nato|.{2,2}))$/ig) == null) {
		    return false;
		}
		return true;
	}
    function FormValidator_validate_int(field,params){
		if ((field.value != null)&&(field.value != "")) {
			var value=parseInt(field.value);
			if(isNaN(value))return false;
			if(params){
				if(params.min&&(value<params.min))return false;
				if(params.max&&(value>params.max))return false;
				}
		}
		return true;
	}
    function FormValidator_validate_double(field,params){
		if (field.value != null) {
		    var value = parseFloat(field.value);
		    if(isNaN(value))return false;
		    if(params){
				if(params.minInclusive&&(value < params.minInclusive))return false;
			    if(params.maxInclusive&&(value > params.maxInclusive))return false;
			    if(params.minExclusive&&(value <= params.minExclusive))return false;
			    if(params.maxExclusive&&(value >= params.maxExclusive))return false;
			    }
		    }
		return true;
		}
	//	Allows a field to be validated by comparing it to another fields value
	function FormValidator_validate_fieldcompare(field,params){
		//	params: minInclusiveField minExclusiveField maxInclusiveField maxExclusiveField failOnEmptyField
		var formName=(field.formName?field.formName:(field.form&&field.form.id?field.form.id:null));
		var dependsVal;
		if(!formName)return;
		var value=field.value;
		if(value)value=parseFloat(value);
		if(params.minInclusiveField){
			var field=this.parent.getField(formName,params.minInclusiveField);
			if(field.div.style.display=="none"){return true;}
			dependsVal=this.getFieldValue(formName,params.minInclusiveField,params);
			if(dependsVal){dependsVal=parseFloat(dependsVal);}
			//this.parent.log.debug("Validating minInclusiveField field " +value+" compared to field " + dependsVal);
			if(((dependsVal==null)&&params.failOnEmptyField)||(dependsVal&&(value<=dependsVal))){return false;}
			}
		if(params.minExclusiveField){
			var field=this.parent.getField(formName,params.minExclusiveField);
			if(field.div.style.display=="none"){return true;}
			dependsVal=this.getFieldValue(formName,params.minExclusiveField,params);
			if(dependsVal){dependsVal=parseFloat(dependsVal);}
			//this.parent.log.debug("Validating minExclusiveField field " +value+" compared to field " + dependsVal);
			if(((dependsVal==null)&&params.failOnEmptyField)||(dependsVal&&(value<dependsVal))){return false;}
			}
		if(params.maxInclusiveField){
			var field=this.parent.getField(formName,params.maxInclusiveField);
			if(field.div.style.display=="none"){return true;}
			dependsVal=this.getFieldValue(formName,params.maxInclusiveField,params);
			if(dependsVal){dependsVal=parseFloat(dependsVal);}
			//this.parent.log.debug("Validating maxInclusiveField field " +value+" compared to field " + dependsVal);
			if(((dependsVal==null)&&params.failOnEmptyField)||(dependsVal&&(value>=dependsVal))){return false;}
			}
		if(params.maxExclusiveField){
			var field=this.parent.getField(formName,params.maxExclusiveField);
			if(field.div.style.display=="none"){return true;}
			dependsVal=this.getFieldValue(formName,params.maxExclusiveField,params);
			if(dependsVal){dependsVal=parseFloat(dependsVal);}
			//this.parent.log.debug("Validating maxExclusive field " +value+" compared to field " + dependsVal);
			if(((dependsVal==null)&&params.failOnEmptyField)||(dependsVal&&(value>dependsVal))){return false;}
			}
		return true;
		}
	
	function FormValidator_validate_Date(formName,fieldName,params){
		/*
		private Date afterDate = null;
		private Date beforeDate = null;
		private int afterDays = 0;
		private int beforeDays = 0;
		private Date afterDaysFromToday = null;
		private Date beforeDaysFromToday = null;
		*/
		var value=this.parent.getFieldValue(formName,fieldName);
		
		}
	
	function FormValidator_getFieldValue(formName,fieldName,params){
		var value=this.parent.getFieldValue(formName,fieldName);
		if(value == undefined){this.parent.log.error("Could not find field value for field " + fieldName + ", in FormValidator.getFieldValue()");}
		var oldValue = value;
		if(params.percent&&(params.percent!=0)){
			value=parseFloat(value*(parseFloat(params.percent)/100));
			}
		if(params.offset&&(params.offset!=0)){
			value+=params.offset;
			}
		//this.parent.log.debug("Found field value " + value);
		return value;
		}