// fonction Trim en natif pour les objets String
String.prototype.trim = function()
{ return this.replace(/(^\s*)|(\s*$)/g, ""); }


String.prototype.Right = function(Taille) {
	var Chaine=this;
	var Lg=Chaine.length;
	if (isNaN(Taille)) Taille=Lg;
	return Chaine.substring(Lg-Taille,Lg);
}

String.prototype.Left = function(Taille) {
	var Chaine=this;
	var Lg=Chaine.length;
	if (isNaN(Taille)) Taille=Lg;
	return Chaine.substring(0,Taille);
}

// Verification de dates !
String.prototype.isDate = function() {
	var Date_Test = this;
	var Mois,Jour,Annee;
	Date_Test=Date_Test.split("/")
	if (Date_Test.length!=3) return false;
	else {
		Jour=Date_Test[0];
		Mois=Date_Test[1];
		Annee=Date_Test[2];
		if (isNaN(Jour) || isNaN(Mois) || isNaN(Annee)) return false;

		// conversion en nombres
		Jour=parseInt(Jour ,10);		
		Mois=parseInt(Mois ,10);
		Annee=parseInt(Annee ,10);	
		if (Mois<1||Mois>12||Jour<1||Jour>31||Annee.toString().length!=4||Annee<1900||Annee>2500) return false;
		
		// Test fin sur les mois :
		// on test pour les mois de 30 jours 
		// => attention au fonctionnement du switch ! Execute toutes les fonctions dès qu'un test est OK 
		switch (Mois) {
			case 2:
			case 4:
			case 6:
			case 9:
			case 11:
				if (Jour>30) return false;
				// Cas spécial pour février ! (on ne teste pas les années bisextiles)
				if (Mois==2 && Jour>29) return false;
				break;
		}	
	}	
	return true;
}

function DateCompare(D1,D2) {
	// return true if D2>=D1
	
	if (!D1.isDate()) {
		alert("Date 1 non valide !")
		return false;
	}
	if (!D2.isDate()) {
		alert("Date 2 non valide !")
		return false;
	}	
	
	var J1, M1, A1;
	var J2, M2, A2;
	
	D1=D1.split("/");
	D2=D2.split("/");	

	// conversion en nombres
	J1=parseInt(D1[0] ,10);
	M1=parseInt(D1[1] ,10);
	A1=parseInt(D1[2] ,10);

	J2=parseInt(D2[0] ,10);
	M2=parseInt(D2[1] ,10);
	A2=parseInt(D2[2] ,10);
	
	if (A2<A1) return false;	
	if (A2>A1) return true;
	else { // A1=A2, testing month
		if (M2<M1) return false;
		if (M2>M1) return true;
		else { // M1=M2, testing days
			if (J2>J1) return true;
			if (J2<J1) return false;
			else return true; // D1=D2 ;)
		}
	}
}


// Fonctions de validation des dates/heure
function ValidDate(Champ) {
	if (!Champ.value.isDate()) {
		alert("Vous devez saisir une date valide !\n\n( jj/mm/aaaa )");
		Champ.select();		
		Champ.focus();
		return false;
	}
	return true;
}

function ValidHeure(Champ) {
	var DeuxPts=Champ.value.indexOf(":");
	var H=Champ.value.substring(0,2);
	var M=Champ.value.substring(3,5);
	if (DeuxPts!=2 || isNaN(H) || isNaN(M) || H>23 || M>59) {
		alert("Vous devez saisir une heure valide !\n\n( hh:mm )");
		Champ.select();		
		Champ.focus();
		return false;
	}
	return true;
}

function Valid_TelFormat(Champ) {
	var Val=Champ.value.trim();
	if (Val!="") {
		// on supprime les infos non numériques avant des chiffres
		Val=Val.replace(/[ \\\/\._\-\*]+(\d+)/ig,"$1");
		Champ.value=Val;
	}
}

function Valid_EMail(Champ) {
	var Val=Champ.value.trim();
	if (Val!="") {
		if (Val.indexOf("@")==-1) {
			alert("Vous devez saisir une adresse e-mail valide !");
			Champ.select();
			Champ.focus();
			return false;		
		}
	}
	return true;
}

function Valid_CP(Champ) {
	// CP valide si 5 chiffres ou B+4 chiffres (pour la belgique)
	var Val=Champ.value;
	var RegEx=/\d{5}|(B\d{4})/ig
	
	if (RegEx.exec(Val)==null) {
		alert("Vous devez saisir un code postal valide !");
		Champ.select();
		Champ.focus();
	}
}

function ValidNombre(Champ) {
	var Val=Champ.value;
	Val=Val.replace(",",".");
	Champ.value=Val;
	if (Val!=""&&isNaN(Val)) {
		alert("Vous devez saisir un nombre valide !");
		Champ.select();
		Champ.focus();
	}
}

function Valid_Siret(Champ) {
	var Val=Champ.value;

	// On supprime tout ce qui n'est pas un chiffre
	Val=Val.replace(/(\D+)/ig,"");
	Champ.value=Val;	
	if (Val.length!=14) {
		alert("Vous devez saisir un numéro SIRET valide !\n(Numéro 14 chiffres : SIREN à 9 chiffres, +5 chiffres)");
		Champ.select();
		Champ.focus();	
	}
}

function NewWin(MyURL) {
	PosInter=MyURL.indexOf("?");
	if (PosInter>0) {
		TitreWin=MyURL.substring(0,PosInter-1);
	}
	else {
		TitreWin=MyURL;
	}
	PosPoint=TitreWin.lastIndexOf(".");
	PosLastBarre=TitreWin.lastIndexOf("/")+1;
	
	
	SizeX=screen.availWidth;
	SizeY=screen.availHeight;
	TitreWin=TitreWin.substring(PosLastBarre,PosPoint);
	TitreWin=TitreWin.replace("-","");
	
	NouvelleWin=window.open(MyURL,"F"+TitreWin,"menubar=yes,status=no,location=no,resizable=yes,toolbar=no,scrollbars=yes,width="+SizeX*0.75+",height="+SizeY*0.7);
	NouvelleWin.moveTo((SizeX-(SizeX*0.75))/2,(SizeY-(SizeY*0.8))/2);
	return void(NouvelleWin);
}	


function setCookie( name, value )
{
	// '=' is identifier between name and value for real cookie.
	var expireDate = new Date(); 
	expireDate.setTime(expireDate.getTime() + (365 * 24 * 3600 * 1000)); // 1 ans
	document.cookie = name + '=' + value + ";expires=" + expireDate.toGMTString(); 
}


function getCookie( name )
{
	var flag = document.cookie.indexOf( name+'=' )
	var end
	if( flag != -1 ) {
		flag += name.length + 1
		end = document.cookie.indexOf( "; ", flag )
		if( end == -1 ) end = document.cookie.length
		return document.cookie.substring( flag, end )
	}
	else return ""
}

function FocusOn() {
	setTimeout("self.focus();",150);
}


function Toggle(idDiv) {
	var div=document.getElementById(idDiv);
	if (div) {
		if (div.style.display=="none" || div.style.display=="") {
			div.style.display="block";
		}
		else {
			div.style.display="none";
		}
	}
}
