//Gareth Button
//July 2002
//This function validates a given Field according to the parameters supplied
//PARAMETER DESCRIPTIONS : 	[InputField]	- <object id=OBJECT1>  - id of the Input tag
//				[FieldDesc]	- <string>  - Description of the field for the user
//				[CheckEmpty]	- <boolean> - To check if field is empty
//				[CheckSpace]	- <boolean> - To check if first char is a space
//				[CheckChars]	- <boolean> - To check for invalid characters
//				[MinLength]	- <integer> - If greater than 0 then check for min length of field
//-----------------------------------------------------------------------------------------------------------------
function ValidateInputField(InputField, FieldDesc, CheckEmpty, CheckSpace, CheckChars, MinLength)
{
	if (CheckEmpty)
	{
		if (InputField.value == "")
		{
			window.alert("Please enter a value for '" + FieldDesc + "' field.");
			InputField.focus();
			return (false);
		}
	}
	
	if (CheckSpace)
	{		
		if (FirstCharSpace(InputField))
		{
			window.alert("'" + FieldDesc + "' field cannot begin with a space");
			InputField.focus();
			return (false);
		}
	}
	
	if (MinLength > 0)
	{
		if (InputField.value.length < MinLength)
		{
			window.alert("Please enter a valid value, minimum " + MinLength + " characters for '" + FieldDesc + "' field.");
			InputField.focus();
			return (false);
		}
	}
	
	if (CheckChars)
	{
		if (InvalidChars(InputField))
		{
			alert("Please enter only letter and digit characters in '" + FieldDesc + "' field.");
			InputField.focus();
			return (false);
		}
	}
return(true);
}

//-------------------------------------------------------------------------------------------
//This function checks if an input field contains no characters
function FieldIsEmpty(InputField)
{
	if (InputField.value == "")
		return (true);
	else
		return (false);
}

//-------------------------------------------------------------------------------------------
//This function checks if any characters other than the ones in 
//the variable [checkOK] were entered in a input field
function InvalidChars(InputField)
{
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.@ ?_+";
  var checkStr = InputField.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
        
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
  }
    if (allValid)
		return(false);
	else 
	{
		alert("The character '" + ch + "' is not allowed.");
		return(true);
	}
}

//This function checks if a required character is missing in an input field
//Supply a "@" and then a "." for [ReqChar] for proper validation
function InvalidEmail(InputField, ReqChar)
{
  var checkStr = InputField.value;
  var CharMissing = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
	if (ch == ReqChar)
	{
		CharMissing = false;
		break;
	}
	if (i == checkStr.length)
	break;
  }

	if ((checkStr.charAt(0) == ReqChar) || (checkStr.charAt((checkStr.length - 1)) == ReqChar))
	CharMissing = true;

    if (CharMissing)
		return(true);
	else 
		return(false);	
}
//-------------------------------------------------------------------------------------------
//This function checks if the first character of an input field is a space
function FirstCharSpace(InputField)
{
	var strCheck = InputField.value;
	if (strCheck.charAt(0) == " ") 
	{
		return (true); 
	}
	else 
	{
		return (false);
	}
}

