// Copyright 2002 - Tim McLaughlin, Iteration ZERO, Inc.
// Use only as permitted.
// contact - tim@iterationzero.com 

HTMLBOX_URL='';

document.write('<style>\n\
._enabled 	{background: buttonface; border: 1px solid buttonface; margin: 0;}\n\
._disabled 	{background: buttonface; border: 1px solid buttonface; margin: 0;filter:alpha(OPACITY=40);}\n\
._raised	{border-top: 1px solid buttonhighlight; border-left: 1px solid buttonhighlight; border-bottom: 1px solid buttonshadow; border-right: 1px solid buttonshadow; background: buttonface; margin: 0;}\n\
._pressed	{border-top: 1px solid buttonshadow;border-left: 1px solid buttonshadow;border-bottom: 1px solid buttonhighlight;border-right: 1px solid buttonhighlight;background: buttonface;margin: 0;}\n\
._latched	{border: 1px inset;background:#cccccc;}\n\
</style>');

function _mover(e) { if(!e.disabled && (e.className!='_latched')) e.className='_raised'; }
function _mout(e) { if(!e.disabled && (e.className!='_latched')) e.className='_enabled'; }
function _mdown(e) { if(!e.disabled && (e.className!='_latched')) e.className='_pressed'; }
function _mup(e) { if(!e.disabled && (e.className!='_latched')) e.className='_enabled'; }

