/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Viala :: http://www.viala.hu */

/* Based on: Travis Beckham ::
http://www.squidfingers.com | http://www.podlob.com
Based on: Manzi Olivier :: http://www.imanzi.com/
Based on: jgw (jgwang@csua.berkeley.edu )/ */

//*********************************************************************
/* Added alternate email address verification script as noted
in the isEmail function.

Also added a field property 'allowDefault' to allow the default
value when a php page is reloaded due to error. This allows for
the previous form values entered to remain so that the user
doesn't have to reenter anything.*/
//*********************************************************************

function checkCapsLock(e) {
	var myKeyCode = 0;
	var myShiftKey = false;

	// Internet Explorer 4+
	if (document.all) {
		myKeyCode = e.keyCode;
		myShiftKey = e.shiftKey;

	// Netscape 4
	} else if (document.layers) {
		myKeyCode = e.which;
		myShiftKey = (myKeyCode == 16) ? true : false;

	// Netscape 6
	} else if (document.getElementById) {
		myKeyCode = e.which;
		myShiftKey = (myKeyCode == 16) ? true : false;
	}

	// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
	if ((myKeyCode >= 65 && myKeyCode <= 90) && !myShiftKey) {
		alert(errormsg[100]);
	// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
	} else if ((myKeyCode >= 97 && myKeyCode <= 122) && myShiftKey) {
		alert(errormsg[100]); }
}

function CalcKeyCode(aChar) {
	var character = aChar.substring(0,1);
	var code = aChar.charCodeAt(0);
	return code;
}

function checkNumber(val) {
	var strPass = val.value;
	var strLength = strPass.length;
	var lchar = val.value.charAt((strLength) - 1);
	var cCode = CalcKeyCode(lchar);

	/* Check if the keyed in character is a number do you want alphabetic UPPERCASE only ?
     or lower case only just check their respective codes and replace the 48 and 57 */

	if (cCode < 48 || cCode > 57) {
		var myNumber = val.value.substring(0, (strLength) - 1);
		val.value = myNumber; }
	return false;
}

function isEmpty(str){
	return (str == null) || (str.length == 0);
}

