// Modified by Volker Puttrich

var B_open = 0;
var I_open = 0;
var U_open = 0;
var QUOTE_open = 0;
var CODE_open = 0;
var SQL_open = 0;
var HTML_open = 0;

var bbtags   = new Array();

var myAgent   = navigator.userAgent.toLowerCase();
var myVersion = parseInt(navigator.appVersion);

var is_ie   = ((myAgent.indexOf("msie") != -1)  && (myAgent.indexOf("opera") == -1));
var is_nav  = ((myAgent.indexOf('mozilla')!=-1) && (myAgent.indexOf('spoofer')==-1)
                && (myAgent.indexOf('compatible') == -1) && (myAgent.indexOf('opera')==-1)
                && (myAgent.indexOf('webtv') ==-1)       && (myAgent.indexOf('hotjava')==-1));

var is_win   =  ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1));
var is_mac    = (myAgent.indexOf("mac")!=-1);

var allcookies = document.cookie;
var pos = allcookies.indexOf("bbmode=");

prep_mode();

function ConvertBR(input) {
	// Converts carriage returns 
	// to <BR> for display in HTML
	var output = "";
	for (var i = 0; i < input.length; i++) {
		if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) {
			i++;
			output += "<BR>";
		} else {
			output += input.charAt(i);
	   }
	}
	return output;
}

function IniciaConversion(sms){
	return ConvertBR(sms);
}

function prep_mode()
{
	if (pos != 1) {
		var cstart = pos + 7;
		var cend   = allcookies.indexOf(";", cstart);
		if (cend == -1) { cend = allcookies.length; }
		cvalue = allcookies.substring(cstart, cend);
		
	} 
	else {
		document.REPLIER.bbmode[1].checked = true;
	}
}

function setmode(mVal)
{
	document.cookie = "bbmode="+mVal+"; path=/; expires=Wed, 1 Jan 2020 00:00:00 GMT;";
}

function get_easy_mode_state()
{
	if (document.REPLIER.bbmode[0].checked) {
		return true;
	}
	else {
		return false;
	}
}

function cstat()
{
	var c = stacksize(bbtags);
	
	if ( (c < 1) || (c == null) ) {
		c = 0;
	}
	
	if ( ! bbtags[0] ) {
		c = 0;
	}
	
	document.REPLIER.tagcount.value = c;
}

function stacksize(thearray)
{
	for (i = 0 ; i < thearray.length; i++ ) {
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') ) {
			return i;
		}
	}
	
	return thearray.length;
}

function pushstack(thearray, newval)
{
	arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

function popstack(thearray)
{
	arraysize = stacksize(thearray);
	theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}

function closeall()
{
	if (bbtags[0]) {
		while (bbtags[0]) {
			tagRemove = popstack(bbtags)
			document.REPLIER.texto.value += "</" + tagRemove + ">";
			
			if ( (tagRemove != 'FONT') && (tagRemove != 'SIZE') && (tagRemove != 'COLOR') )
			{
				eval("document.REPLIER." + tagRemove + ".value = ' " + tagRemove + " '");
				eval(tagRemove + "_open = 0");
			}
		}
	}
	
	document.REPLIER.tagcount.value = 0;
	bbtags = new Array();
	document.REPLIER.texto.focus();
}

function emoticon(theSmilie)
{
	doInsert(" " + theSmilie + " ", "", false);
}

function alterfont(theval, thetag)
{
    if (theval == 0)
    	return;
	
	if(doInsert("<" + thetag + " " + theval + ">", "</" + thetag + ">", true))
		pushstack(bbtags, thetag);
	
    document.REPLIER.ffont.selectedIndex  = 0;
    document.REPLIER.fcolor.selectedIndex = 0;
	
}

function simpletag(thetag)
{
	var tagOpen = eval(thetag + "_open");
	
	if ( get_easy_mode_state() ) {
		inserttext = prompt(prompt_start + "\n<" + thetag + ">xxx[<" + thetag + ">");
		if ( (inserttext != null) && (inserttext != "") ) {
			doInsert("<" + thetag + ">" + inserttext + "</" + thetag + "> ", "", false);
		}
	}
	else {
		if (tagOpen == 0) {
			if(doInsert("<" + thetag + ">", "</" + thetag + ">", true)){
				eval(thetag + "_open = 1");
				eval("document.REPLIER." + thetag + ".value += '*'");
		
				pushstack(bbtags, thetag);
				cstat();
			}
		}
		else {
			lastindex = 0;
			
			for (i = 0 ; i < bbtags.length; i++ ) {
				if ( bbtags[i] == thetag ) {
					lastindex = i;
				}
			}
			while (bbtags[lastindex]) {
				tagRemove = popstack(bbtags);
				doInsert("</" + tagRemove + ">", "", false)
				
				eval("document.REPLIER." + tagRemove + ".value = ' " + tagRemove + " '");
				eval(tagRemove + "_open = 0");
			}
			
			cstat();
		}
	}
}


function tag_list()
{
	var listvalue = "init";
	var thelist = "<UL>\n";
	
	while ( (listvalue != "") && (listvalue != null) ) {
		listvalue = prompt("Valores de la lista [Cuando termine de a cancelar]:", "");
		if ( (listvalue != "") && (listvalue != null) ) {
			thelist = thelist+"\t<LI>"+listvalue+"</LI>\n";
		}
	}

	doInsert(thelist + "</UL>\n", "", false);
}

function tag_url()
{
    var FoundErrors = '';
    var enterURL   = prompt("¿Cuál es la dirección de la página?", "http://");
    var enterTITLE = prompt("Nombre del enlace", "Titulo de la página");

    if (!enterURL) {
        FoundErrors += " " + "No has puesto ninguna dirección.";
    }
    if (!enterTITLE) {
        FoundErrors += " " + "No has seleccionado ningún nombre al vínculo.";
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("<A HREF='"+enterURL+"'>"+enterTITLE+"</A>", "", false);
}

function tag_img()
{
    var FoundErrors = '';
    var enterURL   = prompt("¿Cuál es la dirección de la imagen?", "http://");
    var enterWidth = prompt("Ancho de la imagen", "");
    var enterHeight = prompt("Alto de la imagen", "");
	var TenterWidth = '';
	var TenterHeight = '';

    if (!enterURL) {
        FoundErrors += " " + "No has puesto ninguna dirección.";
    }
    if (enterWidth) {
        TenterWidth = "width = " + enterWidth;
    }
    if (enterHeight) {
        TenterHeight = "height = " + enterHeight;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }
	doInsert("<IMG SRC='"+enterURL+"' "+TenterWidth+" "+TenterHeight+" />" , "" , false );
}

function tag_email()
{
    var emailAddress = prompt("Direccion de correo electronico: ", "");

    if (!emailAddress) { 
		alert(error_no_email); 
		return; 
	}

	doInsert("<A HREF='MAILTO:"+emailAddress+"'>"+emailAddress+"</A>", "", false);
}

function doInsert(ibTag, ibClsTag, isSingle)
{
	var isClose = false;
	var obj_ta = document.REPLIER.texto;

	if ( (myVersion >= 4) && is_ie && is_win)
	{
		if(obj_ta.isTextEdit){ 
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null){
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					isClose = true;
	
				rng.text = ibTag;
			}
		}
		else{
			if(isSingle)
				isClose = true;
	
			obj_ta.value += ibTag;
		}
	}
	else
	{
		if(isSingle)
			isClose = true;

		obj_ta.value += ibTag;
	}

	obj_ta.focus();

	return isClose;
}	
