function toHTML(cadena)
{
	var i;
	var salida='';

	for (i=0;i<cadena.length;i++)
	{
		if (cadena.charAt(i) == '"') salida = salida + '&quot;';
		else 
		if (cadena.charAt(i) == '<') salida = salida + '&lt;';
		else
		if (cadena.charAt(i) == '>') salida = salida + '&gt;';
		else
		salida = salida + cadena.charAt(i);
	}
	return salida;
}

function insertar(obj, texto)
{
	obj.focus();
	var sele = window.document.selection.createRange();
	sele.text = texto;
	//sele.pasteHTML(texto);
}
function insertarHTML(obj, texto)
{
	obj.focus();
	var sele = window.document.selection.createRange();
	//sele.text = texto;
	sele.pasteHTML(texto);
}

function pegar(obj)
{
	event.returnValue = false;
    var aux='';
    
    try
    {	
        var range = document.selection.createRange();        
        //alert(range.getBookmark().charCodeAt(2) - 2);
        //alert(range.offsetLeft);

	    var cbd = clipboardData.getData("Text");
	    var resultado = ''
    	
	    if (cbd!='')
	    {
	        cbd = reemplazar(cbd , String.fromCharCode(13), '');
	        var arr = cbd.split('\n');
	        var html = '';
	        for (i=0;i<arr.length;i++)
	        {

                if (i==0 && obj.innerText!='')
                {
                    resultado = arr[i];
                    
                    resultado = reemplazar(resultado,'&','&amp;');
                    resultado = reemplazar(resultado,'<','&lt;');
                    resultado = reemplazar(resultado,'>','&gt;');
                    resultado = reemplazar(resultado,'>','&quot;');
                    resultado = reemplazar(resultado,'\'','&acute;');
                    if (trim(resultado) != '' && resultado.charCodeAt(0)!=13)
                    {
                        html += resultado;
                    }
                }
                else
                {
                    resultado = arr[i];
                    
                    resultado = reemplazar(resultado,'&','&amp;');
                    resultado = reemplazar(resultado,'<','&lt;');
                    resultado = reemplazar(resultado,'>','&gt;');
                    resultado = reemplazar(resultado,'>','&quot;');
                    resultado = reemplazar(resultado,'\'','&acute;');
                    if (trim(resultado) != '')
                    {
                        html += '<p>'+resultado+'</p>';
                    }
                }
		    }
		    
	    }
	    insertarHTML(obj, html);
	}
	catch(e)
	{
	}
}
function edModificar(obj)
{
    return;
	//alert(obj.tagName)
	alert(obj.tagName);
	
    var newRow=obj.insertRow(0); //1=below  0=above
    for (i=0; i < 2; i++){
    var newCell=newRow.insertCell(i);
    newCell.innerHTML="#";
   }

}


function edNegrita(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand('bold', false, null);
}
function edCursiva(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand('italic', false, null);
}
function edSubrayado(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand('underline', false, null);
}
function edPrueba(id)
{
	var obj = document.getElementById(id);
	obj.focus();
    obj.document.execCommand("FormatBlock", false, "<h2>"); 
}
function edIzquierda(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand('JustifyLeft', false, null);
}
function edCentrado(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand('JustifyCenter', false, null);
}
function edDerecha(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand('JustifyRight', false, null);
}
function edJustificado(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand('JustifyFull', false, null);
}
function edListaO(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand("InsertOrderedList", false, null); 
}
function edListaD(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand("InsertUnorderedList", false, null); 
}
function edImagen(id, url)
{
    var obj = document.getElementById(id);
    try
    {
        var sele = window.document.selection.createRange();
        if (sele(0).tagName=='IMG')
        {
            obj.document.execCommand("InsertImage", true);
            return;
        }
    }
    catch(e)
    {
    }

    var imagen = window.showModalDialog(url+'abrirModal.aspx?abrir=gestionImagenes.aspx', '', 'dialogWidth=640px;dialogHeight=480px');
    obj.focus();

    if (imagen!=undefined)
    {
        obj.document.execCommand("InsertImage", false, url+imagen);
        obj.document.execCommand("InsertImage", true);
        //obj.style.textAlign='right';
        //obj.document.execCommand("InsertImage", true, url+imagen);
    }


/*
    var sele = window.document.selection;
    alert(sele.type);
    return;
*/


}
function edEnlace(id, url)
{
	var obj = document.getElementById(id);
	obj.focus();

	if (window.document.selection.createRange().text=='')
	{
		alert('Select a text or an image.');
		return;
	}

	var tipoEnlace = window.showModalDialog(url+'abrirModal.aspx?abrir=tipoEnlace.aspx', '', 'dialogWidth=240px;dialogHeight=180px');

	if (tipoEnlace!=undefined)
	{
	    if (tipoEnlace[0]==1)
	    {
	        if (tipoEnlace[1]!=undefined)
	        {
        	    obj.document.execCommand("CreateLink", false, tipoEnlace[1]);
        	}
        }
	    else
	    {
	        obj.document.execCommand("CreateLink", true, "");
	    }
	}
}

