if (typeof _editor_url == "string") {
	// Leave exactly one backslash at the end of _editor_url
	_editor_url = _editor_url.replace(/\x2f*$/, '/');
} else {
	_editor_url = '';
}

// make sure we have a language
if (typeof _editor_lang == "string") {
	_editor_lang = _editor_lang.toLowerCase();
} else {
	_editor_lang = "sp";
}
function NINBOX(textarea,config) {
        this.config=config;
        this.tablesprop="yes";
		this.interface="full";
		this.fullPage=0;
        this.fullenable=1;
       if(this.config!=undefined){
         if(this.config["fullscreen"]==undefined)
             this.fullpage=0;
         else
             this.fullpage=this.config["fullscreen"];
		if(this.config["interface"]==undefined)
             this.interface="full";
         else
             this.interface=this.config["interface"];	 
        if(this.config["pdefault"]==undefined)
             this.pdefault="<p>";
         else
             this.pdefault=this.config["pdefault"];

        if(this.config["fdefault"]==undefined)
             this.fdefault="Times New Roman";
         else
             this.fdefault=this.config["pdefault"]; 
		
		 if(this.config["tablesprop"]==undefined)
             this.tablesprop="yes";
         else
             this.tablesprop=this.config["tablesprop"];
    
        if(this.config["fsdefault"]==undefined)
             this.fsdefault="3";
         else
             this.fsdefault=this.config["fsdefault"];       

         } else{
            this.fsdefault="3";
            this.fdefault="Times New Roman";
            this.pdefault="<p>";
            this.fullpage=0;
			this.interface="full";
			this.table
         } 
        this.busy=0;
        this.path=null;
		this._htmlArea = null;
		this._textArea = textarea;
		this._editMode = "wysiwyg";
		this.doctype = '';
		this.version='1';
        this.path=null;
		this.editor="";  //editor 
		

	this.fontname = {
		"Arial":	   'arial,helvetica,sans-serif',
		"Courier New":	   'courier new,courier,monospace',
		"Georgia":	   'georgia,times new roman,times,serif',
		"Tahoma":	   'tahoma,arial,helvetica,sans-serif',
		"Times New Roman": 'times new roman,times,serif',
		"Verdana":	   'verdana,arial,helvetica,sans-serif',
		"impact":	   'impact',
		"WingDings":	   'wingdings'
	};

	this.fontsize = {
		"1 (8 pt)":  "1",
		"2 (10 pt)": "2",
		"3 (12 pt)": "3",
		"4 (14 pt)": "4",
		"5 (18 pt)": "5",
		"6 (24 pt)": "6",
		"7 (36 pt)": "7"
	};
   
	this.formatblock = {
		"Heading 1": "h1",
		"Heading 2": "h2",
		"Heading 3": "h3",
		"Heading 4": "h4",
		"Heading 5": "h5",
		"Heading 6": "h6",
		"Normal": "p",
		"Address": "address",
		"Formatted": "pre"
	};
	
	//this.customSelects = {"fontname","fontsize","formatblock"};
	if(this.interface=="minimo"||this.interface=="reduce")
	  this.tablesprop="no";
	this.toolbar=setinterface(this.interface);
    
    this.maximizar= {0:"fullscreen",1:"fullscreen.gif",2:"false",3:"1",4:"Maximizar"};
    this.minimizar= {0:"minscreen",1:"minscreen.gif",2:"false",3:"1",4:"Minimizaa"};
	
	    this.toolbartable = {
		 nl2:[ "NL", null, null, 3,null ],
		 tableprop:[ "tableprop", "tableprop.gif", false, 1,"Propiedades de la tabla" ],
		 space1:[ "space", null, null, 3,null ],
		 rowprop:[ "rowprop", "rowprop.gif", false, 1,"Propiedades de la fila" ],
		 space2:[ "space", null, null, 3,null ],
		 cellprop:[ "cellprop", "cellprop.gif", false, 1,"Propiedades de la Celda" ],
		 space3:[ "space", null, null, 3,null ],
		 tablesplit:[ "tablesplit", "split.gif", false, 1,"Dividir Celdas" ],
		 space4:[ "space", null, null, 3,null ],
		 tablejoin:[ "tablesjoin", "join.gif", false, 1,"Unir Celdas" ]
		}
}
NINBOX.RE_tagName = /(<\/|<)\s*([^ \t\n>]+)/ig;
NINBOX.RE_doctype = /(<!doctype((.|\n)*?)>)\n?/i;
NINBOX.RE_head    = /<head>((.|\n)*?)<\/head>/i;
NINBOX.RE_body    = /<body>((.|\n)*?)<\/body>/i;
NINBOX.agt = navigator.userAgent.toLowerCase();
NINBOX.is_ie	   = ((NINBOX.agt.indexOf("msie") != -1) && (NINBOX.agt.indexOf("opera") == -1));
NINBOX.is_opera  = (NINBOX.agt.indexOf("opera") != -1);
NINBOX.is_mac	   = (NINBOX.agt.indexOf("mac") != -1);
NINBOX.is_mac_ie = (NINBOX.is_ie && NINBOX.is_mac);
NINBOX.is_win_ie = (NINBOX.is_ie && !NINBOX.is_mac);
NINBOX.is_gecko  = (navigator.product == "Gecko");
NINBOX.replaceAll = function(config) {
	var tas = document.getElementsByTagName("textarea");
	for (var i = tas.length; i > 0; (new NINBOX(tas[--i], config)).generate());
};

