function RichEdit(instance_name, content)
	{
  this.instance_name = instance_name;
  this.content = content.replace('&lt;', '<').replace('&gt;', '>');	//Recreate missing HTML tags
  this.fieldContent="content";  
  this.fieldHidden="html";      
  this.viewMode = 1;	//Start with the rendered view

  if(this.isSupported())
    {
		//Show the content in the window
    ctl = document.getElementById(this.fieldContent)
    ctl.style.visibility = "visible";
    doc = ctl.contentWindow.document;
    doc.designMode='On';
    doc.open();
		doc.write('<link rel="stylesheet" href="/site.css">');
    doc.write(this.content);
    doc.close();
    this._updateFonts();	//Ensure the display looks correct
		this.textFormat("justifyleft|");
    }

  //Load the content
  document.getElementById(this.fieldHidden).value=this.content;
	}

RichEdit.prototype.prepareSubmit = function ()
	{
  var htmlCode=document.getElementById(this.fieldContent).contentWindow.document.body.innerHTML;
  if(this.viewMode == 2)
	  // be sure this is in design view before submission
  	{ this.toggleMode(); }
  document.getElementById(this.fieldHidden).value=htmlCode;
  return true;
	}

RichEdit.prototype.blockFormat = function (listBox)
	{
	if (this.value != "") 
  	{ 
    this.textFormat(listBox.value); 
		listBox.selectedIndex = 0;
    }
  }
RichEdit.prototype.textFormat = function (fullCommand)
	{
	//Determine the two parameters
  m = fullCommand.split("|");
  command = m[0];
  optn = (m[1] == "") ? "" : "<" + m[1] + ">";

  ctl = document.getElementById(this.fieldContent);
  ctl.contentWindow.focus();

  if(command=='toggle')
  	{ this.toggleMode(); }	//Toggle between rendered and HTML view
  else if(command=='table')
  	{ this.insertTable(); }	//Get the extra table information before creating the table
  else if(command == "image")
  	{ this.insertImage(); }	
  else if(command == "linkinternal")
  	{ this.insertLinkInternal(); }
  else if(command == "link")
  	{ this.insertLink(); }
  else if(command == "template")
  	{ this.insertTemplate(); }	
  else  
//    if(command=='createlink'){
//        var szURL=prompt('Enter a URL:', '');
//        if(document.getElementById(this.fieldContent).contentWindow.document.queryCommandEnabled(command)){
//            document.getElementById(this.fieldContent).contentWindow.document.execCommand('CreateLink',false,URL);
//            return true;
//        }else return false;
//    }else{
		{
		//General formatting commands
    if(ctl.contentWindow.document.queryCommandEnabled(command))
      { ctl.contentWindow.document.execCommand(command, false, optn); }
    else 
      { return false; }
    }
	}

RichEdit.prototype.toggleMode = function ()
	{
	//Toggle between HTML view and rendered view
  if(this.viewMode == 2)
    {
    if(this.isMSIE())
    	{ document.getElementById(this.fieldContent).contentWindow.document.body.innerHTML = document.getElementById(this.fieldContent).contentWindow.document.body.innerText; }
		else
			{
      var html = document.getElementById(this.fieldContent).contentWindow.document.body.ownerDocument.createRange();
      html.selectNodeContents(document.getElementById(this.fieldContent).contentWindow.document.body);
      document.getElementById(this.fieldContent).contentWindow.document.body.innerHTML = html.toString();
			}
	    this.viewMode = 1; // Rendered
    }
  else
    {
    if(this.isMSIE())
	    { document.getElementById(this.fieldContent).contentWindow.document.body.innerText = document.getElementById(this.fieldContent).contentWindow.document.body.innerHTML;}
    else
			{
      var html = document.createTextNode(document.getElementById(this.fieldContent).contentWindow.document.body.innerHTML);
      document.getElementById(this.fieldContent).contentWindow.document.body.innerHTML = '';
      document.getElementById(this.fieldContent).contentWindow.document.body.appendChild(html);
			}    
	    this.viewMode = 2; // HTML
    }

		this._updateFonts();	//Ensure the formatting is correct
    document.getElementById(this.fieldContent).contentWindow.focus();

	}

RichEdit.prototype._updateFonts = function ()
	{
	//Set the formatting for the content window and hide / show the toolbar as necessary
  if(this.viewMode == 1)
		//Rendered view
  	{
		var doc = document.getElementById(this.fieldContent).contentWindow.document;
    if (doc.body != null)
			{
      doc.body.style.fontFamily = 'Verdana, Arial, sans-serif';
			doc.body.style.fontSize = 'smaller';
      }
    document.getElementById(this.fieldContent).contentWindow.focus();
    document.getElementById("toolbar1").style.visibility = 'visible';
    }
  else
  	{
		//HTML view
    document.getElementById(this.fieldContent).contentWindow.document.body.style.fontFamily = 'monospace';
    document.getElementById(this.fieldContent).contentWindow.document.body.style.fontSize = '10pt';
    document.getElementById(this.fieldContent).contentWindow.focus();
    document.getElementById("toolbar1").style.visibility = 'hidden';
    }
	}

RichEdit.prototype.insertTable = function ()
	{
	win = window.open("/php/cms/inserttable.php", "insertTable", "width=498, height=346, toolbar=no, location=no, status=no, resizable=no");
  win.focus();
	}

RichEdit.prototype.insertImage = function ()
	{
	win = window.open("/php/cms/insertimage.php?title=" + this.selectedText(), "insertImage", "width=440, height=400, toolbar=no, location=no, status=no, resizable=no");
  win.focus();
	}

