var B_open = 0;
var I_open = 0;
var U_open = 0;
var QUOTE_open = 0;
var CODE_open = 0;
var CUT_open = 0;

var text_enter_url = "Enter full address (URL)";
var text_enter_url_name = "Enter site name";
var text_enter_image = "Enter the full image address (URL)";
var list_prompt = "Enter the list item. Click 'Cancel' or type space to complete the list";
var error_no_url = "Please enter the address (URL)";
var error_no_title = "Please enter the title";

var bbtags = new Array();

var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion);

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
 && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
 && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

function quoteMessageEx(theAuthor)
{
	var selection;
	if (document.getSelection)
	{
		selection = document.getSelection();
		selection = selection.replace(/\r\n\r\n/gi, "_newstringhere_");
		selection = selection.replace(/\r\n/gi, " ");
		selection = selection.replace(/ /gi, "");
		selection = selection.replace(/_newstringhere_/gi, "\r\n\r\n");
	}
	else
	{
		selection = document.selection.createRange().text;
	}
	if (selection!="")
	{
		document.getElementById("comment").value += "[quote]"+theAuthor+":\n"+selection+"[/quote]\n";
	}
}

function emoticon(theSmilie)
{
	doInsert(" " + theSmilie + " ", "", false);
}

// Insert simple tags: B, I, U, CODE, QUOTE
function simpletag(thetag)
{
	var tagOpen = eval(thetag + "_open");

	if (tagOpen == 0)
	{
		if (doInsert("[" + thetag + "]", "[/" + thetag + "]", true))
		{
			eval(thetag + "_open = 1");
			// Change the button status

			pushstack(bbtags, thetag);
			cstat();
		}
	}
	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);

			// Change the button status
			eval(tagRemove + "_open = 0");
		}

		cstat();
	}
}

// Insert font tag
function alterfont(theval, thetag)
{
	if (theval == 0)
		return;

	if (doInsert("[" + thetag + "=" + theval + "]", "[/" + thetag + "]", true))
		pushstack(bbtags, thetag);

	document.getElementById("select_font").selectedIndex = 0;
	cstat();
	document.getElementById("comment").focus();
}

// Insert url tag
function tag_url()
{
	var enterURL = prompt(text_enter_url, "http://");

	if (enterURL)
	{
		var enterTITLE = prompt(text_enter_url_name, "");
		if (!enterTITLE)
			enterTITLE=enterURL;
		doInsert("[URL="+enterURL+"]"+enterTITLE+"[/URL]", "", false);
	}


}

// Insert image tag
function tag_image()
{
	var enterURL = prompt(text_enter_image, "http://");

	if (enterURL)
		doInsert("[IMG]"+enterURL+"[/IMG]", "", false);
}

// Insert list tag
function tag_list()
{
	var listvalue = "init";
	var thelist = "[LIST]\n";

	while ( (listvalue != "") && (listvalue != null) && (listvalue != " "))
	{
		listvalue = prompt(list_prompt, "");
		if ( (listvalue != "") && (listvalue != null) && (listvalue != " "))
		{
			thelist = thelist+"[*]"+listvalue+"\n";
		}
	}

	doInsert(thelist + "[/LIST]\n", "", false);
}

// Close all tags
function closeall()
{
	if (bbtags[0])
	{
		while (bbtags[0])
		{
			tagRemove = popstack(bbtags);
			document.form_comment.comment.value += "[/" + tagRemove + "]";

			if ( (tagRemove != 'FONT') && (tagRemove != 'SIZE') && (tagRemove != 'COLOR') )
			{
				eval(tagRemove + "_open = 0");
			}
		}
	}

	bbtags = new Array();
	cstat();
}

// Stack functions
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 stacksize(thearray)
{
	for (i = 0 ; i < thearray.length; i++ )
	{
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') )
		{
			return i;
		}
	}

	return thearray.length;
}

// Show statistic
function cstat()
{
	var c = stacksize(bbtags);

	if ( (c < 1) || (c == null) )
	{
		c = 0;
	}

	if ( ! bbtags[0] )
	{
		c = 0;
	}

	if (c > 0)
		document.getElementById("close_all").style.visibility="visible";
	else
		document.getElementById("close_all").style.visibility="hidden";
	document.form_comment.comment.focus();
}

