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

	// Text field validator
	String.prototype.trim = function() { return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");	}
	
	function mandatoryText(input) {
		if(input.value.trim()=="" || input==null) {
			return false;
		}
		else {
			return true;
		}
	}
	
	
/*
 * Functions for the Comments Box
 *
*/
  
	// Converts carriage returns to spaces
	function convertReturns(input) {
		var output = "";
		for (var i = 0; i < input.length; i++) {
			if((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) {
			i++;
			output += " ";
			}
			else {
			output += input.charAt(i);
  	 		}
		}
		return output;
	}

	// Text counter function for the Comments Box
	function textCounter(field, countfield, maxlimit) {
		if(field.value.length > maxlimit) {
			field.value = field.value.substring(0, maxlimit);
		}
		else {
			countfield.value = maxlimit - field.value.length;
		}
	}

	/*
	 * 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|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)$/;
		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("La syntaxe de votre email ne semble pas correcte.");
			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("Votre email comporte des caracteres invalides\n.");
					emailAddress.focus();
					return false;
				}
			}
			for (i=0; i<domain.length; i++) {
				if(validCharset.indexOf(domain.charAt(i))==-1) {
					alert("Votre email comporte des caracteres invalides\n.");
					emailAddress.focus();
					return false;
				}
			}
			if(user.match(validUser)==null) {
				alert("La syntaxe de votre email ne semble pas correcte.");
				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("La syntaxe de votre email ne semble pas correcte.");
					emailAddress.focus();
					return false;
				}
			}
			if((domArr[domArr.length-1].length==2)&&(domArr[domArr.length-1].search(countryTLDs)==-1)) {
					alert("La syntaxe de votre email ne semble pas correcte.");
					emailAddress.focus();
					return false;
			}
			if((domArr[domArr.length-1].length>2)&&(domArr[domArr.length-1].search(gTLDs)==-1)) {
				alert("La syntaxe de votre email ne semble pas correcte.");
				emailAddress.focus();
				return false;
			}
			if((domArr[domArr.length-1].length<2)||(domArr[domArr.length-1].length>6)) {
				alert("La syntaxe de votre email ne semble pas correcte.");
				emailAddress.focus();
				return false;
			}
			if(len<2) {
				alert("La syntaxe de votre email ne semble pas correcte.");
				emailAddress.focus();
				return false;
			}
			return true;
		}
	}

function mandatoryCombo(dropdown) {
		if(dropdown.options[0].selected) {
			if(fieldName != "") {
				alert("Merci de vous abonner au Fil de l'actu.");
			}
			dropdown.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;
		var errors='';
		
	//abo_lettre_field
	if ((!emvForm.abo_enews_field.checked)&&(!emvForm.ABO_LETTRE2_FIELD.checked))
	{
		alert("Merci de cocher les  cases pour choisir ce que vous voulez recevoir.")
		return false;
	}
	
	//TITLE
	if(emvForm.TITLE_FIELD.options[emvForm.TITLE_FIELD.selectedIndex].value =="")
		{
			alert("Merci de nous indiquer votre civilité");
			emvForm.TITLE_FIELD.focus();
			return false;
		}
		
	//LASTNAME
	if(emvForm.LASTNAME_FIELD.value=="")
		{
				alert("Merci de nous indiquer votre nom.\n");
				emvForm.LASTNAME_FIELD.focus();
				return false;
		 }
		 
	//FIRSTNAME
	if(emvForm.FIRSTNAME_FIELD.value=="")
		{
				alert("Merci de nous indiquer votre prénom.\n");
				emvForm.FIRSTNAME_FIELD.focus();
				return false;
		 }
		 
	// EMAIL 
	if(!mandatoryText(emvForm.EMAIL_FIELD))
	{
				alert("Merci d'indiquer votre email.\n");
				emvForm.EMAIL_FIELD.focus();
				return false;
	} else {
			if(!isEmail(emvForm.EMAIL_FIELD))
			{ 
				//alert("Please enter a correct e-mail address.\n");
				emvForm.EMAIL_FIELD.focus();
				return false;
			}
	}
	
	
	// CP
	if(!mandatoryText(emvForm.CODEPOSTAL_FIELD))
		{
				alert("Merci d'indiquer votre code postal.\n");
				emvForm.CODEPOSTAL_FIELD.focus();
				return false;
		}
		
	//PAYS CODEPOSTAL_FIELD
	if(emvForm.PAYS_FIELD.options[emvForm.PAYS_FIELD.selectedIndex].value =="")
		{
			alert("Merci d'indiquer votre pays.");
			emvForm.PAYS_FIELD.focus();
			return false;
		}
	else {
		if (emvForm.PAYS_FIELD.options[emvForm.PAYS_FIELD.selectedIndex].value =="France")
		{
			emvForm.france_field.value = 1;
			emvForm.domtom_field.value = 0;
			emvForm.monde_field.value = 0;
		}
		else {
			if ((emvForm.PAYS_FIELD.options[emvForm.PAYS_FIELD.selectedIndex].value.trim() =="Guadeloupe" )
			|| (emvForm.PAYS_FIELD.options[emvForm.PAYS_FIELD.selectedIndex].value.trim() =="Guyane francaise" )
			|| (emvForm.PAYS_FIELD.options[emvForm.PAYS_FIELD.selectedIndex].value.trim() =="Martinique" )
			|| (emvForm.PAYS_FIELD.options[emvForm.PAYS_FIELD.selectedIndex].value.trim() =="Reunion" )
			|| (emvForm.PAYS_FIELD.options[emvForm.PAYS_FIELD.selectedIndex].value.trim() =="Polynesie française" )
			|| (emvForm.PAYS_FIELD.options[emvForm.PAYS_FIELD.selectedIndex].value.trim() =="Saint-Pierre-et-Miquelon"))
			{
				emvForm.france_field.value = 0;
				emvForm.domtom_field.value = 1;
				emvForm.monde_field.value = 0;
			}
			else {
				emvForm.france_field.value = 0;
				emvForm.domtom_field.value = 0;
				emvForm.monde_field.value = 1;

			}
		}
	}
	}

/*
 *
 * End of the Validation function
 *
*/