/*
	Optimized User-Friendly Forums (OUFF) is a system designed to manage forums.
	Tt is also provided with community tools.
	Copyright (C) 2003  KOZA Charly
	
	This file is part of Optimized User-Friendly Forums (OUFF).
	
	Optimized User-Friendly Forums (OUFF) 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.
	
	Optimized User-Friendly Forums (OUFF) 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 Optimized User-Friendly Forums (OUFF); if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
	19.11.2003 - version 0.2
	KOZA Charly aka Cactusbone : cactusbone@free.fr 
*/

// storeCursor and smiley functions are inspired by the code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
// and improved with www.wikipedia.org javascript code

var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)  && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));

function storeCursor()
{
	var textarea = document.post.message ;
	if (textarea.createTextRange) //test if createTextRange is supported 
		textarea.caretPos = document.selection.createRange().duplicate();
	return;
}

function smiley(code)
{
	var textarea = document.post.message ;
	textarea.focus();
	// IE
	if(document.selection  && !is_gecko)
	{
		if (textarea.createTextRange && textarea.caretPos)//test if createTextRange is supported and caretPos is not empty
		{
			var caretPos = textarea.caretPos;
			if(caretPos.text.charAt(caretPos.text.length - 1) == ' ')
				caretPos.text = code + ' ' ;
			else
				caretPos.text = code ;
		}
		else
			textarea.value += code;
	}
	// Mozilla
	else if(textarea.selectionStart || textarea.selectionStart == '0') 
	{
		var startPos = textarea.selectionStart;
		var scrollTop=textarea.scrollTop;
		textarea.value = textarea.value.substring(0, startPos) + code + textarea.value.substring(startPos, textarea.value.length);
		textarea.focus();
		textarea.scrollTop=scrollTop;
		var cPos=startPos+code.length;
		textarea.selectionStart=cPos;
		textarea.selectionEnd=cPos;
	}
	else
		textarea.value += code;
	
	textarea.focus();
	return;
}

function entoure(debut, fin)
{
	var textarea = document.post.message;
	textarea.focus();
	
	// IE
	if(document.selection  && !is_gecko)
	{
		var theSelection = document.selection.createRange().text;
		if (!theSelection)
		{
			theSelection="";
		}
		document.selection.createRange().text = debut + theSelection + fin;
		textarea.focus();
	}
	// Mozilla
	else if(textarea.selectionStart || textarea.selectionStart == '0') 
	{
 		var startPos = textarea.selectionStart;
		var endPos = textarea.selectionEnd;
		var scrollTop=textarea.scrollTop;
		var myText = (textarea.value).substring(startPos, endPos);
		if(!myText)
		{
			myText="";
		}
		subst = debut + myText + fin;
		textarea.value = textarea.value.substring(0, startPos) + subst + textarea.value.substring(endPos, textarea.value.length);
		textarea.focus();

		var cPos=startPos+(debut.length+myText.length+fin.length);
		textarea.selectionStart=cPos;
		textarea.selectionEnd=cPos;
		textarea.scrollTop=scrollTop;
	}
	// All others
	else
	{
		var text="";
		text=debut+text+fin;
		document.infoform.infobox.value=text;
		// in Safari this causes scrolling
		if(!is_safari) 
		{
			textarea.focus();
		}
		noOverwrite=true;
	}
	// reposition cursor if possible
	if (textarea.createTextRange) textarea.caretPos = document.selection.createRange().duplicate();
	return;
}

function Bold()
{
	entoure('[B]', '[/B]');
	return;
}

function Italic()
{
	entoure("[i]", "[/i]");
	return;
}

function Underline()
{
	entoure("[u]", "[/u]");
	return;
}

function Quote()
{
	// Ici tenter de recuperer ce qui est selectionné pour le recopier
	var q = prompt('Entrez l\'auteur','');
	if(q!="")
		entoure('[q author="' + q + '"]', '[/q]');
	else
		entoure('[q]', '[/q]');
	return;
}

function quote2(auteur,contenu)
{
	var res = replace2("·","'",contenu);
	res = replace2("´","\n",res);
	smiley('[q author="' + auteur + '"]' + res + '[/q]');
	return;
}

function replace2(source, dest, text)
{
	var i=0;
	i=text.indexOf(source,i);
	while(i > -1)
	{
		text = text.substr(0, i) + dest + text.substr(i + source.length);
		i=text.indexOf(source,i);
	}
	return text;
}

function Img()
{
	//ici demander a l'utilisateur le lien vers l'image
	smiley('[img src="' + prompt('Entrez l\'url de l\'image','') + '"]');
	return;
}

function Url()
{
	//ici demander a l'utilisateur le lien qu'il désire
	entoure('[url src="' + prompt('Entrez l\'url','') + '"]', '[/url]');
	return;
}

function couleur()
{
	entoure('[color="' + document.post.colorSelector.options[document.post.colorSelector.selectedIndex].value + '"]', '[/color]');
	document.post.colorSelector.selectedIndex=null
	return;
}

function taille(taille)
{
	entoure('[size="' + document.post.sizeSelector.options[document.post.sizeSelector.selectedIndex].value + '"]', '[/size]');
	document.post.sizeSelector.selectedIndex=null
	return;
}

function noEnterKeyTextArea(textarea, evt)
{
	if((evt.which ? evt.which : evt.keyCode)==13)
	{
		smiley(String.fromCharCode(13));
		return false;
	}
	else
	{
		return true;
	}
}