function HTMLBox(name, source, width, height, stylesheet, styles, hideButtons) {
this.source=source;
this.oldsubmit=source.form.onsubmit;
this.WYSIWYG=((navigator.platform.indexOf('Win')>-1) && (parseInt(navigator.appVersion.split('MSIE')[1])>=5));
if(this.WYSIWYG) {
function hidden(button) {
	if(!hideButtons) return false;
	for(var i=0;i<hideButtons.length;i++) if(hideButtons[i]==button) return true;
	return false;
}
this.name=name;
this.stylesheet=stylesheet;
this.mode=1;
this.menuAction=new Array();
document.write('\
<TABLE border=1 style="border-style:inset;" cellspacing=0 width="'+(width==0?'100%':width)+'" cellpadding=0 bgcolor="buttonface">\n\
<TR><TD>\n\
<SELECT name=currentClass id=currentClass style="display:' + ((hidden('style')||!stylesheet)?'none':'inline') + '" onclick="'+name+'.setClass(document.all.currentClass.value);"><option value=""></option>\n');
for(var style in styles) {
	document.write('<OPTION value="' + style + '">' + styles[style] + '</OPTION>\n');
}
document.write('</SELECT>\n\
<IMG id=cutButton alt="Cut (Ctrl+X)" src="/htmlbox/images/cut.gif" class=_enabled onClick="'+name+'.exec(5003);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=copyButton alt="Copy (Ctrl+C)" src="/htmlbox/images/copy.gif" class=_enabled onClick="'+name+'.exec(5002);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=pasteButton alt="Paste (Ctrl+V)" src="/htmlbox/images/paste.gif" class=_enabled onClick="'+name+'.exec(5032);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG src="/htmlbox/images/sep.gif" width=4 height=20 border=0 align=absbottom>\
<IMG id=boldButton alt="Bold (Ctrl+B)" src="/htmlbox/images/bold.gif" class=_enabled onClick="'+name+'.exec(5000);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=italicButton alt="Italic (Ctrl+I)" src="/htmlbox/images/italic.gif" class=_enabled onClick="'+name+'.exec(5023);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=underButton alt="Underline (Ctrl+U)" src="/htmlbox/images/under.gif" class=_enabled onClick="'+name+'.exec(5048);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG src="/htmlbox/images/sep.gif" width=4 height=20 border=0 align=absbottom>\
<IMG id=leftButton alt="Align Left" src="/htmlbox/images/left.gif" class=_enabled onClick="'+name+'.setAlign(5025);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=centerButton alt="Align Center" src="/htmlbox/images/center.gif" class=_enabled onClick="'+name+'.setAlign(5024);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=rightButton alt="Align Right" src="/htmlbox/images/right.gif" class=_enabled onClick="'+name+'.setAlign(5026);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG src="/htmlbox/images/sep.gif" width=4 height=20 border=0 align=absbottom>\n\
<IMG id=orderlistButton alt="Numbered List" src="/htmlbox/images/numbered.gif" class=_enabled onClick="'+name+'.exec(5030);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=unorderlistButton alt="Bulleted List" src="/htmlbox/images/bullets.gif" class=_enabled onClick="'+name+'.exec(5051);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=outdentButton alt="Outdent (Ctrl+Shift+T)" src="/htmlbox/images/outdent.gif" class=_enabled onClick="'+name+'.exec(5031);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);"  onmouseup="_mup(this);">\
<IMG id=indentButton alt="Indent (Ctrl+T)" src="/htmlbox/images/indent.gif" class=_enabled onClick="'+name+'.exec(5018);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG src="/htmlbox/images/sep.gif" width=4 height=20 border=0 align=absbottom>\
<IMG id=hyperlinkButton alt="Link (Ctrl+L)" src="/htmlbox/images/link.gif" class=_enabled onClick="'+name+'.exec(5016);" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=imageButton alt="Insert Image" src="/htmlbox/images/image.gif" style="display:' + (hidden('image')?'none':'inline') + '" class=_enabled onClick="'+name+'.addImage();" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=tableButton alt="Insert Table" src="/htmlbox/images/table.gif" style="display:' + (hidden('table')?'none':'inline') + '" class=_enabled onClick="'+name+'.addTable();" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\
<IMG id=tableBordersButton alt="Show Borders" src="/htmlbox/images/borders.gif" style="display:' + (hidden('table')?'none':'inline') + '" class=_enabled onClick="'+name+'.toggleBorders();" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">'
+ (hidden('mode')?'':'<IMG src="/htmlbox/images/sep.gif" width=4 height=20 border=0 align=absbottom>\
<IMG id=modeButton alt="Switch Mode" src="/htmlbox/images/mode.gif" class=_enabled onClick="'+name+'.toggleMode();" align=absbottom onmouseover="_mover(this);" onmouseout="_mout(this);" onmousedown="_mdown(this);" onmouseup="_mup(this);">\n')
+ '</TD></TR><tr><td>\n\
<OBJECT classid="clsid:2D360201-FFF5-11D1-8D03-00A0C959BC0A" id="'+name+'_HTMLBox" name="'+name+'_HTMLBox" style="width:100%;" height='+(height-54)+'></OBJECT>\n\
<SCRIPT LANGUAGE="JavaScript" for="'+name+'_HTMLBox" event=Displaychanged>eval("'+name+'.displayChanged()")<\/SCRIPT>\n\
<SCRIPT language="JavaScript" for="'+name+'_HTMLBox" event=ContextMenuAction(i)>eval("'+name+'.contextMenuAction("+i+")")<\/SCRIPT>\n\
<SCRIPT language="JavaScript" for="'+name+'_HTMLBox" event=ShowContextMenu>eval("'+name+'.showContextMenu()")<\/SCRIPT>\n\
</td></tr></TABLE>');
//<SCRIPT language="JavaScript" for="'+this.source.form.name+'" event=onsubmit>return eval("'+name+'.save()")<\/SCRIPT>\n\
this.editor=document.all[name+'_HTMLBox'];
var re = new RegExp('(<img[^>]* src=")(/[^>]+)', 'gi');
var value = source.value ? unescape(source.value).replace(re,"$1http://" + window.location.host + "$2") : '<div></div>';
this.editor.DocumentHTML='<html><head><link rel="stylesheet" type="text/css" href="'+stylesheet+'"></head><body>'+value+'</body></html>';
} else {
//source.form.onsubmit=function() {return eval(name+'.save()')};
document.write('<textarea name="'+name+'_HTMLBox" rows='+height/10+' cols='+width/8+' wrap="virtual" style="height:'+height+';width:'+(width==0?'100%':width+'px')+';">' + this.source.value + '</textarea>');
this.editor=source.form.elements[name+'_HTMLBox'];
}
}

