//--------------------------------------------------------------
function isValidEmail (strEmail) {

	//trim data
	strEmail = Trim(strEmail);
/* 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=strEmail.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. */
		return false
	}

	var user=matchArray[1]
	var domain=matchArray[2]

	// Verify that "user" is valid 
	if (user.match(userPat)==null) { 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) {
				return false; // destination address is invalid
		    }
		}
    	return true;
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) { 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) {
	   // the address must end in at least a two letter word.
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) { return false; }

// If we've gotten this far, everything's valid!
return true;
}
//--------------------------------------------------------------
function RTrim(strValue) {
	var whitespace = new String(" \t\n\r");
	var s = new String(strValue)

	// if there is whitespace at the end ...
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;
		// move to the position where the first non-whitespace character appears
	    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
			i--;
		}
		s = s.substring(0, i+1); // return the first char through the last non-whitespace
	}

   return s;
}
//--------------------------------------------------------------
function LTrim(strValue) {
	var whitespace = new String(" \t\n\r");
	var s = new String(strValue)

	// if there is whitespace at the beginning ...
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		// move to the position where the first non-whitespace character appears
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
			j++;
		}
		s = s.substring(j, i); // return the first non-whitespace through the last char
	}
	return s;
}
//--------------------------------------------------------------
function Trim(strValue) {
	var s = new String(strValue);
	return RTrim(LTrim(s));	
}
//--------------------------------------------------------------
function IsEmpty(strValue) {
	var s = new String(strValue);
	s = Trim(s);
	if ((!(s))||(s=="")) {
		return true;
	} else {
		return false ;
	}
}
//--------------------------------------------------------------
function isNumeric(strValue) {
	// look at each character to see if it is 0-9
	// accept decimal point and comma as well
	for(var i=0; i<strValue.length; i++) {
		var	c = strValue.substring(i,i+1);
        if(!( (c>="0" && c<="9") || (c=="." || c==",") )) {
			return false; // encountered non-numeric character
		}
	}
	return true; // valid number
}
//--------------------------------------------------------------
function isZipCode(strValue) {
	strValue = Trim(strValue);
	var dashArray = strValue.split("-")
	if(dashArray.length == 1) { 
		// no dash is present: must be 5 consecutive digits (e.g. 12345)
		return ( (dashArray[0].length == 5) && isNumeric(dashArray[0]) );
	} else if(dashArray.length == 2) {
		// dash is present: must be 5digits-4digits (e.g. 12345-6789)
		return ( 	(dashArray[0].length == 5) && isNumeric(dashArray[0]) && 
					(dashArray[1].length == 4) && isNumeric(dashArray[1]) );
	} else {
		// More than one dash - unknown format
		return false;
	}
}
//--------------------------------------------------------------

function formatACCT(strValue)
{
	// Supported formats:
	// 		Any combination of 10 or 11 digits (e.g. "123-456-789-0", or "1234567890", or "123 456 789 0 1"	)
	// Returns
	//		123-456-789-0 (for 10 digit numbers)
	//		123-456-789-0-1 (for 11 digit numbers)
	//		empty string (for invalid formats)		

	var digits = "";
	for(var i=0; i<strValue.length; i++) {
		var c = strValue.substr(i,1);
		if(isNumeric(c) && (c!=".") && (c!=",")) { 
			digits = digits + c;
		} 
	}

	if(digits.length == 10) {
		return ( 	digits.substr(0,3) + "-" + digits.substr(3,3) + "-" + digits.substr(6,3) + "-" + 
					digits.substr(9,1) );
	} else if(digits.length == 11) {
		return ( 	digits.substr(0,3) + "-" + digits.substr(3,3) + "-" + digits.substr(6,3) + "-" + 
					digits.substr(9,1) + "-" + digits.substr(10,1) );
	} else {
		return "";
	}
}

