// DHTML Editing Component Constants for JavaScript
// Copyright 1998 Microsoft Corporation.  All rights reserved.

DECMD_BOLD =                      5000;
DECMD_COPY =                      5002;
DECMD_CUT =                       5003;
DECMD_DELETE =                    5004;
DECMD_DELETECELLS =               5005;
DECMD_DELETECOLS =                5006;
DECMD_DELETEROWS =                5007;
DECMD_FINDTEXT =                  5008;
DECMD_FONT =                      5009;
DECMD_GETBACKCOLOR =              5010;
DECMD_GETBLOCKFMT =               5011;
DECMD_GETBLOCKFMTNAMES =          5012;
DECMD_GETFONTNAME =               5013;
DECMD_GETFONTSIZE =               5014;
DECMD_GETFORECOLOR =              5015;
DECMD_HYPERLINK =                 5016;
DECMD_IMAGE =                     5017;
DECMD_INDENT =                    5018;
DECMD_INSERTCELL =                5019;
DECMD_INSERTCOL =                 5020;
DECMD_INSERTROW =                 5021;
DECMD_INSERTTABLE =               5022;
DECMD_ITALIC =                    5023;
DECMD_JUSTIFYCENTER =             5024;
DECMD_JUSTIFYLEFT =               5025;
DECMD_JUSTIFYRIGHT =              5026;
DECMD_LOCK_ELEMENT =              5027;
DECMD_MAKE_ABSOLUTE =             5028;
DECMD_MERGECELLS =                5029;
DECMD_ORDERLIST =                 5030;
DECMD_OUTDENT =                   5031;
DECMD_PASTE =                     5032;
DECMD_REDO =                      5033;
DECMD_REMOVEFORMAT =              5034;
DECMD_SELECTALL =                 5035;
DECMD_SEND_BACKWARD =             5036;
DECMD_BRING_FORWARD =             5037;
DECMD_SEND_BELOW_TEXT =           5038;
DECMD_BRING_ABOVE_TEXT =          5039;
DECMD_SEND_TO_BACK =              5040;
DECMD_BRING_TO_FRONT =            5041;
DECMD_SETBACKCOLOR =              5042;
DECMD_SETBLOCKFMT =               5043;
DECMD_SETFONTNAME =               5044;
DECMD_SETFONTSIZE =               5045;
DECMD_SETFORECOLOR =              5046;
DECMD_SPLITCELL =                 5047;
DECMD_UNDERLINE =                 5048;
DECMD_UNDO =                      5049;
DECMD_UNLINK =                    5050;
DECMD_UNORDERLIST =               5051;
DECMD_PROPERTIES =                5052;

OLECMDEXECOPT_DODEFAULT =         0;
OLECMDEXECOPT_PROMPTUSER =        1;
OLECMDEXECOPT_DONTPROMPTUSER =    2;

DECMDF_NOTSUPPORTED =             0;
DECMDF_DISABLED =                 1;
DECMDF_ENABLED =                  3;
DECMDF_LATCHED =                  7;
DECMDF_NINCHED =                  11;

DEAPPEARANCE_FLAT =               0;
DEAPPEARANCE_3D =                 1;

OLE_TRISTATE_UNCHECKED =          0;
OLE_TRISTATE_CHECKED =            1;
OLE_TRISTATE_GRAY =               2;

// End DHTML Component Constants

var is_ie4 = ((parseInt(navigator.appVersion)  == 4) && (navigator.userAgent.toLowerCase().indexOf("msie 5")==-1) && (navigator.userAgent.toLowerCase().indexOf("msie 6")==-1) );

var dhtml_Fieldname;
var dhtml_HTMLMode=false; //true if current control is in html mode

// globals for the context menus
var MENU_SEPARATOR = "";
var ContextMenu = new Array();
var GeneralContextMenu = new Array();
var TableContextMenu = new Array();

