function bbcode(debut, fin)
	{
	var input = window.document.formulaire.texte;
	input.focus();
	
	var vscroll = input.scrollTop;
	
	if(typeof document.selection != 'undefined')
		{
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = debut + insText + fin;
		range = document.selection.createRange();
		if (insText.length == 0)
			{
			range.move('character', -fin.length);
			}
		else
			{
			range.moveStart('character', debut.length + insText.length + fin.length);
			}
		range.select();
		}
	
	else if(typeof input.selectionStart != 'undefined')
		{
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + debut + insText + fin + input.value.substr(end);
		var pos;
		if (insText.length == 0)
			{
			pos = start + debut.length;
			}
		else
			{
			pos = start + debut.length + insText.length + fin.length;
			}
		input.selectionStart = pos;
		input.selectionEnd = pos;
		}
	else
		{
		var pos;
		var re = new RegExp('^[0-9]{0,3}$');
		while(!re.test(pos))
			{
			pos = prompt("insertion (0.." + input.value.length + "):", "0");
			}
		if(pos > input.value.length)
			{
			pos = input.value.length;
			}
		var insText = prompt("Veuillez taper le texte");
		input.value = input.value.substr(0, pos) + debut + insText + fin + input.value.substr(pos);
		}
	
	input.scrollTop = vscroll;
	}

// Bulles

plink = 0;
pimageweb = 0;
pvideo = 0;

function bblink()
	{
	bbcode('[url='+document.getElementById('linkurl').value+']','[/url]');
	hide('link');
	plink=0;
	document.getElementById('linkurl').value = '';
	}

function bbvideo()
	{
	bbcode('[video]'+document.getElementById('videourl').value+'[/video]','');
	hide('video');
	pvideo=0;
	document.getElementById('videourl').value = '';
	}

function bbimage()
	{
	var align = '';
	
	bbcode('[img]'+document.getElementById('imageurl').value+align+'[/img]','');
	hide('image_web');
	pimageweb=0;
	document.getElementById('imageurl').value = '';
	}

function bbupimage()
	{
	var align = '';
	var imageuploaded = $m("upload").innerHTML.substring(2,$m("upload").innerHTML.length-1);
	
	if(document.getElementById('imageupalign1').checked) { align = '|left'; document.getElementById('imageupalign1').checked = false; }
	if(document.getElementById('imageupalign2').checked) { align = '|center'; document.getElementById('imageupalign2').checked = false; }
	if(document.getElementById('imageupalign3').checked) { align = '|right'; document.getElementById('imageupalign3').checked = false; }
	
	bbcode('[img]'+imageuploaded+align+'[/img]','');
	
	hide('image_upload');
	hide('image_upload_align');
	show('upload');
	show('uploadinput');
	$m('uploadinput').value = '';
	$m('upload').innerHTML = '';
	}

// Upload

function $m(theVar){
	return document.getElementById(theVar)
}

function remove(theVar){
	var theParent = theVar.parentNode;
	theParent.removeChild(theVar);
	
	if($m("upload").innerHTML.substr(0,2) == "ul")
		{
		$m("upload").style.display = 'none';
		$m("image_upload_align").style.display = 'block';
		}
}

function addEvent(obj, evType, fn){
	if(obj.addEventListener)
	    obj.addEventListener(evType, fn, true)
	if(obj.attachEvent)
	    obj.attachEvent("on"+evType, fn)
}

function removeEvent(obj, type, fn){
	if(obj.detachEvent){
		obj.detachEvent('on'+type, fn);
	}else{
		obj.removeEventListener(type, fn, false);
	}
}

function isWebKit(){
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
}

function ajaxUpload(form)
	{
	var attr_target = form.getAttribute('target');
	var attr_action = form.getAttribute('action');
	var attr_enctype = form.getAttribute('enctype');
	var attr_encoding = form.getAttribute('encoding');
	
	var detectWebKit = isWebKit();
	form = typeof(form)=="string"?$m(form):form;
	
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","ajax-temp");
	iframe.setAttribute("name","ajax-temp");
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	form.parentNode.appendChild(iframe);
	window.frames['ajax-temp'].name="ajax-temp";
	
	var doUpload = function(){
		removeEvent($m('ajax-temp'),"load", doUpload);
		var cross = "javascript: ";
		cross += "window.parent.$m('upload').innerHTML = document.body.innerHTML; void(0);";
		$m('ajax-temp').src = cross;
		if(detectWebKit) {
        	remove($m('ajax-temp'));
        } else {
        	setTimeout(function(){ remove($m('ajax-temp'))}, 250);
        }
		
		form.setAttribute('target',attr_target);
		form.setAttribute('action',attr_action);
		form.setAttribute('enctype',attr_enctype);
		form.setAttribute('encoding',attr_encoding);
    }
	
	addEvent($m('ajax-temp'),"load", doUpload);
	form.setAttribute("target","ajax-temp");
	form.setAttribute("action","js/upload.php");
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	$m("uploadinput").style.display = 'none';
	$m("upload").innerHTML = '<img src="../images/loader.gif" /> &nbsp;<b>Envoi...</b>';
	form.submit();
	}

// Apercu

function apercuforum()
	{
	var inputtext = $('#texte').val();
	
	if($("#apercusujet").length > 0)
		{
		var inputtitre = $('#titre').val();
		if(inputtitre == "") { inputtitre = "Pense à donner un titre à ce sujet ;)"; }
		$("#apercusujet").html(inputtitre);
		
		$("#apercusujet").parent().slideDown();
		}
		
	$.post("../js/apercuforum.php",{inputtext:inputtext},
	function(data)
		{
		$('#apercupost').html(data.formatext);
		$('.messageapercu').show();
		$('#apercuposta').fadeIn(700);
		},"json");
	}

function apercun()
	{
	var inputtext = $('#texte').val();
		
	$.post("../js/apercun.php",{inputtext:inputtext},
	function(data)
		{
		$('#apercupost').html(data.formatext);
		},"json");
	}

// Signaler

function signaler(typea,idm)
	{
	if(confirm('Es-tu sûr de vouloir signaler ce message comme indésirable ?'))
		{
		$.post("../js/signaler.php",{idm:idm,type:typea},
		function(data)
			{
			alert("Le message a été signalé. Merci pour ton aide ;)");
			},"json");
		}
	}

// Quote

function forumquote(mesid,user)
	{
	$.post("../js/quote.php",{mesid:mesid},
	function(data)
		{
		$('#texte').append('[quote="'+user+'"]'+data.formatext+'[/quote]');
		},"json");
	}
