

/*///////////////////////////////////////////////////////
// 
//   version: 1.00
//   author: john burnside
//   date: 5.13.03
//   description: functions for client-side form validation
//
///////////////////////////////////////////////////////*/

var DEBUG = true

/////////////////////////////////////////////////////////
//constants for field types
var FT_TEXT = 0						//validates text values (pretty much accepts anything)
var FT_INTEGER = 1						//validates integer values
var FT_CURRENCY = 2					//validates currency values
var FT_EMAIL = 3						//validates email addresses


/////////////////////////////////////////////////////////
//global variables
var aActions = new Array()				//array to hold collections of validation rules
var aErrors = new Array()				//array to hold collection of errors generated during validation


/////////////////////////////////////////////////////////
//functions
/* function: adds error message to list of errors */
function AddErrorMessage(strMessage)
{
	//add message to array of errors
	aErrors[aErrors.length] = strMessage
}

/* function: add a new validation rule to the given collection. */
function AddValidationRule(strAction,strFieldID,strFieldName,iFieldType)
{
	var iIndex

	//get index where item will be inserted, then insert item
	iIndex = aActions[strAction].length
	aActions[strAction][iIndex] = new ValidationRule(strFieldID,strFieldName,iFieldType)
}

/* function: creates new member of collection array to hold a new set of validation rules for the given action */
function AddValidationRuleCollection(strAction)
{
	//create new rule collection
	aActions[strAction] = new Array()
}

/* function: validate the given action with the collection of validation rules that have been created for that action */
function ValidateAction(oForm,strAction,bClearErrors)
{
	var i, strErrors

	//create new array of errors if requested to do so
	if (bClearErrors)
		aErrors = new Array()

	//loop through validation rules for this action
	for(i=0;i<aActions[strAction].length;i++)
		ValidateField(oForm,aActions[strAction][i])
		
	//output error message if errors occurred
	if (aErrors.length > 0)
	{
		strErrors = "Please correct the following errors:\n\n"
	
		//build error string
		for(i=0;i<aErrors.length;i++)
			strErrors += "* " + aErrors[i] + "\n"
			
		//alert user about errors
		alert(strErrors)	
	}
	else
		//submit the valid form
		oForm.submit()
}

/* function: validate the field for the field type given in the validation rule argument */
function ValidateField(oForm,oRule)
{
	//eet a reference to the form field to be validated
	var oField = oForm[oRule.FieldID]

	//validate based on rule type
	switch(oRule.FieldType)
	{
		case FT_TEXT:
		{	
			//generate error if field is empty
			if (!(IsValidText(oField.value)))
				AddErrorMessage(oRule.FieldName + " cannot be blank.")
			break
		}
		case FT_INTEGER:
		{	
			//generate error if field is not integer
			if (!(IsValidText(oField.value)))
				AddErrorMessage(oRule.FieldName + " must be a positive whole number (no decimals).")
			break
		}
		case FT_CURRENCY:
		{	
			//generate error if field is not valid currency
			if (!(IsValidCurrency(oField.value)))
				AddErrorMessage(oRule.FieldName + " must contain a valid decimal value with no currency signs (i.e. no '$').")
			break
		}
		case FT_EMAIL:
		{	
			//generate error if field is not email
			if (!(IsValidEmail(oField.value)))
				AddErrorMessage(oRule.FieldName + " must contain a valid e-mail address.")
			break
		}
		default:
			DebugWrite("Validating default")
	}
}

/* function: validates a value for a required text field */
function IsValidText(strValue)
{
	var reWhiteSpace = /\s/g		//white space regular expression

	return (strValue.replace(reWhiteSpace,"") != "")
}

/* function: validates a value for a required integer field */
function IsValidInteger(strValue)
{
	var reDigits = /[0-9]*/g		//digits only

	if (strValue.replace(reDigits,"") == "" && strValue != "")
		if (parseInt(strValue) < 0)
			return(false)
		else
			return(true)
	else
		return(false)
}

/* function: validates a value for a required integer field */
function IsValidCurrency(strValue)
{
	var reCurrency = /[0-9]+|[0-9]*.[0-9]+/g		//currency format

	if (strValue.replace(reCurrency,"") == "" && strValue != "")
		if (parseFloat(strValue) < 0)
			return(false)
		else
			return(true)
	else
		return(false)
}

/* function: validates a value for a required e-mail field */
function IsValidEmail(strValue)
{
	var reEmail = /^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/g		//email format
	
	return (strValue.replace(reEmail,"") == "" && strValue != "")
}

/* function: used as ValidationRule object, holds information about field to validate */
function ValidationRule(strFieldID,strFieldName,iFieldType)
{
	this.FieldID = strFieldID
	this.FieldName = strFieldName
	this.FieldType = iFieldType
}

/* function: writes messages if debug mode is on */
function DebugWrite(strMessage)
{
	alert(strMessage)
}