////////////////////////////////
//                            //
//          COMMUN            //
//                            //
////////////////////////////////

// Fonction pour mettre une date de peremption sur le site pour la version CD.
function dateLimite(){
	var dateC = new Date(); // date courante.
	var dateCS = dateC.getTime() / 1000; // date courante en seconde.
	var dateL = new Date(2009,3,1,0,0,0); // date limite de validation.
	var dateLS = dateL.getTime() / 1000; // date limite de validation en seconde.
	// si la date courante est superieur a celle de validation on fait une redirection vers la page de peremption. (le site n'est plus utilisable)
	if (dateCS > dateLS){
		document.location.href = "peremption.html";
	}
}

// Pour faire apparaitre la bonne nav.
function selectNav(id){
	var objG = document.getElementsByTagName("*");
	for (i=0; i<objG.length; i++){
		if (objG[i].id.substring(0,4) == 'nav_'){
			objG[i].style.display = 'none';
		}
		if (objG[i].id.substring(0,5) == 'menu_'){
			objG[i].className = 'lienOff';
		}
	}
	// on fait disparaitre le coin perso.
	document.getElementById('coin-perso').style.display = 'none';
	// on fait apparaitre la bonne nav et on selectionne le bon lien.
	if (document.getElementById('nav_'+id)){
		document.getElementById('nav_'+id).style.display = 'block';
		document.getElementById('menu_'+id).className = 'lienOn';
	}
}

// Fonction pour changer d'onglet et afficher le bon contenu.
function onglet(elmt, id){
	// on selectionne l'onglet cliqué.
	var elmtOnglet = document.getElementById('onglets');
	var onglet = elmtOnglet.getElementsByTagName('label');
	for (i=0; i<onglet.length; i++){
		onglet[i].className = 'ongOff';
	}
	elmt.className = 'ongOn';
	// apres on affiche le nouveau resultat.
	var texte = file('ajax.php?type=swapTexte&id='+id);
	document.getElementById('article').innerHTML = texte;
}

// Fonction pour montrer une sous nav.
function montreNav(id){
	cacheToutNav();
	resetClock();
	var d = document.getElementById('snav_'+id);
	if (d){
		d.style.display = 'block';
	}
}

// Fonction pour cacher une sous nav.
function cacheNav(id){
	var d = document.getElementById(id);
	if (d){
		d.style.display = 'none';
	}
}

// Fonction pour cacher toutes les sous nav.
function cacheToutNav(){
	var objBody = document.getElementsByTagName("*");
	for (i=0; i<objBody.length; i++){
		if (objBody[i].id.substring(0,5) == 'snav_'){
			cacheNav(objBody[i].id);
		}
	}
}

// Fonction de timer pour tout cacher ou remettre le timer a 0.
var clockHide;
function hideAll(){
	if (clockHide) resetClock();
	clockHide = setTimeout("cacheToutNav()",500);
}
function resetClock(){
	if (clockHide){
		clearTimeout(clockHide);
		clockHide = null;
	}
}

// Fonction pour bouger un élément par rapport à la souris.
function moveElement(e, id){
	var tempX = 0;
	var tempY = 0;
	var offsetX = 200;
	var offsetY = 135;
	var obj = document.getElementById(id);
	
	if (document.all){
		tempX = event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		tempY = event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	}
	else {
		tempX = e.pageX;
		tempY = e.pageY;
	}
	
	// pour que le bloc reste au milieu de l'article dans la largeur.
	if (tempX > 527){
		tempX = tempX - (tempX - 527);
	}
	
	// pour que le bloc reste au milieu de l'article dans la hauteur.
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var hauteurDef = obj.offsetHeight;
	var hauteurReste = (arrayPageSize[3] + arrayPageScroll[1]) - tempY;
	if (hauteurReste < hauteurDef){ // si il ne reste pas suffissament de place en dessous pour afficher la définition on l'affiche au dessus du lien.
		tempY = tempY - (hauteurDef + 25);
	}
	
	// on applique les position au bloc.
	obj.style.top  = (tempY - offsetY) + 'px';
	obj.style.left = (tempX - offsetX) + 'px';
}

