/*SuperTextbox v3.1*/
function SuperTextBox(name,value,width,height) {
	this.name=name;
	this.value=value||"";
	this.is_alive=true;
	this.edit_mode=true;
	this.enable_help=true;
	this.image_dir="plugins/images/";
	this.editregion;
	this.coderegion;
	this.menuregion;
	this.textarea;
	this.codearea;
	this.popup;
	this.dispatch=new Object();
	this.font=new Object();
	this.colors=new Object();
	this.style=new Object();
	with(this) {           //createCmd (0 = image on 1 = image off, imagename)
		dispatch.RemoveFormat=createCmd(0,"delete.gif");
			dispatch.RemoveFormat.helptip="Remove formatting information<br>ie. forecolor, background color,<br>fontsize, etc";
		dispatch.CreateLink=createCmd(0,"link.gif");
			dispatch.CreateLink.helptip="Create a URL Link...";
		dispatch.Unlink=createCmd(0,"unlink.gif");
			dispatch.Unlink.helptip="Remove URL Link";
		dispatch.SuperScript=createCmd(0,"superscript.gif");
		dispatch.SubScript=createCmd(0,"subscript.gif");
		dispatch.JustifyLeft=createCmd(0,"justifyleft.gif");
		dispatch.JustifyCenter=createCmd(0,"justifycenter.gif");
		dispatch.JustifyRight=createCmd(0,"justifyright.gif");
		dispatch.Indent=createCmd(0,"indent.gif");
		dispatch.Outdent=createCmd(0,"outdent.gif");
		dispatch.InsertOrderedList=createCmd(0,"orderedlist.gif");
			dispatch.InsertOrderedList.helptip="Create an ordered list:<br> 1. ListItem 0<br> 2. ListItem 1<br> 3. ListItem 2";
		dispatch.InsertUnorderedList=createCmd(0,"unorderedlist.gif");
			dispatch.InsertUnorderedList.helptip="Create an unordered list:<br> &#8226; ListItem 0<br> &#8226; ListItem 1<br> &#8226; ListItem 2";
		dispatch.Cut=createCmd(0,"cut.gif");
			dispatch.Cut.status=1;
		dispatch.Copy=createCmd(0,"copy.gif");
			dispatch.Copy.status=1;
		dispatch.Paste=createCmd(0,"paste.gif");
			dispatch.Paste.status=1;
		dispatch.Bold=createCmd(0,"bold.gif");
		dispatch.Italic=createCmd(0,"italic.gif");
		dispatch.Underline=createCmd(0,"underline.gif");
		dispatch.FontName=createCmd(0,"fontfamily.gif",createFontMenu);
			dispatch.FontName.helptip="Font style...";
		dispatch.FontSize=createCmd(0,"fontsize.gif",createSizeMenu);
			dispatch.FontSize.helptip="Font size...";
		dispatch.ForeColor=createCmd(0,"forecolor.gif",createColorGrid);
			dispatch.ForeColor.helptip="Font color...";
		dispatch.BackColor=createCmd(0,"fillcolor.gif",createColorGrid);
			dispatch.BackColor.helptip="Background color...";
		dispatch.InsertImage=createCmd(0,"image.gif");
		dispatch.OptionMenu=createCmd(0,"options.gif",createOptionMenu);
		dispatch.OptionMenu.helptip="Additional options...";
		for(cmd in this.dispatch) this.dispatch[cmd].name=cmd;
		
		font.size=new Object();
		font.size.MIN=1;
		font.size.MAX=7;
		font.face=new Object();
		font.face.Arial=1;
		font.face.Arial_Black=1;
		font.face.Comic_Sans_MS=1;
		font.face.Courier=1;
		font.face.Georgia=1;
		font.face.Impact=1;
		font.face.Lucida_Console=1;
		font.face.Modern=1;
		font.face.MS_Sans_Serif=1;
		font.face.MS_Serif=1;
		font.face.Palatino_Linotype=1;
		font.face.Roman=1;
		font.face.Script=1;
		font.face.Small_Fonts=1;
		font.face.Tahoma=1;
		font.face.Times_New_Roman=1;
		font.face.Trebuchet_MS=1;
		font.face.Verdana=1;
	
		style.editbox="font:normal 10pt Arial;";
		style.codebox="font:normal 10pt Arial;";
		style.width=width||"100%";
		style.height=height||150;
	}
}
SuperTextBox.prototype.create=function(html_obj) {
	var tb=document.createElement("table");
	var tx=document.createElement("textarea");
	var dv=document.createElement("div");
	tx.style.width=dv.style.width="100%";
	tx.style.height=dv.style.height="100%";
	tx.style.border=dv.style.border="2px inset";
	tx.style.overflowY=dv.style.overflowY="scroll";
	tx.style.overflowX=dv.style.overflowX="auto";
	tx.style.padding=dv.style.padding=3;
	tx.name=this.name;
	tx.value=this.value;
	tx.style.display="none";
	this.textarea=dv.textarea=tx;
	dv.contentEditable=true;
	dv.innerHTML=this.value;
	dv.style.color="black";
	dv.style.backgroundColor="white";
	dv.onblur=function() {this.value=this.textarea.value=this.innerHTML}
	this.codearea=dv;
	
	//if(this.style.height=="100%") tb.style.height="100%";
	tb.style.height=this.style.height;
	tb.style.width=this.style.width;
	var tr=tb.insertRow().insertCell();
	tr.style.height=30;
	this.menuregion=tr;
	tr=tb.insertRow().insertCell();
	this.coderegion=tr.appendChild(tx);
	this.editregion=tr.appendChild(dv);
	html_obj.appendChild(tb);
	this.is_alive=true;
	tr.style.width=this.style.width;
	tr.style.height=this.style.height;
	this.setCodeBoxStyle();
	this.setEditBoxStyle();
	this.refresh();
}
SuperTextBox.prototype.refresh=function(html_obj) {
	this.menuregion.innerHTML="";
	this.popup=null;
	var cmd;
	for(cmdname in this.dispatch) {
		cmd=this.dispatch[cmdname];
		if(cmd.status==-1 || cmd.type==1) continue;
		if(cmd.type==0) {
			button=cmd.getButton();
			this.menuregion.appendChild(button);
			button.disabled=(cmd.status==0?true:false);
		}
	}
}
SuperTextBox.prototype.createCmd=function(type,img,diagfunc) {
	var cmd=new Object();	
	cmd.name=name;
	cmd.type=type; /*0=button, 1=list*/
	cmd.status=1; /*-1=hide, 0=disable, 1=enable*/
	cmd.img=img;
	cmd.dialog;
	cmd.helptip;
	cmd.redraw=true;
	cmd.stextbox=this;
	cmd.button;
	cmd.createDialog=diagfunc;
	cmd.getButton=function() {
		var but=document.createElement("button");
		but.style.height=but.style.width=25;
		but.style.backgroundImage="url(" + this.stextbox.image_dir + this.img + ")";
		if(this.status==1) but.style.backgroundColor="d5d6d6"
		but.cmdRef=cmd;
		but.onclick=this.stextbox.execHandler;
		but.onmousemove=new Function("this.cmdRef.stextbox.hoverHelp(this)");
		but.onmouseleave=new Function("if(this.cmdRef.stextbox.popup) this.cmdRef.stextbox.popup.style.display='none'");
		this.button=but;
		return but;
	}
	
	return cmd;
}
SuperTextBox.prototype.execHandler=function() {
	var cmd=this.cmdRef;
	if(cmd.createDialog==null) {
		var sel=document.selection.createRange();
		var rng=document.body.createTextRange();
		rng.moveToElementText(cmd.stextbox.editregion);
		if(!rng.inRange(sel)) {
			rng.collapse(false);
			rng.select();
		}
		document.execCommand(cmd.name,true);
		return;
	}
	var tb;
	if(cmd.redraw) {
		var cl;
		var attrib;
		tb=cmd.dialog=cmd.createDialog(cmd);
		tb.onblur=function() {this.style.display="none";}
		tb.style.position="absolute";
		tb.style.background="#EBE2D1";
		tb.style.color="#42425A";
		tb.style.border="2px outset #A296BA";
		tb.style.filter+="progid:DXImageTransform.Microsoft.dropShadow(Color=bbbbbb,offX=3,offY=3,positive=true)";
		if(tb.rows(0).cells.length==1) {
			for(var i=0;i<tb.rows.length;i++) {
				tb.rows(i).onmouseover=function() {with(this.style){backgroundColor="#565565";color="white";}}
				tb.rows(i).onmouseout=function() {with(this.style){backgroundColor="#EBE2D1";color="#42425A"}}
			}
		}
		cmd.redraw=false;
	}
	else tb=cmd.dialog;
	tb.style.display="block";
	tb.style.top=document.body.scrollTop+event.clientY;
	var leftPos=document.body.scrollLeft+event.clientX;
	tb.style.left=leftPos-(leftPos+tb.style.posWidth>document.body.scrollWidth?tb.style.posWidth:0);
	
	if(cmd.type==0) tb.setActive();
	return;
}
SuperTextBox.prototype.createColorGrid=function(cmdRef) {
	var hex = new Array("00","88","FF");
	var count=0;
	var color;
	tb=document.body.appendChild(document.createElement("table"));
	tb.style.width=150;
	var rw=tb.insertRow();
	var cl;
	for (var first=0; first < hex.length; first++){
		for(var second=0; second < hex.length; second++){
			for(var third=0; third < hex.length; third++){
				color='#' + hex[first] + hex[second] + hex[third];
				if(count % 9==0) rw=tb.insertRow();
				cl=rw.insertCell();
				cl.style.backgroundColor=color;
				cl.style.width=10;
				cl.style.cursor="hand";
				cl.style.border="1px solid";
				cl.tbRef=tb;
				cl.unselectable="on";
				cl.onmouseover=function() {this.style.border="1px inset";}
				cl.onmouseout=function() {this.style.border="1px solid";}
				cl.onclick=new Function("document.execCommand('" + cmdRef.name + "',true,'" + color + "');this.tbRef.style.display='none'");
				cl.innerText=" ";
				cl.title=color;
				count++;
			}
		}
	}
	return tb;
}
SuperTextBox.prototype.createFontMenu=function(cmdRef) {	
	tb=document.body.appendChild(document.createElement("table"));
	tb.style.width=150;
	var attrib,cl;
	for(attrib in cmdRef.stextbox.font.face) {
		attrib=attrib.replace(/_/g," ");
		cl=tb.insertRow().insertCell();
		cl.innerHTML="<font unselectable='on' face='"+attrib+"'>"+attrib+"</font>";
		cl.style.cursor="hand";
		cl.tbRef=tb;
		cl.unselectable="on";
		cl.onclick=new Function("document.execCommand('" + cmdRef.name + "',false,'"+attrib+"');this.tbRef.style.display='none'");
	}
	return tb;
}
SuperTextBox.prototype.createSizeMenu=function(cmdRef) {
	tb=document.body.appendChild(document.createElement("table"));
	tb.style.width=140;
	var sizeObj=cmdRef.stextbox.font.size;
	for(var i=sizeObj.MIN;i<=sizeObj.MAX;i++) {
		cl=tb.insertRow().insertCell();
		cl.innerHTML="<font unselectable='on' size='"+i+"'>Size "+i+"</font>";
		cl.style.cursor="hand";
		cl.tbRef=tb;
		cl.unselectable="on";
		cl.onclick=new Function("document.execCommand('FontSize',false,'"+i+"');this.tbRef.style.display='none'");
	}
	return tb;
}
SuperTextBox.prototype.setEditMode=function(edt_mode,cmdRef) {
	with(this) {
		edit_mode=edt_mode||(edit_mode?false:true);
		editregion.style.display=(edit_mode?"block":"none");
		coderegion.style.display=(edit_mode?"none":"block");
		if(edit_mode)
			editregion.innerHTML=coderegion.value;
		else
			coderegion.value=editregion.innerHTML;
			
		for(cmd in dispatch)
			if(cmd!="OptionMenu" && dispatch[cmd].status != -1) dispatch[cmd].status=(edit_mode?1:0);
		refresh();
	}
}
SuperTextBox.prototype.createOptionMenu=function(cmdRef) {
	tb=document.body.appendChild(document.createElement("table"));
	tb.style.width=120;
	var cmd,cl;
	var dispatch=cmdRef.stextbox.dispatch;
	for(cmdname in dispatch) {
		cmd=dispatch[cmdname];
		if(cmd.status==-1 || cmd.type==0) continue;
		if(cmd.type==1) {
			cl=tb.insertRow().insertCell();
			cl.innerHTML="<font unselectable='on' size='2'>"+cmd.name+"</font>";
			cl.style.cursor="hand";
			cl.tbRef=tb;
			cl.unselectable="on";
			cl.cmdRef=cmd;
			if(cmd.status==0)
				cl.firstChild.style.color="silver";
			else
				cl.onclick=cmd.getButton().onclick;
		}
	}
	/*additional options*/
	cl=tb.insertRow().insertCell();
	cl.innerHTML="<font unselectable='on' face='webdings'>" + (cmdRef.stextbox.enable_help?"&#97;":" ")+ "</font><font unselectable='on' size='2'>Enable Helptips</font>";
	cl.style.cursor="hand";
	cl.tbRef=tb;
	cl.txRef=cmdRef.stextbox;
	cl.cmdRef=cmdRef;
	cl.unselectable="on";
	cl.onclick=function() {
		this.txRef.enable_help=!this.txRef.enable_help;
		this.cmdRef.redraw=true;
		this.tbRef.style.display='none';
	}
	cl=tb.insertRow().insertCell();
	cl.innerHTML="<font unselectable='on' face='webdings'>" + (cmdRef.stextbox.edit_mode?"&#97;":" ")+ "</font><font unselectable='on' size='2'>Design Mode</font>";
	cl.style.cursor="hand";
	cl.tbRef=tb;
	cl.txRef=cmdRef.stextbox;
	cl.cmdRef=cmdRef;
	cl.unselectable="on";
	cl.onclick=function() {
		this.txRef.setEditMode(!this.txRef.edit_mode);
		this.cmdRef.redraw=true;
		this.tbRef.style.display='none';
	}
	return tb;
}
SuperTextBox.prototype.hoverHelp=function(bt_ref) {
	var cmd=bt_ref.cmdRef;
	if(!this.enable_help) return;
	
	var pp=this.popup;
	if(pp==null) {	
		pp=document.createElement("div");
		with(pp.style) {
			width=100;
			position="absolute";
			border="2px solid black";
			backgroundColor="gold";
			font="8pt times";
			color="blue";
			padding=3;
			filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=70)";
			filter+="progid:DXImageTransform.Microsoft.dropShadow(Color=bbbbbb,offX=5,offY=5,positive=true)"
		}
		this.popup=this.menuregion.appendChild(pp);
	}
	pp.style.display="block";
	pp.innerHTML=(cmd.helptip?cmd.helptip:cmd.name.replace(/([a-z]{1})([A-Z]{1})/g,"$1 $2"));
	pp.style.top=document.body.scrollTop+event.clientY+10;
	pp.style.left=document.body.scrollLeft+event.clientX+10;
}
SuperTextBox.prototype.setCodeBoxStyle=function(sty) {
	if(sty) this.style.codebox;
	if(!this.is_alive) return;
	this.setStyle(this.style.codebox,this.coderegion);
}
SuperTextBox.prototype.setEditBoxStyle=function(sty) {
	if(sty) this.style.editbox;
	if(!this.is_alive) return;
	this.setStyle(this.style.editbox,this.editregion);
}
SuperTextBox.prototype.setStyle=function(o_style,obj_ref) {
	var arr=o_style.split(";");
	for(var i = 0; i < arr.length; i++) {
		if(arr[i].length) {
			var prop = arr[i].split(":");
			for(var j=0;j<prop[0].length;j++) {
				if(prop[0].charAt(j)=="-")
					prop[0]=prop[0].replace(/-(\w?)/,prop[0].charAt(j+1).toUpperCase());
			}
			eval("obj_ref.style." + prop[0] + "='" + prop[1]+"'");
		}		
	}
}
SuperTextBox.prototype.disableCommands=function() {
	for(var i=0;i < arguments.length;i++)
		this.dispatch[arguments[i]].status=-1;
}
SuperTextBox.prototype.enableCommands=function() {
	for(var i=0;i < arguments.length;i++)
		this.dispatch[arguments[i]].status=1;
}
SuperTextBox.prototype.createAsListItems=function() {
	for(var i=0;i < arguments.length;i++)
		this.dispatch[arguments[i]].type=1;
}
SuperTextBox.prototype.createAsButtons=function() {
	for(var i=0;i < arguments.length;i++)
		this.dispatch[arguments[i]].type=0;
}
SuperTextBox.prototype.getFormRef=function() {
	return this.coderegion;
}
SuperTextBox.prototype.setImageDir=function(dir) {
	this.image_dir=dir;
}
SuperTextBox.prototype.setValue=function(v) {
	this.codearea.innerHTML=this.textarea.value=v;
}
SuperTextBox.prototype.getValue=function() {
	return (this.edit_mode?this.codearea.innerHTML:this.textarea.value);
}