function initHistory() {

	// Durée du cookie : 4 heures
	var expire = new Date();
	var DureeCookie = expire.getTime() + (4 * 60 * 60 * 1000);
	
	var labIdCourant = 0;
	if($('menu_laboratoire')){labIdCourant = $('menu_laboratoire').value;}
	expire.setTime(DureeCookie);

	// Récupération de la valeur dans le paramètre url 'cook'
	var paramCook = getParamCook(window.location.search);

	// Test : nouvel onglet / nouvelle fenêtre (test sur historique) ou paramCook vide
	if (history.length <= 1 || paramCook == '') {

		// Construction du cookie
		paramCook = new Date();
		paramCook = paramCook.getTime();

		document.cookie = paramCook + '=' + paramCook + '; expires=' + expire.toGMTString();

	}

	// Envoi des informations au serveur
	AJAXRequest("main.php?action=ajx_history",
				"&cook=" + paramCook + "&href=" + encodeURIComponent(window.location.href) + "&titre=" + window.document.title,
				getHistory);

	// Insertion du nom des cookies dans les balises action des différents formulaires de la page
	setFormActionCook(paramCook, labIdCourant);

	// Insertion du nom des cookies dans les balises href des différents liens de la page
	setAHrefCook(paramCook, labIdCourant);

	//Masquage du div de chargement s'il existe, et si l'initialisation de détection du service Entendre3 est terminée
	if(typeof(globalPageHome) != 'undefined'){
		if($('loadingJS') && !globalPageHome){
			$('loadingJS').style.display = 'none';
		}
	} else {
		$('loadingJS').style.display = 'none';
	}
}

function getParamCook(urlSearch) {

	// Paramètre 'cook'
	var paramCook = '';

	// S'il y a des paramètres dans l'url transmise
	if (urlSearch != '') {

		// Recherche du parametre 'cook'
		var positionCook = urlSearch.indexOf('cook=', 0);

		// Récupération de la valeur du paramètre 'cook'
		if (positionCook != -1) {
			// Recherche d'un éventuel paramètre suivant 'cook'
			var positionEsperluette = urlSearch.indexOf('&', positionCook);
			if (positionEsperluette != -1) {
				paramCook = urlSearch.substring(positionCook + 5, positionEsperluette);
			} else {
				paramCook = urlSearch.substring(positionCook + 5);
			}

		}

	}

	return paramCook;

}

function getHistory(xhr) {

	if($('historique') != undefined) {

		if(trim(decodeURIComponent(getXhrValue(xhr, 'prec'))) != '') {
			$('histoPre').innerHTML = decodeURIComponent(getXhrValue(xhr, 'prec'));
		} else {
			$('histoPre').innerHTML = '<span class="menuHistoInactif"><img style="border: none" src="img/precedent.png" alt="" title="" /></span>';
		}

		$('historique').innerHTML = decodeURIComponent(getXhrValue(xhr, 'histo'));

		if(trim(decodeURIComponent(getXhrValue(xhr, 'suiv'))) != '') {
			$('histoSui').innerHTML = decodeURIComponent(getXhrValue(xhr, 'suiv'));
		} else {
			$('histoSui').innerHTML = '<span class="menuHistoInactif"><img style="border: none" src="img/suivant.png" alt="" title="" /></span>';
		}

	}

}

function setFormActionCook(paramCook, labIdCourant) {

	var forms = document.getElementsByTagName('form');
	var anchor = '';
	var labIdCourantStr = '';
	if(labIdCourant > 0){labIdCourantStr = '&labIdCourant=' + labIdCourant;}

	for ( var i = 0; i < forms.length; i++) {

		/* NON UTILISE DANS L'IMMEDIAT
		 *
		 * cook = document.createElement('input');
		 * cook.setAttribute('type', 'hidden');
		 * cook.setAttribute('name', 'cook');
		 * cook.setAttribute('value', paramCook);
		 * forms[i].appendChild(cook);
		 */

		anchor = '';
		if (forms[i].action.indexOf('#', 0) != -1) {
			anchor = forms[i].action.substring(forms[i].action.indexOf('#', 0));
			forms[i].action = forms[i].action.substring(0, forms[i].action.indexOf('#', 0));
		}

		if (forms[i].action.indexOf('?', 0) != -1) {
			if (forms[i].action.indexOf('=', 0) != -1) {
				forms[i].action = forms[i].action + '&cook=' + paramCook + labIdCourantStr + anchor;
			} else {
				forms[i].action = forms[i].action + 'cook=' + paramCook + labIdCourantStr + anchor;
			}
		} else {
			forms[i].action = forms[i].action + '?cook=' + paramCook + labIdCourantStr + anchor;
		}

	}

}

function setAHrefCook(paramCook, labIdCourant) {

	var as = document.getElementsByTagName('a');
	var anchor = '';
	var labIdCourantStr = '';
	if(labIdCourant > 0){labIdCourantStr = '&labIdCourant=' + labIdCourant;}

	for ( var i = 0; i < as.length; i++) {

		anchor = '';
		if (as[i].href.indexOf('#', 0) != -1) {
			anchor = as[i].href.substring(as[i].href.indexOf('#', 0));
			as[i].href = as[i].href.substring(0, as[i].href.indexOf('#', 0));
		}

		if (as[i].href.indexOf('?', 0) != -1) {
			if (as[i].href.indexOf('=', 0) != -1) {
				as[i].href = as[i].href + '&cook=' + paramCook + labIdCourantStr + anchor;
			} else {
				as[i].href = as[i].href + 'cook=' + paramCook + labIdCourantStr + anchor;
			}
		} else if (as[i].href != '') {
			//Uniquement si interne au site
			if(as[i].href.indexOf($('rootUrl').value) >= 0){
				as[i].href = as[i].href + '?cook=' + paramCook + labIdCourantStr + anchor;
			}
		}

	}

}

function goToHRef(key) {
	window.location = $(key).href;
}

function changeWindowLocation(url) {
	// globalCook et globalLabIdCourant sont valorisés dans reponse.php en fin de fichier
	window.location = url + '&cook=' + globalCook + '&labIdCourant=' + globalLabIdCourant;
}

