function clear(number){
	 if ($('parrainage'+number).value=="Email de votre ami(e) "+number){
		$('parrainage'+number).value="";
	}
}


function full(number){
	if ($('parrainage'+number).value==""){
		$('parrainage'+number).value="Email de votre ami(e) "+number;
	}
}

// Ouvre une popup	

function popup(url, nomFen){
	window.open(url,nomFen,'fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=550,height=550,top=0,left=0,true');
}

//Vérifie que l'email entré est valable

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)$/;
	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('L\'adresse Email semble incorrecte,\nveuillez 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\'adresse Email contient des caractères invalides,\nveuillez vérifier la partie avant l\'arobase.');
				emailAddress.focus();
				return false;
			}
		}
		for(i=0;i<domain.length;i++){
			if(validCharset.indexOf(domain.charAt(i))==-1){
				alert('L\'adresse Email contient des caractères invalides,\nveuillez vérifier la partie après l\'arobase.');
				emailAddress.focus();
				return false;
			}
		}
		if(user.match(validUser)==null){
			alert('L\'adresse Email semble incorrecte,\nveuillez vérifier la partie avant l\'arobase.');
			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\'adresse Email semble incorrecte,\nveuillez vérifier la partie après l\'arobase.');
				emailAddress.focus();
				return false;
			}
		}
		if((domArr[domArr.length-1].length==2)&&(domArr[domArr.length-1].search(countryTLDs)==-1)){
			alert('L\'adresse Email semble incorrecte,\nveuillez vérifier le suffixe du domaine.');
			emailAddress.focus();
			return false;
		}
		if((domArr[domArr.length-1].length>2)&&(domArr[domArr.length-1].search(gTLDs)==-1)){
			alert('L\'adresse Email semble incorrecte,\nveuillez vérifier le suffixe du domaine.');
			emailAddress.focus();
			return false;
		}
		if((domArr[domArr.length-1].length<2)||(domArr[domArr.length-1].length>6)){
			alert('L\'adresse Email semble incorrecte,\nveuillez vérifier le suffixe du domaine.');
			emailAddress.focus();
			return false;
		}
		if(len<2){
			alert('L\'adresse Email semble incorrecte.');
			emailAddress.focus();
			return false;
		}
	}
	return true;
}


// Vérifie que tous les champs requis du formulaire inscription sont remplis

function validForm_inscription(){
	var deb = "Vous devez remplir ";
	var fin = " pour pouvoir jouer.";
	
 	theone=-1;
	for (i=0;i<document.form_inscription.titre.length;i++){

		if (document.form_inscription.titre[i].checked==true)
			theone=i
	}
	if (theone==-1)
	{
	  alert("Vous devez renseigner votre civilité");
	   return false;
	}
 	if(document.getElementById("nom").value == ""){
   		alert(deb+"le champ NOM"+fin);
   		document.getElementById("nom").focus();
   		return false;
 	}
 	if(document.getElementById("prenom").value == ""){
 		alert(deb+"le champ PRENOM"+fin);
 		document.getElementById("prenom").focus();
   		return false;
 	}
	if(document.getElementById("email").value==""){
		alert(deb+"le champ EMAIL"+fin);
   		document.getElementById("email").focus();
   		return false;
	}	
	if(!isEmail(document.getElementById("email"))){
  		return false;
  	}
	
  	if(document.getElementById("reglement").checked == false){
  		alert("Vous devez accepter le règlement"+fin);
  		return false;
  	}
}



function verif_parr(){
	var deb = "Vous devez indiquer ";
	var fin = ".";
 	if((document.getElementById("parrainage1").value=="Email de votre ami(e) 1" || document.getElementById("parrainage1").value=="") && (document.getElementById("parrainage2").value=="Email de votre ami(e) 2" || document.getElementById("parrainage2").value=="") && (document.getElementById("parrainage3").value=="Email de votre ami(e) 3" || document.getElementById("parrainage3").value=="") ){
		alert(deb+"au moins UNE adresse mail"+fin);
   		document.getElementById("parrainage1").focus();
   		return false;
	}
	if(document.getElementById("parrainage1").value!="Email de votre ami(e) 1" && document.getElementById("parrainage1").value!=""){
		if(!isEmail(document.getElementById("parrainage1"))){
  			return false;
  		}
	}
	if(document.getElementById("parrainage2").value!="Email de votre ami(e) 2" && document.getElementById("parrainage2").value!=""){
		if(!isEmail(document.getElementById("parrainage2"))){
  			return false;
  		}
	}
	if(document.getElementById("parrainage3").value!="Email de votre ami(e) 3" && document.getElementById("parrainage3").value!=""){
		if(!isEmail(document.getElementById("parrainage3"))){
  			return false;
  		}
	}
}	


window.addEvent('domready', function() {
	if ($('form_inscription')) {
		$('form_inscription').addEvent('submit', function(e){
			return validForm_inscription();
		});
	}
	
	
	
	if ($('form-parrainage')) {
		$('parrainage1').addEvent('focus', function(e){
			return clear(1);
		});
		$('parrainage1').addEvent('blur', function(e){
			return full(1);
		});
		$('parrainage2').addEvent('focus', function(e){
			return clear(2);
		});
		$('parrainage2').addEvent('blur', function(e){
			return full(2);
		});
		$('parrainage3').addEvent('focus', function(e){
			return clear(3);
		});
		$('parrainage3').addEvent('blur', function(e){
			return full(3);
		});
		
		$('formulaire-parrainage').addEvent('submit', function(e){
			return verif_parr();
		});
	}
		
});


