 /*
 * 
 * E-Mail Vision Generic Join Webform Validation JavaScript - (c) 2002
 * 
 *
*/

	// This function retrieves the values of the DOB drop down lists and concatenate them into the 'DOB_FIELD' hidden field
	function makeDate(depart_m, depart_d,depart_y) {
		var emvForm = document.emvForm;
		if  ((emvForm.depart_m.options[emvForm.depart_m.selectedIndex].value != '')&&(emvForm.depart_d.options[emvForm.depart_d.selectedIndex].value != '')&&(emvForm.depart_y.options[emvForm.depart_y.selectedIndex].value != '') ) {
			var month = String(emvForm.depart_m.options[emvForm.depart_m.selectedIndex].value);
			var day = String(emvForm.depart_d.options[emvForm.depart_d.selectedIndex].value);
			var year = String(emvForm.depart_y.options[emvForm.depart_y.selectedIndex].value);
			var emvDepart = month +"/" + day +"/" + year;
			}
		else {
			var emvDepart  = '';
		}
		return emvDepart ;
		
	}
	
	// This function retrieves the values of the Date Of Purchase  drop down lists and concatenate them into the 'B1_FiELD' hidden field
	function makeDate2(return_m, return_d, return_y) {
		var emvForm = document.emvForm;
		if  ((emvForm.return_m.options[emvForm.return_m.selectedIndex].value != '')&&(emvForm.return_d.options[emvForm.return_d.selectedIndex].value != '')&&(emvForm.return_y.options[emvForm.return_y.selectedIndex].value != '') ) {
			var month = String(emvForm.return_m.options[emvForm.return_m.selectedIndex].value);
			var day = String(emvForm.return_d.options[emvForm.return_d.selectedIndex].value);
			var year = String(emvForm.return_y.options[emvForm.return_y.selectedIndex].value);
			var emvReturn=	month +"/" + day +"/" + year;
			}
		else {
			var emvReturn = '';
		}
		return emvReturn;
		
	}
	
	// This function retrieves the values of the DOB drop down lists and concatenate them into the 'DOB_FIELD' hidden field
	function makeDate3(dopa_m, dopa_d, dopa_y) {
		var emvForm = document.emvForm;
		if  ((emvForm.dopa_m.options[emvForm.dopa_m.selectedIndex].value != '')&&(emvForm.dopa_d.options[emvForm.dopa_d.selectedIndex].value != '')&&(emvForm.dopa_y.options[emvForm.dopa_y.selectedIndex].value != '') ) {
			var month = String(emvForm.dopa_m.options[emvForm.dopa_m.selectedIndex].value);
			var day = String(emvForm.dopa_d.options[emvForm.dopa_d.selectedIndex].value);
			var year = String(emvForm.dopa_y.options[emvForm.dopa_y.selectedIndex].value);
			var emvDob =	month + '/' + day + '/' + year;
			}
		else {
			var emvDob = '';
		}
		return emvDob;
	}

	
	// This function retrieves the values of the DOB drop down lists and concatenate them into the 'DOB_FIELD' hidden field
	function makeDate4(dom_m, dom_d, dom_y) {
		var emvForm = document.emvForm;
		if  ((emvForm.dom_m.options[emvForm.dom_m.selectedIndex].value != '')&&(emvForm.dom_d.value != '')&&(emvForm.dom_y.options[emvForm.dom_y.selectedIndex].value != '') ) {
			var month = String(emvForm.dom_m.options[emvForm.dom_m.selectedIndex].value);
			var day = emvForm.dom_d.value;
			var year = String(emvForm.dom_y.options[emvForm.dom_y.selectedIndex].value);
			var emvDob =	day +  month + year;
			}
		else {
			var emvDob = '';
		}
		return emvDob;
	}

	// Text field validator
	function mandatoryText(text, msg) {
		if (text.value=="") {
			alert("Please enter your " + msg + ".");
			text.focus();
			return false;
		}
		return true;
	}
	
	// Drop Down Lists validator	
	function mandatoryCombo(dropdown, msg) {
		if (dropdown.options[0].selected) {
			alert("Please select your " + msg + ".");
			dropdown.focus();
			return false;
		}
		return true;
	}
	
	// Radio buttons arrays validator
	function mandatoryRadio(radioList, msg) {
		var radioValue = null;
		for (var i=0; i<radioList.length; i++) {
			if (radioList[i].checked) {
				radioValue = radioList[i].value;
				break;
			}
		}
		if (radioValue == null) {
			alert(msg);
			radioList[0].focus();
			return false;
		}
		else {
			return true;
	}
}

	// this function prechecks the email format radio buttons based on the email address
	function aol(str) {
		var emvForm = document.emvForm;	
		var add1 = "@aol.";
		var add2 = "@compuserve.co";
		var add3 = "@cs.co";
		str = str.toLowerCase();		
		if ( (str!="")&&((str.indexOf(add1)!= -1)||(str.indexOf(add2)!= -1)||(str.indexOf(add3) != -1)) ) {
			emvForm.C9_FIELD[2].checked=true;
			return;
		}
		else {
			emvForm.C9_FIELD[0].checked=true;
		}
	}
	
	function isEmail(emailAddress) {
		emailAddressValue=emailAddress.value.toLowerCase();
		// Below reside knows 2 letters country TLD and 3 letter gTLDs
		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|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)$/;
		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 (matchArray==null) {
			alert("Please ensure that you enter a correct email address.");
			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("Please ensure that you enter a correct email address.");
					emailAddress.focus();
					return false;
				}
			}
			for (i=0; i<domain.length; i++) {
				if(validCharset.indexOf(domain.charAt(i))==-1) {
					alert("Please ensure that you enter a correct email address.");
					emailAddress.focus();
					return false;
				}
			}
			if (user.match(validUser)==null) {
				alert("Please ensure that you enter a correct email address.");
				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("Please ensure that you enter a correct email address.");
					emailAddress.focus();
					return false;
				}
			}
			if ((domArr[domArr.length-1].length==2)&&(domArr[domArr.length-1].search(countryTLDs)==-1)) {
					alert("Please ensure that you enter a correct email address.");
					emailAddress.focus();
					return false;
			}
			if ((domArr[domArr.length-1].length>2)&&(domArr[domArr.length-1].search(gTLDs)==-1)) {
				alert("Please ensure that you enter a correct email address.");
				emailAddress.focus();
				return false;
			}
			if ((domArr[domArr.length-1].length<2)||(domArr[domArr.length-1].length>6)) {
				alert("Please ensure that you enter a correct email address.");
				emailAddress.focus();
				return false;
			}
			if (len<2) {
				alert("Please ensure that you enter a correct email address.");
				emailAddress.focus();
				return false;
			}
			return true;
		}
	}
			