/** Helper function: replaces the TEXTAREA with the given ID with HTMLArea. */
NINBOX.replace = function(id, config) {
	var ta = NINBOX.getElementById("textarea", id);
	return ta ? (new NINBOX(ta, config)).generate() : null;
};
NINBOX.prototype.generate = function () {

	var editor = this;	// we'll need "this" in some nested functions
	// get the textarea
	var textarea = this._textArea;

	if (typeof textarea == "string") {
		// it's not element but ID
		this._textArea = textarea = NINBOX.getElementById("textarea", textarea);
	}
	if(textarea==null)
	  alert("Revisa el id del texarea coincide en el parametro pasado en Ninbox()");
	  
	this._ta_size = {
		w: textarea.offsetWidth,
		h: textarea.offsetHeight
		
	};
	
	textarea.style.display = "none";
    
	// create the editor framework
	var htmlarea = document.createElement("div");
	//htmlarea.className = "htmlarea";
	this._htmlArea = htmlarea;

	// insert the editor before the textarea.
	textarea.parentNode.insertBefore(htmlarea, textarea);
   
	if (textarea.form) {
	  	// we have a form, on submit get the HTMLArea content and
		// update original textarea.
		var f = textarea.form;
		
		f.onsubmit = function() {
			editor._textArea.value = editor.getInnerHTML();
		};
	}
	// add a handler for the "back/forward" case -- on body.unload we save
	// the HTML content into the original textarea.
	// add a handler for the "back/forward" case -- on body.unload we save
	// the HTML content into the original textarea.
	window.onunload = function() {
		editor._textArea.value = editor.getInnerHTML();
	};

	// creates & appends the toolbar
	this._createToolbar();
 
	// create the IFRAME
	var iframe = document.createElement("iframe");
	htmlarea.appendChild(iframe);

	this._iframe = iframe;

	// creates & appends the status bar, if the case
	
	this._createStatusBar();
	
	// remove the default border as it keeps us from computing correctly
	// the sizes.  (somebody tell me why doesn't this work in IE)

	if (!NINBOX.is_ie) {
		iframe.style.borderWidth = "1px";
	// iframe.frameBorder = "1";
	// iframe.marginHeight = "0";
	// iframe.marginWidth = "0";
	}

	// size the IFRAME according to user's prefs or initial textarea
	var height = this._ta_size.h + "px";
	height = parseInt(height);
	var width = this._ta_size.w + "px";
	width = parseInt(width);
    
	
	if (!NINBOX.is_ie) {
		height -= 2;
		width -= 2;
	}

	iframe.style.width = width + "px";
	if (height < 0) {
		height = 0;
	}
	iframe.style.height = height + "px";
    

	// the editor including the toolbar now have the same size as the
	// original textarea.. which means that we need to reduce that a bit.
	textarea.style.width = iframe.style.width;
 	textarea.style.height = iframe.style.height;
    this._toolbar.style.width=iframe.style.width;
	this._statusBar.style.width=iframe.style.width;
	// IMPORTANT: we have to allow Mozilla a short time to recognize the
	// new frame.  Otherwise we get a stupid exception.
	
   function initIframe() {
		var doc = editor._iframe.contentWindow.document;
		if (!doc) {
            // Try again..
			// FIXME: don't know what else to do here.  Normally
			// we'll never reach this point.
			if (NINBOX.is_gecko) {
				setTimeout(initIframe, 100);
				return false;
			} else {
				alert("ERROR: IFRAME can't be initialized.");
			}
		}
		if (NINBOX.is_gecko) {
            // enable editable mode for Mozilla
			try{
              doc.designMode = "on";
            }catch(e){
               alert(e);
                  }
		}
		editor._doc = doc;
		if (!editor.fullPage) {
			doc.open();
			var html = "<html>\n";
			html += "<head>\n";
			html += "</head>\n";
			html += "<body>\n";
			html += editor._textArea.value;
			html += "</body>\n";
			html += "</html>";
			doc.write(html);
			doc.close();
		} else {
			var html = editor._textArea.value;
			doc.open();
			doc.write(html);
			doc.close();
		}

		if (NINBOX.is_ie) {
			// enable editable mode for IE.	 For some reason this
			// doesn't work if done in the same place as for Gecko
			// (above).
			doc.body.contentEditable = true;
		}

		

		//SETDEFAULT
		 
		// intercept some events; for updating the toolbar & keyboard handlers
		NINBOX._addEvents
			(doc, ["keyup","mouseup"],
			 function (event) {
                if(editor.busy==0){
                   if(document.all)
                        tecla=event.keyCode;
                    else
                        tecla=event.which;
        
                  if(tecla==37||tecla==39||tecla==38||tecla==40||tecla==0)
			        editor._tree();
                 }
			 });

		//me falta setdeafual
	      setTimeout(function(){ 
                editor.foc();
             if(editor.fullpage==0){
                //alert("no full");
				editor._doc.execCommand('fontname', false,editor.fdefault);
				editor._doc.execCommand('fontsize', false,editor.fsdefault);
				editor._doc.execCommand('formatblock', false,editor.pdefault);
			 }
                editor._tree(); 
            }, 250);
		

		
	};
	setTimeout(initIframe, 100);
    
};
//replace all...
NINBOX.prototype._createStatusBar = function() {
	var statusbar = document.createElement("div");
	this._statusBar = statusbar;
	
	var table = document.createElement("table");
    table.width="100%";
    table.border = "0px";
    table.className = "table";
    table.cellSpacing = "0px";
    table.cellPadding = "0px";
    statusbar.appendChild(table);

    var tb_body = document.createElement("tbody");
    table.appendChild(tb_body);
    tb_row = document.createElement("tr");
    tb_body.appendChild(tb_row);
    var tb_cell = document.createElement("td");
    tb_row.appendChild(tb_cell);
	var div=document.createElement("div");
	div.id="divpath"+this._textArea.id;
	tb_cell.appendChild(div);
	tb_row = document.createElement("tr");
    tb_body.appendChild(tb_row);
    var tb_cell = document.createElement("td");
    tb_cell.align="center";
    tb_row.appendChild(tb_cell);
    sp=document.createElement("span");
    sp.className="Estilo1";
    txt=document.createTextNode("editor ninbox v."+this.version);
    sp.appendChild(txt);
	tb_cell.appendChild(sp);

	this._htmlArea.appendChild(statusbar);
};

NINBOX.prototype._createToolbar = function () {
	var editor = this;	// to access this in nested functions
	var toolbar = document.createElement("div");
	this._toolbar = toolbar;
	toolbar.unselectable = "1";
	
  var table = document.createElement("table");
  table.width="100%";
  table.border = "0px";
  table.className = "table";
  table.cellSpacing = "0px";
  table.cellPadding = "0px";
  toolbar.appendChild(table);
  // TBODY is required for IE, otherwise you don't see anything
  // in the TABLE.
  
  var tb_body = document.createElement("tbody");
  table.appendChild(tb_body);
  tb_row = document.createElement("tr");
  tb_body.appendChild(tb_row);
  var tb_cell = document.createElement("td");
  tb_row.appendChild(tb_cell);
  

  function crearboton(btn){
	var img = document.createElement("img");
	img.title=btn[4];  //mejorar
	img.id=btn[0]+editor._textArea.id;
	img.src =_editor_url+"images/"+btn[1];
	img.className="but_off";
	img.style.align="texttop";
	img.style.width = "18px";
	img.style.height = "18px";
	img.act=btn[2];
	NINBOX._addEvent(img, "mouseover", function () {
				NINBOX._selOn(img);
				});
	NINBOX._addEvent(img, "mouseout", function () {
				NINBOX._selOut(img);
				});
	NINBOX._addEvent(img, "mousedown", function () {
				NINBOX._selDown(img);
				});
	NINBOX._addEvent(img, "mouseup", function () {
				editor._choice(img,btn[0],btn[2]);
				//actualiza;
				});
	
	tb_cell.appendChild(img);
  }
    function crearselect(btn){
	  if(btn[0]=="fontname"){
	   options=editor.fontname;
	  }
	  if(btn[0]=="fontsize"){
	   options=editor.fontsize;
	  }
	  if(btn[0]=="formatblock"){
	   options=editor.formatblock;
	  }
	if (options) {
		var el = document.createElement("select");
		//alert(btn[0]+editor._textArea.id);
		el.id=btn[0]+editor._textArea.id;
		el.className="list_defecto";
		for (var i in options) {
				var op = document.createElement("option");
				op.appendChild(document.createTextNode(i));
				if(options==editor.formatblock&&NINBOX.is_ie==true)//ojo con opera
				   op.value = "<"+options[i]+">";
				else
				  op.value = options[i];
				el.appendChild(op);
		}
        NINBOX._addEvent(el, "change", function () {
				editor._choice(el,btn[0],btn[2]);
				//actualiza;
				});
	  tb_cell.appendChild(el);
	}
	else
	 alert("ERROR DE LISTA");
  }
  function crearspecial(el){
       if(el[0]=="NL"){
         tb_row = document.createElement("tr");
         tb_body.appendChild(tb_row);
         tb_cell = document.createElement("td");
         tb_row.appendChild(tb_cell);
	   }
	    if(el[0]=="separator"){
           var img = document.createElement("img");
	       img.src =_editor_url+"images/vr.gif";
		   img.className="separator";
		   tb_cell.appendChild(img);
	   }
	   if(el[0]=="space"){
          tb_cell.appendChild(document.createTextNode(" "));
	   }
  
  }
   //comienza el juego
	for (var i in this.toolbar) {
	    var group = this.toolbar[i];
		if(group[3]==1)
		   crearboton(group);
		else if(group[3]==2){
		  crearselect(group);
		}else
		  crearspecial(group);
    }

   if(editor.fullpage!=0)
	  crearboton(editor.minimizar);
   if(editor.fullenable==1&&editor.fullpage==0)
	  crearboton(editor.maximizar);
  if(this.tablesprop=="yes"){
	for (var i in this.toolbartable) {
	    var group = this.toolbartable[i];
		if(group[3]==1)
		   crearboton(group);
		else if(group[3]==2){
		  crearselect(group);
		}else
		  crearspecial(group);
    }
  }
   this._htmlArea.appendChild(toolbar);
   

};