HTMLBox.prototype.setClass = function(className) {
	function setNodeClass(node, className) {
		var kids = node.children;
		if(kids && kids.length>0) {
			for(var i=0; i<kids.length; i++) {
				setNodeClass(kids[i], className);
			}
		}
		if(node.nodeName=='DIV' || node.nodeName=='P' || node.nodeName=='TD')
			node.className=className;
	}
	if(window.event.offsetY>document.all.currentClass.clientHeight) {
		var r = this.editor.DOM.selection.createRange();
		if(r.parentElement) {
			var e = r.parentElement();
			setNodeClass(e, className);
			this.cleanup();
		}
	}
}

HTMLBox.prototype.addImage = function() {
    if(this.editor.QueryStatus(5017)==3) {
        window.open('/htmlbox/add_image.htm?htmlbox='+this.name+'&location='+escape(window.location.pathname+window.location.search),"dialog",'width=320,height=125');
	}
}
HTMLBox.prototype.insertImage = function(url, alt) {
    if(this.editor.QueryStatus(5017)==3) {
		this.editor.ExecCommand(5017,2,url);
		var img=this.editor.DOM.selection.createRange().commonParentElement();
		img.border=0;
		img.alt=alt;
	}
}

HTMLBox.prototype.addTable = function() {
    if(this.editor.QueryStatus(5022)==3) {	
        var s = window.showModalDialog("/htmlbox/add_table.htm",[this.stylesheet],'dialogWidth:350px;dialogHeight:270px;help:no;center:yes;status:no');
        if(s) {
			var params=s.split(',');
			var rows=parseInt(params[0]);
			var cols=parseInt(params[1]);
			var headers=parseInt(params[2]);
			var s='<TABLE '+params[3]+'>';
			s+='<THEAD>';
			for(var i=0;i<headers;i++) {
				s+='<TR>';
				for(var j=0;j<cols;j++)
					s+='<TD valign="top"></TD>';
				s+='</TR>';
			}
			s+='</THEAD>';
			for(var i=0;i<rows;i++) {
				s+='<TR>';
				for(var j=0;j<cols;j++)
					s+='<TD valign="top"></TD>';
				s+='</TR>';
			}
			s+='</TABLE>';
			var rg = this.editor.DOM.selection.createRange();
			rg.pasteHTML(s);
		}
    }
	this.cleanup();
}

HTMLBox.prototype.toggleBorders = function() {
	this.editor.ShowBorders = !this.editor.ShowBorders;
	document.all.tableBordersButton.className=(this.editor.ShowBorders?'_latched':'_enabled');
	this.cleanup();
}

HTMLBox.prototype.exec = function(action) {
    if(this.editor.QueryStatus(action)==3||this.editor.QueryStatus(action)==7)
        this.editor.ExecCommand(action);
	this.cleanup();
}

HTMLBox.prototype.setAlign = function(action) {
	var rg = this.editor.DOM.selection.createRange();
	var e = (rg.parentElement)?rg.parentElement():rg.commonParentElement();
	if(e.nodeName=='IMG') {
		switch(action) {
			case 5025:
				e.align = 'left'; break;
			case 5024:
				e.align = 'center'; break;
			case 5026:
				e.align = 'right';
		}
	} else {
		if(this.editor.QueryStatus(action)==3||this.editor.QueryStatus(action)==7)
			this.editor.ExecCommand(action);
	}
	this.cleanup();
}

HTMLBox.prototype.cleanup = function() {
	window.event.cancelBubble=true;
	this.editor.focus();
}