/*
 *
 * This function validates the form fields
 * Use the functions above to make the relevant
 * fields required
 *
*/

function dateCtrl(myDate, field) { 
	// Enforce date later than sysdate 
	var cal = new Array();
	cal[0] = "January";
	cal[1] = "February";
	cal[2] = "March";
	cal[3] = "April";
	cal[4] = "May";
	cal[5] = "June";
	cal[6] = "July";
	cal[7] = "August";
	cal[8] = "September";
	cal[9] = "October";
	cal[10] = "November";
	cal[11] = "December";
	var sysDate = new Date();
	var effDateChar = myDate.value;
	//alert(effDateChar);
/*
	// Translates date entered by user into JS format 
	// DD lists passing MM/DD/YYYY
	var bufArray = effDateChar.split("/");
	var finalMonth;
	bufArray[0].indexOf("0") > 1 ? finalMonth = ((bufArray[0].substring(1,2))-1) : finalMonth = ((bufArray[0].substring(0,2))-1);  
	var effDateMonth = cal[finalMonth];
	var effDateDay = bufArray[1];
	var effDateYear = bufArray[2];
*/
	var tempMonth = effDateChar.substring(0,2);
	var effDateDay = "01"; //effDateChar.substring(0,2);
	var effDateYear = effDateChar.substring(2,6);
	var finalMonth;
	tempMonth.indexOf("0") > 1 ? finalMonth = ((tempMonth.substring(1,2))-1) : finalMonth = ((tempMonth.substring(0,2))-1);  
	var effDateMonth = cal[finalMonth];
	
	//alert("Day " + effDateDay + " | Month " + effDateMonth + " | Year " + effDateYear);
	var effDateDate = new Date(effDateMonth + " " + effDateDay + ", " + effDateYear + " 00:00:01"); 
	//alert("Date Of Move:" + effDateDate + "\nToday: " + sysDate);
	// compare the 2 dates 
	if (effDateDate > sysDate) {
		alert("The date you moved can't be in the future."); 
		field.focus();
		return false;
	} 
	return true; 
}



