function ParseaAjax (metodo,url,div_contenido,div_cargando,valores) {

	if ((metodo.toUpperCase() != 'POST') && (metodo.toUpperCase() != 'GET')) {
		return;
	}

	var divContenido	= document.getElementById(div_contenido);
	var divCargando		= document.getElementById(div_cargando);
	var divCargando_cambia	= document.getElementById(div_cargando + "_cambia");

	if (valores != '') {
		var campos = valores.split(" ");
		var numero_campo = 0;
		var valores = '';
		while (numero_campo < campos.length) {
			if (document.getElementById(campos[numero_campo])) {
				valor_campo = document.getElementById(campos[numero_campo]).value;
			} else {
				valor_campo = '';
			}
			valores = valores + campos[numero_campo] + '=' + valor_campo + '&';
			numero_campo += 1;
		}
		valores = valores.substr(0,valores.length - 1);
		var ajax		= new Ajax.Updater(div_contenido, url, {method: metodo.toUpperCase(),asynchronous:true, evalScripts: true, parameters: valores });
	} else {
		var ajax		= new Ajax.Updater(div_contenido, url, {method: metodo.toUpperCase(),asynchronous:true, evalScripts: true });
	}
	
	img_cargando = "";

	if (img_cargando == "") {
		divCargando.innerHTML = '';
		divCargando.innerHTML = '&nbsp;&nbsp;&nbsp;<img src="/fotos/cargando_4.gif" class="cargando">';
	} else {

		if (div_cargando == "div_cargando") {
			divCargando.innerHTML = '<img src="/fotos/' + img_cargando + '.gif" class="' + img_cargando + '">';
			divCargando.style.visibility = 'visible';
		} else if (div_cargando == "div_notifica_cambia") {
			divCargando.innerHTML = '<div style="width: 220px; margin: 0 auto; color: #999999;"><div style="float: left;"><img src="/fotos/cargando.gif" alt="cargando" title="cargando" /></div><div>Un momento por favor ...</div></div>';
		} else {
			var myImage = new Image();
			myImage.src = '/fotos/' + img_cargando + '.gif';
			if (IE == 1) {
				margen_izquierdo	= Math.floor((divCargando.offsetWidth - myImage.width) / 2);
				margen_superior		= Math.floor((divCargando.offsetHeight - myImage.height) / 2);
				divCargando.innerHTML = '<img src="' + myImage.src + '" style="margin-left: ' + margen_izquierdo + 'px; margin-top: ' + margen_superior + 'px">';		
			} else {
				myImage.onload = function() {
					margen_izquierdo	= Math.floor((divCargando.offsetWidth - myImage.width) / 2);
					margen_superior		= Math.floor((divCargando.offsetHeight - myImage.height) / 2);
					divCargando.innerHTML = '<img src="' + myImage.src + '" style="margin-left: ' + margen_izquierdo + 'px; margin-top: ' + margen_superior + 'px">';
				}
			}
		}	
	}


	if (div_cargando == "div_cargando") {	
		divCargando.style.visibility = 'hidden';
	}
	
	return;
}

function nuevoAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function CargaCombo(combo, id_provincia, id_localidad_seleccionada) {
	var ajax = nuevoAjax();
	temp_timestamp = new Date().getTime();
	ajax.open("POST", "/xml/localidades/" + temp_timestamp + "/", true);
	ajax.onreadystatechange=function() {
		var obj = document.getElementById(combo);
		for(var i = 0; i < obj.options.length; i++) {
			obj.options[i] = null;
		}
		if(ajax.readyState == 3) {
			obj.options[0] = new Option("Cargando la lista...", "");
		}
		if (ajax.readyState==4) {
			var xmlData = ajax.responseXML;
			obj.options[0] = new Option("Seleccione de la lista ...", "");
			for(i=0; i<xmlData.getElementsByTagName("localidad").length; i++) {
				var elem = xmlData.getElementsByTagName("localidad")[i].childNodes[0].nodeValue;
				var id_localidad = xmlData.getElementsByTagName("localidad")[i].getAttribute("id");
				obj.options[i+1] = new Option(elem, id_localidad);
				if (id_localidad == id_localidad_seleccionada)
					obj.selectedIndex = i + 1;
			}
			obj.options.length = (i+1);
			obj.disabled = false;
		}
	}
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send("&id_provincia="+id_provincia);
}
