var mode = 2;
//functions to get and set data
function getValue()
{
   var curHTML;
  isEdited = true;
   switch(mode)
   {
      case 1 : 
if (window.top.Inside) {
   curHTML = window.top.Inside.document.getElementById('EDITOR').innerHTML  = getValue();
} else {
	curHTML = document.getElementById("DHTMLEdit1").DOM.body.innerHTML;
}
 break;
      case 0 : curHTML = document.getElementById("tab1").innerHTML; break;
      default : curHTML = document.getElementById("HTML_EDIT").value; break;
   }
   return curHTML;
}
function showHTML()
{
   window.status = 'Display HTML';
   val = getValue();
   mode = 2;
   document.getElementById("HTML_EDIT").value = replaceDbPath(val);
   showTab(3); hideTab(1); hideTab(4); hideTab(5);
   hideTab(2); // Microsoft ONLY
}
function showPreview()
{
   window.status = 'Preview mode enabled';
   showTab(1);
   hideTab(2); // Microsoft ONLY
   hideTab(3); hideTab(4); hideTab(5);
   val = getValue();
   mode = 0;
   document.getElementById("tab1").innerHTML = getDbPath(val);
}
function showEditor()
{
   window.status = 'Edit mode enabled';
   showTab(2);	hideTab(1); hideTab(3); hideTab(4); hideTab(5);
   editor = document.getElementById('DHTMLEdit1');
   val = getDbPath(getValue());
   mode = 1;
   setTimeout(updateEditor, 500);
}
function updateEditor()
{
	editor.DOM.body.innerHTML = getDbPath(val);
}
function pasteHTML(val)
{
   if (val!='') {
      var sel = document.forms[0].DHTMLEdit1.DOM.selection;
      range = sel.createRange();
      range.pasteHTML(val);
   }
}
function displayChanged()
{
   document.all.vhtml.value = document.all.DHTMLEdit1.DocumentHTML;
}
function submitHTML()
{
   document.forms[0].Body.value = replaceDbPath(getValue());
}
//------------------------------------------------------------
//background text color
function setHilite()
{
   var arr = showModalDialog("/" + DatabasePath + "/jslibs/$file/selcolor.htm?OpenElement", "Background Color", "unadorned: yes; dialogHeight: 500px; dialogWidth: 420px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
   if(arr != null) document.forms[0].DHTMLEdit1.ExecCommand(DECMD_SETBACKCOLOR, OLECMDEXECOPT_DODEFAULT, arr);
}
//special characters
function setChar(ref)
{
   var arr = showModalDialog(ref, "Special Character", "unadorned: yes; dialogHeight: 330px; dialogWidth: 470px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
   if(arr != null)
   {
      var sel = document.forms[0].DHTMLEdit1.DOM.selection;
      range = sel.createRange();
      range.pasteHTML(arr);
      document.DHTMLEdit1.focus();
   }
}
//Table function
// Change the text format
function ChangeFormat()
{
   el = document.getElementById('oSelectBlock');
   var sSelection = el.options[el.selectedIndex].text; //change the block format of the selection in the div equal to the user's selection
      var sel = document.forms[0].DHTMLEdit1.DOM.selection;
      range = sel.createRange();
      curText = range.text;
   if(curText != null) document.forms[0].DHTMLEdit1.ExecCommand(DECMD_SETBLOCKFMT, OLECMDEXECOPT_DODEFAULT, sSelection);
   document.DHTMLEdit1.focus();
}
function InsertTable(ref)
{
   var pVar = document.forms[0].ObjTableInfo;
   var args = new Array();
   var arr = null;
   // Display table information dialog
   args["NumRows"] = pVar.NumRows;
   args["NumCols"] = pVar.NumCols;
   args["TableAttrs"] = pVar.TableAttrs;
   args["CellAttrs"] = pVar.CellAttrs;
   args["Caption"] = pVar.Caption;
   arr = null;
   arr = showModalDialog(ref, args, "font-family:Verdana; font-size:12; dialogWidth:36em; dialogHeight:25em");
   if(arr != null)
   {
      // Initialize table object
      for(elem in arr)
      {
         if("NumRows" == elem && arr["NumRows"] != null)
         {
            pVar.NumRows = arr["NumRows"];
         }
         else if("NumCols" == elem && arr["NumCols"] != null)
         {
            pVar.NumCols = arr["NumCols"];
         }
         else if("TableAttrs" == elem)
         {
            pVar.TableAttrs = arr["TableAttrs"];
         }
         else if("CellAttrs" == elem)
         {
            pVar.CellAttrs = arr["CellAttrs"];
         }
         else if("Caption" == elem)
         {
            pVar.Caption = arr["Caption"];
         }
      }
      document.forms[0].DHTMLEdit1.ExecCommand(DECMD_INSERTTABLE, OLECMDEXECOPT_DODEFAULT, pVar);
   }
}
//Functions to open image/attachment/html windows
function imageForm(elm)
{
   imgMgr = window.open('/' + DatabasePath + '/webimagefile?OpenForm' +((elm == '') ? '' : '&amp;elm=' + elm), "UploadImage", "scrollbars=yes, resizable=yes, height=450, width=600, status=no");
}
//Function to insert a DHTML count down
function insertCountdown()
{
//   val = showModalDialog('/' + DatabasePath + '/COUNTDOWN?OpenForm','', "dialogWidth:32em; dialogHeight:32em");
   window.open('/' + DatabasePath + '/COUNTDOWN?OpenForm', "Countdown", "scrollbars=yes, resizable=yes, height=425, width=500, status=yes");
}
//Function to insert a counter
function counterForm(unid)
{
   window.open('/' + DatabasePath + '/counter?OpenForm' +((unid == '') ? '' : '&unid=' + unid), "Counter", "scrollbars=yes, resizable=yes, height=325, width=500, status=no");
}
//Function to edit a counter
function editCounter(unid)
{
   window.open('/' + DatabasePath + '/counters/' + unid + '?OpenDocument', "Counter", "scrollbars=yes, resizable=yes, height=325, width=500, status=no");
}
function attachForm()
{
   window.open('/' + DatabasePath + '/attachment?OpenForm', "UploadFile", "height=300, width=400, status=no, top=100, left=200");
}
function strikeThru()
{
   var sel = document.forms[0].DHTMLEdit1.DOM.selection;
   range = sel.createRange();
   if(range.htmlText == "")
   {
      alert('You must first select some text before using this feature');
      return false;
   }
   if(range.htmlText.charAt(0) == "<")
   {
      range.execCommand("RemoveFormat");
   }
   else 
   {
      range.pasteHTML("<strike>" + range.htmlText + "</strike>");
      document.all.DHTMLEdit1.focus();
   }
}
function superText()
{
   var sel = document.forms[0].DHTMLEdit1.DOM.selection;
   range = sel.createRange();
   if(range.htmlText == "")
   {
      alert('You must first select some text before using this feature');
      return false;
   }
   if(range.htmlText.charAt(0) == "<")
   {
      range.execCommand("RemoveFormat");
   }
   else 
   {
      range.pasteHTML("<sup>" + range.htmlText + "</sup>");
      document.all.DHTMLEdit1.focus();
   }
}
function subText()
{
   var sel = document.forms[0].DHTMLEdit1.DOM.selection;
   range = sel.createRange();
   if(range.htmlText == "")
   {
      alert('You must first select some text before using this feature');
      return false;
   }
   if(range.htmlText.charAt(0) == "<")
   {
      range.execCommand("RemoveFormat");
   }
   else 
   {
      range.pasteHTML("<sub>" + range.htmlText + "</sub>");
      document.all.DHTMLEdit1.focus();
   }
}
//toolbar functions
function justifyLeft()
{
   document.DHTMLEdit1.ExecCommand(DECMD_JUSTIFYLEFT);
   document.DHTMLEdit1.focus();
}
function justifyCenter()
{
   document.DHTMLEdit1.ExecCommand(DECMD_JUSTIFYCENTER);
   document.DHTMLEdit1.focus();
}
function justifyRight()
{
   document.DHTMLEdit1.ExecCommand(DECMD_JUSTIFYRIGHT);
   document.DHTMLEdit1.focus();
}
function indent()
{
   document.DHTMLEdit1.ExecCommand(DECMD_INDENT);
   document.DHTMLEdit1.focus();
}
function outdent()
{
   document.DHTMLEdit1.ExecCommand(DECMD_OUTDENT);
   document.DHTMLEdit1.focus();
}
function bulletList()
{
   document.DHTMLEdit1.ExecCommand(DECMD_UNORDERLIST);
   document.DHTMLEdit1.focus();
}
function numList()
{
   document.DHTMLEdit1.ExecCommand(DECMD_ORDERLIST);
   document.DHTMLEdit1.focus();
}
function cut()
{
   document.DHTMLEdit1.ExecCommand(DECMD_CUT);
   document.DHTMLEdit1.focus();
}
function copy()
{
   document.DHTMLEdit1.ExecCommand(DECMD_COPY);
   document.DHTMLEdit1.focus();
}
function paste()
{
   document.DHTMLEdit1.ExecCommand(DECMD_PASTE);
   document.DHTMLEdit1.focus();
}
function undo()
{
   document.DHTMLEdit1.ExecCommand(DECMD_UNDO);
   document.DHTMLEdit1.focus();
}
function redo()
{
   document.DHTMLEdit1.ExecCommand(DECMD_REDO);
   document.DHTMLEdit1.focus();
}
function findText()
{
   document.DHTMLEdit1.ExecCommand(DECMD_FINDTEXT);
   document.DHTMLEdit1.focus();
}
function hyperLink()
{
   document.DHTMLEdit1.ExecCommand(DECMD_HYPERLINK);
   document.DHTMLEdit1.focus();
}
function insRow()
{
   status = document.DHTMLEdit1.QueryStatus(DECMD_INSERTROW);
   if(status == DECMDF_DISABLED || status == DECMDF_NOTSUPPORTED)
   {
      alert('Feature only available in a table');
   }
   else 
   {
      document.DHTMLEdit1.ExecCommand(DECMD_INSERTROW);
      document.DHTMLEdit1.focus();
   }
}
function delRow()
{
   status = document.DHTMLEdit1.QueryStatus(DECMD_DELETEROWS);
   if(status == DECMDF_DISABLED || status == DECMDF_NOTSUPPORTED)
   {
      alert('Feature only available in a table');
   }
   else 
   {
      document.DHTMLEdit1.ExecCommand(DECMD_DELETEROWS);
      document.DHTMLEdit1.focus();
   }
}
function insCol()
{
   status = document.DHTMLEdit1.QueryStatus(DECMD_INSERTCOL);
   if(status == DECMDF_DISABLED || status == DECMDF_NOTSUPPORTED)
   {
      alert('Feature only available in a table');
   }
   else 
   {
      document.DHTMLEdit1.ExecCommand(DECMD_INSERTCOL);
      document.DHTMLEdit1.focus();
   }
}
function delCol()
{
   status = document.DHTMLEdit1.QueryStatus(DECMD_DELETECOLS);
   if(status == DECMDF_DISABLED || status == DECMDF_NOTSUPPORTED)
   {
      alert('Feature only available in a table');
   }
   else 
   {
      document.DHTMLEdit1.ExecCommand(DECMD_DELETECOLS);
      document.DHTMLEdit1.focus();
   }
}
function insCell()
{
   status = document.DHTMLEdit1.QueryStatus(DECMD_INSERTCELL);
   if(status == DECMDF_DISABLED || status == DECMDF_NOTSUPPORTED)
   {
      alert('Feature only available in a table');
   }
   else 
   {
      document.DHTMLEdit1.ExecCommand(DECMD_INSERTCELL);
      document.DHTMLEdit1.focus();
   }
}
function delCell()
{
   status = document.DHTMLEdit1.QueryStatus(DECMD_DELETECELLS);
   if(status == DECMDF_DISABLED || status == DECMDF_NOTSUPPORTED)
   {
      alert('Feature only available in a table');
   }
   else 
   {
      document.DHTMLEdit1.ExecCommand(DECMD_DELETECELLS);
      document.DHTMLEdit1.focus();
   }
}
function showBorders()
{
   document.forms[0].DHTMLEdit1.ShowBorders = !document.forms[0].DHTMLEdit1.ShowBorders;
}
function hr()
{
   var sel = document.forms[0].DHTMLEdit1.DOM.selection;
   range = sel.createRange();
   range.execCommand("InsertHorizontalRule");
   document.all.DHTMLEdit1.focus();
}
function textArea()
{
   var sel = document.forms[0].DHTMLEdit1.DOM.selection;
   range = sel.createRange();
   range.execCommand("InsertTextArea");
   document.all.DHTMLEdit1.focus();
}
function doRightClick()
{
   var sel = document.getElementById("DHTMLEdit1").DOM.selection;
   var rng = sel.createRange();
   var el = rng.parentElement();
window.status=el.tagName;
	editor = document.forms[0].DHTMLEdit1;
	menuStrings = new Array();
	menuStates = new Array();
	tableStrings = new Array();
	tableStates = new Array();
	tableStrings[0] = "Insert Row";
	tableStrings[1] = "Insert Column";
	tableStates[0] = OLE_TRISTATE_UNCHECKED;
	tableStates[1] = OLE_TRISTATE_UNCHECKED;
	menuStrings[0] = "Text Properties...";
	menuStrings[1] = "";
	menuStrings[2] = "Cut";
	menuStrings[3] = "Copy";
	menuStrings[4] = "Paste";
	menuStrings[5] = "";
	menuStrings[6] = "Normal Text";
	menuStrings[7] = "Italic";
	menuStrings[8] = "Bold";
	menuStrings[9] = "Underline";
	menuStrings[10] = "";
	menuStrings[11] = "Insert Link...";
	menuStrings[12] = "Insert Image...";
	menuStrings[13] = "Insert Table...";
	menuStrings[14] = "";
	menuStrings[15] = "Select All";
	menuStrings[16] = "Find";
	menuStates[0] = OLE_TRISTATE_UNCHECKED;
	menuStates[1] = 0;
	menuStates[2] = OLE_TRISTATE_UNCHECKED;
	menuStates[3] = OLE_TRISTATE_UNCHECKED;
	menuStates[4] = OLE_TRISTATE_UNCHECKED;
	menuStates[5] = 0;
	menuStates[6] = OLE_TRISTATE_UNCHECKED;
	menuStates[7] = (el.tagName == "I" || el.tagName == "EM") ? OLE_TRISTATE_CHECKED : OLE_TRISTATE_UNCHECKED;
	menuStates[8] = (el.tagName == "STRONG" || el.tagName == "B") ? OLE_TRISTATE_CHECKED : OLE_TRISTATE_UNCHECKED;
	menuStates[9] = OLE_TRISTATE_UNCHECKED;
	menuStates[10] = 0;
	menuStates[11] = OLE_TRISTATE_UNCHECKED;
	menuStates[12] = OLE_TRISTATE_UNCHECKED;
	menuStates[13] = OLE_TRISTATE_UNCHECKED;
	menuStates[14] = 0;
	menuStates[15] = OLE_TRISTATE_UNCHECKED;
	menuStates[16] = OLE_TRISTATE_UNCHECKED;
	document.forms[0].DHTMLEdit1.SetContextMenu( menuStrings, menuStates );
}
function doMenuAction(itemIndex)
{
   switch(itemIndex)
   {
   case 0 : editor.ExecCommand(DECMD_FONT); break;
   case 2 : cut(); break;
   case 3 : copy(); break;
   case 4 : paste(); break;
   case 6 : editor.ExecCommand(DECMD_REMOVEFORMAT); break;
   case 7 : editor.ExecCommand(DECMD_ITALIC); break;
   case 8 : editor.ExecCommand(DECMD_BOLD); break;
   case 9 : editor.ExecCommand(DECMD_UNDERLINE); break;
   case 11 : hyperLink(); break;
   case 12 : imageForm(); break;
   case 13 : InsertTable('/websites/gorman.nsf/jslibs/$file/instable.htm?openelement');document.DHTMLEdit1.focus(); break;
   case 15 : editor.ExecCommand(DECMD_SELECTALL); break;
   case 16 : editor.ExecCommand(DECMD_FINDTEXT); break;
   }
}
function updateToolbar() {
   var sel = document.getElementById("DHTMLEdit1").DOM.selection;
   var rng = sel.createRange();
   var el = rng.parentElement();
   switch (el.tagName)
   {
   case "H1" :
         document.getElementById('oSelectBlock').selectedIndex=2
         break
   case "H2" :
         document.getElementById('oSelectBlock').selectedIndex=3
         break
   case "H3" :
         document.getElementById('oSelectBlock').selectedIndex=4
         break
   case "H4" :
         document.getElementById('oSelectBlock').selectedIndex=5
         break
   case "ADDRESS" :
         document.getElementById('oSelectBlock').selectedIndex=1
         break
   case "LI" :
         document.getElementById('oSelectBlock').selectedIndex=6
         break
   default :
         document.getElementById('oSelectBlock').selectedIndex=0
         break
}
}
 function getIFrameDocument(){
var aID = "EDIT_FRAME";
  if (document.getElementById(aID).contentDocument){
    return document.getElementById(aID).contentDocument;
  } else {
    // IE
    return document.frames[aID].document;
  }
}
 function doRichEditCommand(aName, aArg){
  getIFrameDocument('editorWindow').execCommand(aName,false, aArg);
  document.getElementById('editorWindow').contentWindow.focus()
}
function getEditor() {
	if (window.top.Inside) {
		return window.top.Inside;
	} else {
		return  document.getElementById('DHTMLEdit1');
	}
}
