//CSEditor.js

//Variables globales
var esRichText = false;

var esIE;
var esGecko;
var esSafari;
var esKonqueror;

var agente = navigator.userAgent.toLowerCase();
esIE = ((agente.indexOf("msie") != -1) && (agente.indexOf("opera") == -1) && (agente.indexOf("webtv") == -1)); 
esGecko = (agente.indexOf("gecko") != -1);
esSafari = (agente.indexOf("safari") != -1);
esKonqueror = (agente.indexOf("konqueror") != -1);
	
//ver si designMode esta disponible
if (document.getElementById && document.designMode && !esSafari && !esKonqueror) {
	esRichText = true;
}
	
if (esIE) {
	document.onmouseover = raiseButton;
	document.onmouseout  = normalButton;
	document.onmousedown = lowerButton;
	document.onmouseup   = raiseButton;
}
	
var EDITOR_HTMLMODE_BACKGROUND_COLOR = "rgb(255,255,255)";
var EDITOR_HTMLMODE_COLOR = "#000086";	
var EDITOR_HTMLMODE_FONT_FAMILY = "Verdana, Helvetica, sans-serif";
//var EDITOR_HTMLMODE_FONT_SIZE = "11px"; //Se define en CSUtil_js
var EDITOR_HTMLMODE_MARGIN = "10px";
var EDITOR_HTMLMODE_PADDING = "0px";

var EDITOR_NEW_IKO = "img/new.gif";
var EDITOR_CUT_IKO = "img/cut.gif";
var EDITOR_COPY_IKO = "img/copy.gif";
var EDITOR_PASTE_IKO = "img/paste.gif";
var EDITOR_BOLD_IKO = "img/bold.gif";
var EDITOR_ITALIC_IKO = "img/italic.gif";
var EDITOR_UNDERLINE_IKO = "img/underline.gif";
var EDITOR_STRIKE_THROUGH_IKO = "img/strike.gif";
var EDITOR_SUPERSCRIPT_IKO = "img/superscript.gif";
var EDITOR_SUBSCRIPT_IKO = "img/subscript.gif";
var EDITOR_ALIGN_LEFT_IKO = "img/left.gif";
var EDITOR_ALIGN_CENTER_IKO = "img/center.gif";
var EDITOR_ALIGN_RIGHT_IKO = "img/right.gif";
var EDITOR_ALIGN_JUSTIFY_IKO = "img/justify.gif";
var EDITOR_INSERT_HORIZONTAL_RULE_IKO = "img/horizrule.gif";
var EDITOR_NUMBERED_LIST_IKO = "img/numberedlist.gif";
var EDITOR_BULLETED_LIST_IKO = "img/bulletedlist.gif";
var EDITOR_DECREASE_INDENT_IKO = "img/indentleft.gif";
var EDITOR_INCREASE_INDENT_IKO = "img/indentright.gif";

var EDITOR_NEW_ALT = map["EDITOR_NEW_ALT"];
var EDITOR_CUT_ALT = map["EDITOR_CUT_ALT"];
var EDITOR_COPY_ALT = map["EDITOR_COPY_ALT"];
var EDITOR_PASTE_ALT = map["EDITOR_PASTE_ALT"];
var EDITOR_BOLD_ALT = map["EDITOR_BOLD_ALT"];
var EDITOR_ITALIC_ALT = map["EDITOR_ITALIC_ALT"];
var EDITOR_UNDERLINE_ALT = map["EDITOR_UNDERLINE_ALT"];
var EDITOR_STRIKE_THROUGH_ALT = map["EDITOR_STRIKE_THROUGH_ALT"];
var EDITOR_SUPERSCRIPT_ALT = map["EDITOR_SUPERSCRIPT_ALT"];
var EDITOR_SUBSCRIPT_ALT = map["EDITOR_SUBSCRIPT_ALT"];
var EDITOR_ALIGN_LEFT_ALT = map["EDITOR_ALIGN_LEFT_ALT"];
var EDITOR_ALIGN_CENTER_ALT = map["EDITOR_ALIGN_CENTER_ALT"];
var EDITOR_ALIGN_RIGHT_ALT = map["EDITOR_ALIGN_RIGHT_ALT"];
var EDITOR_ALIGN_RIGHT_ALT = map["EDITOR_ALIGN_RIGHT_ALT"];
var EDITOR_ALIGN_JUSTIFY_ALT = map["EDITOR_ALIGN_JUSTIFY_ALT"];
var EDITOR_INSERT_HORIZONTAL_RULE_ALT = map["EDITOR_INSERT_HORIZONTAL_RULE_ALT"];
var EDITOR_NUMBERED_LIST_ALT = map["EDITOR_NUMBERED_LIST_ALT"];
var EDITOR_BULLETED_LIST_ALT = map["EDITOR_BULLETED_LIST_ALT"];
var EDITOR_DECREASE_INDENT_ALT = map["EDITOR_DECREASE_INDENT_ALT"];
var EDITOR_INCREASE_INDENT_ALT = map["EDITOR_INCREASE_INDENT_ALT"];

