   var fileStatus="";
   var fileArray = new Array();
   var imageArray = new Array();
   var BUTTON_IMAGE_PREFIX = "buttonImage";  //To give the unique identificatiopn for
                                             //button image accompanied by id
   var BUTTON_DIV_PREFIX = "buttonDiv";      //To give the the unique <div> tag for each button
                                             //accompanied by its id.
   var BUTTON_PAD1_PREFIX = "buttonPad1";    //to set the width and height of the button pads
   var BUTTON_PAD2_PREFIX = "buttonPad2";    // at the time of mousepressed and mouse release event
   var buttonMap = new Object();


function UtilBeginScript()
{
   return String.fromCharCode(60, 115, 99, 114, 105, 112, 116, 62);
}

function UtilEndScript()
{
   return String.fromCharCode(60, 47, 115, 99, 114, 105, 112, 116, 62);
}

function IDGenerator(nextID)
{
   this.nextID = nextID;
   this.GenerateID = IDGeneratorGenerateID;
}

function IDGeneratorGenerateID()
{
   return this.nextID++;
}

function Button
(
   idGenerator,//id for button
   caption,   //identifiction for button
   action,     //jobs of button
   buttonmage //image for button
)

   {
      this.idGenerator = idGenerator;
      this.caption = caption;
      this.action = action;
      this.image = buttonmage;
      this.enabled = true;
      this.Instantiate = ButtonInstantiate;  //To initiate the buttons like cut, copy, etc
      this.Enable = ButtonEnable;            // To enable the button for job
   }


