/* 
	NAME:		GridValidation.js 
	PURPOSE:	All client validation for grid values
	---------------------------------------------------------
	HISTORY
	DATE		INITIALS	COMMENTS
	11-27-2004	KMN			Original Work
	03-25-2004	KMN			Changed validate decimal to include the $
*/

var gOrigValue=null;		// this holds the value of the cell before the user changed anything

// FUNCTION:	ValidateThis
// PURPOSE:		validate the user input against valid values
function ValidateThis(Value, ValidCharachters)
{
	var blnRes=0;
	if ( Value.length==0 ) return 1;
	for (i = 0; i < Value.length && blnRes == 0; i++)
	{
		strCharToCheck = Value.charAt(i);
		if (ValidCharachters.indexOf(strCharToCheck) == -1)
		{
			blnRes = 1;
		}
	}
	return blnRes;
}

// FUNCTION:	ValidateNumeric
// PURPOSE:		validate the user input is Numeric
function ValidateNumeric(val)
{
	return ValidateThis(val, "0123456789,");
}

// FUNCTION:	ValidateAlpha
// PURPOSE:		validate the user input is Alpha
function ValidateAlpha(val)
{
	// convert all to lowercase
	return ValidateThis(val.toLowerCase(), "abcdefghijklmnopqrstuvwxyz ");
}

// FUNCTION:	ValidateAlphaNumeric
// PURPOSE:		validate the user input is Alpha or Numeric
function ValidateAlphaNumeric(val)
{
	return ValidateThis(val.toLowerCase(), "abcdefghijklmnopqrstuvwxyz 0123456789,");
}

// FUNCTION:	ValidateAlphaSpecChars
// PURPOSE:		validate the user input is Alpha or special charachters
function ValidateAlphaSpecChars(val)
{
	return ValidateThis(val.toLowerCase(), "abcdefghijklmnopqrstuvwxyz ,./<>?;':\"[]\\{}|~`!@#$%^&*()_+=-");
}

// FUNCTION:	ValidateNumericSpecChars
// PURPOSE:		validate the user input is Numeric or special characters
function ValidateNumericSpecChars(val)
{	
	return ValidateThis(val.toLowerCase(), "0123456789,. /<>?;':\"[]\\{}|~`!@#$%^&*()_+=-");
}

// FUNCTION:	ValidateDate
// PURPOSE:		validate the user input is a valid date
function ValidateDate(val)
{
	if (!CheckDate(val))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

function ValidateCurrency(val)
{
	return ValidateThis(val, "$0123456789,.");
}

function ValidateDecimalSpecChars(val)
{
	return ValidateThis(val, "0123456789,. /<>?;':\"[]\\{}|~`!@#$%^&*()_+=-");
}

function CheckDate(strMMDDYYYY)
{
try{
	// split the variable up
	var lDateArray=strMMDDYYYY.split("/");
	// make sure it is 3 long
	if ( lDateArray.length!=3 )
	{
		// alert('Invalid Date'); 
		return false;
	}
	else
	{
		var lMonth=lDateArray[0];
		var lDay=lDateArray[1];
		var lYear=lDateArray[2];
		// make sure we have a valid month
		if (lMonth.length<1 || lMonth<1 || lMonth>12)
		{
			//alert("Please enter a valid month");
			return false;
		}
		// make sure we have a valid year
		if (lYear.length<4 || lYear.length>4)
		{
			//alert("Please enter a valid year");
			return false;
		}
		// validate the day of the month
		if (lDay.length<1 || lDay.length>2)
		{
			//alert("Please enter a valid day");
			return false;
		}
		return ValidateDay(lMonth, lDay, lYear)
	}
	}
	catch(er)
	{
		alert(er.description);
	}
	
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    	// EXCEPT for centurial years which are not also divisible by 400.
    	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   	} 
   	return this
}

function ValidateDay(strMonth, strDay, strYear)
{
try{
	// alert("Please enter a valid day")
//	alert('Month-'+strMonth+' Day-'+strDay+' Year-'+strYear);
	var daysInMonth = DaysArray(12);
	
	if (strDay.length<1 || strDay<1 || strDay>31 || (strMonth==2 && strDay>daysInFebruary(strYear)) || strDay > daysInMonth[strMonth])
	{
		// alert("Please enter a valid day");
		return false;
	}
	return true;
	}
catch(er)
	{
		alert(er.description);
	}	
}