/*
-----------------------------------------------
Validate Script
language=JavaScript1.1
-----------------------------------------------
*/

//	Return a value with leading and trailing white spaces removed
function trim(str)
{
	var reTrim = /^\s*(\S[\s\S]*?)\s*$/;

	str.match(reTrim);

	return RegExp.$1;
}


//	Determine if an entered value is empty or all white spaces
function isBlank(str)
{
	var reEmpty = /^\s*$/;

	return reEmpty.test(str);
}


//Function for determining if form value is a number.
//Note:  The JScript isNaN method is a more elegant way to determine whether
//a value is not a number. However, some older browsers do not support this method.
function isValidNumber(sNumber)
{
	var numberPat = /^\d+$/;

	//	Check to see if the format is ok?
	return sNumber.match(numberPat) != null
}


function isValidMoney(sMoney)
{
//	var moneyPat = /^(\$?)(\d(1,3))(((\,?)(\d(3)))?)(((\.)(\d(2)))?)$/;
//	var moneyPat = /^(((\d{1,3})((\,\d{3})?))|(\d{4}))((\.\d{2})?)$/;
	var moneyPat = /^((\d{1})\,(\d{3}),(\d{3}))|((\d{1,3}\,\d{3})|(\d{1,7}))((\.\d{2})?)$/;
//	var moneyPat = /^((\d(1,1))\,(\d{1,3}\,\d{3})|(\d{1,7}))((\.\d{2})?)$/;

	//	Check to see if the format is ok?
	return sMoney.match(moneyPat) != null
}


function isValidDate(sDate)
{
	//	Checks for the following valid date formats:
	//	MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	//	Also separates date into month, day, and year variables

	//	Match format string to ensure a valid date format
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

	//	To require a 4 digit year entry, we can use this line instead:
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	//	Check to see if the format is ok?
	var matchArray = sDate.match(datePat);

	if (matchArray == null) {
		alert("Date is not in a valid format.")
		return false;
	}

	//	Parse date into variables
	month = matchArray[1];
	day = matchArray[3];
	year = matchArray[4];

	//	Check month range
	if (month < 1 || month > 12) {
		alert("Month must be between 1 and 12.");
		return false;
	}

	//	Check the day range
	if (month==4 || month==6 || month==9 || month==11)
		lastday=30;
	else
		if (month==2)
			lastday=29;
		else
			lastday=31;
	if (day < 1 || day > lastday)
	{
		alert("Day must be between 1 and " + lastday + ".");
		return false;
	}

	//	Check the year range
	if (year <= 99 && year >= 70)
		year = parseInt(year) + 1900;
	else
		if (year < 70 && year >= 0)
			year = parseInt(year, 10) + 2000;
		else
			if (year < 1970 || year >= 2070)
			{
				alert("Year is not in the proper range.");
				return false;
			}

	//	Fine tune the day validation for February
	if (month==2 && day==29 && !(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) {
		alert("February 29, " + year + " is not a valid .");
		return false;
	}

	// Date is valid
	return true;
}


function isValidEmail(emailStr) {
	isValidEmail(emailStr, false);
}

function isValidEmail(emailStr, bRequired)
{
	//	This pattern is used to check if the entered e-mail address fits the
	//	user@domain format.  It's also used to separate the username from the domain.
	var emailPat=/^(.+)@(.+)$/;


	//	The following string represents a pattern used to match special characters.
	//	The special characters that are not allowed in an e-mail address are:
	//	< > @ , ; : " . <space> [ ] ( ) \
	var specialChars="<>@,;:\\\"\\.\\s\\[\\]\\(\\)\\\\";


	//	The following string represents the range of characters 
	//	that are not allowed in a username or domain.
	var validChars="\[^" + specialChars + "\]";


	//	The following pattern represents the range of characters allowed as the
	//	first character in a valid username or domain.  It is presently set to
	//	the same as above, but any different constraints would be made here.
	var firstChars=validChars;


	//	The following string represents an atom (basically a series of
	//	non-special characters.)
	var atom="(" + firstChars + validChars + "*" + ")";


	//	The following pattern applies if the username is a quoted string.
	//	A quoted username has no rules about which characters are allowed
	//	and which aren't; anything goes. (eg. "jiminy cricket"@disney.com)
	var quotedUser="(\"[^\"]*\")";


	//	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 a quoted string.
	var word="(" + atom + "|" + quotedUser + ")";


	// The following pattern describes the structure of the full username
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");


	//	The following patter applies for domains that are IP addresses,
	//	rather than symbolic names.  (eg. job@[123.45.67.89] is valid)
	//	NOTE: The square brackets are required.
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;


	//	The following pattern describes the structure of a normal
	//	symbolic domain, as opposed to ipDomainPat, shown above.
	var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");


	//	Check for blank entry
	if (bRequired) {
		if (isBlank(emailStr)) {
			alert("Please enter an email address.");

			return false;
		}
	}


	//	Finally, start trying to figure out if the supplied address is valid.
	//	Begin with the course pattern to simply break up the user@domain
	//	into different pieces that are easy to analyze.
	var matchArray=emailStr.match(emailPat);


	//	Check for the presence of an @ sign.
	if (matchArray==null) {
		alert("Email address is invalid.  Check for an '@' sign.");
		return false;
	}


	//	Capture the username and domain sections
	var user=matchArray[1];
	var domain=matchArray[2];


	// See if the username is valid
	if (user.match(userPat)==null) {
		alert("Email username is invalid.");
		return false;
	}


	//	Determine if an IP address has been entered 
	//	(as opposed to a symbolic host name).
	var IPArray=domain.match(ipDomainPat);


	//	If an IP address has been entered, make sure it's valid.
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid.");
				return false;
			}
		}
	 
		return true;
	}


	// Domain is symbolic name
	var domainArray=domain.match(domainPat)


	//	Check the domain for invalid characters
	if (domainArray==null) {
		alert("Email domain has invalid characters.");
		return false;
	}


	//	So far the domain name seems valid.  Now we need to break up
	//	the domain to get the count of how many atoms it consists of.
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;


	//	Make sure that the domain name consists of more than 1 atom
	if (len < 2) {
		alert("Email domain is not in the proper format.");
		return false;
	}


	//	Make sure the domain name ends in an atom between 2 and 4 characters long.
	var tldLen = domArr[len-1].length;
	if (tldLen<2 || tldLen>4) {
		alert("Email address must end in a domain of two to four letters.");
		return false;
	}


	//	If a domain name ends in a two-letter country code, the full domain
	//	must include a hostname and category (e.g. host.co.uk or host.pub.nl).
//	if (domArr[domArr.length-1].length==2 && len<3) {
//	   alert("A country code must be preceeded by a hostname and category.");
//	   return false;
//	}

	
	// If we've gotten this far, everything's valid!
	return true;
}
