﻿function startup()
{
    showSpeechJSTags();
    showLogin();
}

function showSpeechJSTags()
{
    if (document.getElementById("speechjstags")) document.getElementById("speechjstags").className = document.getElementById("speechjstags").className.replace(' hideme',' showmeb');
}

function showLogin()
{
    if (document.getElementById("loginform"))
    {
        if (document.getElementById("loginform").className == 'hideme') document.getElementById("loginform").className = document.getElementById("loginform").className.replace('hideme','showmeb');
        else document.getElementById("loginform").className = document.getElementById("loginform").className.replace('showmeb','hideme');
    }
}

function insertTag(tag,type)
{
	document.getElementById(type).focus();
	endTag = tag;
	//special stuff to do if the tag is a LINK
	if (tag == "LINK") {
		startTag = "LINK";
		address = null;
		title = null;

		address = prompt("What is the address of your link?","http://");
		if ((address == null) || (address == "") || (address == "http://")) return;
		startTag += " address=\"" + address + "\"";

		//admin only
		if (atAdminPage())
		{
			title = prompt("What text do you want to show up when the mouse hovers over the link? This isn't necessary.","");
			if ((title != null) && (title != "")) startTag += " title=\"" + title + "\"";
		}
	}
	//special stuff to do if the tag is a PIC
	else if (tag == "PIC") {
		startTag = "PIC";
		filename = null;
		position = "middle";
		picturetext = null;
		link = null;
		
		filename = prompt("Enter the picture's filename. It must be in /post_images, http:// addresses aren't allowed.","");
		if ((filename == null) || (filename == "")) return;
		startTag += " filename=\"" + filename + "\"";
		
		position = prompt("Enter the picture's position. It can be either left, right or middle.","middle");
		if ((position != null) && (position != "")) startTag += " position=\"" + position + "\"";
		
		picturetext = prompt("What text do you want to show up when the mouse hovers over the picture? This isn't necessary.","");
		if ((picturetext != null) && (picturetext != "")) startTag += " picturetext=\"" + picturetext + "\"";
		
		link = prompt("Do you want this picture to be a link? If so, enter the filename of the thing that you are linking to. It must be in /linked, otherwise you'll need to enter the full http:// address.","");
		if ((link != null) && (link != "")) startTag += " link=\"" + link + "\"";
	}
	//special stuff to do if the tag is a COLOUR
	else if (tag == "COLOUR") {
	    startTag = "COLOUR";
		hex = null;
		
		hex = prompt("Enter the hexadecimal code for the colour. e.g. red is ff0000, blue is 0000ff.","");
		if ((hex == null) || (hex == "")) return;
		startTag += " " + hex;
	}
	//special stuff to do if the tag is a SIZE
	else if (tag == "SIZE") {
	    startTag = "SIZE";
		size = null;
		
		size = prompt("Enter the a size between 1 (smallest) and 4 (biggest). 2 is regular.","");
		if ((size == null) || (size == "") || (parseInt(size) > 4) || (parseInt(size) < 1)) return;
		startTag += " " + size;
	}
	//special stuff to do if the tag is a QUOTE
	else if (tag == "QUOTE") {
	    startTag = "QUOTE][WHAT";
		endTag = "WHAT][WHO][/WHO][/QUOTE";
	}
	else startTag = tag;
	//ie (and opera too)
	if (document.selection)
	{
		highlighted = document.selection.createRange();
		if (highlighted.text == "") moveCaret = -1 * (endTag.length + 3);
		else moveCaret = 0;
		highlighted.text = "[" + startTag + "]" + highlighted.text + "[/" + endTag + "]";
		highlighted.collapse(false);
		highlighted.moveStart('character',moveCaret);
		highlighted.moveEnd('character',moveCaret);
		highlighted.select();
	}
	//firefox, opera and safari
	else
	{
		stuff = document.getElementById(type);
		backpart = stuff.value.substr(stuff.selectionEnd);
		if (stuff.selectionStart == stuff.selectionEnd) moveCaret = -1 * (endTag.length + 3);
		else moveCaret = 0;
		stuff.value = stuff.value.substr(0, stuff.selectionStart) + "[" + startTag + "]" + stuff.value.substr(stuff.selectionStart, stuff.selectionEnd - stuff.selectionStart) + "[/" + endTag + "]";
		putCaretHere = stuff.selectionEnd + moveCaret;
		stuff.value += backpart;
		stuff.selectionStart = putCaretHere;
		stuff.selectionEnd = putCaretHere;
	}
	document.getElementById(type).focus();
}

function atAdminPage()
{
	//determines whether or not we are at the admin page. also gives away location of admin page.
	//if you can read a regex, you probably deserve to find the admin page anyway.
	if (null == document.URL.match(/^http:\/\/[^\/]*\/?[^\/]*\/moa\/?/i)) return false;
	return true;
}