/*
 *		Generic Validation Script
 *		--------------------------------------------------------------------------------
 *		
 *		Copyright ©		: OpenSearch 2002
 *		Created By		: Brett Errington
 *		Last Updated	: 28/10/2002
 *		Version			: 0.9 (Final before full approval)
 *
 *
 *		Description
 *		--------------------------------------------------------------------------------
 *
 *		For use as a general validation routine on all HTML forms. Based on the .Net
 *		method of invoking validation using one hidden validator field per user field
 *		requiring validation. This routine caters for validation that occurs on most
 *		forms, such as checking for required fields, if an email is valid etc.
 *
 *
 *		Usage
 *		--------------------------------------------------------------------------------
 *
 *		In form add a hidden (validator) field as so:
 *
 *			<input type="hidden" name="[field]_Validator"
 *			 value="[required],[type]|[chars],[error1]|[error2]|[error3],[min],[max]">
 *
 *		Where:
 *				[field]			= The name of the field to validate (exact name)
 *				[required]		= If the field is required (true or false)
 *				[type]			= Special validation type. Choices:
 *										Text 	(default)
 *										Number 	(value is valid number)
 *										Email 	(value is valid email address)
 *										RegEx 	(please see regular expression section)
 *										File 	(value is valid filename, please see
 *												 for further details)
 *				[chars]			= List of invalid characters (e.g. "*()\/")
 *										You cannot use: ",|
 *				[error1]		= Message for required field error
 *				[error2]		= Message for minimum/maximum error
 *				[error3]		= Message for special type error
 *				[min]			= If special type is a number then this is the minimum
 *								  value, else this is the minimum length
 *				[min]			= If special type is a number then this is the maximum
 *								  value, else this is the maximum length
 *
 *		Finally add the following to the form tag:
 *
 *			onSubmit="return ValidateForm(this);"
 *
 *
 *		Notes
 *		--------------------------------------------------------------------------------
 *
 *		- Add one validator field per field requiring validation
 *		- Use underscores instead of spaces for field names
 *		- JavaScript is case sensitive
 *		- If you are using regular expressions, make sure they are valid for other
 *		  browsers such as Netscape.
 *
 *
 *		Regular Expressions
 *		--------------------------------------------------------------------------------
 *		
 *		If you intend on using a regular expression ( [type] = RegEx ) as the validator
 *		for a field then you will also need to add an extra hidden field as follows:
 *
 *			<input type="hidden" name="[field]_Validator_Ext"
 *			 value="[expression]">
 *
 *		Where:
 *				[field]			= The name of the field to validate (exact name)
 *				[expression]	= The regular expression to use for validation
 *
 *		--------------------------------------------------------------------------------
 *
*/


document.write("<scr" + "ipt language='javascript' src='/OSLibrary/JScript/inheritance.js'" +
			   " type='text/javascript'><\/scr" + "ipt>");
document.close();
			   
Imports    = new Array();
Imports[0] = 'Browser.Detection';
Imports[1] = 'Objects.Array';



function ValidateField( theValue, isRequired, specialConditions, minValue, maxValue, theField )
{
	specialConditions = specialConditions.split('|');
	
	var theType 	 = specialConditions[0];
	var invalidChars = specialConditions.length > 1 ? specialConditions[1] : '';
	
	if( isRequired == 'true' && theValue == '' )
	{
		return 'Blank';
	}
	else if( theValue != '' )
	{
		switch( theType.toLowerCase() )
		{
			case 'email' :
			{
				var emailValidation = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				
				if( ! emailValidation.test( theValue ) )
				{
					return 'Invalid';
				}
				
				break;
			}
			case 'number' :
			{
				if( isNaN( theValue ) )
				{
					return 'Invalid';
				}
				else
				{
					if( minValue != '' )
					{
						if( 1 * theValue < 1 * minValue )
						{
							return 'MinMax';
						}
					}
					if( maxValue != '' )
					{
						if( 1 * theValue > 1 * maxValue )
						{
							return 'MinMax';
						}
					}
				}
				break;
			}
			case 'regex' :
			{
				var theForm		 = theField.form;
				var validatorExt = new RegExp( eval('theForm.' + theField.name + '_Validator_Ext').value );
				var matches 	 = validatorExt.exec( theValue );
				
				if( ! ( matches != null && theValue == matches[0] ) )
				{
					return 'Invalid';
				}
				
				break;
			}
			case 'file' :
			{
				var theForm		 = theField.form;
				var validatorExt = eval('theForm.' + theField.name + '_Validator_Ext');
				
				var fileNLength = theValue.length;
				var fileNExt	= theValue.substring(fileNLength - 3, fileNLength);
				
				if( theValue.charAt( fileNLength - 4 ) != '.' )
				{
					return 'Invalid';
				}
				
				if( validatorExt != undefined )
				{
					var validExtensions = validatorExt.value.split('|');
					
					if( ! validExtensions.contains( fileNExt ) )
					{
						return 'InvalidExt';
					}
				}
			}
			default :
			{
				if( invalidChars != '' )
				{
					for( j = 0; j < invalidChars.length; j++ )
					{
						if( theValue.indexOf( invalidChars.charAt(j) ) > -1 )
						{
							return 'Invalid';
						}
					}
				}
				if( minValue != '' )
				{
					if( theValue.length < 1 * minValue )
					{
						return 'MinMax';
					}
				}
				if( maxValue != '' )
				{
					if( theValue.length > 1 * maxValue )
					{
						return 'MinMax';
					}
				}
				break;
			}
		}
	}
	
	return '';
}

function ValidateForm( theForm )
{
	for( var i = 0; i < theForm.elements.length; i++ )
	{
		var baseTypes  = new Array('text','textarea','password','file');
		var baseType   = theForm.elements[i].type;
		
		if( baseTypes.contains( baseType ) )
		{
			validator = eval('theForm.' + theForm.elements[i].name + '_Validator');
			
			if( validator != undefined )
			{
				theValue   = theForm.elements[i].value;
				theParams  = validator.value.split(',');
				theErrMsgs = theParams[2].toString().split('|');
				
				theResult = ValidateField( theValue,
										   theParams[0],
										   theParams[1],
										   theParams[3],
										   theParams[4],
										   theForm.elements[i] );
				
				if( theResult == 'Blank' )
				{
					alert( theErrMsgs[0] );
					return false;
				}
				else if( theResult == 'MinMax' )
				{
					alert( theErrMsgs[1] );
					return false;
				}
				else if( theResult == 'Invalid' )
				{
					var theErr = theErrMsgs.length > 2 ? theErrMsgs[2] : theErrMsgs[1];
					alert( theErr );
					return false;
				}
				else if( theResult == 'InvalidExt' )
				{
					alert( theErrMsgs[2] );
					return false;
				}
			}
		}
	}
	
	return true;
}