function smileTable(theform, addspot)
{
		document.writeln('<table border="0" cellspacing="1" cellpadding="0" style="float: right; margin: 10px; position: relative;"><tr>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':)\')"><img src="img/smiles/face-01.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\';)\')"><img src="img/smiles/face-02.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':D\')"><img src="img/smiles/face-03.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\';D\')"><img src="img/smiles/face-04.gif" border="0" /></a></td></tr>');

			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':@\')"><img src="img/smiles/face-05.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':(\')"><img src="img/smiles/face-06.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':o\')"><img src="img/smiles/face-07.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\'8)\')"><img src="img/smiles/face-08.gif" border="0" /></a></td></tr>');
			
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\'::)\')"><img src="img/smiles/face-10.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':P\')"><img src="img/smiles/face-11.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':-[\')"><img src="img/smiles/face-12.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':-X\')"><img src="img/smiles/face-13.gif" border="0" /></a></td></tr>');

			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':-|\')"><img src="img/smiles/face-14.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':-*\')"><img src="img/smiles/face-15.gif" border="0" /></a></td>');
			document.writeln('<td><a href="javascript:void()" onclick="ins_smiles(document.'+theform+'.'+addspot+',\':*(\')"><img src="img/smiles/face-16.gif" border="0" /></a></td></tr>');
		document.writeln('</table>');
}

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

function colorPalette(dir, width, height, theform, addspot)
{
	var r = 0, g = 0, b = 0;
	var numberList = new Array(6);
	var color = '';

	numberList[0] = '00';
	numberList[1] = '40';
	numberList[2] = '80';
	numberList[3] = 'BF';
	numberList[4] = 'FF';

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

	for (r = 0; r < 5; r++)
	{
		if (dir == 'h')
		{
			document.writeln('<tr>');
		}

		for (g = 0; g < 5; g++)
		{
			if (dir == 'v')
			{
				document.writeln('<tr>');
			}
			
			for (b = 0; b < 5; b++)
			{
				color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
				document.write('<td bgcolor="#' + color + '" width="' + width + '" height="' + height + '">');
				document.write('<a href="javascript:void()" onclick="cl_styles(document.'+theform+'.'+addspot+',\'color=#'+color+'\',\'color\')"><img src="img/spacer.gif" border="0" width="' + width + '" height="' + height + '" title="#' + color + '" style="border: 0px solid #000; display: block;"/></a>');
				document.writeln('</td>');
			}

			if (dir == 'v')
			{
				document.writeln('</tr>');
			}
		}

		if (dir == 'h')
		{
			document.writeln('</tr>');
		}
	}
	document.writeln('</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+"]xxx[/"+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 Style Tags
function ins_style_dropdown(theform, sb_code) {
	// Insert [sb_code]xxx[/sb_code] style markup
	
	if ( sb_code != '-'+'-' ) {
		// Get selected text
		var selected_text = getSelectedText(theform);
	
		if (selected_text == '') {
			prompt_text = '[' + sb_code + ']xxx[/' + sb_code + ']';
			user_input = prompt( prompt_text, '' );
			if ( (user_input != null) ) {
				insertAtCaret(theform, '['+sb_code+']'+user_input+'[/'+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+"xxx]",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://xxx] or [img=xxx]\n\nOptional:\nwidth=xxx height=xxx popup=true/false float=left/right','http://');
	if ((image_url != null) && (image_url != '')) {
		// Optional
		image_width = prompt('Width (Optional):'+'\n[img=xxx width=xxx]','');
		image_height = prompt('Height (Optional):'+'\n[img=xxx height=xxx]','');
		image_popup = prompt('View full-size in pop-up when clicked (Optional):'+'\n[img=xxx popup=true/false]', '');
		image_float = prompt('Float (Optional):'+'\n[img=xxx 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=xxx][/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=]xxx[/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=xxx][/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=]xxx[/url]',"");
		}
		if ( (link_text == null) || (link_text == '') ) {
			link_text = link_url;
		}
		str = '[url='+link_url+']'+link_text+'[/url]';
		
		insertAtCaret(theform, str);
		theform.focus();
	}
}

//Insert Emoticon
function ins_emoticon(theform, emoticon) {
	insertAtCaret(theform, emoticon);
	theform.focus();
}

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

// Validate the 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;
	}
}

// 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();
}

function validate_signup(theform) 
{
	var error = "";

	var usereg = /^([A-Za-z0-9_\-]{3,10})$/;
	if (usereg.test(theform.username.value) == false) 
	{
		error += "Username must be 3 to 10 characters.\n [Only letters, numbers, \"_\" and \"-\" are valid.]\n";
	}

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(theform.email.value) == false) 
	{
		error += "Valid email address was not given.\n";
	}

	var firreg = /^([A-Za-z]{1,25})$/;
	if (firreg.test(theform.first.value) == false) 
	{
		error += "First name was not given.\n";
	}
	
	var lasreg = /^([A-Za-z]{1,25})$/;
	if (lasreg.test(theform.last.value) == false) 
	{
		error += "Last name was not given.\n";
	}

	for(var i=0; i<theform.gender.length; i++)
	{
		if(theform.gender[i].checked==true) break;
		
		if(theform.gender[i].checked==false && i == (theform.gender.length - 1)) 
		{
			error += "Gender was not selected.\n";
			var genderror = 1;
		}
	}
	
	if ( theform.month.value.length == 0 || theform.day.value.length == 0) 
	{
		error += "Month and day of birth were not selected.\n";
	}
	else
	if ( theform.month.value == 2 && theform.day.value > 29) 
	{
		error += "February only has 28* days.\n";
	}
	else
	if ( ( theform.month.value == 4 || theform.month.value == 6 || theform.month.value == 9 || theform.month.value == 11 ) && theform.day.value > 30) 
	{
		error += "The month selected only has 30 days.\n";
	}
	
	if ( theform.year.value.length == 0) 
	{
		error += "Year of birth was not selected.\n";
	}	
	
	if ( theform.country.value.length == 0) 
	{
		error += "Country was not selected.\n";
	}
	
	if (error.length != 0)
	{
		if( theform.username.value == 0 && theform.email.value == 0 && theform.first.value == 0 && theform.last.value == 0 && genderror == 1 && theform.month.value == 0 && theform.day.value == 0 && theform.year.value == 0 && theform.country.value == 0 )
		{
			alert("Epic Fail!\n(Fill out the form!)");
		}
		else
		{
			alert(error);
		}
		return false;
	}
	else
	{
		return true;
	}
}

function validate_profile(theform) 
{
	var error = "";

	var regpass = /^([ ]+)$/;
	if(theform.password.value.length == 0 || regpass.test(theform.password.value) == true) 
	{
		error += "Your current password is required to make any changes.\n";
	}
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(theform.email.value.length != 0 && reg.test(theform.email.value) == false) 
	{
		error += "Valid email address was not given.\n";
	}

	var passreg = /^([A-Za-z0-9]{5,25})$/;
	if(theform.new_password.value.length != 0 && passreg.test(theform.new_password.value) == false) 
	{
		error += "Passwords can only use numbers and letters\n and must be 5-25 characters.\n";
	}

	var firreg = /^([A-Za-z]{1,25})$/;
	if (firreg.test(theform.first.value) == false) 
	{
		error += "First name was not given.\n";
	}
	
	var lasreg = /^([A-Za-z]{1,25})$/;
	if (lasreg.test(theform.last.value) == false) 
	{
		error += "Last name was not given.\n";
	}
	
	if ( theform.country.value.length == 0) 
	{
		error += "Country was not selected.\n";
	}
	
	var shopreg = /^([A-Za-z0-9 ]{3,20})$/;
	if ( theform.myshop.value == 1 && shopreg.test(theform.shop_name.value) == false)
	{
		error += "Shop name can only use numbers and letters\n and must be 3-20 characters.\n";		
	}
	
	if (error.length != 0)
	{
		alert(error);
		return false;
	}
	else
	{
		return true;
	}
}

function validate_forgotten(theform) 
{
	var error = "";

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(theform.email.value) == false) 
	{
		error += "Valid email address was not given.\n";
	}

	if (error.length != 0)
	{
		alert(error);
		return false;
	}
	else
	{
		return true;
	}
}