//define context menu elements
var genId = 0;
var tblId = 0;
GeneralContextMenu[genId++] = new ContextMenuItem("Select All", DECMD_SELECTALL);
GeneralContextMenu[genId++] = new ContextMenuItem("Cut", DECMD_CUT);
GeneralContextMenu[genId++] = new ContextMenuItem("Copy", DECMD_COPY);
GeneralContextMenu[genId++] = new ContextMenuItem("Paste", DECMD_PASTE);
GeneralContextMenu[genId++] = new ContextMenuItem("Undo", DECMD_UNDO);
GeneralContextMenu[genId++] = new ContextMenuItem("Redo", DECMD_REDO);
GeneralContextMenu[genId++] = new ContextMenuItem(MENU_SEPARATOR, 0);
//GeneralContextMenu[genId++] = new ContextMenuItem("Bold", DECMD_BOLD);
//GeneralContextMenu[genId++] = new ContextMenuItem("Italic", DECMD_ITALIC);
//GeneralContextMenu[genId++] = new ContextMenuItem("Underline", DECMD_UNDERLINE);
//GeneralContextMenu[genId++] = new ContextMenuItem("Font", DECMD_FONT);
//GeneralContextMenu[genId++] = new ContextMenuItem("Remove Format", DECMD_REMOVEFORMAT);
//GeneralContextMenu[genId++] = new ContextMenuItem(MENU_SEPARATOR, 0);
//GeneralContextMenu[genId++] = new ContextMenuItem("Left", DECMD_JUSTIFYLEFT);
//GeneralContextMenu[genId++] = new ContextMenuItem("Center", DECMD_JUSTIFYCENTER);
//GeneralContextMenu[genId++] = new ContextMenuItem("Right", DECMD_JUSTIFYRIGHT);
//GeneralContextMenu[genId++] = new ContextMenuItem(MENU_SEPARATOR, 0);
GeneralContextMenu[genId++] = new ContextMenuItem("Delete", DECMD_DELETE);
GeneralContextMenu[genId++] = new ContextMenuItem("Link", DECMD_HYPERLINK);
GeneralContextMenu[genId++] = new ContextMenuItem("Unlink", DECMD_UNLINK);
GeneralContextMenu[genId++] = new ContextMenuItem("Image", DECMD_IMAGE);
GeneralContextMenu[genId++] = new ContextMenuItem(MENU_SEPARATOR, 0);
GeneralContextMenu[genId++] = new ContextMenuItem("Table", DECMD_INSERTTABLE);
TableContextMenu[tblId++] = new ContextMenuItem(MENU_SEPARATOR, 0);
TableContextMenu[tblId++] = new ContextMenuItem("Insert Row", DECMD_INSERTROW);
TableContextMenu[tblId++] = new ContextMenuItem("Delete Rows", DECMD_DELETEROWS);
TableContextMenu[tblId++] = new ContextMenuItem(MENU_SEPARATOR, 0);
TableContextMenu[tblId++] = new ContextMenuItem("Insert Column", DECMD_INSERTCOL);
TableContextMenu[tblId++] = new ContextMenuItem("Delete Columns", DECMD_DELETECOLS);
TableContextMenu[tblId++] = new ContextMenuItem(MENU_SEPARATOR, 0);
TableContextMenu[tblId++] = new ContextMenuItem("Insert Cell", DECMD_INSERTCELL);
TableContextMenu[tblId++] = new ContextMenuItem("Delete Cells", DECMD_DELETECELLS);
TableContextMenu[tblId++] = new ContextMenuItem("Merge Cells", DECMD_MERGECELLS);
TableContextMenu[tblId++] = new ContextMenuItem("Split Cell", DECMD_SPLITCELL);
GeneralContextMenu[genId++] = new ContextMenuItem("Find", DECMD_FINDTEXT);
GeneralContextMenu[genId++] = new ContextMenuItem(MENU_SEPARATOR, 0);
//GeneralContextMenu[genId++] = new ContextMenuItem("OL", DECMD_ORDERLIST);
//GeneralContextMenu[genId++] = new ContextMenuItem("UL", DECMD_UNORDERLIST);
//GeneralContextMenu[genId++] = new ContextMenuItem("Indent", DECMD_INDENT);
//GeneralContextMenu[genId++] = new ContextMenuItem("Outdent", DECMD_OUTDENT);
GeneralContextMenu[genId++] = new ContextMenuItem("Properties", DECMD_PROPERTIES);

