
// 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 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);
	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);
}



var TabMenus=new Array("MenuActus","MenuAnnuaires","MenuMIAB","MenuServices","MenuPublications");
var TabSubMenus=new Array("SubMenuPartHeb");

function ShowMenu(NomMenu,LienClique) {
	var o; // objet survolé

	HideMenus();
	
	menu=document.getElementById("Sub" + NomMenu);
	menu.style.display="none";
	if (LienClique!='') {
		o=document.getElementById(LienClique)
		x=-6; 
		y=o.offsetHeight-2
		// on va rechercher la position TOP du lien suvolé
		while(o){
			if (o.tagName.toUpperCase()=="BODY") {
				o=null;
			}
			else {
				x+=o.offsetLeft;
				if (o.tagName.toUpperCase()!="A")	y+=o.offsetTop;
				o=o.parentNode;
			}
		}
		menu.style.left=x;
		menu.style.top=y;
		menu.className="Entete";
		menu.style.display="block";
		menu.style.position="absolute";
	}
	
	return void(null); //Désactive le lancement du lien
}

function ShowSubMenu(NomMenu,LienSurvol,MenuOuvrant) {

	menu=document.getElementById(NomMenu);
	o=LienSurvol;
	x=190+2; 

	y=2;
	menu.style.display="none";
	while(o){
		if (o.tagName.toUpperCase()=="BODY") {
			o=null;
		}
		else {

			x+=o.offsetLeft;
			if (o.tagName.toUpperCase()!="A")	y+=o.offsetTop;
			o=o.parentNode;
		}
	}
	
	if (x>200) {
	//permet de ne pas afficher le menu lors du survol des liens "menu à gauche"
		document.getElementById(MenuOuvrant).style.display="block"; // on réaffiche le menu principal	
		menu.style.left=x;
		menu.style.top=y+17;
		menu.className="Entete";
		menu.style.display="block";
		menu.style.position="absolute";
	}	
}

function ReShowMenu(Menu) {
	//document.getElementById(Menu).style.display="none";
	Menu.style.display="block";
}


function HideThisMenu(Menu) {
	//document.getElementById(Menu).style.display="none";
	Menu.style.display="none";
}


function HideMenus() {
	for(i=0;i<TabMenus.length;i++) {
		document.getElementById("Sub" + TabMenus[i]).style.display="none";
	}
	HideSubMenus();
}

function HideSubMenus() {
	for(i=0;i<TabSubMenus.length;i++) {
		document.getElementById(TabSubMenus[i]).style.display="none";
	}
}

function ShowSingleMenu(Menu) {
	document.getElementById(Menu).style.display="block";
}

function HideSubMenu(Menu) {
	//document.getElementById(Menu).style.display="none";
}


function ValiderSearchNormes() {
	var F=document.forms["Veille"];
	var OK=true;
	if (F.DateInf.value!="" && F.DateSup.value!="") {  
		if (!DateCompare(F.DateInf.value,F.DateSup.value)) {
			alert("La date de fin doit être supérieure ou égale à la date de début !");
			F.DateSup.select();
			F.DateSup.focus();
			OK=false;
		}
	}

	//if (OK) F.submit();
	return OK;
}




/* SCRIPTS AJAX */
function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (E) {
			xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} 
		catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}


/*

Méthodes de l'objet XMLHttpRequest 
	Méthode 		Description 
open() 			Met fin à la requête en cours et en prépare une nouvelle, en indiquant la méthode (GET ou POST) et l'URL.  
send()  		Lance la requête  
abort()  		Met fin à la requête en cours  
setRequestHeader()  	Assigne un couple nom/valeur à l'en-tête accompagnant la requête  
getResponseHeader()  	Récupère la valeur d'une chaîne de l'en-tête de réponse  
getAllResponseHeader()  Récupère l'ensemble des en-têtes de réponse 

Proprétés de l'objet XMLHttpRequest 
	Propriété 		Description 
status  		Code renvoyé par le serveur (exemple, 200 pour OK ou 404 pour un fichier introuvable)  
statusText  		La chaîne accompagnant le code  
readyState  		Un entier indiquant l'état de l'objet. Peut prendre 5 valeurs : 
	0 = non initialisé 
	1 = en cours de chargement 
	2 = chargé 
	3 = interaction 
	4 = terminé  
onreadystate  		Gestionnaire d'évènement pour chaque changement d'état de l'objet  
responseText  		Chaîne correspondant à la réponse du serveur à la requête  
respondeXML  		Version XML de la chaîne de réponse  


*/



function AJAX_ShowInfo(Id, TypePage) {

	var Details=document.getElementById("Details" + Id);

	Details.className="";	
	Details.innerHTML="<BR><img src=\"/images/AjaxLoading.gif\">";

	var http = getHTTPObject();
	switch (TypePage) {
		case "news" :
			http.open("get","/ajax/getactus.asp?id=" +Id);
			break;
		case "agenda" :
			http.open("get","/ajax/getagenda.asp?id=" +Id);
			break;
	}
	http.onreadystatechange=function() {
		if (http.readyState!=4) return;

		Details.innerHTML=http.responseText;
	}

	http.send(null);
}

function Box(IdBox, URL, Logo, Texte) {
	function Interne_LoadBox() {
		var http = getHTTPObject();
		http.onreadystatechange=function() {
			if (http.readyState!=4) return;
			// le this.boxIdBox n'est pas accessible ici, vu qu'on est dans une nouvelle fonction !
			document.getElementById(IdBox).innerHTML=http.responseText;		
		}	
		http.open("get",this.boxURL + "?" + VarSearch);
		http.send(null);
	}
	
	this.SetURL=function(URL) {
		this.boxURL=URL;
	}

	this.ShowMessage =function () {
		document.getElementById(IdBox).innerHTML="<img src=\"/images/" + this.logo + "\"> " + Texte + " ..."
	}	
	
	this.LoadBox=Interne_LoadBox;
	this.boxIdBox=IdBox;
	this.boxURL=URL;
	this.logo=Logo;
}

var VarSearch="";
var BoxNews=new Box("BoxNews","/recherche/search_presse.asp","AjaxLoading.gif","Recherche en cours dans les dépèches/revues de presse ...");
var BoxAgenda=new Box("BoxAgenda","/recherche/search_agenda.asp","AjaxLoading.gif","Recherche en cours dans l'agenda ...");
var BoxIaa=new Box("BoxIaa","/recherche/search_iaa.asp","AjaxLoading.gif","Recherche en cours dans les industries et prestataires ...");

function DoLoadBoxs() {
//return;
	var Timer=75; //750; //50; 
	var DelaiLoad=750; //625; //75;

	BoxNews.ShowMessage();
	BoxAgenda.ShowMessage();
	BoxIaa.ShowMessage();	
	
	setTimeout("BoxNews.LoadBox();",Timer+=DelaiLoad);
	setTimeout("BoxAgenda.LoadBox();",Timer+=DelaiLoad);
	setTimeout("BoxIaa.LoadBox();",Timer+=DelaiLoad);
}

function Toggle(Id) {
	var DivLink=document.getElementById("LinkShow" + Id);;
	var DivInfo=document.getElementById("Results" + Id);

	if (DivInfo.style.display=="none") {
		DivInfo.style.display="block";
		DivLink.innerHTML="Cacher les résultats";
	}
	else {
		DivInfo.style.display="none";
		DivLink.innerHTML="Afficher les résultats";		
	}
}