function sHTML() {
	this.init=sHTML_init;
	this.getConfigKey=sHTML_getConfigKey;
	this.getVisualTemplate=sHMTLVisual_getTemplate;
	this.addToolButton=sHTMLVisual_addToolButton;
	this.addToolDivider=sHTMLVisual_addToolDivider;
	this.addToolCombo=sHTMLVisual_addToolCombo;
	this.replaceVar=sHTML_replaceVar;
	this.replaceVars=sHTML_replaceVars;
	this.regexpReplace=sHTML_regexpReplace;
	this.toRGB=sHTML_toRGB;
	this.update=sHTML_update;
	this.runCmd=sHTML_runCmd;
	this.setDesignMode=sHTML_setDesignMode;
	this.getDocument=sHTML_getDocument;
	this.getWindow=sHTML_getWindow;
	this.getComboBox=sHTML_getComboBox;
	this.getSourceForm=sHTML_getSourceForm;
	this.getSourceControl=sHTML_getSourceControl;
	this.addEventHandlers=sHTML_addEventHandlers;
	this.handleEvent=sHTML_handleEvent;
	this.setFormat=sHTML_setFormat;
	this.setFontFace=sHTML_setFontFace;
	this.setFontSize=sHTML_setFontSize;
	this.insertDateTime=sHTML_insertDateTime;
	this.setColor=sHTML_setColor;
	this.insertLink=sHTML_insertLink;
	this.insertImage=sHTML_insertImage;
	this.insertTable=sHTML_insertTable;
	this.updateTable=sHTML_updateTable;
	this.manageTable=sHTML_manageTable;
	this.getParentElement=sHTML_getParentElement;
	this.getAttribute=sHTML_getAttribute;
	this.setAttribute=sHTML_setAttribute;
	this.getElementByAttributeValue=sHTML_getElementByAttributeValue;
	this.getElementsByAttributeValue=sHTML_getElementsByAttributeValue;
	this.getFocusElement=sHTML_getFocusElement;
	this.openPopup=sHTML_openPopup;
	this.openPopupDialog=sHTML_openPopupDialog;
	this.getPopupAttribute=sHTML_getPopupAttribute;
	this.onToolButtonOver=sHTMLVisual_onToolButtonOver;
	this.onToolButtonOut=sHTMLVisual_onToolButtonOut;
	this.cleanUp=sHTML_cleanUp;
	this.cleanupNode=sHTML_cleanupNode;
}

function sHTMLVisual_addToolButton(cmd,help,source) {
	res='<img name="btn_'+cmd+'" src="{$imgsrc}/btn_'+cmd+'.gif" alt="'+help+'"onmouseover="shtml.onToolButtonOver(\'btn_'+cmd+'\')" onmouseout="shtml.onToolButtonOut(\'btn_'+cmd+'\')" onclick="shtml.runCmd(\''+cmd+'\')">';
	source+=res;
	return res;
}

function sHTMLVisual_addToolDivider() {
	res='<img src="{$imgsrc}/tb_div.gif">';
	return res;
}

function sHTMLVisual_addToolCombo(comboname) {
	var ctrlName=""
	var res="";
	var formats=new Array('Normal','Heading 1','Heading 2','Heading 3','Heading 4','Heading 5','Heading 6','Address','Formatted');
	var fonts=new Array('Arial','Courier New','Georgia','Tahoma','Times New Roman','Verdana','WingDings');
	var sizes=new Array('8 pt','10 pt','12 pt','14 pt','18 pt','24 pt','36 pt');
	var arr=new Array();
	switch(comboname) {
	case "format":
		ctrlName="shtml_format";
		arr=formats;
		break;
	case "font":
		ctrlName="shtml_font";
		arr=fonts;
		break;
	case "fontsize":
		ctrlName="shtml_fontsize";
		arr=sizes;
		break;
	}
	res='<select class="shtml_input" name="'+ctrlName+'" id="'+ctrlName+'" onChange=\'shtml.runCmd("'+comboname+'",{})\'>';
	for (i=0;i<arr.length;i++)
		res+='<option value="'+arr[i]+'">'+arr[i]+'</option>';
	res+='</select>';
	return res;
}

