// $Id: find_tellFriend.js 1103 2008-05-09 10:06:09Z dmf $
// Functions to support validation of find_tellFriend

// Variables to be populated in the HTML: 
// none, but valStrings should come from requiredFields.js

// checkEmail by DMF, based on emailCheck v1.1.2 from http://javascript.internet.com 
function checkEmail (formElement) {
	var emailStr = formElement.value;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\!\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var domAtom = '[a-zA-Z0-9\-]+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + domAtom + "(\\." + domAtom +")*$");
	var domAtomPat = new RegExp(domAtom,"g");

	// Don't bother if there's no emailStr
	if (!emailStr) {
		return true;
	}

	// 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) {
		alert (valStrings["emailUserAtDomain"]);
		formElement.focus();
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];

	// See if "user" is valid 
	if (user.match(userPat) == null) {
		alert (valStrings["emailUserPart"]);
		formElement.focus();
		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) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert (valStrings["emailIPBad"]);
				formElement.focus();
				return false;
			}
		}
		return true;
	}

	// Domain is symbolic name
	if (domain.match(domainPat) == null) {
		alert (valStrings["emailDomainPart"]);
		formElement.focus();
		return false;
	}

	// Domain name seems valid, but now make sure that it ends in a valid TLD and that there's a hostname preceding the TLD
	var domArr = domain.match(domAtomPat);
	var len = domArr.length;
	if (!domArr[len-1].match(/^(com|net|org|edu|gov|mil|int|arpa|biz|info|name|pro|museum|aero|coop|travel|jobs|eu)$/i) // .travel, .jobs and .eu added on 2005-04-08
	 && !domArr[len-1].match(/^(ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz)$/i)
	 && !domArr[len-1].match(/^(ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy)$/i)
	 && !domArr[len-1].match(/^(hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz)$/i)
	 && !domArr[len-1].match(/^(na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|sv|sy|sz)$/i)
	 && !domArr[len-1].match(/^(tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$/i)
	   ) {
		alert (valStrings["emailBadTLD"]);
		formElement.focus();
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len < 2) {
		alert (valStrings["emailNoSLD"]);
		formElement.focus();
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

// Conditions for form submission
function submitTellFriend (checkForm) {
	return (checkRequiredFields(checkForm) 
	     && checkEmail (checkForm.email));
}

function submitTellFriendSite (checkForm) {
	return (checkRequiredFields(checkForm) 
	     && checkEmail (checkForm.friendEmail));
}

function submitTellFriendSiteG (checkForm) {
	return (checkRequiredFields(checkForm) 
	     && checkEmail (checkForm.senderEmail)
	     && checkEmail (checkForm.friendEmail));
}

