/*
*
* fonction vérifiant le bon format des donnés
*
*/

/*
* fonction
*/
function defaultErreur(th, vdefault, erreurMessage){
	if (th.value != ""){
		alert(erreurMessage);
	}
	th.value = vdefault;
	return true;
}

/*
*  verifier que this .value est un int
*/
function vint(th){
	if (!th.value){
		return true;
	}
	var v =th.value;
	var z = "";
	for(var i =  0; i < v.length; i++){
		var c = v.charCodeAt(i);
		if(c >= "0".charCodeAt() && c <= "9".charCodeAt())
			z += v.charAt(i);
	}
	//if(z == "") z=0;
	th.value = z;
	return true;
}

/*
*  verifier que this .value est un int
*/
function vintsigne(th){
	if (!th.value){
		return true;
	}
	var v =th.value;
	var z = "";
	for(var i = 0; i < v.length; i++){
		var c = v.charCodeAt(i);
		if((c >= "0".charCodeAt() && c <= "9".charCodeAt()) || (c == "-".charCodeAt() && i == 0))
			z += v.charAt(i);
	}
	//if(z == "") z=0;
	th.value = z;
	return true;
}

/*
*  verifier que this .value est un double
*/
function vdouble(th){
	if (!th.value){
		return true;
	}
	var v = th.value;
	var z = "";
	for(var i =  0; i < v.length; i++){
		var c = v.charCodeAt(i);
		if((c >= "0".charCodeAt() && c <= "9".charCodeAt()) || c == ",".charCodeAt() || c == ".".charCodeAt()|| c == "-".charCodeAt())
			z += v.charAt(i);
	}
	var d = parseFloat(z);
	if(isNaN(d))
		 return defaultErreur(th, 0, "Rentrez svp un nombre.");
	th.value = d;
	return true;
}

/*
*  verifier que this .value est une date xx/xx/xx[xx]
*
*/
function vdate(th){
	dateStr = th.value;
	var defaultvalue = "";
    var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        return defaultErreur(th, "", "Rentrez svp une date au format jj/mm/aaaa");
	if(isNaN(i))
		return defaultErreur(th, "", "Rentrez svp une date au format jj/mm/aaaa");
    }
    day = matchArray[1]; // parse date into variables
    month = matchArray[3];
    year = matchArray[5];
    if (month < 1 || month > 12) { // check month range
        return defaultErreur(th, "", "Le mois est incorrect");
    }
    if (day < 1 || day > 31) {
        return defaultErreur(th, "", "Le jour est incorrect");
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		 return defaultErreur(th, "", "Le jour est incorrect");
    }
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
             return defaultErreur(th, "", "Le mois est incorrect");
        }
    }
    return true; // date is valid
}
function vtime(th){
	timeStr = th.value;
	var defaultvalue = "";
    var timePat = /^(\d{1,2})(\:)(\d{1,2})$/;
    var matchArray = timeStr.match(timePat); // is the format ok?

    if (matchArray == null) {
        return defaultErreur(th, "", "Rentrez svp une heure au format hh:mm");
	if(isNaN(i))
		return defaultErreur(th, "", "Rentrez svp une heure au format hh:mm");
    }
    hour = matchArray[1]; // parse date into variables
    minute = matchArray[3];
    
    if (hour < 0 || hour > 23) { // check month range
        return defaultErreur(th, "", "L' heure est incorrecte");
    }
    if (minute < 0 || minute > 59) {
        return defaultErreur(th, "", "Les minutes sont incorrectes");
    }
    return true; // date is valid
}

