// Modified by Volker Puttrich
var b_open = 0;
var i_open = 0;
var u_open = 0;

var left_open = 0;
var center_open = 0;
var right_open = 0;
var justify_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 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);
		
		if (cvalue == 'ezmode') {
			document.REPLIER.bbmode[0].checked = true;
		} else {
			document.REPLIER.bbmode[1].checked = true;
		}
	} 
	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 hstat(msg)
{
	document.REPLIER.helpbox.value = eval( "help_" + msg );
}

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.Contenido.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.Contenido.focus();
}

function emoticon(theSmilie)
{
	doInsert(" " + theSmilie + " ", "", false);
}

function add_code(NewCode)
{
    document.REPLIER.Contenido.value += NewCode;
    document.REPLIER.Contenido.focus();
}

function alterfont(theval, thetag)
{
    if (theval == 0)
    	return;
	
	if(doInsert("<" + thetag + " " + theval + ">", "</" + thetag + ">", true))
		pushstack(bbtags, thetag);
	
    document.REPLIER.ffont.selectedIndex  = 0;
    document.REPLIER.fsize.selectedIndex  = 0;
    document.REPLIER.fcolor.selectedIndex = 0;
    
    cstat();
	
}

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();
				hstat('click_close');
			}
		}
		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)

				if ( (tagRemove != 'font') && (tagRemove != 'SIZE') && (tagRemove != 'COLOR') )
				{
					eval("document.REPLIER." + tagRemove + ".value = ' " + tagRemove + " '");
					eval(tagRemove + "_open = 0");
				}
			}
			
			cstat();
		}
	}
}


function tag_list()
{
	var listvalue = "init";
	var thelist = "";
	
	while ( (listvalue != "") && (listvalue != null) )
	{
		listvalue = prompt(list_prompt, "");
		if ( (listvalue != "") && (listvalue != null) )
		{
			thelist = thelist+"<li>"+listvalue+"</li>";
		}
	}
	
	if ( thelist != "" )
	{
		doInsert( "<ul>" + thelist + "</ul>", "", false);
	}
}

function tag_url()
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_url, "http://");
    var enterTITLE = prompt(text_enter_url_name, "Mi sitio Web");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }
    if (!enterTITLE) {
        FoundErrors += " " + error_no_title;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("<a href="+enterURL+" target=_blank>"+enterTITLE+"</a>", "", false);
}

function tag_image()
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_image, "http://");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("<img border=0 align=left src="+enterURL+">", "", false);
}

function tag_email()
{
    var emailAddress = prompt(text_enter_email, "");

    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.Contenido;

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

