/*
-----------------------------------------------
Validate Script
language=JavaScript1.1
-----------------------------------------------
*/

function isBlank(str)
{
	if ((str == null) || (str.length == 0)) {
		return true;
	}

	for (var i=0; i < str.length; i++)
		if (str.charAt(i) != ' ')
			return false

	return true;
}


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 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?
	var matchArray = sMoney.match(moneyPat);

	if (matchArray == null)	{
		alert("Dollar amount is not in a valid format.");
		return false;
	}

	//	Dollar amount is valid
	return true;
}


//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?
	var matchArray = sNumber.match(numberPat);

	if (matchArray == null) {
		alert("Number is not in a valid format.");
		return false;
	}

	//	Number is valid
	return true
}	


function isValidEmail(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 represents the range of characters allowed as
	   the first character in a valid username or domain.  I just made it
	   the same as above, but if you want to add a different constraint,
	   you would change it here. */

	var firstChars=validChars

	/* 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 at atom (basically a series of
	   non-special characters.) */

	var atom="(" + firstChars + 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 course 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 is invalid (check @)")
		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 username is invalid.")
	    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("Destination IP address is invalid.")
				return false
		    }
	    }
	    return true
	}

	// Domain is symbolic name

	var domainArray=domain.match(domainPat)
	if (domainArray==null) 
	{
		alert("Email domain has invalid characters.");
	    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).
	   If there's a country code at the end of the address, the full domain
	   must include a hostname and category (e.g. host.co.uk or host.pub.nl).
	   If it ends in a .com or something, make sure there's a hostname.*/

	/* 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 (len < 2)
	{
		alert("Email domain is not in the proper format.");
		return false;
	}

//	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("Email address must end in a three-letter domain, or two-letter country.")
//	   return false
//	}
//
//	/* If it ends in a country code, we want to make sure there are at
//	   least 2 atoms preceding it (representing host and category (i.e.
//	   com, gov, etc.)) */
//	if (domArr[domArr.length-1].length==2 && len<3) 
//	{
//	   alert("A country code must be preceeded by a hostname and category.");
//	   return false
//	}
//
//	/* If it just ends in .com, .gov, etc., make sure there's a host name.
//	   This case can never actually happen because earlier checks take
//	   care of this implicitly, but we'll do it anyway. */
//	if (domArr[domArr.length-1].length==3 && len<2) 
//	{
//	   alert("Email hostname is missing.")
//	   return false
//	}
	
	// If we've gotten this far, everything's valid!
	return true;
}