function sHTMLVisual_onToolButtonOver(img_name) {
	document[img_name].src=this.config["imagedir"]+"/"+img_name+"_on.gif";
}

function sHTMLVisual_onToolButtonOut(img_name) {
	if (this.buttons[img_name])
		document[img_name].src=this.config["imagedir"]+"/"+img_name+"_set.gif";	
	else
		document[img_name].src=this.config["imagedir"]+"/"+img_name+".gif";
}


function sHMTLVisual_getTemplate() {
	html='<style>\
	       TD.shtml_tb {\
		   font: bolder 10px Tahoma,Verdana,Arial;\
		   color:#000000;\
		   vertical-align:middle;\
		   background-image: url("{$imgsrc}/tb_bkg.gif");\
		  }\
		   Input.shtml_Input,Select.shtml_Input {\
		   font: 10px Tahoma,Verdana,Arial;\
		   color: #000000;\
		   background: #FFFFFF;\
		   height:15px;\
		   border: #808080 1px solid;\
		   }\
		  </style>';
	html+='<table cellpadding=0 cellspacing=0 width=99%  align=center style="border:#808080 1px solid">\
		  <tr>\
		  <td class=shtml_tb height=24><img src="{$imgsrc}/tb_left.gif" height=24</td>\
		  <td class=shtml_tb height=24 width=100% align=left colspan=2>Title: <input class="shtml_Input" type="text" name="shtml_title" size=50 value="'+this.config["title"]+'"></td>\
		  </tr>\
		  <tr>\
		  <td class=shtml_tb height=24><img src="{$imgsrc}/tb_left.gif" height=24></td>\
		  <td class=shtml_tb height=24 width=100% align=left colspan=2>';
	html+=shtml.addToolButton("save","Save");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("cut","Cut");
	html+=shtml.addToolButton("copy","Copy");
	html+=shtml.addToolButton("paste","Paste");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("undo","Undo");
	html+=shtml.addToolButton("redo","Redo");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("link","Insert Link");
	html+=shtml.addToolButton("unlink","Delete Link");
	html+=shtml.addToolButton("image","Insert Image");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("tblins","Insert Table");
	html+=shtml.addToolButton("tblopt","Table Properties");
	html+=shtml.addToolButton("tblinsrowa","Insert Row Above");
	html+=shtml.addToolButton("tblinsrowb","Insert Row Below");	
	html+=shtml.addToolButton("tbldelrow","Delete Row");
	html+=shtml.addToolButton("tblinscoll","Insert Column to the left");
	html+=shtml.addToolButton("tblinscolr","Insert Column to the right");	
	html+=shtml.addToolButton("tbldelcol","Delete Column");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("hr","Insert Horizontal Line");
	html+=shtml.addToolButton("date","Insert Current Date");
	html+=shtml.addToolButton("time","Insert Current Time");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("src","View HTML Source");
	html+='\</td>\
		  		</tr>';
	
	html+='<tr>\
   		  <td class=shtml_tb height=24 valign=24><img src="{$imgsrc}/tb_left.gif" height=24></td>\
		  <td class=shtml_tb height=24 align=left>'
	html+=shtml.addToolCombo("format");
	html+="&nbsp;";	
	html+=shtml.addToolCombo("font");
	html+=shtml.addToolCombo("fontsize");
	html+='</td>\
		  <td class=shtml_tb height=24 width=100% align=left>';
	html+=shtml.addToolButton("bold","Bold");
	html+=shtml.addToolButton("italic","Italic");
	html+=shtml.addToolButton("underline","Underline");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("justifyleft","Align left");
	html+=shtml.addToolButton("justifycenter","Align center");
	html+=shtml.addToolButton("justifyright","Align right");
	html+=shtml.addToolButton("justifyfull","Align full");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("insertorderedlist","Ordered list");
	html+=shtml.addToolButton("insertunorderedlist","Unordered list");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("outdent","Outdent");
	html+=shtml.addToolButton("indent","Indent");
	html+=shtml.addToolDivider();
	html+=shtml.addToolButton("forecolor","Text color");
	html+=shtml.addToolButton("backcolor","Background color");
	html+='</td>\
			</tr>\
			<tr>';	
	html+='<td class=tb colspan=3>\
			    <iframe id="ed" src="#" width=100% height=300></iframe>\
				</td>\
			    </tr>\
		        </table>';
	return html;
}