NINBOX.getElementById = function(tag, id) {
	var el, i, objs = document.getElementsByTagName(tag);
	for (i = objs.length; --i >= 0 && (el = objs[i]);)
		if (el.id == id)
			return el;
	return null;
};
NINBOX.prototype.getInnerHTML = function() {
	switch (this._editMode) {
	    case "wysiwyg"  :
		if (!this.fullPage)
			return this._doc.body.innerHTML;
		else
			return this.doctype + "\n" + this._doc.documentElement.innerHTML;
	    case "textmode" : return this._textArea.value;
	    default	    : alert("Mode <" + mode + "> not defined!");
	}
	return false;
};
NINBOX._addEvent = function(el, evname, func) {
	if (NINBOX.is_ie) {
		el.attachEvent("on" + evname, func);
	} else {
		el.addEventListener(evname, func, true);
	}
};
NINBOX._selAct=function(ctrl) 
{ 
   ctrl.className="but_act";
}
NINBOX._selOn=function(ctrl) 
{ 
 if(ctrl.className!="but_act"&&ctrl.className!="but_ina")
   ctrl.className="but_on";
}
NINBOX._selUp=function(ctrl) 
{ 
 if(ctrl.className!="but_act"&&ctrl.className!="but_ina")
   ctrl.className="but_on";
}
NINBOX._selOut=function(ctrl) 
{ 
 if(ctrl.className!="but_act"&&ctrl.className!="but_ina")
   ctrl.className="but_off";
}
NINBOX._selDown=function(ctrl) 
{ 
 if(ctrl.className!="but_act"&&ctrl.className!="but_ina")
   ctrl.className="but_down";
}
NINBOX._selOff=function(ctrl) 
{ 
   ctrl.className="but_off";
}
NINBOX._selIna=function(ctrl) 
{ 
   ctrl.className="but_ina";
}
NINBOX.prototype._choice=function(el,cmd,act){
  switch(cmd){
     case "saveAs" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "copy" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "paste" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "cut" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "undo" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "redo" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "bold" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "italic" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "underline" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "strikethrough" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "Hight" : if(this._editMode=="wysiwyg"||act==true) this._doHight(cmd); break;
	 case "fontname" : if(this._editMode=="wysiwyg"||act==true) this._doFontname(el); break;
	 case "fontsize" : if(this._editMode=="wysiwyg"||act==true) this._doFontsize(el); break;
	 case "formatblock" : if(this._editMode=="wysiwyg"||act==true) this._doHead(el); break;
     case "subscript" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "superscript" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "justifyleft" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "justifyright" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "justifycenter" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "justifyfull" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "insertorderedlist" : this._doexe(cmd); break;
     case "insertunorderedlist" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "indent" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "outdent" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "inserthorizontalrule" : if(this._editMode=="wysiwyg"||act==true) this._doexe(cmd); break;
     case "fcolor" : if(this._editMode=="wysiwyg"||act==true) this._doFcolor(); break;
     case "bcolor" : if(this._editMode=="wysiwyg"||act==true) this._doBcolor(); break; 
     case "icono" : if(this._editMode=="wysiwyg"||act==true) this._doIcono(); break; 
     case "icono2" : if(this._editMode=="wysiwyg"||act==true) this._doIcono2(); break;
     case "charmap" : if(this._editMode=="wysiwyg"||act==true) this._doCharmap(); break;
	 case "table" : if(this._editMode=="wysiwyg"||act==true) this._doTable(); break;
	 case "fullscreen" : this._doFullscreen(); break;
     case "minscreen" : onOK(); break;
     case "ancla" : if(this._editMode=="wysiwyg"||act==true) this._doAncla(); break;
     case "noancla" : if(this._editMode=="wysiwyg"||act==true) this._doNoancla(); break;
     case "link" : if(this._editMode=="wysiwyg"||act==true) this._doLink(); break;
     case "nolink" : if(this._editMode=="wysiwyg"||act==true) this._doNolink(); break;
     case "image" : if(this._editMode=="wysiwyg"||act==true) this._doImage(); break;
     case "swf" : if(this._editMode=="wysiwyg"||act==true) this._doSwf(); break;
     case "html" : if(this._editMode=="wysiwyg"||act==true) this._doOpenhtml(); break;
     case "setmode" : if(this._editMode=="wysiwyg"||act==true) this.setMode(); break;
     case "tableprop" : if(this._editMode=="wysiwyg"||act==true) this._doTableprop(); break;
	 case "rowprop" : if(this._editMode=="wysiwyg"||act==true) this._doRowprop(); break;
	 case "cellprop" : if(this._editMode=="wysiwyg"||act==true) this._doCellprop(); break;
	 case "tablesplit" : if(this._editMode=="wysiwyg"||act==true) this._doTableSplit(); break;
	 case "tablesjoin" : if(this._editMode=="wysiwyg"||act==true) this._doTableJoin(); break;
	 case "about" : if(this._editMode=="wysiwyg"||act==true) this._doAbout(); break;
     default : alert("FUNCION NO DEFINIDA"); return false;
  
  }
}
NINBOX.prototype.foc = function() {
	switch (this._editMode) {
	    case "wysiwyg" : this._iframe.contentWindow.focus(); break;
	    case "textmode": this._textArea.focus(); break;
	    default	   : alert("ERROR: mode " + this._editMode + " is not defined");
	}
	return this._doc;
};
NINBOX._addEvents = function(el, evs, func) {
	for (var i in evs) {
		NINBOX._addEvent(el, evs[i], func);
	}
};

NINBOX.prototype._update=function(){
 var editor=this;
 if(this._editMode=="wysiwyg"){
   for (var i in this.toolbar) {
	    var group = this.toolbar[i];
		switch(group[0]){
		  case "bold":
          case "italic":
          case "underline":
          case "subscript":
          case "superscript":
          case "strikethrough":
          case "justifyleft":
          case "justifyright":
          case "justifyfull":
          case "justifycenter":
          case "insertorderedlist":
          case "insertunorderedlist" : 
		               r=this._doc.queryCommandState(group[0]);
		               this._setbutton((group[0]+this._textArea.id),r);
		               break;
         case "fontsize":
         case "fontname":
         case "formatblock": 
		               r=this._doc.queryCommandValue(group[0]);
		               this._setselect((group[0]+this._textArea.id),r);
		               break;
	
		case "ancla":
        case "noancla":
               foofo=false;
               if((p=this.getNodewithTag("a"))!=null&&p.getAttribute("name")!=""&&p.getAttribute("name")!=null){
                   foofo=true;
                }
                this._setbutton((group[0]+this._textArea.id),foofo);
                break;
        case "link":
        case "nolink":
               foofo=false;
               if((p=this.getNodewithTag("a"))!=null&&p.getAttribute("href")!=""&&p.getAttribute("href")!=null){
                   foofo=true;
                }
                this._setbutton((group[0]+this._textArea.id),foofo);
                break;

        }
    }
	if((p=this.getNodewithTag("table"))!=null){
	this._ontoolbartable();
	}
	else{
	this._offtoolbartable();
	}
 } 
  
}

NINBOX.prototype._setbutton=function(but,valor){

   if(valor==true)
     NINBOX._selAct(document.getElementById(but));
   else
     NINBOX._selOff(document.getElementById(but));
}
NINBOX.prototype._offtoolbar=function(sel,valor){
   for (var i in this.toolbar) {
	    var group = this.toolbar[i];
		if(group[2]!=true&&group[3]==1){
            NINBOX._selIna(document.getElementById(group[0]+this._textArea.id));
         }
         if(group[2]!=true&&group[3]==2){
            document.getElementById(group[0]+this._textArea.id).className="list_defecto_off";
         }
		
    }
	if(this.tablesprop=="yes"){
	   for (var i in this.toolbartable) {
	    var group = this.toolbartable[i];
		if(group[2]!=true&&group[3]==1){
            NINBOX._selIna(document.getElementById(group[0]+this._textArea.id));
         }
         if(group[2]!=true&&group[3]==2){
            document.getElementById(group[0]+this._textArea.id).className="list_defecto_off";
         }
		
     }
	}
}
NINBOX.prototype._offtoolbartable=function(sel,valor){
   for (var i in this.toolbartable) {
	    var group = this.toolbartable[i];
		if(group[2]!=true&&group[3]==1){
            NINBOX._selIna(document.getElementById(group[0]+this._textArea.id));
         }
         if(group[2]!=true&&group[3]==2){
            document.getElementById(group[0]+this._textArea.id).className="list_defecto_off";
         }
		
    }
}
NINBOX.prototype._ontoolbar=function(sel,valor){
   for (var i in this.toolbar) {
	    var group = this.toolbar[i];
		if(group[2]!=true&&group[3]==1){
            NINBOX._selOff(document.getElementById(group[0]+this._textArea.id));
         }
         if(group[2]!=true&&group[3]==2){
            document.getElementById(group[0]+this._textArea.id).className="list_defecto";
         }
		
    }
    this._update();
}
NINBOX.prototype._ontoolbartable=function(sel,valor){
   for (var i in this.toolbartable) {
	    var group = this.toolbartable[i];
		if(group[2]!=true&&group[3]==1){
            NINBOX._selOff(document.getElementById(group[0]+this._textArea.id));
         }
         if(group[2]!=true&&group[3]==2){
            document.getElementById(group[0]+this._textArea.id).className="list_defecto";
         }
		
    }
}
NINBOX.prototype._setselect=function(sel,valor){
flag=0;
    for( var i=0 ; i<document.getElementById(sel).length&&flag==0; i++){
	     if(document.getElementById(sel).options[i].value==valor||document.getElementById(sel).options[i].text==valor){
			  flag=1;
			  document.getElementById(sel).options[i].selected=true;
		 }
	}
}