/*
* vérifie que le champ est bien un tel
* vieille fonction utiliser vtel à la place 
*/
function vtel2(th){
	var v = th.value;
	var z = "";
	for(var i =  0; i < v.length; i++){
		var c = v.charCodeAt(i);
		if((c >= "0".charCodeAt() && c <= "9".charCodeAt()) || c == "(".charCodeAt() || c == ".".charCodeAt() || c == ")".charCodeAt() || c == "+".charCodeAt()|| c == " ".charCodeAt())
			z += v.charAt(i);
	}
	th.value = z;
	var tel = z;
    var telPat = /^(\d{2}).(\d{2}).(\d{2}).(\d{2}).(\d{2})$/;
    var matchArray = tel.match(telPat); // is the format ok?

    if (matchArray == null) {
		 telPat = /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/;
    	 matchArray = tel.match(telPat); // is the format ok?
		 if (matchArray == null) {
       	   // return defaultErreur(th, z, "Rentrez svp un numéro de téléphone valide");
		 } else {
		 	th.value = tel.substring(0,2) + "." +tel.substring(2,4) + "." +tel.substring(4,6) + "." +tel.substring(6,8) + "." +tel.substring(8,10);
		 }
    }
	return true;
}

function vtel(champ){
	
	var v = champ.value;
	var tampon = "";
	var flagPlus=0;
	if (v!="") {
		for (var i = 0; i<v.length; i++)
		{
			var c = v.charCodeAt(i);
			if ( (c >=48 && c <=57) || (c=="+".charCodeAt() ) ) 
			{
				if ((c!="+".charCodeAt()) || ((c=="+".charCodeAt())&& (flagPlus<1) && (v.indexOf("+")==0) ) )
				{
					tampon += v.charAt(i);
				}
				if (c=="+".charCodeAt()) 
				{
					flagPlus++;
				}	
			}
			
		}
		// tampon n'est constitué que de chiffres et éventuellement d'un plus +
		// on l'affiche dans le champ
		champ.value = tampon;
		var tel=tampon;
	       	   // on écrit le numéro selon le format (+11)1.11.11.11.11 ou 11.11.11.11.11
		if (champ.value.length<10) 
		{
			alert("Ce champ doit comporter 10 caractères au minimum");
			champ.value="";
			champ.focus();
		}
		else 
		{
			var longueur=champ.value.length;
			var telFinal="";
			if ((champ.value.indexOf("+")==-1) && (eval((champ.value.length/2)-parseInt(champ.value.length/2))==0)) {
				while (longueur>2) 
				{
					telFinal = "."+tel.substring(longueur-2,longueur) +telFinal;
					longueur=longueur-2;
				}
				telFinal=tel.substring(0,2)+telFinal;
			}
			else {
				while (longueur>4) 
				{
					telFinal = "."+tel.substring(longueur-2,longueur) +telFinal;
					longueur=longueur-2;
				}
				telFinal=tel.substring(0,longueur)+telFinal;	
			}
	    		champ.value=telFinal;
	    	}
		return true;
	}

}
function vcodecli(champ){
	if (champ.value.length<6 && champ.value!=""){
		alert("Ce champ doit comporter 6 caractères");
		champ.value="";
		champ.focus();
	}
}
function vnumadr(champ){
	if (champ.value.length<2 && champ.value!=""){
		alert("Ce champ doit comporter 2 caractères");
		champ.value="";
		champ.focus();
	}
}
function vfax(champ){	
	var v = champ.value;
	var tampon = "";
	var flagPlus=0;
	if (v!="") {
		for (var i = 0; i<v.length; i++)
		{
			var c = v.charCodeAt(i);
			if ( (c >=48 && c <=57) || (c=="+".charCodeAt() ) || (c=="Z".charCodeAt()) || (c=="z".charCodeAt())) 
			{
				if ((c!="+".charCodeAt()) || ((c=="+".charCodeAt())&& (flagPlus<1) && (v.indexOf("+")==0) ) )
				{
					tampon += v.charAt(i);
				}
				if (c=="+".charCodeAt()) 
				{
					flagPlus++;
				}	
			}
			
		}
		// tampon n'est constitué que de chiffres et éventuellement d'un plus +
		// on l'affiche dans le champ
		champ.value = tampon;
		var tel=tampon;
	       	   // on écrit le numéro selon le format (+11)1.11.11.11.11 ou 11.11.11.11.11
		if (champ.value.length<10) 
		{
			alert("Ce champ doit comporter 10 caractères au minimum");
			champ.value="";
			champ.focus();
		}
		else 
		{
			var longueur=champ.value.length;
			var telFinal="";
			if ((champ.value.indexOf("+")==-1) && (eval((champ.value.length/2)-parseInt(champ.value.length/2))==0)) {
				while (longueur>2) 
				{
					telFinal = "."+tel.substring(longueur-2,longueur) +telFinal;
					longueur=longueur-2;
				}
				telFinal=tel.substring(0,2)+telFinal;
			}
			else {
				while (longueur>4) 
				{
					telFinal = "."+tel.substring(longueur-2,longueur) +telFinal;
					longueur=longueur-2;
				}
				telFinal=tel.substring(0,longueur)+telFinal;	
			}
	    		champ.value=telFinal;
	    	}
		return true;
	}

}



