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



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

//--------------------------------------------
// 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()
{
	//var c=bbtags.length;
	
	//alert('Stack: ' + c);
	
	if (bbtags[0])
	{
		while (bbtags[0])
		{
			tagRemove = popstack(bbtags)
			document.form1.story.value += "[/" + tagRemove + "]";
			
			// Change the button status
			//eval("document.form1." + tagRemove + ".value = ' " + tagRemove + " '");
			eval(tagRemove + "_open = 0");
		}
		
	}
	
	// Ensure we got them all
	
	bbtags = new Array();
	document.form1.story.focus();
}


//--------------------------------------------
// SIMPLE TAGS (such as B, I, U, etc)
//--------------------------------------------

function simpletag(thetag)
{

	var tagOpen = eval(thetag + "_open");
	
	if (tagOpen == 0)
		{
			document.form1.story.value += "[" + thetag + "]";
			
			eval(thetag + "_open = 1");
			
			// Change the button status
			
			//eval("document.form1." + thetag + ".value += '*'");
			
			pushstack(bbtags, thetag);
		}
		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);
				document.form1.story.value += "[/" + tagRemove + "]";
				
				// Change the button status
				//eval("document.form1." + tagRemove + ".value = ' " + tagRemove + " '");
				eval(tagRemove + "_open = 0");
			}
			
		}	
	document.form1.story.focus();
}

function tag_url()
{
    var FoundErrors = '';
    var enterURL   = prompt("Enter URL:", "http://");
    var enterTITLE = prompt("Enter Anchor Text:", "My Webpage");
    if (!enterURL)
    {
        FoundErrors += " " + "No URL entered";
    }
    if (!enterTITLE)
    {
        FoundErrors += " " + "No Anchor Text entered";
    }
    if (FoundErrors)
    {
        alert("Error!"+FoundErrors);
        return;
    }
    var ToAdd = "[URL="+enterURL+"]"+enterTITLE+"[/URL]";
    document.form1.story.value+=ToAdd;
	document.form1.story.focus();
}


function tag_email()
{
    var emailAddress = prompt("Enter Email Address:","");
   	if (!emailAddress)
	{ 
		alert("No Email Address entered"); 
		return; 
	}
    var ToAdd = "[EMAIL]"+emailAddress+"[/EMAIL]";
    document.form1.story.value+=ToAdd;
	document.form1.story.focus();
}
