function CrearAJAX() {
	
	var re = false;
	
	try {
		re = new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e1) {
		try {
		re = new ActiveXObject('Microsoft.XMLHTTP');
	} catch(e2) {
		re = false;
	}
	}
	
	if(!re && (typeof XMLHttpRequest != 'undefined' || window.XMLHttpRequest)) {
		re = new XMLHttpRequest();
	}
		
	return re;
}

var ajax = CrearAJAX();

function procesar(id) {
	var rand = Math.random(9999);
	var dire = id + '.php?rand=' + rand;
	
	ajax.open('GET', dire, true);
	ajax.onreadystatechange = tab;
	ajax.send(null);
}

function estilos() {
	var rand = Math.random(9999);
	
	ajax.open('GET', 'estilos/update.php', true);
	ajax.onreadystatechange = updestilo;
	ajax.send(null);
}

function emoticonos() {
	var rand = Math.random(9999);
	
	ajax.open('GET', 'emoticonos/update.php', true);
	ajax.onreadystatechange = updemo;
	ajax.send(null);
}

function tab() {
	if(ajax.readyState != 4) {
		document.getElementById('content').innerHTML = "Cargando...";
	} else {
		document.getElementById('content').innerHTML = ajax.responseText;
	}
}

function updestilo() {
	if(ajax.readyState != 4) {
		document.getElementById('update').innerHTML = "<p>Cargando...</p>";
	} else {
		document.getElementById('update').innerHTML = "<p>Los estilos han sido actualizados, actualize la pagina.</p>"
	}
}

function updemo() {
	if(ajax.readyState != 4) {
		document.getElementById('updatemo').innerHTML = "<p>Cargando...</p>";
	} else {
		document.getElementById('updatemo').innerHTML = "<p>Los emoticonos han sido actualizados, actualize la pagina.</p>"
	}
}

function hideshow(which){
if (!document.getElementById)
return
if (which.style.display=="block")
which.style.display="none"
else
which.style.display="block"
}

function addText( text, text2, id )
{
    diff = document.getElementById('charEnd').value - document.getElementById('charStart').value;
    document.getElementById( id ).value = document.getElementById( id ).value.substr( 0, document.getElementById('charStart').value ) + text + document.getElementById( id ).value.substr( document.getElementById('charStart').value, diff ) + text2 + document.getElementById( id ).value.substr( document.getElementById('charEnd').value );
    document.getElementById( id ).focus();
    CharPos( id );
    return true;
}

function getCharPos( id )
{
    var charPos = new Object();
    var element = document.getElementById( id );
    if( document.all ){
        var range = document.selection.createRange();
        var stored_range = range.duplicate();
        stored_range.moveToElementText( element );
        stored_range.setEndPoint('EndToEnd', range );
        charPos.start = stored_range.text.length - range.text.length;
        charPos.end = charPos.start + range.text.length;
    }
    else
    {
        charPos.start = element.selectionStart;
        charPos.end = element.selectionEnd;
	}
    charPos.length = charPos.end - charPos.start;
    return charPos;
}

function CharPos( id )
{
    var charPos = getCharPos( id );
    document.getElementById('charStart').value = charPos.start;
    document.getElementById('charEnd').value = charPos.end;

} 

function Addsimpletext( text, id )
{
	document.getElementById( id ).value += text;	
}

