function smileTable(theform, addspot)
{
		document.writeln('<table border="0" cellspacing="1" cellpadding="0" style="float: right; margin: -5px 5px 5px 5px;"><tr>');
		var facelist = new Array(':)', ';)', ':D', ';D', ':@', ':(', ':o', '8)', ':P', ':-[', ':-X', ':-|', ':-*', ':*(');
		var filelist = new Array('01', '02', '03', '04', '05', '06', '07', '08', '10', '11', '12', '13', '14', '15', '16');
		
		var total = 15;
		for(var x = 0; x < total; x++)
		{
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\''+facelist[x]+'\')"><img src="/img/smiles/face-'+filelist[x]+'.gif" border="0" onmouseover="this.style.opacity=0.5;this.filters.alpha.opacity=50;" onmouseout="this.style.opacity=1.0; this.filters.alpha.opacity=100;" /></a></td>');
			if(Math.floor(total/2) == x)
				document.writeln('</tr><tr>');
		}
		document.writeln('</tr></table>');
}

function ins_smiles(theform,smile) {
	//Insert emoticons
	insertAtCaret(theform, " "+smile+" ");
	theform.focus();
}

function colour_bar(theform, addspot) {
	var r = 0;
	var color = '';
	var numberList = new Array('ffffff', 'dddddd', 'c0c0c0', '969696', '808080', '646464', '4b4b4b', '242424', '000000', '0000aa', '5555ff', '00aa00', '55ff55', '00aaaa', '55ffff', 'aa0000', 'ff5555', 'aa00aa', 'ff55ff', 'aa5500', 'ffff55');

	document.writeln('<table cellspacing="1" cellpadding="0" class="font_table"><tr>');

	for (r = 0; r < 21; r++)
	{
		color = numberList[r];
		document.write('<td bgcolor="#' + color + '" width="9" height="9">');
		document.write('<a href="javascript:void()" onclick="cl_styles(document.'+theform+'.'+addspot+',\'color=#'+color+'\',\'color\')"><img src="/img/spacer.gif" width="9" height="9" alt="#' + color + '" title="#' + color + '" style="display: block;"/></a>');
		document.writeln('</td>');

		document.writeln('</tr><tr>');
	}
	
	document.writeln('</tr></table>');
}

function cl_styles(theform,sb_code,sb_code_end) {
	//Insert font color
	var selected_text = getSelectedText(theform);
	
	insertAtCaret(theform, "["+sb_code+"]"+selected_text+"[/"+sb_code_end+"]");
	theform.focus();
}

// Insert Style Tags
function ins_styles(theform,sb_code,prompt_text,tag_prompt) {
	// Insert [x]yyy[/x] style markup
	
	// Get selected text
	var selected_text = getSelectedText(theform);
	
	if (selected_text == '') {
		// Display prompt if no text is selected
		var inserttext = prompt( 'Enter the text to be formatted:'+"\n["+sb_code+"]...[/"+sb_code+"]", '' );
		if ( (inserttext != null) ) {
			insertAtCaret(theform, "["+sb_code+"]"+inserttext+"[/"+sb_code+"]");
			theform.focus();
		}
	} else {
		// Insert text automatically around selection
		insertAtCaret(theform, "["+sb_code+"]"+selected_text+"[/"+sb_code+"]");
		theform.focus();
	}
}

// Insert Image Tag
function ins_image(theform,prompt_text) {
	// Insert [x]yyy[/x] style markup
	inserttext = prompt('Enter the URL for the image:'+"\n[img="+prompt_text+"...]",prompt_text);
	if ((inserttext != null) && (inserttext != "")) {
		insertAtCaret(theform, "[img="+inserttext+"]");
	}
	theform.focus();
}

// Insert Image Tag
function ins_image_v2(theform) {
	image_url = prompt('Enter the URL for the image:'+'\n[img=http://...] or [img=...]\n\nOptional:\nwidth=... height=... popup=true/false float=left/right','http://');
	if ((image_url != null) && (image_url != '')) {
		// Optional
		image_width = prompt('Width (Optional):'+'\n[img=... width=...]','');
		image_height = prompt('Height (Optional):'+'\n[img=... height=...]','');
		image_popup = prompt('View full-size in pop-up when clicked (Optional):'+'\n[img=... popup=true/false]', '');
		image_float = prompt('Float (Optional):'+'\n[img=... float=left/right]','');
		
		str = '[img='+image_url;
		if ((image_width != null) && (image_width != '')) {
			str += ' width='+image_width;
		}
		if ((image_height != null) && (image_height != '')) {
			str += ' height='+image_height;
		}
		if ((image_popup != null) && (image_popup != '')) {
			image_popup.toLowerCase;
			if ( image_popup == 'true' || image_popup == 'false' ) {
				str += ' popup='+image_popup;
			}
		}
		if ((image_float != null) && (image_float != '')) {
			image_float.toLowerCase;
			if ( image_float == 'left' || image_float == 'right' ) {
				str += ' float='+image_float;
			}
		}
		str += ']';
		
		insertAtCaret(theform, str);
		theform.focus();
	
	}
}

// Insert Image Dropdown Menu
function ins_image_dropdown(theform,theImage) {
	if (theImage.value != '-'+'-') {
		insertAtCaret(theform, theImage.value);
		theform.focus();
	}
}