var elem_id=0;

function addEvent(el, evname, func)
{
	if(el.attachEvent) // IE
		el.attachEvent("on" + evname, func);
	else if(el.addEventListener) // Gecko / W3C
		el.addEventListener(evname, func, false);
	else
		el["on" + evname] = func;
}

function GetRealPos(el)
{
	if(!el || !el.offsetParent)
		return false;
	var res=Array();
	res["left"] = el.offsetLeft;
	res["top"] = el.offsetTop;
	var objParent = el.offsetParent;
	while(objParent.tagName != "BODY")
	{
		res["left"] += objParent.offsetLeft;
		res["top"] += objParent.offsetTop;
		objParent = objParent.offsetParent;
	}
	res["right"]=res["left"] + el.offsetWidth;
	res["bottom"]=res["top"] + el.offsetHeight;

	return res;
}

function AddCategory()
{
	oSel = document.getElementById('CATEGORY_ID');

	if (oSel.value=='NEW')
	{

		iSel = oSel.selectedIndex;
		if ((res = prompt('Category name','')))
		{
			var exOpt = false;
			for(i=1; i<oSel.options.length; i++)
			{
				if(res == oSel.options[i].text)
				{
					var selectOpt = i;
					exOpt = true;
				}
			}

			if(!exOpt)
			{
				var oOption = new Option(res,"ADD");
				if (oSel.options[1].value != "ADD")
				{
					oOption = oSel.insertBefore(oOption,oSel.options[1]);
					oOption.value = "ADD";
					oOption.innerText = res;
				}
				else
					oSel.options[1] = oOption;
				document.REPLIER.NEW_CATEGORY.value = res;
			}
			else
				oSel.options[selectOpt].selected = true;
		}

		if (oSel.options[1].value=="NEW")
			oSel.options[0].selected = true;
		else
		{
			if(exOpt)
				oSel.options[selectOpt].selected = true;
			else
				oSel.options[1].selected = true;
		}
	}
}

function show_special()
{
	o = document.getElementById('special_perms');
	if (document.getElementById('blog_perms_1').checked==true)
		o.style.display='block';
	else
		o.style.display='none';

}

function mozillaWr(textarea, open, close)
{
	var selLength = textarea.textLength;
	var selStart = textarea.selectionStart;
	var selEnd = textarea.selectionEnd;

	if (selEnd == 1 || selEnd == 2)
	selEnd = selLength;

	var s1 = (textarea.value).substring(0,selStart);
	var s2 = (textarea.value).substring(selStart, selEnd)
	var s3 = (textarea.value).substring(selEnd, selLength);
	textarea.value = s1 + open + s2 + close + s3;

	textarea.selectionEnd = 0;
	textarea.selectionStart = selEnd + open.length + close.length;
	return;
}

function doInsert(ibTag, ibClsTag, isSingle)
{
	var isClose = false;
	var textarea = document.form_comment.comment;

	if (isSingle)
		isClose = true;

	if (is_ie)
	{
		textarea.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;
				isClose = false;
			}

			rng.text = ibTag;
		}
	}
	else
	{
		if (is_nav && document.getElementById)
		{
			if (ibClsTag != "" && textarea.selectionEnd > textarea.selectionStart)
			{
				mozillaWr(textarea, ibTag, ibClsTag);
				isClose = false;
			}
			else
				mozillaWr(textarea, ibTag, '');
		}
		else
			textarea.value += ibTag;
	}

	textarea.focus();
	return isClose;
}

function quoteMessage()
{
	var selection;
	if (window.getSelection)
	{
		selection = window.getSelection();
	}
	else
	{
		selection = document.selection.createRange().text;
	}

	if (selection=="")
	{
		simpletag("QUOTE");
	}
	else
	{
		//document.getElementById("comment").value += "[QUOTE]"+selection+"[/QUOTE]\n";
		document.form_comment.comment.value += "[QUOTE]"+selection+"[/QUOTE]\n";

	}

	document.getElementById("comment").focus();
}