function validate_forgotten_two(theform) 
{
	var error = "";

	var reg = /^([A-Za-z0-9]{5,25})$/;
	if(reg.test(theform.password.value) == false) 
	{
		error += "Passwords can only use numbers and letters\n and must be 5-25 characters.\n";
	}
	
	if(theform.password.value != theform.r_password.value)
	{
		error += "The passwords entered did not match.\n";
	}
	
	if (error.length != 0)
	{
		alert(error);
		return false;
	}
	else
	{
		return true;
	}
}

function validate_mail(theform) 
{
	var error = "";
	
	var toreg = /^([A-Za-z]{3,25})$/;
	if (toreg.test(theform.sendto.value) == false) 
	{
		error += "Recipient name was not given.\n";
	}

	var subreg = /^([A-Za-z0-9_\-\., "']{5,70})$/;
	if(subreg.test(theform.subject.value) == false) 
	{
		error += "Subjects must be 5-70 characters.\n";
	}

	if(theform.message.value.length < 15 || theform.message.value.length > 2000) 
	{
		error += "Messages must be between 15 and 2000 characters.\n";
	}
	
	if (error.length != 0)
	{
		alert(error);
		return false;
	}
	else
	{
		return true;
	}
}

function validate_search(theform) 
{
	var error = "";

	var reg = /^[A-Za-z0-9_\-\. ]{3,}$/;
	if(reg.test(theform.search.value) == false) 
	{
		error += "Search must be 3 or more characters.\n";
	}

	if (error.length != 0)
	{
		alert(error);
		return false;
	}
	else
	{
		return true;
	}
}

function checkAll(checkname, exby) {
for (i = 0; i < checkname.length; i++)
checkname[i].checked = exby.checked? true:false
}

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // trim when over
	field.value = field.value.substring(0, maxlimit);
	// update counter
	else
	cntfield.value = maxlimit - field.value.length;
}

function directionally(letter) {
	if(letter == 1) {
		alert("You can't go that way!");
		return false;
	}
}