var winArea;
var htmlMode;
var ObjectInserted = false;
var P = "";
P += "<nobr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Bold\")' title=\"Negrita\"><img src='img/bold.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Italic\");' title=\"Cursiva\"><img src='img/italic.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Underline\");' title=\"Subrayada\"><img src='img/underline.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1><wbr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"JustifyLeft\");' title=\"Alinear a la izquierda\"><img src='img/left.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"JustifyCenter\");' title=\"Centrar\"><img src='img/center.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"JustifyRight\");' title=\"Alinear a la derecha\"><img src='img/right.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1><wbr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Cut\");' title=\"Cortar\"><img src='img/cut.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Copy\");' title=\"Copiar\"><img src='img/copy.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Paste\");' title=\"Pegar\"><img src='img/paste.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1><wbr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Undo\");' title=\"Deshacer\"><img src='img/undo.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Redo\");' title=\"Rehacer\"><img src='img/redo.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1><wbr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"InsertOrderedList\");' title=\"Numeraci&oacute;n\"><img src='img/ol.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"InsertUnorderedList\");' title=\"Vi&ntilde;etas\"><img src='img/ul.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1><wbr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Outdent\");' title=\"Reducir sangr&iacute;a\"><img src='img/outdent.gif' width=16 height=16 border=0></button>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"Indent\");' title=\"Aumentar sangr&iacute;a\"><img src='img/indent.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1><wbr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"InsertHorizontalRule\");' title=\"Insertar l&iacute;nea horizontal\"><img src='img/hr.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1><wbr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"InsertImage\");' title=\"Insertar imagen\"><img src='img/image.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1><wbr>\n";
P += "<button class=btn unselectable=on onclick='editCallCommand(\"InsertFile\");' title=\"Archivo documental\"><img src='img/file.gif' width=16 height=16 border=0></button>\n";
P += "<wbr><img src='#' width=1 height=1><wbr>\n";
P += "<button class=btn unselectable=on onclick='editCallCommand(\"CreateLink\");' title=\"Insertar enlace\"><img src='img/a.gif' width=16 height=16 border=0></button>\n";
P += "<wbr><img src='#' width=1 height=1><wbr>\n";
//P += "<button class=btn unselectable=on onclick='editCallCommand(\"RemoveFormat\");' title=\"Borrar formato\"><img src='img/new.gif' width=16 height=16 border=0></button>\n";
//P += "<wbr><img src='#' width=1 height=1>";
//P += "<button class=btn unselectable=on onclick='showHtml()' title=\"Código HTML\"><img src='img/html.gif' width=16 height=16 border=0></button>\n";
//P += "<br>";

//P += "%OBJECT%";

/*P += "<wbr><img src='#' width=1 height=1><wbr><select onchange='editCallCommand(\"FontName\",this[this.selectedIndex].text); this.selectedIndex=0;'>\n";
P += "<option selected>- Fuente -</option>\n";
P += "<option></option>\n";
P += "<option>Arial</option>\n";
P += "<option>Comic Sans MS</option>\n";
P += "<option>Courier New</option>\n";
P += "<option>Garamond</option>\n";
P += "<option>Georgia</option>\n";
P += "<option>Tahoma</option>\n";
P += "<option>Times New Roman</option>\n";
P += "<option>Trebuchet MS</option>\n";
P += "<option>Verdana</option>\n";
P += "</select>&nbsp;<wbr><select onchange='editCallCommand(\"FontSize\", this[this.selectedIndex].text);this.selectedIndex=0;'>\n";
P += "<option>- Tama&ntilde;o -</option>\n";
P += "<option></option>\n";
P += "<option>1</option>\n";
P += "<option>2</option>\n";
P += "<option>3</option>\n";
P += "<option>4</option>\n";
P += "<option>5</option>\n";
P += "</select>";*/
/*P += "&nbsp;<wbr><select unselectable=on onchange='editCallCommand(\"ForeColor\", this[this.selectedIndex].text); this.selectedIndex=0;'>\n";
P += "<option selected>- Color -</option>\n";
P += "<option style='background-color: #000000'>#000000</option>\n";
P += "<option style='background-color: #000066'>#000066</option>\n";
P += "<option style='background-color: #006600'>#006600</option>\n";
P += "<option style='background-color: #660000'>#660000</option>\n";
P += "<option style='background-color: #006666'>#006666</option>\n";
P += "<option style='background-color: #666600'>#666600</option>\n";
P += "<option style='background-color: #660066'>#660066</option>\n";
P += "<option style='background-color: #666666'>#666666</option>\n";
P += "<option></option>\n";
P += "<option style='background-color: #000088'>#000088</option>\n";
P += "<option style='background-color: #008800'>#008800</option>\n";
P += "<option style='background-color: #880000'>#880000</option>\n";
P += "<option style='background-color: #008888'>#008888</option>\n";
P += "<option style='background-color: #888800'>#888800</option>\n";
P += "<option style='background-color: #880088'>#880088</option>\n";
P += "<option style='background-color: #888888'>#888888</option>\n";
P += "<option></option>\n";
P += "<option style='background-color: #0000aa'>#0000aa</option>\n";
P += "<option style='background-color: #00aa00'>#00aa00</option>\n";
P += "<option style='background-color: #aa0000'>#aa0000</option>\n";
P += "<option style='background-color: #00aaaa'>#00aaaa</option>\n";
P += "<option style='background-color: #aaaa00'>#aaaa00</option>\n";
P += "<option style='background-color: #aa00aa'>#aa00aa</option>\n";
P += "<option style='background-color: #aaaaaa'>#aaaaaa</option>\n";
P += "<option></option>\n";
P += "<option style='background-color: #0000ff'>#0000ff</option>\n";
P += "<option style='background-color: #00ff00'>#00ff00</option>\n";
P += "<option style='background-color: #ff0000'>#ff0000</option>\n";
P += "<option style='background-color: #00ffff'>#00ffff</option>\n";
P += "<option style='background-color: #ffff00'>#ffff00</option>\n";
P += "<option style='background-color: #ff00ff'>#ff00ff</option>\n";
P += "<option style='background-color: #ffffff'>#ffffff</option>\n";
P += "</select>\n";
P += "</nobr>\n";*/


