// *****************************************************************
// *
// * Name: emailCheck
// *
// * Purpose: Validate Email Address
// *
// * Inputs: Email Address
// * 
// * Outputs: True or False
// *
// *
// *****************************************************************

function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address appears to be incorrect (for example, myemail@xyz.com)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("Email address appears to be incorrect (for example, myemail@xyz.com)")
    //alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
		alert("Email address appears to be incorrect (for example, myemail@xyz.com)")
	        //alert("Destination IP address is invalid.")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Email address appears to be incorrect (for example, myemail@xyz.com)")
	//alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   //alert("Your e-mail adress should end in a two- or three-letter domain or country (for example, (myemail@xyz.com or myemail@xyz.ca)")
     alert("Email address appears to be incorrect (for example, myemail@xyz.com)")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname."
   //alert(errStr)
	alert("Email address appears to be incorrect (for example, myemail@xyz.com)")
   return false
}

// If we've gotten this far, everything's valid!
return true;
} // End Function

// *****************************************************************
// *
// * Name: LeapYear
// *
// * Purpose: Determine if year is a leap year
// *
// * Inputs: 4 digit year (2000)
// * 
// * Outputs: True or False
// *
// *****************************************************************
function LeapYear(year) {
	return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;

} // End Function

// *****************************************************************
// *
// * Name: NumberOfDays
// *
// * Purpose: Returns the number of days for a given month
// *
// * Inputs: month - month entered
// *		 year - year entered
// *
// * Outputs: days in a Month
// *
// *****************************************************************
function NumberOfDays(month, year) {

	var days = new Array(12);
	
	days[0] = 31;
	days[1] = 28;
	days[2] = 31;
	days[3] = 30;
	days[4] = 31;
	days[5] = 30;
	days[6] = 31;
	days[7] = 31;
	days[8] = 30;
	days[9] = 31;
	days[10] = 30;
	days[11] = 31;
		
	if ((LeapYear(year)) && (month == 2)) {
		return days[--month] += 1;
	} else {
		return days[--month];	
	} // End IF
	
} // End Function

// *****************************************************************
// *
// * Name: SetFocus
// *
// * Purpose: Looks for the first field and sets the focus to it
// *          
// * Inputs: none
// * 
// * Outputs: nothing
// *
// *****************************************************************
function SetFocus() {

document.queryCommandValue 
var i;
	if(document.forms.length > 0) {
		for(i=0; i < document.forms[0].elements.length; i++) {
			if(document.forms[0].elements[i].type != "hidden") {
				document.forms[0].elements[i].focus();
				break;
			} // End IF
		} // End For
	} // End IF
} // End Function

// *****************************************************************
// *
// * Name: ValidateNumeric
// *
// * Purpose: Determine if a field contains only numeric values
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateNumeric(field) {
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1")
			ok = "no";
	} // End For
	if (ok == "no") {
		alert("Please enter numeric values only.");
		field.focus();
		field.select();
		return (false);
	} else {
		return (true);
	} // End IF
} // End Function

// *****************************************************************
// *
// * Name: ValidateNumericNoMessage
// *
// * Purpose: Determine if a field contains only numeric values 
// *			*note: this method is identical to ValidateNumeric
// *					but does not output any error messages
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateNumericNoMessage(field) {
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1")
			ok = "no";
	} // End For
	if (ok == "no") {
		return (false);
	} else {
		return (true);
	} // End IF
} // End Function

// *****************************************************************
// *
// * Name: ValidatePassword
// *
// * Purpose: Check to see if a password matches the requirements
// *
// * Inputs: fieldPassword - the password field name on the page to validate
// *		 fieldUsername - the username field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidatePassword(fieldPassword, fieldUsername) {
// Validate Password		

		if ((fieldPassword.value.length < 7) || (fieldPassword.value.length > 12)) {
			alert("The new Password must be between 7 and 12 characters in length.");
			fieldPassword.focus();
			fieldPassword.select();
			return (false);
		} // End IF

		if (fieldPassword.value.toLowerCase() == fieldUsername.value.toLowerCase()) {
			alert("The UserID and new Password cannot be the same.");
			fieldPassword.value = '';
			fieldPassword.focus();
			fieldPassword.select();
			return (false);
		} // End IF

		if (ValidateTypeMix(fieldPassword.value) == false) {
			alert("Your new Password must contain both letters and numbers (e.g. password1).");
			fieldPassword.value = '';
			fieldPassword.focus();
			fieldPassword.select();				
			return (false);
		} // End If		
	
		if (ValidateChars(fieldPassword)== false) {
			return (false);
		} // End If		

		return (true);
}

