// ****************************************************************************************************
// Shared JavaScript Functions
// ****************************************************************************************************

// ****************************************************************************************************
// Function validateEMail
// ****************************************************************************************************
function validateEMail(emailAddress) {

	var knownDomsPat	= /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;	// List of known TLDs that an e-mail address must end with.
	var emailPat		= /^(.+)@(.+)$/;																// 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 specialChars	= "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";												// Represents the pattern for matching all special characters.  We don't want to allow special characters in the address. These characters include ( ) < > @ , ; : \ " . [ ]
	var validChars		= "\[^\\s" + specialChars + "\]";												// Represents the range of characters allowed in a username or domainname.  It really states which chars aren't allowed.
	var quotedUser		= "(\"[^\"]*\")";																// 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 ipDomainPat		= /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;							// 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 atom			= validChars + '+';																// Represents an atom (basically a series of non-special characters.)
	var word			= "(" + atom + "|" + quotedUser + ")";											// 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 userPat			= new RegExp("^" + word + "(\\." + word + ")*$");								// Describes the structure of the user
	var domainPat		= new RegExp("^" + atom + "(\\." + atom +")*$");								// Describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above.


	// ****************************************************************************************************
	// Check Basic Pattern & Split User and Domain
	// ****************************************************************************************************
	var matchArray		= emailAddress.match(emailPat);
	if (matchArray == null) {
		return false;
	}

	var user			= matchArray[1];
	var domain			= matchArray[2];


	// ****************************************************************************************************
	// Confirm Only Basic ASCII Characters are used (0 - 127)
	// ****************************************************************************************************
	for (i = 0; i < user.length; i++) {
		if (user.charCodeAt(i) > 127) {
			return false;
	   }
	}

	for (i = 0; i < domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
			return false;
	   }
	}


	// ****************************************************************************************************
	// Confirm "User" entry is Valid
	// ****************************************************************************************************
	if (user.match(userPat) == null) {
		return false;
	}


	// ****************************************************************************************************
	// Handle IP Addresses and Confirm Validity
	// ****************************************************************************************************
	var IPArray			= domain.match(ipDomainPat);
	if (IPArray != null) {
		// this is an IP address
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				return false;
			}
		}
		return true;
	}


	// ****************************************************************************************************
	// Domain is a Symbolic Name. Confirm it is Valid.
	// ****************************************************************************************************
	var atomPat			= new RegExp("^" + atom + "$");
	var domArr			= domain.split(".");
	var len				= domArr.length;
	for (i = 0; i < len; i++) {
		if (domArr[i].search(atomPat) == -1) {
			return false;
	   }
	}

	// Domain name valid. Confirm known top-level domain (com, edu, gov) or two-letter country code (uk, nl) is used.
	// Verify that there's a hostname preceding the domain or country.
	if (domArr[domArr.length - 1].length != 2 && domArr[domArr.length - 1].search(knownDomsPat) == -1) {
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len < 2) {
		return false;
	}

	// ****************************************************************************************************
	// E-Mail Address is Valid
	// ****************************************************************************************************
	return true;
}


// ****************************************************************************************************
// Function popupWindow
// ****************************************************************************************************
function popupWindow(URL, Width, Height) {
  windowHandle	= window.open(URL, '', 'toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width='+Width+', height='+Height);
}

// ****************************************************************************************************
// Function IsNumeric
// This is used in the Hawaiian pages, to check that the account number is numeric
// ****************************************************************************************************

function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var Char;
	var IsNumber = true;
	var i;

	for (i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			return false;
		}
	}
	return true;
}