//		Generic TextBox / HTML Code Helper (TextArea).  Designed / tested for stumbleupon
//		Copyright (C) 2005  Jon Cummings
//
//		This program is free software; you can redistribute it and/or
//		modify it under the terms of the GNU General Public License
//		as published by the Free Software Foundation; either version 2
//		of the License, or (at your option) any later version.
//
//		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.  See the
//		GNU General Public License for more details.
//
//		You should have received a copy of the GNU General Public License
//		along with this program; if not, write to the Free Software
//		Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//		http://www.gnu.org/copyleft/gpl.html
//		http://www.gnu.org/licenses/gpl.txt


// Script requirements:  Firefox with Greasemonkey 3.3 (or above) extension installed.
// To install:  Rename the file to ' su_navigator.user.js ' or any filename with a ' user.js ' extension. Load the user script file into Firefox, either through the file menu or drag/drop.  Goto tools->Install User Script.
// To uninstall or disable:  Goto tools->manage user script.
//
// Description:
// As above:  A Generic TextBox/HTML Code Helper (TextArea) to help or speed up entry of html code into the main textarea element present on a page.

// version:  0.0.4
// date:   July 9, 2005

// meta
// ==UserScript==
// @name SU TextBox Assistant
// @description SU TextBox Assistant
// @namespace http://jc68hc11dll.stumbleupon.com/userscripts/
// @include http://*.stumbleupon.com/*
// ==/UserScript==

////  /*         DISABLED - testing only            file:///*SU_LOCALTESTPAGE_*.htm         */

