function insertTag(startTag, endTag, textareaId, tagType) {
	var field = document.getElementById(textareaId);
	field.focus();
	
	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);
	}
	
	if (tagType) {
		switch (tagType) {
			case "lien":
				endTag = "</lien>";
				if (currentSelection) {
					if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
									var label = prompt("Quel est le libellé du lien ?") || "";
									startTag = "<lien url=\"" + currentSelection + "\">";
									currentSelection = label;
							} else {
									var URL = prompt("Quelle est l'url ?");
									startTag = "<lien url=\"" + URL + "\">";
							}
					} else {
							var URL = prompt("Quelle est l'url ?") || "";
							var label = prompt("Quel est le libellé du lien ?") || "";
							startTag = "<lien url=\"" + URL + "\">";
							currentSelection = label;                     
					}
			break;
			case "citation":
					endTag = "</citation>";
					if (currentSelection) {
							if (currentSelection.length > 30) {
									var auteur = prompt("Quel est l'auteur de la citation ?") || "";
									startTag = "<citation nom=\"" + auteur + "\">";
							} else {
									var citation = prompt("Quelle est la citation ?") || "";
									startTag = "<citation nom=\"" + currentSelection + "\">";
									currentSelection = citation;    
							}
					} else {
							var auteur = prompt("Quel est l'auteur de la citation ?") || "";
							var citation = prompt("Quelle est la citation ?") || "";
							startTag = "<citation nom=\"" + auteur + "\">";
							currentSelection = citation;    
					}
			break;	
		}
	}
	
	if (window.ActiveXObject) {
		textRange.text = startTag + currentSelection + endTag;
		textRange.moveStart('character', -endTag.length-currentSelection.length);
		textRange.moveEnd('character', -endTag.length);
		textRange.select();
	} else { // Ce n'est pas IE
		field.value = startSelection + startTag + currentSelection + endTag + endSelection;
		field.focus();
		field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
	}      
}

