var acceptStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_- ";
var numStr = "1234567890.";
var intStr = "1234567890";
var nameStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'-.,` ";
var alphaStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var alphanumericStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

function formError( f, msg, elName )
{
	alert( msg );
	if ( f.elements[elName].length > 1 )
		f.elements[elName][0].focus();
	else
		f.elements[elName].focus();		
	return false;		
}

function isAlphaNumericNoDash( str )
{
	var i;
	var result = true;
	for ( i=0 ; i<str.length ; i++ )
		if ( alphanumericStr.indexOf( str.charAt( i ) ) == -1 )
			result = false;
	return result;	
}

function isAlphaNumeric( str )
{
	var i;
	var result = true;
	for ( i=0 ; i<str.length ; i++ )
		if ( acceptStr.indexOf( str.charAt( i ) ) == -1 )
			result = false;
	return result;	
}

function isAlphabetic( str )
{
	var i;
	var result = true;
	for ( i=0 ; i<str.length ; i++ )
		if ( alphaStr.indexOf( str.charAt( i ) ) == -1 )
			result = false;
	return result;	
}

function isNumber( str )
{
	var i;
	//if ( str.charAt(0) == "-" )
		//str = str.substr(1);
	for ( i=0; i<str.length; i++ )
		if ( isNaN(parseInt(str.charAt(i))) )
			return false;
	return true;
}

function isNumeric( str )
{
	var i;
	var i;
	var result = true;
	var dotCount = 0;
	for ( i=0 ; i<str.length ; i++ )
	{
		if ( numStr.indexOf( str.charAt( i ) ) == -1 )
			result = false;
		if ( str.charAt( i ) == '.' )
			dotCount++; 
	}
	if ( dotCount > 1 )
		result = false;
	return result;		
}

function isInteger( str )
{
	var i;
	var i;
	var result = true;
	var dotCount = 0;
	for ( i=0 ; i<str.length ; i++ )
	{
		if ( intStr.indexOf( str.charAt( i ) ) == -1 )
			result = false;
	}
	return result;		
}

// Check whether string s is empty.
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or whitespace characters only.
function isWhitespace(s)
{   
	var i;
	var whitespace = " \t\n\r";
	if (isEmpty(s)) return true;
	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	// All characters are whitespace.
	return true;
}

// Email address must be of form a@b.c 
// Rules are:
// - there must be at least one character before the @
// - there must be at least one character before and after the .
// - the characters @ and . are both required
function isEmail(str)
{   
	if (isEmpty(str)) return false;
	if (isWhitespace(str)) return false;
	// there must be >= 1 character before @, so we
	// start looking at character position 1 (i.e. second character)
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
	    return false
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	}
	
	if (str.indexOf(dot,(lat+2))==-1){
	    return false
	}
	
	if (str.indexOf(" ")!=-1){
	    return false
	}
	
	return true					
}

function isPassword(strPassword, strUserid)
{
	var i;
	var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@$^*()-_+={}[]:;<>,.";
	var alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var numbers = "1234567890";
	var specialChars = "!@$^*()-_+={}[]:;<>,.";
	var alphabetsFound = false;
	var numbersFound = false;
	var specialCharsFound = false;

	// Make sure the password does not contain the UserID.
	if ((strPassword.toLowerCase()).indexOf((strUserid.toLowerCase())) >= 0)
	{
		alert("Password cannot contain the UserID.");
		return false;
	}
	
	// Check for length of characters in the password.
	if (strPassword.length < 7 || strPassword.length > 50)
	{
		alert("Please enter between 7 and 50 characters for the password.");
		return false;
	} 
	
	// Check each character against the valid characters.  If a char is not found it is invalid.
	for (i = 0; i < strPassword.length; i++)
	{
		var c = strPassword.charAt(i);
		if (validChars.indexOf(c) == -1)
		{ 
			alert("Password cannot contain character: " + c);
			return false;
		}
	}
	
	// Make sure the password is comprised of letters and at least one number or one special charecter.
	for (i = 0; i < strPassword.length; i++)
	{
		var c = strPassword.charAt(i);
		if (alphabets.indexOf(c) >= 0)
		{
			alphabetsFound = true;
			break;
		}
	}
	
	for (i = 0; i < strPassword.length; i++)
	{
		var c = strPassword.charAt(i);
		if (numbers.indexOf(c) >= 0)
		{
			numbersFound = true;
			break;
		}
	}
	
	for (i = 0; i < strPassword.length; i++)
	{
		var c = strPassword.charAt(i);
		if (specialChars.indexOf(c) >= 0)
		{
			specialCharsFound = true;
			break;
		}
	}
	
	if (!(alphabetsFound == true && (numbersFound == true || specialCharsFound == true)))
	{
		alert("Password must be comprised of letters and at least one number or one special character.");
		return false;
	}
	
	return true;
}