// Fonctions pour afficher ou cacher un mot du glossaire.
function showGlos(e, id){
	if (document.getElementById('glos_'+id)){
		document.getElementById('glos_'+id).style.display = 'block';
		moveElement(e, 'glos_'+id); // on lance la fonction de placement par rapport a la souris.
	}
}
function hideGlos(id){
	if (document.getElementById('glos_'+id)){
		document.getElementById('glos_'+id).style.display = 'none';
	}
}

// Fonction pour afficher ou cacher un point de détail.
function showDetail(id){
	var elmt = document.getElementById('ptDetail_'+id);
	if (elmt){
		if (elmt.className == 'para-detailOn'){
			elmt.className = 'para-detailOff';
		}
		else {
			elmt.className = 'para-detailOn';
		}
	}
}

// Fonctions de preload et de swap image.
function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Ouverture de popup
function ouvreFenetre(nom, adresse, largeur, hauteur){
	var top = 100;
	var left = 100;
	toto = window.open(adresse, nom, "scrollbars=yes,toolbar=no,menubar=yes,resizable=no,top="+top+",left="+left+",height="+hauteur+",width="+largeur);
	toto.focus();
}

// Fonction pour avoir la taille des scroll de la page.
function getPageScroll(){
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) { // all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;   
    }
    arrayPageScroll = new Array(xScroll,yScroll)
    return arrayPageScroll;
}

// Fonction qui retourne les tailles de la pages.
function getPageSize(){
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {   
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }   
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){   
        pageWidth = xScroll;       
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}