// initializes the control
function dhtml_initialize() {
	thisContentItem=eval("document.all.dhtml_body_content");
	thisForm=thisContentItem;
	// make sure that we have found the outermost form tag
	while(thisForm.tagName!="FORM"&&thisForm.tagName!="HTML") {
		thisForm=thisForm.parentElement; 
	}
	if(thisForm.tagName!="HTML") {		
		DHTMLSafe = eval("thisForm.DHTMLSafe");
		dhtml_Fieldname = thisContentItem.name;		
		// bind form onSubmit event to dhtml control onsubmit event()
		thisForm.onsubmit = dhtml_onSubmit;
	}
	DHTMLSafe.NewDocument();
	DHTMLSafe.BaseURL=dhtml_baseurl;
 	dhtml_setStates();
	dhtml_onLoad();
	dhtml_HTMLMode = false;
}

// populate the dhtml control and related elements
function dhtml_onLoad() {
	// wait a bit if a component is still loading
	if(DHTMLSafe.Busy) {
		setTimeout("dhtml_onLoad("+");", 100);	
		return;
	}
	// bind this function to the window unload
	window.onunload = dhtml_onUnload;
	// pull the available html elements from the dhtml control 
	if(oSel=eval('document.all.lstStyle')) { //Populate QuickFormat Box
		var f=new ActiveXObject("DEGetBlockFmtNamesParam.DEGetBlockFmtNamesParam");
	    DHTMLSafe.ExecCommand(DECMD_GETBLOCKFMTNAMES,OLECMDEXECOPT_DODEFAULT,f);
		vbarr = new VBArray(f.Names);
		arr = vbarr.toArray();
		for (var i=0;i<arr.length;i++) {
			oOption = document.createElement("OPTION");
			oSel.options.add(oOption);
			oOption.text=arr[i];
			oOption.name=arr[i];
		}
	}
	// pull the current page from the textarea element
	thisContentItem=eval("document.all.dhtml_body_content");
	// check for an empty form and reset the dhtml control
	if(thisContentItem.value.length)
		DHTMLSafe.DOM.body.innerHTML = thisContentItem.value;	
	else
		DHTMLSafe.DOM.body.innerHTML = " ";

///////////////////// todo
	// cant get a style sheet to load yet, may not be possible
	//thisContentItem=eval("document.all.dhtml_head_content");
	//if(thisContentItem.value.length)
		//DHTMLSafe.DOM.body.insertAdjacentHTML ("AfterBegin", thisContentItem.value);	

	// modify the form elements to match the new content
	dhtml_setStates;
}

// the train is leaving the station
function dhtml_onUnload() {
	var dhtml_content = "";
	thisContentItem = eval("document.all.dhtml_body_content");
	// come out of source view
	if(dhtml_HTMLMode) dhtml_editsource(false);
	var content = DHTMLSafe.DOM.body.innerHTML;
	// push the content back into the textarea element	
	thisContentItem.value = content;
}

///////////////////////// todo
//////////////// not doing this from the form anymore, but may leave in to cover
// the train is leaving the station, but the form is calling
function dhtml_onSubmit() {
	var dhtml_content = "";
	// come out of source view
	if(dhtml_HTMLMode) dhtml_editsource(false);
	thisContentItem = eval("document.all.dhtml_body_content");
	// push the content back into the textarea element	
	dhtml_content = dhtml_onBeforeSave(DHTMLSafe, dhtml_Fieldname);
	thisContentItem.value = dhtml_content;
}