function sHTML_replaceVar(replace_haystack, replace_var, replace_str) {
	var re = new RegExp('{\\\$' + replace_var + '}', 'g');
	return replace_haystack.replace(re, replace_str);
}

function sHTML_replaceVars(replace_haystack, replace_vars) {
	for (var key in replace_vars) {
		var value = replace_vars[key];
		replace_haystack = shtml.replaceVar(replace_haystack, key, value);
	}
	return replace_haystack;
}

function sHTML_regexpReplace(in_str,reg_exp,replace_str,opts) {
	if (typeof(opts)=="undefined")
		opts = 'g';
	var re=new RegExp(reg_exp,opts);
	return in_str.replace(re,replace_str);
}

function sHTML_toRGB() {
 var hex_string = "";
  for (i=0;i<3;i++) {
    var bt = value & 0xFF;            
    value >>= 8;                        
    var nybble2 = bt & 0x0F;          
    var nybble1 = (bt >> 4) & 0x0F;   
    hex_string += nybble1.toString(16); 
    hex_string += nybble2.toString(16); 
  }
  return hex_string.toUpperCase();
}

function sHTML_getWindow() {
	return this.frame.contentWindow;
}

function sHTML_getDocument() {
	return this.frame.contentWindow.document;
}

function sHTML_getComboBox(id) {
	return document.getElementById(id);
}

function sHTML_getSourceForm() {
	return document.forms[this.config["srcform"]];
}

function sHTML_getSourceControl() {
	return this.getSourceForm().elements[this.config["srccontrol"]];
}

function sHTML_setDesignMode(val) {
	this.getDocument().designMode=val;
}

function sHTML_cleanupNode(node,src_flag) {
	var brd=0;
	var border=node.style.border;
	var bt=node.style.borderTop;
	var bb=node.style.borderBottom;
	var br=node.style.borderRight;
	var bl=node.style.borderLeft;
	var css=node.style.cssText.toLowerCase();
	if (border!="" && (bt==border && bb==border && bl==border && br==border)) {
		css=shtml.regexpReplace(css, 'border-top: ' + border + '?; ?', '');
		css=shtml.regexpReplace(css, 'border-left: ' + border + '?; ?', '');
		css=shtml.regexpReplace(css, 'border-right: ' + border + '?; ?', '');
		css=shtml.regexpReplace(css, 'border-bottom: ' + border + '?;( ?)', 'border: ' + border + ';($1)');			
		css=shtml.regexpReplace(css, 'border-bottom: ' + border, 'border: ' + border);		
		if (src_flag && css==this.config["table_visualhelp"].toLowerCase()) css="";
	}
	if (!src_flag && brd==0) {
		if (css.length==0)
			css=this.config["table_visualhelp"];
		else
			css+=";"+this.config["table_visualhelp"];
	}
	node.style.cssText=css;	
}

function sHTML_cleanUp(node,src_flag) {
	var res="";
	if (node.hasChildNodes)
		for (var i=0;i<node.childNodes.length;i++)
			res+=this.cleanUp(node.childNodes[i],src_flag);
	if (node.nodeName=="TABLE") {
		shtml.cleanupNode(node,src_flag);
		for (var j=0;j<node.rows.length;j++) {
			tr=node.rows[j];
			for (var i=0;i<tr.cells.length;i++)
				shtml.cleanupNode(tr.cells[i],src_flag);
		}
	}
}

function sHTML_handleEvent(e) {
	if (shtml.view) {
		shtml.update();
		shtml.getSourceControl().value=shtml.getDocument().body.innerHTML;
	}
	else {
		shtml.getSourceControl().value=shtml.getDocument().body.innerText;
	}
}

function sHTML_addEventHandlers() {
	var doc=document.getElementById("ed").contentWindow.document;
	var tempFunction=function() {
		var event=document.frames["ed"].event;
		event.target=event.srcElement;
		sHTML_handleEvent(event);
	}
	doc.onkeypress=tempFunction;
	doc.onkeyup=tempFunction;
	doc.onkeydown=tempFunction;
	doc.onclick=tempFunction;
	doc.onmouseup=tempFunction;	
}