function Addimage( url, borde, id )
{
	document.getElementById( id ).value += "[img]";
	document.getElementById( id ).value += url;
	document.getElementById( id ).value += "[/img]";
	document.getElementById( id ).value += borde;
	document.getElementById( id ).value += "[//img]";
}
function pimage()
{
	var image = prompt("Introduzca la direccion de la imagen","http://");
	var borde = prompt("¿Cuantos pixeles de borde desea?");
		if(image!=null)
		{
			Addimage( image, borde, 'noticia');
		}
	
}
function Addurl( url, texto, id )
{
	document.getElementById( id ).value += "[url]";
	document.getElementById( id ).value += url;
	document.getElementById( id ).value += "[/url]";
	document.getElementById( id ).value += texto;
	document.getElementById( id ).value += "[//url]";
}
function purl()
{
	var url = prompt("Introduzca la direccion","http://");
	var texto = prompt("Introduzca un texto para la direccion","");
		if(url!=null && texto!=null)
		{
			Addurl( url, texto, 'noticia');
		}
	
}
function acteditor( titulo, texto, id, which)
{
		which.style.display="block"

	while (texto.indexOf("<br />") != -1) {
		texto = texto.replace("<br />","\n");
	}
	while (texto.indexOf("<comilla>")!=-1) {
		texto = texto.replace("<comilla>","'");
	}
	while (texto.indexOf("<intrdrch>")!=-1) {
		texto = texto.replace("<intrdrch>","¿");
	}
	while (texto.indexOf("<cr1>")!=-1) {
		texto = texto.replace("<cr1>","ç");
	}
	while (texto.indexOf("<cr2>")!=-1) {
		texto = texto.replace("<cr2>","¡");
	}
	while (texto.indexOf("<cr3>")!=-1) {
		texto = texto.replace("<cr3>","´");
	}
	while (texto.indexOf("<cr4>")!=-1) {
		texto = texto.replace("<cr4>",'"');
	}
	while (texto.indexOf("<cr5>")!=-1) {
		texto = texto.replace("<cr5>",'ª');
	}
	while (texto.indexOf("<cr6>")!=-1) {
		texto = texto.replace("<cr6>",'\\');
	}
	while (texto.indexOf("<cr7>")!=-1) {
		texto = texto.replace("<cr7>",'·');
	}
	while (texto.indexOf("<cr8>")!=-1) {
		texto = texto.replace("<cr8>",'Ç');
	}
	while (texto.indexOf("<cr9>")!=-1) {
		texto = texto.replace("<cr9>",'¬');
	}
	while (texto.indexOf("<cr10>")!=-1) {
		texto = texto.replace("<cr10>",'¨');
	}
	while (texto.indexOf("<cr11>")!=-1) {
		texto = texto.replace("<cr11>",'º');
	}
	while (texto.indexOf("<cr12>")!=-1) {
		texto = texto.replace("<cr12>",'ñ');
	}
	while (texto.indexOf("<cr13>")!=-1) {
		texto = texto.replace("<cr13>",',');
	}
	document.getElementById('noticia').value = texto;
	document.getElementById('tituloup').value = titulo;
	document.getElementById('idup').value = id;
	
}

function seguro( titulo, id )
{
var x=window.confirm('¿Estas seguro de que quieres eliminar la noticia con el titulo "'+titulo+'"?')
if (x)
location.href="funciones.php?q=eliminar&id="+id;	
}

function limpiar ( id, valued )
{
if(document.getElementById( id ).value==valued) {
	document.getElementById( id ).value = "";	
}
}

function seleccionarcolor ( color )
{
if(document.getElementById('color').value=="brdcolors") {
	document.getElementById('brdcolors').value=color;
}
if(document.getElementById('color').value=="fondcolors") {
	document.getElementById('fondcolors').value=color;
}
if(document.getElementById('color').value=="fondcolornots") {
	document.getElementById('fondcolorsnot').value=color;
}
}

function cerrarfull ( which , idimg)
{
if (which.style.display=="block") {
which.style.display="none"
} else {
which.style.display="block"
}

if (which.style.display=="block") {
var img = window.document.getElementById("imgfull"+idimg).src;
img = img.replace("mas.gif",'menos.gif');
window.document.getElementById("imgfull"+idimg).src = img;

} else {
var img = window.document.getElementById("imgfull"+idimg).src;
img = img.replace("menos.gif",'mas.gif');
window.document.getElementById("imgfull"+idimg).src = img;
}

}

function changepass ( id )
{
var newpass = prompt("Introduzca la nueva contraseña","********");
		if(newpass!=null)
		{
			location.href="funciones.php?id="+id+"&acc=changepass&newpass="+newpass;
		}
}

function checkv() {
if(document.getElementById("comentario").value == "") {
alert("No escribiste nada en el comentario");
return (false);
}
if(document.getElementById("comentario").value == " ") {
alert("Debes escribir algo en el comentario");
return (false);
}
if(document.getElementById("name").value == "") {
alert("No escribiste nada en el nombre");
return (false);
} 
if(document.getElementById("name").value == " ") {
alert("Debes escribir algo en el nombre");
return (false);
}
if(document.getElementById("mail").value == "") {
alert("No escribiste nada en el email");
return (false);
}
if(document.getElementById("mail").value == " ") {
alert("Debes escribir algo en el email");
return (false);
}
if(document.getElementById("mail").value == "Email") {
alert("Debes escribir algo en el email");
return (false);
}
if(document.getElementById("comentario").value == "Escribe tu comentario") {
alert("Debes escribir algo en el comentario");
return (false);
}
if(document.getElementById("name").value == "Nombre") {
alert("Debes escribir algo en el nombre");
return (false);
}
if(!escorreo(document.getElementById("mail"))) {
alert("Debes escribir un correo valido");
return (false);
}
return (true);
}

function escorreo(theElement)
{
var s = theElement.value;
var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
if (s.length == 0 ) return true;
if (filter.test(s))
return true;
else
return false;
}