 var editorMap = new Object();
 var EDITOR_TOOLBAR_PREFIX = "editorToolbar";
 var EDITOR_COMPOSITION_PREFIX = "editorComposition";
 var EDITOR_IMAGE_CHOOSER_PREFIX = "editorImageChooser";
 var EDITOR_DEFAULT_FONT_FAMILY = "Meridien Roman, Verdana";
 var EDITOR_DEFAULT_FONT_SIZE = "13px";
 var EDITOR_FONT_PREFIX = "editorFont";
 var buttonMap = new Object();
 var BUTTON_DIV_PREFIX = "buttonDiv";
 var BUTTON_PAD1_PREFIX = "buttonPad1";
 var BUTTON_PAD2_PREFIX = "buttonPad2";
 var BUTTON_IMAGE_PREFIX = "buttonImage";
 var editorIDGenerator = null;
 
 
 // ButtonOnDblClick(element)
 function ButtonOnDblClick(element){
  ButtonOnClick(element);
 }
 
 // ButtonOnClick(element)f
 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);
  }
 }
 
 // ButtonOnMouseOver(element)
 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";
  }
 }
 
 // ButtonOnMouseOut(element)
 function ButtonOnMouseOut(element){
  var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
  var button = buttonMap[id];
  if (button.enabled){
   ButtonReleaseButton(id);
  }
 }
 
 // ButtonReleaseButton(id)
 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";
 }
 
 // ButtonOnMouseUp(element)
 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);
			}
  }
	     }
 
 // ButtonPushButton(id)
 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";
 }
 
 // ButtonOnMouseDown(element)
 function ButtonOnMouseDown(element){
  if (event.button == 1){
			var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
			var button = buttonMap[id];
			if (button.enabled)	{
				ButtonPushButton(id);
			}
  }
	     }
 
 // ButtonOnDragStart()
 function ButtonOnDragStart(){
  window.event.returnValue = false;
 }
 
 // ButtonOnSelectStart()
 function ButtonOnSelectStart(){
  window.event.returnValue = false;
 }
 
 // init()
 function init(){
  if(document.getElementById){
   if(typeof OnLoad != "undefined") OnLoad();
  }
 }
 
 // OnLoad()
 function OnLoad(){
  SetHtml();
  RestoreBackground();
 }
 // RestoreBackground()
 function RestoreBackground(){
  editor.SetBackgroundImage("");
 }
 
 // IDGenerator(nextID)
 function IDGenerator(nextID){
  this.nextID = nextID;
  this.GenerateID = IDGeneratorGenerateID;
 }
 
 // IDGeneratorGenerateID()
 function IDGeneratorGenerateID(){
  return this.nextID++;
 }
 
 // Button(idGenerator,caption,action,text,image)
 function Button(idGenerator,caption,action,text,image){
  this.idGenerator = idGenerator;
  this.caption = caption;
  this.action = action;
  this.text = text;
  this.image = image;
  this.enabled = true;
  this.Instantiate = ButtonInstantiate;
  this.Enable = ButtonEnable;
 }
 // ButtonInstantiate()
 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 += " onfocus=\"alert()\"";
  html += ">";
  html += "<a href=\"#\" style=\"cursor:hand\" onmouseover=\"window.status='" + this.caption + "';return true;\" onmouseout=\"window.status=window.defaultStatus;return true;\">";
  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><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr>";
  html += "<td>";
  html += "<img id=\"";
  html += BUTTON_IMAGE_PREFIX;
  html += this.id;
  html += "\" src=\"";
  html += this.image;
  html += "\" title=\"";
  html += this.caption;
  html += "\">";
  html += "</td>";
  if (this.text != "") {
  	 html += "<td>&nbsp;</td>";
	 html += "<td class=ButtonText>";
	 html += this.text;
	 html += "</td>";
  }
  html += "</tr></table></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 += "</a>";
  html += "</div>";
  document.write(html);
 }
 // ButtonEnable(enabled)
 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";
  }
 }
 
 // EditorOnCut(id)
 function EditorOnCut(id){
  EditorFormat(id, "cut");
 }
 
 // EditorOnCopy(id)
 function EditorOnCopy(id){
  EditorFormat(id, "copy");
 }
 
 // EditorValidateMode(id)
 function EditorValidateMode(id){
  var editor = editorMap[id];
  if (!editor.textMode) {
   return true;
  }
  alert("Please uncheck the \"View HTML Source\" checkbox to use the toolbars.");
  eval(EDITOR_COMPOSITION_PREFIX + id).focus();
  return false;
 }
 
 // EditorHideAllDropDowns(id)
 function EditorHideAllDropDowns(id){
  var editor = editorMap[id];
  for (var i in editor.dropDownMap) {
   EditorHideDropDown(id, i);
  }
 }
 
 // EditorHideDropDown(id, prefix)
 function EditorHideDropDown(id, prefix){
  d.all[prefix + id].style.display = "none";
 }
 
 
 // EditorFormat(id, what, opt)
 function EditorFormat(id, what, opt){
  if (!EditorValidateMode(id)) {
   return;
  }
  if (opt == "removeFormat") {
  	 what = opt;
	 opt = null;
  }
  EditorHideAllDropDowns(id);
  eval(EDITOR_COMPOSITION_PREFIX + id).focus();
  if (opt == null) {
   eval(EDITOR_COMPOSITION_PREFIX + id).document.execCommand(what);
  } else {
   eval(EDITOR_COMPOSITION_PREFIX + id).document.execCommand(what,"",opt);
  }
 }
 
 // EditorGetHTML()
 function EditorGetHTML(){
  if (this.textMode) {
   return eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerText;
  }
  var html =  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerHTML;
  return html.toLowerCase() == "<div></div>" ? "" : html;
 }
 
 // EditorSetHTML(html)
 function EditorSetHTML(html){
  if (this.textMode) {
   eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerText = html;
  } else {
   html = "<div>" + html + "</div>";
   eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.innerHTML = html;
  }
 }
 
 // EditorFocus()
 function EditorFocus(){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).focus();
 }
 
 // SetDomain(d)
 function SetDomain(d){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.domain = d;
 }
 
 // EditorSetBackgroundColor(color)
 function EditorSetBackgroundColor(color){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.backgroundColor = color;
  this.backgroundColor = color;
 }
 
 // EditorGetBackgroundColor()
 function EditorGetBackgroundColor(){
  return this.backgroundColor;
 }
 
 // EditorRemoveBackgroundColor(color)
 function EditorRemoveBackgroundColor(color){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.backgroundColor = "";
  this.backgroundColor = "";
 }
 
 // EditorSetBackgroundImage(url)
 function EditorSetBackgroundImage(url){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.backgroundImage = "url(" + url + ")";
  this.backgroundImage = url;
 }
 
 // EditorGetBackgroundImage()
 function EditorGetBackgroundImage(){
  return this.backgroundImage;
 }
 
 // EditorRemoveBackgroundImage(url)
 function EditorRemoveBackgroundImage(url){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.backgroundImage = "none";
  this.backgroundImage = "";
 }
 
 // EditorSetForegroundColor(color)
 function EditorSetForegroundColor(color){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.color = color;
  this.foregroundColor = color;
 }
 
 // EditorGetForegroundColor()
 function EditorGetForegroundColor(){
  return this.foregroundColor;
 }
 
 // EditorRemoveForegroundColor(color)
 function EditorRemoveForegroundColor(color){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.color = "";
  this.foregroundColor = "";
 }
 
 // EditorSetFontFamily(fontFamily)
 function EditorSetFontFamily(fontFamily){
  if (fontFamily == "") {
   fontFamily = EDITOR_DEFAULT_FONT_FAMILY;
  }
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.fontFamily = fontFamily;
  this.fontFamily = fontFamily;
 }
 
 // EditorGetFontFamily()
 function EditorGetFontFamily(){
  return this.fontFamily;
 }
 
 // EditorRemoveFontFamily(font)
 function EditorRemoveFontFamily(font){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.fontFamily = EDITOR_DEFAULT_FONT_FAMILY;
  this.fontFamily = EDITOR_DEFAULT_FONT_FAMILY;
 }
 
 // EditorSetFontSize(fontSize)
 function EditorSetFontSize(fontSize){
  if (fontSize == "") {
   fontSize = EDITOR_DEFAULT_FONT_SIZE;
  }
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.fontSize = fontSize;
  this.fontSize = fontSize;
 }
 
 // EditorGetFontSize()
 function EditorGetFontSize(){
  return this.fontSize;
 }
 
 // EditorRemoveFontSize(font)
 function EditorRemoveFontSize(font){
  eval(EDITOR_COMPOSITION_PREFIX + this.id).document.body.style.fontSize = EDITOR_DEFAULT_FONT_SIZE;
  this.fontSize = EDITOR_DEFAULT_FONT_SIZE;
 }
 
 // EditorGetElement(tagName, start)
 function EditorGetElement(tagName, start){
  while (start && start.tagName != tagName) {
   start = start.parentElement;
  }
  return start;
 }

 // Editor(idGenerator)
 function Editor(idGenerator){
  this.idGenerator = idGenerator;
  this.textMode = false;
  this.backgroundColor = "";
  this.backgroundImage = "";
  this.foregroundColor = "";
  this.fontFamily = "";
  this.fontSize = "";
  this.stationery = false;
  this.stationeryWindow = null;
  this.instantiated = false;
  this.Instantiate = EditorInstantiate;
  this.GetHTML = EditorGetHTML;
  this.SetHTML = EditorSetHTML;
  this.Focus = EditorFocus;  
  this.SetDomain = SetDomain;  
  this.SetBackgroundColor = EditorSetBackgroundColor;  
  this.GetBackgroundColor = EditorGetBackgroundColor;  
  this.RemoveBackgroundColor = EditorRemoveBackgroundColor;    
  this.SetBackgroundImage = EditorSetBackgroundImage;  
  this.GetBackgroundImage = EditorGetBackgroundImage;  
  this.RemoveBackgroundImage = EditorRemoveBackgroundImage;  
  this.SetForegroundColor = EditorSetForegroundColor;  
  this.GetForegroundColor = EditorGetForegroundColor;  
  this.RemoveForegroundColor = EditorRemoveForegroundColor;  
  this.SetFontFamily = EditorSetFontFamily;  
  this.GetFontFamily = EditorGetFontFamily;  
  this.RemoveFontFamily = EditorRemoveFontFamily;  
  this.SetFontSize = EditorSetFontSize;  
  this.GetFontSize = EditorGetFontSize;
  this.RemoveFontSize = EditorRemoveFontSize;
 }
 
 // EditorOnCopy(id)
 function EditorOnCopy(id){
  EditorFormat(id, "copy");
 }
 
 // EditorOnPaste(id)
 function EditorOnPaste(id){
  EditorFormat(id, "paste");
 }
 
 // EditorOnBold(id)
 function EditorOnBold(id){
  EditorFormat(id, "bold");
 }
 
 // EditorOnItalic(id)
 function EditorOnItalic(id){
  EditorFormat(id, "italic");
 }
 
 // EditorOnUnderline(id)
 function EditorOnUnderline(id){
  EditorFormat(id, "underline");
 }
 
 // UtilBeginScript()
 function UtilBeginScript(){
  return String.fromCharCode(60, 115, 99, 114, 105, 112, 116, 62);
 } 
 
 // EditorOnCreateHyperlink(id)
 function EditorOnCreateHyperlink(id){
  if (!EditorValidateMode(id)) {
   return;
  }
  EditorHideAllDropDowns(id);
  var anchor = EditorGetElement("A", eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.createRange().parentElement());
  var link = prompt("Entra l'enllaç (ex. http://www.yahoo.com):", anchor ? anchor.href : "http://");
  if (link && link != "http://") {
   if (eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.type == "None") {
    var range = eval(EDITOR_COMPOSITION_PREFIX + id).document.selection.createRange();
			range.pasteHTML("<A HREF=\"" + link + "\"></A>");
			range.select();
   } else {
    EditorFormat(id, "CreateLink", link);
   }
  }
 }
 
 // UtilEndScript()
 function UtilEndScript(){
  return String.fromCharCode(60, 47, 115, 99, 114, 105, 112, 116, 62);
 }
 
  
  // EditorHideDropDown(id, prefix)
  function EditorHideDropDown(id, prefix){
   d.all[prefix + id].style.display = "none";
  }

 // EditorOnClick
 function EditorOnClick(id){
  EditorHideAllDropDowns(id);
 }

