/**
*	Connectix Boards 0.8, free interactive php bulletin boards.
*	Copyright (C) 2005-2007  Jasienski Martin.
*
*	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 3 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 can find a copy of the GNU General Public License at 
*	<http://www.connectix-boards.org/license.txt>.
*/

/** 
* Contient toutes les fonctions relatives au formulaire interactif de rédaction de messages
* de Connectix Boards.
* 
* Ce fichier nécéssite l'inclusion préalable de cb_functions.js !!!!
*/

/* FONCTION D'AJOUT DE SMILEYS */
function emoticon(smiley,taid) {
	insert(taid," " + smiley + " ","");
}

/* FONCTIONS DE BBCODE */
// Insertion de n'importe quoi (smileys, bbcodes, ...) dans le textarea taid
function insert(taid,tag_begin,tag_end) {
	checkRange(taid);
	if (promptOpened) removePrompt();
	
	var textarea = document.getElementById(taid);
	var scroll = textarea.scrollTop;
	
	if (Mozilla) {
		var sel_newcursorpos = tag_begin.length + textarea.selectionEnd + (isTextSelected(taid)?tag_end.length:0);
		var sel_remember = isTextSelected(taid);
		var sel_start = textarea.selectionStart;
		
		textarea.value = textarea.value.substring(0 , textarea.selectionStart) 
			+ tag_begin
			+ textarea.value.substring(textarea.selectionStart ,textarea.selectionEnd)
			+ tag_end
			+ textarea.value.substring(textarea.selectionEnd , textarea.value.length);
		
		textarea.setSelectionRange(sel_remember ? sel_start : sel_newcursorpos,sel_newcursorpos);
	} else if (document.selection) {
		textarea.focus();
		
		var sel_remember = isTextSelected(taid);
		var range = document.selection.createRange();
		var sel_length = range.text.length + tag_begin.length + tag_end.length;
		
		range.text = tag_begin + range.text + tag_end;
		if (sel_remember)
			range.moveStart('character',-sel_length);
		else
			range.moveEnd('character',-tag_end.length);
		
		range.select();
	} else {
		textarea.value += tag_begin + tag_end;
	}
	
	textarea.focus();
	textarea.scrollTop = scroll;
}
// Détermine s'il y a du texte sélectionné dans le textarea taid
function isTextSelected (taid) {
	var textarea = document.getElementById(taid);
	if (Mozilla && (textarea.selectionEnd - textarea.selectionStart) > 0)
		return true;
	else if (Mozilla)
		return false;
	else if (rememberRange != null && rememberRange.text.length >0)
		return true;
	else if (rememberRange != null)
		return false;
	else if (document.selection.createRange().text.length > 0)
		return true;
	else
		return false;
}
// Gestion d'un tag normal
function tag (tag, taid) {
	insert(taid,"[" + tag + "]","[/" + tag + "]");
}
// Gestion du tag d'url
function tag_url (taid) {
    cbPrompt(taid, "tag_url_end", form_url, "http://");
}
function tag_url_end (taid,input_url) {
    insert(taid,"[url="+input_url+"]","[/url]");
}
// Gestion du tag d'image
function tag_image (taid) {
    if (!isTextSelected(taid))
		cbPrompt(taid, "tag_image_end", form_img, "http://");
	else
		insert(taid,"[img]","[/img]");
}
function tag_image_end (taid,input_url) {
	insert(taid,"[img]"+input_url,"[/img]");
}
// Gestion du tag d'e-mail
function tag_email (taid) {
    cbPrompt(taid, "tag_email_end", form_mail, "me@provider.com");
}
function tag_email_end (taid,email) {
    insert(taid,"[email="+email+"]","[/email]");
}
// Gestion des tag de sélection
function tag_select (select,taid,tag_name) {
	var tag_value =  select.options[select.options.selectedIndex].value;

	select.options[0].selected = true;
	
	if (tag_value && tag_value != 'none')
		insert(taid,"["+tag_name+"="+tag_value+"]","[/"+tag_name+"]");
}

/* PROMPT PERSONNALISE POUR FORMULAIRE DE MESSAGES */
var promptOpened = false;
var rememberRange = null;
function cbPrompt (taid , endfunction , fmessage , value) {
	if (document.selection && rememberRange == null) {
		document.getElementById(taid).focus();
		rememberRange = document.selection.createRange();
	}
	
	if (promptOpened) removePrompt();
	
	var cbprompt = document.createElement('div');
	cbprompt.id = 'cbprompt';
	cbprompt.innerHTML = 
		'<form onsubmit="return false;">'+
		'<p id="prompt_message">'+fmessage+'</p>'+
		'<p id="prompt_form">'+
			'<input type="text" name="prompt_value" id="prompt_value" value="'+value+'" onkeyup="if(event.keyCode==13) managePrompt(true,\''+taid+'\',\''+endfunction+'\');" />'+
		'</p>'+
		'<p id="prompt_submit">'+
			'<input type="button" name="prompt_ok" id="prompt_ok" value="Ok" onclick="managePrompt(true,\''+taid+'\',\''+endfunction+'\');" />'+
			'<input type="button" name="prompt_cancel" id="prompt_cancel" value="Cancel" onclick="managePrompt(false,\''+taid+'\',\''+endfunction+'\');" />'+
		'</p>'+
		'</form>';
	
	document.getElementById('main').appendChild(cbprompt);
	
	cbprompt.focus();
	selectAll(document.getElementById('prompt_value'));
	promptOpened = true;
	document.getElementById(taid).disabled = true;
}
function managePrompt (ok,taid,endfunc) {
	if (ok) 
		eval(endfunc+'("'+taid+'","'+document.getElementById('prompt_value').value+'");');
	
	checkRange(taid);
	if (promptOpened) removePrompt();
}
function removePrompt() {
	var cbprompt = document.getElementById('cbprompt');
	cbprompt.style.display = "none";
	cbprompt.innerHTML = "";
	cbprompt.id = null;
	cbprompt = null;
	promptOpened = false;
}

// Fonction qui sélectionne tout le contenu d'un champ
function selectAll (field) {
	if (Mozilla) {
		field.setSelectionRange(0,field.value.length);
	} else if (document.selection) {
		field.focus();
		
		var range = document.selection.createRange();
		range.moveStart('character',-range.offsetLeft);
		range.moveEnd('character',field.value.length);
		
		range.select();
	}
	field.focus();
}

// Fonction nécessaire pour se permettre quelques fantaisies avec IE
function checkRange(taid) {
	var textarea = document.getElementById(taid);
	textarea.disabled = false;
	if (rememberRange != null) {
		textarea.focus();
		rememberRange.select();
		rememberRange = null;
	}
}