function isEmail(emailStr){
	/*Original code replaced to do a more thorough check of the email address
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
	End of original code*/

	//**********************************************************************************
	//<!-- 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  */


	/* 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 = /^(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 = 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. */
		return 20; }

	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 21;

	for (i = 0; i < domain.length; i++)
		if (domain.charCodeAt(i) > 127) return 22;

	// See if "user" is valid 
	if (user.match(userPat) == null) return 23;

	/* 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) {
		for (var i = 1; i <= 4; i++)
			if (IPArray[i] > 255) return 24;
		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 25;

	/* 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 26;

	// Make sure there's a host name preceding the domain.
	if (len < 2) return 27;

	// If we've gotten this far, everything's valid!
	return 0;
	//**********************************************************************************
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str) {
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters 0-9
function isNumeric(str) {
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str) {
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string's length equals "len"
function isLength(str, len) {
	return str.length == len;
}

// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max) {
	return (str.length >= min) && (str.length <= max);
}

// returns true if the value is between "min" and "max"
function isNumRange(val, min, max) {
	return (val >= min) && (val <= max);
}

// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str) {
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}

// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str) {
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var y = parseInt(result[3]);
	var m = parseInt(result[1]);
	var d = parseInt(result[2]);
	if (m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if (m == 2) {
		var days = ((y % 4) == 0) ? 29 : 28;
	} else if (m == 4 || m == 6 || m == 9 || m == 11) {
		var days = 30;
	} else {
		var days = 31; }
	return (d >= 1 && d <= days);
}

// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2) {
	return str1 == str2;
}

// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
// NOT USED IN FORM VALIDATION
function isWhitespace(str) { 
	var re = /[\S]/g
	if (re.test(str)) return false;
	return true;
}

// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
// NOT USED IN FORM VALIDATION
function stripWhitespace(str, replacement) {
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if (str.search(re) != -1) {
		result = str.replace(re, replacement);
	}
	return result;
}

function resetForm(f, errorClass, newClass) {
	var i,e,t,str;
	var re = new RegExp(errorClass);
	for (i = 0; i < f.elements.length; i++) {
		e = f.elements[i];
		t = e.type;
		if (t == 'text' || t == 'password' || t == 'textarea') {
			str = e.className;
			e.className = str.replace(re, newClass); } } }

// validate the form
function validateForm(f, preCheck, orgClass, newClass, alerttype) {
	var errors = '';
	var errorsa = '';
	var reNew = new RegExp(newClass);
	var reOrg = new RegExp(orgClass);
	if (preCheck != null) errors += preCheck;
	var i,e,t,n,v;
	for (i = 0; i < f.elements.length; i++) {
		e = f.elements[i];

		t = e.type;
		n = e.id;
		v = e.value;
		if (e.optional && v == "") continue;
		if (t == 'text' || t == 'password' || t == 'textarea') {
			if (isEmpty(v)) {
				errors += n + errormsg[1] + '<br>';
				errorsa += n + errormsg[1] + '\n';
				e.className = e.className.replace(reOrg, newClass);
				continue;
			} else {
				e.className = e.className.replace(reNew, orgClass); }

			if (!e.allowDefault) {
				if (v == e.defaultValue) {
					errors += n + errormsg[2] + '<br>';
					errorsa += n + errormsg[2] + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if(e.isAlpha){
				if (!isAlpha(v)) {
					errors += n + errormsg[3] + '<br>';
					errorsa += n + errormsg[3] + '\n';
					overlib('eaaaa');
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isNumeric) {
				if (!isNumeric(v)) {
					errors += n + errormsg[4] + '<br>';
					errorsa += n + errormsg[4] + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isAlphaNumeric) {
				if (!isAlphaNumeric(v)) {
					errors += n + errormsg[5] + '<br>';
					errorsa += n + errormsg[5] + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isEmail) {
				rc = isEmail(v);
				if (rc > 0) {
					errors += errormsg[rc] + '<br>';
					errorsa += errormsg[rc] + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isLength != null) {
				var len = e.isLength;
				if (!isLength(v,len)) {
					errors += n + errormsg[7] + len + '<br>';
					errorsa += n + errormsg[7] + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isLengthBetween != null) {
				var min = e.isLengthBetween[0];
				var max = e.isLengthBetween[1];
				if (!isLengthBetween(v,min,max)) {
					errors += n + errormsg[8] + min + '-' + max + '<br>';
					errorsa += n + errormsg[8] + min + '-' + max + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isNumRange != null) {
				var min = e.isNumRange[0];
				var max = e.isNumRange[1];
				if (!isLNumRange(v,min,max)) {
					errors += n + errormsg[28] + min + '-' + max + '<br>';
					errorsa += n + errormsg[28] + min + '-' + max + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isPhoneNumber) {
				if (!isPhoneNumber(v)) {
					errors += v + errormsg[9] + '<br>';
					errorsa += n + errormsg[9] + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isDate) {
				if (!isDate(v)) {
					errors += v + errormsg[10] + '<br>';
					errorsa += n + errormsg[10] + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } }

			if (e.isMatch != null) {
				if (!isMatch(v, e.isMatch)) {
					errors += n + errormsg[11] + '<br>';
					errorsa += n + errormsg[11] + '\n';
					e.className = e.className.replace(reOrg, newClass);
					continue;
				} else {
					e.className = e.className.replace(reNew, orgClass); } } }

		if (t.indexOf('select') != -1) {
			if (isEmpty(e.options[e.selectedIndex].value)) {
				errors += n + errormsg[12] + '<br>';
				errorsa += n + errormsg[12] + '\n';
				e.className = e.className.replace(reOrg, newClass);
				continue;
			} else {
				e.className = e.className.replace(reNew, orgClass); } }

		if (t == 'file') {
			if (isEmpty(v)) {
				errors += n + errormsg[13] + '<br>';
				errorsa += n + errormsg[13] + '\n';
				e.className = e.className.replace(reOrg, newClass);
				continue;
			} else {
				e.className = e.className.replace(reNew, orgClass); } } }

	div = document.getElementById('errordiv');
	if (errors != '') {
		if (alerttype == '2' || alerttype == '3') alert(errorsa);
		if (alerttype == '1' || alerttype == '3') return dispErr(errors, div);
	}
	if (div != null)
		div.style.display="none";
	return errors == '';
}

dispErr = function(error, divo) {
	divo.style.display="block";
	divo.innerHTML = error;
	return false;
}

/*
The following elements are not validated...

button   type="button"
checkbox type="checkbox"
hidden   type="hidden"
radio    type="radio"
reset    type="reset"
submit   type="submit"

All elements are assumed required and will only be validated for an
empty value or defaultValue unless specified by the following properties.

isEmail = true;          // valid email address
isAlpha = true;          // A-Z a-z characters only
isNumeric = true;        // 0-9 characters only
isAlphaNumeric = true;   // A-Z a-z 0-9 characters only
isLength = number;       // must be exact length
isLengthBetween = array; // [lowNumber, highNumber] must be between lowNumber and highNumber
isPhoneNumber = true;    // valid phone number. See "isPhoneNumber()" comments for the formatting rules
isDate = true;           // valid date. See "isDate()" comments for the formatting rules
isMatch = string;        // must match string
optional = true;         // element will not be validated

alerttype = 0            // no error msg
alerttype = 1            // error msg in div
alerttype = 2            // error msg in alert
alerttype = 3            // error msg in div and alert
*/

//============================

// error msg depends on the language
var errormsg = new Array();
errormsg[0] = 'Select at least one checkbox!';
errormsg[1] = ' cannot be empty!';
errormsg[2] = ' cannot use the default value!';
errormsg[3] = ' can only contain characters A-Z a-z!';
errormsg[4] = ' can only contain characters 0-9!';
errormsg[5] = ' can only contain characters A-Z a-z 0-9!';
errormsg[6] = ' is not a valid email!';
errormsg[7] = ' character number must be less than ';
errormsg[8] = ' character number must be between ';
errormsg[9] = ' is not a valid US phone number!';
errormsg[10] = ' is not a valid date!';
errormsg[11] = ' does not match!';
errormsg[12] = ' needs an option selected!';
errormsg[13] = ' needs a file to upload!';
errormsg[20] = 'Email address seems incorrect (check @ and .\'s)';
errormsg[21] = 'Ths username contains invalid characters.';
errormsg[22] = 'Ths domain name contains invalid characters.';
errormsg[23] = 'The username doesn\'t seem to be valid.';
errormsg[24] = 'Destination IP address is invalid!';
errormsg[25] = 'The domain name does not seem to be valid.';
errormsg[26] = 'The address must end in a well-known domain or two letter country.';
errormsg[27] = 'This address is missing a hostname!';
errormsg[28] = ' number must be between ';
errormsg[99] = 'All form information will be erased!';
errormsg[100] = 'Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';