/*
* vérifie que le champ est bien un fax
* vieille fonction utiliser vfax à la place 
*/
function vfax2(th){
	var v = th.value;
	var z = "";
	for(var i =  0; i < v.length; i++){
		var c = v.charCodeAt(i);
		if((c >= "0".charCodeAt() && c <= "9".charCodeAt()) || c == "(".charCodeAt() || c == ".".charCodeAt() || c == ")".charCodeAt() || c == "+".charCodeAt()|| c == " ".charCodeAt() || c == "Z".charCodeAt() || c == "z".charCodeAt())
			z += v.charAt(i);
	}
	th.value = z;
	var tel = z;
    var telPat = /^(\d{2}).(\d{2}).(\d{2}).(\d{2}).(\d{2})$/;
    var matchArray = tel.match(telPat); // is the format ok?

    if (matchArray == null) {
		 telPat = /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/;
    	 matchArray = tel.match(telPat); // is the format ok?
		 if (matchArray == null) {
       	   // return defaultErreur(th, z, "Rentrez svp un numéro de fax valide");
		 } else {
		 	th.value = tel.substring(0,2) + "." +tel.substring(2,4) + "." +tel.substring(4,6) + "." +tel.substring(6,8) + "." +tel.substring(8,10);
		 }
    }
	return true;
}


/*
* vérifie que le champ est bien un mail
*/
function vmail(th){
	var tel = th.value;
	//var telPat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var mail = /[-a-zA-Z_.0-9]+@[-a-zA-Z_.0-9]+[.]\w{2,3}$/
    var matchArray = tel.match(mail); // is the format ok?
    if (matchArray == null) {
        return defaultErreur(th, "", "Rentrez svp une adresse mail valide");
    }
	return true;
}

/*
* vérifie que le champ est bien un code naf
*/
function vnaf(th){
	var v = th.value;
	if (v==""){
		return true;
	}else if (v=="MMMM"){
		return true;
	}else if (v=="ZZZZ"){
		return true;
	}else if (v=="XXXX"){
		return true;
	}else{
		var naf = /^(\d{1,3})[a-zA-Z]$/
		var matchArray = v.match(naf);
    	if (matchArray == null) {
        	return defaultErreur(th, "", "Rentrez svp un code NAF valide");
   		}
		th.value = v.toUpperCase();
		return true;
	}
}

function vsiret(th){
	var v = th.value;
	if (v==""){
		return true;
	}else if (v=="**************"){
		return true;
	}else if (v=="MMMMMMMMMMMMMM"){
		return true;
	}else if (v=="ZZZZZZZZZZZZZZ"){
		return true;
	}else if (v=="XXXXXXXXXXXXXX"){
		return true;
	}else{
		vint(th);
		if (v.length!=14){
			alert("Le numéro de Siret doit comporter 14 caractères !");
			th.focus();
		}
		return true;
	}
	
	
}
/*
* vérifie que this est un int, et soit superieur a v
*/
function vintMin(th, v){
var i = parseInt(th.value);
	if(isNaN(i))
		 i = 0;
	if( i < v)
		i = v;
	th.value = i;
}


/*
* vérifie que this est un int, et soit inferieur a v
*/
function vintMax(th, v){
	var i = parseInt(th.value);
	if(isNaN(i))
		 i = 0;
	if( i > v)
		i = v;
	th.value = i;
}

