// *******************************************************

tags = new Array();

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

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);

// *******************************************************
// replacements for unsupported array functions (because arrayname.push(var)
// and arrayname.pop() are not implemented in IE until version 5.5)

function getarraysize(thearray) {
// replacement for arrayname.length property
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

function arraypush(thearray,value) {
// replacement for arrayname.push(value)
	thearraysize = getarraysize(thearray);
	thearray[thearraysize] = value;
}

function arraypop(thearray) {
// replacement for arrayname.pop()
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

// *******************************************************
function refocus(theform) {
	theform.sizeselect.selectedIndex = 0;
	theform.marginselect.selectedIndex = 0;
	theform.fontselect.selectedIndex = 0;
	theform.colorselect.selectedIndex = 0;
	theform.alignselect.selectedIndex = 0;
	theform.message.focus();
}

function stat(thevalue) {
// places mini-help text into help/error textbox
// strings are stored in vbcode_language.js
	document.vbform.status.value = eval(thevalue+"_text");
}
// *******************************************************

function closetag(theform) {
// closes last opened tag
		if (tags[0]) {
				if (theform.message.createTextRange && theform.message.caretPos) {
				var caretPos = theform.message.caretPos;
				caretPos.text += "[/"+ arraypop(tags) +"]";
				}
				else {
				theform.message.value += "[/"+ arraypop(tags) +"]";
				}
			}
		else {
			stat('no_tags');
			}
	theform.message.focus();
}

function closeall(theform) {
// closes all open tags
		if (tags[0]) {
			while (tags[0]) {
					if (theform.message.createTextRange && theform.message.caretPos) {
					var caretPos = theform.message.caretPos;
					caretPos.text += "[/"+ arraypop(tags) +"]";
					}
					else {
					theform.message.value += "[/"+ arraypop(tags) +"]";
					}
			}
		}
		else {
			stat('no_tags');
		}
	theform.message.focus();
}

// *******************************************************
function vbcode(theform,vbcode,prompttext) {

	var txtarea = document.vbform.message;
	txtarea.focus();

	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			donotinsert = false;
				for (i = 0; i < tags.length; i++) {
				if (tags[i] == vbcode)
				donotinsert = true;
				}
			if (donotinsert)
				stat("already_open");
		                	else {
				if (txtarea.createTextRange && txtarea.caretPos) {
				var caretPos = txtarea.caretPos;
				caretPos.text += "["+vbcode+"]";
				}
				else {
				theform.message.value += "["+vbcode+"]";
				}
				arraypush(tags,vbcode);
				txtarea.focus();
			}
			txtarea.focus();
			return;
		}
		document.selection.createRange().text = "["+vbcode+"]" + theSelection + "[/"+vbcode+"]";
		txtarea.focus();
		return;
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		var vbopen = "["+vbcode+"]";
		var vbclose = "[/"+vbcode+"]";
		mozWrap(txtarea, vbopen, vbclose);
		return;
	}
	else
	{
		donotinsert = false;
			for (i = 0; i < tags.length; i++) {
			if (tags[i] == vbcode)
			donotinsert = true;
			}
		if (donotinsert)
		stat("already_open");
		                else {
		theform.message.value += "["+vbcode+"]";
		arraypush(tags,vbcode);
		}
		return;
	}
	storeCaret(txtarea);
}

// *******************************************************

function fontformat(theform,thevalue,thetype) {
// insert two-parameter markup - [x=y]zzz[/x]
	var txtarea = document.vbform.message;
	
	if ((clientVer >= 4) && is_ie && is_win && thevalue!=0) {
		txtarea.focus();
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
				if (txtarea.createTextRange && txtarea.caretPos) {
				var caretPos = txtarea.caretPos;
				caretPos.text += "["+thetype+"="+thevalue+"]";
				}
				else {
				txtarea.value += "["+thetype+"="+thevalue+"]";
				}
			arraypush(tags,thetype);
			refocus(theform);
			return;
		}
		document.selection.createRange().text = "["+thetype+"="+thevalue+"]" + theSelection + "[/"+thetype+"]";
		refocus(theform);
		return;
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		var vbopen = "["+thetype+"="+thevalue+"]";
		var vbclose = "[/"+thetype+"]";
		mozWrap(txtarea, vbopen, vbclose);
		refocus(theform);
		return;
	}
	else
	{
		txtarea.value += "["+thetype+"="+thevalue+"]";
		arraypush(tags,thetype);
		refocus(theform);
		return;
	}
	storeCaret(txtarea);
}

function insertline(theform,prompttext) {
	var txtarea = document.vbform.message;
	var caretPos = txtarea.caretPos;
	
	inserttext = prompt("Âûðàâíèâàíèå ëèíèè (ÍÅÎÁßÇÀÒÅËÜÍÎ) \n(LEFT (âëåâî) / RIGHT (âïðàâî) / CENTER (ïî öåíòðó))","");
	if ((inserttext != null) && (inserttext != "")) {
		if (txtarea.createTextRange && txtarea.caretPos) {
		caretPos.text = caretPos.text + "[hr="+inserttext+"][/hr]";
		} else {
		txtarea.value  += "[hr="+inserttext+"][/hr]";
		}
	}
	else if (inserttext != null)
	{
		if (txtarea.createTextRange && txtarea.caretPos) {
		caretPos.text = caretPos.text + "[hr][/hr]";
		} else {
		txtarea.value  += "[hr][/hr]";
		}
	}
	txtarea.focus();
}

