// JavaScript Document

function fieldValidation(formName, fieldName, required, minLength, maxLength, regExp, valFunction, errorMsg)
{
	// the form name
	this.formName = formName;
	
	// the field's name
	this.fieldName = fieldName;
	
	
	// minimum length of the field
	this.minLength = minLength;
	
	// maximum length of the field
	this.maxLength = maxLength;

	// set regular expression and regular expression flag.
	this.regExp = regExp;

	if(regExp.length > 0)
		this.bRegExp = true;
	else
		this.bRegExp = false;	
	
	// set the function and function flag
	this.valFunction = valFunction;
	if(valFunction.length > 0)
		this.bValFunction = true;
	else
		this.bValFunction = false;
	
	// the error message
	this.errorMsg = errorMsg;
	
	// the required flag
	this.required = required;
	
	this.getFieldObject = fieldValidation_getFieldObject;
	this.validate = fieldValidation_validate;	
	this.getError = fieldValidation_getError;	
	this.valLength = fieldValidation_valLength;
	this.doValFunction = fieldValidation_doValFunction;
	this.valRegExp = fieldValidation_valRegExp;
	this.isRequired = fieldValidation_isRequired;
}

function fieldValidation_getFieldObject()
{
	return eval('document.'+this.formName+'.'+this.fieldName);
}

function fieldValidation_valLength()
{
	result = true;
	if(this.minLength >= 0)
	{
		result = (this.fieldObject.value.length >= this.minLength) && result;
		//alert("Validation with minLength of "+this.minLength+" was "+result);
	}
	if(this.maxLength >= 0)
	{
		result = (this.fieldObject.value.length <= this.maxLength) && result;
		//alert("Validation with maxLength of "+this.maxLength+" was "+result);
	}
	return result;
}

function fieldValidation_doValFunction()
{
	result = true;
	if(this.bValFunction)
	{
		//alert(this.fieldName+".bValFunction: "+this.valFunction);
		if(this.getFieldObject() && this.getFieldObject().value)
			eval('result = '+this.valFunction+'(this.getFieldObject().value);');
		else
			eval('result = '+this.valFunction+'();');
	}
	
	return result;		
}

function fieldValidation_valRegExp()
{
	result = true;	
	
	if(this.bRegExp == false)
		return result;
	
	eval('reg = '+this.regExp);	
	result = (reg.test(this.fieldObject.value)) && result;	
	return result;
}

function fieldValidation_validate()
{
	result = true;
	
	this.fieldObject = this.getFieldObject();
	
	if(!this.valLength())
		return false;
	if(!this.valRegExp())
		return false;
	if(!this.doValFunction())
		return false;
		
/***
 ## This doesn't work for some reason that I can't understand...
 
	result = (result && this.valLength());	
	result = (result && this.valRegExp());
	result = (result && this.valFunction());
***/

	// return result.
	return true;
}

function fieldValidation_getError()
{
	return this.errorMsg;
}

function fieldValidation_isRequired()
{
	return this.required;
}

function formValidation(formName)
{
	this.formName = formName;
	this.fields = new Array();
	this.fieldCount = 0;
	
	// set the functions
	this.addField = formValidation_addField;
	this.validate = formValidation_validate;
	this.removeField = formValidation_removeField;
	this.submit = formValidation_submit;
}

function formValidation_addField(fieldObject)
{
	this.fields[this.fieldCount] = fieldObject;
	this.fieldCount++;
}

function formValidation_removeField(fieldName)
{
	found = false;
	for(i = 0; i < this.fieldCount; i++)
	{
		if(this.fields[i].fieldName == fieldName)
		{
			found = true;
			if((i+1) == this.fieldCount) this.fields[i] = null;
		}
		else if(found == true)
		{
			this.fields[i-1] = this.fields[i];
			if((i+1) == this.fieldCount)
				this.fields[i] = null;
		}
	}
	if(found) this.fieldCount--;
}

function formValidation_validate()
{
	lclResult = true;
	firstError =0;
	error = '';
	
	for(i = 0; i < this.fieldCount; i++)
	{
		// check to see if required !
		if(this.fields[i].isRequired())
		{
			if(!this.fields[i].validate())
			{
				lclResult &= false;
				if(error == "")
					firstError = i;
				error += this.fields[i].getError()+"\n";				
			}			
		}
		// else check to see if it has content.. if it does, then it needs to be corrected
		//else
	}
	
	
	
	if(lclResult == false)
	{
		if(this.fields[firstError] && this.fields[firstError].getFieldObject() && this.fields[firstError].getFieldObject().focus)
			this.fields[firstError].getFieldObject().focus();
		alert(error);
		return false;
	}
	else
		return true;
}

function formValidation_submit()
{
	if(this.validate())
		eval('document.'+this.formName+'.submit();');
}