// All Legacyfaces form validation scripting is contained in this file.

function showErrors(allerrors) {
	// opens the window for displaying errors
	width = 300;
	centw = (screen.width-300)/2;
	height = 300;
	centh = (screen.height-300)/2;
	child = window.open("../formerror.jsp","formerrors", "left="+centw+", top="+centh+", status=no, width=" + width + ",height=" + height + ",resizable=yes ,scrollbars=yes");	
}

function goToField(field) {
	// sets cursor focus to specified field
	field.focus();
	field.select();
}

function checkNum(field) {
	// checks to make sure user typed a number, 
	// if not, removes the letter/symbol
	if (window.event.keyCode != 9) {
		filteredValues = "1234567890.-";
		returnString = "";
		currvalue = field.value;
		for (i = 0; i < currvalue.length; i++) { 
			var c = currvalue.charAt(i);
			if (filteredValues.indexOf(c) != -1) returnString += c;
		}
		field.value = returnString;
	}
}

function checkChar(field) {
	// checks to make sure user typed a letter, 
	// if not, removes the number/symbol
	if (window.event.keyCode != 9) {
		filteredValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		returnString = "";
		currvalue = field.value;
		for (i = 0; i < currvalue.length; i++) { 
			var c = currvalue.charAt(i);
			if (filteredValues.indexOf(c) == -1) returnString += c;
		}
		field.value = returnString;
		field.focus();
	}
}

function isUpper(field) {
	// changes all typing to uppercase, as it is typed
	if (window.event.keyCode != 9) {
		thevalue = field.value;
		if (thevalue.toUpperCase() != thevalue)
			field.value = field.value.toUpperCase();	
	}
}

function checkDate(yr,mo,dy,focusto) {
	if (isValidDate(yr,mo,dy))
		alert("Date is OK");
	else {
		alert("Invalid Date");
		focusto.focus();
		focusto.select();
	}
}

function isValidDate(yrf,mof,dyf) {
	// checks to make sure the date entered is a valid date returns boolean value
	yr1 = yrf.value;
	mo1 = mof.value;
	dy1 = dyf.value;
	mo="";
	dy="";
	yr="";
	for (i=0; i<mo1.length; i++) {
		if (mo1.charAt(i) != " ")
			mo += mo1.substring(i,i+1);
	}
	for (i=0; i<dy1.length; i++) {
		if (dy1.charAt(i) != " ")
			dy += dy1.substring(i,i+1);
	}
	for (i=0; i<yr1.length; i++) {
		if (yr1.charAt(i) != " ")
			yr += yr1.substring(i,i+1);
	}
	if (yr.length < 1 || mo.length < 1 || dy.length <1)
		return false;
	if (yr.length == 1)
		yrf.value = "0" + yr;
	if (mo.length == 1)
		mof.value = "0" + mo;
	if (dy.length == 1)
		dyf.value = "0" + dy;
	if (yr > "50")
		yr = "19"+yr;
	else
		yr = "20"+yr;
	if (mo < 1 || mo > 12)
		return false;
	else {
		if ((mo == 4 || mo == 6 || mo == 9 || mo == 11) && dy > 30)
			return false;
		if ((mo == 1 || mo == 3 || mo == 5 || mo == 7 || mo == 8 || mo == 10 || mo == 12) && dy > 31)
			return false;
		if (mo == 2){
			if (LeapYear(yr) == true) {
				if (dy > 29) 
					return false;
			}
			else {
				if (dy > 28) 
					return false;
			}
		}
	}
	return true;
	
}
function LeapYear(intYear) {
	// checks if provided year is a leap year
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}
function isNotBlank(allerrors,formname) {
	// checks to make sure user entered something in each required field
	errors=0;
	for (i=0;i<formname.length;i++) {
		var field=formname.elements[i];
		if (field.required != null) {
			if (field.required.toUpperCase() == "TRUE") {
				if (field.value == " " || field.value == "") {
					if (field.title != null)
						allerrors += field.title + " is required|"+formname.name+"."+field.name+"\n";
					else						
						allerrors += field.name + " is required|"+formname.name+"."+field.name+"\n";
				}
			}
		}
	}
	return allerrors;
}

function isValidZip(fzip, fzip4) {
	// returns boolean check on correct number of digits
	zip = fzip.value;
	zip4 = fzip4.value;
	
	if (zip.length < 5) 
		return false;
	else if (zip.length = 5 && zip4.length > 0 && zip4.length < 4)
		return false;
	else 
		return true;
}

function isValidPhone(farea, fpre, fsuff) {
	// returns boolean check on correct phone format
	area = farea.value;
	pref = fpre.value;
	suff = fsuff.value;
	
	if (area != "" || pref != "" || suff != "") {
		if (area.length < 3)
			return false;
		if (pref.length < 3)
			return false;
		if (suff.length < 4)
			return false;
	}
	return true;
}

function addCentury(field) {
	// adds the century to the date entered
	yr = field.value;
	if (yr != "") {
		if (yr < 50) {
			yr = "20";
		}
		else {
			yr = "19";
		}
	}
	else
		yr = "  ";
	return yr;
	
}

/* 
V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) 
Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) 
Changes:

1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.

1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum).  One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function.  Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all.  This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to 
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.

1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).

1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal.  However, there's still the 
restriction that an address must end in a two or three letter
word.

1.1: Rewrote most of the function to conform more closely to RFC 822.

1.0: Original  */

function emailCheck (emailStr,fieldname) {
	
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 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=/^(ws|us|com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* 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=fieldname.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 (emailStr+" seems incorrect (check @ and .'s)");
	}
	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) {
			return ("The "+emailStr+" username contains invalid characters.");
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return ("The "+emailStr+" domain name contains invalid characters.");
		}
	}
	
	// See if "user" is valid 
	
	if (user.match(userPat)==null) {
		
		// user is not valid
		
		return ("The "+emailStr+" username doesn't seem to be valid.");
	}
	
	/* 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 (emailStr+" destination IP address is invalid!");
			}
		}
		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) {
			return ("The "+emailStr+" domain name does not seem to be valid.");
		}
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
			return (emailStr+" must end in a well-known domain or two letter " + "country.");
		}
	
	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
		return (emailStr+" address is missing a hostname!");
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

//  End -->

