var Url = { 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
/*
function listaCamposForm(nombre_formulario){  //Recoge todos los campos de un formulario y los concatena para el ajax.
	if (typeof nombre_formulario == "string")
		var formulario = document[nombre_formulario];
	else if (typeof nombre_formulario == "object")
		var formulario = nombre_formulario;
	else
		var formulario = 0;

	if (formulario != 0) {
		var i = 0;
		var campos = "";
		var nombre_campo = "";
		var valor_campo = "";
		while (formulario.elements[i]){
			if (typeof formulario.elements[i].type != "undefined"){			
				nombre_campo = "";
				valor_campo = "";
				if (formulario.elements[i].type != "radio" && formulario.elements[i].type != "checkbox"){
					nombre_campo 	= formulario.elements[i].name;
					valor_campo 	= formulario.elements[i].value;
				}
				else if (formulario.elements[i].type == "checkbox" || formulario.elements[i].type == "radio"){
					if (formulario.elements[i].checked){
						nombre_campo 	= formulario.elements[i].name;
						valor_campo		= formulario.elements[i].value;
					}
				}
				
				if (campos && nombre_campo != "")
					campos += "&"+nombre_campo+"="+Url.encode(valor_campo);
				else if (nombre_campo != "")
					campos = nombre_campo+"="+Url.encode(valor_campo);
			}
			i++;
		}
	} else {
		alert("El formulario que esta insertando en la funcion de ajax no es correcto.");
		campos = "";
	}
		
	return campos;
}
*/

function funcion_verificacion(funcionEjecutar) {
	if (http.readyState == 4)
	{ 
		if (http.status == 200)
		{
			if (http.responseText.indexOf('invalid') == -1)
			{
				enProceso = false;
				var tmp = http.responseText.replace(/^\s+|\s+$/, '');
				resultados = tmp.split("|||");
				
				if (typeof funcionEjecutar == "function" && funcionEjecutar != ""){
					funcionEjecutar(resultados[0]);
				} else {
					verificacion(resultados[0]);
				}
			}
		}
		else
		{
			//alert("Se ha producido un error en la consulta.");
			enProceso = false;
		}
	}
}

function hacer_consulta(url, variables, funcionEjecutar) {
	if (!enProceso && http){
		http.open("POST", url, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		if (typeof funcionEjecutar == "undefined"){
			funcionEjecutar = "";
		}
		http.onreadystatechange = function(){ funcion_verificacion(funcionEjecutar);};
		enProceso = true;
		http.send(variables);
	}
}

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}

		catch (e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E) 
			{
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try { xmlhttp = new XMLHttpRequest();}
		catch (e) {	xmlhttp = false;}
	}
	return xmlhttp;
}

var enProceso = false; // lo usamos para ver si hay un proceso activo
var http = getHTTPObject(); // Creamos el objeto XMLHttpRequest

