

var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = (navigator.product == 'Gecko');
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));

function text_keydown(where, max)
{
	if (!window.event) return true;
	var keycode = window.event.keyCode;

	if (keycode == 9)
	{
		if (document.selection.createRange().duplicate().text.length)
		{
			if (!window.event.shiftKey)
			{
				document.selection.createRange().duplicate().text =
					"\t" +
					document.selection.createRange().duplicate().text.replace(/\n/g, "\n\t") +
					"\n";
			}
			else
			{
				document.selection.createRange().duplicate().text =
					document.selection.createRange().duplicate().text.replace(/\n\t/g, "\n");
				document.selection.createRange().duplicate().text =
					document.selection.createRange().duplicate().text.replace(/^\t/g, "") +
					"\n";
			}
		}
		else
		{
			if (!window.event.shiftKey)
			{
				insert_text("\t");
			}
		}
		return false;
	}
}


function doCmd(event, cmd, arg1)
{
	textbox.focus();
	switch (cmd)
	{
		case "bold":
			enclose_text("[b]", "[/b]", 1); break;
		case "italic":
			enclose_text("[i]", "[/i]", 1); break;
		case "underline":
			enclose_text("[u]", "[/u]", 1); break;
		case "strike":
			enclose_text("[s]", "[/s]", 1); break;
		case "mono":
			enclose_text("[m]", "[/m]", 1); break;
		case "quote":
			if (event.shiftKey)
			{
				insert_text("[/quote]\n\n\n\n[quote]\n");
				// If on Mozilla, move cursor to correct position
				if (textbox.selectionStart >= 0) textbox.selectionStart = textbox.selectionEnd -= 10;
			}
			else
			{
				enclose_text("[quote]\n", "\n[/quote]", 1);
			}
			break;
		case "code":
			if (event.shiftKey)
			{
				insert_text("[/code]\n\n\n\n[code]\n");
				// If on Mozilla, move cursor to correct position
				if (textbox.selectionStart >= 0) textbox.selectionStart = textbox.selectionEnd -= 10;
			}
			else
			{
				enclose_text("[code]\n", "\n[/code]", 1);
			}
			break;
		case "url":
			if (event.shiftKey)
			{
				enclose_text("[url=]", "[/url]", 0);
				// If on Mozilla, move cursor to correct position
				if (textbox.selectionStart >= 0) textbox.selectionStart = textbox.selectionEnd = textbox.selectionStart + 5;
			}
			else
			{
				enclose_text("[url]", "[/url]", 1);
			}
			break;
		case "img":
			enclose_text("[img]", "[/img]", 1); break;
		case "color":
			enclose_text("[color=" + arg1 + "]", "[/color]", 1); break;
		case "font":
			enclose_text("[font=" + arg1 + "]", "[/font]", 1); break;
		case "size":
			enclose_text("[size=" + arg1 + "]", "[/size]", 1); break;

		// "not currently supported":
		case "undo":
			document.selection.createRange().execCommand("Undo");
		case "redo":
			document.selection.createRange().execCommand("Redo");
	}
}