// Insert URL Tag
function ins_url(theform) {

	// inserts named url link - [url=mylink new=true]text[/url]
	link_url = prompt('Enter the full URL for the link:'+'\n[url=...][/url]',"http://");
	if ( (link_url != null) ) {
	
		// Get selected text
		var link_text = getSelectedText(theform);
		if (link_text == '') {
			// Display prompt if no text is selected
			link_text = prompt('Enter the text to be displayed for the link (Optional):'+'\n[url=]...[/url]',"");
		}
		if ( (link_text == null) || (link_text == '') ) {
			link_text = link_url;
		}
		link_target = prompt('Open URL in new window (Optional):'+'\n[url= new=true/false][/url]','');
		str = '[url='+link_url;
		if ((link_target != null) && (link_target != '')) {
			link_target.toLowerCase;
			if ( link_target == 'true' || link_target == 'false' ) {
				str += ' new='+link_target;
				
			}
		}
		str += ']'+link_text+'[/url]';
		
		insertAtCaret(theform, str);
		theform.focus();
	}
}

// Insert URL Tag
function ins_url_no_options(theform) {
	// inserts named url link - [url=mylink new=true]text[/url]
	link_url = prompt('Enter the full URL for the link:'+'\n[url=...][/url]',"http://");
	if ( (link_url != null) ) {
		// Get selected text
		var link_text = getSelectedText(theform);
		if (link_text == '') {
			// Display prompt if no text is selected
			link_text = prompt('Enter the text to be displayed for the link (Optional):'+'\n[url=]...[/url]',"");
		}
		if ( (link_text == null) || (link_text == '') ) {
			link_text = link_url;
		}
		str = '[url='+link_url+']'+link_text+'[/url]';
		
		insertAtCaret(theform, str);
		theform.focus();
	}
}

// From:
// http://parentnode.org/javascript/working-with-the-cursor-position/
function insertAtCaret2(obj, text) {
	if (document.selection && document.selection.createRange) {
		// Internet Explorer 4.0x
		
		obj.focus();
		var orig = obj.value.replace(/\r\n/g, "\n"); // IE Bug
		var range = document.selection.createRange();

		if(range.parentElement() != obj) {
			return false;
		}

		range.text = text;
		
		var actual = tmp = obj.value.replace(/\r\n/g, "\n");

		for(var diff = 0; diff < orig.length; diff++) {
			if(orig.charAt(diff) != actual.charAt(diff)) break;
		}

		for(var index = 0, start = 0; 
			tmp.match(text.toString()) 
				&& (tmp = tmp.replace(text.toString(), "")) 
				&& index <= diff; 
			index = start + text.toString().length
		) {
			start = actual.indexOf(text.toString(), index);
		}
	} else if (obj.selectionStart >= 0) {
		// FireFox & Safari
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	}
	
	if (start != null) {
		setCaretTo(obj, start + text.length);
	} else {
		obj.value += text;
	}
}

function getSelectedText(obj) {
	if (document.selection && document.selection.createRange) {
		// Internet Explorer 4.0x
		
		obj.focus();
		var orig = obj.value.replace(/\r\n/g, "\n"); // IE Bug
		var range = document.selection.createRange();
		
		if (range.parentElement() != obj) {
			return '';
		}
		
		txt = range.text;
		
		return txt;
	} else if (obj.selectionStart >= 0) {
		// FireFox & Safari
		var start = obj.selectionStart;
		var end    = obj.selectionEnd;
		var txt    = obj.value.substr(start, end-start);
		
		return txt;
	} else {
		return '';
	}
}

function setCaretTo(obj, pos) {
	if(obj.createTextRange) {
		var range = obj.createTextRange();
		range.move('character', pos);
		range.select();
	} else if(obj.selectionStart) {
		obj.focus();
		obj.setSelectionRange(pos, pos);
	}
}

function getSel() {
	var txt = '';
	var foundIn = '';
	if (window.getSelection) {
		// the alternative code
		txt = window.getSelection();
		foundIn = 'window.getSelection()';
	} else if (document.getSelection) {
		// Navigator 4.0x
		txt = document.getSelection();
		foundIn = 'document.getSelection()';
	} else if (document.selection) {
		// Internet Explorer 4.0x
		txt = document.selection.createRange().text;
		foundIn = 'document.selection.createRange()';
	} else {
		return;
	}
	return txt;
}

function insertAtCaret(obj, text) {
	var mytext;
	obj.focus();
	
	if (document.selection) {
		// 'Code For IE'
		text = text;
		if (obj.createTextRange && obj.caretPos) {
			var caretPos = obj.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? text + '' : text;
			return;
		}
	} else if (obj.selectionStart!==false) {
		// 'Code for Gecko'
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;
		
		obj.value = obj.value.substr(0, start) + text + obj.value.substr(end, obj.value.length);
	}
	
	if (start != null) {
		setCaretTo(obj, start + text.length);
	} else {
		obj.focus();
		obj.value += text;
	}
}

// Insert at Caret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

// Validate Entry Form
function validate(theform) {
	if (theform.static_text.value=="" || theform.static_subject.value=="") {
		alert("Please complete the Subject and Entry fields.");
		return false;
	} else {
		return true;
	}
}

// Validate Static Form
function validate_static(theform) {
	if (theform.static_text.value=="" || theform.static_subject.value=="" || theform.link.value=="" ) {
		alert("Please complete the Subject, Entry and Link fields.");
		return false;
	} else {
		return true;
	}
}

// Validate Comments Form
function validate_comment(theform) {
  if ( theform.static_text.value.length < 15 || theform.comment_name.value.length < 3 ) {
	alert("Please complete the Name and Comment fields.\nMinumum 3 characters for name, 15 for comment.");
	return false;
  } else {
	return true;
  }
}