// JavaScript Document
// fonctions courantes de TSC

	function cache(event, id)
	{
		if (document.getElementById(id).className == 'on') document.getElementById(id).className='off';
		else {
			document.getElementById(id).className='on';
			event.cancelBubble = true;
		}
	}

	function off(id)
	{
		document.getElementById(id).className='off';
	}

	function CaseACocher() {
		var cycle = 1;
		while (cycle<21) {
			if (document.getElementById("check_".concat(cycle))) {
				if (document.getElementById("check_".concat(cycle)).checked==false) { document.getElementById("check_".concat(cycle)).checked=true; }
				else { document.getElementById("check_".concat(cycle)).checked=false; }
			}
			cycle++;
		}
	}

	function ChangeUrl(formulaire)	{
		if (formulaire.acces.selectedIndex != 0)	{
			location.href = formulaire.acces.options[formulaire.acces.selectedIndex].value;
			}
		else 	{
			alert('Veuillez choisir une destination.');
			}
		}

	function fenetre(page) {
	window.open(page, 'titre', 'toolbar=no, location=no, directories=no, status=no, scrollbars=yes, resizable=yes, width=750, height=550');
	}
	
	function view_img(page) {
	window.open(page, 'titre', 'toolbar=no, location=no, directories=no, status=no, scrollbars=no, resizable=yes, width=820, height=620');
	}

	function help(page) {
	window.open(page, 'titre', 'toolbar=no, location=no, directories=no, status=no, scrollbars=yes, resizable=yes, width=280, height=420');
	}

	function confirmSup(chaine){ 
		var ok;
		ok=confirm(chaine); 
		return ok;
	}

	function goto_page_rep(LIEN)
	{
		var PAGE = prompt("Accèder à la page...", "");
		
		if (PAGE && PAGE != " " && !isNaN(PAGE)) {
			window.location = LIEN + '&page=' + PAGE;
		}
		else if (PAGE) {
			alert ("Le numéro de la page n'est pas valide !");
		}
	}
	
	function creerRequete () {
		try {
			requete = new XMLHttpRequest();
		} catch (essaimicrosoft) {
			try {
				requete = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (autremicrosoft) {
				try {
					requete = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (echec) {
					requete = null;
				}
			}
		}
	}
	
	function verif_mp ()
	{
		var temps = parseInt(0);
		
		creerRequete();
		
		requete.open("GET", "inc_commun/maj_mp.php", true); // initialise
		requete.onreadystatechange = maj_nv_mp;
		requete.send(null); // envoit
		
		if (temps < 40)
		{
			setTimeout('verif_mp()',1000*10);
			temps = temps + 10;
		}
		else if (temps < 280)
		{
			setTimeout('verif_mp()',1000*10);
			temps = temps + 60;
		}
		else
		{
			setTimeout('verif_mp()',1000*600);
		}
	}
	
	function maj_nv_mp () {
		if (requete.readyState == 4 && requete.status == 200) {
			var nouvelExpe = unescape(requete.responseText);
			nouvelExpe.replace (/&eacute;/, "é");
			
			if (nouvelExpe != 0) {
				var spanExpe = document.getElementById("alerte_mp_expe");
				var statut = getTexte(spanExpe);
				if (statut != nouvelExpe) {
					document.getElementById('alerte_mp').className='';
					remplacerTexte(spanExpe, nouvelExpe);
					
						var nb_mp = document.getElementById("nb_new_mp");
						var new_nb_mp = parseInt(getTexte(nb_mp)) + 1;
						remplacerTexte(nb_mp, new_nb_mp);
				}
			}
		}
	}
	
	function remplacerTexte(el, texte) {
		effacerTexte(el);
		var nouveauNoeud = document.createTextNode(texte);
		el.appendChild(nouveauNoeud);
	}
	
	function effacerTexte(el) {
	  if (el != null) {
		if (el.childNodes) {
		  for (var i = 0; i < el.childNodes.length; i++) {
			var noeudFils = el.childNodes[i];
			el.removeChild(noeudFils);
		  }
		}
	  }
	}
	
	function getTexte(el) {
	  var texte = "";
	  if (el != null) {
		if (el.childNodes) {
		  for (var i = 0; i < el.childNodes.length; i++) {
			var noeudFils = el.childNodes[i];
			if (noeudFils.nodeValue != null) {
			  texte = texte + noeudFils.nodeValue;
			}
		  }
		}
	  }
	  return texte;
	}


function insertTag(startTag, endTag, textareaId, tagType) {
	var field = document.getElementById(textareaId); 
	field.focus();
	
	/* === Partie 1 : on récupère la sélection === */
	if (window.ActiveXObject) {
		var textRange = document.selection.createRange();    
		var currentSelection = textRange.text;
	} else {
		var startSelection   = field.value.substring(0, field.selectionStart);
		var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
		var endSelection     = field.value.substring(field.selectionEnd);       
	}
	
	/* === Partie 2 : on analyse le tagType === */
	if (tagType) {
		switch (tagType) {
		case "lien":
			endTag = "[/url]";
			if (currentSelection) { // Il y a une sélection
				if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
					// La sélection semble être un lien. On demande alors le libellé
					var label = prompt("Entrez le titre...") || "";
					startTag = "[url=" + currentSelection + "]";
					currentSelection = label;
				} else {
					// La sélection n'est pas un lien, donc c'est le libelle. On demande alors l'URL
					var URL = prompt("Entrez le lien complet...");
					startTag = "[url=" + URL + "]";
				}
			} else { // Pas de sélection, donc on demande l'URL et le libelle
				var URL = prompt("Entrez le lien complet...") || "";
				var label = prompt("Entrez le titre...") || "";
				startTag = "[url=" + URL + "]";
				currentSelection = label;     
			}
		break;
		case "citation":
			endTag = "[/quote]";
			if (currentSelection) { // Il y a une sélection
				var auteur = prompt("Entrez l'auteur de la citation...") || "";
				if (auteur == "") startTag = "[quote]";
				else startTag = "[quote=" + auteur + "]";
			} else { // Pas de selection, donc on demande l'Auteur et la Citation
				var auteur = prompt("Entrez l'auteur de la citation...") || "";
				var citation = prompt("Entrez la citation...") || "";
				if (auteur == "") startTag = "[quote]";
				else startTag = "[quote=" + auteur + "]";
				currentSelection = citation;    
			}
		break;
		}
	}
	
	/* === Partie 3 : on insère le tout === */
	if (window.ActiveXObject) {
		textRange.text = startTag + currentSelection + endTag;
		textRange.moveStart("character", -endTag.length - currentSelection.length);
		textRange.moveEnd("character", -endTag.length);
		textRange.select();     
	} else {
		field.value = startSelection + startTag + currentSelection + endTag + endSelection;
		field.focus();
		field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
	}       
}