// *****************************************************************
// *
// * Name: ValidateTypeMix
// *
// * Purpose: Check to see if a string contains atleast 1 alpha and 1 numeric character
// *
// * Inputs: aString - the string to parse
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateTypeMix(aString) {
	var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'";
	var numericChars = "0123456789'";
	var okAlpha = false;
	var okNumeric = false;
	var temp;
	
	// Alpha Check
	for (var i=0; i<aString.length; i++) {
		temp = "" + aString.substring(i, i+1);
		if (alphaChars.indexOf(temp) != "-1")
		{
			okAlpha = true;
			break;
		}
	} // End For
	
	// Numeric Check
	for (var i=0; i<aString.length; i++) {
		temp = "" + aString.substring(i, i+1);
		if (numericChars.indexOf(temp) != "-1")
		{
			okNumeric = true;
			break;
		}
	} // End For
	
	return (okAlpha && okNumeric);
}

// *****************************************************************
// *
// * Name: ValidatePhoneNumber
// *
// * Purpose: Determine if a phone number field is valid
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidatePhoneNumber(field) {
	if (! ValidateNumericNoMessage(field)) {
		return (false);
	}
	else if (field.value.length != 10) {
		return (false);
	}
	return (true);
} // End Function

// *****************************************************************
// *
// * Name: ValidateChars
// *
// * Purpose: Determine if a field contains only numeric values
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateChars(field) {
	var valid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()-_=+[{]}\\|;:\,<.>/?"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1")
			ok = "no";
	} // End For
	if (ok == "no") {
		alert("Please enter valid characters only (no spaces or quotes).");
		field.focus();
		field.select();
		return (false);
	} else {
		return (true);
	} // End IF
} // End Function

// *****************************************************************
// *
// * Name: ValidateAlpha
// *
// * Purpose: Determine if a field contains only alpha characters
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateAlpha(field) {
	var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' -";
	var ok = "yes";
	var temp;
	
	// Alpha Check
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (alphaChars.indexOf(temp) == "-1")
			ok = "no";
	} // End For
	
	if (ok == "no") {
		return (false);
	} else {
		return (true);
	} // End IF
	
} // End Function

// *****************************************************************
// *
// * Name: ValidateAlphaNumericProper
// *
// * Purpose: Determine if a field contains alpha and numeric properly
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateAlphaNumericProper(field) {

	var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?0123456789 ";
	var ok = "yes";
	var temp;
	
	// Numeric Check
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (alphaChars.indexOf(temp) == "-1")
			ok = "no";
	} // End For
	
	if (ok == "no") {
		return (false);
	} else {
		return (true);
	} // End IF
	
} // End Function

// *****************************************************************
// *
// * Name: ValidateAlphaNumeric
// *
// * Purpose: Determine if a field contains alpha and numeric
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateAlphaNumeric(field) {

	var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
	var ok = "yes";
	var temp;
	
	// Numeric Check
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (alphaChars.indexOf(temp) == "-1")
			ok = "no";
	} // End For
	
	if (ok == "no") {
		return (true);
	} else {
		return (false);
	} // End IF
	
} // End Function

// *****************************************************************
// *
// * Name: ValidateFirstAlpha
// *
// * Purpose: Determine if a field contains a first pos alpha
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateFirstAlpha(field) {
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var ok = "yes";
	var temp;
	
	temp = "" + field.value.substring(0, 1);
	if (valid.indexOf(temp) == "-1")
		ok = "no";
		
	if (ok == "no") {
		alert("First character must be a letter of the alphabet.");
		field.focus();
		field.select();
		return (false);
	} else {
		return (true);
	} // End IF
} // End Function

// *****************************************************************
// *
// * Name: ValidateNotGenericID
// *
// * Purpose: Determine if a field contains a "GE" as the first 2 chars
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateNotGenericID(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
	var ok = "yes";
	var temp;
	
	temp = "" + field.value.substring(0, 2);
	if ((temp == "GE") || (temp == "ge") || (temp == "Ge") || (temp == "gE")) {
		temp = "" + field.value.substring(2, 3);	
		if (valid.indexOf(temp) == "-1") {
			ok = "no";
		} // End IF
	} // End IF	
	
	if (ok == "no") {
		alert("Please change the third character of your User ID to a letter.");
		field.focus();
		field.select();
		return (false);
	} else {
		return (true);
	} // End IF
} // End Function



// *****************************************************************
// *
// * Name: ValidateGenericID
// *
// * Purpose: Determine if a field contains a "GE" as the first 2 chars
// *
// * Inputs: field - the field name on the page to validate
// * 
// * Outputs: True or False
// *
// *****************************************************************
function ValidateGenericID(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
	var ok = "yes";
	var temp;
	
	temp = "" + field.value.substring(0, 2);
	if ((temp == "GE") || (temp == "ge") || (temp == "Ge") || (temp == "gE")) {
		temp = "" + field.value.substring(2, 3);	
		if (valid.indexOf(temp) == "-1") {
			ok = "no";
		} // End IF
	} // End IF	
	
	if (ok == "no") {
		alert("You are not authorized to change the password associated with this User ID.");
		field.focus();
		field.select();
		return (false);
	} else {
		return (true);
	} // End IF
} // End Function



