function e(getid){ return document.getElementById(getid); }

var make = new Object();

make.Ajax = function(array){
	this.VerificaObjeto();
	this.url = array['url'];
	this.metodo = array['metodo'];
	this.vars = (array['metodo'] == "GET" && typeof array['vars'] == "undefined")? null : array['vars'];
	this.asincrono = (array['asincrono'])? array['asincrono'] : true;
	this.EnviaDatos();
	this.cargando =  (typeof array.cargando == "function")? array.cargando : null;
	this.cargaCompleta = array.cargaCompleta;
	this.muestraError = (typeof array.errorFuncion == "function")? array.errorFuncion : this.errorFunction;
	this.CargaDatos();
}

make.Ajax.prototype = {
	VerificaObjeto : function(){
		var majax = false;
		try {
			majax = new ActiveXObject("msxml2.XMLHTTP");
		} catch(e){
			try{
				majax=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E){
				majax=false;
			}
		
		}
	
		if(!majax && typeof XMLHttpRequest != 'undefined'){
			majax=new XMLHttpRequest();
		}
		this.objeto=majax;
	},
	
	EnviaDatos : function(){	
		this.objeto.open(this.metodo,this.url,this.asincrono);
		if(this.metodo == "POST"){
			this.objeto.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		}
		this.objeto.send(this.vars);	
	},
	
	CargaDatos : function(){
		var este = this;
		este.objeto.onreadystatechange=function(){
			if(este.objeto.readyState == 4 && este.objeto.status == 200){
				este.respuesta = este.objeto.responseText;
				este.cargaCompleta();
			}
			else if (este.objeto.readyState == 4 && este.objeto.status == 404){
				este.muestraError();
			}
		}
		
		if(typeof this.cargando == "function"){
			this.cargando();
		}
	},
	
	errorFunction : function(){
		alert("Error:\nXMLHttpRequest.status: "+this.objeto.status+"\nXMLHttpRequest.readyState :"+this.objeto.readyState+"\nXMLHttpRequest.AllResponseHeaders: "+this.objeto.getAllResponseHeaders());
	}	
}

//comentamos
function comentar(identrada,logeado,urlblog){
	//damos de alta las varibles
	var comentario = e('com');
	var i = e('i');
	if(logeado){
		//verificamos datos vacios.
		if(comentario.value == "" && i.value == ""){
			alert("Debes llenar todos los datos que se te piden para poder comentar.");
			var comenta = false;
		}
		else{
			var comenta = true;
		}
		
		var enviarvars = '';
		
	}
	else{
		var u = e('ucom');
		var email = e('email');
		var pgweb = e('pgweb');
		
		if(comentario.value == "" && i.value == "" && u.value == "" && email.value == ""){
			alert("Debes llenar todos los datos que se te piden para poder comentar.");
			var comenta = false;
		}else{
			var comenta = true;
		}
		
		var enviarvars = '&u='+u.value+'&email='+email.value+'&pgweb='+pgweb.value;
	}
	
	if(comenta){
		var comentar=new make.Ajax({
			'url' : 'http://'+window.location.host+'/'+urlblog+'/comentar.php',	
			'metodo' : 'POST',
			'vars' : 'action=comentar&identrada='+identrada+'&captcha='+i.value+'&com='+comentario.value+enviarvars,
			cargaCompleta : function(){
				var mensaje = this.respuesta.split('<!----->');
				alert(mensaje[2]);
				if(eval(mensaje[1])){
					window.location=mensaje[3];
				}
			}
		});
	}
}

function login(action){
	if(action == 'login'){
		var usuario = e('u');
		var contra = e('pass');
	
		if(usuario.value == ""){
			alert("El usuario esta vacio.");
		}
		else if(contra.value == ""){
			alert("No has escrito tu contraseña.");
		}
		else{
			var login=new make.Ajax({
				'url' : 'http://'+window.location.host+'/login.php',	
				'metodo' : 'POST',
				'vars' : 'logear=inicio&u='+usuario.value+'&pass='+contra.value,
				cargaCompleta : function(){
					var mensaje = this.respuesta.split('<!----->');
					alert(mensaje[2]);
					if(eval(mensaje[1])){
						e('login').innerHTML = mensaje[3];
					}
					
				}
			});
		}
	}else{
		var logout=new make.Ajax({
				'url' : 'http://'+window.location.host+'/login.php',	
				'metodo' : 'POST',
				'vars' : 'logear=fin',
				cargaCompleta : function(){
					var mensaje = this.respuesta.split('<!----->');
					alert(mensaje[2]);
					if(eval(mensaje[1])){
						e('login').innerHTML = mensaje[3];
					}
					
				}
			});
	}
}