function isEnterKey(evt)
{
	evt = (evt) ? evt : (window.event) ? window.event : "";

	var theKey ;
	if (evt)
	{
		theKey = (evt.which) ? evt.which : evt.keyCode ;
	}
	return (theKey == 13);

}

function fncWhenKeyPress(evt)
{
	if (isEnterKey(evt))
	{
		//document.Form1.submit();
		return true;
	}
	else
		return false ;
}


function popupcentree(page,titre,largeur,hauteur,options){
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	fen = window.open(page,titre,"top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
	if (self.focus) 
		fen.focus();
}





function averto(url)
{
	if (confirm('Confirmez-vous la suppression de cet enregistrement ?')){
		document.location.href = url;
	}
}


function delElement(type, id)
{
	if (type == 'pub'){
		if(confirm('Etes-vous sûr(e) de vouloir supprimer cette pub ?'))
		{
			document.location.href = 'edition-header.asp?act=del&idpub=' + id;
			document.location.href = 'edition-header.asp';
			//document.location.reload();
			alert("La pub a bien été supprimée");
		}
	}
	
	else if (type == 'compte'){
		if(confirm('Etes-vous sûr(e) de vouloir supprimer ce compte ?'))
		{				
			document.location.href = 'edition-compte.asp?act=del&idM=' + id;
			document.location.href = 'edition-compte.asp';
			//document.location.reload();
			alert("Le compte client a bien été supprimé");
		}	
	}
	else if (type == 'abo'){
	
		if (id == 1)
			alert("Vous ne pouvez pas supprimer l'abonnement d'origine.");
		else{
			if(confirm('Etes-vous sûr(e) de vouloir supprimer cet abonnement ?'))
			{				
				document.location.href = 'edition-abo.asp?act=del&idA=' + id;
				document.location.href = 'edition-abo.asp';
				//document.location.reload();
				alert("L'abonnement a bien été supprimé");
			}
		}
	}
	
	if (type == 'valid_annee'){
		if(confirm('Etes-vous sûr(e) de vouloir définitivement valider votre année '+id+' ?'))
		{
			alert("OK");
			document.location.href = 'annee_validee.asp?mode=valid&annee=' + id;
			//document.location.href = 'edition-frais2.asp?mode=valid&annee=' + id;
			//document.location.reload();
			//alert("L'année a  bien été validée");
		}
	}
}

function verif_champ(champname, nomfr){
	verif = true;
	expr = new RegExp("['<>/\\\(\);,?]+", "gi");
	
	if (expr.test(document.getElementById(champname).value)){
	    alert('Veuillez vérifier que vous n\'avez pas tapé de caractère interdit dans le champ \"'+nomfr+'\".\n\nListe des caractères interdits \'<>/\\\(\);,?');
		verif = false;
	}
	return verif;
}

function verif_champtexte(champname, nomfr){
	verif = true;
	expr = new RegExp("[<>/\\\(\);?]+", "gi");
	
	if (expr.test(document.getElementById(champname).value)){
	    alert('Veuillez vérifier que vous n\'avez pas tapé de caractère interdit dans le champ \"'+nomfr+'\".\n\nListe des caractères interdits <>/\\\(\);?');
		verif = false;
	}
	return verif;
}

function verif_longueur(champname, mini, maxi, nomfr){
	verif = true;
	if ((document.getElementById(champname).value.length < mini) || (document.getElementById(champname).value.length > maxi)){
	    if (mini!=maxi){
	    	alert('Tapez une valeur de '+mini+' à '+maxi+' caractères pour le champ \"'+nomfr+'\".');
		}else{
	    	alert('Tapez une valeur de '+mini+' caractères pour le champ \"'+nomfr+'\".');
		}
		verif = false;
	}
    return verif;
}

var colorOk = '#808080';
var colorKo = '#FFA800';
var nomStylechampAlerte = 'champ-alerte';
var nomStylechamp = 'champ';
if (document.location.href.search('manager') > 0 || document.location.href.search('Manager') > 0){
	colorKo = 'white';
}else{
	if (document.location.href.search('manager') > 0 || document.location.href.search('Manager') > 0){
		var colorOk = '#666666';
		var colorKo = '#FF0000';
		var nomStylechampAlerte = 'champ-ajout-deplacement-unique-alerte';
		var nomStylechamp = 'champ-ajout-deplacement-unique';
	}
}

//------------------------------------------------------------------------------------
function TestChaine(champname,mini,maxi,nomfr){
	if ((!(verif_longueur(champname, mini, maxi, nomfr))) ||(!(verif_champ(champname, nomfr)))){
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		return (false);
	}else{
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}
}

//------------------------------------------------------------------------------------
function TestTexte(champname,mini,maxi,nomfr){
	if ((!(verif_longueur(champname, mini, maxi, nomfr))) ||(!(verif_champtexte(champname, nomfr)))){
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		return (false);
	}else{
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}
}

//------------------------------------------------------------------------------------
function TestInt(champname,mini,maxi,nomfr){
	var p= / /gi
	document.getElementById(champname).value=document.getElementById(champname).value.replace(p,'');
	
	expr = new RegExp("^[0-9]{" + mini + "," + maxi + "}$", "gi");
	if (!(expr.test(document.getElementById(champname).value))){
	    if (mini!=maxi){
	    	alert('Tapez une valeur de '+mini+' à '+maxi+' chiffres pour le champ \"'+nomfr+'\".');
		}else{
	    	alert('Tapez une valeur de '+mini+' chiffres pour le champ \"'+nomfr+'\".');
		}
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		return (false);
	}else{
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}
}

//------------------------------------------------------------------------------------
function TestTel(champname,mini,maxi,nomfr){
	var p= / /gi
	document.getElementById(champname).value=document.getElementById(champname).value.replace(p,'');

	var p= /-/gi
	document.getElementById(champname).value=document.getElementById(champname).value.replace(p,'');

	var p= /\//gi
	document.getElementById(champname).value=document.getElementById(champname).value.replace(p,'');

	var p= /\./gi
	document.getElementById(champname).value=document.getElementById(champname).value.replace(p,'');

	expr = new RegExp("^[0-9]{" + mini + "," + maxi + "}$", "gi");
	if (!(expr.test(document.getElementById(champname).value))){
	    if (mini!=maxi){
	    	alert('Tapez une valeur de '+mini+' à '+maxi+' chiffres, espaces et caractères \".-/\" pour le champ \"'+nomfr+'\".');
		}else{
	    	alert('Tapez une valeur de '+mini+' chiffres, espaces et caractères \".-/\" pour le champ \"'+nomfr+'\".');
		}
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		return (false);
	}else{
		var valeur=document.getElementById(champname).value;
		if (valeur.length>10){
			document.getElementById(champname).value=valeur.substr(0,valeur.length-10) + '-' + valeur.substr(valeur.length-9,'1') + '.' + valeur.substr(valeur.length-8,'2') + '.' + valeur.substr(valeur.length-6,'2') + '.' + valeur.substr(valeur.length-4,'2') + '.' + valeur.substr(valeur.length-2,'2');
		}else{
			document.getElementById(champname).value=valeur.substr(valeur.length-10,'2') + '.' + valeur.substr(valeur.length-8,'2') + '.' + valeur.substr(valeur.length-6,'2') + '.' + valeur.substr(valeur.length-4,'2') + '.' + valeur.substr(valeur.length-2,'2');
		}
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}
}

//------------------------------------------------------------------------------------
function TestDate(champnameJ,champnameM,champnameA,nomfr){
	var p= /J/gi
	labelname=champnameJ.replace(p,'');

	var Newdate=new Date(document.getElementById(champnameA).value, document.getElementById(champnameM).value-1 , document.getElementById(champnameJ).value , 0 , 0);

	if ((champnameJ != 0) & (champnameM != 0) & (champnameA != 0))
		{
		var tmpJ=Newdate.getDate();
		var tmpM=Newdate.getMonth()+1;
		var tmpA=Newdate.getFullYear();

		if ((document.getElementById(champnameJ).value != tmpJ) | (document.getElementById(champnameM).value != tmpM) | (document.getElementById(champnameA).value != tmpA)){
	    	alert(nomfr + ' : date non valide.');
			
			document.getElementById(champnameA).focus();
			document.getElementById('l' + labelname).style.color = colorKo;
			document.getElementById(champnameA).className=nomStylechampAlerte;
			document.getElementById(champnameM).className=nomStylechampAlerte;
			document.getElementById(champnameJ).className=nomStylechampAlerte;
			return (false);
		}else{
			document.getElementById('l' + labelname).style.color = colorOk;
			document.getElementById(champnameA).className=nomStylechamp;
			document.getElementById(champnameM).className=nomStylechamp;
			document.getElementById(champnameJ).className=nomStylechamp;
		}
	}
}

//------------------------------------------------------------------------------------
function TestSiret(champname,mini,maxi,nomfr){
	var p= / /gi
	document.getElementById(champname).value=document.getElementById(champname).value.replace(p,'');
	
	expr = new RegExp("^[0-9]{14}$", "gi");
	if (!(expr.test(document.getElementById(champname).value))){
    	alert('Le N° de siret doit être saisi sous la forme 000 000 000 00000 (14 chiffres).');
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		return (false);
	}else{
		document.getElementById(champname).value=document.getElementById(champname).value.substr('0','3') + ' ' + document.getElementById(champname).value.substr('3','3') + ' ' + document.getElementById(champname).value.substr('6','3') + ' ' + document.getElementById(champname).value.substr('9','5');
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}
}

//------------------------------------------------------------------------------------
function TestTVAIntra(champname,mini,maxi,nomfr){
	var p= / /gi
	document.getElementById(champname).value=document.getElementById(champname).value.replace(p,'');
	
	expr = new RegExp("^[a-zA-Z]{2}[0-9]{11}$", "gi");
	if (!(expr.test(document.getElementById(champname).value))){
    	alert('Le N° de TVA intracommunautaire doit être saisi sous la forme FR00000000000 (2 lettres + 11 chiffres).');
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		return (false);
	}else{
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}
}

//------------------------------------------------------------------------------------
function TestMail(champname,nomfr){
	verif = true;
	if (!(verif_longueur(champname, '6', '250', nomfr))){
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
    	return false;
	}else{
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@._";
	var checkStr = document.getElementById(champname).value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length){
				allValid = false;
				break;
			}
	}
	
	if (allValid){
		var VerifMail = new RegExp("^.+@.+\..{2,3}$");
		if (document.getElementById(champname).value.search(VerifMail)==-1){
			allValid = false;
		}
	}

	if (!allValid){
    	alert('Tapez une valeur valide pour le champ \"'+nomfr+'\".');
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		verif = false;
	}else{
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}

    return verif;
}

