// JS QuickTags version 1.2
//
// Copyright (c) 2002-2005 Alex King
// http://www.alexking.org/
//
// Licensed under the LGPL license
// http://www.gnu.org/copyleft/lesser.html
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
// **********************************************************************
//
// This JavaScript will insert the tags below at the cursor position in IE and 
// Gecko-based browsers (Mozilla, Camino, Firefox, Netscape). For browsers that 
// do not support inserting at the cursor position (Safari, OmniWeb) it appends
// the tags to the end of the content.
//
// The variable 'edCanvas' must be defined as the <textarea> element you want 
// to be editing in. See the accompanying 'index.html' page for an example.

function smiley(myValue) {
	myField = document.forms.messageboard.message;
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += myValue;
		myField.focus();
	}
}

function tag(myField, start, end) {
	//IE support
	if (document.selection) {
		myField.focus();
	    sel = document.selection.createRange();
		if (sel.text.length > 0) {
			sel.text = start + sel.text + end;
		}
		else {
		//	if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
		//		sel.text = edButtons[i].tagStart;
		//		edAddTag(i);
		//	}
		//	else {
		//		sel.text = edButtons[i].tagEnd;
		//		edRemoveTag(i);
		//	}
		sel.text = start + end;
		}
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = myField.scrollTop;
		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
			              + start
			              + myField.value.substring(startPos, endPos) 
			              + end
			              + myField.value.substring(endPos, myField.value.length);
			cursorPos += start.length + end.length;
		}
		else {
		//	if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
				myField.value = myField.value.substring(0, startPos) 
				              + start + end
				              + myField.value.substring(endPos, myField.value.length);
		//		edAddTag(i);
		//		cursorPos = startPos + edButtons[i].tagStart.length;
		//	}
		//	else {
		//		myField.value = myField.value.substring(0, startPos) 
		//		              + edButtons[i].tagEnd
		//		              + myField.value.substring(endPos, myField.value.length);
		//		edRemoveTag(i);
		//		cursorPos = startPos + edButtons[i].tagEnd.length;
		//	}
			cursorPos = startPos + start.length;
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		//if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
		//	myField.value += edButtons[i].tagStart;
		//	edAddTag(i);
		//}
		//else {
		//	myField.value += edButtons[i].tagEnd;
		//	edRemoveTag(i);
		//}
		myField.value += start + end;
		myField.focus();
	}
}


function link(myField, myURL, myTITLE) {
	if (myURL.value == "http://"||!myURL.value) { alert("You have not entered a URL.  Please try again."); return false; }
	if (myURL.value.substring(14,0) == "http://http://") { alert("Your URL appears to contain a duplicate \"http://\" and therefore will not function correctly.  Please try again."); return false; }
	if (!myTITLE.value) { myTITLE.value = myURL.value; }
	var myValue = "[url=" + myURL.value + "]" + myTITLE.value + "[/url]";
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += myValue;
		myField.focus();
	}
	myURL.value = "http://";
	myTITLE.value = "";
}

function image(myField, myURL) {
	if (myURL.value == "http://"||!myURL.value) { alert("You have not entered a URL.  Please try again."); return false; }
	if (myURL.value.substring(14,0) == "http://http://") { alert("Your URL appears to contain a duplicate \"http://\" and therefore will not function correctly.  Please try again."); return false; }
	var myValue = "[img]" + myURL.value + "[/img]";
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += myValue;
		myField.focus();
	}
	myURL.value = "http://";
}

function youtubeb(myField, myURL) {
	if (myURL.value == "http://"||!myURL.value) { alert("You have not entered a URL.  Please try again."); return false; }
	if (myURL.value.substring(14,0) == "http://http://") { alert("Your URL appears to contain a duplicate \"http://\" and therefore will not function correctly.  Please try again."); return false; }
	var myValue = "[youtube]" + myURL.value + "[/youtube]";
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += myValue;
		myField.focus();
	}
	myURL.value = "http://";
}


//
//
//
//
//
// miscellaneous unrelated functions, here because it's already loaded!
//
//
//
//

function add_a_link() {
	if (document.getElementById("optional_link").style.display == "none") {
		document.getElementById("optional_link").style.display = "";
		document.getElementById("linkspan").style.border = "1px solid #999999";
		document.messageboard["url_title"].focus();
	}
	else {
		document.getElementById("optional_link").style.display = "none";
	}
}


function includepoll() {
	if (document.getElementById("poll_form").style.display == "none") {
		document.getElementById("poll_form").style.display="";
		document.forms.msg.poll_post.value="1";
		//alert("You may now scroll down to include a poll in this post.");
	}
	else {
		document.getElementById("poll_form").style.display="none";
		document.forms.msg.poll_post.value="0";
	}
}

function add_an_image() {
	if (document.getElementById("optional_image").style.display == "none") {
		document.getElementById("optional_image").style.display = "";
		document.getElementById("imagespan").style.border = "1px solid #999999";
		document.messageboard.img.focus();
	}
	else {
		document.getElementById("optional_image").style.display = "none";
	}
}
function add_youtube() {
	if (document.getElementById("optional_youtube").style.display == "none") {
		document.getElementById("optional_youtube").style.display = "";
		document.getElementById("youtubespan").style.border = "1px solid #999999";
		document.messageboard.youtube.focus();
	}
	else {
		document.getElementById("optional_youtube").style.display = "none";
	}
}

function smilies() {
	if (document.getElementById("smilies").style.display == "none") {
		document.getElementById("smilies").style.display = "";
		//document.getElementById("linkspan").style.border = "1px solid #999999";
	}
	else {
		document.getElementById("smilies").style.display = "none";
	}
}
function toggle(over,spanid) {
	if (over) {
		document.getElementById(spanid).style.border = "1px solid #999999";
	}
	else {
		if (spanid == "smileyspan") { 
			if (!(document.getElementById("smilies").style.display == "none")) { return; }
		}
		else if (spanid == "pollspan") {
			if (!(document.getElementById("poll_form").style.display == "none")) { return; }
		}
		else if (spanid == "imagespan") {
			if (!(document.getElementById("optional_image").style.display == "none")) { return; }
		}
		else if (spanid == "linkspan") {
			if (!(document.getElementById("optional_link").style.display == "none")) { return; }
		}
		document.getElementById(spanid).style.border = "1px solid #EEEEEE";
	}
}