//*****************************************************************************
function str_replace(a, b, s) {
	if(6 > parseInt(navigator.appVersion.substr(navigator.appVersion.indexOf("MSIE")+5, navigator.appVersion.length)))
		return str_replace_slow(a, b, s);
	if(!b || b == null || !b.length) return s;
	r = eval('new RegExp(/'+(""+a)+'/gi);');
	return(s.replace(r, ""+b));
}
function str_replace_slow(a, b, s) {
//	window.status = "ATENCIÓN! Usando algoritmo lento";
	if(!b || b == null || !b.length) return s;
	for(idx = 0; idx < s.length - a.length; idx++)
		if(s.substring(idx, idx+a.length) == a) {
			s = s.substring(0, idx) + b + s.substring(idx + a.length, s.length);
			idx += b.length;
		}
	return s;
}

//*****************************************************************************
function editCallCommand(cmd, param) {
	if("FontName" == cmd || "FontSize" == cmd || "ForeColor" == cmd || "FormatBlock" == cmd) {
		edit.document.execCommand(cmd, true, param);
	} else if("InsertImage" == cmd) {
		if(!edit.isFocused) return false;
		edit.document.execCommand(cmd, true, "");
	} else if("InsertFile" == cmd) {
		if(!edit.isFocused) return false;
		interToolCommunication(1);
	} else if(cmd.length > 0) {
		if(!edit.isFocused) return false;
		edit.document.execCommand(cmd);
	}
}

//*****************************************************************************
function editSubmit(win, fld) {
	if(win.tagName && "SPAN" == win.tagName) return false;
	document.all.editControls.innerHTML = '<br>';
	fld.value = (htmlMode && edit == win) ? document.getElementById('edithtml').value : win.document.body.innerHTML;
}

//*****************************************************************************
function editReset(win) {
	if(win.tagName && "SPAN" == win.tagName) return false;
	if('edit' == win.name) winArea = win;

	win.document.body.innerHTML = win.parent[win.parent.document.getElementById(win.name).srcvar]
		? win.parent[win.parent.document.getElementById(win.name).srcvar]
		: "";
//	if('edit' == win.name) win.parent.document.getElementById('edithtml').value = html;

	if(!ObjectInserted) {
		var S = "";
		var DH = document.getElementById('datos');
		if(DH && DH.blockFormats) {
			S += "<wbr><select onchange='editCallCommand(\"FormatBlock\",this[this.selectedIndex].text); this.selectedIndex=0;'>\n";
			S += "<option selected>- Formato del bloque -</option>\n";
			S += "<option></option>";
			for(i=1; i <= DH.blockFormats.count; i++) {
				S += "<option>"+DH.blockFormats(i)+"</option>\n";
			}
			S += "</select>&nbsp;";
		}
		P = str_replace("%OBJECT%", S, P);
		ObjectInserted = true;
	}

	document.all.editControls.innerHTML = P;
}

//*****************************************************************************
function showHtml() {
	return false;
	var ta = document.getElementById('edithtml');
	var fr = document.getElementById('edit');

	htmlMode = !htmlMode;

	if(htmlMode) {
		ta.value = winArea.document.body.innerHTML;
		ta.style.display = 'block';
		fr.style.display = 'none';
	} else {
		winArea.document.body.innerHTML = ta.value;
		ta.style.display = 'none';
		fr.style.display = 'block';
	}
	return false;
}

//*****************************************************************************
function editFocus(win, mod) {
	win.isFocused = mod;
}

//*****************************************************************************
function interToolCommunication(mod) {
	var pageUrl, urlType;
	var W = 400;
	var H = 300;
	switch(mod) {
		case 1 :
			pageUrl = "winarc.php";
			urlType = "ARCHIVO";
			break;

		default : return false;
	}

	// R is an array in format of [ID, Description]
	R = window.showModalDialog(pageUrl, '_blank', 'dialogWidth:'+W+'px; dialogHeight:'+H+'px; dialogTop:?; dialogLeft:?; edge:Sunken; center:Yes; help:No; resizable:Yes; status:No; unadorned:1;');
	if(!R) return false;
	var r = document.selection.createRange();

/*
	r.pasteHTML(R[1]);
	r.moveStart("character", -R[1].length);
	r.select();
	document.execCommand('createLink', false, R[0]);
	r.moveStart("character", R[1].length);
	r.select();
*/
	if(!R[1] && !R[2])
	{
		r.pasteHTML(R[0]);
	} else {
		r.pasteHTML(R[2]+"&nbsp;");							// insertar imagen con espacio separador
		r.pasteHTML(R[1]);									// insertar texto de descripcion
		r.moveStart("character", -R[1].length);				// ponerse al principio del texto
		r.select();											// seleccionar el texto
		document.execCommand('createLink', false, R[0]);	// crear enlace
		r.move("character", R[1].length + 0);				// ponerse al final del texto insertado
		r.select();											// deseleccionar todo
	}

}
