// JavaScript Document
function htmlchar( text ) {
  i = text.indexOf(' ');
  while (  i > 1 ) {
  	text = text.substring(0,i) + "%20" + text.substring(i+1);
    i = text.indexOf(' ');
  }
  i = text.indexOf('\n');
  while (  i > 1 ) {
  	text = text.substring(0,i) + "%0D" + text.substring(i+1);
    i = text.indexOf('\n');
  }
  return text;
}


function storeCaret(obj) {
  if (!is_ie)  return true;
  if (obj.createTextRange)
	  obj.caretPos=document.selection.createRange().duplicate();
  //alert("S");
  //obj.caretPos=document.selection.createRange().duplicate();
}

function smilie(text) {
	text = ' ' + text + ' ';
	ftext(text, '');
	return;
	textarea=document.form2.msg;
		if (is_ie && document.form2.msg.createTextRange && document.form2.msg.caretPos) {
		var caretPos = document.form2.msg.caretPos;
		//alert(caretPos);
		caretPos.text = (caretPos.text.charAt(caretPos.text.length - 1) == ' ' )? text + ' ' : text;
		document.form2.msg.focus();
		}
	/*	else 
		
		
		
		
		if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
		
	} 
	
	*/
	
	else {
		document.form2.msg.value  += text;
		document.form2.msg.focus();
	}
}

function CheckQuote() {
	var i,q,uq,last;
	last=0;
	q=0;
	uq=0;
	er=0;
	sp=0;
	ers="";
	test=document.form2.msg.value;
	i=test.indexOf('[');
	while ( i != -1 ) {
	
				if ( test.substring(i+1,i+1+7)=='spoiler]' ) {
					if (q >0 || uq > 0) { 
						er=1;ers="quotatura non chiusa";
						break;
					}
					if (sp >0) {
						er=1;ers="spoiler già aperto";
						break;				
					}
					sp++;
				}
				
				if ( test.substring(i+1,i+1+8)=='/spoiler]' ) {
					if (sp  <1) {
						er=1;ers="spoiler chiuso, ma nn aperto";
						break;				
					}
					sp--;
				}				
	
		if ( test.substring(i+1,i+1+6)=='quote]' ) {
			q=q+1;
			if (last==1) {
				er=1;
                ers="Non puoi aprire un tag [quote] all'interno di [uquote] !";
				break;
			}
		}
		if ( test.substring(i+1,i+1+7)=='/quote]' ) {
			q=q-1;
			if (last==1) {
				er=1;
                ers="Non puoi chiudere un tag [/quote] devi prima chiudere  [/uquote] !";
				break;
			}
			if ( q < 0 ) {
				er=1;
				ers="Hai chiuso un tat [/quote] senza che sia stato aperto [quote]";
				break;
			}
		}
		if ( test.substring(i+1,i+1+7)=='uquote]' ) {
			uq=uq+1;
			if (last==1) {
				er=1;
                ers="Non puoi aprire un tag [uquote] all'interno di un'altro [uquote] !";
				break;
			}
			if (q==0) {
				er=1;
                ers+="Non puoi aprire un tag [uquote] se non apri prima [quote] !";
				break;
			}
		}
		if ( test.substring(i+1,i+1+8)=='/uquote]' ) {
			uq=uq-1;
			if (last==1) {
				er=1;
                ers="Non puoi chiudere un tag [/quote] devi prima chiudere  [/uquote] !";
				break;
			}
			if ( uq < 0 ) {
				er=1;
				ers+="Hai chiuso un tag [/uquote] senza che sia stato aperto [uquote]";
				break;
			}
			
		}
		i=test.indexOf('[',i+1);
	}
	if (q != 0 && er==0)  { ers=" Non hai chiuso " + q + " tag [quote] ";er=1};
	if (uq != 0 && er==0) {ers=" Non hai chiuso " + uq + " tag [uquote] ";er=1};
	if (sp !=0 && er==0) {ers =" Non hai chiuso uno [spoiler]";er=1};
	if (er==1) alert(ers);
	return (er==0);
}
 
function helpline(help) {
	if (help != '1')
		document.form2.helpbox.value = eval(help + "_help");
}

var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav  = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_win   = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac    = (clientPC.indexOf("mac")!=-1);
// Helpline messages
b_help = "Grassetto: <b>testo</b>  (alt+b)";
i_help = "Corsivo: <i>testo</i>  (alt+i)";
u_help = "Sottolineato: <u>testo</u>  (alt+u)";
q_help = "Citazione: [quote]testo[/quote]  (alt+q)";
c_help = "Codice: [code]codice[/code]  (alt+c)";
l_help = "Lista: [list]testo[/list] (alt+l)";
o_help = "Lista ordinata: [list=]testo[/list]  (alt+o)";
p_help = "Inserisci un'immagine: [img]http://image_url[/img]  (alt+p)";
w_help = "Inserisci URL: [link]http://link[/link] alt+w)";
//o [url=http://url]testo URL[/url] 
a_help = "Inserisce la lettera accentata";
//s_help = "Colore carattere: [color=red]testo[/color]  Suggerimento: puoi anche usare color=#FF0000";
f_help = "Dimensione carattere: [size=x-small]testo piccolo[/size]";
s_help ="Per le anticipazioni: [spoiler] Anticipazione [/spoiler]";

lh_help ="Lo Hobbit: Lo Hobbit, J. R. R. Tolkien, Ed. Adelphi, tradotto da Elena Jeronimidis Conte.";
lha_help ="Lo Hobbit Annotato: Lo Hobbit Annotato, J. R. R. Tolkien, Ed. Mondolibri SpA, tradotto da tradotto da Elena Jeronimidis Conte. Note di Douglas A. Anderson tradotte da Grazia Maria Griffini.";
isda_help ="Il Signore degli Anelli: Il Signore degli Anelli, J. R. R. Tolkien, Ed. Rusconi, tradotto da Vicky Alliata di Villafranca.";
silm_help ="Il Silmarillion: Il Silmarillion, J. R. R. Tolkien, Ed. Rusconi, tradotto da Francesco Saba Sardi.";
rr_help ="Racconti Ritrovati: Racconti Ritrovati, J. R. R. Tolkien, Ed. Rusconi, tradotto da Cinzia Pieruccini.";
rp_help ="Racconti Perduti: Racconti Perduti, J. R. R. Tolkien, Ed. Rusconi, tradotto da Cinzia Pieruccini.";

cda_help="La Compagnia dell'Anello: La Compagnia dell'Anello, di Peter Jackson, Medusa Film, New Line Cinema.";
ldt_help="Le Due Torri: Le Due Torri, di Peter Jackson, Medusa Film, New Line Cinema.";
rdr_help="RdR o RotK o Il ritorno del Re: Il ritorno del Re, di Peter Jackson, Medusa Film, New Line Cinema.";
def_help="usa i tastini per applicare lo stile o le citazioni con un click";




function ftext(bbopen, bbclose) {
	textarea=document.form2.msg;
	text1=bbopen;
	text2=bbclose;
	if ( (clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			document.form2.msg.value += bbopen + bbclose;
			document.form2.msg.focus();
			storeCaret(document.form2.msg);
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		document.form2.msg.focus();
		storeCaret(document.form2.msg);
		return;
		
	} 
	
	if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + bbopen + selection + bbclose + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	
	
	else {
		document.form2.msg.value += bbopen + bbclose;
		document.form2.msg.focus();
		return;
	}
	//storeCaret(document.form2.msg);
}
     



