// Use addOnload to assign functions that must be run when the
// page is finished loading.  Allows multiple, separate script
// files to each attach something to window.onload without
// tromping on whatever was previously assigned to the event
// handler.
function addOnload(newFunction)
{
	// Store away whatever was already assigned to window.onload.
	// If nothing had been assigned, this doesn't do anything.
	var oldOnload = window.onload;
	
	if (typeof oldOnload == "function") {
		window.onload = function() {
			if (oldOnload) {
				oldOnload();
			}
			newFunction();
		}
	}
	
	else {
		window.onload = newFunction;
	} 
}

function prepNumberField(amount)
{
	// strip '$' and commas
	var result;
	result = amount.replace(/,/g,"");
	result = result.replace(/\$/g,"");
	return result;
}

// Regular expression-based decimal number check.
function decimalNumberCheck(value)
{
	// This particular regex requires that the leading
	// zero (to the left of the decimal place) is included
	// when evaluating values less than 1.
	var re = /^\d*[0-9](\.\d*[0-9])?$/;
	return re.test(value);
}

// Regular expression-based natural number (0, 1, 2, ...) check.
function naturalNumberCheck(value)
{
	var re = /^\d+$/;
	return re.test(value);
}

// Regular expression-based dollar currency check.
function dollarCurrencyCheck(amount)
{
	var re = /^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;
	return re.test(amount);
}

// Regular expression-based email check.  More robust
// than the basic check.
function emailCheck(email)
{
	// This regular expression covers the vast majority
	// of valid email address strings.
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/;
	return re.test(email);
}

// Regular expression-based check for fivie-digit USA zip code.
function fiveDigitZipCodeCheck(zipCode)
{
	var re = /\b[0-9]{5}(?:-[0-9]{4})?\b/;
	return re.test(zipCode);
}

// Regular expression-based check for two-letter USA state
// abbreviation.
function usaStateCodeCheck(state)
{
	var re = /\b(?:A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])\b/;
	return re.test(state);
}

// Regular expression-based phone number check.
function phoneNumberCheck(phoneNum, fieldName)
{
	// This regular expression handles many ways of entering a
	// phone number:
	// - optional left parenthesis
	// - 3 digits
	// - optional right parenthesis
	// - optional period, dash, forward slash, or space
	// - 3 digits
	// - optional period, dash, forward slash, or space
	// - 4 digits
	var re = /^\(?(\d{3})\)?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/;

	// exec is a method on a RegExp object, and returns null if
	// nothing in the supplied string (phoneNum, in this case)
	// matches the regular expression.  Otherwise, it returns 
	// what was matched.  In this case, there are three groupings
	// (within parentheses) in the regex, and so phoneArray will
	// contain three elements: the area code, the prefix, and
	// the four-digit portion.
	var phoneArray = re.exec(phoneNum);
	if (phoneArray)
	{
		document.getElementById(fieldName).value = "(" + phoneArray[1] + ") " + phoneArray[2] + "-" + phoneArray[3];
		return true;
	}
	else
		return false;
}

// Makes sure two password fields contain the same text.  Returns
// true if all validation checks pass, false otherwise.  This
// function takes care of issuing alerts to the user if there
// are any problems with the password fields.
function validateDoublePasswordEntry(firstFieldID, secondFieldID)
{
	var result = true;
	var password = document.getElementById(firstFieldID).value;
	var password2 = document.getElementById(secondFieldID).value;
	
	if (password == "" || password2 == "")
	{
		alert("A password field cannot be blank.");
		result = false;
	}
	
	else
	{
		// Make sure the two password entries are identical.
		if (password != password2)
		{
			alert("The two password fields do not match.  Please try again.");
			
			// Clear the two password fields of any input so the
			// user has to retype both.
			document.getElementById(firstFieldID).value = "";
			document.getElementById(secondFieldID).value = "";
			document.getElementById(firstFieldID).focus();
			
			result = false;
		}
	}
	
	// Returning false prevents the form action from being taken
	return result;
}