// onmousedown event listener
function dhtml_onmousedown() {
// nothing exciting to do here
}

// onclick event listener
function dhtml_onclick() {
	// modify the form elements to match the new content
	dhtml_setStates;
}

// ondisplaychange event listener
function dhtml_ondisplaychange() {
	// modify the form elements to match the new content
 	dhtml_setStates();
}

// onmenuaction event listener
function dhtml_onmenuaction(itemIndex) {
	// handle the selected menu command
	dhtml_onCommand(ContextMenu[itemIndex].cmdId);
}

// this bad boy does all the work
function dhtml_onCommand(cmdId) {
	//no commands should work in html mode
	if (dhtml_HTMLMode) { return; }
	// handle the command
	switch(parseInt(cmdId)) {
		case DECMD_IMAGE:
			onImage();
			break;
		case DECMD_INSERTTABLE:
			insertTable();
			break;
		default:
			if(DHTMLSafe.QueryStatus(cmdId)!=DECMDF_DISABLED) {
				DHTMLSafe.ExecCommand(cmdId, OLECMDEXECOPT_DODEFAULT);
			}
			break;
	}
	DHTMLSafe.focus();
}

// mod all the form elements from the dhtml control	
function dhtml_setStates() {
	var pbtn;
	var cid;
	var state;
	// trap the unsafe status
	if(DHTMLSafe.QueryStatus(5002)!=DHTMLSafe.QueryStatus(5003)) return;
	// adjust the font pulldown
	if(oSel=eval('document.all.lstFont')){
	if(DHTMLSafe.QueryStatus(DECMD_SETFONTNAME)!=DECMDF_DISABLED&&DHTMLSafe.DOM.selection.type!="control") {
		if(oSel.disabled) oSel.disabled=false;
			fontname=DHTMLSafe.ExecCommand(DECMD_GETFONTNAME);
			if(fontname!=""&&fontname!=null) {
				fCreateNew=true;
				window.status=fontname;
				for(i=0;i<oSel.options.length;i++) {
					if(oSel.options[i].name==fontname) {
						if(!oSel.options[i].selected)
							oSel.options[i].selected=true;
						fCreateNew=false;
					}
				}
				// add a new font to the list if it doesn't exist
				if(fCreateNew) {
					var oOption = document.createElement("OPTION");
					oSel.options.add(oOption,0);
					oOption.text = fontname;
					oOption.name = fontname;
					oOption.selected=true;
				}
			}
			// default to a blank pulldown
			else {
				oSel.selectedIndex=-1;
			}
		}
		else {
			if(!oSel.disabled) oSel.disabled=true;
		}
	}
	// adjust the font size pulldown
	if(oSel=eval('document.all.lstFontSize')){
		if(DHTMLSafe.QueryStatus(DECMD_SETFONTSIZE)!=DECMDF_DISABLED&&DHTMLSafe.DOM.selection.type!="control") {
			if(oSel.disabled) oSel.disabled=false; 
			fs=DHTMLSafe.ExecCommand(DECMD_GETFONTSIZE);
			if(fs!=null&&fs!="") {
				for(i=0;i<oSel.options.length;i++) {
					if(oSel.options[i].name=="dhtml_fs"+fs&&!oSel.options[i].selected)
						oSel.options[i].selected=true;
				}
			}
			// default to a blank pulldown
			else {
				oSel.selectedIndex=-1;
			}
		}
		else {
			if(!oSel.disabled) oSel.disabled=true;
		}
	}	
	// adjust the style pulldown
	if(oSel=eval('document.all.lstStyle')){
		if(DHTMLSafe.QueryStatus(DECMD_SETBLOCKFMT)!=DECMDF_DISABLED&&DHTMLSafe.DOM.selection.type!="control") {
			if(oSel.disabled) oSel.disabled=false;
			fmt=DHTMLSafe.ExecCommand(DECMD_GETBLOCKFMT);
			if(fmt!=""&&fmt!=null) {
				for(i=0;i<oSel.options.length;i++) {
					if(oSel.options[i].name==fmt&&!oSel.options[i].selected)
						oSel.options[i].selected=true;
				}
			}
			// default to a blank pulldown
			else {
				oSel.selectedIndex=-1;
			}
		}
		else {
			if(!oSel.disabled) oSel.disabled=true;
		}
	}	
}