function edCarpeta(id, url)
{
	if (window.document.selection.createRange().text=='')
	{
		alert('Select a text or an image.');
		return;
	}
	var obj = document.getElementById(id);
	var rutaArchivo = window.showModalDialog(url+'abrirModal.aspx?abrir=gestionArchivos.aspx', '', 'dialogWidth=640px;dialogHeight=480px');
	obj.focus();
	
	if (rutaArchivo!=undefined)
	{
    		obj.document.execCommand("CreateLink", false, rutaArchivo); 
	}
}

function edIndent(id)
{
	var obj = document.getElementById(id);
	obj.focus();

	obj.document.execCommand("indent", false, ""); 
}

function edOutdent(id)
{
	var obj = document.getElementById(id);
	obj.focus();

	obj.document.execCommand("outdent", false, ""); 
}

function edTabla(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	var sele = window.document.selection.createRange();
	//sele.text = texto;
	try
	{
		sele.pasteHTML('<table border=1><tr><td>1<td>d</table>');
	}
	catch(e){}
}

function edNormal(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand("FormatBlock", false, "<p>"); 
}

function edTitulo(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand("FormatBlock", false, "<h1>"); 
}

function edTitulo2(id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.document.execCommand("FormatBlock", false, "<h2>"); 
}

function edEtiquetas(id, url)
{
	var obj = document.getElementById(id);
	var etiqueta = window.showModalDialog(url+'abrirModal.aspx?abrir=editorEtiquetas.aspx', '', 'dialogWidth=640px;dialogHeight=480px');
	obj.focus();
	var sele = window.document.selection.createRange();

	if (etiqueta!=undefined)
	{
	    sele.pasteHTML(etiqueta);
	}
}

