<!--
/********************************************************************

Procedure Name:		ValidateInput
Short Description:	This function (along with helper functions) was
					developed for client-end validation of text fields.
					This file may be used as an include file.

Arguments:			curObj		- The object of the field to be
								  validated.
					filedType	- Defines the type of the field
					bTrim		- If true, then white spaces on left
								  and right-handside are removed
					bAllowNull	- If true, then field might be empty.
								  Takes precedence over nMinLength
					nMinLength	- Value must be greater or equal to
								  nMinLength in length. nMinLength is
								  ignored if bAllowNull is true
Input:				An object of a text field
Output:				An error message and 'false', otherwise true
Author:				Hamid Quddus Akhtar
Limitations:		The object passed must have a valid 'Title'
					The object must be a valid field type
Date Created:		July 01, 2001
Date Modified:		June 20, 2002
Version:			1.0.2.1

NOTE:				Functions for validation exploit 'Regular Expression'
					For help on 'Regular Expressions', see JScript or
					VBScript reference in MSDN

********************************************************************/

function ValidateInput(curObj,fieldType,bTrim,bAllowNull,nMinLength)
{
	// Trims the fields if the value is true
	if( bTrim )
		TrimString(curObj);

	// Boolean - true if field is validated, otherwise false
	var bisVarified = false;


	// fieldType case...
	switch(fieldType)
	{
		case  0: bisVarified = ValidateNames(curObj);					break;
		case  1: bisVarified = ValidateCompanyName(curObj);				break;
		case  2: bisVarified = ValidateCityName(curObj);			 	break;
		case  3: bisVarified = ValidatePostCode(curObj);				break;
		case  4: bisVarified = ValidateID(curObj);						break;
		case  5: bisVarified = ValidateStreetBuildingAddress(curObj);	break;
		case  6: bisVarified = ValidateEmailAddress(curObj);			break;
		case  7: bisVarified = ValidateTelephoneNumber(curObj);			break;
		case  8: bisVarified = ValidateNumber(curObj);					break;
		case  9: bisVarified = ValidateFloatingPoint(curObj);			break;
		case 10: bisVarified = ValidatePassword(curObj);				break;
		case 11: bisVarified = ValidateDateEng(curObj);					break;
		case 12: bisVarified = ValidateAlphaNumeric(curObj);			break;
		default: alert("ValidateInput ERROR: Invalid fieldType value." + fieldType);	return false;
	}

	// If field is a 'nullable' field and empty, then return true
	if( curObj.value == "" && bAllowNull )
		return true;

	// If field length is less than minimum length, the generate error, and set focus back to field
	// NOTE: If field is nullable and empty, then this code is never reached!
	if( curObj.value.length < nMinLength )
	{
		// Using field title in error generated.
		alert( "No value, or value too small for " + curObj.title + ".\nThe value should at least be " + nMinLength + "  characters in length." );
		curObj.focus();
		return false;
	}

	// If field is not validate, then generate an error, and return false
	if( bisVarified == false && fieldType != 11 )
	{
		alert( "Incorrect value for " + curObj.title + "." );
		curObj.focus();
		return false;
	}
	else if( fieldType == 11 && bisVarified == false )
	{
		alert( "Incorrect value for " + curObj.title + ".\nDate required in dd/mm/yyyy format." );
		curObj.focus();
		return false;
	}

	// True, if everything has been successful
	return true;
}


// Trims spaces, vertical/horizontal tabs, newline and carriage returns
// This function fixes the actual value in the text box
function TrimString(curObj)
{
	var re;
	re = /^[ \f\n\r\t\v]+/;
	curObj.value = curObj.value.replace(re,"");
	re = /[ \f\n\r\t\v]+$/;
	curObj.value = curObj.value.replace(re,"");
}

// Helper function for following validation functions
// Returns false if the value is not validated, otherwise true
function MatchRegExp(curObj,re)
{
	if( curObj.value.match(re) == null )
		return false;

	return true;
}
function ValidateNames(curObj)
{
	var re;
	re = /^[a-zA-Z]+(( |-)[a-zA-Z]+)*$/;

	return MatchRegExp(curObj,re);
}

function ValidateCompanyName(curObj)
{
	return true ;
}

function ValidateCityName(curObj)
{
	return true ;
}

function ValidatePostCode(curObj)
{
	return true ;
}

function ValidateID(curObj)
{
	var re;
	re = /^[a-zA-Z0-9]+(( )?(-( )?)?[a-zA-Z0-9]+)*$/;

	return MatchRegExp(curObj,re);
}

function ValidateStreetBuildingAddress(curObj)
{
	return true ;
}

function ValidateEmailAddress(curObj)
{
	var re;
//	re = /^([A-Za-z0-9])+(\.([A-Za-z0-9])+|_([A-Za-z0-9])+)*@(([a-zA-Z0-9])+(_)?([a-zA-Z0-9])+)+((_|\.)?([a-zA-Z0-9])+)*\.([a-zA-Z]){2,}$/;
//	re = /^([A-Za-z0-9])+(\.([A-Za-z0-9])+|(_|-)([A-Za-z0-9])+)*@(([a-zA-Z0-9])+(_)?([a-zA-Z0-9])+)+((_|\.|-)?([a-zA-Z0-9])+)*\.([a-zA-Z]){2,}$/;
	re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

	return MatchRegExp(curObj,re);
}

function ValidateTelephoneNumber(curObj)
{
	var re;
	re = /^(\([a-zA-Z0-9]+((-|\.| )[a-zA-Z0-9]+)*\))?( )?([a-zA-Z0-9]+((-|\.| )[a-zA-Z0-9]+)*)+$/;

	return MatchRegExp(curObj,re);
}

function ValidateNumber(curObj)
{
	var re;
	re = /^[0-9]+$/;

	return MatchRegExp(curObj,re);
}

function ValidateFloatingPoint(curObj)
{
	var re;
	re = /^([0-9])*(\.([0-9]){1,2})?$/

	return MatchRegExp(curObj,re);
}

function ValidatePassword(curObj)
{
	var re;
	re = /^\w+$/

	return MatchRegExp(curObj,re);
}

function ValidateDateEng(curObj)
{
	var re;
	re = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}$/;

	if( !MatchRegExp(curObj,re) )
		return false;

	var Dates = curObj.value.split("/");

	var myDay, myMonth, myYear;
	myDay	= parseInt(Dates[0]);
	myMonth = parseInt(Dates[1]);
	myYear	= parseInt(Dates[2]);

	if( myMonth > 12 )
		return false;

	if( myMonth == 1 || myMonth == 3 || myMonth == 5 || myMonth == 7 || myMonth == 8 || myMonth == 10 || myMonth == 12 )
	{
		if( myDay > 31 )
			return false;
	}

	if( myMonth == 4 || myMonth == 6 || myMonth == 9 || myMonth == 11 )
	{
		if( myDay > 30 )
			return false;
	}

	if( myMonth == 2 )
	{
		if( myYear % 4 == 0 )
		{
			if( myDay > 29 )
				return false;
		}
		else
		{
			if( myDay > 28 )
				return false;
		}
	}

	return true;
}

function ValidateAlphaNumeric(curObj)
{
//	var re;
//	re = /^[a-zA-Z]+(( |-)?[a-zA-Z0-9]+)*$/;

	return true ;
}

//-->