//This function checks if an input field is numeric
function IsNumeric(InputField)
{
	if (isNaN(InputField))
	{
		window.alert ("Please enter a numeric value");
		return (false);
	}
	else 
		return (true);
}
//-------------------------------------------------------------------------------------------
function fnTrapKB()
{
	if ((event.keyCode >= 46 && event.keyCode <= 57) || (event.keyCode >= 35 && event.keyCode <= 39) || event.keyCode == 13 || event.keyCode == 8 || event.keyCode == 9 || (event.keyCode >= 96 && event.keyCode <= 105))
		event.returnValue=true;
	else
		event.returnValue=false;
	
	if (event.shiftKey == true)
	{
		if (event.keyCode == 9 || (event.keyCode >= 35 && event.keyCode <= 39))
			event.returnValue=true;
		else
			event.returnValue=false;
	}
}
//-------------------------------------------------------------------------------------------
function fnDisableKey(intKeyCode)
{
	if (event.keyCode == intKeyCode)
		event.returnValue=false;
	else
		event.returnValue=true;
}
//-------------------------------------------------------------------------------------------
function IsValidDate(InputField, blnCheckFutureNA, blnCheckHistNA)
{

     DaysInMonth = new Array()
     DaysInMonth[0] = 31;
     DaysInMonth[1] = 28;
     DaysInMonth[2] = 31;
     DaysInMonth[3] = 30;
     DaysInMonth[4] = 31;
     DaysInMonth[5] = 30;
     DaysInMonth[6] = 31;
     DaysInMonth[7] = 31;
     DaysInMonth[8] = 30;
     DaysInMonth[9] = 31;
     DaysInMonth[10] = 30;
     DaysInMonth[11] = 31;
     
	MonthName = new Array()
	MonthName[0] = "Jan";
	MonthName[1] = "Feb";
	MonthName[2] = "Mar";
	MonthName[3] = "Apr";
	MonthName[4] = "May";
	MonthName[5] = "Jun";
	MonthName[6] = "Jul";
	MonthName[7] = "Aug";
	MonthName[8] = "Sep";
	MonthName[9] = "Oct";
	MonthName[10] = "Nov";
	MonthName[11] = "Dec";
     
	mSecs_In_Day = 86400000;
	
     var checkStr = InputField.value;
     var DateValid = true;
     
     if (checkStr.length < 10)
        return(false);
     
     year  = checkStr.substring(0,4);
     month = checkStr.substring(5,7);
     day   = checkStr.substring(8,10);

     if (isNaN(day) || isNaN(month) || isNaN(year))
		return (false);
		
     monthstr = MonthName[month-1];

     if (month == 2)
		if (year % 100 != 0 || year % 400 == 0)
			if (year % 4 == 0)
				DaysInMonth[1] = 29;

	if (year < 1996 || month < 1 || month > 12 || day < 1 || day > DaysInMonth[month - 1] )
		DateValid = false;
            
     if (!DateValid)
        return (false);
     
     var dtCur = new Date();
     var dtCheck = new Date(day + " " + monthstr + " " + year);
     
     if ( isNaN(dtCheck) )
		return (false);
	 
	 if (blnCheckFutureNA)
	 {		
		if ( parseInt((dtCur - dtCheck) / mSecs_In_Day) < 0 )
			return (false);
     }

	 if (blnCheckHistNA)
	 {		
		if ( parseInt((dtCheck - dtCur) / mSecs_In_Day) < 0 )
			return (false);
     }
     
return (true);
}
//-------------------------------------------------------------------------------------------
//This function checks if a required character is missing in an input field
//Supply a "@" and then a "." for [ReqChar] for proper validation
function InvalidEmail(InputField, ReqChar)
{
  var checkStr = InputField.value;
  var CharMissing = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
	if (ch == ReqChar)
	{
		CharMissing = false;
		break;
	}
	if (i == checkStr.length)
	break;
  }

	if ((checkStr.charAt(0) == ReqChar) || (checkStr.charAt((checkStr.length - 1)) == ReqChar))
	CharMissing = true;

    if (CharMissing)
		return(true);
	else 
		return(false);	
}
function isValidEmail(email) 
{
	//checks if email includes "@" character, "." character
	//checks if the last "." character is atleast 2 positions after the "@" character
	//checks that there is onle one "@" charater
	//checks if there are atleast 2 characters after the last "." character
	//checks if there are no "." characters immdeiately before or after "@" character	
	pos1=email.indexOf("@");
	pos2=email.lastIndexOf(".");
	pos3=email.lastIndexOf("@");
	pos4=email.length-1;
	return ((pos1>=1 && pos2>=0) && (pos1<=pos2-2) && (pos1 == pos3) && (pos2<=email.length-3) && 
			 ("." != email.substring(pos1-1, pos1)) && ("." != email.substring(pos1+1, pos1+2)) );
}