//Clase Editor
function CSEditor(edit, html, width, height, buttons, readonly)
{
 //Propiedades.
  this.edit = edit;
  this.html = html;
  this.width = width;
  this.height = height;
  this.buttons = buttons;
  this.readonly = readonly;

  this.Irudikatu = CSEditorIrudikatu;
  this.GetHTML = CSEditorGetHTML;
  this.SetHTML = CSEditorSetHTML;
  this.GetText = CSEditorGetText;

  this.enableDesignMode = CSEditorenableDesignMode;

}

function CSEditorIrudikatu()
{

	if (esRichText) {
		
		if (this.readonly) this.buttons = false;

		//Ajuste para IE.
		if (esIE) {
			var tablewidth = this.width;
		} else {
			var tablewidth = this.width + 4;
		}

        document.writeln('<div class="editDiv">');
		if (this.buttons == true) {
			document.writeln('<table border="0" class="editBack" cellpadding="0" cellspacing="0" id="Buttons1_' + this.edit + '" width="' + tablewidth + '">');
			document.writeln('	<tr>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_NEW_IKO + '" alt="' + EDITOR_NEW_ALT + '" title="' + EDITOR_NEW_ALT + '" onClick="newText(\'' + this.edit + '\')"></td>');
			document.writeln('		<td width="2"><div class=\"editDivider\"></div></td>');
			document.writeln('		<td width="25"><img id="bold" class="editImage" src="' + EDITOR_BOLD_IKO + '" alt="' + EDITOR_BOLD_ALT + '" title="' + EDITOR_BOLD_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'bold\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_ITALIC_IKO + '" alt="' + EDITOR_ITALIC_ALT + '" title="' + EDITOR_ITALIC_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'italic\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_UNDERLINE_IKO + '" alt="' + EDITOR_UNDERLINE_ALT + '" title="' + EDITOR_UNDERLINE_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'underline\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_STRIKE_THROUGH_IKO + '" alt="' + EDITOR_STRIKE_THROUGH_ALT + '" title="' + EDITOR_STRIKE_THROUGH_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'strikethrough\', \'\')"></td>');
			document.writeln('		<td width="2"><div class=\"editDivider\"></div></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_ALIGN_LEFT_IKO + '" alt="' + EDITOR_ALIGN_LEFT_ALT + '" title="' + EDITOR_ALIGN_LEFT_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'justifyleft\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_ALIGN_CENTER_IKO + '" alt="' + EDITOR_ALIGN_CENTER_ALT + '" title="' + EDITOR_ALIGN_CENTER_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'justifycenter\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_ALIGN_RIGHT_IKO + '" alt="' + EDITOR_ALIGN_RIGHT_ALT + '" title="' + EDITOR_ALIGN_RIGHT_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'justifyright\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_ALIGN_JUSTIFY_IKO + '" alt="' + EDITOR_ALIGN_JUSTIFY_ALT + '" title="' + EDITOR_ALIGN_JUSTIFY_ALT + '" onclick="editCommand(\'' + this.edit + '\', \'justifyfull\', \'\')"></td>');
			document.writeln('		<td width="2"><div class=\"editDivider\"></div></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_INSERT_HORIZONTAL_RULE_IKO + '" alt="' + EDITOR_INSERT_HORIZONTAL_RULE_ALT + '" title="' + EDITOR_INSERT_HORIZONTAL_RULE_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'inserthorizontalrule\', \'\')"></td>');
			document.writeln('		<td width="2"><div class=\"editDivider\"></div></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_NUMBERED_LIST_IKO + '" alt="' + EDITOR_NUMBERED_LIST_ALT + '" title="' + EDITOR_NUMBERED_LIST_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'insertorderedlist\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_BULLETED_LIST_IKO + '" alt="' + EDITOR_BULLETED_LIST_ALT + '" title="' + EDITOR_BULLETED_LIST_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'insertunorderedlist\', \'\')"></td>');
			document.writeln('		<td width="25"><div class=\"editDivider\"></div></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_DECREASE_INDENT_IKO + '" alt="' + EDITOR_DECREASE_INDENT_ALT + '" title="' + EDITOR_DECREASE_INDENT_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'outdent\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_INCREASE_INDENT_IKO + '" alt="' + EDITOR_INCREASE_INDENT_ALT + '" title="' + EDITOR_INCREASE_INDENT_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'indent\', \'\')"></td>');
			document.writeln('		<td width="25"><div class=\"editDivider\"></div></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_SUPERSCRIPT_IKO + '" alt="' + EDITOR_SUPERSCRIPT_ALT + '" title="' + EDITOR_SUPERSCRIPT_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'superscript\', \'\')"></td>');
			document.writeln('		<td width="25"><img class="editImage" src="' + EDITOR_SUBSCRIPT_IKO + '" alt="' + EDITOR_SUBSCRIPT_ALT + '" title="' + EDITOR_SUBSCRIPT_ALT + '" onClick="editCommand(\'' + this.edit + '\', \'subscript\', \'\')"></td>');
			document.writeln('		<td width="100%">&nbsp;</td>');
            document.writeln('	</tr>');
			document.writeln('</table>');
		}
		document.writeln('<iframe id="' + this.edit + '" name="' + this.edit + '" width="' + this.width + 'px" height="' + this.height + 'px"></iframe>');
        document.writeln('</div>');

        this.enableDesignMode(this.html);

	} else {
		if (!this.readonly) {
			document.writeln('<textarea style="background: ' + EDITOR_HTMLMODE_BACKGROUND_COLOR + ';color: ' + EDITOR_HTMLMODE_COLOR + ';font-family: ' + EDITOR_HTMLMODE_FONT_FAMILY + ';font-size: ' + EDITOR_HTMLMODE_FONT_SIZE + ';width: ' + this.width + 'px; height: ' + this.height + 'px;" name="' + this.edit + '" id="' + this.edit + '">' + this.html + '</textarea>');
		} else {
			document.writeln('<textarea style="background: ' + EDITOR_HTMLMODE_BACKGROUND_COLOR + ';color: ' + EDITOR_HTMLMODE_COLOR + ';font-family: ' + EDITOR_HTMLMODE_FONT_FAMILY + ';font-size: ' + EDITOR_HTMLMODE_FONT_SIZE + ';width: ' + this.width + 'px; height: ' + this.height + 'px;" name="' + this.edit + '" id="' + this.edit + '">' + this.html + '</textarea>');
		}
	}

}


function CSEditorenableDesignMode() {
	var frameHtml = "<html id=\"" + this.edit + "\">\n";
	frameHtml += "<head>\n";
	frameHtml += "<style>\n";
	frameHtml += "body {\n";
	frameHtml += "	background: " + EDITOR_HTMLMODE_BACKGROUND_COLOR + ";\n";
	frameHtml += "	color: " + EDITOR_HTMLMODE_COLOR + ";\n";
    frameHtml += "	font-family: " + EDITOR_HTMLMODE_FONT_FAMILY + ";\n";
	frameHtml += "	font-size: " + EDITOR_HTMLMODE_FONT_SIZE + ";\n";
	frameHtml += "	margin: " + EDITOR_HTMLMODE_MARGIN + ";\n";
	frameHtml += "	padding: " + EDITOR_HTMLMODE_PADDING + ";\n";
	frameHtml += "}\n";
	frameHtml += "p {\n";
	frameHtml += "	margin: 0px;\n";
	frameHtml += "}\n";
	frameHtml += "ul,ol {\n";
	frameHtml += "	margin-top: 10px;\n";
	frameHtml += "	margin-bottom: 10px;\n";
	frameHtml += "}\n";
	frameHtml += "</style>\n";
	frameHtml += "</head>\n";
	frameHtml += "<body>\n";
	frameHtml += this.html + "\n";
    frameHtml += "</body>\n";
	frameHtml += "</html>";
	
	if (document.all) {
		var objEdit = frames[this.edit].document;
		objEdit.open();
		objEdit.write(frameHtml);
		objEdit.close();
		if (!this.readonly) objEdit.designMode = "On";
	} else {
		try {
			if (!this.readonly) document.getElementById(this.edit).contentDocument.designMode = "on";
			try {
				var objEdit = document.getElementById(this.edit).contentWindow.document;
				objEdit.open();
				objEdit.write(frameHtml);
				objEdit.close();
				if (esGecko && !this.readonly) {
					//attach a keyboard handler for gecko browsers to make keyboard shortcuts work
					objEdit.addEventListener("keypress", kb_handler, true);
				}
			} catch (e) {
				alert("Error preloading content.");
			}
		} catch (e) {
			//gecko may take some time to enable design mode.
			//Keep looping until able to set.
			if (esGecko) {
				setTimeout("CSEditorenableDesignMode();", 10);
			} else {
				return false;
			}
		}
	}
}


function CSEditorGetHTML()
{
  var objEdit,cadena;

  if (esRichText) {

	if (document.all) {
		objEdit = frames[this.edit].document;
	} else {
		objEdit = document.getElementById(this.edit).contentWindow.document;
	}

    cadena = objEdit.body.innerHTML;
  
  } else {
	  
    cadena = document.getElementById(this.edit).value;

  }

  cadena = cadena.replace(/'/ig," ");
  cadena = cadena.replace(/`/ig," ");
  cadena = cadena.replace(/´/ig," ");

  return cadena;

}


function CSEditorSetHTML(html)
{
  var objEdit;

  if (esRichText) {

    this.html = html;
	this.enableDesignMode();
   
  } else {
	  
    document.getElementById(this.edit).value = html;

  }

}

function stripHTML(oldString) {
  //function to strip all html
  var newString = oldString.replace(/(<([^>]+)>)/ig,"");
	
  //replace carriage returns and line feeds
  newString = newString.replace(/\r\n/g," ");
  newString = newString.replace(/\n/g," ");
  newString = newString.replace(/\r/g," ");
	
  //trim string
  newString = trim(newString);
	
  return newString;
}

function trim(inputString) {
   if (typeof inputString != "string") return inputString;
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
	
   while (ch == " ") {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length - 1, retValue.length);
	
   while (ch == " ") {
      retValue = retValue.substring(0, retValue.length - 1);
      ch = retValue.substring(retValue.length - 1, retValue.length);
   }
	
   while (retValue.indexOf("  ") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length);
   }
   return retValue;
}

function CSEditorGetText()
{
  var html,texto;
  
  html = this.GetHTML();
  texto = stripHTML(html);

  texto = texto.replace(/'/ig," ");
  texto = texto.replace(/`/ig," ");
  texto = texto.replace(/´/ig," ");

  return texto;


}

//function CSEditorGetText()
//{
//  var objEdit;
//  var texto;

//  if (esRichText) {

//	if (document.all) {
//		objEdit = frames[this.edit].document;
//	} else {
//		objEdit = document.getElementById(this.edit).contentWindow.document;
//	}

//    texto = objEdit.body.innerText;
//    if (texto == undefined) { return ""; } else { return texto; }
  
//  } else {
    
//	texto = document.getElementById(this.edit).value; 
//    if (texto == undefined) { return ""; } else { return texto; }

//  }

//}




function editCommand(edit, command, option) {
	var objEdit;
	if (document.all) {
		objEdit = frames[edit];
	} else {
		objEdit = document.getElementById(edit).contentWindow;
	}
	
	try {
		objEdit.focus();
	  	objEdit.document.execCommand(command, false, option);
		objEdit.focus();
	} catch (e) {
//		alert(e);
//		setTimeout("editCommand('" + edit + "', '" + command + "', '" + option + "');", 10);
	}
}

function newText(edit) {
	var objEdit;
	if (document.all) {
		objEdit = frames[edit].document;
	} else {
		objEdit = document.getElementById(edit).contentWindow.document;
	}

    objEdit.body.innerHTML = "";
}

function kb_handler(evt) {
	var edit = evt.target.id;
	
	if (evt.ctrlKey) {
		var key = String.fromCharCode(evt.charCode).toLowerCase();
		var cmd = '';
		switch (key) {
			case 'b': cmd = "bold"; break;
			case 'i': cmd = "italic"; break;
			case 'u': cmd = "underline"; break;
		};

		if (cmd) {
			editCommand(edit, cmd, null);
			
			// stop the event bubble
			evt.preventDefault();
			evt.stopPropagation();
		}
 	}
}

function raiseButton(e) {
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'editImage' || className == 'editImageLowered') {
		el.className = 'editImageRaised';
	}
}

function normalButton(e) {
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'editImageRaised' || className == 'editImageLowered') {
		el.className = 'editImage';
	}
}

function lowerButton(e) {
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'editImage' || className == 'editImageRaised') {
		el.className = 'editImageLowered';
	}
}