function editorHTMLURL(nombre, htmlContenido, readOnly, url)
{
	var html='';
	var aux='true';
	if (readOnly==true) aux = 'false';

	html += '<table cellpaddin=0 cellspacing=0>';
	html += '<tr>';

    if (readOnly!=true)
    {
	    html += '<td style="font-size:1px;height:24px;background:url('+url+'img/degradadoBarraHerramientas.png);padding-left:5px;">';
	    html += 	'<img onClick="edNegrita(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorNegrita.png" onMouseOver="this.src=\''+url+'img/editorNegritaOn.png\'" onMouseOut="this.src=\''+url+'img/editorNegrita.png\'" width=23 height=22>'
	    html += 	'<img onClick="edCursiva(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorCursiva.png" onMouseOver="this.src=\''+url+'img/editorCursivaOn.png\'" onMouseOut="this.src=\''+url+'img/editorCursiva.png\'" width=23 height=22>'
	    html += 	'<img onClick="edSubrayado(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorSubrayado.png" onMouseOver="this.src=\''+url+'img/editorSubrayadoOn.png\'" onMouseOut="this.src=\''+url+'img/editorSubrayado.png\'" width=23 height=22>'
	    html += 	'<img onClick="edIzquierda(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorIzquierda.png" onMouseOver="this.src=\''+url+'img/editorIzquierdaOn.png\'" onMouseOut="this.src=\''+url+'img/editorIzquierda.png\'" width=23 height=22>'
	    html += 	'<img onClick="edCentrado(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorCentrado.png" onMouseOver="this.src=\''+url+'img/editorCentradoOn.png\'" onMouseOut="this.src=\''+url+'img/editorCentrado.png\'" width=23 height=22>'
	    html += 	'<img onClick="edDerecha(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorDerecha.png" onMouseOver="this.src=\''+url+'img/editorDerechaOn.png\'" onMouseOut="this.src=\''+url+'img/editorDerecha.png\'" width=23 height=22>'
	    html += 	'<img onClick="edJustificado(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorJustificado.png" onMouseOver="this.src=\''+url+'img/editorJustificadoOn.png\'" onMouseOut="this.src=\''+url+'img/editorJustificado.png\'" width=23 height=22>'
    	
	    html += 	'<img onClick="edListaO(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorListaOrdenada.png" onMouseOver="this.src=\''+url+'img/editorListaOrdenadaOn.png\'" onMouseOut="this.src=\''+url+'img/editorListaOrdenada.png\'" width=23 height=22>'
	    html += 	'<img onClick="edListaD(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorListaDesOrdenada.png" onMouseOver="this.src=\''+url+'img/editorListaDesOrdenadaOn.png\'" onMouseOut="this.src=\''+url+'img/editorListaDesOrdenada.png\'" width=23 height=22>'

	    html += 	'<img onClick="edOutdent(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorOutdent.png" onMouseOver="this.src=\''+url+'img/editorOutdentOn.png\'" onMouseOut="this.src=\''+url+'img/editorOutdent.png\'" width=23 height=22>'
	    html += 	'<img onClick="edIndent(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorIndent.png" onMouseOver="this.src=\''+url+'img/editorIndentOn.png\'" onMouseOut="this.src=\''+url+'img/editorIndent.png\'" width=23 height=22>'

	    html += 	'<img onClick="edImagen(\''+nombre+'\',\''+url+'\')" style="cursor:pointer;" src="'+url+'img/editorImagen.png" onMouseOver="this.src=\''+url+'img/editorImagenOn.png\'" onMouseOut="this.src=\''+url+'img/editorImagen.png\'" width=23 height=22>'
	    html += 	'<img onClick="edEnlace(\''+nombre+'\',\''+url+'\')" style="cursor:pointer;" src="'+url+'img/editorEnlace.png" onMouseOver="this.src=\''+url+'img/editorEnlaceOn.png\'" onMouseOut="this.src=\''+url+'img/editorEnlace.png\'" width=23 height=22>'
	    html += 	'<img onClick="edCarpeta(\''+nombre+'\',\''+url+'\')" style="cursor:pointer;" src="'+url+'img/editorCarpeta.png" onMouseOver="this.src=\''+url+'img/editorCarpetaOn.png\'" onMouseOut="this.src=\''+url+'img/editorCarpeta.png\'" width=23 height=22>'

	    html += 	'<img onClick="edNormal(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorNormal.png" onMouseOver="this.src=\''+url+'img/editorNormalOn.png\'" onMouseOut="this.src=\''+url+'img/editorNormal.png\'" width=50 height=22>'
	    html += 	'<img onClick="edTitulo(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorTitulo.png" onMouseOver="this.src=\''+url+'img/editorTituloOn.png\'" onMouseOut="this.src=\''+url+'img/editorTitulo.png\'" width=46 height=22>'
	    html += 	'<img onClick="edTitulo2(\''+nombre+'\')" style="cursor:pointer;" src="'+url+'img/editorTitulo2.png" onMouseOver="this.src=\''+url+'img/editorTitulo2On.png\'" onMouseOut="this.src=\''+url+'img/editorTitulo2.png\'" width=50 height=22>'

	    html += 	'<img alt="Etiquetas Especiales" onClick="edEtiquetas(\''+nombre+'\',\''+url+'\')" style="margin-left:5px;cursor:pointer;" src="'+url+'img/editorEtiquetas.png" onMouseOver="this.src=\''+url+'img/editorEtiquetasOn.png\'" onMouseOut="this.src=\''+url+'img/editorEtiquetas.png\'" width=30 height=22>'

	    html +='</td>';
    }
	html += '<input type="hidden" name="'+nombre+'_hidden" id="id_'+nombre+'_hidden" value="'+toHTML(htmlContenido)+'">';

	html += '<tr><td style="border:1px solid gray">';
	html += '<div id="'+nombre+'" contenteditable="'+ aux +'" class="editorHTML" onPaste="pegar(this)" onDblClick="edModificar(event.srcElement);" onBlur="document.getElementById(\'id_'+nombre+'_hidden\').value=this.innerHTML;">'+htmlContenido+'</div>';
	html += '</table>';


	document.write(html);
}
function editorHTML(nombre, htmlContenido)
{
    editorHTMLURL(nombre, htmlContenido, false, '');
}
function editorHTMLReadOnly(nombre, htmlContenido)
{
    editorHTMLURL(nombre, htmlContenido, true, '');
}