function sHTML_init(config) {
	this.config=config;
	this.windowArgs=new Array();
	this.buttons=new Array();
	
	function getKey(key,defKey) {
		config[key]=shtml.getConfigKey(key,defKey);
	}
	
	getKey("shtmldir","includes");
	getKey("imagedir","images");
	getKey("srcform","shtml_form");
	getKey("srccontrol","shtml_src");
	getKey("height","300");
	getKey("table_visualhelp","BORDER: #808080 1px dashed");
	getKey("table_noborder","BORDER=0");
	getKey("title","");
	getKey("uploadimagedir","images");
	
	html=shtml.replaceVar(this.getVisualTemplate(),"imgsrc",this.config["imagedir"]);
	document.write(html);
	this.frame=document.getElementById("ed");
	this.setDesignMode("on");
	this.getDocument().open();
	this.getDocument().write("<html><body>"+this.getSourceControl().value+"</body></html>");
	this.getDocument().close();	
	//this.getDocument().body.innerHTML=this.getSourceControl().value;
	this.view=true;
	window.setTimeout("sHTML_addEventHandlers();", 1);
	this.getWindow().focus();	
	shtml.cleanUp(shtml.getDocument().body,false);
}

function sHTML_update() {
	var bt=new Array("bold","italic","underline","justifyleft","justifycenter","justifyright","justifyfull","insertorderedlist","insertunorderedlist");
	var doc=this.getDocument();
	for (i=0;i<bt.length;i++) {
		try {
			cmdActive = doc.queryCommandState(bt[i]);	
			this.buttons["btn_"+bt[i]]=cmdActive;
			this.onToolButtonOut("btn_"+bt[i]);	
		}
		catch (e) {};
	}
	this.buttons["btn_src"]=(!this.view);
	this.onToolButtonOut("btn_src");
	var fontface=doc.queryCommandValue('FontName');
	var fontsize=doc.queryCommandValue('FontSize');
	var fontfrm = doc.queryCommandValue('FormatBlock');
	if (fontfrm!=null) {
		var cb=this.getComboBox("shtml_format");
		for (i=0;i<cb.length;i++)
			if (cb[i].text==fontfrm)
				cb.selectedIndex=i;
	}
	if (fontsize!=null) {
		var cb=this.getComboBox("shtml_fontsize");
		cb.selectedIndex=fontsize-1;
	}	
	if (fontface!=null) {
		var cb=this.getComboBox("shtml_font");
		for (i=0;i<cb.length;i++)
			if (cb[i].text==fontface)
				cb.selectedIndex=i;		
	}
}

function sHTML_openPopup(template,vars) {
	var file=shtml.config["shtmldir"]+"/templates/"+template["file"];
	this.windowArgs=vars;
	for (var v in vars)
		file=shtml.replaceVar(file,v,escape(vars[v]));
	var width=(typeof(template["width"])=="undefined")?320:template["width"];
	var height=(typeof(template["height"])=="undefined")?240:template["height"];
	x=parseInt(screen.width/2)-(width/2);
	y=parseInt(screen.height/2)-(height/2);
	var win=window.open(file,"shtml_popup","top="+y+",left="+x+"scrollbars=no,dialog=yes,status=yes,minimizable=no,modal=yes,width="+width+",height="+height+",resizeable=no");
	win.focus();
}

function sHTML_openPopupDialog(template,vars) {
	var file=shtml.config["shtmldir"]+"/templates/"+template["file"];
	var width=(typeof(template["width"])=="undefined")?320:template["width"];
	var height=(typeof(template["height"])=="undefined")?240:template["height"];
	x=parseInt(screen.width/2)-(width/2);
	y=parseInt(screen.height/2)-(height/2);
	return showModalDialog(file,vars,"dialogWidth:"+width+"px; dialogHeight: "+height+"px; dialogTop: "+y+"; dialogLeft: "+x+"; resizable: no; help: no; status: no; scroll: no; ");
}

function sHTML_getConfigKey(key,defKey) {
	var val=(typeof(this.config[key])=="undefined")?defKey:this.config[key];
	return val;
}

