
function insertarCodigo(campo, codigo) {
		if (document.selection) {
			//ie
			campo.focus();
			sel = document.selection.createRange();
			sel.text = codigo;
		} else if (campo.selectionStart || campo.selectionStart == '0') {
			//moz
			var startPos = campo.selectionStart;
			var endPos = campo.selectionEnd;
			campo.value = campo.value.substring(0, startPos) + codigo + campo.value.substring(endPos, campo.value.length);
		} else {
			campo.value += codigo;
		}
		document.agregar_post.texto.focus();
	}


function creaTag(tag) {

	campo = document.agregar_post.texto;

	//hay seleccion?
	hayseleccion = false;
	if(campo.selectionStart || campo.selectionStart == '0') {
		var texto = campo.value.substring(campo.selectionStart,campo.selectionEnd);
		if(texto != "") hayseleccion = true;
	} else hayseleccion = false;
	

	if(tag=='b') {
		if(!hayseleccion) texto = prompt("Introduce el texto en negrita","");
		if(texto != "") insertarCodigo(document.agregar_post.texto, "<strong>" + texto + "</strong>");
	} else if(tag=='i') {
		if(!hayseleccion) texto = prompt("Introduce el texto en cursiva","");
		if(texto != "") insertarCodigo(document.agregar_post.texto, "<em>" + texto + "</em>");
	} else if(tag=='s') {
		if(!hayseleccion) texto = prompt("Introduce el texto a tachar","");
		if(texto != "") insertarCodigo(document.agregar_post.texto, "<s>" + texto + "</s>");
	} else if(tag=='a') {
		if(!hayseleccion) texto = prompt("Introduce el texto a enlazar","");
		url = prompt("Introduce la dirección del enlace (URL)","http://");
		urlTitle = prompt("Introduce el Title de la URL","");
		if(texto == "") { texto = url; }
		if(url!="http://" && texto!="") {
			insertarCodigo(document.agregar_post.texto, "<a href=\"" + url + "\" title=\"" + urlTitle + "\">" + texto + "</a>");
		}
	}else if(tag=='e') {
		if(!hayseleccion) texto = prompt("Introduce el texto a enlazar","");
		url = prompt("Introduce la dirección de correo eletrónico","");
		if(texto == "") { texto = url; }
		if(url!="http://" && texto!="") {
			insertarCodigo(document.agregar_post.texto, "<a href=\"mailto:" + url + "\">" + texto + "</a>");
		}
	}
}


function verComentario(){
	var pre=document.agregar_post.texto.value;
	var re = new RegExp('\n', 'gi');
	pre=pre.replace(re, "<br />");
	pw.innerHTML='<p>' + pre + '</p>';
	return true;
}
function crear_botones(){
	document.write("<input name=\"Negrita\" type=\"button\" id=\"Negrita\" value=\"Negrita (b)\" class=\"button\" accesskey=\"b\" onclick=\"creaTag('b')\" /> ");
	document.write("<input name=\"Cursiva\" type=\"button\" id=\"Cursiva\" value=\"Cursiva (i)\" class=\"button\" accesskey=\"i\" onclick=\"creaTag('i')\" /> ");
	document.write("<input name=\"Vinculo\" type=\"button\" id=\"Vinculo\" value=\"Vinculo (w)\" class=\"button\" accesskey=\"w\" onclick=\"creaTag('a')\" /> ");
	document.write("<input name=\"Code\" type=\"button\" id=\"Code\" value=\"e-mail (e)\" class=\"button\"  accesskey=\"e\" onclick=\"creaTag('e')\" /> ");
}