// Changement automatique apres sélection d'un champ select.
function jumpMenu(selObj,restore){
  eval("location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


////////////////////////////
//                        //
//      ESPACE LOGIN      //
//                        //
////////////////////////////

// Fonction pour cacher ou montrer des options sur l'espace de connexion.
function showLog(cible){
	var objBody = document.getElementsByTagName("*");
	for (i=0; i<objBody.length; i++){
		if (objBody[i].id.substring(0,4) == 'log_'){
			objBody[i].style.display = 'none';
		}
	}
	document.getElementById('log_'+cible).style.display = 'block';
}

// Fonction pour verifier la connexion.
function verifLog(form){
	if (testVide(form.login) != 'ok') {return 'non'}
	if (testMail(form.login) != 'ok') {return 'non'}
	if (testVide(form.password) != 'ok') {return 'non'}
	return 'ok';
}
function valLog(){
	if (verifLog(document.identification)=='ok'){
		document.identification.submit();
	}
}

// Fonction pour verifier la connexion.
function verifRec(form){
	if (testVide(form.login) != 'ok') {return 'non'}
	if (testMail(form.login) != 'ok') {return 'non'}
	return 'ok';
}
function valRec(){
	if (verifRec(document.recuperation)=='ok'){
		document.recuperation.submit();
	}
}

function titreLog(titre){
	document.getElementById('log-titre').innerHTML = titre;
}


////////////////////////////
//                        //
//      FORMULAIRES       //
//                        //
////////////////////////////

// Fonction pour savoir si une checkbox est selectionné.
function AnySelected(champs){
    for (i=0; i<champs.length; i++) {
        if (champs[i].checked) return true;
    }
    return false;
}

// Fonction pour indiquer ce qu'il faut faire dans le champ de recherche de texte.
function onText(champ){
	if(champ.value == 'tapez un texte'){
		champ.value = '';
	}
}
function outText(champ){
	if(champ.value == ''){
		champ.value = 'tapez un texte';
	}
}

// Fonction pour cacher ou montrer des options sur des checkbox (fiches).
function checkChecked(champ, cible){
	if (champ.checked == true){
		document.getElementById(cible).style.display = 'block';
	}
	else {
		document.getElementById(cible).style.display = 'none';
	}
}

// Fonction pour mettre en surbrillance un champ quand il n'est pas rempli.
function highlightChamp(champ){
	champ.focus();
	if (champ.tagName.toLowerCase() == 'select'){
		champ.className = 'selectOn';
	}
	else {
		champ.className = champ.type.toLowerCase()+'On';	
	}
}

// Fonction pour enlever la surbrillance a un champ.
function shadowChamp(champ){
	if (champ.tagName.toLowerCase() == 'select'){
		champ.className = 'select';
	}
	else {
		champ.className = champ.type.toLowerCase();	
	}
}

// Fonction pour vider l'id proprietaire apres avoir vider le champ de saisie automatique.
function videId(champ){
	if (testVide(champ) != 'ok'){
		var name = 'hidden_'+champ.id;
		if (document.getElementById(name)){ document.getElementById(name).value = ''; }
	}
}

// Fonction de validité de mail.
function testMail(champ) {
	var car = 'non';
	for (var i = 0; i < champ.value.length; i++) {
		var ch = champ.value.substring(i, i + 1);
		if (ch == '@') {
			for (var j = i+1; j < champ.value.length; j++) {
				var ch2 = champ.value.substring(j, j + 1);
				if (ch2 == '.') {car='oui';}
				}
			}
		}
	if (car!='oui'){
		highlightChamp(champ); // on met le champ en avant.
		return 'non';
	}
	shadowChamp(champ); // on remet le champ par défaut.
	return 'ok';
}

// Fonction test de champ vide.
function testVide(champ){
	if (champ.value.length == 0){
		highlightChamp(champ); // on met le champ en avant.
		return 'non';
    }
    if (champ.value.length > 0){
        var tout_espaces='oui';
    	for (var i = 0; i < champ.value.length; i++){
        	if (champ.value.substring(i, i + 1) != ' '){ tout_espaces='non'; }
      	}
      	if (tout_espaces=='oui'){
			highlightChamp(champ); // on met le champ en avant.
			return 'non';
      	}
    }
	shadowChamp(champ); // on remet le champ par défaut.
	return 'ok';
}

// Fonction pour verifier les boutons radio.
function verifRadio(champ){
	var one = 'non';
	for (var i=0; i<champ.length; i++){
		if (champ[i].checked){
			var one = 'oui';
		}
	}
	if (one != 'oui'){
		highlightChamp(champ); // on met le champ en avant.
		return 'non';
	}
	shadowChamp(champ); // on remet le champ par défaut.
	return 'ok';
}

// Fonction pour vérifier le champ de recherche de texte.
function verifSearchText(form){
	if (form.Texte.value == 'tapez un texte') {form.Texte.focus();return 'non'}
	return 'ok';
}
function validSearchText(){
	if (verifSearchText(document.rechText)=='ok') {
		document.rechText.submit();
	}
}

// Fonction pour valider une inscription.
function verifInsc(form){
	if (testVide(form.Nom) != 'ok') {return 'non'}
	if (testVide(form.Prenom) != 'ok') {return 'non'}
	if (testVide(form.Tel1) != 'ok') {return 'non'}
	if (testVide(form.Email) != 'ok') {return 'non'}
	if (testMail(form.Email) != 'ok') {return 'non'}
	if (testVide(form.Societe) != 'ok') {return 'non'}
	return 'ok';
}
function valInsc(){
	if (verifInsc(document.inscription)=='ok') {
		document.inscription.but.disabled = true;
		document.inscription.submit();
	}
}


////////////////////////////
//                        //
//         AJAX           //
//                        //
////////////////////////////

function file(fichier){
	//On teste le navigateur
	if ( window.XMLHttpRequest ){ // On teste si le navigateur est Firefox
		xhr_object = new XMLHttpRequest();
	}
	else if ( window.ActiveXObject ){ // Sinon, on teste si c'est IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		return false;
	}
	xhr_object.open( "GET", fichier, false );
	xhr_object.send(null);
	if ( xhr_object.readyState == 4 ){
		return xhr_object.responseText;
	}
	else{
		return false;
	}
}