(function()
{
	/////////////////////////
	//Globals////////////////
	var tDocURL="";
	var target_form=null;
	var target_textarea=null;
	var txtUserInput=null;
	var selFont=null;
	var selSize=null;
	var selColor=null;
	var chbxTargetNew=null;
	var divEdit=null;
	var btnDisplay=null;

	//new
	var selHTML=null;
	var btnMiscHTML=null;
	var btnTemplate=null;
	var divTemplate=null;
	var txtTemplate=null;
	var btnScrubHTML=null;

	//new
	var chbxBlog=null;
	var chbxForum=null;
	var chbxReview=null;

	//global "constants"
	//var tParam_TemplateStore_Main="stumbleupon";		//obsolete
	var tStoreParam_Template_Content="template_content";
	var tStoreParam_Template_Apply="template_apply";


	//debug counter
	var dbg=0;

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////Begin tHTMLCodeBlock//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	var tHTMLCodeBlock =
		'\n<br>\n' +

		'<div style="text-align:right; padding:2px;">' +
		//div=1

			//  <embedded image>
			//  data:uri resource, base64 encoded
			//  Greasemonkey statusbar icon
			//  to generate data:uri resources goto:
			//  http://software.hixie.ch/utilities/cgi/data/data
				'<img title="derGreaseMonkey.gif" alt="derGreaseMonkey.gif" style="vertical-align:text-bottom;" src="data:image/gif;base64,R0lGODlhEAAQAOYAAM6KjNaanO%2Byte%2B6vffDxr2anGtZWq2qraWipXt5e4SChHt9e5SWlISGhK2uraWmpb1RALVNAK1JAKVJAJRBAMZdEL1ZEIxJGIxdOb1NAKVFAJxBAJQ8AM5dELVVEJxNGJRRIYxNIb1pMaVlOa1tQq1JCKVNGJRJGKVVIZxRIZRNIYxVMbV5UoxpUoxtWpQ8CJxNIbVhMZxZMZRVMaV1Wr1hMZRdQr1pQs55UqVhQsZ1UtZ9Ws59Wr1xUs55WrVtUsZ1Wq1pUpRdSsaCa5SOjM6Ca855Y8Z9a86Gc86Cc715a96ajNaOhN6WjNaSjIx1c4RlY9aOjN6enM6mpd62teempf%2FHxv%2FPzgAAAP%2F%2F%2FwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAFkALAAAAAAQABAAQAewgFmCg4SFgi8eAVdYWIuNARUaGA2CQQNXV1U3VZgDOTSDSVYGjKWMBlZIQgiEGxqvEhIaM4QiEEeYmAS5V0MRGgpZUbzEmE4jD4IhFjxNAs9NOx0nT4ZZIBwcFCnWgj9LTDESNUxLPS6DLBJKvAC8ShMXWSQaQJiMjpg%2BGh%2BUHkWcilUx0mGFgywCqGApQGwKlikCVBzIIkOHlGJXpOCAAYVQCxQaIkTIUMKEDSKDAgEAOw%3D%3D">' +
			//  </embedded image>

			'&nbsp;' +
			'<input id="id_btnDisplay" name="nm_btnDisplay" type="button" style="font-family:arial narrow; font-size:small;" value="show html coder">' +
		'</div>' +
		//div=0

		'<div id="id_divEdit" name="nm_divEdit" style="display:none; background-color:#A2B2CF; color:#000077; text-align:center; border-style:solid; border-width:1px; border-color:#114477; padding:2px; font-family:arial narrow; font-size:small; ">' +
		//div=1


			'<div style="text-align:center; border-style:solid; border-width:1px; border-color:#000077; padding:2px; ">' +
			//div=2
				'Font:&nbsp;' +
				'<select name="nm_selFont" id="id_selFont">' +
					'<option></option>' +
					'<option style="font-family:Arial;">Arial</option>' +
					'<option style="font-family:Arial Black;">Arial Black</option>' +
					'<option style="font-family:Arial Narrow;">Arial Narrow</option>' +
					'<option style="font-family:Comic Sans MS;">Comic Sans MS </option>' +
					'<option style="font-family:Courier;">Courier</option>' +

					'<option style="font-family:Courier New;">Courier New</option>' +
					'<option style="font-family:Garamond;">Garamond</option>' +
					'<option style="font-family:Georgia;">Georgia</option>' +
					'<option style="font-family:Helvetica;">Helvetica</option>' +
					'<option style="font-family:Impact;">Impact</option>' +
					'<option style="font-family:Lucida Console;">Lucida Console</option>' +
					'<option style="font-family:Lucida Blackletter;">Lucida Blackletter</option>' +
					'<option style="font-family:Lucida Sans Unicode;">Lucida Sans Unicode</option>' +
					'<option style="font-family:MS Sans Serif;">MS Sans Serif</option>' +

					'<option style="font-family:MS Serif;">MS Serif</option>' +
					'<option style="font-family:Tahoma;">Tahoma</option>' +
					'<option style="font-family:Times New Roman;">Times New Roman</option>' +
					'<option style="font-family:Trebuchet MS;">Trebuchet MS</option>' +
					'<option style="font-family:Verdana;">Verdana</option>' +

					'<option style="font-family:Bookman;">Bookman</option>' +
				'</select>' +

				'&nbsp;Size:&nbsp;' +
				'<select name="nm_selSize" id="id_selSize" style="font-family: Arial;">' +
					'<option></option>' +
					'<option>0</option>' +
					'<option>1</option>' +
					'<option>2</option>' +
					'<option>3</option>' +
					'<option>4</option>' +
					'<option>5</option>' +
					'<option>6</option>' +
					'<option>7</option>' +
				'</select>' +

				'&nbsp;Color:&nbsp;' +
				'<select name="nm_selColor" id="id_selColor">' +
					'<option></option>' +
					'<option value="antiquewhite" style="color:antiquewhite;">antiquewhite</option>' +
					'<option value="aqua" style="color:aqua;">aqua</option>' +
					'<option value="aquamarine" style="color:aquamarine;">aquamarine</option>' +
					'<option value="azure" style="color:azure;">azure</option>' +
					'<option value="beige" style="color:beige;">beige</option>' +
					'<option value="bisque" style="color:bisque;">bisque</option>' +
					'<option value="black" style="color:black;">black</option>' +
					'<option value="blanchedalmond" style="color:blanchedalmond;">blanchedalmond</option>' +
					'<option value="blue" style="color:blue;">blue</option>' +
					'<option value="blueviolet" style="color:blueviolet;">blueviolet</option>' +
					'<option value="brown" style="color:brown;">brown</option>' +
					'<option value="burlywood" style="color:burlywood;">burlywood</option>' +
					'<option value="cadetblue" style="color:cadetblue;">cadetblue</option>' +
					'<option value="chartreuse" style="color:chartreuse;">chartreuse</option>' +
					'<option value="chocolate" style="color:chocolate;">chocolate</option>' +
					'<option value="coral" style="color:coral;">coral</option>' +
					'<option value="cornflowerblue" style="color:cornflowerblue;">cornflowerblue</option>' +
					'<option value="cornsilk" style="color:cornsilk;">cornsilk</option>' +
					'<option value="crimson" style="color:crimson;">crimson</option>' +
					'<option value="cyan" style="color:cyan;">cyan</option>' +
					'<option value="darkblue" style="color:darkblue;">darkblue</option>' +
					'<option value="darkcyan" style="color:darkcyan;">darkcyan</option>' +
					'<option value="darkgoldenrod" style="color:darkgoldenrod;">darkgoldenrod</option>' +
					'<option value="darkgray" style="color:darkgray;">darkgray</option>' +
					'<option value="darkgreen" style="color:darkgreen;">darkgreen</option>' +
					'<option value="darkkhaki" style="color:darkkhaki;">darkkhaki</option>' +
					'<option value="darkmagenta" style="color:darkmagenta;">darkmagenta</option>' +
					'<option value="darkolivegreen" style="color:darkolivegreen;">darkolivegreen</option>' +
					'<option value="darkorange" style="color:darkorange;">darkorange</option>' +
					'<option value="darkorchid" style="color:darkorchid;">darkorchid</option>' +
					'<option value="darkred" style="color:darkred;">darkred</option>' +
					'<option value="darksalmon" style="color:darksalmon;">darksalmon</option>' +
					'<option value="darkseagreen" style="color:darkseagreen;">darkseagreen</option>' +
					'<option value="darkslateblue" style="color:darkslateblue;">darkslateblue</option>' +
					'<option value="darkslategray" style="color:darkslategray;">darkslategray</option>' +
					'<option value="darkturquoise" style="color:darkturquoise;">darkturquoise</option>' +
					'<option value="darkviolet" style="color:darkviolet;">darkviolet</option>' +
					'<option value="deeppink" style="color:deeppink;">deeppink</option>' +
					'<option value="deepskyblue" style="color:deepskyblue;">deepskyblue</option>' +
					'<option value="dimgray" style="color:dimgray;">dimgray</option>' +
					'<option value="dodgerblue" style="color:dodgerblue;">dodgerblue</option>' +
					'<option value="firebrick" style="color:firebrick;">firebrick</option>' +
					'<option value="floralwhite" style="color:floralwhite;">floralwhite</option>' +
					'<option value="forestgreen" style="color:forestgreen;">forestgreen</option>' +
					'<option value="fuchsia" style="color:fuchsia;">fuchsia</option>' +
					'<option value="gainsboro" style="color:gainsboro;">gainsboro</option>' +
					'<option value="ghostwhite" style="color:ghostwhite;">ghostwhite</option>' +
					'<option value="gold" style="color:gold;">gold</option>' +
					'<option value="goldenrod" style="color:goldenrod;">goldenrod</option>' +
					'<option value="gray" style="color:gray;">gray</option>' +
					'<option value="green" style="color:green;">green</option>' +
					'<option value="greenyellow" style="color:greenyellow;">greenyellow</option>' +
					'<option value="honeydew" style="color:honeydew;">honeydew</option>' +
					'<option value="hotpink" style="color:hotpink;">hotpink</option>' +
					'<option value="indianred" style="color:indianred;">indianred</option>' +
					'<option value="indigo" style="color:indigo;">indigo</option>' +
					'<option value="ivory" style="color:ivory;">ivory</option>' +
					'<option value="khaki" style="color:khaki;">khaki</option>' +
					'<option value="lavender" style="color:lavender;">lavender</option>' +
					'<option value="lavenderblush" style="color:lavenderblush;">lavenderblush</option>' +
					'<option value="lemonchiffon" style="color:lemonchiffon;">lemonchiffon</option>' +
					'<option value="lightblue" style="color:lightblue;">lightblue</option>' +
					'<option value="lightcoral" style="color:lightcoral;">lightcoral</option>' +
					'<option value="lightcyan" style="color:lightcyan;">lightcyan</option>' +
					'<option value="lightgoldenrodyellow" style="color:lightgoldenrodyellow;">lightgoldenrodyellow</option>' +
					'<option value="lightgreen" style="color:lightgreen;">lightgreen</option>' +
					'<option value="lightgray" style="color:lightgray;">lightgray</option>' +
					'<option value="lightpink" style="color:lightpink;">lightpink</option>' +
					'<option value="lightsalmon" style="color:lightsalmon;">lightsalmon</option>' +
					'<option value="lightseagreen" style="color:lightseagreen;">lightseagreen</option>' +
					'<option value="lightskyblue" style="color:lightskyblue;">lightskyblue</option>' +
					'<option value="lightsteelblue" style="color:lightsteelblue;">lightsteelblue</option>' +
					'<option value="lightyellow" style="color:lightyellow;">lightyellow</option>' +
					'<option value="lime" style="color:lime;">lime</option>' +
					'<option value="limegreen" style="color:limegreen;">limegreen</option>' +
					'<option value="linen" style="color:linen;">linen</option>' +
					'<option value="magenta" style="color:magenta;">magenta</option>' +
					'<option value="maroon" style="color:maroon;">maroon</option>' +
					'<option value="mediumaquamarine" style="color:mediumaquamarine;">mediumaquamarine</option>' +
					'<option value="mediumblue" style="color:mediumblue;">mediumblue</option>' +
					'<option value="mediumorchid" style="color:mediumorchid;">mediumorchid</option>' +
					'<option value="mediumpurple" style="color:mediumpurple;">mediumpurple</option>' +
					'<option value="mediumseagreen" style="color:mediumseagreen;">mediumseagreen</option>' +
					'<option value="mediumslateblue" style="color:mediumslateblue;">mediumslateblue</option>' +
					'<option value="mediumspringblue" style="color:mediumspringblue;">mediumspringblue</option>' +
					'<option value="mediumturquoise" style="color:mediumturquoise;">mediumturquoise</option>' +
					'<option value="mediumvioletred" style="color:mediumvioletred;">mediumvioletred</option>' +
					'<option value="midnightblue" style="color:midnightblue;">midnightblue</option>' +
					'<option value="mintcream" style="color:mintcream;">mintcream</option>' +
					'<option value="mistyrose" style="color:mistyrose;">mistyrose</option>' +
					'<option value="navajowhite" style="color:navajowhite;">navajowhite</option>' +
					'<option value="navy" style="color:navy;">navy</option>' +
					'<option value="oldlace" style="color:oldlace;">oldlace</option>' +
					'<option value="olive" style="color:olive;">olive</option>' +
					'<option value="olivedrab" style="color:olivedrab;">olivedrab</option>' +
					'<option value="orange" style="color:orange;">orange</option>' +
					'<option value="orangered" style="color:orangered;">orangered</option>' +
					'<option value="orchid" style="color:orchid;">orchid</option>' +
					'<option value="palegoldenrod" style="color:palegoldenrod;">palegoldenrod</option>' +
					'<option value="palegreen" style="color:palegreen;">palegreen</option>' +
					'<option value="paleturquoise" style="color:paleturquoise;">paleturquoise</option>' +
					'<option value="palevioletred" style="color:palevioletred;">palevioletred</option>' +
					'<option value="papayawhip" style="color:papayawhip;">papayawhip</option>' +
					'<option value="peachpuff" style="color:peachpuff;">peachpuff</option>' +
					'<option value="peru" style="color:peru;">peru</option>' +
					'<option value="pink" style="color:pink;">pink</option>' +
					'<option value="plum" style="color:plum;">plum</option>' +
					'<option value="powderblue" style="color:powderblue;">powderblue</option>' +
					'<option value="purple" style="color:purple;">purple</option>' +
					'<option value="red" style="color:red;">red</option>' +
					'<option value="rosybrown" style="color:rosybrown;">rosybrown</option>' +
					'<option value="royalblue" style="color:royalblue;">royalblue</option>' +
					'<option value="saddlebrown" style="color:saddlebrown;">saddlebrown</option>' +
					'<option value="salmon" style="color:salmon;">salmon</option>' +
					'<option value="sandybrown" style="color:sandybrown;">sandybrown</option>' +
					'<option value="seagreen" style="color:seagreen;">seagreen</option>' +
					'<option value="seashell" style="color:seashell;">seashell</option>' +
					'<option value="sienna" style="color:sienna;">sienna</option>' +
					'<option value="silver" style="color:silver;">silver</option>' +
					'<option value="skyblue" style="color:skyblue;">skyblue</option>' +
					'<option value="slateblue" style="color:slateblue;">slateblue</option>' +
					'<option value="slategray" style="color:slategray;">slategray</option>' +
					'<option value="snow" style="color:snow;">snow</option>' +
					'<option value="springgreen" style="color:springgreen;">springgreen</option>' +
					'<option value="steelblue" style="color:steelblue;">steelblue</option>' +
					'<option value="tan" style="color:tan;">tan</option>' +
					'<option value="teal" style="color:teal;">teal</option>' +
					'<option value="thistle" style="color:thistle;">thistle</option>' +
					'<option value="tomato" style="color:tomato;">tomato</option>' +
					'<option value="turquoise" style="color:turquoise;">turquoise</option>' +
					'<option value="violet" style="color:violet;">violet</option>' +
					'<option value="wheat" style="color:wheat;">wheat</option>' +
					'<option value="white" style="color:white;">white</option>' +
					'<option value="whitesmoke" style="color:whitesmoke;">whitesmoke</option>' +
					'<option value="yellow" style="color:yellow;">yellow</option>' +
					'<option value="yellowgreen" style="color:yellowgreen;">yellowgreen</option>' +

				'</select>&nbsp;&nbsp;' +

				'<button id="id_btnFont" name="nm_btnFont" type="button">Insert Font</button>' +
			'</div>' +
			//div=1


			'<div style="text-align:center; border-style:solid; border-width:1px; border-color:#000077; padding:2px; font-family:arial narrow; font-size:small; ">' +
			//div=2
				'<button id="id_btnCenter" name="nm_btnCenter" type="button">center</button>&nbsp;' +
				'<button id="id_btnBold" name="nm_btnBold" type="button"><b>bold</b></button>&nbsp;' +
				'<button id="id_btnItalic" name="nm_btnItalic" type="button"><i>italic</i></button>&nbsp;&nbsp;&nbsp;&nbsp;' +
				'<button id="id_btnScrubHTML" name="nm_btnScrubHTML" type="button"><del>&lt;HTML&gt;</del></button>&nbsp;&nbsp;&nbsp;' +
				'<select name="nm_selHTML" id="id_selHTML">' +
					'<option></option>' +
					'<option value="<del>|</del>">strikethrough</option>' +
					'<option value="<u>|</u>">underline</option>' +
					'<option value="<em>|</em>">emphasis</option>' +
					'<option value="<ul>|</ul>">unordered list</option>' +
					'<option value="<ol>|</ol>">ordered list</option>' +
					'<option value="<li>">list item</option>' +
				'</select>&nbsp;' +
				'<button id="id_btnMiscHTML" name="nm_btnMiscHTML" type="button">Insert</button>&nbsp;' +
				//'<button id="id_btnStrike" name="nm_btnStrike" type="button"><del>strike</del></button>&nbsp;' +
				//'<button id="id_btnUnderline" name="nm_btnUnderline" type="button"><u>underline</u></button>' +
			'</div>' +
			//div=1


			'<div style="text-align:center; border-style:solid; border-width:1px; border-color:#000077; padding:2px; font-family:arial narrow; font-size:small; ">' +
			//div=2
				'URL:&nbsp;<input id="id_txtUserInput" name="nm_txtUserInput" size="40" type="text" value="">&nbsp;' +
				'<button id="id_btnLink" name="nm_btnLink" type="button">Link</button>&nbsp;' +
				'<button id="id_btnImage" name="nm_btnImage" type="button">Image</button><br>' +
				'link: new tab&nbsp;<input type="checkbox" name="nm_chbxTargetNew" id="id_chbxTargetNew">&nbsp;image: border' +
			'</div>' +
			//div=1

			//begin new code (template)
			'<div style="text-align:center; border-style:solid; border-width:1px; border-color:#000077; padding:2px; font-family:arial narrow; font-size:small; ">' +
			//div=2

				'<div style="text-align:right; vertical-align:top;"><input id="id_btnTemplate" name="nm_btnTemplate" type="button" style="font-family:arial narrow; font-size:small;" value="edit template"></div>' +
				'<div id="id_divTemplate" name="nm_divTemplate" style="display:none;">' +
				//div=3
					'<div>' +
						'<textarea rows="5" cols="50" id="id_txtTemplate" name="nm_txtTemplate"></textarea>' +
					'</div>' +
					'<div style="font-family:arial narrow; font-size:small;">' +
						'<span><input type="checkbox" name="nm_chbxBlog" id="id_chbxBlog">&nbsp;blog</span>' +
						'<span style="display:none; "><input type="checkbox" name="nm_chbxForum" id="id_chbxForum">&nbsp;forum</span>' +
						'<span><input type="checkbox" name="nm_chbxReview" id="id_chbxReview">&nbsp;review</span>' +
					'</div> ' +
				'</div>' +
				//div=2
			'</div>' +
			//div=1

			//end new code

		'</div>' ;
		//div=0
	////////////////////////////////End tHTMLCodeBlock/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////
	//Functions, Subroutines, Events


	//var target_form;
	//var target_textarea;

	function initialize()
	{
		//elements
		var el_forms;
		var el_form;
		var el_texts;
		var el_text;
		var el_trs;
		var el_tr;
		var el_tds;
		var el_td;
		var el;

		var target_tr;
		var target_td;

		//counters
		var i,j,k;
		var x,y,z;

		//target_form;
		//target_textarea;
		//el_forms=document.forms;
		//el_trs = document.getElementsByTagName("tr");
		//for(i=0;i<el_trs.length;i++)

		if(document.getElementsByTagName("textarea").length>1)return;
		el_tds = document.getElementsByTagName("td");
		for(i=0;i<el_tds.length;i++)
		{
			//el_tr=el_trs[i];
			//el_forms = document.getElementsByTagName("form");
			//el_forms = el_tr.getElementsByTagName("form");

			dbg++;
			el_td=el_tds[i];
			el_texts=el_td.getElementsByTagName("textarea");
			for(j=0;j<el_texts.length;j++)
			{
				dbg++;
				el_text=el_texts[j];
				if(el_text.rows>=3)
				{
					dbg++;
					if(el_text.rows<10) el_text.rows = 10;
					target_textarea=el_text;
					target_form=el_form;
					//target_tr=el_tr;
					target_td=el_td;
					break;
				}
			}


			// removed this code chunk to provide functionality in forums :]
			/*
			el_forms = el_td.getElementsByTagName("form");
			for(j=0;j<el_forms.length;j++)
			{
				//el_texts=el_form.getElementsByTagName("textarea");
				//el_texts=document.getElementsByTagName("textarea");

				dbg++;
				el_form=el_forms[j];
				el_texts=el_form.getElementsByTagName("textarea");
				for(k=0;k<el_texts.length;k++)
				{
					dbg++;
					el_text=el_texts[k];
					if(el_text.rows>=3)
					{
						dbg++;
						target_textarea=el_text;
						target_form=el_form;
						//target_tr=el_tr;
						target_td=el_td;
						break;
					}
				}
			}
			*/
		}

		//if(target_form!=null && target_textarea!=null && target_tr!=null)
		//if(target_form!=null && target_textarea!=null && target_td!=null)
		if(target_textarea!=null && target_td!=null)
		{
					//el_tr=document.createElement("tr");
					//target_tr.parentNode.insertBefore(el_tr, target_tr.nextSibling);
					//el_td=document.createElement("td");
					//el_tr.appendChild(el_td);
					//el_td.innerHTML = tHTMLCodeBlock;
					//tHTMLCodeBlock="";
					//target_td.innerHTML +=  tHTMLCodeBlock;
					//tHTMLCodeBlock="";

					//var el;

					//el=document.getElementsByName("nm_btnFont")[0];

					//avoid doing .innerHTML operations on native SU elements; it'll break things.
					//create new element instead.  and append or insert.
					//el.style.visibility="collapse";
					//el.style.display="none";
					//target_td.style.display="compact";

			el=document.createElement("div");
			el.innerHTML=tHTMLCodeBlock;
			target_td.appendChild(el);

			tHTMLCodeBlock=null;


			el=document.getElementById("id_btnFont");
			el.addEventListener("click", btnFontClick, true);

			//el=document.getElementsByName("nm_btnBold")[0];
			el=document.getElementById("id_btnBold");
			el.addEventListener("click", btnBoldClick, true);

			//el=document.getElementsByName("nm_btnItalic")[0];
			el=document.getElementById("id_btnItalic");
			el.addEventListener("click", btnItalicClick, true);

			//el=document.getElementsByName("nm_btnStrike")[0];
			//el=document.getElementById("id_btnStrike");
			//el.addEventListener("click", btnStrikeClick, true);

			//el=document.getElementsByName("nm_btnUnderline")[0];
			//el=document.getElementById("id_btnUnderline");
			//el.addEventListener("click", btnUnderlineClick, true);

			//el=document.getElementsByName("nm_btnCenter")[0];
			el=document.getElementById("id_btnCenter");
			el.addEventListener("click", btnCenterClick, true);

			//el=document.getElementsByName("nm_btnLink")[0];
			el=document.getElementById("id_btnLink");
			el.addEventListener("click", btnLinkClick, true);

			//el=document.getElementsByName("nm_btnImage")[0];
			el=document.getElementById("id_btnImage");
			el.addEventListener("click", btnImageClick, true);

			//el=document.getElementsByName("nm_txtUserInput")[0];
			el=document.getElementById("id_txtUserInput");
			el.addEventListener("click", txtUserInputClick, true);
			txtUserInput=el;

			el=document.getElementById("id_chbxTargetNew");
			chbxTargetNew=el;

			el=document.getElementById("id_selFont");
			el.addEventListener("change", selFontChange, true);
			selFont=el;
			el=document.getElementById("id_selSize");
			el.addEventListener("change", selSizeChange, true);
			selSize=el;
			el=document.getElementById("id_selColor");
			el.addEventListener("change", selColorChange, true);
			selColor=el;

			el=document.getElementById("id_divEdit");
			divEdit=el;
			divEdit.style.display="none";  //redundant


			//divEdit.style.display="none";
			//divEdit.style.visibility = "hidden";
			//divEdit.style.visibility = "collapse";
			//divEdit.style.display=""

			el=document.getElementById("id_btnDisplay");
			el.addEventListener("click", btnDisplayClick, true);
			btnDisplay=el;
			btnDisplay.value="show html coder";

			//newcode
			//add event + variable for each.
			el=document.getElementById("id_selHTML");
			el.addEventListener("change", selHTMLChange, true);
			selHTML=el;

			el=document.getElementById("id_btnMiscHTML");
			el.addEventListener("click", btnMiscHTMLClick, true);
			btnMiscHTML=el;

			el=document.getElementById("id_btnTemplate");
			el.addEventListener("click", btnTemplateChange, true);
			btnTemplate=el;

			el=document.getElementById("id_divTemplate");
			divTemplate=el;
			divTemplate.style.display="none";  //redundant

			el=document.getElementById("id_txtTemplate");
			txtTemplate=el;

			el=document.getElementById("id_btnScrubHTML");
			el.addEventListener("click", btnScrubHTMLClick, true);
			btnScrubHTML=el;


			el=document.getElementById("id_chbxBlog");
			chbxBlog=el;

			/*
			el=document.getElementById("id_chbxForum");
			chbxForum=el;
			*/

			el=document.getElementById("id_chbxReview");
			chbxReview=el;


			Template_Restore();
			//dbg="initialize okay";

			dbg="100000"
			//GM_log("initialized");
		}
				//GM_log("\ndbg = " + dbg + "\ndlength = " + tHTMLCodeBlock.length);
				//DebugOut(dbg);
				//alert(dbg + "\n");
				//alert(document.getElementsByTagName("form").length);
	}

				/*
				function DebugOut(debugparam)
				{
					var el;
					el = document.getElementById("div_debugout");
					if(el!=null)
					{
						if(debugparam!=null)
						{
							el.innerHTML+=debugparam.toString();
						}
						else
						{
							el.innerHTML="";
						}
					}
				}
				*/

	function btnDisplayClick()
	{
		//divEdit.style.display==!divEdit.style.display;
		//DebugOut(divEdit.style.display);
		//DebugOut="hello";
		//var oldvalue=divEdit.style.display;
		//if(divEdit.style.display=="none")
		/*if(oldvalue=="hidden")
		{

			//divEdit.style.display=="";
			newvalue="visible";
			//divEdit.style.display=newvalue;
		}
		else if(oldvalue=="visible")
		{
			//divEdit.style.display="none"
			newvalue="hidden";
			//divEdit.style.display=newvalue;
		}
		else
		{
			//divEdit.style.display="none"
			newvalue="hidden";
			//divEdit.style.display=newvalue;
		}*/
		//divEdit.style.display=newvalue;
		//GM_log("\nvisibility = " + newvalue)
		//DebugOut("from: " + oldvalue + "; to: " + newvalue);

		var oldvalue;
		var newvalue;
		//oldvalue = divEdit.style.visibility;
		oldvalue = divEdit.style.display;
		//if(oldvalue!="collapse")
		if(oldvalue!="none")
		{
			//newvalue="collapse";
			newvalue="none";
			btnDisplay.value="show html coder";
		}
		else
		{
			//newvalue="visible";
			newvalue="block";
			btnDisplay.value="hide html coder";
		}
		//divEdit.style.visibility=newvalue;
		divEdit.style.display=newvalue;


		//testing
		//this.focus();
		//trying to update firefox display to properly collapse div / margin when the html coder is located at the bottom of the screen...  pages jumps around a bit as firefox properly re-renders page after collapsed div.  bah
		//target_textarea.focus();
		this.focus();

	}

	//function InsertCode(obj_ta, tcode)
	function GrabSelection(obj_ta)
	{
		var ss = obj_ta.selectionStart;
		var es = obj_ta.selectionEnd;
		return (obj_ta.value).substring(ss, es);
	}

	function InsertCode(obj_ta, ibTag, ibClsTag, ibOverwriteSelection)
	{
		// code borrowed from bulletin board software :)
		var isClose = false;

		var ss = obj_ta.selectionStart;
		var st = obj_ta.scrollTop;
		var es = obj_ta.selectionEnd;

		if (es <= 2)
		{
			es = obj_ta.textLength;
		}

		var start  = (obj_ta.value).substring(0, ss);
		var middle = (obj_ta.value).substring(ss, es);
		var end    = (obj_ta.value).substring(es, obj_ta.textLength);

	  var insertion;
		//-----------------------------------
		// text range?
		//-----------------------------------

		/*
		if (obj_ta.selectionEnd - obj_ta.selectionStart > 0)
		{
			insertion = ibTag + middle + ibClsTag;
			//middle = tcode + middle;
		}
		else
		{
			insertion = ibTag + middle + ibClsTag;
			//middle = tcode + middle;

			//if (isSingle)
			//{
			//	isClose = true;
			//}
		}
		*/
		if(ibTag==null)ibTag="";
		if(ibClsTag==null)ibClsTag="";
		if(ibOverwriteSelection!=null) middle=ibOverwriteSelection;  //jcfix
		insertion = ibTag + middle + ibClsTag;

		obj_ta.value = start + insertion + end;

		//var cpos = ss + (middle.length);

		//var cpos = ss + (ibTag.length);  //previous
		if(middle.length==0)
		{
		var cpos = ss + (ibTag.length) + (middle.length);
		}
		else
		{
			var cpos = ss + (ibTag.length) + (middle.length);
		}

		obj_ta.selectionStart = cpos;
		obj_ta.selectionEnd   = cpos;
		obj_ta.scrollTop      = st;
	}

	function btnFontClick()
	{

		//var tText0=document.forms.frmMain.txtURL.value;
		//var tText=tText0;

		var tText  = "";
		var tText0 = "";
		var tText1 = "";
		var tTemp  = "";
		var tFontAttr = "";

		var rex_ignorefilter= /^[\-\s]*$/i;	//ignore dashes/whitespace
		//var objElement;
		//var ff = document.forms.frmMain;

		//objElement=ff.selFont;
		//tTemp=objElement.options[objElement.selectedIndex].value;
		tTemp=selFont.options[selFont.selectedIndex].value;
		if(tTemp.length>0 && !rex_ignorefilter.test(tTemp))
		{
			tFontAttr+=" face=\""+tTemp+"\"";
		}
		//objElement=ff.selSize;
		//tTemp=objElement.options[objElement.selectedIndex].value;
		tTemp=selSize.options[selSize.selectedIndex].value;
		if(tTemp.length>0 && !rex_ignorefilter.test(tTemp))
		{
			tFontAttr+=" size=\""+tTemp+"\"";
		}
		//objElement=ff.selColor;
		//tTemp=objElement.options[objElement.selectedIndex].value;
		tTemp=selColor.options[selColor.selectedIndex].value;
		if(tTemp.length>0 && !rex_ignorefilter.test(tTemp))
		{
			tFontAttr+=" color=\""+tTemp+"\"";
		}
		if(tFontAttr.length>0)
		{
			  //tText0 = "<font"+tFontAttr+">\n";
			  //tText1 = "\n</font>";
			  tText0 = "<font"+tFontAttr+">";
			  tText1 = "</font>";

		}

		InsertCode(target_textarea, tText0, tText1);
		target_textarea.focus();

	}

	function btnLinkClick()
	{
		//el=document.getElementsByName("nm_txtUserInput")[0];
		//el=document.getElementById("id_txtUserInput");
		//el.addEventListener("click", txtUserInputClick, true);

		var targetattr="";
		var objElement;
		//var ff = document.forms.frmMain;
		var pregexp=/^https?:\/\/.+$/i;
		var turl="";
		var tsel="";
		//if(pregexp.test(ff.txtUserInput.value)) turl=ff.txtUserInput.value;


		//objElement=ff.chbxTargetNew;
		//if(objElement.checked) targetattr=" target=\"_blank\"";
		if(chbxTargetNew.checked) targetattr=" target=\"_blank\"";

		turl=txtUserInput.value;
		tsel=GrabSelection(target_textarea);
		if(turl.length==0)
		{
			if(pregexp.test(tsel))
			{
				turl=tsel;
				//txtUserInput.value=tsel;
				tsel="";
			}
		}
		if(turl.length>0)
		{
			var tText0 = "<a href=\"" + turl + "\"" + targetattr + ">" + tsel;
			var tText1 = "</a>";
		}
		else
		{
			var tText0 = "<a href=\"";
			var tText1 = "\"" + targetattr + ">" + tsel + "</a>";
			//ff.txtUserInput.style.backgroundColor="red";
		}
		if(!pregexp.test(txtUserInput.value))
		{
			txtUserInput.style.backgroundColor="red";
		}
		InsertCode(target_textarea, tText0, tText1, "");
		target_textarea.focus();
	}

	function btnImageClick()
	{
		var borderattr="";
		var objElement;
		//var ff = document.forms.frmMain;
		var pregexp=/^https?:\/\/.+$/i;
		var turl="";
		var tsel="";
		//if(pregexp.test(ff.txtUserInput.value)) turl=ff.txtUserInput.value;


		//objElement=ff.chbxTargetNew;
		//if(objElement.checked) borderattr=" border=\"1\"";
		if(chbxTargetNew.checked) borderattr=" border=\"1\"";

		turl=txtUserInput.value;
		tsel=GrabSelection(target_textarea);
		if(turl.length==0)
		{
			if(pregexp.test(tsel))
			{
				turl=tsel;
				//txtUserInput.value=tsel;
				tsel="";
			}
		}
		if(turl.length>0)
		{
			var tText0 = tsel + "<img src=\"" + turl + "\"" + borderattr + ">";
			var tText1 = "";
		}
		else
		{
			var tText0 = tsel + "<img src=\"";
			var tText1 = "\"" + borderattr + ">";
			//ff.txtUserInput.style.backgroundColor="red";

		}
		if(!pregexp.test(txtUserInput.value))
		{
			txtUserInput.style.backgroundColor="red";
		}
		InsertCode(target_textarea, tText0, tText1, "");
		target_textarea.focus();
	}

	function btnBoldClick()
	{
		//var ff = document.forms.frmMain;
		InsertCode(target_textarea, "<b>", "</b>");
		target_textarea.focus();
	}

	function btnItalicClick()
	{
		//var ff = document.forms.frmMain;
		InsertCode(target_textarea, "<i>", "</i>");
		target_textarea.focus();
	}

	/*
	function btnStrikeClick()
	{
		//var ff = document.forms.frmMain;
		InsertCode(target_textarea, "<del>", "</del>");
		target_textarea.focus();
	}

	function btnUnderlineClick()
	{
		//var ff = document.forms.frmMain;
		InsertCode(target_textarea, "<u>", "</u>");
		target_textarea.focus();
	}
	*/

	function btnCenterClick()
	{
		//var ff = document.forms.frmMain;
		//InsertCode(target_textarea, "<center>\n", "\n</center>");
		InsertCode(target_textarea, "<center>", "</center>");
		target_textarea.focus();
	}

	function txtUserInputClick()
	{
		//var ff = document.forms.frmMain;
		//ff.txtUserInput.style.backgroundColor="";
		txtUserInput.style.backgroundColor="";
	//InsertCode(target_textarea, "<center>\n", "\n</center>");
	//target_textarea.focus();
	}

	function selFontChange()
	{
		//var el;
		//el=selFont.options[selFont.selectedIndex];
		//el.style??
	}
	function selSizeChange()
	{

	}
	function selColorChange()
	{

	}

	//newcode

	function selHTMLChange()
	{

	}

	function btnMiscHTMLClick()
	{
		var tText="";
		var tText0="";
		var tText1="";
		//var tTemp="";
		//var tSplit;
		//var tTemp0="";
		//var tTemp1="";
		var tValue="";
		var nValue=0;
		var el;
		var i, j;
		var rex=/^(<[^>]*>)([^\|]*\|[^\|]*)(<\/[^>]*>)$/i;

		tValue=selHTML.options[selHTML.selectedIndex].value;
		nValue=selHTML.selectedIndex;
		if(tValue.length>0)
		{
			if(rex.exec(tValue)!=null)
			{
				tText0=RegExp.$1;
				//tTemp=RegExp.$2;
				tText1=RegExp.$3;
				//tSplit=tTemp.split(/\|/i);
				//if(tSplit.length==2)
				//{
					//tText0=tText0 + tSplit[0].replace(/n/i, "\n");
					//tText1=tText1 + tSplit[1].replace(/n/i, "\n");
					InsertCode(target_textarea, tText0, tText1);

					if(/^<(ul|ol)>.*<\/(ul|ol)>/i.test(tValue))
					{
						for(i=0;i<selHTML.options.length;i++)
						{
							if(/<li>/i.test(selHTML.options[i].value))
							{
								//selFont.selectedIndex=i;
								selHTML.options[i].selected=true;
								//alert(i);
								break;
							}
						}
					}
					target_textarea.focus();
				//}
			}
			else
			{
				//tText0="";
				//tText1=tValue;
				tText0=tValue;
				tText1="";
				InsertCode(target_textarea, tText0, tText1);
				target_textarea.focus();
			}
		}
		//GM_log(nValue + ":" + tValue);
	}

	function btnTemplateChange()
	{
		var oldvalue;
		var newvalue;
		oldvalue = divTemplate.style.display;
		if(oldvalue!="none")
		{
			newvalue="none";
			btnTemplate.value="edit template";
			btnTemplate.style.color="";
		}
		else
		{
			newvalue="block";
			btnTemplate.value="save template";
			btnTemplate.style.color="red";
		}
		if(oldvalue=="block" && newvalue=="none")
		{
			Template_Store();
		}
		divTemplate.style.display=newvalue;
		//target_textarea.focus();
		this.focus();
	}

	function Template_Store()
	{
		//tParam_TemplateStore_Main
		//GM_setValue("foo", "bar");

		//		var tStoreParam_Template_Content="template_content";
		//		var tStoreParam_Template_Apply="template_apply";
		var a="";
		var b="";
		if(txtTemplate.value.length<=8000 && txtTemplate.value.length>0)
		{
			b=txtTemplate.value;
		}
		else
		{
			b="";	//redundant;
		}
		a=btoa(b);
		GM_setValue(tStoreParam_Template_Content,a);

		var loc_arr=new Array("0","0");
		if(chbxBlog.checked)loc_arr[0]="1";
		if(chbxReview.checked)loc_arr[1]="1";

		GM_setValue(tStoreParam_Template_Apply, loc_arr.join("|"));

		/*
		var loc_arr=new Array("0","0","0");
		if(chbxBlog.checked)loc_arr[0]="1";
		if(chbxForum.checked)loc_arr[1]="1";
		if(chbxReview.checked)loc_arr[2]="1";

		GM_setValue(tStoreParam_Template_Apply, loc_arr.join("|"));
		*/

	}

	function Template_Restore()
	{
		//tParam_TemplateStore_Main
		//GM_getValue(name, default)
		var a="";
		var b="";
		try{
			a=GM_getValue(tStoreParam_Template_Content,"");
			b=atob(a);
		}
		catch(e)
		{
			GM_setValue(tStoreParam_Template_Content,"");
			b=""; //redundant;
		}
		//atob:  Mozilla built-in function that decodes a to original data b.


					/*
					if(b.length<=8000 && b.length>0)
					{
						txtTemplate.value=b;
					}
					if(target_textarea.value.length==0)
					{
						target_textarea.value=txtTemplate.value;
					}
					*/

		if(b.length<=8000 && b.length>0)
		{
			txtTemplate.value=b;
			var t="";
			var loc_arr=null;
			try
			{
				t=GM_getValue(tStoreParam_Template_Apply,"");
				if(t.length>0)
				{
					loc_arr=t.split("|");
				}
			}
			catch(e)
			{
				t=GM_setValue(tStoreParam_Template_Apply,"");
				loc_arr=null;
			}
			if(loc_arr!=null)
			{
				if(loc_arr.length==2)
				{
					if(loc_arr[0]=="1") chbxBlog.checked=true;
					if(loc_arr[1]=="1") chbxReview.checked=true;
					var tlocation=StumbleuponSection(tDocURL);
					if(target_textarea.value.length==0 && tlocation!=null)
					{
						if((tlocation=="blog" && chbxBlog.checked) || (tlocation=="urlreview" && chbxReview.checked))
						{
							target_textarea.value=txtTemplate.value;
						}
					}
				}
			}

						/*
						var t="";
						var loc_arr=null;
						try
						{
							t=GM_getValue(tStoreParam_Template_Apply,"");
							if(t.length>0)
							{
								loc_arr=t.split("|");
							}
						}
						catch(e)
						{
							t=GM_setValue(tStoreParam_Template_Apply,"");
							loc_arr=null;
						}
						if(loc_arr!=null)
						{
							if(loc_arr.length==3)
							{
								if(loc_arr[0]=="1") chbxBlog.checked=true;
								if(loc_arr[1]=="1") chbxForum.checked=true;
								if(loc_arr[2]=="1") chbxReview.checked=true;
								var tlocation=StumbleuponSection(tDocURL);
								if(target_textarea.value.length==0 && tlocation!=null)
								{
									if((tlocation=="blog" && chbxBlog.checked) || (tlocation=="forum" && chbxForum.checked) || (tlocation=="urlreview" && chbxReview.checked))
									{

										target_textarea.value=txtTemplate.value;
									}
								}
							}
						}
						*/
		}
	}

	function StumbleuponSection(turl)
	{
		var pre_urlreview=/^http:\/\/www\.stumbleupon\.com\/url(\/.*)?$/i;
		var pre_forum=/^http:\/\/[^\.\/]+\.group\.stumbleupon\.com\/forum(\/.*)?$/i;
		var pre_contact=/^http:\/\/[^\.\/]+\.stumbleupon\.com\/contact(\/.*)?$/i;
		var pre_blog=/^http:\/\/[^\.\/]+\.stumbleupon\.com(\/.*)?$/i;
		var rval=null;
		if(pre_urlreview.test(turl)) rval="urlreview";
		else if(pre_forum.test(turl)) rval="forum";
		else if(pre_contact.test(turl)) rval="contact"
		else if(pre_blog.test(turl)) rval="blog";
		return rval;
	}

	function btnScrubHTMLClick()
	{

		var tsel=GrabSelection(target_textarea);
		var tscrubbed;
		if(tsel.length>0)
		{
			tscrubbed=scrubHTML(tsel);
			InsertCode(target_textarea, "","", tscrubbed);
		}
	}

	function scrubHTML(tHTML)
	{

		/*
		t=t.replace(/\&/ig, "&amp;");
		t=t.replace(/</ig, "&lt;");
		t=t.replace(/>/ig, "&gt;");
		t=t.replace(/http\:\/\//ig, "http&#58;//");
		t=t.replace(/\t/ig, "&nbsp;&nbsp;&nbsp;&nbsp;");
		*/

		var t=tHTML;

		t=t.replace(/\&/ig, "&amp;");
		t=t.replace(/</ig, "&lt;");
		t=t.replace(/>/ig, "&gt;");
		t=t.replace(/http\:\/\//ig, "http&#58;//");
		t=t.replace(/http\:\/\/www\./ig, "http://www&#46;");
		t=t.replace(/www\./ig, "www&#46;");
		t=t.replace(/\t/ig, "&nbsp;&nbsp;&nbsp;&nbsp;");

		return t;
	}





	///////////////////////////
	//Start Code Execution Here


	tDocURL=document.URL;
	dbg++;
	//tDocURL=tDocURL.replace(/#.*$/i,"");
	var rex=/^http:\/\/[^\?\/]+\.stumbleupon\.com\/.*$/i;  	//regular expression pattern



	//Double-check that we're at stumbleupon.com  and not something like http://www.someone-else.com/www.stumbleupon.com/etc

	/*
	//DISABLED - just for testing local file
	//var rex_test=/^file:\/\/\/.*SU_LOCALTESTPAGE_.*\.htm$/;
	//if(rex.test(tDocURL)==true || rex_test.test(tDocURL)==true)
	*/

	if(rex.test(tDocURL)==true)
	{
		dbg++;
		rex=/^http:\/\/[^\?\/]+\.stumbleupon\.com\/prefs(\/.*)?$/i;
		if(rex.test(tDocURL)==false)
		{
			dbg++;
			initialize();
		}
	}
	//GM_log("\ndbg = " + dbg);
})();






/*

// some greasemonkey documentation for built-in functions.

GM_setValue(name, value)

Allows user script authors to persist simple values locally. Strings, booleans, and integers are the only allowed datatypes. For example:

GM_setValue("foo", "bar");

GM_getValue(name, default)

Retrieve a value set with setValue. If the value is not found, default is returned instead. If default is not supplied, undefined is returned.

alert(GM_getValue("foo"));

GM_log(message, level)

Allows script authors simple access to logging informational messages in the JS Console. This can be helpful for debugging. Level is optional and defaults to 0. Valid values are: 0 - info, 1 - warning, 2 - error.

GM_log("Hello, World!")

*/

/*

//resource for generating data:uri

http://software.hixie.ch/utilities/cgi/data/data

*/