function sHTML_getParentElement(node,elements,attrName,attrValue) {
	if (typeof(elements)=="undefined") {
		if (node.nodeType==1)
			return node;
		while ((node=node.parentNode)!=null && node.nodeType!=1);
		return node;
	}
	var arr=elements.split(',');
	if (node==null)
		return null;
	do {
		for (var i=0;i<arr.length;i++) {
			if (node.nodeName.toLowerCase()==arr[i].toLowerCase() || elements == "*") {
				if (typeof(attrName)=="undefined")
					return node;
				else if (node.getAttribute(attrName)) {
					if (typeof(attrValue)=="undefined") {
						if (node.getAttribute(attrName)!="")
							return node;
					} else if (node.getAttribute(attrName)==attrValue)
						return node;
				}
			}
		}
	} while (node=node.parentNode);
	return null;
}

function sHTML_getFocusElement() {
	var doc = this.getDocument();
	var rng = doc.selection.createRange();
	var res = rng.item ? rng.item(0) : rng.parentElement();
	return res;
}

function sHTML_getElementByAttributeValue(node,element_name,attrib,value) {
	var elements = this.getElementsByAttributeValue(node,element_name,attrib,value);
	if (elements.length==0)
		return null;
	return elements[0];
}

function sHTML_getElementsByAttributeValue(node,element_name,attrib,value) {
var elements = new Array();
	if (node && node.nodeName.toLowerCase()==element_name) {
		if (node.getAttribute(attrib) && node.getAttribute(attrib).indexOf(value)!=-1)
			elements[elements.length]=node;
	}
	if (node.hasChildNodes) {
		for (var x=0; x<node.childNodes.length; x++) {
			var childElements=this.getElementsByAttributeValue(node.childNodes[x], element_name, attrib, value);
			for (var i=0;i<childElements.length;i++)
				elements[elements.length]=childElements[i];
		}
	}
	return elements;
}

function sHTML_getAttribute(element,name,defValue) {
	var v = element.getAttribute(name);
	return (v && v!="")?v:defValue;
}

function sHTML_setAttribute(element,name,value) {
	if (typeof(value)=="number")
		value=""+value;
	if (value!=null && value!=-1 && value!="")
		element.setAttribute(name,value);
	else
		element.removeAttribute(name);
}

function sHTML_setFormat() {
	var formats=new Array("p","h1","h2","h3","h4","h5","h6","address","pre");
	var i=this.getComboBox("shtml_format").selectedIndex;
	this.getDocument().execCommand("FormatBlock",false,"<"+formats[i]+">");
}

function sHTML_setFontFace() {
	var cb=this.getComboBox("shtml_font");
	var face=cb.options[cb.selectedIndex].value;
	this.getDocument().execCommand("FontName",false,face);
}

function sHTML_setFontSize() {
	var size=this.getComboBox("shtml_fontsize").selectedIndex+1;
	this.getDocument().execCommand("FontSize",false,size);
}

function sHTML_insertDateTime(cmd) {
	var str="";
	if (cmd!="date" && cmd!="time") return;
	var dt=new Date();
	if (cmd=="date")
		str=dt.toLocaleDateString();
	else
		str=dt.toLocaleTimeString();
	this.getWindow().focus();
	var sel=this.getDocument().selection.createRange();
	sel.pasteHTML(str);
}

function sHTML_getPopupAttribute(name,defValue) {
	if (typeof(defValue)=="undefined") defValue="";
	return (typeof(this.windowArgs[name])=="undefined")?defValue:this.windowArgs[name];
}

function sHTML_setColor(cmd) {
	var prev=0;
	try {
		prev=shtml.toRGB(this.getDocument().queryCommandValue(cmd))
	}
	catch (e) {};
	var color=shtml.openPopupDialog({file:"cl_picker.html",width:"238",height:"191"},prev);
	if (color!=null) this.getDocument().execCommand(cmd,false,"#"+color);
}

