var theSelection = false;

var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion);

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);

b_help = "Negrita: [B]text[/B]";
i_help = "Italica: [I]text[/I]";
u_help = "Subrayado: [U]text[/U]";
q_help = "Cita: [QUOTE]text[/QUOTE]";
c_help = "Codigo: [CODE]code[/CODE]";
p_help = "Imagen: [IMG]http://image_url[/IMG]";
w_help = "Url: [URL=http://url]URL text[/URL]";
s_help = "Color fuente: [COLOR=red]text[/COLOR] Tip: can also use HTML color=#FF0000";
f_help = "Tamaņo fuente: [SIZE=9]small text[/SIZE]";

var Quote = 0;
var Bold  = 0;
var Italic = 0;
var Underline = 0;
var Code = 0;

function helpline(help) {
	document.post.helpbox.value = eval(help + "_help");
	document.post.helpbox.readOnly = "true";
}

function checkForm() {
	formErrors = false;    
	if (document.post.elements[campomesaje].value.length < 2) {
		formErrors = "You must enter a message when posting";
	}
	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		//formObj.preview.disabled = true;
		//formObj.submit.disabled = true;
		return true;
	}
}

function emoticon(text,campomesaje) {
	text = ' ' + text + ' ';
	PostWrite(text,campomesaje);
}

function bbfontstyle(bbopen, bbclose,campomesaje) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			document.post.elements[campomesaje].value += bbopen + bbclose;
			document.post.elements[campomesaje].focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		document.post.elements[campomesaje].focus();
		return;
	} else {
		document.post.elements[campomesaje].value += bbopen + bbclose;
		document.post.elements[campomesaje].focus();
		return;
	}
	storeCaret(document.post.elements[campomesaje]);
}

function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

function PostWrite(text,campomesaje) {
	var campomesaje;
	if (document.post.elements[campomesaje].createTextRange && document.post.elements[campomesaje].caretPos) {
		var caretPos = document.post.elements[campomesaje].caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?	text + ' ' : text;
	}
	else document.post.elements[campomesaje].value += text;
	document.post.elements[campomesaje].focus(caretPos)
}

function BBCcode(campomesaje) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (theSelection != '') {
		document.selection.createRange().text = "[CODE]" + theSelection + "[/CODE]";
		document.post.elements[campomesaje].focus();
		return;
		}
	}
	if (Code == 0) {
		ToAdd = "[CODE]";
		document.post.code.value = "Codigo*";
		Code = 1;
	} else {
		ToAdd = "[/CODE]";
		document.post.code.value = "Codigo";
		Code = 0;
	}
	PostWrite(ToAdd,campomesaje);
}

function BBCquote(campomesaje,xusernames) {
	var xusernames = xusernames;
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (theSelection != '') {
		document.selection.createRange().text = "[QUOTE]" + theSelection + "[/QUOTE]";
		document.post.elements[campomesaje].focus();
		return;
		}
	}
	if (Quote == 0) {
		ToAdd = "[QUOTE="+ xusernames +"]";
		document.post.quote.value = "Cita*";
		Quote = 1;
	} else {
		ToAdd = "[/QUOTE]";
		document.post.quote.value = "Cita";
		Quote = 0;
	}
	PostWrite(ToAdd,campomesaje);
}

function BBCbold(campomesaje) {
	var campomesaje;
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (theSelection != '') {
		document.selection.createRange().text = "[B]" + theSelection + "[/B]";
		document.post.elements[campomesaje].focus();
		return;
		}
	}
	if (Bold == 0) {
		ToAdd = "[B]";
		document.post.bold.value = "B*";
		Bold = 1;
	} else {
		ToAdd = "[/B]";
		document.post.bold.value = "B";
		Bold = 0;
	}
	PostWrite(ToAdd,campomesaje);
}

function BBCitalic(campomesaje) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (theSelection != '') {
		document.selection.createRange().text = "[I]" + theSelection + "[/I]";
		document.post.elements[campomesaje].focus();
		return;
		}
	}
	if (Italic == 0) {
		ToAdd = "[I]";
		document.post.italic.value = "i*";
		Italic = 1;
	} else {
		ToAdd = "[/I]";
		document.post.italic.value = "i";
		Italic = 0;
	}
	PostWrite(ToAdd,campomesaje);
}

function BBCunder(campomesaje) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (theSelection != '') {
		document.selection.createRange().text = "[U]" + theSelection + "[/U]";
		document.post.elements[campomesaje].focus();
		return;
		}
	}
	if (Underline == 0) {
		ToAdd = "[U]";
		document.post.under.value = "U*";
		Underline = 1;
	} else {
		ToAdd = "[/U]";
		document.post.under.value = "U";
		Underline = 0;
	}
	PostWrite(ToAdd,campomesaje);
}

function BBCurl(campomesaje) {
	var FoundErrors = '';
	var explorersecurity = "\n\nSi utilizas Iexplorer\ntienes que permitir que se abra la ventana emergente";
	var enterURL   = prompt("Inserta url", "http://");
	var enterTITLE = prompt("Titulo de la pagina", "Tiitulo");
	if (!enterURL)    {
		FoundErrors += " Tienes que insertar una url!\n";
	}
	if (!enterTITLE)  {
		FoundErrors += " Tienes que insertar un titulo para la url!";
	}
	if (FoundErrors)  {
		alert("Error!\n\n"+FoundErrors+explorersecurity);
		return;
	}
	var ToAdd = "[URL="+enterURL+"]"+enterTITLE+"[/URL]";
	PostWrite(ToAdd,campomesaje);
}

function BBCimg(campomesaje) {
	var FoundErrors = '';
	var explorersecurity = "\n\nSi utilizas Iexplorer\ntienes que permitir que se abra la ventana emergente";
	
	var enterURL   = prompt("Inserta la url de tu imagen","http://");
	if (!enterURL) {
		FoundErrors += " No has insertado ninguna url!";
	}
	if (FoundErrors) {
		alert("Error!\n\n"+FoundErrors+explorersecurity);
		return;
	}
	var ToAdd = "[IMG]"+enterURL+"[/IMG]";
	PostWrite(ToAdd,campomesaje);
}