// cursorpos: 0: begin | 1: middle | 2: end
//   (only relevant if no text is selected)
//
function enclose_text(t_open, t_close, cursorpos)
{
	if (is_ie && document.selection && document.selection.createRange().duplicate().text.length)
	{
		// IE with selected text
		var seltext = document.selection.createRange().duplicate().text;
		if (seltext.substring(0, t_open.length) == t_open &&
			seltext.substring(seltext.length - t_close.length, seltext.length) == t_close)
		{
			// tags are already there, remove them
			document.selection.createRange().duplicate().text = seltext.substring(t_open.length, seltext.length - t_close.length);
		}
		else
		{
			document.selection.createRange().duplicate().text = t_open + seltext + t_close;
		}
	}
	else if (textbox.selectionEnd && (textbox.selectionEnd - textbox.selectionStart > 0))
	{
		// Mozilla with selected text
		var start_selection = textbox.selectionStart;
		var end_selection = textbox.selectionEnd;
		var new_endsel;
		var scroll_top = textbox.scrollTop;
		var scroll_left = textbox.scrollLeft;

		// fetch everything from start of text area to selection start
		var start = textbox.value.substring(0, start_selection);
		// fetch everything from start of selection to end of selection
		var seltext = textbox.value.substring(start_selection, end_selection);
		// fetch everything from end of selection to end of text area
		var end = textbox.value.substring(end_selection, textbox.textLength);

		if (seltext.substring(0, t_open.length) == t_open &&
			seltext.substring(seltext.length - t_close.length, seltext.length) == t_close)
		{
			// tags are already there, remove them
			seltext = seltext.substring(t_open.length, seltext.length - t_close.length);
			new_endsel = end_selection - t_open.length - t_close.length;
		}
		else
		{
			seltext = t_open + seltext + t_close;
			new_endsel = end_selection + t_open.length + t_close.length;
		}

		textbox.value = start + seltext + end;

		textbox.selectionStart = start_selection;
		textbox.selectionEnd = new_endsel;
		textbox.scrollTop = scroll_top;
		textbox.scrollLeft = scroll_left;
	}
	else
	{
		// no selection, insert opening/closing tags alone
		insert_text(t_open + t_close);
		if (cursorpos <= 1) textbox.selectionEnd -= t_close.length;
		if (cursorpos <= 0) textbox.selectionEnd -= t_open.length;
	}
}


function insert_text(what)
{
	if (textbox.createTextRange)
	{
		textbox.focus();
		document.selection.createRange().duplicate().text = what;
		textbox.focus();
	}
	else if (textbox.selectionStart >= 0)
	{
		// Mozilla without selected text
		var start_selection = textbox.selectionStart;
		var scroll_top = textbox.scrollTop;
		var scroll_left = textbox.scrollLeft;

		// fetch everything from start of text area to selection start
		var start = textbox.value.substring(0, start_selection);
		// fetch everything from end of selection to end of text area
		var end = textbox.value.substring(start_selection, textbox.textLength);

		textbox.value = start + what + end;

		textbox.selectionStart = textbox.selectionEnd = start_selection + what.length;
		textbox.focus();
		textbox.scrollTop = scroll_top;
		textbox.scrollLeft = scroll_left;
	}
	else
	{
		textbox.value += what;
		textbox.focus();
	}
}


function checklength(where, max)
{
	if (where.value.length <= max) return true;
	where.value = where.value.substr(0, max);
	return false;
}

function getlength(where, max)
{
	alert(STRING_GETLENGTH_1 + where.value.length + STRING_GETLENGTH_2 + max + STRING_GETLENGTH_3);
}


function popup(url, w, h)
{
	window.open(url, "", "width=" + w + ", height=" + h + ", resizable=yes, scrollbars=yes");
}

function goDelete(url)
{
	if (confirm(STRING_DELETE)) document.location.href = url;
}


function doNothing()
{
}

// type = 1: code | 2: quote
//
function cblk(obj, type)
{
	var cobj = obj.parentNode.previousSibling;
	var d = cobj.style.display;

	if (d == "")
	{
		cobj.style.display = "none";

		if (type == 1) obj.innerHTML = STRING_SHOW_CODE;
		if (type == 2) obj.innerHTML = STRING_SHOW_QUOTE;
	}
	else
	{
		cobj.style.display = "";

		if (type == 1) obj.innerHTML = STRING_HIDE_CODE;
		if (type == 2) obj.innerHTML = STRING_HIDE_QUOTE;
	}
}


// disable obj (string) in 10ms
//
function disableObj(obj)
{
	window.setTimeout("if (" + obj + ") { " + obj + ".disabled = true; " + obj + ".blur(); }", 10);
}

function PreviewDesign()
{
	window.open(MODULE_MAIN + 'setdesign=' + document.getElementById('form').Design.value + SID, '_blank');
	//document.location.href = MODULE_MAIN + 'setdesign=' + document.getElementById('form').Design.value + SID;
}

// toggle visibility of an object
//
function toggleVisId(id, state)
{
	toggleVisObj(document.getElementById(id), state);
}