/*
* vérifie que this est un int, et soit entre vmin et vmax
*/
function vintEntre(th, vmin, vmax){
	if (!th.value){
		return true;
	}
	var i = parseInt(th.value);
	var correct=1;
	if(isNaN(i)){
		alert("Rentrez svp un nombre.");
		th.value = 0;
		return false;
	}
	if( i < vmin){
		i = vmin;
		correct=0;
	}
	if( i > vmax){
		i = vmax;
		correct=0;
	}
	if (correct==0){
		th.value = i;
		return false;
	}
	return true;
}



/*
* vérifie que this est un double, et soit superieur a v
*/
function vdoubleMin(th, v){
	var i = parseFloat(th.value);
	if(isNaN(i))
		 i = 0;
	if( i < v)
		i = v;
	th.value = i;
}


/*
* vérifie que this est un double, et soit infereiru a v
*/
function vdoubleMax(th, v){
	if (!th.value){
		return true;
	}
	var i = parseFloat(th.value);
	if(isNaN(i)){
		alert("Rentrez svp un nombre.");
		th.value = 0;
		return false;
	}
	if( i > v){
		i = v;
		th.value = i;
		return false;
	}
	return true;
	
}

/*
* vérifie que this est un double, et soit entre vmin et vmax
*/
function vdoubleEntre(th, vmin, vmax){
	var i = parseFloat(th.value);
	if(isNaN(i))
		 i = 0;
	if( i < vmin)
		i = vmin;
	if( i > vmax)
		i = vmax;
	th.value = i;
}

// verifie la longueur du champ
function vLongueur(champ,longueur){
	if (champ.value){
		if (champ.value.length>longueur){
			champ.value=champ.value.substring(0,longueur);
			return false;
		}
	}
	return true;
}

// vérifie que le champ est bien un réel et dont la précision n'est pas plus grande que 
// celle fournie en argument 
function vdoublePrecision(champ,precision){
	if (vdouble(champ)){
		var valeur=champ.value;
		var valeur=champ.value;
		var p=Math.pow(10,precision);
		var	arrondi=Math.round(p*valeur)/p;
		if (valeur!=arrondi){
			champ.value=arrondi;
			return false;
		}
		return true;
	}
	return false;
}


function vdoubleMaxPrecision(champ,max,precision){
	if (vdoubleMax(champ,max)){
		var valeur=champ.value;
		var p=Math.pow(10,precision);
		var	arrondi=Math.round(p*valeur)/p;
		if (valeur!=arrondi){
			champ.value=arrondi;
			return false;
		}
		return true;
	}
	return false;
}

function vMoney(champ){
	return vdoublePrecision(champ,2);
}

function vMoneyMax(champ,max){
	return vdoubleMaxPrecision(champ,max,2);
}

function vPrixCopie(champ){
	return vdoublePrecision(champ,5);
}

function vPrixCopieMax(champ,max){
	return vdoubleMaxPrecision(champ,max,5);
}

/* même fonctions mais sur le onkeypress*/
// appeler en plus les fonctions ci-dessus sur le onBlur, attention, bien mettre onBlur et pas onChange
function verifieLongueur(champ,longueur){
	if (champ.value.length>=longueur){
		return false;
	}
	return true;
}

//longueur facultative
function verifieNumerique(champ,longueur){
	var c = window.event.keyCode;
	
	if (c >=48 && c <=57){
		if (longueur){
			return verifieLongueur(champ,longueur);
		}
		return true;
	}
	return false;
}
function verifieNumeriqueR(champ,longueur){
	var c = window.event.keyCode;
	
	if ((c >=48 && c <=57) || c ==42){
		if (longueur){
			return verifieLongueur(champ,longueur);
		}
		return true;
	}
	return false;
}
//longueur facultative
function verifieNumeriqueSigne(champ,longueur){
	var c = window.event.keyCode;
	if ((c >=48 && c <=57)||(c==45)){
		if (c==45){
			if (champ.value.indexOf("-")!=-1){
				return false;
			}
		}
		if (longueur){
			return verifieLongueur(champ,longueur);
		}
		return true;
	}
	return false;
}

