// JavaScript Document
function addBBCode(textbox, codeStart, codeEnd)
{
	var tb = document.getElementById(textbox);
	
	if (document.selection)
	{
		var str=document.selection.createRange().text;
		var sel=document.selection.createRange();
		sel.text= codeStart + str + codeEnd;
	}
	else if(typeof tb.selectionStart != 'undefined')
	{
		var before, after, selection;
		before= tb.value.substring(0, tb.selectionStart)
		selection = tb.value.substring(tb.selectionStart, tb.selectionEnd)
		after = tb.value.substring(tb.selectionEnd, tb.value.length)
	 	
		var newValue = new String("");
		tb.value = newValue.concat(before, codeStart, selection, codeEnd, after);
	}
	//tb.focus();
}

function addYoutubeVideoBBCode(textbox)
{
	var enterURL   = prompt("Give the ID of the YouTube movie \n  The section after 'http://www.youtube.com/watch?v=' e.g. t5R9bJOulhY", "Enter Here");
	
	if(!enterURL)
	{
		alert("Error:  You did not give an ID");
	}
	else	
	{
		addBBCode(textbox, "[youtubevid]"+enterURL, "[/youtubevid]");
	}
}
			 
function addWysiwygBar(divId, div2)
{
	var div = document.getElementById(divId);
	var content = new String("");
	
	if(getInternetExplorerVersion() == -1)
	{
		content = "<div><span>"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[b]', '[/b]');\">Bold</a>]"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[i]', '[/i]');\">Italic</a>]"
				 +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[quote]', '[/quote]');\">Quote</a>]"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[code]', '[/code]');\">Code</a>]"
				 +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addYoutubeVideoBBCode('"+div2+"');\">Youtube Video</a>]"
				 +"</span></div>"
				 +"<div><span>"
				 +"<a href=\"javascript:void(0);\" onclick=\"addAdvancedWysiwygBar('"+divId+"', '"+div2+"')\">Open Advanced BBCode</a>"
				 +"</span></div>";
		
		div.innerHTML = content;
	}
	else
	{
		content = "<div><span>"
				 +"Warning:  Internet Explorer doesn't support our editor feature.  You will need to use it manually."
				 +"</span></div>"
				 +"<div><span>"
				 +"<b>Bold</b>: [b]Text[/b], <b>Italic</b>: [i]Text[/i], <b>Quote</b>: [quote]Text[/quote], <b>Code</b>: [code]Text[/code]"
				 +"</span></div>"
				 +"<div><span>"
				 +"<b>2nd Header</b>: [h2]Text[/h2], <b>3rd Header</b>: [h3]Text[/h3]"
				 +"</span></div>";
		
		div.innerHTML = content;
	}
}

function addAdvancedWysiwygBar(divId, div2)
{
	var div = document.getElementById(divId);
	var content = new String("");
	
	if(getInternetExplorerVersion() == -1)
	{
		content = "<div><span>"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[b]', '[/b]');\">Bold</a>]"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[i]', '[/i]');\">Italic</a>]"
				 +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[quote]', '[/quote]');\">Quote</a>]"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[code]', '[/code]');\">Code</a>]"
				 +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addYoutubeVideoBBCode('"+div2+"');\">Youtube Video</a>]"
				 +"</span></div>"
				 +"<div><span>"
				 +"<a href=\"javascript:void(0);\" onclick=\"addWysiwygBar('"+divId+"', '"+div2+"')\">Close Advanced BBCode</a>"
				 +"</span></div>"
				 +"<div></span>"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[h2]', '[/h2]');\">2nd Header</a>]"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[h3]', '[/h3]');\">3rd Header</a>]"
				 +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[sub]', '[/sub]');\">Subscript</a>]"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[sup]', '[/sup]');\">Superscript</a>]"
				 +"[<a href=\"javascript:void(0);\" onclick=\"addBBCode('"+div2+"', '[strike]', '[/strike]');\">Strikethrough</a>]"
				 +"</span></div>";
		
		div.innerHTML = content;
	}
	else
	{
		addWysiwygBar(divId, div2);
	}
}

function ifBroswerIsIE(divId, div2)
{
	
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		{
			rv = parseFloat( RegExp.$1 );
		}
	}
	return rv;
}