function sHTML_insertLink(pre,args) {
	function setAttribute(element,name,value) {
		if (value!= null && value!="")
			element.setAttribute(name,value);
		else
			element.removeAttribute(name);
	}
	if (pre) {
		var sel="";
		var rng = this.getDocument().selection.createRange();
		sel=rng.text;
		if ((shtml.getFocusElement().nodeName.toLowerCase()!="img") && (sel.length<=0))
			return;
		shtml.openPopup({file:"link.asp",width:"350",height:"100"},{});
	}
	else {
		if (shtml.getFocusElement() && shtml.getFocusElement().nodeName.toLowerCase()=="img") {
			var doc=this.getDocument();
			var linkElement=doc.createElement("a");
			setAttribute(linkElement,'href',args["href"]);
			linkElement.appendChild(this.getFocusElement().cloneNode(true));
			this.getFocusElement().parentNode.replaceChild(linkElement, this.getFocusElement());
			return;
		}
		var doc=this.getDocument();
		doc.execCommand("createlink", false, "#shtml_temp_url#");
		shtml.linkElement = this.getElementByAttributeValue(doc.body,"a","href","#shtml_temp_url#");
		var elementArray = this.getElementsByAttributeValue(this.getDocument().body,"a","href","#shtml_temp_url#");

		for (var i=0; i<elementArray.length; i++) {
			setAttribute(elementArray[i], 'href', args["href"]);
		}
		shtml.linkElement = elementArray[0];		
		if (shtml.linkElement) {
			setAttribute(this.linkElement,'href',args["href"]);
		}
	}
}

function sHTML_insertImage(pre,args) {
	function setAttribute(element,name,value) {
		if (value!= null && value!="")
			element.setAttribute(name,value);
		else
			element.removeAttribute(name);
	}
	if (pre) {
		shtml.openPopup({file:"image.asp",width:"340",height:"200"},{});
	}
	else {
		var href=this.config['uploadimagedir']+"/"+args["href"];
		var alt=args["descr"];
		var vspace=args["vspace"];
		var hspace=args["hspace"];
		var border=args["border"];
		var align=args["align"];
		var doc=this.getDocument();
		doc.execCommand("insertimage", false, "#mce_temp_url#");
		shtml.imgElement = this.getElementByAttributeValue(this.getDocument().body, "img", "src", "#mce_temp_url#");
		if (this.imgElement) {
			setAttribute(this.imgElement,'src',href);
			setAttribute(this.imgElement, 'alt', alt);
			setAttribute(this.imgElement, 'align', align);
			setAttribute(this.imgElement, 'border', border);
			setAttribute(this.imgElement, 'hspace', hspace);
			setAttribute(this.imgElement, 'vspace', vspace);
		}
	}
}

function sHTML_insertTable(pre,args) {
	if (pre)
		shtml.openPopup({file:"table.html",width:"250",height:"150"},{});
	else {
		var cols=(args["cols"]=="")?2:args["cols"];
		var rows=(args["rows"]=="")?2:args["rows"];
		var cellpadding=(args["cellpadding"]=="")?-1:args["cellpadding"];
		var cellspacing=(args["cellspacing"]=="")?-1:args["cellspacing"];
		var align=args["align"];
		var border=(args["border"]=="")?0:args["border"];
		var width=(args["width"]=="")?-1:args["width"];
		var height=(args["height"]=="")?-1:args["height"];
		html="<table border="+border;
		if (border==0) html+=" style='"+this.config["table_visualhelp"]+"'";
		if (cellpadding>-1) html+=" cellpadding="+cellpadding;
		if (cellspacing>-1) html+=" cellspacing="+cellspacing;
		if (align!="") html+=" align="+align;
		if (width>-1) html+=" width="+width;
		if (height>-1) html+=" height="+height;
		html+=">";
		for (var j=0;j<rows;j++) {
			html+="<tr>";
			for (var i=0;i<cols;i++) {
				html+="<td";
				if (border==0) html+=" style='"+this.config["table_visualhelp"]+"'";
				html+=">&nbsp;</td>";
			}
			html+="</tr>";
		}	
		html+="</table>"
		this.getWindow().focus();
		var sel=this.getDocument().selection.createRange();
		if (sel.item)
			sel.item(0).outerHTML=html;
		else
			sel.pasteHTML(html);
	}
}

