
var forum_text_id = "forum_text"
var forum_bbtags = new Array();
var forum_tags = []
forum_tags["B"]     = 0
forum_tags["I"]     = 0
forum_tags["U"]     = 0
forum_tags["QUOTE"] = 0
forum_tags["CODE"]  = 0
forum_tags["SQL"]   = 0
forum_tags["HTML"]  = 0

// Determine browser type and stuff.
// Borrowed from http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html

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);


//--------------------------------------------
// GENERAL INSERT FUNCTION
//--------------------------------------------
// tag: opening tag
// tag2: 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(tag, tag2, is_single) {
	var obj = document.getElementById(forum_text_id)
	if (!obj) return;
	
	var is_close = false

	// Ensure it works for IE4up / Win only
	if ((myVersion >= 4) && is_ie && is_win) {
		// this doesn't work for NS, but it works for IE 4+ and compatible browsers
		if (obj.isTextEdit) {
			obj.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if ((sel.type == "Text" || sel.type == "None") && rng != null) {
				if (tag2 != "" && rng.text.length > 0) tag += rng.text + tag2
				else if (is_single) is_close = true
	
				rng.text = tag;
			}
		} else {
			if (is_single) is_close = true;
			obj.value += tag;
		}
	} else {
		if (is_single) is_close = true;
		obj.value += tag;
	}

	obj.focus();
	
	return is_close;
}	


//--------------------------------------------
// Set the number of tags open box
//--------------------------------------------

function cstat() {
	var c = stacksize(forum_bbtags);
	
	if ((c < 1) || (c == null)) c = 0;
	
	if (!forum_bbtags[0]) c = 0;
	
	document.getElementById("forum_tagcount").value = 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;
}

//--------------------------------------------
// 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 obj = document.getElementById(forum_text_id)
	if (!obj) return;
	
	if (forum_bbtags[0]) {
		while (forum_bbtags[0]) {
			tag_remove = popstack(forum_bbtags)
			obj.value += "[/" + tag_remove + "]";
			
			// 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 ((tag_remove != 'FONT') && (tag_remove != 'SIZE') && (tag_remove != 'COLOR')) {
				document.getElementById("forum_button_" + tag_remove).value = tag_remove
				forum_tags[tag_remove] = 0
			}
		}
	}
	
	// Ensure we got them all
	forum_bbtags = new Array();
	obj.focus();
	document.getElementById("forum_tagcount").value = 0
}

//--------------------------------------------
// Простые теги (B,I,U, etc)
//--------------------------------------------

function simpletag(tag) {
	if (forum_tags[tag] == 0) {
		if (doInsert("[" + tag + "]", "[/" + tag + "]", true)) {
			forum_tags[tag] = 1
			// Change the button status
			document.getElementById("forum_button_" + tag).value += "*"
	
			pushstack(forum_bbtags, tag)
			cstat()
		}
	} else {
		// Find the last occurance of the opened tag
		lastindex = 0
		
		for (i = 0 ; i < forum_bbtags.length; i++ ) {
			if (forum_bbtags[i] == tag) lastindex = i;
		}
		
		// Close all tags opened up to that tag was opened
		while (forum_bbtags[lastindex]) {
			tag_remove = popstack(forum_bbtags);
			doInsert("[/" + tag_remove + "]", "", false)
			
			// Change the button status
			if ((tag_remove != 'FONT') && (tag_remove != 'SIZE') && (tag_remove != 'COLOR')) {
				document.getElementById("forum_button_" + tag_remove).value = tag_remove
				forum_tags[tag_remove] = 0
			}
		}
		
		cstat();
	}
}

//--------------------------------------------
// Вставка остальных тегов
//--------------------------------------------

function tag_list() {
	var listvalue = "init"
	var thelist = ""
	
	while ((listvalue != "") && (listvalue != null)) {
		listvalue = prompt("Введите пункт списка. Для завершения списка, нажмите 'отмена' или оставьте очередное поле пустым", "")
		if ((listvalue != "") && (listvalue != null)) thelist = thelist + "[*]" + listvalue+"\n"
	}
	
	if (thelist != "") doInsert("[LIST]\n" + thelist + "[/LIST]\n", "", false)
}

//--------------------------------------------

function tag_url() {
    var url   = prompt("Введите полный URL ссылки", "http://")
    var title = prompt("Введите название сайта", "My Webpage")

    if (!url) {
    	alert("Вы должны ввести URL")
    	return
    }
    if (!title) {
    	alert("Вы должны ввести название")
    	return
    }

	doInsert("[URL=" + url + "]" + title + "[/URL]", "", false)
}

//--------------------------------------------

function tag_image() {
    var url = prompt("Введите полный URL изображения", "http://")
    if (!url) {
    	alert("Вы должны ввести URL")
    	return;
    }

	doInsert("[IMG]" + url + "[/IMG]", "", false)
}

//--------------------------------------------

function tag_email() {
    var mail = prompt("Введите e-mail адрес", "")
    if (!mail) { 
		alert("Вы должны ввести e-mail адрес")
		return
	}

	doInsert("[EMAIL]" + mail + "[/EMAIL]", "", false)
}

