function isEmail(emailAddress){
emailAddressValue=emailAddress.value.toLowerCase();
var countryTLDs=/^(ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|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|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|tc|td|tf|tg|th|tj|tk|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)$/;
var gTLDs=/^(aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|jobs)$/;
var basicAddress=/^(.+)@(.+)$/;
var specialChars='\\(\\)><@,;:\\\\\\\"\\.\\[\\]';
var validChars='\[^\\s'+specialChars+'\]';
var validCharset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\'-_.';
var quotedUser='(\"[^\"]*\")';
var atom=validChars+'+';
var word='('+atom+'|'+quotedUser+')';
var validUser=new RegExp('^'+word+'(\.'+word+')*$');
var symDomain=new RegExp('^'+atom+'(\.'+atom+')*$');
var matchArray=emailAddressValue.match(basicAddress);
if(emailAddress.value==''||emailAddress==null){
return true;
}
if(matchArray==null){
alert('The Email address doesn\'t seem to be correct,\nplease check syntax.');
emailAddress.focus();
return false;
}else{
var user=matchArray[1];
var domain=matchArray[2];
for(i=0;i<user.length;i++){
if(validCharset.indexOf(user.charAt(i))==-1){
alert('The Email address contains invalid characters,\nplease check the username.');
emailAddress.focus();
return false;
}
}
for(i=0;i<domain.length;i++){
if(validCharset.indexOf(domain.charAt(i))==-1){
alert('The Email address contains invalid characters,\nplease check the domain.');
emailAddress.focus();
return false;
}
}
if(user.match(validUser)==null){
alert('The Email address doesn\'t seem to be correct,\nplease check the username.');
emailAddress.focus();
return false;
}
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 Email address doesn\'t seem to be correct,\nplease check the domain name.');
emailAddress.focus();
return false;
}
}
if((domArr[domArr.length-1].length==2)&&(domArr[domArr.length-1].search(countryTLDs)==-1)){
alert('The Email address doesn\'t seem to be correct,\nplease check domain suffix.');
emailAddress.focus();
return false;
}
if((domArr[domArr.length-1].length>2)&&(domArr[domArr.length-1].search(gTLDs)==-1)){
alert('The Email address doesn\'t seem to be correct,\nplease check domain suffix.');
emailAddress.focus();
return false;
}
if((domArr[domArr.length-1].length<2)||(domArr[domArr.length-1].length>6)){
alert('The Email address doesn\'t seem to be correct,\nplease check domain suffix.');
emailAddress.focus();
return false;
}
if(len<2){
alert('The Email address doesn\'t seem to be correct,\nplease check missing hostname.');
emailAddress.focus();
return false;
}
}
return true;
}

String.prototype.trim = function() { return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");}

function mandatoryText(input,fieldName) {
	if (input.value.trim()==''||input==null) {
		alert('Please enter your '+fieldName+'.');
		input.focus();
		return false;
	} else {
		return true;
	}
}

function emailConfirmation(email,email2) {
	if (email.value.trim()== email2.value.trim()) {
		return true;
	} else {
		alert('E-mail confirmation must match e-mail.');
		email2.focus();
		return false;
	}
}

function validateZipCode(input) {
	return true;
}

function optionalText(input,validationFunction) {
	if (input.value.trim() == '' || input.value == input.defaultValue) {
		//prevent zip from being submitted where null or default, so as not to overwrite existing value
		input.removeAttribute("name");
	}
	if (input == null || input.value.trim() == '') {
		return true;
	} else {
		if (validationFunction(input)) {
			return true;
		} else {
			return false;
		}
	}
}

function isSomethingChecked(f) {
	for (i=0;i<f.elements.length;i++) {
		if ((f.elements[i].type == 'checkbox') && (f.elements[i].checked)) {
			return true;
		}
	}
	alert('At least one box must be checked.');
	return false;
}

function updateTimestamps(f) {
	for (i=0;i<f.elements.length;i++) {
		if ((f.elements[i].type == 'checkbox') && (!f.elements[i].checked)) {
			var timestamp = 'TS_' + f.elements[i].name;
			var node = document.getElementById(timestamp);
			if (node) {
				node.removeAttribute("name");
			}
		}
	}
}

function validForm() {
	if (!mandatoryText(document.getElementById('EMAIL_FIELD'), 'E-mail')) return;
	if (!isEmail(document.getElementById('EMAIL_FIELD'))) return;
	if (!optionalText(document.getElementById('ZIPCODE_FIELD'),validateZipCode)) return;
	if (!isSomethingChecked(document.getElementById('emvForm'))) return;
	updateTimestamps(document.getElementById('emvForm'));
	document.getElementById('emvForm').submit();
}

function validFormPopup() {
	if (!mandatoryText(document.getElementById('EMAIL_FIELD'), 'E-mail')) return;
	if (!isEmail(document.getElementById('EMAIL_FIELD'))) return;
	if (!optionalText(document.getElementById('ZIPCODE_FIELD'),validateZipCode)) return;
	document.getElementById('emvForm').submit();
}

function validFormUnsub() {
	if (!mandatoryText(document.getElementById('EMAIL_FIELD'), 'E-mail')) return;
	if (!isEmail(document.getElementById('EMAIL_FIELD'))) return;
	if (!isSomethingChecked(document.getElementById('emvForm'))) return;
	updateTimestamps(document.getElementById('emvForm'));
	document.getElementById('emvForm').submit();
}

function clearText(thefield) { 
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
	} else {
		thefield.value = thefield.defaultValue;
	}
}