// Returns true if string s is a valid Year number.  Must be 4 digits.
function IsYear(s)
{   
	var i;
	var validChars = "1234567890";
	
    // Verify that year is all digits
	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (validChars.indexOf(c) == -1) return false;
	}
	
	return (s.length == 4);
}

function isDate( str )
{
	var inputStr = str;
	var delim1;
	var delim2;
	var mm;
	var dd;
	var yyyy;	
	var numdelim;
	var validcharresult = true;
	
	// First check for allowed numbers and characters
	for ( i=0 ; i<str.length ; i++ )
		if ( dateStr.indexOf( str.charAt( i ) ) == -1 )
			validcharresult = false;
	if (! validcharresult) {
		alert("The date entry is not in an acceptable format.\nPlease enter dates in the following format:  mm/dd/yyyy.");
		return false;
	}
			
	// Check if there are more than two delimiters
	numdelim = 0;
	for ( i=0 ; i<str.length ; i++ )
		if ( "/".indexOf( str.charAt( i ) ) > -1 )
			numdelim++;
			
	if (numdelim > 2) {
		// There are more than two delimiters
		alert("The date entry is not in an acceptable format.\nPlease enter dates in the following format:  mm/dd/yyyy.");
		return false;
	}

	delim1 = inputStr.indexOf("/");
	delim2 = inputStr.lastIndexOf("/");

	if (delim1 != -1 && delim1 == delim2) {
		// There is only one delimiter in the string
		alert("The date entry is not in an acceptable format.\nPlease enter dates in the following format:  mm/dd/yyyy.");
		return false;
	}

	if (delim1 != -1) {
		// There are delimiters - extract month, day, and year component values and discard delimiters.
		mm = parseInt(inputStr.substring(0, delim1), 10);
		dd = parseInt(inputStr.substring(delim1 + 1, delim2), 10);
		yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length), 10);
	}
	else {
		// There are no delimiters.
		alert("The date entry is not in an acceptable format.\nPlease enter dates in the following format:  mm/dd/yyyy.");
		return false;		
	}

	if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
		// There is a non-numeric character in one of the component values.
		alert("The date entry is not in an acceptable format.\nPlease enter dates in the following format:  mm/dd/yyyy.");
		return false;
	}
	
	if (mm < 1 || mm > 12) {
		// Month value is not 1 thru 12
		alert("Months must be entered between the range of 01 (January) and 12 (December).");
		return false;
	}
	
	if (dd < 1 || dd > 31) {
		// Date value is not 1 thru 31
		alert("Days must be entered between the range of 01 and a maximum of 31 (depending on the month and year).");
		return false;
	}
	
	if (yyyy < 100) {
		// Year value is two digits
		alert("The date entry is not in an acceptable format.\nPlease enter dates in the following format:  mm/dd/yyyy.");
		return false;
	}
	
	if (!checkMonthLength(mm, dd)) {
		return false;
	}
	if (mm == 2) {
		if (!checkLeapMonth(mm, dd, yyyy)) {
			return false;
		}
	}
	
	return true;
}

function logout()
{
	document.cookie = "msd=0";
	this.location.href="msdlogin.php";
}

function URLencode(sStr) {
    return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}