HTMLBox.prototype.toggleMode = function (mode) {
	function disable(e,disabled) {
        e.className=disabled?'_disabled':'_enabled';
        e.disabled=disabled;
	}
	var all=document.all;
	if(this.mode==0||this.mode==2) {
		disabled=this.mode==2;
		all.currentClass.disabled=disabled;
	    disable(all.boldButton,disabled);
	    disable(all.italicButton,disabled);
		disable(all.underButton,disabled);
	    disable(all.leftButton,disabled);
	    disable(all.centerButton,disabled);
	    disable(all.rightButton,disabled);
	    disable(all.orderlistButton,disabled);
	    disable(all.unorderlistButton,disabled);
	    disable(all.outdentButton,disabled);
		disable(all.indentButton,disabled);
	    disable(all.hyperlinkButton,disabled);
	    disable(all.imageButton,disabled);
	    disable(all.tableButton,disabled);
		disable(all.tableBordersButton,disabled);
	}
	if(this.mode==0) {
		if(this.editor.DOM.body.innerText!='')
	        this.editor.DOM.body.innerHTML=this.editor.FilterSourceCode(this.editor.DOM.body.innerText);
		this.mode=1;
    } else if(this.mode==1) {
		this.editor.ShowDetails=true;
		this.mode=2;
	} else {
		this.editor.ShowDetails=false;
		if(this.editor.DOM.body.innerHTML!='')
	        this.editor.DOM.body.innerText=this.editor.FilterSourceCode(this.editor.DOM.body.innerHTML);
        this.mode=0;
	}
	this.cleanup();
}

HTMLBox.prototype.save = function() {
	if(this.WYSIWYG) {
		if(document.forms.length>0 && this.editor.DOM) {
			var value='';
			if(this.mode==0 && this.editor.DOM.body.innerText!='') value=this.editor.DOM.body.innerText;
			else if(this.editor.DOM.body.innerHTML!='') value=this.editor.FilterSourceCode(this.editor.DOM.body.innerHTML);
			var re = new RegExp('&nbsp;|<div[^>]*>|</div>|\s','gi');
			if(value.replace(re,'')=='') {
				this.source.value = '';
			} else {
				re = new RegExp('(<img[^>]* src=")http://' + window.location.host + '(/[^>]+)', 'gi');
				this.source.value = value.replace(re,"$1$2");
			}
		}
	} else {
		this.source.value=this.editor.value;
	}
}