function quoteformat(theform) {
// insert two-parameter markup - [x=y]zzz[/x]
	var txtarea = document.vbform.message;
	
	if ((clientVer >= 4) && is_ie && is_win) {
		txtarea.focus();
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			inserttext = prompt("Ââåäèòå òåêñò, êîòîðûé âû õîòèòå îòôîðìàòèðîâàòü:\n[QUOTE=Èñòî÷íèê Öèòàòû][/QUOTE]",'');
			insertsrc = prompt("Èñòî÷íèê? (ÍÅÎÁßÇÀÒÅËÜÍÎ). Åñëè íóæíû êàâû÷êè - èñïîëüçóéòå\nALT+0171 è ALT+0187 (ñ âêëþ÷åííûì NumLock)",'');
				if (inserttext != null && inserttext != "" && insertsrc != null && insertsrc != "") {
				txtarea.value += "[quote="+insertsrc+"]"+inserttext+"[/quote]";
				}
				else if ((inserttext != null) && (inserttext != ""))
				{
				theform.message.value += "[quote]"+inserttext+"[/quote]";
				}
			txtarea.focus();
			return;
		}
		insertsrc = prompt("Èñòî÷íèê? (ÍÅÎÁßÇÀÒÅËÜÍÎ). Åñëè íóæíû êàâû÷êè - èñïîëüçóéòå\nALT+0171 è ALT+0187 (ñ âêëþ÷åííûì NumLock)",'');
			if (insertsrc != null && insertsrc != "") {
				document.selection.createRange().text = "[quote="+insertsrc+"]" + theSelection + "[/quote]";
			}
			else
			{
				document.selection.createRange().text = "[quote]" + theSelection + "[/quote]";
			}
		txtarea.focus();
		return;
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, "[quote]", "[/quote]");
		return;
	}
	else
	{
		inserttext = prompt("Ââåäèòå òåêñò, êîòîðûé âû õîòèòå\nîòôîðìàòèðîâàòü:\n[QUOTE=Èñòî÷íèê Öèòàòû][/QUOTE]",'');
		insertsrc = prompt("Èñòî÷íèê? (ÍÅÎÁßÇÀÒÅËÜÍÎ). Åñëè íóæíû êàâû÷êè - èñïîëüçóéòå\nALT+0171 è ALT+0187 (ñ âêëþ÷åííûì NumLock)",'');
			if (inserttext != null && inserttext != "" && insertsrc != null && insertsrc != "") {
			txtarea.value += "[quote="+insertsrc+"]"+inserttext+"[/quote]";
			}
			else if ((inserttext != null) && (inserttext != ""))
			{
			theform.message.value += "[quote]"+inserttext+"[/quote]";
			}
		txtarea.focus();
		return;
	}
	storeCaret(txtarea);
}

// *******************************************************

function namedlink(theform,thetype) {
// inserts named url or email link - [url=mylink]text[/url]
	linktext = prompt(link_text_prompt,"");
		var prompttext;
		if (thetype == "URL") {
			prompt_text = link_url_prompt;
			prompt_contents = "http://";
			}
		else {
			prompt_text = link_email_prompt;
			prompt_contents = "";
			}
	linkurl = prompt(prompt_text,prompt_contents);
	if ((linkurl != null) && (linkurl != "")) {
		if ((linktext != null) && (linktext != ""))
			theform.message.value += "["+thetype+"="+linkurl+"]"+linktext+"[/"+thetype+"] ";
		else
			theform.message.value += "["+thetype+"]"+linkurl+"[/"+thetype+"] ";
		}
	theform.message.focus();
}

// *******************************************************

function dolist(theform) {
// inserts list with option to have numbered or alphabetical type
	listtype = prompt(list_type_prompt, "");
	if ((listtype == "a") || (listtype == "1")) {
		thelist = "[list="+listtype+"]\n";
		listend = "[/list="+listtype+"] ";
		}
	else {
		thelist = "[list]\n";
		listend = "[/list] ";
		}
	listentry = "initial";
	while ((listentry != "") && (listentry != null)) {
		listentry = prompt(list_item_prompt, "");
		if ((listentry != "") && (listentry != null))
			thelist = thelist+"[*]"+listentry+"\n";
		}
	theform.message.value += thelist+listend;
	theform.message.focus();
}

// *******************************************************

function smilie(text) {
	var txtarea = document.vbform.message;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

function opensmiliewindow(x,y,sessionhash) {
// um... opens smilie overflow window.
		window.open("misc.php?action=getsmilies&s="+sessionhash, "smilies", "toolbar=no,scrollbars=yes,resizable=yes,width="+x+",height="+y);
}

function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