function validate() {
	var emvForm = document.emvForm;
		if (emvForm.GNER_OPTIN.checked) { emvForm.GNER_OPTIN_FIELD.value = "1"; }else{emvForm.GNER_OPTIN_FIELD.value = "0";};
	// Checking title
	if(!mandatoryCombo(emvForm.TITLE_FIELD, "Title")) {
		return;
	} else if(!mandatoryText(emvForm.FIRSTNAME_FIELD, "First Name")) {
		return;
	} else if(!mandatoryText(emvForm.LASTNAME_FIELD, "Surname")) {
		return;
	} else if(!mandatoryText(emvForm.EMAIL_FIELD, "E-Mail Address")) {
		return;
	} else if(!isEmail(emvForm.EMAIL_FIELD)) {
		return;

	/* 
	 * Validating the Date of purchase (3 drop down lists: dd + mm + yyyy)
	 * for leap years / non leap years.
	 *
	*/
	// checking that a day of purchase is selected	
		} else if ( (emvForm.depart_d.value =="")||((emvForm.depart_m.value =="")||(emvForm.depart_y.value =="")) ) {
		alert("Please select a day, a month and a year of your departure date.");	  
		return;
	} else if ((emvForm.depart_m.options[2].selected)&&(emvForm.depart_d.value > 29)) {
		alert("The month of February has:\n    - 28 days on non-leap years\n    - 29 days on leap years.");
		emvForm.depart_d.focus();
		return;
	} else if ((emvForm.depart_m.options[2].selected)&&(emvForm.depart_d.value == 29)) {
		if ((emvForm.depart_y.value % 4) != 0) {
			alert("The month of February has 28 days for the year selected (non-leap year).");
			emvForm.depart_d.focus();
			return;
		}
	} else if ( ((emvForm.depart_m.options[4].selected)||(emvForm.depart_m.options[6].selected)||(emvForm.depart_m.options[9].selected)||(emvForm.depart_m.options[11].selected))&&(emvForm.depart_d.options[31].selected)) {
			var finalMonth;
			var theMonth = document.emvForm.depart_m.options[document.emvForm.depart_m.selectedIndex].value;
			switch (theMonth) {
				case '04': finalMonth='The month of April';
				break;
				case '06': finalMonth='The month of June';
				break;
				case '09': finalMonth='The month of September';
				break;
				case '11': finalMonth='The month of November';
				break;
				default: finalMonth='The selected month';
			}
			alert(finalMonth + ' has only 30 days.');
			emvForm.depart.focus();
			return;
	/*
	 *   end dob validation
	 *
	*/ 
	
	//} else if ( (emvForm.depart_d.value =="")&&((emvForm.depart_m.value =="")&&(emvForm.depart_y.value =="")) ) {
		//alert("Please select a day, a month and a year of your departure date.");	  
		//return;
	} else if ((emvForm.return_m.options[2].selected)&&(emvForm.return_d.value > 29)) {
		alert("The month of February has:\n    - 28 days on non-leap years\n    - 29 days on leap years.");
		emvForm.return_d.focus();
		return;
	} else if ((emvForm.return_m.options[2].selected)&&(emvForm.return_d.value == 29)) {
		if ((emvForm.return_y.value % 4) != 0) {
			alert("The month of February has 28 days for the year selected (non-leap year).");
			emvForm.return_d.focus();
			return;
		}
	} else if ( ((emvForm.return_m.options[4].selected)||(emvForm.return_m.options[6].selected)||(emvForm.return_m.options[9].selected)||(emvForm.return_m.options[11].selected))&&(emvForm.return_d.options[31].selected)) {
			var finalMonth;
			var theMonth = document.emvForm.return_m.options[document.emvForm.depart_m.selectedIndex].value;
			switch (theMonth) {
				case '04': finalMonth='The month of April';
				break;
				case '06': finalMonth='The month of June';
				break;
				case '09': finalMonth='The month of September';
				break;
				case '11': finalMonth='The month of November';
				break;
				default: finalMonth='The selected month';
			}
			alert(finalMonth + ' has only 30 days.');
			emvForm.depart.focus();
			return;
	/*
	 *   end dob validation
	 *
	*/ 

	} else {
			emvForm.action="http://as1.emv2.com/D";
			emvForm.method="GET";
			emvForm.target="_top";
			emvForm.submit();
			//}
		
	}
}

