/*
        File: validation.js
     Version: 1.0

     Created: 10/05/05
    Modified: 24/07/05
   Copyright: Matthew Cassidy 2005
       Email: matt@afterglowmedia.com.au

     Purpose: Validates forms depending on information provided
        Note: None

     License: For use solely by Afterglow Media. 
*/


/*------------------------------------------------------------------*/
/* Function: validate()                                             */
/* Sets variables and calls functions required to validate the form */
/*------------------------------------------------------------------*/
function validate() {
	this.errors = "";
	arguments = validate.arguments;
	/* Set the form name variable */
	var formName = arguments[0];
	
	/* Set the form object */
	this.formObj=document.forms[formName];
	
	/* Check that a form name was provided and that it exists in the document */
	if (!formName || !this.formObj) {
		alert ("System bug encountered.\nCannot validate data - continuing without validation.");
		return true;
	}
	
	/* Iterate through the element data provided and call required functions */
	var elementsCount = arguments.length;
	i=0;
	for (x=1; x<=(elementsCount-1)/3; x++) {
		i++;
		this.elementName = arguments[i];
		i++;
		this.validationType = arguments[i];
		i++;
		this.errorMsg = "\n* " + arguments[i];
		this.errors += validateData();
	}
	/* Display errors if found otherwise continue */
	if (this.errors) {
		alert ("There are errors in the form"+this.errors);
		return false;
	}
	else {
		return true;
	}
}


/*-----------------------------------------------------------------------------------------*/
/* Function: validateData()                                                                */
/* Works out the type of validation required, calls necessary functions and returns result */
/*-----------------------------------------------------------------------------------------*/
function validateData() {
	/* Set error retention variable */
	var error = "";
	
	/* Ensure element is present */ 
	this.elementObj = this.formObj.elements[this.elementName];
	if (!this.elementObj) {
		alert ("Element: '"+this.elementName+"' not found.\nCannot validate this field");
	}
	
	/* Work out the type of validation required */
	var validationArray = this.validationType.split("=");
	
	/* Call necessary function depending on validation required */
	switch (validationArray[0]) {
		case "required": 
		case "req": {
			(!validateRequired()) ? error = this.errorMsg : error = "";
			break
		}
		case "maxlength": 
		case "maxlen": {
			(!validateMax(validationArray[1])) ? error = this.errorMsg : error = "";
			break
		}
	}
	
	/* Reset element vars */
	clearElementVars();
	return error;
}


/*--------------------------------*/
/* Function: validateRequired()   */
/* Performs 'required' validation */
/*--------------------------------*/
function validateRequired() {
	var result = "";
	/* Act according to the element type */
	switch (this.elementObj.type) {
		/* Textbox, selectbox, file or text area */
		case "text":
		case "select-one":
		case "file": 
		case "textarea": {
			/* Check to see if there is data in the element */
			(!this.elementObj.value) ? result = false : result = true;
			break;
		}
		/* Checkbox */
		case "checkbox": {
			/* Check to see if there is data in the element */
			(!this.elementObj.checked) ? result = false : result = true;
			break;
		}
		/* Unsupported */
		default: {
			result = true;
			break;
		}
	}
	return result;
}


/*--------------------------------------*/
/* Function: validateMax(maxLength)     */
/* Performs 'maximum length' validation */
/*--------------------------------------*/
function validateMax(maxLength) {
	var result = "";
	/* Check to see if the data is too long */
	(this.elementObj.value.length >= maxLength) ? result = false : result = true;
	return result;
}


/*----------------------------------------------------*/
/* Function: clearElementVars()                       */
/* Clears the variables used in validating an element */
/*----------------------------------------------------*/
function clearElementVars() {
	this.elementName = "";
	this.validationType = "";
	this.errorMsg = "";
}
