//--------------------------------------------
// Set up our simple tag open values
//--------------------------------------------

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();

function get_easy_mode_state(frmname)
{
	// Returns true if we've chosen easy mode	
	if (eval("document." + frmname + ".bbmode[0].checked")) {
		return true;
	}
	else {
		return false;
	}
}

//--------------------------------------------
// GENERAL INSERT FUNCTION
//--------------------------------------------
// ibTag: opening tag
// ibClsTag: closing tag, used if we have selected text
// isSingle: true if we do not close the tag right now
// return value: true if the tag needs to be closed later

//

function doInsert(ibTag, ibClsTag, isSingle, frmname)
{
	var isClose = false;
	var obj_ta = eval("document." + frmname + ".ftext");

	try
	{
		if(obj_ta.isTextEdit){ // this doesn't work for NS, but it works for IE 4+ and compatible browsers
			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;
		}
	}
	catch(e)
	{
		if(isSingle)
			isClose = true;

		obj_ta.value += ibTag;
	}

	obj_ta.focus();

	return isClose;
}	

//--------------------------------------------
// SIMPLE TAGS (such as B, I U, etc)
//--------------------------------------------

function simpletag(thetag,frmname)
{
	var tagOpen = eval(thetag + "_open");
	var prompt_start = '';
	
	if ( get_easy_mode_state(frmname) )
	{
		inserttext = prompt(prompt_start + "\n[" + thetag + "]xxx[/" + thetag + "]");
		if ( (inserttext != null) && (inserttext != "") )
		{
			doInsert("[" + thetag + "]" + inserttext + "[/" + thetag + "] ", "", false,frmname);
		}
	}
	else {
		if (tagOpen == 0)
		{
			if(doInsert("[" + thetag + "]", "[/" + thetag + "]", true,frmname))
			{
				eval(thetag + "_open = 1");
				// Change the button status
				//eval("document.getElementById('"+frmname+"')." + thetag + ".value = '*'");
				eval("document."+frmname+"." + thetag + ".src = 'images/ppcode/" + thetag + "_.gif'");
		
				pushstack(bbtags, thetag);
				cstat(frmname);
				hstat('click_close',frmname);
			}
		}
		else {
			// Find the last occurance of the opened tag
			lastindex = 0;
			
			for (i = 0 ; i < bbtags.length; i++ )
			{
				if ( bbtags[i] == thetag )
				{
					lastindex = i;
				}
			}
			
			// Close all tags opened up to that tag was opened
			while (bbtags[lastindex])
			{
				tagRemove = popstack(bbtags);
				doInsert("[/" + tagRemove + "]", "", false,frmname)
				
				// Change the button status
				if ( (tagRemove != 'FONT') && (tagRemove != 'SIZE') && (tagRemove != 'COLOR') )
				{
					//eval("document.getElementById('"+frmname+"')." + tagRemove + ".value = ' " + tagRemove + " '");
					eval("document."+frmname+"." + tagRemove + ".src = 'images/ppcode/" + tagRemove + ".gif'");
					eval(tagRemove + "_open = 0");
				}
			}
			
			cstat(frmname);
		}
	}
}

//--------------------------------------------
// Push stack
//--------------------------------------------

function pushstack(thearray, newval)
{
	arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

//--------------------------------------------
// Pop stack
//--------------------------------------------

function popstack(thearray)
{
	arraysize = stacksize(thearray);
	theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}


//--------------------------------------------
// Close all tags
//--------------------------------------------

function closeall(frmname)
{
	if (bbtags[0]) {
		while (bbtags[0]) {
			tagRemove = popstack(bbtags);
			eval("document." + frmname + ".ftext.value += '[/" + tagRemove + "]';");
			
			// Change the button status
			// Ensure we're not looking for FONT, SIZE or COLOR as these
			// buttons don't exist, they are select lists instead.
			
			if ( (tagRemove != 'FONT') && (tagRemove != 'SIZE') && (tagRemove != 'COLOR') )
			{
				eval("document." + frmname + "." + tagRemove + ".value = ' " + tagRemove + " '");
				eval(tagRemove + "_open = 0");
			}
		}
	}
	
	// Ensure we got them all
	//document.getElementById(frmname).tagcount.value = 0;
	document.getElementById('tagcount').innerHTML = "0";
	bbtags = new Array();
	eval("document." + frmname + ".ftext.focus()");
}

//--------------------------------------------
// EMOTICONS
//--------------------------------------------

function emoticon(theSmilie,frmname)
{
	doInsert(" " + theSmilie + " ", "", false,frmname);
}

// Set the number of tags open box
function cstat(frmname)
{
	var c = stacksize(bbtags);
	
	if ( (c < 1) || (c == null) ) {
		c = 0;
	}
	
	if ( ! bbtags[0] ) {
		c = 0;
	}
	
	//document.getElementById(frmname).tagcount.value = c;
	document.getElementById('tagcount').innerHTML = c;	
}

//--------------------------------------------
// Get stack size
//--------------------------------------------

function stacksize(thearray)
{
	for (i = 0 ; i < thearray.length; i++ ) {
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') ) {
			return i;
		}
	}
	
	return thearray.length;
}

//--------------------------------------------
// Set the help bar status
//--------------------------------------------

function hstat(msg,frmname)
{
	//document.getElementById(frmname).helpbox.value = eval( "help_" + msg );
}

function tag_url(frmname)
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_url, "http://");
    var enterTITLE = prompt(text_enter_url_name, "My Webpage");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }
    if (!enterTITLE) {
        FoundErrors += " " + error_no_title;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("[URL="+enterURL+"]"+enterTITLE+"[/URL]", "", false,frmname);
}

function tag_image(frmname)
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_image, "http://");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("[IMG]"+enterURL+"[/IMG]", "", false,frmname);
}

//--------------------------------------------
// ALTER FONT
//--------------------------------------------

function alterfont(theval, thetag, frmname)
{
    if (theval == 0)
    	return;
	
	if(doInsert("[" + thetag + "=" + theval + "]", "[/" + thetag + "]", true,frmname))
		pushstack(bbtags, thetag);
		
	eval("document." + frmname + ".ffont.selectedIndex  = 0;");
	eval("document." + frmname + ".fsize.selectedIndex  = 0;");
	eval("document." + frmname + ".fcolor.selectedIndex = 0;");
    
    cstat(frmname);	
}