// hellish function for creating default table params
function insertTable() {
	var tableparamobj=new ActiveXObject("DEInsertTableParam.DEInsertTableParam");
	tableparamobj.NumRows=3;
	tableparamobj.NumCols=3;   
	tableparamobj.Caption="";
	tableparamobj.TableAttrs='width="80%"';
	tableparamobj.CellAttrs="";
	DHTMLSafe.ExecCommand(DECMD_INSERTTABLE,OLECMDEXECOPT_DODEFAULT, tableparamobj);    
}

// called for image handling
function onImage() {
	//insert the image using the default handler
	//DHTMLSafe.ExecCommand(DECMD_IMAGE,OLECMDEXECOPT_DONTPROMPTUSER,u);
	DHTMLSafe.ExecCommand(DECMD_IMAGE,OLECMDEXECOPT_DODEFAULT,'image.jpg');
////////////////// todo
//////////// this is nice, but we need to handle this from the existing image library
//////////// check with shaun for the image library call
	//DHTMLSafe.DOM.selection.createRange().collapse(false);
	//var img = "<img src=\"" + u + "\" align=\"" + ialign + "\" alt=\"" + ialt + "\">";
	//DHTMLSafe.DOM.selection.createRange().pasteHTML(img);
}

// this is our opportunity to clean up the code
function dhtml_onBeforeSave(dhtmlObject) {

////////////////////// todo	
////////// might be easier/safer to do on the server
////////// check with shaun to see if we can handle as part of the error handling or something

	//for images
	for (var i=0;i<dhtmlObject.DOM.images.length;i++) {
		hs=dhtmlObject.DOM.images[i].getAttribute("STYLE",0).height;
		ws=dhtmlObject.DOM.images[i].getAttribute("STYLE",0).width;
		if(hs.length) {
			dhtmlObject.DOM.images[i].removeAttribute("HEIGHT", 0); 			
			dhtmlObject.DOM.images[i].setAttribute("HEIGHT", replaceString("px", "", hs),0); 
			dhtmlObject.DOM.images[i].getAttribute("STYLE",0).removeAttribute("HEIGHT",0);
		}
		if(ws.length) {
			dhtmlObject.DOM.images[i].removeAttribute("WIDTH", 0);	
			dhtmlObject.DOM.images[i].setAttribute("WIDTH", replaceString("px", "", ws),0); 
			dhtmlObject.DOM.images[i].getAttribute("STYLE",0).removeAttribute("WIDTH",0);
		}
	}
	//for tables
	for (var k=0;k<dhtmlObject.DOM.all.tags("TABLE").length;k++) {
		hs=dhtmlObject.DOM.all.tags("TABLE").item(k).getAttribute("STYLE",0).height;
		ws=dhtmlObject.DOM.all.tags("TABLE").item(k).getAttribute("STYLE",0).width;
		if(hs.length) {
			dhtmlObject.DOM.all.tags("TABLE").item(k).removeAttribute("HEIGHT", 0); 			
			dhtmlObject.DOM.all.tags("TABLE").item(k).setAttribute("HEIGHT", replaceString("px", "", hs),0); 
			dhtmlObject.DOM.all.tags("TABLE").item(k).getAttribute("STYLE",0).removeAttribute("HEIGHT",0);
		}
		if(ws.length) {
			dhtmlObject.DOM.all.tags("TABLE").item(k).removeAttribute("WIDTH", 0);	
			dhtmlObject.DOM.all.tags("TABLE").item(k).setAttribute("WIDTH", replaceString("px", "", ws),0); 
			dhtmlObject.DOM.all.tags("TABLE").item(k).getAttribute("STYLE",0).removeAttribute("WIDTH",0);
		}
	}
	// use DOM to filter out just what's inside the body
	var content = dhtmlObject.DOM.body.innerHTML;
	if(content.length) {
		content = dhtmlObject.FilterSourceCode(content);
	}

////////////////////// todo	
////////// might be easier/safer to do on the server
////////// check with shaun to see if we can handle as part of the error handling or something
	// XHTML compliance stuff will go here:
	// convert img and br tags
	// change all tags and params to lowercase
	
	// i have no idea where this comes from, perhaps unicode? 
	replaceString("&#65279;", " ", content); 
	return content;
}

