//--------------------------------------------
// Set up our simple tag open values
//--------------------------------------------

var b_open = 0;
var i_open = 0;
var u_open = 0;

var bbtags = new Array();

// Determine browser type and stuff.

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

//--------------------------------------------
// Get stack size
//--------------------------------------------

function stacksize(thearray)
{
	for (var 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)
{
	var arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

//--------------------------------------------
// Pop stack
//--------------------------------------------

function popstack(thearray)
{
	var arraysize = stacksize(thearray);
	var theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}


//--------------------------------------------
// Close all tags
//--------------------------------------------

function closeall()
{
	if (bbtags[0]) {
		while (bbtags[0]) {
			tagRemove = popstack(bbtags)
			document.post.message.value += "[/" + tagRemove + "]";
		}
	}

	// Ensure we got them all
	bbtags = new Array();
	document.post.message.focus();
}

//--------------------------------------------
// EMOTICONS
//--------------------------------------------

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

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

function simpletag(thetag)
{
	var tagOpen = eval(thetag + "_open");

	if (tagOpen == 0)
	{
		if(doInsert("[" + thetag + "]", "[/" + thetag + "]", true))
		{
			eval(thetag + "_open = 1");
			pushstack(bbtags, thetag);
		}
	}
	else {
		// Find the last occurance of the opened tag
		var lastindex = 0;
			
		for (var 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])
		{
			var tagRemove = popstack(bbtags);
			doInsert("[/" + tagRemove + "]", "", false)
		}
	}
}

function tag_url()
{
  var enterURL   = prompt("Въведете адрес на сайта:", "http://");
  var enterTITLE = prompt("Въведете име на сайта:", "My Webpage");

  if (!enterTITLE || !enterURL) return;

  doInsert("[url="+enterURL+"]"+enterTITLE+"[/url]", "", 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)
{
	var isClose = false;
	var obj_ta = document.post.message;

	if ( (myVersion >= 4) && is_ie && is_win) // Ensure it works for IE4up / Win only
	{
		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;
		}
	}
	else if (document.getSelection) {  
		var text = obj_ta.value;
		var selTxt = text.substring(obj_ta.selectionStart, obj_ta.selectionEnd);
		var cursorPos = obj_ta.selectionStart;
		
		if(ibClsTag != "" && selTxt.length > 0)
			ibTag += selTxt + ibClsTag;
		else if(isSingle)
			isClose = true;
 		cursorPos += ibTag.length;
      	
		replaceSelection(obj_ta, ibTag);  
		obj_ta.focus();
		obj_ta.selectionStart = cursorPos;
		obj_ta.selectionEnd = cursorPos;      
	} 
	else
	{
		if(isSingle)
			isClose = true;

		obj_ta.value += ibTag;
	}

	obj_ta.focus();

	return isClose;
}	


// Replaces the current selection in the TextArea o with the string s
function replaceSelection(o, s)
{
   var s2 = o.value;
   o.value = s2.substring(0, o.selectionStart) + s + s2.substr(o.selectionEnd);
}