//longueur facultative
function verifieReel(champ,longueur){
	var c = window.event.keyCode;
	if ((c >=48 && c <=57)||(c==46)){
		if (c==46){
			if (champ.value.indexOf(".")!=-1){
				return false;
			}
		}
		if (longueur){
			return verifieLongueur(champ,longueur);
		}
		return true;
	}
	return false;
}

//longueur facultative
function verifieReelSigne(champ,longueur){
	var c = window.event.keyCode;
	if ((c >=48 && c <=57)||(c==46)||(c==45)){
		if (c==46){
			if (champ.value.indexOf(".")!=-1){
				return false;
			}
		}
		if (c==45){
			if (champ.value.indexOf("-")!=-1){
				return false;
			}
		}

		if (longueur){
			return verifieLongueur(champ,longueur);
		}
		return true;
	}
	return false;
}

//longueur obligatoire
function verifieNom(champ,longueur){
	var c = window.event.keyCode;

	if ((c==32)||(c==39)||(c==45)||(c==46)||(c==128)||(c==224)||(c==226)||((c>=231)&&(c<=235))||(c==238)||(c==239)||(c==244)||(c==246)||(c==249)||(c==251)||(c==252)||((c>=65)&&(c<=90))||((c>=97)&&(c<=122))){
		// espace, apostrophe, tiret, point,ä,à,â,ç,è,é,ê,ë,î,ï,ô,ö,ù,û,ü,majuscules, minuscules
		return true;
	}
	return false;
}

//longueur obligatoire
function verifieAlphaNumerique(champ,longueur){
	var c = window.event.keyCode;
	if (verifieLongueur(champ,longueur)){
		// chiffres, majuscules, minuscules
		return ((c >=48 && c <=57) ||((c>=65)&&(c<=90))||((c>=97)&&(c<=122)));
	}
	return false;
}

//longueur obligatoire
function verifieEmail(champ,longueur){
	var c = window.event.keyCode;
	if (verifieLongueur(champ,longueur)){
		// chiffres, majuscules, minuscules, ., @
		return ((c >=48 && c <=57) ||((c>=65)&&(c<=90)) || ((c>=97)&&(c<=122)) || (c==46) || (c==64)|| (c==95));
	}
	return false;
}

//verifie qu'on ne puisse taper que des + et des chiffres pour un téléphone
function verifieTel(champ){
	var c = window.event.keyCode;
	if (c=="+".charCodeAt()&& champ.value==""){
		if (champ.value.indexOf("+")!=-1){
			return false;
		}
	}else if(c=="+".charCodeAt()) {
		return false;
	}
	if (champ.value.indexOf("+")!=-1 && verifieLongueur(champ,12) ){
		return ((c >=48 && c <=57) || (c=="+".charCodeAt()));
	}else if (verifieLongueur(champ,10)){
		// chiffres, .
		return ((c >=48 && c <=57) || (c=="+".charCodeAt()));
	}
	return false;
}

function verifieFax(champ){
	var c = window.event.keyCode;
	if (c=="+".charCodeAt()&& champ.value==""){
		if (champ.value.indexOf("+")!=-1){
			return false;
		}
	}else if(c=="+".charCodeAt()) {
		return false;
	}
	if (champ.value.indexOf("+")!=-1 && verifieLongueur(champ,12) ){
		return ((c >=48 && c <=57) || (c=="+".charCodeAt()) || (c=="Z".charCodeAt()) || (c=="z".charCodeAt()));
	}else if (verifieLongueur(champ,10)){
		// chiffres, .
		return ((c >=48 && c <=57) || (c=="+".charCodeAt()) || (c=="Z".charCodeAt()) || (c=="z".charCodeAt()));
	}
	return false;
}


