/* ================================================================================================ */
function emailCheck (emailStr) 
{
	/* 1 means check two country letter, 0 means don't. */
	var checkTLD=1;
	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	/* Does the address fit the user@domain format.  Used to separate the username from the domain. */
	var emailPat=/^(.+)@(.+)$/;
	/* Don't allow these characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	/* The range of characters allowed.*/
	var validChars="\[^\\s" + specialChars + "\]";
	/* A quoted email address in not a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";
	/* 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 + '+';
	/* In john.doe@somewhere.com, john and doe are words. 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. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	/* Break up user@domain into different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) 
		{
			/* If there are too many or too few @'s, this address doesn't fit. */
			alert("Email address seems incorrect (check @ and .'s)");
			return false;
		}
		
	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) 
		{
			if (user.charCodeAt(i)>127) 
				{
					alert("Ths username contains invalid characters.");
					return false;
   				}
		}
		
	for (i=0; i<domain.length; i++) 
		{
			if (domain.charCodeAt(i)>127) 
				{
					alert("Ths domain name contains invalid characters.");
					return false;
   				}
		}

	// See if "user" is valid 
	if (user.match(userPat)==null) 
		{
			alert("The username doesn't seem to be valid.");
			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.  Check if it's 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) 
				{
					alert("The domain name does not seem to be valid.");
					return false;
   				}
		}

	/* At this point the domain name seems valid, but now make sure that it ends in a known top-level domain. */
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
		{
			alert("The address must end in a well-known domain or two letter " + "country.");
			return false;
		}

	// Make sure there's a host name preceding the domain.
	if (len<2) 
		{
			alert("This address is missing a hostname!");
			return false;
		}

// Everything's valid!
return true;
}
/* ================================================================================================ */


	
	// Check whether string s is empty.
	function isEmpty(s)
	{ return ((s == null) || (s.length == 0)) }
	
	/****************************************************************/
	// whitespace characters
	var whitespace = " \t\n\r";

      function isWhitespace (s)
      {
           var i;

           // Is s empty?
           if (isEmpty(s)) return true;

           // Search through string's characters one by one
           // until we find a non-whitespace character.
           // When we do, return false; if we don't, return true.

           for (i = 0; i < s.length; i++)
           {
                // Check that current character isn't whitespace.
                var c = s.charAt(i);
                if (whitespace.indexOf(c) == -1) return false;
           }
           // All characters are whitespace.
           return true;
      }
	  
		var numb = '0123456789';
		var lwr = 'abcdefghijklmnopqrstuvwxyz';
		var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		
		function isValid(parm,val) {
			if (parm == "") return true;
			for (i=0; i<parm.length; i++) 
				{
				if (val.indexOf(parm.charAt(i),0) == -1) return false;
				}
			return true;
			}
		
		function isNum(parm) {return isValid(parm,numb);}
		function isLower(parm) {return isValid(parm,lwr);}
		function isUpper(parm) {return isValid(parm,upr);}
		function isAlpha(parm) {return isValid(parm,lwr+upr);}
		function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);}
		
		function isValidDate(dateStr) {
		// Date validation function courtesty of 
		// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
		
		// Checks for the following valid date formats:
		// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
		
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
		
		var matchArray = dateStr.match(datePat); // is the format ok?
		if (matchArray == null) 
		{
		alert(dateStr + " is not in a valid date format.")
		return false;
		}
		month = matchArray[1]; // parse date into variables
		day = matchArray[3];
		year = matchArray[4];
		if (month < 1 || month > 12) 
			{ // check month range
			alert("Month must be between 1 and 12.");
			return false;
			}
		if (day < 1 || day > 31) 
			{
			alert("Day must be between 1 and 31.");
			return false;
			}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) 
			{
			alert("Month "+month+" doesn't have 31 days!")
			return false;
			}
		if (month == 2) 
			{ // check for february 29th
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day==29 && !isleap)) 
				{
				alert("February " + year + " doesn't have " + day + " days!");
				return false;
				}
			}
		return true;
		}
			
		function isValidTime(timeStr) {
		// Time validation function courtesty of 
		// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
		
		// Checks if time is in HH:MM:SS AM/PM format.
		// The seconds and AM/PM are optional.
		
		var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
		
		var matchArray = timeStr.match(timePat);
		if (matchArray == null) {
		alert("Time is not in a valid format.");
		return false;
		}
		hour = matchArray[1];
		minute = matchArray[2];
		second = matchArray[4];
		ampm = matchArray[6];
		
		if (second=="") { second = null; }
		if (ampm=="") { ampm = null }
		
		if (hour < 0  || hour > 23) {
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
		}
		if (hour <= 12 && ampm == null) {
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
		alert("You must specify AM or PM.");
		return false;
		   }
		}
		if  (hour > 12 && ampm != null) {
		alert("You can't specify AM or PM for military time.");
		return false;
		}
		if (minute < 0 || minute > 59) {
		alert ("Minute must be between 0 and 59.");
		return false;
		}
		if (second != null && (second < 0 || second > 59)) {
		alert ("Second must be between 0 and 59.");
		return false;
		}
		return true;
		}

function inputValidation()
{	

	// Last Name cannot be left blank
	if ( isWhitespace(myForm.firstName.value)) 
	{
		myForm.firstName.focus();
		alert('First Name cannot be left blank');
		return false;
	}

	// Last Name cannot be left blank
	if ( isWhitespace(myForm.lastName.value)) 
	{
		myForm.lastName.focus();
		alert('Last Name cannot be left blank');
		return false;
	}

	// Last Name cannot be left blank
	if ( isWhitespace(myForm.phonearea.value)) 
	{
		myForm.phonearea.focus();
		alert('Please enter your phone area code.');
		return false;
	}

	// Last Name cannot be left blank
	if ( isWhitespace(myForm.phoneprefix.value)) 
	{
		myForm.phoneprefix.focus();
		alert('Please enter your phone prefix.');
		return false;
	}

	// Last Name cannot be left blank
	if ( isWhitespace(myForm.phonenumber.value)) 
	{
		myForm.phonenumber.focus();
		alert('Please enter your phone number.');
		return false;
	}

		// Last Name cannot be left blank
		if ( isWhitespace(myForm.email.value)) 
		{
			myForm.email.focus();
			alert('Please enter a value for your email address.');
			return false;
		}
	
		// Last Name cannot be left blank
		if(!emailCheck(myForm.email.value))
		{
			return false;
		}

	// Last Name cannot be left blank
	if ( isWhitespace(myForm.customerBillingZip.value)) 
	{
		myForm.customerBillingZip.focus();
		alert('Please enter a value for the zip code field.');
		return false;
	}
	// All is well, submit the form
	return true;
}