// mirrors the VB replace function, another great reason to handle this on the server
function replaceString(oldS,newS,fullS) {
	for (var i=0; i<fullS.length; i++) {
 		if (fullS.substring(i,i+oldS.length) == oldS) {
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
		}
	}
 	return fullS;
}

// switches the dhtml control to a simulated HTML view
function dhtml_editsource(bVal) {
	// dont bother if we're already there
	if(dhtml_HTMLMode==bVal) return;
	thisContentItem=eval("document.all.dhtml_body_content");
	DHTMLSafe.DOM.selection.empty();
	// switching to WYSIWYG view
	if(dhtml_HTMLMode) {
		DHTMLSafe.DOM.body.style.fontFamily="";	
		DHTMLSafe.DOM.body.style.fontSize="";
		thisContentItem.value=DHTMLSafe.DOM.body.createTextRange().text;
		DHTMLSafe.DOM.body.innerHTML = thisContentItem.value;	
		dhtml_HTMLMode = false;
	}
	// switching to source view
	else {
		var regex=/((<BR>)+)/ig;
		// Set font to monospace
		DHTMLSafe.DOM.body.style.fontFamily = "Courier New";
		DHTMLSafe.DOM.body.style.fontSize = "10pt";
		thisContentItem.value=DHTMLSafe.DOM.body.innerHTML;
		DHTMLSafe.DOM.body.innerHTML = "";
		DHTMLSafe.DOM.body.createTextRange().text = thisContentItem.value.replace(regex, "$1\n");
		dhtml_HTMLMode = true;
	}
}

////////////////////// todo
///////////// are we going to hide the font extras for style compliance?

// handle the font name pulldown
function dhtml_font() {
	// no commands should work in html mode
	if (dhtml_HTMLMode) { return; }
	oSel=eval('document.all.lstFont');	
	DHTMLSafe.ExecCommand(DECMD_SETFONTNAME, OLECMDEXECOPT_DODEFAULT, oSel.options[oSel.selectedIndex].name);
	DHTMLSafe.focus();
}

// handle the font size pulldown
function dhtml_fontsize() {
	//no commands should work in html mode
	if (dhtml_HTMLMode) { return; }
	oSel=eval('document.all.lstFontSize');	
	DHTMLSafe.ExecCommand(DECMD_SETFONTSIZE, OLECMDEXECOPT_DODEFAULT, oSel.options[oSel.selectedIndex].value);
	DHTMLSafe.focus();
}

// handle the style pulldown
function dhtml_style() {
	//no commands should work in html mode
	if (dhtml_HTMLMode) { return; }
	oSel=eval('document.all.lstStyle');	
	DHTMLSafe.ExecCommand(DECMD_SETBLOCKFMT, OLECMDEXECOPT_DODEFAULT, oSel.options[oSel.selectedIndex].name);
	DHTMLSafe.focus();
}

