
var _navegador = navigator.userAgent;
var ie = /msi/i.test(_navegador);
var op = /opera/i.test(_navegador);
var mo = /gecko/i.test(_navegador);
var otro = !(ie || mo);
var _insertor, _insertar, _formulario, _texto, _lector = "";

function datos_ie() {
	_texto = document.selection.createRange().text;
	if (_formulario.createTextRange)
		_formulario.posi = document.selection.createRange().duplicate();
	return true;
}

function captura_ie()	{
	return _texto;
}

function captura_mo() {
	with (_formulario) return value.substring(selectionStart, selectionEnd);
}

function captura_otro()	{
	return "";
}

function poner_mo(f, x)	{//alert(x);
	var _ini = f.selectionStart;
	var _fin = f.selectionEnd;
	var inicio = f.value.substr(0, _ini);
	var fin = f.value.substr(_fin, f.value.length);

	f.value = inicio + x + fin;
	if (_ini == _fin)	{
		f.selectionStart = inicio.length + x.length;
		f.selectionEnd = f.selectionStart;
	}
	else	{
		f.selectionStart = inicio.length;
		f.selectionEnd = inicio.length + x.length;
	}
	f.focus();
}

function poner_otro(f, x)	{// opera u otros navegadores desconocidos
	f.value += x;//alert(x);
	f.focus();
}

function poner_ie(f, x)	{
	f.focus();
	if (f.createTextRange)	{// && f.posi)	{
		if (!f.posi)	datos_ie();
		with(f)	{
			var actuar = (posi.text == "");
			posi.text = x;
			if (!actuar)
				posi.moveStart("character", -x.length);
			posi.select();
		}
	}
}

function ini_editor(formu)	{
	_formulario = formu;
	
	if (op || mo)	{//alert("mozilla u opera");
		_insertar = function(f, x) {poner_mo(f, x);};
		_lector = captura_mo;
	}

else	if (otro)	{//alert("otro");
		_insertar = function(f, x) {poner_otro(f, x);};
		_lector = captura_otro;
	}

else	if (ie)	{
		_formulario.onchange = datos_ie;
		_formulario.onclick  = datos_ie;
		_insertar = function(f, x) {poner_ie(f, x);};
		_lector = captura_ie;
	}
	return formu;
}

function crearEnlace(nombreCampo)
{
	//alert("asdf");
	
	var seleccion;
	var opciones = "toolbar=0,location=0,status=1,menubar=0,scrollbars=0,resizable=0,width=200,height=200,left=200,top=200;"
	
	if(ie)
	{
		//alert(captura_ie());
		seleccion=captura_ie();
	}
	else
	{
		//alert(captura_mo());
		seleccion=captura_mo();
	}
	if (seleccion=="")
	{
		alert("Selecciona el texto donde deseas crear el enlace.");
	}
	else
	{
		AbrirVentana(seleccion, nombreCampo);
	}
}

function AbrirVentana(seleccion,nombreCampo)
{
	
    var ventana = null;
    var ancho, alto;
    ancho=350;
    alto=200;
    x = (screen.width - ancho) / 2;
    y = (screen.height - alto) / 2;
    basePage = "../../../creaEnlace.asp?seleccion="+seleccion+"&nombreCampo="+nombreCampo;
    if (ventana == null) {    
    ventana = window.open (basePage,"Crear_Enlace","toolbar=no,scrollbars=yes,status=no,left=" + x + ",top=" + y + ",width=" + ancho + ",height=" + alto + ",resizable");
    }
    else if (ventana.closed) {
    ventana = window.open (basePage,"Crear_Enlace","toolbar=no,scrollbars=yes,status=no,left=" + x + ",top=" + y + ",width=" + ancho + ",height=" + alto + ",resizable");
    } else {
    ventana.document.location=basePage;
    }
   
   ventana.focus ();
   
}

