/*
	Copyright (C) 2006 - 2009 COLIN François

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

	//Fonction permettant demander avant d'aller à une page de suppression
	function vraimentSupprimer(lien) {
		if (confirm("Vraiment supprimer ?\nCeci est définitif et irrémédiable !")) {
			window.location.href = lien;
		}
	}
	
	// Fonction pour afficher / masquer un objet
	function affEffDescr(idObj){
		if (document.getElementById(idObj).style.display == 'block') {
			document.getElementById(idObj).style.display = 'none';
		} else {
			document.getElementById(idObj).style.display = 'block';
		}
		return false;
	}
	
	//Fonctions d'ajout des balises de mise en forme	
	function balisePMod(id_textarea, champ_valeur) {
		if (document.getElementById(champ_valeur).value != '') {
			balise_debut = 'p' + document.getElementById(champ_valeur).value + '. ';
			balise(balise_debut, '', id_textarea);
		}
	}
	
	function baliseDonnees(balise_debut, balise_milieu, balise_fin, id_textarea) {
		var donnees = prompt("Entrez les données nécessaires (adresse du lien, de l'image, numéro de la note de pied de page, ...)");
		balise_fin = balise_milieu + donnees + balise_fin;
		balise(balise_debut, balise_fin, id_textarea);
	}
	
	function balise(balise_debut, balise_fin, id_textarea) {
		var champ = document.getElementById(id_textarea);
		var scroll = champ.scrollTop;
		balise_debut = remplace(balise_debut, '<br />', "\n");
/*
		if( balise_fin == '') {
			balise_debut = ' ' + balise_debut + ' ';
		}
*/
		if (champ.curseur) {
			champ.curseur.text = balise_debut + champ.curseur.text + balise_fin;
			
		} else if (champ.selectionStart >= 0 && champ.selectionEnd >= 0) {
			var debut = champ.value.substring(0, champ.selectionStart);
			var entre = champ.value.substring(champ.selectionStart, champ.selectionEnd);
			var fin = champ.value.substring(champ.selectionEnd);
			champ.value = debut + balise_debut + entre + balise_fin + fin;
			champ.focus();
			champ.setSelectionRange(debut.length + balise_debut.length, champ.value.length - fin.length - balise_fin.length);
			
		} else {
			champ.value += balise_debut + balise_fin;
			champ.focus();
		}
		champ.scrollTop = scroll;
	}
	function remplace(data, search, replace)
	{
		var temp = data;
		var longueur = search.length;
		
		while (temp.indexOf(search) > -1) {
			pos = temp.indexOf(search);
			temp = (temp.substring(0, pos) + replace + temp.substring((pos + longueur), temp.length));
		}
		
		return temp;
	}
	
	/* Fonctions AJAX */
	
	// Fonction de récupération
	function textilise(idZoneTexte, idZoneAUpdater) { 
		var xhr; 
		try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
		catch (e) 
		{
			try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
			catch (e2) 
			{
				try {  xhr = new XMLHttpRequest();     }
				catch (e3) {  xhr = false;   }
			}
		}
		
		xhr.onreadystatechange = function()	{ 
			if(xhr.readyState == 4) {
				if(xhr.status == 200) 
					document.getElementById(idZoneAUpdater).innerHTML = xhr.responseText;
				else 
					document.getElementById(idZoneAUpdater).innerHTML = "<p>Réponse en attente...</p>";
			}
		};

		xhr.open("POST", "ajax.textilise.php",  true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("texte=" + document.getElementById(idZoneTexte).value);
	} 