//////////////////////////////// todo
// bypass the standard control and insert a link
function dhtml_hyperlink(iHref, iTarget, iStyle, iClass, iName) {
	var uid="dhtml"+Math.random().toString();
	// unlink on blank url
	if(iHref=="" && !iName.length) {
		if(DHTMLSafe.QueryStatus(DECMD_UNLINK)==DECMDF_ENABLED)
			DHTMLSafe.ExecCommand(DECMD_UNLINK);
	}
	else {
		var trSel=DHTMLSafe.DOM.selection.createRange();
    	// was playing with another selection technique
		//if(DHTMLSafe.DOM.selection.type=="Control") {
    	//	var el=DHTMLSafe.DOM.selection.createRange().commonParentElement();
    	//	var tr = DHTMLSafe.DOM.body.createTextRange();
    	//	tr.moveToElementText(el);
    	//	tr.select();
    	//}
		// this is a new link
		if(trSel.compareEndPoints("StartToEnd",trSel)==0) {
			txtHTML="<a href=\""+iHref+"\" ";
			if(iTarget.length)
				txtHTML+="target=\""+iTarget+"\" ";
			if(iStyle.length)
				txtHTML+="style=\""+iStyle+"\" ";
			if(iClass.length)
				txtHTML+="class=\""+iClass+"\" ";
			if(iName.length)
				txtHTML+="name=\""+iName+"\" ";
			txtHTML+=">"+iHref+"</a>";
			trSel.pasteHTML(txtHTML);
		}
		// update existing link or link text
		else {
			DHTMLSafe.ExecCommand(DECMD_HYPERLINK,OLECMDEXECOPT_DONTPROMPTUSER,uid);
			var coll=DHTMLSafe.DOM.all.tags("A");
			for(i=0;i<coll.length;i++) {
				if(coll[i].href==uid) {
					coll[i].href=iHref;
					if(iTarget.length) coll[i].target=iTarget;
					else coll[i].removeAttribute("TARGET",0);
					if(iStyle.length) coll[i].style.cssText=iStyle;
					else coll[i].style.cssText="";
					if(iClass.length) coll[i].className=iClass;
					else coll[i].className="";
				}
			}
		}
	}
	DHTMLSafe.focus();
}

function dhtml_showmenu() {
	var menuStrings = new Array();
	var menuStates = new Array();
	var state;
	var i = 2;
	var idx = 0;
	// Rebuild the context menu. 
	ContextMenu.length = 0;
	// Always show general menu
	for (; i<GeneralContextMenu.length; i++) {
		ContextMenu[idx++] = GeneralContextMenu[i];
	}
	// add table menu if selection is inside table
	if (DHTMLSafe.QueryStatus(DECMD_INSERTROW) != DECMDF_DISABLED) {
		for (i=0; i<TableContextMenu.length; i++) {
			ContextMenu[idx++] = TableContextMenu[i];
		}
	}
	// setup the actual arrays that get passed to SetContextMenu
	for (i=0; i<ContextMenu.length; i++) {
		menuStrings[i] = ContextMenu[i].string;
		if (menuStrings[i] != MENU_SEPARATOR) {
			state = DHTMLSafe.QueryStatus(ContextMenu[i].cmdId);
		}
		else {
			state = DECMDF_ENABLED;
		}
		if (state == DECMDF_DISABLED || state == DECMDF_NOTSUPPORTED) {
			menuStates[i] = OLE_TRISTATE_GRAY;
		}
		else if (state == DECMDF_ENABLED || state == DECMDF_NINCHED) {
			menuStates[i] = OLE_TRISTATE_UNCHECKED;
		}
		else { // DECMDF_LATCHED
			menuStates[i] = OLE_TRISTATE_CHECKED;
		}
	}
	// Set the context menu
	DHTMLSafe.SetContextMenu(menuStrings, menuStates);
}

// some additional handlers for the context menus
function ContextMenuItem(string, cmdId) {
	this.string = string;
	this.cmdId = cmdId;
}
function QueryStatusItem(command, element) {
	this.command = command;
	this.element = element;
}