function sHTML_updateTable(pre,args) {
	if (pre) {
		shtml.tableCurrent=shtml.getParentElement(shtml.getFocusElement(),"table")
		if (shtml.tableCurrent) {
			var arr=shtml.tableCurrent.rows;
			var cols=0;
			for (var i=0;i<arr.length;i++)
				if (arr[i].cells.length>cols)
					cols=arr[i].cells.length;
			rows=arr.length;
			border=shtml.getAttribute(shtml.tableCurrent,"border",0);
			cellpadding=shtml.getAttribute(shtml.tableCurrent,"cellpadding","");
			cellspacing=shtml.getAttribute(shtml.tableCurrent,"cellspacing","");
			width=shtml.getAttribute(shtml.tableCurrent,"width","");
			height=shtml.getAttribute(shtml.tableCurrent,"height","");
			align=shtml.getAttribute(shtml.tableCurrent,"align","");
			shtml.openPopup({file:"table.html",width:"250",height:"150"},{border:border,cellpadding:cellpadding,cols:cols,rows:rows,cellspacing:cellspacing,width:width,height:height,align:align,update:"yes"});
		}
	}
	else {
		if (shtml.tableCurrent) {
			var cellpadding=(args["cellpadding"]=="")?-1:args["cellpadding"];
			var cellspacing=(args["cellspacing"]=="")?-1:args["cellspacing"];
			var align=args["align"];
			var border=(args["border"]=="")?0:args["border"];
			var width=(args["width"]=="")?-1:args["width"];
			var height=(args["height"]=="")?-1:args["height"];		
			shtml.setAttribute(shtml.tableCurrent,"border",border)
			shtml.setAttribute(shtml.tableCurrent,"cellPadding",cellpadding);
			shtml.setAttribute(shtml.tableCurrent,"cellSpacing",cellspacing);	
			shtml.setAttribute(shtml.tableCurrent,"width",width);
			shtml.setAttribute(shtml.tableCurrent,"height",height);
			shtml.setAttribute(shtml.tableCurrent,"align",align);
			var cssText=this.config["table_visualhelp"];
			if (border==0) shtml.tableCurrent.style.cssText=cssText; else shtml.tableCurrent.style.cssText="";
			for (var j=0;j<shtml.tableCurrent.rows.length;j++) {
				for (var i=0;i<shtml.tableCurrent.rows[j].cells.length;i++)
					shtml.tableCurrent.rows[j].cells[i].style.cssText=(border==0)?cssText:"";
			}
			shtml.tableCurrent.outerHTML=shtml.tableCurrent.outerHTML;
			this.getWindow().focus();					
		}
	}
}

function sHTML_manageTable(cmd) {
	var tbl=shtml.getParentElement(shtml.getFocusElement(),"table");
	var td=shtml.getParentElement(shtml.getFocusElement(),"td");
	var tr=shtml.getParentElement(shtml.getFocusElement(),"tr");
	if (!tbl) return;
	var border=tbl.getAttribute("border")
	var cssText=(border==0)?this.config["table_visualhelp"]:"";
	if (tbl.firstChild && tbl.firstChild.nodeName.toLowerCase()=="tbody")
		tbl=tbl.firstChild;
	if (tbl && tr) {
		switch(cmd) {
		case "tblinsrowa":
		case "tblinsrowb":
			var cols=tr.cells.length;
			var rows=0;
			var tmptr=tr;
			while (tmptr) {
				if (tmptr.nodeName.toLowerCase()=="tr")
					rows++;
				tmptr=tmptr.previousSibling;
			}
			if (cmd=="tblinsrowa")
				var row=tbl.insertRow(rows==0?1:rows-1);
			else
				var row=tbl.insertRow(rows=0?1:rows);
			for (var i=0;i<cols;i++) {
				var newtd=this.getDocument().createElement("td");
				newtd.innerHTML="&nbsp;";
				if (border==0)
					newtd.style.cssText = cssText;
				var col = row.appendChild(newtd);
				if (td.parentNode.childNodes[i].colSpan)
					col.colSpan = td.parentNode.childNodes[i].colSpan;
			}	
			break;
		case "tblinscoll":
			var cols=td.cellIndex;
			for (var j=0;j<tbl.rows.length;j++) {
				var cell=tbl.rows[j].cells[cols];
				if (!cell) break;
				var newtd = this.getDocument().createElement("td");
				newtd.innerHTML = "&nbsp;";
				if (border==0)
					newtd.style.cssText = cssText;
				cell.parentNode.insertBefore(newtd,cell);
			}
			break;
		case "tblinscolr":
			var cols=td.cellIndex;
			for (var j=0;j<tbl.rows.length;j++) {
				var app=false;
				var cell=tbl.rows[j].cells[cols];
				if (cols==tbl.rows[j].cells.length-1)
					app=true;
				else
					cell=tbl.rows[j].cells[cols+1];
				var newtd=this.getDocument().createElement("td");
				newtd.innerHTML = "&nbsp;";
				if (border==0)
					newtd.style.cssText = cssText;
				if (app)
					cell.parentNode.appendChild(newtd);
				else
					cell.parentNode.insertBefore(newtd,cell);
			}
			break;
		case "tbldelrow":
			if (tbl.rows.length <= 1) {
				tbl.parentNode.removeChild(tbl);
				return;
			}
			tr.parentNode.removeChild(tr);
			break;
		case "tbldelcol":	
			var index=td.cellIndex;
			var cols=0;
			for (var j=0;j<tbl.rows.length;j++) {
				if (tbl.rows[j].cells.length>cols)
					cols=tbl.rows[j].cells.length;
			}
			if (cols<=1) {
				tbl.parentNode.removeChild(tbl);
				return;
			}
			for (var j=0;j<tbl.rows.length;j++) {
				var cell=tbl.rows[j].cells[index];
				if (cell)
					cell.parentNode.removeChild(cell);
			}
			break;
		}
	}
}