function ButtonInstantiate()
{
   this.id = this.idGenerator.GenerateID();
   buttonMap[this.id] = this;
   var html = "";
   html += '<div id="';
   html += BUTTON_DIV_PREFIX;
   html += this.id;
   html += '" class="ButtonNormal"';
   html += ' onselectstart="ButtonOnSelectStart()"';
   html += ' ondragstart="ButtonOnDragStart()"';
   html += ' onmousedown="ButtonOnMouseDown(this)"';
   html += ' onmouseup="ButtonOnMouseUp(this)"';
   html += ' onmouseout="ButtonOnMouseOut(this)"';
   html += ' onmouseover="ButtonOnMouseOver(this)"';
   html += ' onclick="ButtonOnClick(this)"';
   html += ' ondblclick="ButtonOnDblClick(this)"';
   html += '>';
   html += '<table cellpadding=0 cellspacing=0 border=0><tr><td><img id="';
   html += BUTTON_PAD1_PREFIX;
   html += this.id;
   html += '" width=2 height=2></td><td></td><td></td></tr><tr><td></td><td>';
   html += '<img id="';
   html += BUTTON_IMAGE_PREFIX;
   html += this.id;
   html += '" src="';
   html += this.image;
   html += '" title="';
   html += this.caption;
   html += '" class="Image"';
   html += '>';
   html += '</td><td></td></tr><tr><td></td><td></td><td><img id="';
   html += BUTTON_PAD2_PREFIX;
   html += this.id;
   html += '" width=2 height=2></td></tr></table>';
   html += '</div>';
   document.write(html);
}

   function ButtonEnable(enabled)
   {
      this.enabled = enabled;
      if (this.enabled)
      {
         document.all[BUTTON_DIV_PREFIX + this.id].className = "ButtonNormal";
      }
      else
      {
         document.all[BUTTON_DIV_PREFIX + this.id].className = "ButtonDisabled";
      }
   }



   function ButtonOnSelectStart()
   {
      window.event.returnValue = false;//cancel the default action of the button
   }



   function ButtonOnDragStart()
   {
      window.event.returnValue = false;
   }


   function ButtonOnMouseDown(element)
   {
      if (event.button == 1)//specifies the left mouse button
      {
         var id = element.id.substring(BUTTON_DIV_PREFIX.length,
   element.id.length);//takes the button name
         var button = buttonMap[id];
         if (button.enabled)
         {
            ButtonPushButton(id);
         }
      }
   }



   function ButtonOnMouseUp(element)
   {
      if (event.button == 1)
      {
         var id = element.id.substring(BUTTON_DIV_PREFIX.length,
element.id.length);
         var button = buttonMap[id];
         if (button.enabled)
         {
            ButtonReleaseButton(id);
         }
      }
   }



   function ButtonOnMouseOut(element)
   {
      var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
      var button = buttonMap[id];
      if (button.enabled)
      {
         ButtonReleaseButton(id);
      }
   }


   function ButtonOnMouseOver(element)
   {
      var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
      var button = buttonMap[id];
      if (button.enabled)
      {
         ButtonReleaseButton(id);
         document.all[BUTTON_DIV_PREFIX + id].className = "ButtonMouseOver";
      }
   }


   function ButtonOnClick(element)
   {
      var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
      var button = buttonMap[id];
      if (button.enabled)
      {
         eval(button.action);
      }
   }


   function ButtonOnDblClick(element)
   {
      ButtonOnClick(element);
   }

   function ButtonPushButton(id)
   {
      document.all[BUTTON_PAD1_PREFIX + id].width = 3;
      document.all[BUTTON_PAD1_PREFIX + id].height = 3;
      document.all[BUTTON_PAD2_PREFIX + id].width = 1;
      document.all[BUTTON_PAD2_PREFIX + id].height = 1;
      document.all[BUTTON_DIV_PREFIX + id].className = "ButtonPressed";
   }


   function ButtonReleaseButton(id)
   {
      document.all[BUTTON_PAD1_PREFIX + id].width = 2;
      document.all[BUTTON_PAD1_PREFIX + id].height = 2;
      document.all[BUTTON_PAD2_PREFIX + id].width = 2;
      document.all[BUTTON_PAD2_PREFIX + id].height = 2;
      document.all[BUTTON_DIV_PREFIX + id].className = "ButtonNormal";
   }


   var EDITOR_COMPOSITION_PREFIX = "editorComposition";
   var EDITOR_PARAGRAPH_PREFIX = "editorParagraph";
   var EDITOR_LIST_AND_INDENT_PREFIX = "editorListAndIndent";
   var EDITOR_TOP_TOOLBAR_PREFIX = "editorTopToolbar";
   var EDITOR_BOTTOM_TOOLBAR_PREFIX = "editorBottomToolbar";
   //var EDITOR_SMILEY_BUTTON_PREFIX = "editorSmileyButton";
   var EDITOR_IMAGE_CHOOSER_PREFIX = "editorImageChooser";
   var editorMap = new Object();
   var editorIDGenerator = null;

   function Editor(idGenerator)
   {
      this.idGenerator = idGenerator;
      this.textMode = false;
      this.brief = false;
      this.instantiated = false;
      this.Instantiate = EditorInstantiate;
      this.GetText = EditorGetText;
      this.SetText = EditorSetText;
      this.GetHTML = EditorGetHTML;
      this.SetHTML = EditorSetHTML;
      this.GetBrief = EditorGetBrief;
      this.SetBrief = EditorSetBrief;
   }

   function EditorInstantiate()
   {
   if (this.instantiated) {
         return;
      }
      this.id = this.idGenerator.GenerateID();
      editorMap[this.id] = this;
      editorIDGenerator = this.idGenerator;

      var html = "";

	  html += "<table align=\"left\" cellpaddin=\"0\" cellspacing=\"0\" border=\"0\">";
      html += "<tr>";
      html += "<td class=\"Text\">";
      html += "<input type=\"checkbox\" onclick=\"EditorOnViewHTMLSource(" + this.id + ", this.checked)\">";
      html += "View HTML Source";
      html += "</td>";
      html += "<td align=\"right\">";
      html += UtilBeginScript();
      html += "var createHyperlinkButton = new Button(";
      html += "editorIDGenerator,";
      html += "\"Create Hyperlink\",";
      html += "\"EditorOnCreateHyperlink(" + this.id + ")\",";
      html += "\"images/wlink.gif\"";
      html += ");";
      html += "createHyperlinkButton.Instantiate();";
      html += UtilEndScript();
      html += "</td>";
      html += "</tr>";

      html += "<tr>";
      html += "<td colspan=\"2\">";
               
      html += "<iframe frameborder=1 marginheight=1 marginwidth=1 id=\"" + EDITOR_COMPOSITION_PREFIX + this.id + "\" width=\"400\" height=\"40\" name='TextField'>";
      html += "</iframe>";
	 
      html += "</td>";
      html += "</tr>";
      html += "</table>";

	  document.write(html);
      html = '';
      html += '<body  style="font:10pt arial" >';
      html += '</body>';
      eval(EDITOR_COMPOSITION_PREFIX + this.id).document.open();
      eval(EDITOR_COMPOSITION_PREFIX + this.id).document.write(html);
      eval(EDITOR_COMPOSITION_PREFIX + this.id).document.close();
	   eval(EDITOR_COMPOSITION_PREFIX + this.id).document.designMode = "on";
      eval(EDITOR_COMPOSITION_PREFIX + this.id).document.onclick = new Function("EditorOnClick(" + this.id + ")");
      editorIDGenerator = null;
      this.instantiated = true;
   }

   function  EditorGetText()
   {
      return eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerText;
   }

   function  EditorSetText(text)
   {
      text = text.replace(/\n/g, " ");
      text = text.replace("<p>"," ");
      eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerHTML = text;
   }

   function  EditorGetHTML()
   {
      if (this.textMode) {
         return eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerText;
      }
      EditorCleanHTML(this.id);
      EditorCleanHTML(this.id);
      return eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerHTML;
   }

   function  EditorSetHTML(html)
   {
      if (this.textMode) {
         eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerText = html;
      }
      else {
         eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerHTML = html;
      }
   }

   function EditorGetBrief()
   {
      return this.brief;
   }

   function EditorSetBrief(brief)
   {
      this.brief = brief;
      var display = this.brief ? "none" : "inline";
      if (this.instantiated) {
         eval(EDITOR_PARAGRAPH_PREFIX + this.id).style.display = display;
         eval(EDITOR_LIST_AND_INDENT_PREFIX + this.id).style.display = display;
      }
   }


   
  
   function EditorOnCreateHyperlink(id)
   {
      if (!EditorValidateMode(id)) 
	  {
         return;
      }

      var anchor = EditorGetElement("A", eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.createRange().parentElement());
      var link = prompt("Please enter link location (eg. http://www.ilri.org):", anchor ?
    anchor.href : "http://");


      if (link && link != "http://") {
         
         if (eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.type == "None") 
         

         {
           eval(EDITOR_COMPOSITION_PREFIX + id).focus();
            var range = eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.createRange();
            range.pasteHTML('<A HREF="' + link + ' " target=_blank>'+link+'</A>');
            range.select(); 
          }
         
         

         else 
         
         {

            EditorFormat(id, "CreateLink", link);
         }
      }
   }

   function EditorOnFile(id, select)
   {

      mytext = eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.createRange().htmlText;
      if (mytext == "")
      {
         alert("Please select the text, that should be shown as a link");
      }
      eval(EDITOR_COMPOSITION_PREFIX + id).focus();
      fileLink = fileStatus + "index.php?pageName='" + select[select.selectedIndex].value + "'";

      EditorFormat(id, "CreateLink", fileLink);
      select.selectedIndex = 0;
   }

   function EditorOnImage(id, select)
   {
      if (!EditorValidateMode(id)) {
         return;
      }
      eval(EDITOR_COMPOSITION_PREFIX + id).focus();
      source = select[select.selectedIndex].value;
      if (eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.type == "None")
      {
            var range = eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.createRange();
            range.pasteHTML('<img src= ' + source + '>');
            range.select();
         }

      select.selectedIndex = 0;
   }


   function EditorOnViewHTMLSource(id, textMode)
   {
      var editor = editorMap[id];
      editor.textMode = textMode;
      if (editor.textMode) {
         EditorCleanHTML(id);
         EditorCleanHTML(id);
         eval(EDITOR_COMPOSITION_PREFIX + id).document.body.innerText =eval(EDITOR_COMPOSITION_PREFIX + id).document.body.innerHTML;
      }
      else {
         eval(EDITOR_COMPOSITION_PREFIX + id).document.body.innerHTML = eval(EDITOR_COMPOSITION_PREFIX + id).document.body.innerText;
      }
      eval(EDITOR_COMPOSITION_PREFIX + id).focus();
   }

   function EditorOnClick(id)
   {
      //eval(EDITOR_IMAGE_CHOOSER_PREFIX + id).Hide();
   }

   function EditorValidateMode(id)
   {
      var editor = editorMap[id];
      if (!editor.textMode) {
         return true;
      }
      alert("Please uncheck the \"View HTML Source\" checkbox to use this tool.");
      eval(EDITOR_COMPOSITION_PREFIX + id).focus();
      return false;
   }

   function EditorFormat(id, what, opt)
   {

      if (!EditorValidateMode(id)) {
         return;
      }
      if (opt == "removeFormat") {
         what = opt;
         opt = null;
      }
      if (opt == null) {
         eval(EDITOR_COMPOSITION_PREFIX + id).document.execCommand(what);
      }
      else {
         eval(EDITOR_COMPOSITION_PREFIX + id).document.execCommand(what, "", opt);
      }
   }

   function EditorCleanHTML(id)
   {
      var fonts = eval(EDITOR_COMPOSITION_PREFIX + id).document.body.all.tags("FONT");
      for (var i = fonts.length - 1; i >= 0; i--) {
         var font = fonts[i];
         if (font.style.backgroundColor == "#ffffff") {
            font.outerHTML = font.innerHTML;
         }
      }
   }

   function EditorGetElement(tagName, start)
   {
      while (start && start.tagName != tagName) {
         start = start.parentElement;

      }
      return start;
   }


function SetVals(element)
 {
   element.value=editor.GetHTML();

 }


function fileDropdownElement(fileString)
{
   fileString = fileString . substring(0,fileString.lastIndexOf(","));
   fileArray = fileString.split(",");
   fileArray.sort();
}

function imageDropdownElement(imageString)
{
   imageString = imageString . substring(0,imageString.lastIndexOf(","));
   imageArray = imageString.split(",");
   imageArray.sort();
}

function setFileStatus(status)
{
   fileStatus = status;
}   