//=------------------------------------------------------------------------=
//	Copyright (C) 1998-2000 Microsoft Corporation.  All rights reserved.
//
// 	File:  Validation_Functions.js
//
// 	Purpose:
//			This contains functions for validating form fields, and displayig 
//			errors when appropriate
// 	History:
//			09-27-00 Created by jcroney
//			05-01-01  srinip		Added global variable for dealing with
//											setTimeout.
//=------------------------------------------------------------------------=

// Adding a global variable because the 
var g_oElem;
//=------------------------------------------------------------------------=
//	Begin Functions
//=------------------------------------------------------------------------=

//=-------------------------------------------------------------------------=
//	Function: Get the value from a control
//=-------------------------------------------------------------------------=
	
function getValue(oFormElement)
{
	var strRet = ""
	// Check that I actually got an object passed to me
	if (typeof oFormElement == "object") 
	{
		switch (oFormElement.type)
		{
			case 'text':
			// Check for a non-blank value
				if (!isBlank(oFormElement.value))
				{
					strRet = oFormElement.value;
				}
			break;
			case 'textarea':
			// Check for a non-blank value
				if (!isBlank(oFormElement.value))
				{
					strRet = oFormElement.value;
				}
			break;			
			case 'select-one':
			// Check that the selected index in greater than 0
				if (oFormElement.selectedIndex >= 0) 
				{
					strRet = oFormElement.options[oFormElement.selectedIndex].value;
				}
			break;
			case 'file':
			// Check for a non-blank value
				if (!isBlank(oFormElement.value))
				{
					strRet = oFormElement.options[oFormElement.selectedIndex].value;
				}
			break;	
			default:
				break;
		}

		// Checking for radion buttons, checkboxes and selects
		// currently, checking only for radio buttons
		if (oFormElement.length != null) 
		{
			if (oFormElement[0] && oFormElement[0].type == 'radio') 
			{
				for (var i=0; i < oFormElement.length; i++)
				{	
					if (oFormElement[i].checked == true)
						strRet = oFormElement[i].value;
				}
			}
		}
	}
	return strRet;
}