NINBOX.prototype._tree=function(){
  var editor=this;
//alert(this.busy);
if(this.busy==0){
 this.busy=1;

var atree=new Array();
var p = this._getParentElement(0);
while (p && (p.nodeType == 1) && (p.tagName.toLowerCase() != 'body')&& (p.tagName.toLowerCase() != 'head')&& (p.tagName.toLowerCase() != 'html')) {
	atree[atree.length]=p;
	p = p.parentNode;
}

atree[atree.length]=this._doc.body;

if(this.path!=null)
       document.getElementById("divpath"+this._textArea.id).parentNode.removeChild(this.path);

this.path = document.createElement("div");
this.path.className="defecto";
document.getElementById("divpath"+this._textArea.id).parentNode.insertBefore(this.path, document.getElementById("divpath"+this._textArea.id));
if(this._editMode=="wysiwyg"){
 for(i=atree.length-1;i>=0;i--){
   var inter = document.createTextNode(" > ");

    this.path.appendChild(inter);
    var a = document.createElement("a");
    a.ele=atree[i];
    a.onmouseover=function(){
    this.className="pathon";
   };
   a.onmouseout=function(){
      this.className="pathoff";
   };
   a.onclick=function(){
    editor.selectNodeContents(this.ele);
   };
a.setAttribute("className","pathoff");
a.setAttribute("class","pathoff");
a.setAttribute("id",eval(atree[i]).tagName);
a.setAttribute("href","javascript:;");
var frase="Atributos: ";
if(eval(atree[i]).getAttribute("name")!=""&&eval(atree[i]).getAttribute("name")!=null)
  frase=frase+" name="+eval(atree[i]).getAttribute("name");
if(eval(atree[i]).getAttribute("href")!=""&&eval(atree[i]).getAttribute("href")!=null)
  frase=frase+" href="+eval(atree[i]).getAttribute("href");
a.setAttribute("title",frase);
var frase="[ "+eval(atree[i]).tagName+" ]";
var newText = document.createTextNode(frase);
a.appendChild(newText);
this.path.appendChild(a);
}
}
this._update();
this.busy=0;
}
}


NINBOX.prototype._getParentElement = function(level) {
	var sel = this._getSelection();
	var range = this._createRange(sel);
	if (NINBOX.is_ie) {
		switch (sel.type) {
		    case "Text":
		    case "None":
			// It seems that even for selection of type "None",
			// there _is_ a parent element and it's value is not
			// only correct, but very important to us.  MSIE is
			// certainly the buggiest browser in the world and I
			// wonder, God, how can Earth stand it?
			p=range.parentElement();
            for(var i=0;i<level&&p!=null;i++)
		      p=p.parentNode;
		    return p;
            case "Control":
			return range.item(0);
		    default:
			return this._doc.body;
		}
	} else try {
		var p = range.commonAncestorContainer;
		if (!range.collapsed && range.startContainer == range.endContainer &&
		    range.startOffset - range.endOffset <= 1 && range.startContainer.hasChildNodes())
			p = range.startContainer.childNodes[range.startOffset];
		/*
		alert(range.startContainer + ":" + range.startOffset + "\n" +
		      range.endContainer + ":" + range.endOffset);
		*/
		while (p.nodeType == 3) {
			p = p.parentNode;
		}
        for(var i=0;i<level&&p!=null;i++)
		  p=p.parentNode;
		//alert(p.tagName);
		return p;
	} catch (e) {
		return null;
	}
};
NINBOX.prototype._getSelection = function() {
	if (NINBOX.is_ie) {
		return this._doc.selection;
	} else {
		return this._iframe.contentWindow.getSelection();
	}
};

// returns a range for the current selection
NINBOX.prototype._createRange = function(sel) {
	if (NINBOX.is_ie) {
		return sel.createRange();
	} else {
		this.foc();
		if (typeof sel != "undefined") {
			try {
				return sel.getRangeAt(0);
			} catch(e) {
				return this._doc.createRange();
			}
		} else {
			return this._doc.createRange();
		}
	}
};

NINBOX.prototype.selectNodeContents = function(node, pos) {
	this.foc();
	//this.forceRedraw();
	var range;
	var collapsed = (typeof pos != "undefined");
	if (NINBOX.is_ie) {
		range = this._doc.body.createTextRange();
		range.moveToElementText(node);
		(collapsed) && range.collapse(pos);
		range.select();
	} else {
		var sel = this._getSelection();
		range = this._doc.createRange();
		range.selectNodeContents(node);
		(collapsed) && range.collapse(pos);
		sel.removeAllRanges();
		sel.addRange(range);
	}
};

//FUNCIONES DE LOS ELEMENTOS DEL TOOLBAR

