// BlackBoard - PHP Internet Newsboard System
// Copyright 2003-4 by Yves Goergen
// Homepage: http://blackboard.unclassified.de
// See docs/license.txt for distribution/license details
//
// editor.js
// Javascript for editor

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]"); break;
		case "italic":
			enclose_text("[i]", "[/i]"); break;
		case "underline":
			enclose_text("[u]", "[/u]"); break;
		case "strike":
			enclose_text("[s]", "[/s]"); break;
		case "mono":
			enclose_text("[m]", "[/m]"); 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]");
			}
			break;
		case "code":
			enclose_text("[code]\n", "\n[/code]"); break;
		case "url":
			enclose_text("[url]", "[/url]"); break;
		case "img":
			enclose_text("[img]", "[/img]"); break;
		case "color":
			enclose_text("[color=" + arg1 + "]", "[/color]"); break;
		case "font":
			enclose_text("[font=" + arg1 + "]", "[/font]"); break;
		case "size":
			enclose_text("[size=" + arg1 + "]", "[/size]"); break;

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


function enclose_text(t_open, t_close)
{
	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;

		// 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;
	}
	else
	{
		// no selection, insert opening/closing tags alone
		insert_text(t_open + t_close);
		textbox.selectionEnd -= t_close.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;

		// 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();
	}
	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(obj + ".disabled = true;", 10);
}

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