//------------------------------------------------------------------------------------
function TestUrl(champname,nomfr){
	if (!(verif_longueur(champname, '7', '300', nomfr))){
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		return (false);
	}else{
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:/?-._";
	var checkStr = document.getElementById(champname).value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length){
				allValid = false;
				break;
			}
	}
	
//	alert(allValid);
	if (allValid){
		var VerifUrl = new RegExp("^(http://)?[a-zA-Z0-9]+([\./]?[a-zA-Z0-9_\-]+)*$");
//		alert(document.getElementById(champname).value.search(VerifUrl));
//		var VerifUrl = new RegExp("^http://[\w.%/?=&#:;+-]{5,}$");
		if (document.getElementById(champname).value.search(VerifUrl)==-1){
			allValid = false;
		}
	}

	if (!allValid){
    	alert('Tapez une valeur valide pour le champ \"'+nomfr+'\".');
		document.getElementById(champname).focus();
		document.getElementById('l' + champname).style.color = colorKo;
		document.getElementById(champname).className=nomStylechampAlerte;
		return (false);
	}else{
		document.getElementById('l' + champname).style.color = colorOk;
		document.getElementById(champname).className=nomStylechamp;
	}
}

function verifLogin(nom_form){
	if ((!(verif_champ('loginAxx', 'IDENTIFIANT'))) || document.getElementById('loginAxx').value.length == 0 || (!(verif_champ('passAxx', 'MOT DE PASSE'))) || document.getElementById('passAxx').value.length == 0){
		document.getElementById('loginAxx').value="Identification incorrecte";
		document.getElementById('passAxx').value="";
		document.getElementById('loginAxx').focus;
		return false;
	}else{
		document.getElementById(nom_form).submit()
	}		
}

