/****************************************************************************
** Method:		Validate_Input()
** Parameters:	None
** Returns:		Boolean
**
** Purpose:	This method gets called when things on the page need to get
**		validated. It returns true or false depending on validation.
****************************************************************************/
function Validate_Input()
{		
	var strUsername = Trim_String(document.frmDefault.txtUsername.value);
	var strPassword = Trim_String(document.frmDefault.txtPassword.value);
		
	document.frmDefault.txtUsername.value = strUsername;
	document.frmDefault.txtPassword.value = strPassword;
	
	//Username must not be blank.
	if (strUsername == "")
	{
		alert("Please enter a username.");
		document.frmDefault.txtUsername.focus();
		return false;
	}
	//Password must not be blank.
	else if (strPassword == "")
	{
		alert("Please enter a password.");
		document.frmDefault.txtPassword.focus();
		return false;
	}
	else
	{
		document.frmDefault.hdnAction.value = "L";
		document.frmDefault.submit();
	}
	return true;
}

/****************************************************************************
** Method:		Validate_Password()
** Parameters:	None
** Returns:		Boolean
**
** Purpose:	This method gets called when user is updating password.
****************************************************************************/
function Validate_Password()
{		
	var strOPassword = Trim_String(document.frmDefault.txtOPassword.value);
	var strNPassword = Trim_String(document.frmDefault.txtNPassword.value);
	var strCPassword = Trim_String(document.frmDefault.txtCPassword.value);
		
	document.frmDefault.txtOPassword.value = strOPassword;
	document.frmDefault.txtNPassword.value = strNPassword;
	document.frmDefault.txtCPassword.value = strCPassword;
	
	//Old password must not be blank.
	if (strOPassword == "")
	{
		alert("Please enter an old password.");
		document.frmDefault.txtOPassword.focus();
		return false;
	}
	else if ((strNPassword == "") || (strNPassword.length > 12) || (strNPassword.length < 8))
	{
		alert("Please enter a new password from 8 to 12 characters.");
		document.frmDefault.txtNPassword.focus();
		document.frmDefault.txtNPassword.select();
		return false;
	}
	else if ((strCPassword == "") || (strCPassword.length > 12) || (strCPassword.length < 8))
	{
		alert("Please confirm your password.");
		document.frmDefault.txtCPassword.focus();
		document.frmDefault.txtCPassword.select();
		return false;
	}
	else if (strCPassword != strNPassword)
	{
		alert("Passwords do not match.");
		document.frmDefault.txtNPassword.focus();
		document.frmDefault.txtNPassword.select();
		return false;
	}
	else
	{
		document.frmDefault.hdnAction.value = "U";
		document.frmDefault.submit();
	}
	return true;
}

/****************************************************************************
** Method:		Trim_String()
** Parameters:	String to be trimmed
** Returns:		Trimmed string
**
** Purpose:		Trims the passed in string
****************************************************************************/
function Trim_String(strInput) 
{
	if (typeof strInput != "string") 
	{ 
		return strInput; 
	}
	
	var retValue = strInput;
	var ch = retValue.substring(0, 1);
	
	// Check for spaces at the beginning of the string
	while (ch == " ") 
	{ 
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
	}
   
	ch = retValue.substring(retValue.length-1, retValue.length);
   
	// Check for spaces at the end of the string
	while (ch == " ") 
	{
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
	}
   
	// Note that there are two spaces in the string - look for multiple spaces within the string
	while (retValue.indexOf("  ") != -1) 
	{
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	
	// Return the trimmed string back to the user
	return retValue; 
	
} 

/****************************************************************************
** Method:		Check_Logon_Key()
** Parameters:	None
** Returns:		Boolean
**
** Purpose:		Checks to see what key was pressed for an event.
****************************************************************************/
function Check_Logon_Key()
{
	if (window.event.keyCode == 13)
	{
		Validate_Input();
		return true;
	}
}

/****************************************************************************
** Method:		Check_Change_Key()
** Parameters:	None
** Returns:		Boolean
**
** Purpose:		Checks to see what key was pressed for an event.
****************************************************************************/
function Check_Change_Key()
{
	if (window.event.keyCode == 13)
	{
		Validate_Password();
		return true;
	}
}