NINBOX.prototype._doFontname=function(el){
  this.foc();
  this._doc.execCommand('fontname', false, el.options[el.selectedIndex].value);
  this._tree();  
}
NINBOX.prototype._doFontsize=function(el){
  this.foc();
  this._doc.execCommand('fontsize', false, el.options[el.selectedIndex].value);
  this._tree();  
}
NINBOX.prototype._doHead=function(el){
  this.foc();
  this._doc.execCommand('formatblock', false, el.options[el.selectedIndex].value);
  this._tree();  
}
NINBOX.prototype._doexe=function(cmd){
  this.foc();
  this._doc.execCommand(cmd, false, null); 
  this._tree();  
}
NINBOX.prototype._doHight=function(){
  this.foc();
  sColor="FFFF00";
 if(NINBOX.is_ie)
        this._doc.execCommand('backcolor', false, sColor);
        else
        this._doc.execCommand('hilitecolor', false, sColor); 
  this._tree();
}
NINBOX.prototype._doFcolor=function(){
  var editor=this;
this.foc();
  outparam=null; 
	this._popupDialog(_editor_url+"templates/color.html", function(param) {
        editor.foc(); 
		if (param=="undefined")
			return false;
		sColor=param;
       editor._doc.execCommand('forecolor', false, sColor);
	}, outparam,"fcolor");
};
NINBOX.prototype._doAbout=function(){
  outparam=null; 
	this._popupDialog(_editor_url+"templates/about.html", function(param) {
	    
	}, outparam,"about");
}
NINBOX.prototype._doBcolor=function(){
  var editor=this;
this.foc();
  outparam=null; 
	this._popupDialog(_editor_url+"templates/color.html", function(param) {
	    editor.foc(); 
		if (param=="undefined")
			return false;
		sColor=param;
        if(NINBOX.is_ie)
        editor._doc.execCommand('backcolor', false, sColor);
        else
        editor._doc.execCommand('hilitecolor', false, sColor);
	}, outparam,"bcolor");
}
NINBOX.prototype._doFullscreen=function(){
  var editor=this;
this.foc();
  outparam=null;
  
   outparam = {
		f_editor :  editor.getInnerHTML()
	};
	this._popupDialog(_editor_url+"templates/full.htm", function(param) {
      if(editor._editMode=="textmode")
          editor.setMode();
	    editor.foc(); 
		if (param=="undefined")
			return false;
		src=param.contenido;
       editor._doc.body.innerHTML=src;
        //editor.insertHTML(src);
	}, outparam,"full");
}
NINBOX.prototype._doCharmap=function(){
  var editor=this;
this.foc();
  outparam=null; 
	this._popupDialog(_editor_url+"templates/charmap.htm", function(param) {
	    editor.foc(); 
		if (param=="undefined")
			return false;
		src=param;
        editor.insertHTML(src);
	}, outparam,"char");
}
NINBOX.prototype._doIcono=function(){
  var editor=this;
this.foc();
  outparam=null; 
	this._popupDialog(_editor_url+"templates/icono.htm", function(param) {
	    editor.foc(); 
		if (param=="undefined")
			return false;
		src=param;
        editor.insertHTML('<img src="'+src+'">');
	}, outparam,"ic");
}
NINBOX.prototype._doIcono2=function(){
  var editor=this;
this.foc();
  outparam=null; 
	this._popupDialog(_editor_url+"templates/icono2.htm", function(param) {
	    editor.foc(); 
		if (param=="undefined")
			return false;
		src=param;
        editor.insertHTML('<img src="'+src+'">');
	}, outparam,"ic1");
}
NINBOX.prototype._doOpenhtml=function(){
  var editor=this; 
this.foc();
outparam=null; 
	this._popupDialog(_editor_url+"templates/insert.html", function(param) {
	    editor.foc(); 
		if (param=="undefined")
			return false;
        editor.insertHTML(param);
	}, outparam,"html");
}
NINBOX.prototype._doAncla=function(){
  var editor=this;
  this.foc();
  if(NINBOX.is_ie)
   var sel = this._doc.selection.createRange().text;
  else
   var sel = this._getSelection();
  
  var nameancla="";
  var r=0;
  var p=null;
  if((p=this.getNodewithTag("a"))!=null){
      nameancla=p.getAttribute("name");
      r=1;
  }else{
     nameancla=sel;
  }
   var anclat= prompt("Nombre del ancla: ",nameancla);

   if(anclat!=null){
     if(r==1){
        p.setAttribute("name",anclat);
      }else{
        bloq="<a name='"+anclat+"'>"+sel+"</a>";
        this.insertHTML(bloq);
      }
   } 
  this._tree();  
}
NINBOX.prototype._doNoancla=function(){ 
  var editor=this;
this.foc();
var p=null;
p=this.getNodewithTag("a");
if(p!=null&&p.getAttribute("name")!=""&&p.getAttribute("name")!=null){
 var content=p.innerHTML;
 a=p.parentNode;
 a.removeChild(p);
 this.insertHTML(content);
}
  this._tree();  
}
NINBOX.prototype._doLink=function(){
  var editor=this;
 this.foc();
 var ancla=new Array();
    if(NINBOX.is_ie)
   var sela = this._doc.selection.createRange().text;
  else
   var sela = this._getSelection(); 
 var l=this._doc.getElementsByTagName("a").length;
  
 if(l>0){
   var pt=this._doc.getElementsByTagName("a");
   for(i=0;i<pt.length;i++){
     if(pt[i].getAttribute("name")!=null||pt[i].getAttribute("name")!=""){
	      ancla[ancla.length]=pt[i].getAttribute("name");
	  }
	 }
 }
var urlancla="";
for(i=0;i<ancla.length;i++)
  urlancla=urlancla+ancla[i]+"*";
var sel = this._getSelection();
var range = this._createRange(sel);
var urllink="";
var r=0;
var p=null;
if((p=this.getNodewithTag("a"))!=null){
 urllink=p.getAttribute("href");
 urltitle=p.getAttribute("title");
 urltarget=p.getAttribute("target");
 r=1;
}else{
  urllink= sela;
  urltitle="";
  urltarget="";
  }
 outparam = {
		f_href : urllink,
		f_title : urltitle,
		f_ancla : urlancla,
		f_target : urltarget 
	};

  
	Dialog(_editor_url+"templates/link.html", function(param) {
	    
		if (param=="undefined")
			return false;
		
		if(param.f_href!=null&&param.f_href!=""){

       if(r==1){

          p.setAttribute("href",param.f_href);
		  p.setAttribute("title",param.f_title);
		  p.setAttribute("target",param.f_target);
     }else{
		 if(param.f_href.charAt(0)=="#")
         editor._doc.execCommand('CreateLink', false, param.f_href);
		 else{
		    bloq="<a href='"+param.f_href+"' title='"+param.f_title+"' target='"+param.f_target+"'>"+sela+"</a>";
             editor.insertHTML(bloq);
		 }
       	
	 }
    } 

  editor._tree();  
	}, outparam);

}
NINBOX.prototype._doSwf=function(){
  var editor=this;
 this.foc();

  
var sel = this._getSelection();
var range = this._createRange(sel);


	Dialog(_editor_url+"templates/swf.html", function(param) {
	    
		if (param=="undefined")
			return false;
		
		if(param.f_href!=null&&param.f_href!=""){
             bloq='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+param.f_ancho+param.f_unit_ancho+'" height="'+param.f_alto+param.f_unit_alto+'">';
       	     bloq=bloq+'<param name="movie" value="'+param.f_href+'">';
			 bloq=bloq+' <param name="quality" value="high">';
			 bloq=bloq+'<embed src="'+param.f_href+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+param.f_ancho+param.f_unit_ancho+'" height="'+param.f_alto+param.f_unit_alto+'"0"></embed>';
			 bloq=bloq+'</object>';
			 editor.insertHTML(bloq);
		 }
       	
	  editor._tree();  
	}, null);

}
NINBOX.prototype._doTableprop=function(){
  var editor=this;
 this.foc();
 
var sel = this._getSelection();
var range = this._createRange(sel);


if((p=this.getNodewithTag("table"))!=null){
	 width=p.style.width;
 height=p.style.height;
 align=p.getAttribute("align");
 cellpadding=p.getAttribute("cellpadding");
 cellspacing=p.getAttribute("cellspacing");
 border=p.getAttribute("border");
 bordercolor=p.getAttribute("bordercolor");
 bgcolor=p.getAttribute("bgcolor");
 background=p.getAttribute("background");
 borderstyle=p.style.borderStyle;
//falta estilo de borde
 outparam = {
	    f_width : width,
	    f_height : height,
		f_align : align,
		f_cellpadding : cellpadding,
		f_cellspacing : cellspacing,
		f_border : border,
		f_bordercolor : bordercolor,
		f_bgcolor : bgcolor,
		f_background : background,
        f_borderstyle : borderstyle
	};

 Dialog(_editor_url+"templates/table_prop.html", function(param) {
	    
		if (param=="undefined")
			return false;

		p.style.width= param.f_width+param.f_unit; 
		p.style.height= param.f_height+param.f_unit2;
		  
	    if(param.f_align!="")
          p.setAttribute("align",param.f_align);
		else
		  p.removeAttribute("align");
         
		if(param.f_cellpadding!="")
		  p.setAttribute("cellPadding",param.f_cellpadding);
		else
		  p.removeAttribute("cellPadding");
		
		if(param.f_cellspacing!="")
		  p.setAttribute("cellSpacing",param.f_cellspacing);
		else
		  p.removeAttribute("cellSpacing");

		if(param.f_border!="")
		  p.setAttribute("border",param.f_border);
		else
		  p.removeAttribute("border");
		
		if(param.f_bordercolor!="")
		  p.setAttribute("borderColor",param.f_bordercolor);
		else
		  p.removeAttribute("borderColor");  

		if(param.f_bgcolor!="")
		  p.setAttribute("bgColor",param.f_bgcolor);
		else
		  p.removeAttribute("bgColor"); 

		if(param.f_background!="")
		  p.setAttribute("background",param.f_background);
		else
		  p.removeAttribute("background"); 

         p.style.borderStyle=param.f_borderstyle;

  editor._tree();  
	}, outparam);

}
}
NINBOX.prototype._doRowprop=function(){
  var editor=this;
 this.foc();
 
var sel = this._getSelection();
var range = this._createRange(sel);


if((p=this.getNodewithTag("tr"))!=null){
 align=p.getAttribute("align");
 valign=p.getAttribute("vAlign");
 bordercolor=p.getAttribute("bordercolor");
 bgcolor=p.getAttribute("bgcolor");
 background=p.getAttribute("background");
 if(background==null)
    background="";
//falta estilo de borde
 outparam = {
		f_align : align,
		f_valign : valign,
		f_bordercolor : bordercolor,
		f_bgcolor : bgcolor,
		f_background : background
	};

 Dialog(_editor_url+"templates/row_prop.html", function(param) {
	    
		if (param=="undefined")
			return false;
			
	    
		  
	    if(param.f_align!="")
          p.setAttribute("align",param.f_align);
		else
		  p.removeAttribute("align");
		  
	    if(param.f_valign!="")
          p.setAttribute("vAlign",param.f_valign);
		else
		  p.removeAttribute("vAlign");
		
		if(param.f_bordercolor!="")  
          p.setAttribute("borderColor",param.f_bordercolor);
		else
		  p.removeAttribute("borderColor");
		  
		if(param.f_bgcolor!="")  
          p.setAttribute("bgColor",param.f_bgcolor);
		else
		  p.removeAttribute("bgColor");
		  
		if(param.f_background!="")  
          p.setAttribute("background",param.f_background);
		else
		  p.removeAttribute("background");

      
  editor._tree();  
	}, outparam);

}
}
NINBOX.prototype._doCellprop=function(){
  var editor=this;
 this.foc();
 
var sel = this._getSelection();
var range = this._createRange(sel);


if((p=this.getNodewithTag("td"))!=null){
 width=p.style.width;
 height=p.style.height;
 align=p.getAttribute("align");
 valign=p.getAttribute("vAlign");
 bordercolor=p.getAttribute("bordercolor");
 bgcolor=p.getAttribute("bgcolor");
 background=p.getAttribute("background");
 if(background==null)
    background="";
//falta estilo de borde
 outparam = {
	    f_width : width,
		f_height : height,
		f_align : align,
		f_valign : valign,
		f_bordercolor : bordercolor,
		f_bgcolor : bgcolor,
		f_background : background
	};

 Dialog(_editor_url+"templates/cell_prop.html", function(param) {
	    
		if (param=="undefined")
			return false;
			
	    
		p.style.width= param.f_width+param.f_unit; 
		p.style.height= param.f_height+param.f_unit2;  
	    if(param.f_align!="")
          p.setAttribute("align",param.f_align);
		else
		  p.removeAttribute("align");
		  
	    if(param.f_valign!="")
          p.setAttribute("vAlign",param.f_valign);
		else
		  p.removeAttribute("vAlign");
		
		if(param.f_bordercolor!="")  
          p.setAttribute("borderColor",param.f_bordercolor);
		else
		  p.removeAttribute("borderColor");
		  
		if(param.f_bgcolor!="")  
          p.setAttribute("bgColor",param.f_bgcolor);
		else
		  p.removeAttribute("bgColor");
		  
		if(param.f_background!="")  
          p.setAttribute("background",param.f_background);
		else
		  p.removeAttribute("background");

      
  editor._tree();  
	}, outparam);

}
}
NINBOX.prototype._insertd=function(td,f_n){
	  var editor=this;
      var acrear=f_n;
			var trs = td.parentNode.parentNode.rows;
			tr = td.parentNode;
			var n = parseInt("" + td.rowSpan);
		    var nc = parseInt("" + td.colSpan);
			var itr = tr.rowIndex;
			var index = td.cellIndex;

            while(nc>1&&acrear>0){   //no hacemos nada mas
				nc=(nc-1);
				td.colSpan=nc;
				acrear=acrear-1;
			}
		  for(var i=0;i<f_n;i++)
			  editor._intd(tr.cells[index]);
}
NINBOX.prototype._doMapa=function(td){
var mapa=new Array();

var trs=td.parentNode.parentNode.rows;
tr = td.parentNode;
var n = parseInt("" + td.rowSpan);  
var nc = parseInt("" + td.colSpan);
var itr = tr.rowIndex;
var index = td.cellIndex;
for(var i=0;i<trs.length;i++)
  mapa[i]=new Array();
for(var i=0;i<trs.length;i++){
	var des=0;
	for(j=0;j<trs[i].cells.length;j++){
		tmpcol=trs[i].cells[j].colSpan;
		tmprow=trs[i].cells[j].rowSpan;

		while(mapa[i][j+des]!=undefined)
		  des++;
		
		if(i==itr&&j==index)
		  mapa[i][j+des]="x";
		else
		  mapa[i][j+des]="n";
        
		des2=des;
		for(k=1;k<tmpcol;k++){//actualizo colspan
            mapa[i][j+k+des2]="c-"+j;
			des2++;
		}
		
		for(k=1;k<tmprow;k++){//actualizo rowspan
		des2=des;
		  for(p=0;p<tmpcol;p++){//actualizo colspan
	        if(p==0)
			  mapa[i+k][j+p+des2]="r-"+i;
			else
			  mapa[i+k][j+p+des2]="r-"+i+"_c-"+j;
			des2++;  
		  }
        }
 }
}
return mapa
}
NINBOX.prototype._intd=function(td){
	var otd = editor._doc.createElement("td");
	otd.innerHTML=NINBOX.is_gecko ? "&nbsp;" : "";
	otd.rowSpan=td.rowSpan;
	otd.style.width=td.style.width;
	otd.style.height=td.style.height;
	otd.align=td.align;
	otd.vAlign=td.vAlign;
	otd.borderColor=td.borderColor;
	otd.bgColor=td.bgColor;
	otd.background=td.background;
	//propiedades de estilo
	tr.insertBefore(otd, td);
}
NINBOX.prototype._intr=function(td){
	
	tr=td.parentNode;
	table=tr.parentNode;
	var nc = parseInt("" + td.colSpan);
	var otr = editor._doc.createElement("tr");
    var otd = editor._doc.createElement("td");
	otd.colSpan=nc;
	otd.innerHTML=NINBOX.is_gecko ? "&nbsp;" : "";
	otr.appendChild(otd);
				
	otd.rowSpan=td.rowSpan;
	
	otd.align=td.align;
	otd.vAlign=td.vAlign;
	otd.borderColor=td.borderColor;
	otd.bgColor=td.bgColor;
	otd.background=td.background;
	//propiedades de estilo
	table.insertBefore(otr, tr.nextSibling);
}
NINBOX.prototype._doTableSplit=function(){
  var editor=this;
 this.foc();
 
var sel = this._getSelection();
var range = this._createRange(sel);

if((td=this.getNodewithTag("td"))!=null){

 outparam = null;

 Dialog(_editor_url+"templates/split.html", function(param) {
													 
	   var trs=td.parentNode.parentNode.rows;
       tr = td.parentNode;
       var n = parseInt("" + td.rowSpan);  
       var nc = parseInt("" + td.colSpan);
       var itr = tr.rowIndex;
       var index = td.cellIndex;
	   
	   if (param=="undefined")
			return false;
			//no funciona
	    if(param.f_n<2)  //ajuste con interfaz grafica
		 param.f_n=2;
		param.f_n=param.f_n-1;  //fin de ajuste con interfaz grafica
		 
		mapa=editor._doMapa(td);
		/*
		var frase="";
for(i=0;i<mapa.length;i++){
	for(j=0;j<mapa[i].length;j++){
         frase=frase+mapa[i][j]+" ";		
	}
	frase=frase+"\n    ";
}
alert(frase);*/
	   //extraigo mis coordenadas en mi mapa
		var imapa=0;
		var jmapa=0;
		for(var i=0;i<mapa.length;i++)
			for(var j=0;j<mapa[i].length;j++)
				if(mapa[i][j]=="x"){
					imapa=i;
					jmapa=j;
				}
				
     	if(param.f_tipo=="td"){
            var acrear=param.f_n;
			    
          while(nc>1&&acrear>0){   //no hacemos nada mas
				nc=(nc-1);
				td.colSpan=nc;
				acrear=acrear-1;
			}

			if(acrear>0){  //subimos colspan de las otras filas para ajuste
				for(var i=0;i<mapa.length;i++){
					if(i!=imapa){
					var lflag=0;
					var l=0;
					var m=0;

						for(m=0;m<trs[i].cells.length&&lflag==0;m++){
						    l=l+parseInt("" + trs[i].cells[m].colSpan);
							
							if(l>jmapa)
							 lflag=1;
						}
						
					//if(m==trs[i].cells.length)
					 m=m-1;
					
					var q=new Array();
					if(mapa[i][jmapa]!=undefined)	
					q=mapa[i][jmapa].split("-");
					else
					q[0]="c";  //fallo en mapa
					if(q[0]=="n"){
						trs[i].cells[m].colSpan=parseInt("" + trs[i].cells[m].colSpan)+acrear;
					}
                    if(q[0]=="c"){
						trs[i].cells[m].colSpan=parseInt("" + trs[i].cells[m].colSpan)+acrear;
					}
				
				}
			  }
			}
			//insertamos columnan
			for(var i=0;i<param.f_n;i++)
          		editor._intd(td);
			  
		    
		}//fin si es td
		
     		if(param.f_tipo=="tr"){  //no va
              var acrear=param.f_n;
			  var table=td.parentNode.parentNode;
			  var trs = td.parentNode.parentNode.rows;
			  tr = td.parentNode;
			  var n = parseInt("" + td.rowSpan);
		      var nc = parseInt("" + td.colSpan);
			  var itr = tr.rowIndex;
			  var index = td.cellIndex;
           var ojo=0;
		   naux=n;
            while(n>1&&acrear>0){   //no hacemos nada mas
				n=(n-1);
				td.rowSpan=n;
				acrear=acrear-1;
				ojo++;
			}
			if(ojo>0){  //meter tds en los otros
                 //recorro trs    
			          for (var i=0;i<ojo;i++) {
			              var nn=itr+naux-i-1;
						  var desprowspan=0;
                          
	                      for(j=0;j<trs[nn].cells.length&&flag==0;j++){
							       nindex=nindex+parseInt("" + trs[nn].cells[j].colSpan);
								   if(nindex>index){										
                                       flag=1;
								   }
						    }
							
					        //estamos en la celda que nos corresponde
							editor._insertd(trs[nn].cells[j],1);
					}
					//fin de recorrer trs
			
			}//fin ojo
          
		   	if(acrear>0){  //rowSpan de las otras filas que afectan a esta celda
			  for (var i=0;i<itr;i++) {
			      var nindex=0;
				  var flag=0;
    			  for(var j=0;j<trs[i].cells.length&&flag==0;j++){
					  nindex=nindex+parseInt("" + trs[i].cells[j].colSpan);
					  if(nindex>index||(j==trs[i].cells.length-1)){
						    if(trs[i].cells[j].rowSpan+trs[i].rowIndex>itr){
							flag=1;  //no sigo ya he incrementado
					        nx=parseInt("" + trs[i].cells[j].rowSpan);
                            trs[i].cells[j].rowSpan=nx+parseInt(acrear);
							//editor._insertd(trs[i].cells[j],1);
					       }
						}
					  } //fin for td
				} //fin for tr
					
			}//fin acrear
			if(acrear>0){  //rowSpan de las otras celdas 
			       for(var j=0;j<trs[itr].cells.length;j++){
				        if(j!=index){										
		                   nx=parseInt("" + trs[itr].cells[j].rowSpan);
                           trs[itr].cells[j].rowSpan=nx+parseInt(acrear);
					   }
				    }
			}
			//insertamos columna
			  for(var i=0;i<acrear;i++)
   				editor._intr(td);
		    
		}//fin si es tr
      
  editor._tree();  
	}, outparam);

}
}
NINBOX.prototype._doTableJoin=function(){
  var editor=this;
 this.foc();
 
var sel = this._getSelection();
var range = this._createRange(sel);

if((td=this.getNodewithTag("td"))!=null){
       var trs=td.parentNode.parentNode.rows;
       tr = td.parentNode;
       var itr = tr.rowIndex;
       var index = td.cellIndex;
	   var right=0;
	   var left=0;
	   var up=0;
	   var down=0;
	   var lup=0;
	   var ldo=0;
	   var lri=0;
	   var lle=0;
	   for(var i=0;i<trs.length;i++){
		   flag=0;
		   nindex=0;
		  for(j=0;j<trs[i].cells.length&&flag==0;j++){
			 nindex=nindex+parseInt("" + trs[i].cells[j].colSpan);
		     if(nindex>=index)										
                   flag=1;
		  }
		  j=j-1;
		  if(i==itr)
			j=index;
			
			
		  if(i<itr){
			  if(parseInt(""+trs[i].cells[j].colSpan)!=parseInt(""+td.colSpan))
				              lup=1;
			 if(lup==0)
     			up++;
		  }
		  if(i>itr){
		    if(parseInt(""+trs[i].cells[j].colSpan)!=parseInt(""+td.colSpan))
				  ldo=1;
			 if(ldo==0)
     			down++;
		  
		  }
		  if(i==itr){
		  for(var j=0;j<trs[i].cells.length;j++){
			    // alert(parseInt("",trs[i].cells[j].rowSpan)+"  "+parseInt("",td.rowSpan));
			if(j<index){
				if(parseInt(""+trs[i].cells[j].rowSpan)!=parseInt(""+td.rowSpan))
				  lle=1;
				if(lle==0)
				   left++;
			}
				 
				 if(j>index){
				  		if(parseInt(""+trs[i].cells[j].rowSpan)!=parseInt(""+td.rowSpan))
				              lri=1;
				        if(lri==0)
				         right++;
				 }
		  }
		  }
	   }

 outparam = {
	    f_up : up,
		f_down : down,
		f_left : left,
		f_right : right
	};


 Dialog(_editor_url+"templates/join.html", function(param) {
													 
	   var trs=td.parentNode.parentNode.rows;
       tr = td.parentNode;
       var n = parseInt("" + td.rowSpan);  
       var nc = parseInt("" + td.colSpan);
       var itr = tr.rowIndex;
       var index = td.cellIndex;
	   
	   if (param=="undefined")
			return false;
			//no funciona
	    if(param.f_n==0)  //ajuste con interfaz grafica
             return false;
		 
		mapa=editor._doMapa(td);
		
	   //extraigo mis coordenadas en mi mapa
		var imapa=0;
		var jmapa=0;
		for(var i=0;i<mapa.length;i++)
			for(var j=0;j<mapa[i].length;j++)
				if(mapa[i][j]=="x"){
					imapa=i;
					jmapa=j;
				}
				
     	if(param.f_tipo=="td"){
			  if(param.f_dir=="d"){
				  var contenido="";
				  var aborrar=0;
				  var cc=0;
				  var fin=tr.cells.length;

				  for(var i=index+1;i<fin&&aborrar<param.f_n;i++){
					  aborrar=aborrar+1;
					  contenido=contenido+tr.cells[i].innerHTML;
		     		  cc=cc+parseInt("" + tr.cells[i].colSpan)-1;
				  }
				  var aborrar=0;
				  for(var i=index+1;i<fin&&aborrar<param.f_n;i++){
				    aborrar=aborrar+1;
					 tr.removeChild(tr.cells[(td.cellIndex+1)]);
				  }
				  td.innerHTML=td.innerHTML+contenido;
				  td.colSpan=parseInt("" + td.colSpan)+parseInt(param.f_n)+parseInt(cc);
      		  }
			  if(param.f_dir=="i"){
				  var contenido="";
				  var aborrar=0;
				  var cc=0;
				  var fin=tr.cells.length;

				  for(var i=index-1;i>=0&&aborrar<param.f_n;i--){
					  aborrar=aborrar+1;
					  contenido=contenido+tr.cells[i].innerHTML;
					  cc=cc+parseInt("" + tr.cells[i].colSpan)-1;
					  //tr.removeChild(tr.cells[i]);
				  }
				   var aborrar=0;
				  for(var i=index-1;i>=0&&aborrar<param.f_n;i--){
				    aborrar=aborrar+1;
					  tr.removeChild(tr.cells[(td.cellIndex-1)]);
				  }
				  td.innerHTML=td.innerHTML+contenido;
				  td.colSpan=parseInt("" + td.colSpan)+parseInt(param.f_n)+parseInt(cc);
      		  }
			  //AJUSTA COLSPAN
			  var mcm=0;
			   var index = td.cellIndex;
			    for (var i=0;i<trs.length;i++) {
					var flag=0;
					var nindex=0;
					
                           for(j=0;j<trs[i].cells.length&&flag==0;j++){
							       nindex=nindex+parseInt("" + trs[i].cells[j].colSpan);
								   if(nindex>=index){										
                                       flag=1;
								   }
						    }
						j=j-1;
						if(i==itr)
						  j=index;
						  
						
                       if(mcm==0)
					     mcm=parseInt("" + trs[i].cells[j].colSpan);
					   else if( parseInt("" + trs[i].cells[j].colSpan)<mcm)
					     mcm=parseInt("" + trs[i].cells[j].colSpan);
				}

				mcm=mcm-1;
				if(mcm>0){
					for (var i=0;i<trs.length;i++) {
						var flag=0;
						var nindex=0;
                           for(j=0;j<trs[i].cells.length&&flag==0;j++){
							       nindex=nindex+parseInt("" + trs[i].cells[j].colSpan);
								   if(nindex>=index){										
                                       flag=1;
								   }
						    }
						j=j-1;
                       	if(i==itr)
						  j=index;
						
					    trs[i].cells[j].colSpan=parseInt("" + trs[i].cells[j].colSpan)-mcm;
					
				     }
				}
		      //FIN AJUSTE COLSPAN
		
		}//fin si es td
		
     		if(param.f_tipo=="tr"){  //no va
             if(param.f_dir=="ab"){
				  var contenido="";
				  var aborrar=0;
				  var cc=0;
				  var fin=trs.length;

				  for(var i=itr+1;i<fin&&aborrar<param.f_n;i++){
					  aborrar=aborrar+1;
					  var flag=0;
					  var nindex=0;
					  for(j=0;j<trs[i].cells.length&&flag==0;j++){
							nindex=nindex+parseInt("" + trs[i].cells[j].colSpan);
							  if(nindex>=index){										
                                     flag=1;
							  }
					  }
					  j=j-1;
					  contenido=contenido+trs[i].cells[j].innerHTML;
		     		  cc=cc+parseInt("" + trs[i].cells[j].rowSpan)-1;
				  }
				  var aborrar=0;
				  for(var i=itr+1;i<fin&&aborrar<param.f_n;i++){
				    aborrar=aborrar+1;
					 var flag=0;
					  var nindex=0;
					  for(j=0;j<trs[i].cells.length&&flag==0;j++){
							nindex=nindex+parseInt("" + trs[i].cells[j].colSpan);
							  if(nindex>=index){										
                                     flag=1;
							  }
					  }
					  j=j-1;
					  if(trs[i].cells.length>1){
					  trs[i].removeChild(trs[i].cells[j]);
					  }else{
					  trs[i].parentNode.removeChild(trs[i]);
					  var fin=trs.length;
					  itr=td.rowIndex;
					  //cc=cc-1;
					  i=i-1;
					 }
				  }
				  td.innerHTML=td.innerHTML+contenido;
				  td.rowSpan=parseInt("" + td.rowSpan)+parseInt(param.f_n)+parseInt(cc);
      		  }
			  
			  //AJUSTE DE ROWSPAN
			  var mcm=0;
			   var index = td.cellIndex;
			    for (var i=0;i<tr.cells.length;i++) {
					
					  if(mcm==0)
					     mcm=parseInt("" + tr.cells[i].rowSpan);
					   else if( parseInt("" + tr.cells[i].rowSpan)<mcm)
					     mcm=parseInt("" + tr.cells[i].rowSpan);
				}
               
				mcm=mcm-1;
				if(mcm>0){
					for (var i=0;i<tr.cells.length;i++) {
						    					
					    tr.cells[i].rowSpan=parseInt("" + tr.cells[i].rowSpan)-mcm;
					
				     }
				}
			  //FIN AJUSTE ROWSPAN
		    
		}//fin si es tr
      
  editor._tree();  
	}, outparam);

}
}
NINBOX.prototype._doImage=function(){
 var editor=this;
 this.foc();
 var r=0;
 var p=null;
 var outparam = null;

if((p=this.getNodewithTag("img"))!=null){
 im_url=p.getAttribute("src");
 im_alt=p.getAttribute("alt");
 im_border=p.getAttribute("border");
 im_align=p.getAttribute("align");
 im_valign=p.getAttribute("vspace");
 im_halign=p.getAttribute("hspace");
 r=1;

 outparam = {
        f_url : im_url,
		f_alt : im_alt,
		f_border : im_border,
		f_align : im_align,
		f_vert : im_valign,
		f_horiz : im_halign 
	};
}
  
	this._popupDialog(_editor_url+"templates/image.html", function(param) {
	    
		if (param=="undefined")
			return false;
		
		if (r==0) {
			var sel = editor._getSelection();
			var range = editor._createRange(sel);
			editor._doc.execCommand("insertimage", false, param.f_url);
			
			if (NINBOX.is_ie) {
				img = range.parentElement();
				// wonder if this works...
				if (img.tagName.toLowerCase() != "img") {
					img = img.previousSibling;
				}
			} else {
				img = range.startContainer.previousSibling;
			}
		} else {
			img=p;
			img.src = param.f_url;
		}
		for (field in param) {
			var value = param[field];
			switch (field) {
			    case "f_alt"    : img.alt	 = value; break;
			    case "f_border" : img.border = parseInt(value || "0"); break;
			    case "f_align"  : img.align	 = value; break;
			    case "f_vert"   : img.vspace = parseInt(value || "0"); break;
			    case "f_horiz"  : img.hspace = parseInt(value || "0"); break;
			}
		}  editor._tree();  
	}, outparam,"i");

}
NINBOX.prototype._doTable = function() {
	this.foc();
	var sel = this._getSelection();
	var range = this._createRange(sel);
	var editor = this;	// for nested functions

	this._popupDialog(_editor_url+"templates/table.html", function(param) {
		if (param=="undefined") {	// user must have pressed Cancel
			return false;
		}
		var doc = editor._doc;
		// create the table element
		var table = doc.createElement("table");
		// assign the given arguments
		for (var field in param) {
			var value = param[field];
			if (!value) {
				continue;
			}
			switch (field) {
				case "f_height"   : table.style.height = value + param["f_unit2"]; break;
			    case "f_width"   : table.style.width = value + param["f_unit"]; break;
			    case "f_align"   : table.align	 = value; break;
			    case "f_border"  : table.border	 = parseInt(value); break;
			    case "f_spacing" : table.cellspacing = parseInt(value); break;
			    case "f_padding" : table.cellpadding = parseInt(value); break;
			}
		}
		var tbody = doc.createElement("tbody");
		table.appendChild(tbody);
		for (var i = 0; i < param["f_rows"]; ++i) {
			var tr = doc.createElement("tr");
			tbody.appendChild(tr);
			for (var j = 0; j < param["f_cols"]; ++j) {
				var td = doc.createElement("td");
				tr.appendChild(td);
				// Mozilla likes to see something inside the cell.
				td.innerHTML="&nbsp;";
			}
		}
		if (NINBOX.is_ie) {
			range.pasteHTML(table.outerHTML);
		} else {
			// insert the table
			editor.insertNodeAtSelection(table);
		}
		editor._tree();
		return true;
	}, null,"wer");
};