function toggleVisObj(obj, state)
{
	if (obj == null) return;
	if (state == null)
	{
		if (obj.style.display == "") newstate = "none";
		else                         newstate = "";
	}
	else
	{
		if (state) newstate = "";
		else       newstate = "none";
	}
	obj.style.display = newstate;
}

// toggle enabled state of an object
//
function toggleEnabledId(id, state)
{
	toggleEnabledObj(document.getElementById(id), state);
}

function toggleEnabledObj(obj, state)
{
	if (obj == null) return;
	if (state == null)
	{
		if (obj.disabled) newstate = false;
		else              newstate = true;
	}
	else
	{
		newstate = state;
	}
	obj.disabled = newstate;
}

// scroll the window by the height of an item
//
function scrollDownId(id)
{
	scrollDownObj(document.getElementById(id));
}

function scrollDownObj(obj)
{
	window.scrollBy(0, obj.offsetHeight);
}

function scrollUpId(id)
{
	scrollUpObj(document.getElementById(id));
}

function scrollUpObj(obj)
{
	window.scrollBy(0, -obj.offsetHeight);
}


// global enhanced keyboard controls support
//
var globalKeyHandlers = new Array();

function globalRegisterKeyHandler(keycode, ascii, flags, funcname, funcparam)
{
	var newarr = new Array();
	newarr["keycode"] = keycode;
	newarr["ascii"] = ascii;
	newarr["flags"] = flags;
	newarr["funcname"] = funcname;
	newarr["funcparam"] = funcparam;
	globalKeyHandlers.push(newarr);
}

function globalKeyDispatcher(e)
{
	var myFlags;
	var item;

	for (var i in globalKeyHandlers)
	{
		item = globalKeyHandlers[i];

		myFlags = 0;
		if (e.altKey) myFlags |= 1;
		if (e.ctrlKey) myFlags |= 2;
		if (e.shiftKey) myFlags |= 4;

		if ((item["keycode"] && item["keycode"] == e.keyCode ||
		     item["ascii"] && item["ascii"] == e.which) &&
		    item["flags"] == myFlags)
		{
			return item["funcname"](e, item["funcparam"]);
		}
	}
	//alert("unknown key pressed: keycode = " + e.keyCode + " which = " + e.which);
}

// enable keypress dispatcher (not for IE)
if (navigator.appName != "Microsoft Internet Explorer")
{
	window.captureEvents(Event.KEYPRESS);
	window.onkeypress = globalKeyDispatcher;
}


// Correctly handle PNG transparency in Win IE 5.5 or higher.
// This hack is only applied to a list of files defined by the selected design.
//
function correctPNGforIE()
{
	var imgStyle;
	var files = alphaPNGfiles;

	for (var i = 0; i < document.images.length; i++)
	{
		var img = document.images[i];
		var doit = false;
		for (var n in files)
		{
			if (img.src.substring(img.src.length - files[n].length) == files[n])
			{
				doit = true;
				break;
			}
		}
		if (!doit)
		{
			if (img.src.substring(img.src.length - 4) == ".png" &&
			    img.src.match(/\/_smile\//))
				doit = true;
		}
		if (doit)
		{
			img.outerHTML = "<span" +
				((img.id) ? " id=\"" + img.id + "\"" : "") +
				((img.className) ? " class=\"" + img.className + "\"" : "") +
				((img.title) ? " title=\"" + img.title + "\"" : "") +
				" style=\"width:" + img.width + "px; height:" + img.height + "px; display:inline-block; line-height:1px; margin:0px; font-size:1px; padding:0px;" +
					((img.align == "left") ? " float:left;" : "") +
					((img.align == "right") ? " float:right;" : "") +
					((img.parentElement.href) ? " cursor:hand;" : "") +
					img.style.cssText + "; " +
					"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src + "', sizingMethod='scale');\"></span>";
			i--;
		}
	}
}

if (navigator.appName == "Microsoft Internet Explorer")
{
	window.attachEvent("onload", correctPNGforIE);
}


