<!--
// bbCode control by
// subBlue design
// www.subBlue.com

// Startup variables
var imageTag = false;
var theSelection = false;

// 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);

// Helpline messages
b_help = "Exempel på fetstil: <b>text som skall vara fet</b>";
i_help = "Exempel på kursivstil: <i>text som skall vara kursiv</i>";
w_help = "Exempel på en länk: <a href=\"http://www.exempel.com\">Exempel</a>";


// Define the bbCode tags
bbcode = new Array();
bbtags = new Array(
'<b>','</b>',           //0
'<i>','</i>',           //2
'<a href="http://','"></a>'           //4
);
imageTag = false;

// Shows the help messages in the helpline window
function helpline(help) {
    frm = getForm();
	frm.helpbox.value = eval(help + "_help");
}

// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function bbstyle(bbnumber, arg) {
	var txtarea = getTxtarea();

	txtarea.focus();
	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
		    // Add optional argument to the tag
		    opentag = bbtags[bbnumber];
		    if (arg) {
		        opentag = opentag.substring(0, opentag.length-1) + '=' + arg + ']';
		    }

			// Add tags around selection
			document.selection.createRange().text = opentag + theSelection + bbtags[bbnumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		//mozWrap(txtarea, arg);
		mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1], arg);
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;

				//JÄR SLUTAR E!!
				if (bbnumber == 4){
//					txtarea.value += bbtags[3];
//					var my_link = prompt("Namn(=det som syns på sidan)");
//					txtarea.value += my_link+'</a>';
				}
				else	{
					txtarea.value += bbtags[bbnumber+1];
				}
				deactivateButton(butnumber);
				imageTag = false;
			}
			txtarea.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			txtarea.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			deactivateButton(14);
			imageTag = false;
		}

		// Open tag
		// Add optional argument to the tag
		//JÄR BÖRI E
		if (bbnumber == 4){
		var my_link = prompt("Adress:","http://www.");
		var my_name = prompt("Namn(=det som syns på sidan)");
		opentag = '<a href="'+my_link+'">'+my_name+'</a>';
//		deactivateButton(5);
		} else	{
		opentag = bbtags[bbnumber];
		}
	    if (arg) {
	        opentag = opentag.substring(0, opentag.length-1) + '=' + arg + ']';
	    }
		txtarea.value += opentag;
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		activateButton(bbnumber);
		txtarea.focus();
		return;
	}
//	storeCaret(txtarea);
}

// byter till *-knappen
function activateButton(btnnr) {
    frm = getForm();
    btn = eval('frm.addbbcode' + btnnr);
    if (btn.type == 'button') {
        btn.value += '*';
    }
}

// byter till originalknappen utan *
function deactivateButton(btnnr) {
    frm = getForm();
    btn = eval('frm.addbbcode' + btnnr);
    if (btn.type == 'button') {
		btn.value = btn.value.substr(0,(btn.value.length - 1));
    }
}

// http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close, arg)
//function mozWrap(txtarea, arg)
{
     //Add optional argument to the tag
    opentag = open;
    if (arg) {
        opentag = opentag.substring(0, opentag.length-1) + '=' + arg + ']';
    }

	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);
	
	
	// Special för länkknappen
	if (open=='<a href="http://')	{
		var my_link = prompt("Adress: (Exempel: http://www.exempeladress.com)","http://www.");
		close='">'+s2+'</a>';
		open='<a href="'+my_link;
		txtarea.value = s1 + open + close + s3;
		return;
	}
	else	{
		// s1 är det framför markeringen
		// open är <a href="http://
		// s2 är det som är markerat
		// close är "></a>
		// s3 är det efter markeringen
		txtarea.value = s1 + open + s2 + close + s3;
		return;
	}
}

function getForm() {
    return document.edit_or_add;
}
function getTxtarea() {
    frm = getForm();
    return frm.text;
}
function onOk()
{
   document.edit_or_add.submit();
}
 
function onCancel(link)
{
   window.location.href=link;
   return false;
}

//Asukasopas
//window.open('guide/index.html','guide','scrollbars=auto,resizeable=0,toolbar=no,menubar=yes,location=no,directories=no,copyhistory=no,status=no,width=680,height=480')
function guide(URL) { 
	self.open(URL,'guide','scrollbars=auto,resizeable=0,toolbar=no,menubar=yes,location=no,directories=no,copyhistory=no,status=no,width=680,height=480'); 
}

//-->