//=------------------------------------------------------------------------=
//	Name:			validateEntry
//	Author:			Joseph Croney
//	Description:	checks for valid entry, and normalizes format
//					displays error if appropriate
//	Arguments:		oFormElement	- form element object (this)
//					sFriendlyName	- friendly name of field to display in error
//					iErrorID		- ID for base error string in error llst
//					sFormatRule		- rule to check against
//					SMin			- minimum value
//					SMax			- maximum value	
//					sOptParam		- n number of optional params
//
//	Returns:		Bool
//	History:		9/20/00 - Created
//=------------------------------------------------------------------------=
function validateEntry(oFormElement, sFriendlyName, iErrorID, sFormatRule)
{
	
	// Check that I actually got an object passed to me
	if (typeof oFormElement != "object") return false;

	var s = Trim(getValue(oFormElement));
	
	g_oElem = oFormElement;

	if (isBlank(s) == false){
	// element is not blank 
	// find the appropriate rule
	// run validation rule
	// if fails display error, return -1, set focus to element
	// else apply format rule, update oFormElement.value
	// return 0
		var sOptMin;
		var sOptMax;
		
		switch (sFormatRule)
		{	
			case 'Validate_Integer':
			// Integer Rule
			// First Check if it is a number
			// Opt Param 1 = min
			// Opt Param 2 = max
				if (arguments.length > 4) sOptMin = arguments[4] - 0;		
				if (arguments.length > 5) sOptMax = arguments[5] - 0;	
			
				if (!isInteger(s) || 
					(!isBlank(sOptMin) && s < sOptMin) ||
					(!isBlank(sOptMax) && s > sOptMax)) 
				{	
					displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s, sOptMin, sOptMax));
					delayFocus(oFormElement);
					return false;	
				}		 
			break;
			
			case 'Validate_Numeric':
			// Looks for a positive number
			// may be integer or float
			// Opt Param 1 = min
			// Opt Param 2 = max
				if (arguments.length > 4) sOptMin = arguments[4] - 0;		
				if (arguments.length > 5) sOptMax = arguments[5] - 0;	
				if (!isFloat(s) || 
					(!isBlank(sOptMin) && s < sOptMin) ||
					(!isBlank(sOptMax) && s > sOptMax))
				{
					displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s));
					delayFocus(oFormElement);
					return false;		
				}
			break;
			
			case 'Validate_Currency':
			// Validate a Monetary Value
			// Opt Param 1 must be currency type
			// Opt Param 2 = min
			// Opt Param 3 = max
			// currently support US_Dollar
				var sMonetaryValue='';
			
				if (arguments.length > 5) sOptMin = arguments[5] - 0;		
				if (arguments.length > 6) sOptMax = arguments[6] - 0;
				switch (arguments[4])
				{
					case 'US_Dollar':
					// strip off dolar sign if present
					// format -> $###.##(#)
						sMonetaryValue = stripSpecialChars(s, '$,');
						if (sMonetaryValue.length != 0)
						{
							if (!isFloat(sMonetaryValue) || 
								(!isBlank(sOptMin) && sMonetaryValue < sOptMin) ||
								(!isBlank(sOptMax) && sMonetaryValue > sOptMax)) 
							{
								displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s, formatDollarValue(sOptMin), formatDollarValue(sOptMax)));
								delayFocus(oFormElement);
								return false;
							}
						}
					oFormElement.value = formatDollarValue(sMonetaryValue);	
					break;
				}
					
			break;
			
			case 'Validate_Email':
			// Validate proper email address format
			// Opt Param 1 = email host (i.e. hotmail.com)
				if (arguments.length > 4) var sOptHost = arguments[4];
				if ((s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) ||
					(sOptHost && (s.search(sOptHost) == -1)))
				{
					displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s,"","",sOptHost));
					delayFocus(oFormElement);
					return false;				
				}
			break;
			
			case 'Validate_URL':
			// format url with default http protocol
					
				if (s.indexOf('http://') != 0 &&
					s.indexOf('https://') != 0 &&
					s.indexOf('ftp://') != 0)
					oFormElement.value = 'http://' + s;
					
			break;
			
			case 'Validate_Phone':
			// Validates Phone Number
			// Param 1 -> Format US_Phone
			// Param 2 -> Min digits
			// Param 3 -> Max digits
			
				if (arguments.length > 5) sOptMin = arguments[5] - 0;		
				if (arguments.length > 6) sOptMax = arguments[6] - 0;			
				switch(arguments[4])
				{
					case 'US_Phone':
					// US Phone (###)###-#### ###...
					// letter ok in number
					// strip initial 1 if given
						var normalizedPhone = stripSpecialChars(s, '()-+. ');
						normalizedPhone = normalizedPhone.toUpperCase();
						if (normalizedPhone.charAt(0) == 1 || normalizedPhone.charAt(0) == 0) normalizedPhone = normalizedPhone.slice(1); 
						if (!isAlphaNumeric(normalizedPhone) || normalizedPhone.length < 10 ||
							(!isBlank(sOptMin) && normalizedPhone.length < sOptMin) ||
							(!isBlank(sOptMax) && normalizedPhone.length > sOptMax ))
						{
							displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s, sOptMin, sOptMax, arguments[4]));
							delayFocus(oFormElement);
							return false;	
						}
						// Looks good now format it
						oFormElement.value = formatString(normalizedPhone,"(",3,") ",3,"-",4);
						if (normalizedPhone.length > 10) oFormElement.value+=' '+normalizedPhone.slice(10);			
					break;
				}						
			break;	
			
			case 'Validate_PostalCode':
			// Postal Codes
			// Param 1 -> US Zip / International?
			// Param 2 -> Min digits
			// Param 3 -> Max digits

				if (arguments.length > 5) sOptMin = arguments[5] - 0;		
				if (arguments.length > 6) sOptMax = arguments[6] - 0;	

				switch(arguments[4])
				{
					case 'US_ZIP':
					// US Zip Code #####(-####)
						var sNormalizedZip = stripSpecialChars(s, '- ');
						if ((isInteger(sNormalizedZip) == false ) || 
							(sNormalizedZip.length != 5 && sNormalizedZip.length != 9)||
							(!isBlank(sOptMin) && sNormalizedZip.length < sOptMin) ||
							(!isBlank(sOptMax) && sNormalizedZip.length > sOptMax ) ||
							(isNonZero(sNormalizedZip) == false)
						   )
						{
								displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s, sOptMin, sOptMax, arguments[4]));
								delayFocus(oFormElement);
								return false;	
						}
						if (sNormalizedZip.length == 9)
							oFormElement.value = formatString(sNormalizedZip,"",5,"-",4);
						else
							oFormElement.value = sNormalizedZip;
					break;		
				}			
			break;	
			
			case 'Validate_State':
			// Checks for a US State
			// Param 1 -> string of | delimited states which are valid
				var bFound = false;
			 
				if (arguments.length > 4) var aStateCodes = arguments[4].split("|");
				else var aStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP".split("|");
				var sNormalizedState = s.toUpperCase();
				for (var i=0; i< aStateCodes.length; i++)
					if (aStateCodes[i] == sNormalizedState) bFound = true;			
				if (!bFound)
				{	
					displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s, sOptMin, sOptMax, arguments[4]));
					delayFocus(oFormElement);
					return false;
				}
					oFormElement.value=sNormalizedState;					
			break;
			
			case 'Validate_Year':
			// Year Validation
			// Param 1 -> digits required in year
			// Param 2 -> Min year
			// Param 3 -> Max year
				var sNormalizedYear = stripSpecialChars(s, "' ");
				if (arguments.length > 4) var sOptLength = arguments[4];
				if (arguments.length > 5) sOptMin = arguments[5] - 0;
				if (arguments.length > 6) sOptMax = arguments[6] - 0;
				if (!isInteger(sNormalizedYear) ||
					(sNormalizedYear.length != 4 && sNormalizedYear.length != 2) ||
					(sOptLength && sNormalizedYear.length != sOptLength) ||
					(!isBlank(sOptMin) && sNormalizedYear < sOptMin) ||
					(!isBlank(sOptMax) && sNormalizedYear > sOptMax)) 
				{
					displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s, sOptMin, sOptMax, sOptLength));
					delayFocus(oFormElement);
					return false;
				}	
				oFormElement.value = sNormalizedYear;
	
			break;
			
			case 'Validate_ProperNoun':
			// Proper Noun, no numeric chars
			// Capitalize first letter only
			// Param 1 -> min length
			// Param 2 -> max length
			
				var sNormalizedNoun = s; //.toLowerCase(); 
				
				if (arguments.length > 4) sOptMin = arguments[4];
				if (arguments.length > 5) sOptMax = arguments[5];
				if (!isAlphabetic(sNormalizedNoun,".-,' ") ||
					(!isBlank(sOptMin) && sNormalizedNoun.length < sOptMin) ||
					(!isBlank(sOptMax) && sNormalizedNoun.length > sOptMax))
				{
					displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s, sOptMin, sOptMax));
					delayFocus(oFormElement);
					return false;
				}
				oFormElement.value = capitalizeString(sNormalizedNoun);
			break;

			case 'Validate_Date':
			// Date Entry accepts dates with M/D/YYYY format
			// can handle / or - for dividers
			// Param 1 -> min date MM/DD/YYYY
			// Param 2 -> max date MM/DD/YYYY
				var sOptMin;
				var sOptMax;
				var sReturnDate;	
				if (arguments.length > 4) sOptMin = arguments[4];
				if (arguments.length > 5) sOptMax = arguments[5];
				sReturnDate = isDate(s, sOptMin, sOptMax)
				if (sReturnDate == "")
				{	
					displayErrorMessage(createErrorMessage(iErrorID, sFriendlyName, s, sOptMin, sOptMax));
					delayFocus(oFormElement);
					return false;				
				}
				oFormElement.value = sReturnDate;			
			break;
											
			default:
			// Unknown Validation Rule Name
				return true;
				break;	
		}		
	}
	else
	{
		return false;
	}

	return 1;	
}




