function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
			
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
			
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
			
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
			
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
		   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
		  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
			
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
		  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
			
	return result;
}
function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}
function validExpDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 2); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
 			var year = parseInt(elems[1],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/YY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}
// *****************************************************************************
// Name:  LeaveApplication
// Description: Exit Application routine, confirms user desire to exit the application
// Parameters: None
// Return: None
// Last Updated: 03/08/00
// *****************************************************************************
function LeaveApplication()
{
	if (confirm("Are you sure you want to exit the application?")) 
	{
		window.close()
	}
	
	return false;
}
// *****************************************************************************
// Name:  Strip
// Description: Removes any value in a string of characters not between 0 & 9
// Parameters: pszInput = any ASCII characters
// Return: a whole number as a string with no formating
// Last Updated: 03/08/00
// *****************************************************************************
function Strip(pszInput) 
{
	var intPCount = 0;
	var strDigit = "";
	var intInputlength = pszInput.length;
	for (i = 0; i < intInputlength; i++) 
	{
		var strCharacter = pszInput.charAt(i);
		if ((strCharacter >= '0' && strCharacter <= '9')) 
		{
			strDigit += strCharacter;
		}
	}
	return strDigit;
}
// *****************************************************************************
// Name:  Convertdec
// Description: Converts the input string to an acceptable dollar amount with 
// allowable decimal places, primarily by calling other functions.
// Parameters: pszInput = any ASCII characters input into the calling field by the user
// Return: a number as a string in a US currency format with commas
// Last Updated: 03/08/00
// *****************************************************************************
function Convertdec(pszInput) 
{
	var intNumberLength = pszInput.length;
	var intLoc
	var intEnd
	var strString
	var arynumber
	var strNumber0
	var strNumber1
	var szOutput
	var strDigit = ""; // the reassembled input number after checking for # of periods
	var intPCount = 0; // number of periods
	for (i = 0; i < intNumberLength; i++) 
	{
		var strcharacter = pszInput.charAt(i);
		if (strcharacter == '.') 
		{	
			intPCount += 1;
			
			if (intPCount >= 2) // if there are >= periods the user will be requested to input a valid amount
			{
				alert("Please enter a valid dollar amount.")
				return ""
			}
		}
	}
	if (intPCount > 0)
	{
		intLoc = pszInput.indexOf(".");
		intEnd = pszInput.length;
		strNumber0 = pszInput.substring(0,intLoc);
		strNumber1 = pszInput.substring(intLoc,intEnd);
		
		if (strNumber1.length >= 2)
		{
			strNumber0 = Strip(strNumber0);
			strNumber1 = Strip(strNumber1);
			strNumber1 = strNumber1.substring(0,2);
			strNumber0 = MakeInteger(strNumber0);
			if (strNumber0 == 0)
			{
				strNumber0 = "";
				return strNumber0;
			}
			strNumber0 = Comma(strNumber0);
			if (strNumber1.length >= 2)
			{
				arynumber = (strNumber0 + "."	+ strNumber1);
				return arynumber;				
			}
			return strNumber0;
		}	
		else if (strNumber1.length == 1)
		{
			strNumber0 = Strip(strNumber0);
			strNumber0 = MakeInteger(strNumber0);
			if (strNumber0 == 0)
			{
				strNumber0 = "";
				return strNumber0;
			}
			strNumber0 = Comma(strNumber0);
			return strNumber0;
		}	
	}
	szOutput = Strip(pszInput);
	szOutput = MakeInteger(szOutput);
	if (szOutput == 0)
	{
		szOutput = "";
		return szOutput;
	}
	szOutput = Comma(szOutput);
	return szOutput;
}
// *****************************************************************************
// Name:  convert
// Description: Converts the input string to an acceptable dollar amount 
// primarily by calling other functions.
// Parameters: pszInput = any ASCII characters input into the calling field by the user
// Return: a number as a string in a US currency format with commas
// Last Updated: 03/08/00
// *****************************************************************************
function convert(pszInput) 
{
	var intNumberLength = pszInput.length;
	var intLoc
	var strNumber0
	var strNumber1
	var strDigit = ""; // the reassembled input number after checking for # of periods
	var intPCount = 0; // number of periods
	for (i = 0; i < intNumberLength; i++) 
	{
		var strCharacter = pszInput.charAt(i);
		if (strCharacter == '.') 
		{	
			intPCount += 1;
			if (intPCount >= 2) // if there are >= periods the user will be requested to input a valid amount
			{
				alert("Please enter a valid dollar amount.")
				return "";
			}
		}
	}
	if (intPCount > 0) // if there are periods
	{
		intLoc = pszInput.indexOf(".");
		strNumber0 = pszInput.substring(0,intLoc);
		strNumber0 = Strip(strNumber0);
		strNumber0 = MakeInteger(strNumber0);
		if (strNumber0 == 0)
		{
			strNumber0 = "";
			return strNumber0
		}
		strNumber0 = Comma(strNumber0);
		return strNumber0;		
	}
	szOutput = Strip(pszInput);
	szOutput = MakeInteger(szOutput);
	if (szOutput == 0)
	{
		szOutput = "";
		return szOutput
	}
	szOutput = Comma(szOutput);
	return szOutput;
}
// *****************************************************************************
// Name:  MakeInteger
// Description: Follows previous strip, takes numbers as strings & convers to integer
// Parameters: pszInput = string as a number
// Return: a whole number as a string with no formating. Also removes zeros from
// the begining of a number
// Last Updated: 03/08/00
// *****************************************************************************
function MakeInteger(pszInput) 
{
	var szInput
	var szOutput
	var intLength = pszInput.length
	szInput = pszInput;
	if (intLength >= 1)
	{
		szOutput = (szInput * 1);
		return szOutput;
	}
	szOutput = "";
	return szOutput;
}
// *****************************************************************************
// Name:  Comma
// Description: Inserts commas for a US$ format.
// Parameters: pszInput = a string to have commas inserted
// Return: a whole number as a string with commas inserted after every third 
//	character, from right to left, where a fourth exists
// Last Updated: 03/08/00
// *****************************************************************************
function Comma(pszInput) 
{
	var szOutput
	pszInput = '' + pszInput;
	if (pszInput.length > 3)
	{
		var mod = pszInput.length % 3;
		var output = (mod > 0 ? (pszInput.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(pszInput.length / 3); i++) 
		{
			if ((mod == 0) && (i == 0))
			{
				output += pszInput.substring(mod+ 3 * i, mod + 3 * i + 3);
			}
			else
			{
				output+= ',' + pszInput.substring(mod + 3 * i, mod + 3 * i + 3);
			}
		}
		return output;
	}
	else
		return pszInput;
}
// *****************************************************************************
// Name:  alphaconvert
// Description: Strips out undesireable characters
// Parameters: pszInput = string to have characters which may later cause errors
// in future, or even current, applications removed
// Return: A string of letters, numbers & some characters
// Last Updated: 03/08/00
// *****************************************************************************
function alphaconvert(pszInput) 
{
	var intInputlength = pszInput.length;
	var strDigit = "";
	var badchars = "~!$%^*+_{}|<>[]"; 
	for (i = 0; i < intInputlength; i++) 
	{
		var strCharacter = pszInput.charAt(i);
		if (strCharacter == '"') 
		{
			strDigit += "'"
		}
		else
		{
			if (badchars.indexOf(strCharacter) == -1)
			{
				strDigit += strCharacter;
			}
		} 
	}
	return strDigit;
}
