/*
 * 
 * E-Mail Vision Generic Join Webform Validation JavaScript - (c) 2002
 * 
 *
*/ 

	var emvForm = document.emvForm ;

	// Script Pop Up Window
	function popupWindow(theURL,winName,features) {
		window.open(theURL,winName,features);
	}


	// This function retrieves the values of the DOB drop down lists and concatenate them into the 'DOB_Name' hidden field
	function makeDate(dob_m, dob_d, dob_y) {
		if  ( (document.emvForm.dob_m.options[document.emvForm.dob_m.selectedIndex].value != '')&&(document.emvForm.dob_d.options[document.emvForm.dob_d.selectedIndex].value != '')&&(document.emvForm.dob_y.options[document.emvForm.dob_y.selectedIndex].value != '') ) {
			var month = String(document.emvForm.dob_m.options[document.emvForm.dob_m.selectedIndex].value);
			var day = String(document.emvForm.dob_d.options[document.emvForm.dob_d.selectedIndex].value);
			var year = String(document.emvForm.dob_y.options[document.emvForm.dob_y.selectedIndex].value);
			var emvDob =	month + '/' + day + '/' + year;
			}
		else {
			var emvDob = '';
		}
		return emvDob;
	}

	// This 'function' creates a trim() function
	String.prototype.trim = function() { return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");	}
	
		// This function controls that a text field is filled in
	function mandatoryText(input, name) {
		if (input.value.trim() == "" || input == null) {
			alert("Veuillez indiquer votre " + name + ".");
			input.focus();
			return false;
		}
		else {
			return true;
		}
	}

	// Drop Down Lists validator	
	function mandatoryCombo(dropdown, msg) {
		if (dropdown.options[0].selected) {
			alert(msg);
			dropdown.focus();
			return false;
		}
		return true;
	}

	// This function validates radio buttons arrays
	function mandatoryRadio(radioList, radioAlert) {
		var radioValue = null;
		for (var i=0; i < radioList.length; i++) {
			if (radioList[i].checked) {
				radioValue = radioList[i].value;
				break;
			}
		}
		if (radioValue == null) {
			alert(radioAlert);
			radioList[0].focus();
			return false;
		}
		else {
			return true;
	}
}

	/*
	 * This function validates the email address syntax and characters
	 * It also checks that the TLD is a 2 country code and if 3 or more, it ensures that it is in the Known Domains
	*/
	
	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("L'addresse email indiquée semble incorrecte. Veuillez vérifier la syntaxe.");
			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("L'addresse email indiquée contient des caractères invalides.");
					emailAddress.focus();
					return false;
				}
			}
			for (i=0; i<domain.length; i++) {
				if(validCharset.indexOf(domain.charAt(i))==-1) {
					alert("L'addresse email indiquée contient des caractères invalides.");
					emailAddress.focus();
					return false;
				}
			}
			if (user.match(validUser)==null) {
				alert("L'addresse email indiquée semble incorrecte.");
				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("L'addresse email indiquée semble incorrecte.");
					emailAddress.focus();
					return false;
				}
			}
			if ((domArr[domArr.length-1].length==2)&&(domArr[domArr.length-1].search(countryTLDs)==-1)) {
					alert("L'addresse email indiquée semble incorrecte.");
					emailAddress.focus();
					return false;
			}
			if ((domArr[domArr.length-1].length>2)&&(domArr[domArr.length-1].search(gTLDs)==-1)) {
				alert("L'addresse email indiquée semble incorrecte.");
				emailAddress.focus();
				return false;
			}
			if ((domArr[domArr.length-1].length<2)||(domArr[domArr.length-1].length>6)) {
				alert("L'addresse email indiquée semble incorrecte.");
				emailAddress.focus();
				return false;
			}
			if (len<2) {
				alert("L'addresse email indiquée semble incorrecte.");
				emailAddress.focus();
				return false;
			}
			return true;
		}
	}

	// Controls the invalid characters in the mobile phone string
	var TelNumAuto = new String('0123456789');
	function TelCharCtrl ( In, TelNumAuto ){
		for(i=0; i< In.value.length; i++){
			if( TelNumAuto.indexOf(In.value.charAt(i)) == -1 ) {
				In.focus();
				return false;
			}
		}
		return true;
	}

/*
 *
 * This function validates the form fields
 * Use the functions above to make the relevant
 * fields required
 *
*/

	// Submit function
	function submitForTreatment() {

    var emvForm = document.emvForm;

		
	// Assigning values to the checkboxes - returns 1 if checked and 0 if not checked
		if (!mandatoryCombo(emvForm.TITLE_FIELD, "Veuillez sélectionner votre Civilité")) {
			return;
		}
		if (!mandatoryText(emvForm.LASTNAME_FIELD, "Nom")) {
			return;
		}
		if (!mandatoryText(emvForm.FIRSTNAME_FIELD, "Prénom")) {
			return;
		}
		if (!(mandatoryText(emvForm.EMAIL_FIELD, "E-Mail address"))) {
			return;
		}
		if (!(isEmail(emvForm.EMAIL_FIELD))) {
			return;
		}
		if (!mandatoryText(emvForm.POSTCODE_FIELD, "Code Postal")) {
			return;
		}
	

				else {
					emvForm.action="http://ase.emv3.com/D";
					emvForm.method="GET";
					emvForm.target="_top";
					emvForm.submit();
				}
			}

	
	

/*
 *
 * End of the Validation function
 *
*/