//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///																											               ///
///				General Data Validation Library (Netscape Compliant Version) - Version 1.0 (1999)			               ///
///																											               ///
///		Interface :																							               ///
///		-----------																							               ///
///																											               ///
///		1)	NonBlank(item, SetFocusFlag, Caption)																		   ///
///		2)  ValidEmail(item, SetFocusFlag, Caption)                                                                        ///
///		3)  ValidNumber(item, DisplayFlag, Format, LowerLimit, UpperLimit, Exception1, Exception2, SetFocusFlag, Caption)  ///
///		4)  ValidDate(item, DisplayFlag, Format, LowerLimit, UpperLimit, Exception1, Exception2, SetFocusFlag, Caption)	   ///
///																											               ///
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function NonBlank(item, SetFocusFlag, Caption)
{
	if (Caption!=null){
		var strErrorMsg = Caption + " must have a non-blank value";
	}
	else {
		var strErrorMsg = item.name + " must have a non-blank value";
	}
	if (item.type == 'text' || item.type == 'password' || item.type == 'textarea'){
		if (item.value.length==0)
		{
			if (SetFocusFlag == true) {
				item.focus();
			}
			alert(strErrorMsg);
			return false;
		}
	}
	else
		if (item.options[item.selectedIndex].value.length==0)
		{
			if (SetFocusFlag == true) {
				item.focus();
			}
			alert(strErrorMsg);
			return false;
		}
	return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function ValidEmail(item, SetFocusFlag, Caption)
{
	if (Caption!=null){
		var strErrorMsg = Caption + " must have a valid format";
	}
	else {
		var strErrorMsg = item.name + " must have a valid format";
	}

	if (!(/^[^@]+@[^@.]+.+[^@]*[^@.]$/.test(item.value)) || (item.value.search(String.fromCharCode(34)) != -1))
	{
		if ((SetFocusFlag == true) || (SetFocusFlag == null)) {
			item.focus();
		}
		alert(strErrorMsg);
		return false;
	}

	return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function ValidNumber(item, DisplayFlag, Format, LowerLimit, UpperLimit, Exception1, Exception2, SetFocusFlag, Caption)
{
	var value = item.value;
	if (item.value.length==0)
	{
		return(true);			// Blank is always valid!
	}

	LowerLimit = "" + LowerLimit;
	UpperLimit = "" + UpperLimit;
	Exception1 = "" + Exception1;
	Exception2 = "" + Exception2;

	var	ErrorValue = DoValidation(value, Format, LowerLimit, UpperLimit, Exception1, Exception2);

	if (!ErrorValue)		// No Error
	{
		return (true);
	}
	else if ( (DisplayFlag == false) || (DisplayFlag == null))
	{
		return (false);
	}
	else
	{
		if (SetFocusFlag == true) {
			item.focus();
		}
		switch (ErrorValue)
		{
			case 1:			// Wrong format
				if (Caption!=null){
					alert(Caption + " must be a valid Format");
				}
				else {
					alert(item.name + " must be a valid Format");
				}
				break;
			case 2:			// Wrong value
				if (Caption!=null){
					alert(Caption + " must have a valid value");
				}
				else {
					alert(item.name + " must have a valid value");
				}
				break;
			case 3:			// Invalid parameters
				if (Caption!=null){
					alert(Caption + " Check called with Invalid Parameters");
				}
				else {
					alert(item.name + " Check called with Invalid Parameters");
				}
				break;
			default:
				alert ("Error!");
				break;
		}
		return (false);
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function DoValidation(value, Format, LowerLimit, UpperLimit, Exception1, Exception2)
// returns (0) if valid
// Otherwise returns the error code
// 1 = Invalid Format
// 2 = Invalid value
// 3 = Invalid Limits
{
	if (!ValidateNumberFormat(value, Format))
	{
		return (1);				// Invalid Format
	}
	value = StandardNumericFormat(value, Format);
	if ( (LowerLimit != "undefined") && (LowerLimit != "null") && (LowerLimit != "") )
	{
		if  (!ValidateNumberFormat(LowerLimit, Format))
		{
			return (3);				// Invalid Limits Format
		}
		LowerLimit = StandardNumericFormat(LowerLimit, Format);
		if (value < LowerLimit)
		{
			return (2);				// Invalid value
		}
	}
	if ( (UpperLimit != "undefined") && (UpperLimit != "null") && (UpperLimit != "") )
	{
		if  (!ValidateNumberFormat(UpperLimit, Format))
		{
			return (3);				// Invalid Limits Format
		}
		UpperLimit = StandardNumericFormat(UpperLimit, Format);
		if (value > UpperLimit)
		{
			return (2);				// Invalid value
		}
	}
	if ( (Exception1 != "undefined") && (Exception1 != "null") && (Exception1 != "") )
	{
		if  (!ValidateNumberFormat(Exception1, Format))
		{
			return (3);				// Invalid Limits Format
		}
		Exception1 = StandardNumericFormat(Exception1, Format);
		if (value == Exception1)
		{
			return (2);				// Invalid value
		}
	}
	if ( (Exception2 != "undefined") && (Exception2 != "null") && (Exception2 != "") )
	{
		if  (!ValidateNumberFormat(Exception2, Format))
		{
			return (3);				// Invalid Limits Format
		}
		Exception2 = StandardNumericFormat(Exception2, Format);
		if (value == Exception2)
		{
			return (2);				// Invalid value
		}
	}
	return(0);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function ValidateNumberFormat(value, Format)
{
	var StValue=value;

	if (typeof(value) == "number")
	{
		StValue = "" + value;
	}

	if (typeof(value) != "string")
	{
		return (false);			// Wrong Format
	}

	if (StValue.length == 0)
		return (true);			// Valid Field

	var ValidChars;

	switch (Format)
	{
		case 1:					// Numeric
			ValidChars = "-.0123456789Ee";
			break;
		case 2:					// Integer
			ValidChars = "-0123456789Ee";
			break;
		case 3:					// Numeric With Commas
			ValidChars = "-,.0123456789Ee";
			break;
		case 4:					// Integer With Commas
			ValidChars = "-,0123456789Ee";
			break;
		default:				// Default Numeric
			ValidChars = "-.0123456789Ee";
			break;
	}
	for (var intLoop = 0; intLoop < StValue.length; intLoop++)
	{
		if (ValidChars.indexOf(StValue.charAt(intLoop)) == -1)
		{
			return (false);			// Wrong Format
		}
	}
	if (StValue.indexOf(".")!=StValue.lastIndexOf("."))
	{
		return (false);			// Wrong Format
	}
	if (StValue.indexOf("E")!=StValue.lastIndexOf("E"))
	{
		return (false);			// Wrong Format
	}
	if (StValue.indexOf("e")!=StValue.lastIndexOf("e"))
	{
		return (false);			// Wrong Format
	}
	if ( (StValue.indexOf("e")!= -1) && (StValue.lastIndexOf("E") != -1) )
	{
		return (false);			// Wrong Format
	}
	if ( (StValue.indexOf("e") == 0) || (StValue.lastIndexOf("E") == 0) )
	{
		return (false);			// Wrong Format
	}
	if ( (StValue.indexOf("e") == StValue.length) || (StValue.lastIndexOf("E") == StValue.length) )
	{
		return (false);			// Wrong Format
	}
	if ( (StValue.indexOf("-")!= -1) )
	{
		if ( (StValue.indexOf("-")!=StValue.lastIndexOf("-")) ||
			 (StValue.indexOf("-")!= 0) )
		{
			return (false);			// Wrong Format
		}
	}
	return (true);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function StandardNumericFormat(value, Format)		// Assumes a valid format already been checked
{
var s;
var i;
var StValue;
var Commas = new RegExp(",");

	StValue = "" + value;
	switch (Format)
	{
		case 1:					// Numeric
			value = parseFloat(value);
			return (value);
			break;
		case 2:					// Integer
			value = parseInt(value);
			return (value);
			break;
		case 3:					// Numeric With Commas
			s = StValue.split(Commas);
			StValue = "";
			for (i=0; (s[i] != null); i++)
			{
				StValue += s[i];
			}
			value = parseFloat(StValue);
			return (value);
			break;
		case 4:					// Integer With Commas
			s = StValue.split(Commas);
			StValue = "";
			for (i=0; (s[i] != null); i++)
			{
				StValue += s[i];
			}
			value = parseInt(StValue);
			return (value);
			break;
		default:				// Default Numeric
			return (value);
			break;
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function ValidDate(item, DisplayFlag, Format, LowerLimit, UpperLimit, Exception1, Exception2, SetFocusFlag, Caption)
{
	var value = item.value;
	if (item.value.length==0)
	{
		return(true);			// Blank is always valid!
	}
	
	LowerLimit = "" + LowerLimit;
	UpperLimit = "" + UpperLimit;
	Exception1 = "" + Exception1;
	Exception2 = "" + Exception2;

	var	ErrorValue = DoDateValidation(value, Format, LowerLimit, UpperLimit, Exception1, Exception2);

	if (!ErrorValue)		// No Error
	{
		return (true);
	}
	else if ( (DisplayFlag == false) || (DisplayFlag == null))
	{
		return (false);
	}
	else
	{
		if (SetFocusFlag == true) {
			item.focus();
		}
		switch (ErrorValue)
		{
			case 1:			// Wrong format
				if (Caption!=null){
					alert(Caption + " must be a valid Format");
				}
				else {
					alert(item.name + " must be a valid Format");
				}
				break;
			case 2:			// Wrong value
				if (Caption!=null){
					alert(Caption + " must have a valid value");
				}
				else {
					alert(item.name + " must have a valid value");
				}
				break;
			case 3:			// Invalid parameters
				if (Caption!=null){
					alert(Caption + " Check called with Invalid Parameters");
				}
				else {
					alert(item.name + " Check called with Invalid Parameters");
				}
				break;
			default:
				alert ("Error!");
				break;
		}
		return (false);
	}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function DoDateValidation(value, Format, LowerLimit, UpperLimit, Exception1, Exception2)
// returns (0) if valid
// Otherwise returns the error code
// 1 = Invalid Format
// 2 = Invalid value
// 3 = Invalid Limits
{
	if (!ValidateDateFormat(value, Format))
	{
		return (1);				// Invalid Format
	}
	value = StandardDateFormat(value, Format);

	if ( (LowerLimit != "undefined") && (LowerLimit != "null") && (LowerLimit != "") )
	{
		if  (!ValidateDateFormat(LowerLimit, Format))
		{
			return (3);				// Invalid Limits Format
		}
		LowerLimit = StandardDateFormat(LowerLimit, Format);
		if (Date.parse(value) < Date.parse(LowerLimit))
		{
			return (2);				// Invalid value
		}
	}
	if ( (UpperLimit != "undefined") && (UpperLimit != "null") && (UpperLimit != "") )
	{
		if  (!ValidateDateFormat(UpperLimit, Format))
		{
			return (3);				// Invalid Limits Format
		}
		UpperLimit = StandardDateFormat(UpperLimit, Format);
		if (Date.parse(value) > Date.parse(UpperLimit))
		{
			return (2);				// Invalid value
		}
	}
	if ( (Exception1 != "undefined") && (Exception1 != "null") && (Exception1 != "") )
	{
		if  (!ValidateDateFormat(Exception1, Format))
		{
			return (3);				// Invalid Limits Format
		}
		Exception1 = StandardDateFormat(Exception1, Format);
		if (Date.parse(value) == Date.parse(Exception1))
		{
			return (2);				// Invalid value
		}
	}
	if ( (Exception2 != "undefined") && (Exception2 != "null") && (Exception2 != "") )
	{
		if  (!ValidateDateFormat(Exception2, Format))
		{
			return (3);				// Invalid Limits Format
		}
		Exception2 = StandardDateFormat(Exception2, Format);
		if (Date.parse(value) == Date.parse(Exception2))
		{
			return (2);				// Invalid value
		}
	}
	return(0);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function ValidateDateFormat(value, Format)
{
	switch (Format)
	{
		case	1:
			return (DateFormat_DMYYYY(value));
			break;
		case	2:
			return (DateFormat_MDYYYY(value));
			break;
		default:
			return (false);
			break;
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function DateFormat_DMYYYY(value)
{
	var DateSeparators = new RegExp("[-\/]");
	var i=0;
	var day, month, year;
	var	s;

	s = value.split(DateSeparators);
	for (i=0; (s[i] != null); i++)
		if (isNaN(s[i]) || i>2)
			return (false);

	if (typeof(s[2]) == "undefined")
	{
		return false;
	}
	if (s[0].length > 2 || s[1].length > 2 || s[2].length != 4)
	{
		return(false);
	}
	day = parseInt(s[0]);
	month = parseInt(s[1]);
	year = parseInt(s[2]);

	if (year < 1000 || year > 9999)
	{
		return(false);
	}

	return (CheckDateValidity(year, month, day));
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function DateFormat_MDYYYY(value)
{
	var DateSeparators = new RegExp("[-\/]");
	var i=0;
	var day, month, year;
	var	s;

	s = value.split(DateSeparators);
	for (i=0; (s[i] != null); i++)
		if (isNaN(s[i]) || i>2)
			return (false);

	if (typeof (s[2]) == "undefined")
	{
		return false;
	}
	if (s[0].length > 2 || s[1].length > 2 || s[2].length != 4)
	{
		return(false);
	}
	day = parseInt(s[1]);
	month = parseInt(s[0]);
	year = parseInt(s[2]);

	if (year < 1000 || year > 9999)
	{
		return(false);
	}

	return (CheckDateValidity(year, month, day));
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function CheckDateValidity(year, month, day)
{
	if ( (year <= 0) || (month <= 0) || (month > 12) || (day <= 0) || (day > 31) )
	{
		return(false);
	}
	if (day == 31)
	{
		if (month == 2 || month == 4 || month == 6 | month == 9 || month == 11)
		{
			return false;
		}
	}
	else if (day == 30 && month == 2)
	{
		return false;
	}
	else if (day == 29 && month == 2)
	{
		if ((year % 400) == 0)
		{
		}
		else if ( ((year % 100) == 0) || ((year % 4) != 0))
		{
			return false;
		}
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function StandardDateFormat(value, Format)
{
// Assumes a valid date...
	switch (Format)
	{
		case 1:					// m/d/yyyy
			var	s, temp;
			var DateSeparators = new RegExp("[-\/]");
			s = value.split(DateSeparators);
			var NewValue = new Date(parseInt(s[2]), parseInt(s[1])-1, parseInt(s[0]));
			return (NewValue);
			break;
		case 2:					// d/y/yyyy
			var	s, temp;
			var DateSeparators = new RegExp("[-\/]");
			s = value.split(DateSeparators);
			var NewValue = new Date(parseInt(s[2]), parseInt(s[0])-1, parseInt(s[1]));
			return (NewValue);
			break;
		default:				// Default Date
			return (value);
			break;
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