function preview(textareaId, previewDiv) {
	var field = textareaId.value;
	if (document.getElementById('previsualisation').checked && field) {
		
		var smiliesName = new Array(':magicien:', ':colere:', ':diable:', ':ange:', ':ninja:', '&gt;_&lt;', ':pirate:', ':zorro:', ':honte:', ':soleil:', ':\'\\(', ':waw:', ':\\)', ':D', ';\\)', ':p', ':lol:', ':euh:', ':\\(', ':o', ':colere2:', 'o_O', '\\^\\^', ':\\-°');
		var smiliesUrl  = new Array('magicien.png', 'angry.gif', 'diable.png', 'ange.png', 'ninja.png', 'pinch.png', 'pirate.png', 'zorro.png', 'rouge.png', 'soleil.png', 'pleure.png', 'waw.png', 'smile.png', 'heureux.png', 'clin.png', 'langue.png', 'rire.gif', 'unsure.gif', 'triste.png', 'huh.png', 'mechant.png', 'blink.gif', 'hihi.png', 'siffle.png');
		var smiliesPath = "http://www.siteduzero.com/Templates/images/smilies/";
	
		field = field.replace(/&/g, '&amp;');
		field = field.replace(/</g, '&lt;').replace(/>/g, '&gt;');
		field = field.replace(/\n/g, '<br />').replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		field = field.replace(/&lt;ligne&gt;/g, '<hr />');
		
		field = field.replace(/&lt;gras&gt;([\s\S]*?)&lt;\/gras&gt;/g, '<strong>$1</strong>');
		field = field.replace(/\[b\]([\s\S]*?)\[\/b\]/g, '<strong>$1</strong>');
		field = field.replace(/&lt;italique&gt;([\s\S]*?)&lt;\/italique&gt;/g, '<em>$1</em>');
		field = field.replace(/\[i\]([\s\S]*?)\[\/i\]/g, '<em>$1</em>');
		field = field.replace(/&lt;souligner&gt;([\s\S]*?)&lt;\/souligner&gt;/g, '<u>$1</u>');
		field = field.replace(/\[u\]([\s\S]*?)\[\/u\]/g, '<u>$1</u>');
		field = field.replace(/&lt;barrer&gt;([\s\S]*?)&lt;\/barrer&gt;/g, '<s>$1</s>');
		field = field.replace(/\[barre\]([\s\S]*?)\[\/barre\]/g, '<s>$1</s>');
		field = field.replace(/&lt;exposant&gt;([\s\S]*?)&lt;\/exposant&gt;/g, '<sup>$1</sup>');
		field = field.replace(/\[exposant\]([\s\S]*?)\[\/exposant\]/g, '<sup>$1</sup>');
		field = field.replace(/&lt;indice&gt;([\s\S]*?)&lt;\/indice&gt;/g, '<sub>$1</sub>');
		field = field.replace(/\[indice\]([\s\S]*?)\[\/indice\]/g, '<sub>$1</sub>');
		field = field.replace(/&lt;liste&gt;([\s\S]*?)&lt;\/liste&gt;/g, '<li>$1</li>');
		field = field.replace(/\[liste\];([\s\S]*?)\[\/liste\]/g, '<li>$1</li>');
		field = field.replace(/&lt;puce&gt;([\s\S]*?)&lt;\/puce&gt;/g, '<ul>$1</ul>');
		field = field.replace(/\[puce\]([\s\S]*?)\[\/puce\]/g, '<ul>$1</ul>');
		field = field.replace(/&lt;num&gt;([\s\S]*?)&lt;\/num&gt;/g, '<ol>$1</ol>');
		field = field.replace(/\[num\]([\s\S]*?)\[\/num\]/g, '<ol>$1</ol>');
		field = field.replace(/&lt;lien&gt;([\s\S]*?)&lt;\/lien&gt;/g, '<a href="$1">$1</a>');
		field = field.replace(/\[url\]([\s\S]*?)\[\/url\]/g, '<a href="$1">$1</a>');
		field = field.replace(/&lt;lien url="([\s\S]*?)"&gt;([\s\S]*?)&lt;\/lien&gt;/g, '<a href="$1">$2</a>');
		field = field.replace(/\[url=([\s\S]*?)\]([\s\S]*?)\[\/url\]/g, '<a href="$1">$2</a>');
		field = field.replace(/\[dl=([\s\S]*?)\]([\s\S]*?)\[\/dl\]/g, '<center><a href="$1"><img src="images/download.png" /></a> <br /><b><font color="#0000FF">$2</font></b></center>');
		field = field.replace(/\[source=([\s\S]*?)\]([\s\S]*?)\[\/source\]/g, '<center><b><u>Source :</u></b> <a href="$1">$2</a></center>');
		field = field.replace(/&lt;image&gt;([\s\S]*?)&lt;\/image&gt;/g, '<img src="$1" alt="Image" />');
		field = field.replace(/\[img\]([\s\S]*?)\[\/img\]/g, '<img src="$1" alt="Image" />');
		field = field.replace(/\[ligne\]/g, '<hr />');
		field = field.replace(/\[img w=([\s\S]*?) h=([\s\S]*?)\]([\s\S]*?)\[\/img\]/g, '<img src="$3" alt="Image" width="$1" height="$2" />');
		field = field.replace(/\[img h=([\s\S]*?) w=([\s\S]*?)\]([\s\S]*?)\[\/img\]/g, '<img src="$3" alt="Image" width="$2" height="$1" />');
		field = field.replace(/&lt;citation nom=\"(.*?)\"&gt;([\s\S]*?)&lt;\/citation&gt;/g, '<br /><span class="citation">Citation : $1</span><div class="citation2">$2</div>');
		field = field.replace(/\[quote=\"(.*?)\"\]([\s\S]*?)\[\/quote\]/g, '<br /><span class="citation">Citation : $1</span><div class="citation2">$2</div>');
		field = field.replace(/&lt;citation&gt;([\s\S]*?)&lt;\/citation&gt;/g, '<br /><span class="citation">Citation</span><div class="citation2">$1</div>');
		field = field.replace(/\[quote\]([\s\S]*?)\[\/citation\]/g, '<br /><span class="citation">Citation</span><div class="citation2">$1</div>');
		field = field.replace(/&lt;taille valeur=\"(.*?)\"&gt;([\s\S]*?)&lt;\/taille&gt;/g, '<span class="$1">$2</span>');
		field = field.replace(/\[size=(.*?)\]([\s\S]*?)\[\/size\]/g, '<span class="$1">$2</span>');
		field = field.replace(/&lt;couleur valeur=\"(.*?)\"&gt;([\s\S]*?)&lt;\/couleur&gt;/g, '<font color="$1">$2</font>');
		field = field.replace(/\[color=(.*?)\]([\s\S]*?)\[\/color\]/g, '<font color="$1">$2</font>');
		field = field.replace(/&lt;position valeur=\"(.*?)\"&gt;([\s\S]*?)&lt;\/position&gt;/g, '<p align="$1">$2</p>');
		field = field.replace(/\[align=(.*?)\]([\s\S]*?)\[\/align\]/g, '<p align="$1">$2</p>');
		field = field.replace(/\[youtube\]([\s\S]*?)\[\/youtube\]/g, '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/$1&hl=fr&fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/$1&hl=fr&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>');
		field = field.replace(/\[googlevideo\]([\s\S]*?)\[\/googlevideo\]/g, '<embed id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid=$1&hl=fr&fs=true" style="width:400px;height:326px" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed>');
		field = field.replace(/\[dailymotion\]([\s\S]*?)\[\/dailymotion\]/g, '<object width="420" height="357"><param name="movie" value="http://www.dailymotion.com/swf/$1"></param><param name="allowfullscreen" value="true"></param><embed src="http://www.dailymotion.com/swf/$1" type="application/x-shockwave-flash" width="420" height="357" allowfullscreen="true"></embed></object>');
		
		for (var i=0, c=smiliesName.length; i<c; i++) {
			field = field.replace(new RegExp(" " + smiliesName[i] + " ", "g"), "&nbsp;<img src=\"" + smiliesPath + smiliesUrl[i] + "\" alt=\"" + smiliesUrl[i] + "\" />&nbsp;");
		}
		
		document.getElementById(previewDiv).innerHTML = field;
	}
}

function previewpseudo(inputId, previewPse) {
	var field = inputId.value;
	if (document.getElementById('previsualisation').checked && field) {
		field = '<span class="sous-titre-news">News &eacute;crite par '+field;
		document.getElementById(previewPse).innerHTML = field;
	}
}

function previewtitre(inputId, previewTit) {
	var field = inputId.value;
	if (document.getElementById('previsualisation').checked && field) {
		document.getElementById(previewTit).innerHTML = field;
	}
}

function previewavatar(inputId, previewAva) {
	var field = inputId.value;
	if (document.getElementById('previsualisation').checked && field) {
		field = '<div class="icones"><img src="'+field+'" alt="avatar" class="spip_logos" /></div>';
		document.getElementById(previewAva).innerHTML = field;
	}
}

function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest();
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}


function view(textareaId, viewDiv){
	var content = encodeURIComponent(document.getElementById(textareaId).value);
	var xhr = getXMLHttpRequest();
	
	if (xhr && xhr.readyState != 0) {
		xhr.abort();
		delete xhr;
	}
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200){
			document.getElementById(viewDiv).innerHTML = xhr.responseText;
		} else if (xhr.readyState == 3){
			document.getElementById(viewDiv).innerHTML = "<div style=\"text-align: center;\">Chargement en cours...</div>";
		}
	}
	
	xhr.open("POST", "view.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("string=" + content);
}

getSelectionStart = function()
{
textarea = document.getElementById("textarea");

	if ( typeof textarea.selectionStart != 'undefined' )
		return textarea.selectionStart;
	
	// IE Support
	textarea.focus();
	var range = textarea.createTextRange();
	range.moveToBookmark(document.selection.createRange().getBookmark());
	range.moveEnd('character', textarea.value.length);

	return textarea.value.length - range.text.length;
}

var compt = 0;
var limite;
var nb_ligne;

function init() {
limite = document.getElementById("textarea").cols;
document.getElementById("pos_cur").value = 0;
document.getElementById("lignes").value = 0;
document.getElementById("lim").value = limite;
}

function test(e) {

textarea = document.getElementById("textarea");

var touche=(window.Event)?e.which:e.keyCode; //pour savoir s'il s'agit de Msie ou de Netscape

if(touche==13) {
compt++;
limite = parseInt(pos_cur.value) + textarea.cols;
}
else {
pos_cur = document.getElementById("pos_cur");
pos_cur.value = getSelectionStart();

if (parseInt(pos_cur.value) >=limite) {
compt++;
limite = parseInt(pos_cur.value) + textarea.cols;
}
}
nb_ligne = compt;

document.getElementById("lignes").value = nb_ligne;
document.getElementById("lim").value = limite;

if (nb_ligne >= textarea.rows) {
textarea.rows ++;
}
}
