// JavaScript Document
function nuevoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  		// Creación del objeto ajax para navegadores diferentes a Explorer
	} catch (e) {
		try {								// o bien
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");		// Creación del objet ajax para Explorer
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
function ajaxlTrim(sStr) {
	while (sStr.charAt(0) == " " || sStr.charAt(0) == "	") sStr = sStr.substr(1, sStr.length - 1); 
	return sStr; 
} 

function ajaxrTrim(sStr){ 
	while (sStr.charAt(sStr.length - 1) == " " || sStr.charAt(sStr.length - 1) == "	") sStr = sStr.substr(0, sStr.length - 1); 
	return sStr; 
} 
function ajaxallTrim(sStr){ 
	return ajaxrTrim(ajaxlTrim(sStr)); 
}
function fAjax(URL, Capa, theForm, Metodo, JS, BorraCapa) {
	var mAct = Math.random() * 1000;

	if (BorraCapa != '' && BorraCapa != null) document.getElementById(BorraCapa).innerHTML = '';

	document.getElementById('hStatusAjax').style.background = '#FF0000';
	document.getElementById('hStatusAjax').style.color = '#FFFFFF';
	document.getElementById('hStatusAjax').style.top = document.body.scrollTop;
	document.getElementById('hStatusAjax').style.left = 0;
	document.getElementById('hStatusAjax').innerHTML = '&nbsp;cargando ...';

	ajax=nuevoAjax();
	if (Metodo != null && Metodo.toUpperCase() == 'POST') {
		ajax.open("POST", URL, true);
	}else {
		if (URL.search('=') == -1) {
			ajax.open("GET", URL+"?t="+mAct, true);
		}else {
			ajax.open("GET", URL+"&t="+mAct, true);
		}
	}
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4) {

			if (JS != null && JS.toUpperCase() == 'SI') {
				var aRespuestaAjax = ajax.responseText.split("\n");
				var j = 0;
				var k = '';
				for (var i=0; i<aRespuestaAjax.length; i++) {
					aRespuestaAjax[i] = ajaxallTrim(aRespuestaAjax[i]);
					if (aRespuestaAjax[i].substr(0, 9).toLowerCase() == '</script>') {
						j = 0;
					}	// if
					if (j == 1) {
						eval(aRespuestaAjax[i]);
					}else {
						k += aRespuestaAjax[i] + "\n";
					}	// if	// if	// if
					if (aRespuestaAjax[i].substr(0, 7).toLowerCase() == '<script') {
						j = 1;
					}	// if
				}	// for
				document.getElementById(Capa).innerHTML = k;
			}else {
				document.getElementById(Capa).innerHTML = ajax.responseText;
			}
			document.getElementById('hStatusAjax').style.background = '';
			document.getElementById('hStatusAjax').innerHTML = '';
		}
	}
	if (Metodo != null && Metodo.toUpperCase() == 'POST') {
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		var nCampo;
		var Valores = '';
		for (var i=0; i<theForm.length; i++) {
			nCampo = theForm[i].name;
			if (theForm[i].type == 'select-multiple') {
				for (var j=0; j<theForm[i].length; j++)
				{
					if (theForm[i].options[j].selected) 
					{
						if (Valores != '') Valores += '&';
						Valores += nCampo + '=' + escape(theForm[i].options[j].value);
					}
				}
			}else {
				if (nCampo != '') {
					if (Valores != '') Valores += '&';
					Valores += nCampo + '=' + escape(theForm[nCampo].value);
				}
			}
		}
		ajax.send(Valores);
	}else {
		ajax.send(null);
	}
}