//--------------------------------------------------------------
function formatDate(strValue) {
	// Ensures:
	// 	If strValue is a recognized date MONTH/DAY/YEAR -> return value = M/D/YYYY
	// 		WHERE:
	// 			MONTH/DAY/YEAR is a valid calendar date with or without preceding zeroes
	//	Else -> return value = ""

	// force the input to have two slashes
	var slashArray = strValue.split("/");
	if(slashArray.length == 3) {
		// force the entered format to have a four-digit year
		if(Trim(slashArray[2]).length != 4) { return ""; }

		// Remove leading zeroes (e.g. 05/08/2003 -> 5/8/2003)
		var month = Trim(slashArray[0]);
		if(month.length > 1) { // strip leading zeroes
			if(month.substr(0,1) == "0") { month = month.substr(1,month.length-1); }
		}
		month = parseInt(month);
		
		var day = Trim(slashArray[1]);
		if(day.length > 1) { // strip leading zeroes
			if(day.substr(0,1) == "0") { day = day.substr(1,day.length-1); }
		}
		day = parseInt(day);

		var year = parseInt(Trim(slashArray[2]));
		
		// make sure the entered values are numeric
		if( (isNaN(month)) || (isNaN(day)) || (isNaN(year)) ) { return ""; } 

		// make sure the 4 digit, numeric year wasn't preceded by zeroes (e.g. 0203)
		if(year < 1000) { return ""; }

		// make sure the numbers are valid calendar days
		if( (month >= 1) && (month <= 12) && (day >= 1) && (day <= 31) ) {
			switch(day) {
				case 31: {  
					if( (month==2) || (month==4) || (month==6) || (month==9) || (month==11) ) {
						return ""; // Non-31 day month
					}
				} break;
				case 30: { 
					if(month==2) { return ""; } // February 30th
				} break;
				case 29: {
					// February on a non-leap year
					if( (month==2) && ((year%4)!= 0) ) { return ""; }
				} break;
				default: {}
			}
		} else {
			return ""; // invalid month or day
		}
		
		// passed all cases		
		return ( '' + month + '/' + day + '/' + year );
	} else {
		return ""; // there aren't 2 dashes (unknown format)
	}
}
//--------------------------------------------------------------
function formatPhone(strValue) {
	// Supported formats: (all variations of whitespace is okay)
	//		1234567890
	//		123-456-7890 
	//		(123) 456-7890
	//		123 456-7890
	//		123 456 7890
	// Returns:
	//		(123) 456-7890		

	var retVal = "";
	// create only a digit string: 1234567890
	for(var i=0; i<strValue.length; i++) {
		var c = strValue.charAt(i);
		if( (isNumeric(c)) && (c!=".") && (c!=",") ) {
			retVal = retVal + c; // add the character
		}
	}
	
	if(retVal.length == 10) {
		// change 1234567890 -> (123) 456-7890
		var temp = retVal;
		retVal = new String( "(" + temp.charAt(0) + temp.charAt(1) + temp.charAt(2) + ") " + 
							temp.charAt(3) + temp.charAt(4) + temp.charAt(5) + "-" + 
							temp.charAt(6) + temp.charAt(7) + temp.charAt(8) + temp.charAt(9) );
	} else {
		retVal = ""; // invalid input
	}
	return retVal;
}
//--------------------------------------------------------------
function formatSSN(strValue) {
	// Ensures: 
	// 		If strValue has 9 digits -> return value is in the format: 123-45-6789
	//		Else -> returns empty string		

	var retVal = "";
	for(var i=0; i<strValue.length; i++) {
		var c = strValue.charAt(i)
		if( (isNumeric(c)) ) {
			retVal = retVal + c; // add only numeric character
		}
	}
	
	if(retVal.length == 9) {
		// change 123456789 -> 123-45-6789
		retVal = 	retVal.charAt(0) + retVal.charAt(1) + retVal.charAt(2) + "-" +
					retVal.charAt(3) + retVal.charAt(4) + "-" +
					retVal.charAt(5) + retVal.charAt(6) + retVal.charAt(7) + retVal.charAt(8);
	} else {
		retVal = ""; // invalid input
	}
	return retVal;
}

//--------------------------------------------------------------
function trimTextFields(form) {
	// trim every textual form value
	for(var i=0; i<form.elements.length; i++) {
		if(form.elements.item(i).type == "text") {
 			form.elements.item(i).value = Trim(form.elements.item(i).value); 
		}
	}	
}
//--------------------------------------------------------------