// appeler vdate(champ) sur le onblur en +
function verifieTime(champ){
	var c = window.event.keyCode;
	valeur=champ.value;
	if (c==58){
		var index=valeur.indexOf(":");
		if (index!=-1){
			if (valeur.length<5){
				return false;
			}
			else{
				if (valeur.substring(index+1).indexOf(":")!=-1){
					return false;
				}
			}
		}
		return true;	
	}
	if(valeur.length==0){
		return ((c>=48)&&(c<=50));
	}
	
	if (valeur.length==1){
		if ((c>=48)&&(c<=57)){
			nvelleValeur=valeur+String.fromCharCode(c);
			if (isNaN(parseInt(nvelleValeur))){
				return false;
			}
			else{
				if ((eval(nvelleValeur)>0)&&(eval(nvelleValeur)<=23)){
					champ.value=nvelleValeur+':';
					return false;
				}
				return false;
			}
		}
		return false;
	}
	if (valeur.length==3){
		return ((c>=48)&&(c<=53));	
	}
	if (valeur.length==4){
		return ((c>=48)&&(c<=57));	
	}	
	if (valeur.length>=6){
		return false;
	}
	
	return ((c>=48)&&(c<=57));
}
function verifieDate(champ){
	var c = window.event.keyCode;
	valeur=champ.value;
	if (c==47){
		var index=valeur.indexOf("/");
		if (index!=-1){
			if (valeur.length<5){
				return false;
			}
			else{
				if (valeur.substring(index+1).indexOf("/")!=-1){
					return false;
				}
			}
		}
		return true;	
	}
	if(valeur.length==0){
		return ((c>=48)&&(c<=51));
	}
	
	if (valeur.length==1){
		if ((c>=48)&&(c<=57)){
			nvelleValeur=valeur+String.fromCharCode(c);
			if (isNaN(parseInt(nvelleValeur))){
				return false;
			}
			else{
				if ((eval(nvelleValeur)>0)&&(eval(nvelleValeur)<=31)){
					champ.value=nvelleValeur+'/';
					return false;
				}
				return false;
			}
		}
		return false;
	}
	if (valeur.length==3){
		return ((c>=48)&&(c<=49));	
	}
	if (valeur.length==4){
		if ((c>=48)&&(c<=57)){
			nvelleValeur=valeur.charAt(3)+String.fromCharCode(c);
			if (isNaN(parseInt(nvelleValeur))){
				return false;
			}
			else{
				if ((eval(nvelleValeur)>0)&&(eval(nvelleValeur)<=12)){
					champ.value=valeur.substring(0,3)+nvelleValeur+'/';
					return false;
				}
				return false;
			}
		}
		return false;
	}
	if (valeur.length==6){
		return ((c>=49)&&(c<=50));	
	}
	if (valeur.length==7){
		if (valeur.charAt(6)==1){
			return (c==57);
		}
		else{
			return (c==48);
		}
	}
	
	if (valeur.length>=10){
		return false;
	}
	
	return ((c>=48)&&(c<=57));
}

function verifieSiret(champ){
	var c = window.event.keyCode;
	valeur=champ.value;
	
	// on met 14 * si une est tapée comme 1er caractère
	if ((valeur.length==0)&&(c == "*".charCodeAt())){
		champ.value="*************";
		return true;
	}else if ((valeur.length==0)&&(c == "M".charCodeAt())){
		champ.value="MMMMMMMMMMMMM";
		return true;
	}else if ((valeur.length==0)&&(c == "Z".charCodeAt())){
		champ.value="ZZZZZZZZZZZZZ";
		return true;
	}else if ((valeur.length==0)&&(c == "X".charCodeAt())){
		champ.value="XXXXXXXXXXXXX";
		return true;
	}
	return verifieNumerique(champ,14);
}


function verifieNaf(champ){
	var c = window.event.keyCode;
	valeur=champ.value;
	
	// on met 14 * si une est tapée comme 1er caractère
	if ((valeur.length==0)&&(c == "M".charCodeAt())){
		champ.value="MMM";
		return true;
	}else if ((valeur.length==0)&&(c == "Z".charCodeAt())){
		champ.value="ZZZ";
		return true;
	}else if ((valeur.length==0)&&(c == "X".charCodeAt())){
		champ.value="XXX";
		return true;
	}
	return verifieAlphaNumerique(champ,4);
}