function isBlank(sInputString) 
//	Description:	checks if string is blank / empty
{
	if ((sInputString == null) || (sInputString == "")) return true;
	
	for (var i=0; i< sInputString.length; i++) {
		var c = sInputString.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return false;
}

function isInteger(inString)
//Checks for a numeric string
{
	for (var i = 0; i < inString.length; i++)
	{
		if (!isNumeric(inString.charAt(i)))
		{
			return false;
		}
	}	
	return true;
}

function isFloat(inString)
// Checks for a float
{
	var bSeenDec = false;
	for (var i = 0; i < inString.length; i++)
	{   
	// Check that current character is number.
		var c = inString.charAt(i);
		if ((c == '.') && !bSeenDec) bSeenDec = true;
		else if (!isNumeric(c)) return false;
    }
    return true;
}

function isNumeric(x)
// Checks that char is a digit
{
	return ((x >= "0" && x <= "9"));
}

function isAlphabetic(inString, sAllow)
// checks for an alphabetic string spaces ok
// sAllow are the non alphabetic chars to allow in the string
{
	for (var i = 0; i < inString.length; i++)
	{
		if (!isAlpha(inString.charAt(i)) && inString.charAt(i) != " " && sAllow.indexOf(inString.charAt(i)) == -1)
		{
			return false;
		}
	}	
	return true;	
}

function isAlpha(c)
// checks that char is alphabetic
// I attempted to account for all of the different character sets
// We are not using unicode...
{
	return ( ((c >= "a") && (c <= "z")) || 
		((c >= "A") && (c <= "Z"))) // ||
		//((c >= chr(192)) && (c <= chr(259))))
}

function isAlphaNumeric(inString)
// checks that all elements of string are alphanumeric chars
{
	for (var i = 0; i < inString.length; i++)
	{
		if (!isAlpha(inString.charAt(i)) && inString.charAt(i) != " " && !isNumeric(inString.charAt(i)))
		{
			return false;
		}
	}	
	return true;
}


function isDate(sDate, sMinDate, sMaxDate)
// Checks for a valid date
// All dates follow MM/DD/YYYY format!
// if sMinDate/sMaxDate is blank no range checking is performed
// sMinDate + Max Date MUST be in MM/DD/YYYY i.e, 01/02/2000
// returns "" if there is an error
// else returns date formatted to MM/DD/YYYY
{

	// First we need to decalare the number of days for each month
	var daysInMonth = new Array(12);
	daysInMonth[1] = 31; daysInMonth[3] = 31; daysInMonth[4] = 30; daysInMonth[5] = 31;
	daysInMonth[6] = 30; daysInMonth[7] = 31; daysInMonth[8] = 31; daysInMonth[9] = 30; daysInMonth[10] = 31; 
	daysInMonth[11] = 30; daysInMonth[12] = 31;
	
	// correct feb for leap years
	daysInMonth[2] = (  ((sYear % 4 == 0) && ( (!(sYear % 100 == 0)) || (sYear % 400 == 0) ) ) ? 29 : 28 );	

	// grab the M/D/Y from sDate, we look for both / and - dividers  
	var iFirstSlash = sDate.indexOf("/");
	var iSecondSlash = sDate.indexOf("/", iFirstSlash + 1);

	if (iFirstSlash == -1 || iSecondSlash == -1) {
	// Ok I did not find the / chars so I will look for -'s
		iFirstSlash = sDate.indexOf("-");
		iSecondSlash = sDate.indexOf("-", iFirstSlash + 1);
		if (iFirstSlash == -1 || iSecondSlash == -1) return "";
		// Did not find two /s or -s ;(
	}
	var sMonth = sDate.substring(0, iFirstSlash) - 0;
	var sDay = sDate.substring(iFirstSlash + 1, iSecondSlash) - 0;
	var sYear = sDate.slice(iSecondSlash + 1) - 0;
	if (!isInteger(sDay) || !isInteger(sMonth) || !isInteger(sYear)) return "";

	

	// correct feb for leap years if we have a valid 4 digit year
	if (sYear > 999 && sYear < 10000)
		daysInMonth[2] = (  ((sYear % 4 == 0) && ( (!(sYear % 100 == 0)) || (sYear % 400 == 0) ) ) ? 29 : 28 );	
	else
		return "";
		
	// check that month & day are valid
	if ((sMonth < 1 || sMonth > 12) || (sDay < 1 || sDay > daysInMonth[sMonth])) return "";
	
	// check that date is between min and max if specified
	sMinDate = stripSpecialChars(sMinDate, " /-");
	sMaxDate = stripSpecialChars(sMaxDate, " /-");
	
	if (!isBlank(sMinDate))
	// min check
	{
		var sMinMonth = sMinDate.slice(0,2) - 0;	
		var sMinDay = sMinDate.slice(2,4) - 0;
		var sMinYear = sMinDate.slice(4) - 0;
		if (sYear < sMinYear ||
			(sYear == sMinYear && sMonth < sMinMonth) ||
			(sYear == sMinYear && sMonth == sMinMonth && sDay < sMinDay))
			return "";
	}
			
	if (!isBlank(sMaxDate)) 
	// max check
	{
		var sMaxMonth = sMaxDate.slice(0,2) - 0;	
		var sMaxDay = sMaxDate.slice(2,4) - 0;
		var sMaxYear = sMaxDate.slice(4);
		if (sYear > sMaxYear ||
			(sYear == sMaxYear && sMonth > sMaxMonth) ||
			(sYear == sMaxYear && sMonth == sMaxMonth && sDay > sMaxDay))
			return "";
	}
	return (sMonth + '/' + sDay + '/' + sYear);	
}

function Trim(x)
// Remove all spaces on both sides
{
	return RTrim(LTrim(x));
}

function LTrim(x)
// Remove leading spaces
{
	var i = 0;
	while ( i < x.length && x.charAt(i) == " " ) i++;
	return x.substring(i);
}

function RTrim(x)
// Remove trailing spaces
{
	var i = x.length-1;
	while ( i >= 0 && x.charAt(i) == " " ) i--;
	return x.substring(0, i+1);
}

function stripSpecialChars(sInputString, sCharsToStrip)
// Remove special Characters from string
{   
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in sCharsToStrip, append to returnString.
    if (isBlank(sInputString)) return;
    for (var i = 0; i < sInputString.length; i++) {   
        var c = sInputString.charAt(i);
        if (sCharsToStrip.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function formatString(sInputString)
// Formats a string to specified form
// Pass n pairs of:  (string to insert) and (# of chars to insert from original string)
// For Example to format a phone number to (###) ###-#### call
// formatString('1234567890','(',3,') ',3,'-',4) which will return (123) 456-7890
{   
    var sPos = 0;
    var sResultString = "";
    for (var i = 1; i < arguments.length; i++) {
       var sArg = arguments[i];
       if (i % 2 == 1) sResultString += sArg;
       else {
           sResultString += sInputString.substring(sPos, sPos + sArg);
           sPos += sArg;
       }
    }
    return sResultString;
}

function capitalizeString(sInputString)
// capitalize the first letter of every word in the string
// also capitalizes any letter appearing after a '.'
{
	var sResultString = sInputString.charAt(0).toUpperCase();
	var bSeenDot=false 
	for (var i=1; i < sInputString.length; i++){
		if (sInputString.charAt(i) == " " && sInputString.length > i && 
				sInputString.charAt(i+1) != ".") {
			i++;
			sResultString += " " + sInputString.charAt(i).toUpperCase();
		}
		else if (sInputString.charAt(i) == "." && sInputString.length > i &&
					sInputString.charAt(i+1) != " "){
			i++;
			sResultString += "." + sInputString.charAt(i).toUpperCase();
		}
		else
			sResultString += sInputString.charAt(i);
	}
	return(sResultString);
}

function formatDollarValue(fValue) 
// Formats a string to $##,###.##
// If it is a whole dollar value, do not add '.00'
{
	fValue = fValue.toString().replace(/\$|\,/g,'');
	if(isNaN(fValue)) fValue = "0";
	iCents = Math.floor((fValue*100+0.5)%100);
	iDollars = Math.floor((fValue*100+0.5)/100).toString();
	if(iCents < 10) iCents = "0" + iCents;
	for (var i = 0; i < Math.floor((iDollars.length-(1+i))/3); i++)
		iDollars = iDollars.substring(0,iDollars.length-(4*i+3))+','+iDollars.substring(iDollars.length-(4*i+3));
	return ('$' + iDollars + '.' + iCents);
}

//=------------------------------------------------------------------------=
//	Name:			createErrorMessage
//	Author:			Joseph Croney
//	Description:	grabs error message from array, replaces params
//	Arguments:		iErrorID		- error id
//					sFriendlyName	- friendly name replaces %name%
//					sUserEntry		- user entry replace %userentry%
//					sMin,sMax		- replace %min% or %max%
//					n opt params	- replace %1% ... %n%
//	Returns:		sErrorString	- final error message
//=------------------------------------------------------------------------=
function createErrorMessage(iErrorID, sFriendlyName, sUserEntry, sMin, sMax)
{
	var sErrorString;
	// grab error string by matching ruleID to error table
	// replace %name with friendlyname
	// replace %min% or %max% with sMin,sMax
	// replace %opt1 %opt2 with sParam1 and sParam2
	// Display alert to user
	
	if (!aErrorStrings || isBlank(aErrorStrings[iErrorID])) 
	// Error String Array not loaded, or ErrorID does not exist in array
		sErrorString = "Please enter a valid value for %name%.";
	else
		sErrorString = aErrorStrings[iErrorID];
	sErrorString = sErrorString.replace(/%name%/g, sFriendlyName);
	sErrorString = sErrorString.replace(/%userentry%/g, sUserEntry);
	sErrorString = sErrorString.replace(/%min%/g, sMin);
	sErrorString = sErrorString.replace(/%max%/g, sMax);
	for (var i=5; i < arguments.length; i++)
		sErrorString = sErrorString.replace('%'+(i-4)+'%', arguments[i]);			
	return(sErrorString);
}

function displayErrorMessage(sErrorString)
// display an error message to the user
{
	alert(sErrorString);
}


function delayFocus(oFormElement)
// get's around IE limitations for honoring focus() calls in the onchange event prior to onBlur
// This will fail if we have a funciton which runs on the onBlur event that executes in a time
// greater than iDelaySeconds
{
	var iDelaySeconds = 50; // Miliseconds to delay before calling focus commands
	if(g_oElem.type == "text")
		setTimeout("g_oElem.focus(); g_oElem.select();", 50);
}


// Check for leading zeroes (useful for zipcodes)
function isNonZero(s)
{
	if (s == null) return null
	s = '' + s;
	var t = '';
	var isLeadingZero = true;
	for (var i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (isNumeric(c))
		{
			if (isLeadingZero && (c == '0'))
				continue;  // remove leading zeros to avoid javascript octal conversion
			else
				isLeadingZero=false;
			t += c;
		}
	}
	if (t.length > 0) 
		return true;
	return false;
}



//=------------------------------------------------------------------------=
//	Name:			validateRequired
//	Author:			Joseph Croney
//	Description:	checks that a required form= field has been entered
//	Arguments:		oFormElement	- form element to validate
//					sFriendlyName	- friendly name of field
//  Returns:		Boolean			- True: no errors, False: blank entry
//=------------------------------------------------------------------------=
function validateRequired(oFormElement, sFriendlyName)
{
	// Check that I actually got an object passed to me
	if (typeof oFormElement != "object") return false;
	
	var nID = DEFAULT_REQUIRED_MESSAGE_ID;
	if (arguments.length > 2)
	{
		nID = arguments[2];
	}

	switch (oFormElement.type)
	{
		case 'text':
		// Check for a non-blank value
			if (isBlank(oFormElement.value))
			{
				displayErrorMessage(createErrorMessage(nID, sFriendlyName));
				return false;
			}
		break;
		case 'textarea':
		// Check for a non-blank value
			if (isBlank(oFormElement.value))
			{
				displayErrorMessage(createErrorMessage(nID, sFriendlyName));
				return false;
			}
		break;			
		case 'select-one':
		// Check that the selected index in greater than 0
			if ((oFormElement.selectedIndex < 0) || ((oFormElement.options[oFormElement.selectedIndex].value == '') || (oFormElement.options[oFormElement.selectedIndex].value == null)))
			{
				displayErrorMessage(createErrorMessage(nID, sFriendlyName));
				return false;		
			}		
		break;
		case 'file':
		// Check for a non-blank value
			if (isBlank(oFormElement.value))
			{
				displayErrorMessage(createErrorMessage(nID, sFriendlyName));
				return false;
			}
		break;	
		default:
			break;
	}

	// Checking for radion buttons, checkboxes and selects
	// currently, checking only for radio buttons
	if (oFormElement.length != null) 
	{
		if (oFormElement[0] && oFormElement[0].type == 'radio') 
		{
			for (var i=0; i < oFormElement.length; i++)
			{	
				if (oFormElement[i].checked == true)
					return true;
			}
			displayErrorMessage(createErrorMessage(nID, sFriendlyName));
			oFormElement[0].focus();
			return false;
		}
	}

	return true;
}