RichEdit.prototype.insertLinkInternal = function ()
	{
	win = window.open("/php/cms/insertlinkinternal.php?title=" + this.selectedText(), "insertLink", "width=440, height=346, toolbar=no, location=no, status=no, resizable=no");
  win.focus();
	}

RichEdit.prototype.insertLink = function ()
	{
	win = window.open("/php/cms/insertlink.php?title=" + this.selectedText(), "insertLink", "width=440, height=346, toolbar=no, location=no, status=no, resizable=no");
  win.focus();
	}

RichEdit.prototype.isSupported = function () {
    // This is to get rid of the browser UA check that was previously implemented for this class.
    // should be called from somewhere in the body of the document for best results
    document.write('<iframe id="RichEdit_Testing_Browser_Features" style="display: none; visibility: hidden;"></iframe>');
    test = typeof(document.getElementById('RichEdit_Testing_Browser_Features').contentWindow);
    if(test == 'object'){
        return true;
    }else{
        return false;
    }
    return this.supported;
}

/**
*   RichEdit::isMSIE
*
*   Checks if browser is MSIE by testing the invisible IFRAME from the isSupported method
*/
RichEdit.prototype.isMSIE = function () {
    if(typeof(document.getElementById('RichEdit_Testing_Browser_Features').contentWindow.document.body.innerText)=='undefined'){
        return false;
    }else{
        return true;
    }
}

RichEdit.prototype.selectedText = function ()
  {

  if(this.isMSIE())
  	{
    var cursor = document.getElementById(this.fieldContent).contentWindow.document.selection.createRange(); 
    return cursor.text;
    }
  else
  	{
		var win = document.getElementById(this.fieldContent).contentWindow;
    var sel = win.getSelection();
		return sel;
		}
	}

RichEdit.prototype.insertTextAtSelection = function (text)
  {

  if(this.isMSIE())
  	{
    document.getElementById(this.fieldContent).contentWindow.focus();
    var cursor = document.getElementById(this.fieldContent).contentWindow.document.selection.createRange(); 
    cursor.pasteHTML(text);
    }
  else
  	{
			d = document.getElementById(this.fieldContent).contentWindow.document;

		m = text.split("<BR>");
		if (m.length == 1)
    	{
      insertNode = d.createTextNode(m[0]);
      }
    else
    	{
        insertNode = d.createElement("DIV");
        for (i = 0; i < m.length; i++)
        {
          insertNode.appendChild(d.createTextNode(m[i]));
          insertNode.appendChild(d.createElement("BR"));
        }
      }

			win = document.getElementById(this.fieldContent).contentWindow;
      // get current selection
      var sel = win.getSelection();
      // get the first range of the selection
      // (there's almost always only one range)
      var range = sel.getRangeAt(0);

      // deselect everything
      sel.removeAllRanges();

      // remove content of current selection from document
      range.deleteContents();

      // get location of current selection
      var container = range.startContainer;
      var pos = range.startOffset;

      // make a new range for the new selection
      range=document.createRange();

      if (container.nodeType==3 && insertNode.nodeType==3) {

        // if we insert text in a textnode, do optimized insertion
        container.insertData(pos, insertNode.nodeValue);

        // put cursor after inserted text
        range.setEnd(container, pos+insertNode.length);
        range.setStart(container, pos+insertNode.length);

      } else {


        var afterNode;
        if (container.nodeType==3) {

          // when inserting into a textnode
          // we create 2 new textnodes
          // and put the insertNode in between

          var textNode = container;
          container = textNode.parentNode;
          var text = textNode.nodeValue;

          // text before the split
          var textBefore = text.substr(0,pos);
          // text after the split
          var textAfter = text.substr(pos);

          var beforeNode = document.createTextNode(textBefore);
          afterNode = document.createTextNode(textAfter);

          // insert the 3 new nodes before the old one
          container.insertBefore(afterNode, textNode);
          container.insertBefore(insertNode, afterNode);
          container.insertBefore(beforeNode, insertNode);

          // remove the old node
          container.removeChild(textNode);

        } else {

          // else simply insert the node
          afterNode = container.childNodes[pos];
          container.insertBefore(insertNode, afterNode);
        }

        range.setEnd(afterNode, 0);
        range.setStart(afterNode, 0);
      }

      sel.addRange(range);
  ctl = document.getElementById(this.fieldContent);
  ctl.contentWindow.focus();
}
  };



function debug(objName) {
   var obj = eval(objName)
   var msg = ""
   var count = 0
   var maxProps = 20
   // Loop through properties of the object
   for (var i in obj) {
      if (i != "outerHTML" && i != "outerText" && i != "innerHTML" && i
!= "innerText" && i != "domain") {
         msg += objName + "." + i + "=" + obj[i] + "\n"
         if (count > maxProps) {
            // Output a batch
               alert(msg)
            msg = ""
            count = 0
            continue
         }
         count++
      }
   }
   // Output any leftovers
alert(msg)
   }
   

    function insertField()
    	{
    	var key = document.getElementById("lstFields").value;
      if (key != "")
        {
        s = "[@" + fields[key][0].toUpperCase() + "]";
        insertText(s);
        document.getElementById("lstFields").value = "";
        }
    	}

    function refresh()
      {
      category = document.getElementById("lstCategories").value;
      ctl = document.getElementById("lstFields");

      for(i = ctl.options.length-1; i >= 0; i--)
      {
    	  ctl.options[i] = null;
      }

    	var row = 1;
      ctl.options[0] = new Option("Choose a Field...");
      ctl.options[0].value = "";
      for(i = 0; i < fields.length; i++)
        {
        if (fields[i][2] == category)
          {
          ctl.options[row] = new Option(fields[i][1]);
          ctl.options[row].value = i;
          row++;
          }
        }
      }


