// Browser detection
var if_userAgent = navigator.userAgent.toLowerCase();
var ie4 = (document.all && !document.getElementById) ? true : false; // IE 4
var ie5 = (document.all && document.getElementById) ? true : false; // IE 5+
var opera8 = ((if_userAgent.indexOf('opera 8') != -1 || if_userAgent.indexOf('opera/8') != -1) ? 1 : 0); // Opera 8
var ns6 = (!document.all && document.getElementById) ? true : false; // Netscape 6 & Firefox

function bbCode(startTag, endTag, objTextBox) {

	// Get reference to object
	objTextBox = document.getElementById(objTextBox);

	if (ie4 || ie5 || opera8) {
		objTextBox.focus();

		var s = objTextBox.document.selection;	
		var r = s.createRange(); 
		
		var txt; 
		if (r.text.length > 0) 
			{txt = startTag + r.text + endTag;}
		else 
			{txt = startTag + endTag;}
			
		r.text = txt;
		r.moveEnd("character", - (endTag.length));
		r.select();
	} 
	else if (ns6) {
		if (objTextBox.selectionEnd) {
			// Not at beginning
			var charPos; 
			var charMiddle;
			var selStart = objTextBox.selectionStart;
			var selEnd = objTextBox.selectionEnd; if (selEnd <= 2) {selEnd = objTextBox.textLength;}
			var charStart = objTextBox.value.substring(0, selStart);
			var charEnd = objTextBox.value.substring(selEnd, objTextBox.textLength);

			if (selStart != selEnd) {
				// A selection of text
				charMiddle = objTextBox.value.substring(selStart, selEnd);
				objTextBox.value = charStart + startTag + charMiddle + endTag + charEnd;
				charPos = selStart + (startTag.length + charMiddle.length);} 
			else {
				// At beginning of box
				objTextBox.value = charStart + startTag + endTag + charEnd;
				charPos = selStart + (startTag.length);
			}
			objTextBox.selectionStart = charPos;
			objTextBox.selectionEnd = charPos;
		} 
		else {
			// At beginning
			objTextBox.value += startTag + endTag;
			objTextBox.selectionStart = (startTag.length);
			objTextBox.selectionEnd = (startTag.length);
		}
	} 
	else {
		objTextBox.value += startTag + endTag;
	}

	objTextBox.focus();	
}

function insertHyperlink(objTextBox) {
	var url = prompt('Enter the full URL of the Hyperlink, e.g. http://www.website.com/', 'http://')
	if (url == null)
		return;
	else {
		var text = prompt('Enter the text you would like to display for the link', url)
		if (text == null)
			return;
		else
			bbCode('[url=' + url + ']' + text, '[/url]', objTextBox);
	}
}

function insertImage(objTextBox) {
	var imageurl = prompt('Enter the full URL of the Image, e.g. http://www.website.com/image1.jpg', 'http://')
	if (imageurl == null)
		return;
	else {
		bbCode('[img]' + imageurl, '[/img]', objTextBox);
	}
}

function showColorPalette(iFrameColorPalette) {
	if (document.getElementById(iFrameColorPalette).style.visibility == "hidden") {
		document.getElementById(iFrameColorPalette).style.visibility = "visible";
		document.getElementById(iFrameColorPalette).style.display = "inline";
	}
	else {
		document.getElementById(iFrameColorPalette).style.visibility = "hidden";
		document.getElementById(iFrameColorPalette).style.display = "none";
	}
}

function showMoreEmoticons(pWindowURL) {
	wopen(pWindowURL, 'MoreEmoticons', 260, 300);
}

function wopen(url, name, w, h)
{
	// Fudge factors for window decoration space.
	w += 32;
	h += 96;
	wleft = (screen.width - w) / 2;
	wtop = (screen.height - h) / 2;
	
	var win = window.open(url,
	name,
	'width=' + w + ', height=' + h + ', ' +
	'left=' + wleft + ', top=' + wtop + ', ' +
	'location=no, menubar=no, ' +
	'status=no, toolbar=no, scrollbars=yes, resizable=no');
	
	// Just in case width and height are ignored
	win.resizeTo(w, h);
	
	// Just in case left and top are ignored
	win.moveTo(wleft, wtop);
	win.focus();
}

function clearDialogs(iFrameColorPalette) {
	document.getElementById(iFrameColorPalette).style.visibility = "hidden";
	document.getElementById(iFrameColorPalette).style.display = "none";				
}

function isPositiveNumeric(data) {
	// Check both for a number as well as being positive e.g. for page numbering
	if (isNumeric(data))
	{
		if (data > 0)
			return true;
		else
			return false;
	}
	else
		return false;
}

function isNumeric(data) {          
	var valid = "0123456789."; var checktemp; var start; var decimals;

	// first check that data is not completely null
	if (data == null)
		return false;

	decimals = 0;
	
	// if the first character is a minus sign - that is OK
	if (data.substring(0, 1) == "-")
		start = 1;
	else
		start = 0;
								
	// go through the string.  no decimal allowed at beginning or end.
	for (var i=start; i<data.length; i++)
	{
		checktemp = "" + data.substring(i, i+1)
		if (checktemp == ".")
			if ((i == start) || (i == (data.length - 1)))
				return false;
			else
				decimals++;
									
		if (valid.indexOf(checktemp) == "-1")
			return false;
	}
								
	// if the string is empty
	if ((start - i) == 0)
		return false;
								
	// if there are too many decimals
	if (decimals > 1)
		return false;
								
	return true;
}