function sHTML_runCmd(cmd,args) {
	if (this.view) {
		switch(cmd) {
		case "format":
			this.setFormat();
			break;
		case "font":
			this.setFontFace();
			break;
		case "fontsize":
			this.setFontSize();
			break;
		case "date":
		case "time":
			this.insertDateTime(cmd);
			break;
		case "forecolor":
			this.setColor("forecolor");
			break;
		case "backcolor":
			this.setColor("backcolor");
			break;
		case "image":
			this.insertImage(true);
			break;
		case "imgInsert":
			this.insertImage(false,args);
			break;
		case "link":
			this.insertLink(true);
			break;
		case "linkInsert":
			this.insertLink(false,args);
			break;
		case "tblins":
			this.insertTable(true);
			break;
		case "tblopt":
			this.updateTable(true);
			break;
		case "tblInsert":
			this.insertTable(false,args);
			break;
		case "tblUpdate":
			this.updateTable(false,args);
			break;
		case "tblinsrowa":
		case "tblinsrowb":
		case "tbldelrow":
		case "tblinscoll":
		case "tblinscolr":
		case "tbldelcol":
			this.manageTable(cmd);
			break;
		case "hr":
			this.getDocument().execCommand("inserthorizontalrule");
			break;
		default:
			if (cmd!="src" && cmd!="save")
				this.getDocument().execCommand(cmd)
			break;
		}
		shtml.handleEvent();
	}
	if (cmd=="src") {
		var wnd=this.getWindow();
		var doc=this.getDocument();
		if (this.view) {
			this.view=false;
			doc.body.style.fontFamily="Arial";
			doc.body.style.fontSize="8pt";
			this.cleanUp(doc.body,true);
			var txt=doc.body.innerHTML;
			doc.body.innerText=txt;
		}
		else {
			this.view=true;
			doc.body.style.fontFamily="";
			doc.body.style.fontSize="";
			doc.body.innerHTML=doc.body.innerText;		
			this.cleanUp(doc.body,false);					
		}
		this.buttons["btn_src"]=(!this.view);
		this.onToolButtonOut("btn_src");		
		wnd.focus();
		doc.body.createTextRange().collapse(false)
	}
	else if (cmd=="save") {
		if (shtml.view) {
			shtml.cleanUp(shtml.getDocument().body,true);
			shtml.getSourceControl().value=shtml.getDocument().body.innerHTML;
		}
		else
			shtml.getSourceControl().value=shtml.getDocument().body.innerText;
		this.getSourceForm().submit();
	}
}

var shtml=new sHTML();
window.name="shtml_editor";