HTMLBox.prototype.showContextMenu = function () {
	var menuText=new Array(), menuState=new Array(), menuSpace='', i=0;
	var menuAction=this.menuAction, editor=this.editor;
	var rg = this.editor.DOM.selection.createRange();
	var e = (rg.parentElement)?rg.parentElement():rg.commonParentElement();
	function addSpace(section) {
		if(menuSpace!='' && menuSpace!=section) {
			menuAction[i]=0;
			menuText[i]='';
			menuState[i++]=0;
		}
	}
	function addMenu(section, action, desc) {
		if(editor.QueryStatus(action)==3) {
			addSpace(section);
			menuAction[i]=action;
			menuText[i]=desc;
			menuState[i++]=0;
			menuSpace=section;
		}
	}
	addMenu('cb', 5003, 'Cut');
	addMenu('cb', 5002, 'Copy');
	addMenu('cb', 5032, 'Paste');
	while(e.nodeName!='BODY' && e.nodeName!='TR' && e.nodeName!='TABLE')	e=e.parentElement;
	if(e.nodeName=='TR') {
		addSpace('ht');
		menuAction[i]=-2;
		menuText[i]='Highlight Row';
		menuState[i++]=(e.className=='hilite')?1:0;
		menuSpace='ht';
	}	
	addMenu('it', 5021, 'Insert Row');
	addMenu('it', 5020, 'Insert Column');
	addMenu('dt', 5007, 'Delete Rows');
	addMenu('dt', 5006, 'Delete Columns');
	addMenu('mt', 5029, 'Merge Cells');
	addMenu('mt', 5047, 'Split Cells');
	if(this.editor.DOM.selection.type=="Control") {
		switch(e.nodeName) {
			case 'TABLE':
				addSpace('prop');
				menuAction[i]=-1;
				menuText[i]='Properties';
				menuState[i++]=0;			
				break;
		}
	}
	this.editor.SetContextMenu(menuText,menuState);
}
HTMLBox.prototype.contextMenuAction = function(itemIndex) {
	if(this.menuAction[itemIndex]>=0) {
		this.editor.ExecCommand(this.menuAction[itemIndex]);
	} else {
		switch(this.menuAction[itemIndex]) {
			case -1:
				var e = this.editor.DOM.selection.createRange().commonParentElement();
				var s = window.showModalDialog("/htmlbox/edit_table.htm",[this.stylesheet, e.cellPadding,e.cellSpacing,e.border,e.bgColor,e.width,e.height,e.className],'dialogWidth:310px;dialogHeight:125px;help:yes;center:yes;status:no');
				if(s) {
					var params = s.split(',');
					e.clearAttributes();
					e.cellPadding = parseInt(params[0]);
					e.cellSpacing = parseInt(params[1]);
					e.border = parseInt(params[2]);
					e.bgColor=params[3];
					e.width=params[4];
					e.height=params[5];
					e.className=params[6];
				}
				break;
			case -2:
				var e = this.editor.DOM.selection.createRange().parentElement();
				while(e.nodeName!='TR')	e=e.parentElement;
				e.className=(e.className=='hilite')?'':'hilite';
				break;
		}
	}
}
HTMLBox.prototype.updateButton = function(e,cmd) {
	switch(this.editor.QueryStatus(cmd)) {
	    case 1:
	        e.className='_disabled';
	        e.disabled=true;
			break;
		case 3:
	        e.className='_enabled';
	        e.disabled=false;
			break;
		case 7:
			e.className='_latched';
	        e.disabled=false;
			break;
	}
}
HTMLBox.prototype.displayChanged = function() {
	if(this.editor.Busy==false) {	
		var all=document.all;
		if(this.mode!=0) {
			var t = this.editor.DOM.selection.type;
			if(t!='None') {
				var rg = this.editor.DOM.selection.createRange();
				var e = (rg.parentElement)?rg.parentElement():rg.commonParentElement();	
			}
			var c = all.currentClass;
			var l = c.length;
		    if(l>0 && t=="Text") {
				var cl = e.className;
				c[0].selected=true;
				for(var i=1; i<l; i++) {
					o=c[i];
					if(o.value==cl) {
						o.selected=true;
						break;
					}
				}
		    }
		    this.updateButton(all.boldButton,5000);
		    this.updateButton(all.italicButton,5023);
			this.updateButton(all.underButton,5048);
			if(t=='Control' && e.nodeName=='IMG') {
				all.leftButton.className = '_enabled'; 
				all.rightButton.className = '_enabled';
				all.leftButton.className = '_enabled';
				switch(e.align) {
					case 'left':
						all.leftButton.className = '_latched'; break;
					case 'center':
						all.centerButton.className = '_latched'; break;
					case 'right':
						all.rightButton.className = '_latched';
				}
			} else {
			    this.updateButton(all.leftButton,5025);
			    this.updateButton(all.centerButton,5024);
			    this.updateButton(all.rightButton,5026);
			}
		    this.updateButton(all.cutButton,5003);
		    this.updateButton(all.pasteButton,5032);
		    this.updateButton(all.copyButton,5002);
		    this.updateButton(all.hyperlinkButton,5016);
		    this.updateButton(all.imageButton,5017);
		    this.updateButton(all.tableButton,5022);
		}
		this.updateButton(all.cutButton,5003);
		this.updateButton(all.pasteButton,5032);
		this.updateButton(all.copyButton,5002);
	}
}


