// JavaScript Document
var ajaxReturn = "";
var ajaxReturn2 = "";
function validForm(myForm) {
	var toReturn = "";
    toReturn += verifNotNull(myForm);
    toReturn += verifPassword(myForm.mdp.value, myForm.mdpc.value, 6);
	toReturn += verifEmail(myForm.email);
	toReturn += verifDDN(myForm.ddn);
	if(myForm.email.value!=myForm.emailconf.value)
		toReturn += "- The e-Mails don't match.<br />";
	toReturn += ajaxReturn;
	toReturn += ajaxReturn2;
	if(toReturn.length > 0) 
	{
		var msg = '<p><u>The registration can’t be finalized : </u><br /><br />' + toReturn + '</p>';
		Modalbox.show('affichageLightBox.php', {title: ' ', method: 'get', params: {'msg': msg}, width: 660, height: 350});
		return false;
	}
	if(myForm.news.value == 'invalid') {
		Modalbox.show('affichageLightBoxNews.php', {title: ' ', method: 'post', params: {'form': myForm.id}, width: 660, height: 350});
		return false;
	}
	return true;
}


function verifEmail(email) {
	if(!email.value.match("^([a-z0-9]|\.|-)+@(([a-z0-9]|-)+\.)+[a-z]{2,4}$"))
		return "- " + email.id.replace(/_/g, ' ') + "  is not valid.<br />"; 
	return "";
}

function verifNotNull(myForm) {
	var toReturn = "";
	for(var i = 0; i < myForm.length ; i++) {
		if(myForm[i].value.length==0)
			toReturn += "- The field " + myForm[i].id.replace(/_/g, ' ') + " is empty.<br />";
    } 
	return toReturn;
}


function verifPassword(mdp1, mdp2, length) {
	var toReturn = "";
	if(mdp1!=mdp2)
		toReturn += "- The passwords don't match.<br />";
	if(mdp1.length < length)
   		toReturn += "- The chosen password has less than 6 characters.<br />"; 
	return toReturn;
}

function verifDDN(date) {
	if(!date.value.match("^([0-9]{2})/([0-9]{2})/([0-9]{4})$"))
		return "- The format of " + date.id.replace(/_/g, ' ') + " is not valid (dd/mm/yyyy).<br />"; 
	return "";
}


function verifParrain(parrain,myForm) {
	var url = 'parrain.php';
	new Ajax.Request(url, {
  	   method: 'post',
       parameters: {'code': encodeURIComponent(parrain.value)},
	   onSuccess: function(transport) {
		  if (transport.responseText == true)
				ajaxReturn = "";
		   else
				ajaxReturn = "- Sponsor code isn't valid.<br />";
		   	myForm[myForm.length - 1].disabled = false;
  	   }
	});
}

function verifKey(myForm, keyPressed) {
	if(keyPressed!=13 && keyPressed!=9)
		myForm[myForm.length - 1].disabled = true;
}


function validFormEnvoi(myForm) {
	var toReturn = '';
	var j=0;
	for(var i=0 ; i<(myForm.length-1) ; i++ ) {
		if( myForm[i].value.length > 0 )
			toReturn += verifEmail(myForm[i]);
		else 
			j++;
	}
	if( toReturn.length > 0 )
	{
		var msg;
		msg = "The message can’t be send : <br />" + toReturn;
		Modalbox.show('affichageLightBox.php', {title: ' ', method: 'get', params: {'msg': msg}, width: 660, height: 350});
		return false;
	}
	if(i==j)
		return false;	
	return true;
}

function blockAutoComplete(){
	if (document.getElementsByTagName) {
    	var inputElements = document.getElementsByTagName("input");
	    for (i=0; inputElements[i]; i++) {
    	  if (inputElements[i].className && (inputElements[i].className.indexOf("disableAutoComplete") != -1)) {
        		inputElements[i].setAttribute("autocomplete","off");
      		}
    	}
  	}
}


function emailTaken(email,myForm){
	var url = 'objects/user.php';
	new Ajax.Request(url, {
  	   method: 'post',
       parameters: {'emailAjax': encodeURIComponent(email.value)},
	   onSuccess: function(transport) {
		  if (transport.responseText == false)
				ajaxReturn2 = "";
		   else
				ajaxReturn2 = "- This e-mail address has already been registrated.<br />";
		   	myForm[myForm.length - 1].disabled = false;
  	   }
	});
}



function validFormContact(myForm){
	var toReturn = "";
	var msg = "<p> <u>The message can’t be send :</u> <br />";
	for(var i = 0; i < myForm.length ; i++) {
		if(myForm[i].id.match("^(.*)_obligatoire$") && myForm[i].value.length==0) {
			nom = myForm[i].id.replace(/_obligatoire/g, ' ');
			toReturn += "- The field " + nom.replace(/_/g, ' ') + " is empty.<br />";
		}
		else if(myForm[i].id=="e-mail_obligatoire") {
			msg += verifEmail(myForm[i]).replace(/ obligatoire/g, '');
		}
    } 
	if(toReturn.length > 0) {
		msg += toReturn + '</p>';
		Modalbox.show('affichageLightBox.php', {title: ' ', method: 'get', params: {'msg': msg}, width: 660, height: 350});
		return false;
	}
	return true;
}