function verifRecupPass(nom_form){
	verif = true;
	if (document.getElementById('typCpt').value!='aff'){
		if (!verif_champ('login', 'IDENTIFIANT') || !verif_longueur('login', 3, 50, 'IDENTIFIANT')){
			document.getElementById('llogin').style.color = colorKo;
			document.getElementById('login').className=nomStylechampAlerte;
	
			document.getElementById('lmail').style.color = colorOk;
			document.getElementById('mail').className=nomStylechamp;
			verif = false;
		}else{
			document.getElementById('llogin').style.color = colorOk;
			document.getElementById('login').className=nomStylechamp;
			if (!TestMail('mail','E-MAIL')){
				document.getElementById('mail').focus;
				document.getElementById('lmail').style.color = colorKo;
				document.getElementById('mail').className=nomStylechampAlerte;
				verif = false;
			}
		}
	}else{
		if (!TestMail('mail','E-MAIL')){
			document.getElementById('mail').focus;
			document.getElementById('lmail').style.color = colorKo;
			document.getElementById('mail').className=nomStylechampAlerte;
			verif = false;
		}
	}
	return verif;		
	//document.getElementById(nom_form).submit()
}


function verifCalculs(){
	expr = new RegExp("['<>/\\\(\);,?]+", "gi");
	var puissance = document.getElementById('puissance');
	var nbKm = document.getElementById('nbKm');
	//alert(puissance.value.length);
	//alert(nbKm.value)

	if (puissance.value.length == 0 || nbKm.value.length == 0){
		alert('Vous n\'avez pas rentré tous les champs. Veuillez vérifier vos saisies svp.');
	}
	
	else if (puissance.value > 0 && nbKm.value > 5000){
		alert('Le nombre de Km parcourus doit être inférieur ou égal à 5000');
	}
	
	else if (puissance.value > 0 && nbKm.value > 0){
		//alert('ok');
		if (expr.test(puissance.value) && expr.test(nbKm.value)){
			alert('Vous avez saisie des caractères interdits');
		}
		else{
			calculFraisKm(puissance, nbKm);
		}
	}
	else{
		alert('Mauvaise saisie. Veuillez vérifier vos données svp.');
	}
} 

function calculFraisKm(puissance, nbKm){
	var puiss = puissance.value;
	//alert(puiss);
	idBareme = '' + puiss;
	//alert('idbareme=' + idBareme);
	var coef = document.getElementById(idBareme).value;
	//alert(coef);
	coef = coef.replace(',', '.');
	//alert(coef);
	var result = Math.round(nbKm.value * coef * 100) / 100;
	result.toFixed(2);
	//alert(result + ' ' + coef);
	document.getElementById('resultat').value = result;
}

function SetCookie(nom,valeur,expire){
	if (expire == null){
		expiration = '';
	}else{
		expiration = '; expires=' + expire.toGMTString() + ';';
	}
	
	alert(nom + "=" + escape(valeur) + expiration);
	document.cookie = nom + "=" + escape(valeur) + expiration;
}

function GetCookie(nom){
	var search = new String(nomDuCookie + "=" + nom);
	if (document.cookie.length >0 ){
		debut = document.cookie.indexOf(search);
		if (debut != -1){
			fin = debut + search.length;
			if (fin = -1){
				fin = document.cookie.substring(debut,fin);
				return unescape(document.cookie.substring(debut, fin));
			}else{
				return "";
			}
		}else{
			return "";
		}
	}
}