NINBOX.prototype._popupDialog = function(url, action, init,o) {
	Dialog(url, action, init,o);
};


NINBOX.prototype._doNolink=function(){
  var editor=this;
this.foc();
var p=null;
p=this.getNodewithTag("a");
if(p!=null&&p.getAttribute("href")!=""&&p.getAttribute("href")!=null){
 var content=p.innerHTML;
 a=p.parentNode;
 a.removeChild(p);
 this.insertHTML(content);
}
  this._tree();
}
NINBOX.prototype.getNodewithTag=function(tag){
  var p=null;
  p=this._getParentElement(0);
  if(p!=null){
	 while(p.tagName.toLowerCase()!=tag.toLowerCase()&&p.tagName.toLowerCase()!="html")
	 p=p.parentNode;
  }   
  if(p.tagName.toLowerCase()=="html")
	  p=null;
   return p;
}
NINBOX.prototype.insertHTML=function(html){
var sel = this._getSelection();
var range = this._createRange(sel);
if (NINBOX.is_ie) {
		range.pasteHTML(html);
} else {
   // construct a new document fragment with the given HTML
		var fragment = this._doc.createDocumentFragment();
		var div = this._doc.createElement("div");
		div.innerHTML = html;
		while (div.firstChild) {
			// the following call also removes the node from div
			fragment.appendChild(div.firstChild);
		}
		// this also removes the selection
		var node = this.insertNodeAtSelection(fragment);
   }
};
NINBOX.prototype.insertNodeAtSelection = function(toBeInserted) {
	if (!NINBOX.is_ie) {
		var sel = this._getSelection();
		var range = this._createRange(sel);
		// remove the current selection
		sel.removeAllRanges();
		range.deleteContents();
		var node = range.startContainer;
		var pos = range.startOffset;
		switch (node.nodeType) {
		    case 3: // Node.TEXT_NODE
			// we have to split it at the caret position.
			if (toBeInserted.nodeType == 3) {
				// do optimized insertion
				node.insertData(pos, toBeInserted.data);
				range = this._createRange();
				range.setEnd(node, pos + toBeInserted.length);
				range.setStart(node, pos + toBeInserted.length);
				sel.addRange(range);
			} else {
				node = node.splitText(pos);
				var selnode = toBeInserted;
				if (toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */) {
					selnode = selnode.firstChild;
				}
				node.parentNode.insertBefore(toBeInserted, node);
				this.selectNodeContents(selnode);
				this._tree();
			}
			break;
		    case 1: // Node.ELEMENT_NODE
			var selnode = toBeInserted;
			if (toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */) {
				selnode = selnode.firstChild;
			}
			node.insertBefore(toBeInserted, node.childNodes[pos]);
			this.selectNodeContents(selnode);
			this._tree();
			break;
		}
	} else {
		return null;	// this function not yet used for IE <FIXME>
	}
};
NINBOX.prototype.setMode = function(mode) {
	if (typeof mode == "undefined") {
		mode = ((this._editMode == "textmode") ? "wysiwyg" : "textmode");
	}

   switch (mode) {
	    case "textmode":
		//this._textArea.value = this.getHTML();
        this._textArea.value = this.getInnerHTML();
		this._iframe.style.display = "none";
		this._textArea.style.display = "block";
        this._offtoolbar();
		break;
	    case "wysiwyg":
		if (NINBOX.is_gecko) {
			// disable design mode before changing innerHTML
			try {
				this._doc.designMode = "off";
			} catch(e) {};
		}
		this._doc.body.innerHTML = this.getInnerHTML();
        this._iframe.style.display = "block";
		this._textArea.style.display = "none";
       if (NINBOX.is_gecko) {
			// we need to refresh that info for Moz-1.3a
			try {
				this._doc.designMode = "on";
			} catch(e) {};
		}
        this._ontoolbar();
		break;
	    default:
		alert("Mode <" + mode + "> not defined!");
		return false;
	}
	this._editMode = mode;
	this._tree();
    this.foc();
}
