//Constructor del Objeto Ajax
function crearAjax(){
	//variables
	this.xmlHttp = null;
	//funciones
	this.xmlHttpObject = xmlHttpObject;
	this.llamarAjax = llamarAjax;
}

//Funcion GetxmlHttpObject;
function xmlHttpObject(){
	try{
		// Firefox, Opera 8.0+, Safari
		this.xmlHttp=new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			this.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
}

// Para llamar una funcion con el responseText como parametro
function llamarFuncion(resp, val){
	var response= String(resp);
	//alert(response);
	var s1=escape(response.substr(0,response.length));
	//window.alert(response+'-'+s1+'-');
	var valst = val + "('" + s1 + "');";
	//alert(valst);
	eval(valst);
}

// Para meter el responseText en un Id
function llamarId(resp, val){
	document.getElementById(val).innerHTML = resp;
}

// Para meter el responseText en un Value
function llamarValue(resp, val){
		document.getElementById(val).value = resp;
}

// Para meter el responseText en un Id sin eliminar el contenido
function agregartextoaid(resp, val){
  //se guarda el contenido anterior
  var txtant = document.getElementById(val).innerHTML;
  //se concatena con el nuevo contenido
  var txtfinal = txtant + resp;
  //se sobreescribe todo el texto concatenado
  document.getElementById(val).innerHTML = txtfinal ;
}

// LLama a una pagina que dara el  responseText
function llamarAjax(url, param){
	this.xmlHttp.open("POST",url,true);
	this.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.xmlHttp.send('&'+ param);
}

// Constructor de la Clase Ajax
function claseAjax(){
	//funciones
	this.iniciarAjax = iniciarAjax;
}

/* recibe:

	el Objeto de Ajax (objAjax);
	la pagina (url);
	los parametros que se envian por Post(param);
	el tipo (tipo)
	el valor (val)

	tipo 0: Cuando se llama a una Funcion; valor: Funcion a llamar;
	tipo 1: Cuando se guarda en un ID; valor: Id en donde guarda el response;
	tipo 2: Cuando se guarda en un Value; valor: Value en donde guarda el response;
*/
function iniciarAjax(objAjax, url, param, tipo, val){

	objAjax.xmlHttpObject();
	if (objAjax.xmlHttp==null){
		alert ("Your browser does not support AJAX!");
	  	return;
	}
	objAjax.xmlHttp.onreadystatechange = function (){
		if (objAjax.xmlHttp.readyState==4){
			var response= objAjax.xmlHttp.responseText;
			if(tipo==0)
				llamarFuncion(response, val);
			if(tipo==1)
				llamarId(response, val);
			if(tipo==2)
				llamarValue(response, val);
			if(tipo==3)
				agregartextoaid(response, val)				
		}
	}
	objAjax.llamarAjax(url, param);
}

//corrige caracteres a html que van a ser mostrados en web
function corregirhtml(txt){
	txt.replace('á', '&aacute;');
	txt.replace('é', '&eacute;');
	txt.replace('í', '&iacute;');
	txt.replace('ó', '&oacute;');
	txt.replace('ú', '&uacute;');
	txt.replace('Á', '&Aacute;');
	txt.replace('É', '&Eacute;');
	txt.replace('Í', '&Iacute;');
	txt.replace('Ó', '&Oacute;');
	txt.replace('Ú', '&Uacute;');
        txt.replace('ñ', '&ntilde;');
        txt.replace('N', '&Ntilde;');
	return txt;
}

function gebidval(id){
    var valor = document.getElementById(id).value;
    return convertir_formatohtml(valor);
}
