// JavaScript Document
// PROCESAMIENTO DE PROMOCIONES

var precioDolar = 0.00;

Array.prototype.min = function() {
	var min = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
	return min;
}

Array.prototype.max = function() {
	var max = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
	return max;
}

function promocionServicioGratis(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto){
	while(document.getElementById("campoErroresPromocion").hasChildNodes()){
		document.getElementById("campoErroresPromocion").removeChild(document.getElementById("campoErroresPromocion").firstChild);
	}
	var request_promocion = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		request_promocion = new XMLHttpRequest();
		if (request_promocion.overrideMimeType) {
			request_promocion.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			request_promocion = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request_promocion = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!request_promocion) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	
	request_promocion.onreadystatechange = function(){
		if(request_promocion.readyState == 4){	
			var xmldoc = request_promocion.responseXML;
			var promocion = xmldoc.getElementsByTagName('promocion');
			
			var id = promocion.item(0).childNodes.item(0).childNodes.item(0).data;
			var tipo = promocion.item(0).childNodes.item(1).childNodes.item(0).data;
			var titulo = promocion.item(0).childNodes.item(2).childNodes.item(0).data;
			var descripcion = promocion.item(0).childNodes.item(3).childNodes.item(0).data;
			var fInicioValidez = promocion.item(0).childNodes.item(4).childNodes.item(0).data;
			var categoriasValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('categoria').length;i++){
				categoriasValidas.push(promocion.item(0).getElementsByTagName('categoria').item(i).childNodes.item(0).data);	
			}
			var diasValidos = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('dia').length;i++){
				diasValidos.push(promocion.item(0).getElementsByTagName('dia').item(i).childNodes.item(0).data);
			}
			var ubicacionesValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('ubicacion').length;i++){
				ubicacionesValidas.push(promocion.item(0).getElementsByTagName('ubicacion').item(i).childNodes.item(0).data);
			}
			var fFinValidez;
			if(promocion.item(0).childNodes.item(5).childNodes.item(0)){
				fFinValidez = promocion.item(0).childNodes.item(5).childNodes.item(0).data;
			}
			var diasMinimos = promocion.item(0).childNodes.item(6).childNodes.item(0).data;
			var diasMaximos;
			if(promocion.item(0).childNodes.item(7).childNodes.item(0)){
				promocion.item(0).childNodes.item(7).childNodes.item(0).data;
			}
			var codigoPromocion = promocion.item(0).childNodes.item(8).childNodes.item(0).data;
			var valorPromocion = promocion.item(0).childNodes.item(10).childNodes.item(0).data;
			var aplicable = promocion.item(0).childNodes.item(14).childNodes.item(0).data;
			
			if(diasMinimos!=null && diasRenta < diasMinimos){
				var lineaError = document.createElement("li");
				lineaError.appendChild(document.createTextNode("MINIMUM DAYS ARE NOT MET"));
				document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
			else{
				if(diasMaximos!=null && diasRenta > diasMaximos){
					var lineaError = document.createElement("li");
					lineaError.appendChild(document.createTextNode("MAXIMUM DAYS ARE EXCEEDED"));
					document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
				else{
					var validarDias = true;
					if(diasRenta>6){
						validarDias = true;	
					}
					else{
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						if(aplicable==0){
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[x]==rangoDiasRenta[y]){
										validarDias = true;	
									}
								}
							}
						}
						else{
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[z]==rangoDiasRenta[y]){
										encontrado = true;
									}
								}
								if(!encontrado){
									validarDias=false;	
								}
							}
						}
					}
					if(validarDias){
						
						//CONTINUAR CON EL PROCESO
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								if(z>7)
									z=1;
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						var arregloIdentificar = new Array();
						
						var tamArreglo = rangoDiasRenta.length;
						
						arregloIdentificar[0]=0;
						for(var b=0;b<tamArreglo;b++){
							var valor = rangoDiasRenta.pop();
							var existe = false;
							for(var c=0;c<diasValidos.length;c++){
								if(valor==diasValidos[c]){
									if(arregloIdentificar[valor])
										arregloIdentificar[valor]++;
									else
										arregloIdentificar[valor]=1;
									existe = true;
								}
							}
							if(!existe){
								arregloIdentificar[valor]=0;
							}
						}
						
						
						
						var arregloComparar = new Array();
						var iArregloComparar = 0;
						for(var c=0;c<arregloIdentificar.length;c++){
							if(arregloIdentificar[c]){
								arregloComparar[iArregloComparar++]=arregloIdentificar[c];
							}	
						}
						
						var cantidadPromocionesAplicar;
						
						if(aplicable==0){
							cantidadPromocionesAplicar = 0;
							for(var b=0;b<arregloIdentificar.length;b++){
								if(arregloIdentificar[b]){
									cantidadPromocionesAplicar += arregloIdentificar[b];
								}
							}
						}
						else{
							cantidadPromocionesAplicar = arregloComparar.min();
						}
						
						var validarUbicacion = false;
						for(var x=0;x<ubicacionesValidas.length;x++){
							if(ubicacionesValidas[x]==idUbicacionRenta){
								validarUbicacion = true;	
							}
						}
						
						if(validarUbicacion){							
							
							var request_categoria = false;
							if (window.XMLHttpRequest) { // Mozilla, Safari,...
								request_categoria = new XMLHttpRequest();
								if (request_categoria.overrideMimeType) {
									request_categoria.overrideMimeType('text/xml');
								}
							} else if (window.ActiveXObject) { // IE
								try {
									request_categoria = new ActiveXObject("Msxml2.XMLHTTP");
								} catch (e) {
									try {
										request_categoria = new ActiveXObject("Microsoft.XMLHTTP");
									} catch (e) {}
								}
							}
							if (!request_categoria) {
								alert('Cannot create XMLHTTP instance');
								return false;
							}
							request_categoria.onreadystatechange = function(){
								if(request_categoria.readyState == 4){	
									var xmldoc = request_categoria.responseXML;
									var automovil = xmldoc.getElementsByTagName('automovil');
									var clasificacionAutomovil = automovil.item(0).childNodes.item(9).childNodes.item(0).data;
									var validarCategoria = false;
									for(var i=0;i<categoriasValidas.length;i++){
										if(categoriasValidas[i]==clasificacionAutomovil){
											validarCategoria = true;
											break;
										}
									}
									
									if(validarCategoria){
										document.getElementById("campoErroresPromocionBloque").style.display='none';
										//CONTINUAR CON EL PROCESO
										document.getElementById("contenedorEstadoPromociones").style.display='inline';
										
										for(var i=0;i<document.getElementsByName("servicios[]").length;i++){
											
											if((isNaN(parseInt(document.getElementsByName("servicios[]").item(i).value))||parseInt(document.getElementsByName("servicios[]").item(i).value)<1)&&document.getElementsByName("idServicios[]").item(i).value==valorPromocion){
												
												document.getElementsByName("servicios[]").item(i).value=1;
												desplegarDetalleReservacion(document.getElementById("idAutomovil").value);
												break;
											}
										}
										
										//DESDE AQUI
										var tablaDetallePromociones = document.createElement("table");
										tablaDetallePromociones.style.width='100%';
										tablaDetallePromociones.style.fontSize='11px';
										tablaDetallePromociones.style.lineHeight='15px';
										var cuerpoTablaDetallePromociones = document.createElement("tbody");
										var filaDetallePromociones = document.createElement("tr");
										
										var celdaCantidadPromocion = document.createElement("td");
										celdaCantidadPromocion.style.width='10px';
										celdaCantidadPromocion.style.textAlign='right';
										var celdaDescripcionPromocion = document.createElement("td");
										var celdaSimboloPromocion = document.createElement("td");
										var celdaImportePromocion = document.createElement("td");
										
										
										celdaCantidadPromocion.appendChild(document.createTextNode(cantidadPromocionesAplicar));
										celdaDescripcionPromocion.appendChild(document.createTextNode(titulo.toUpperCase()));
										
										celdaDescripcionPromocion.style.width='240px';
										celdaSimboloPromocion.appendChild(document.createTextNode("$"));
										var precioServicio = promocion.item(0).childNodes.item(15).childNodes.item(0).data;
										celdaImportePromocion.appendChild(document.createTextNode("-"+(parseFloat(precioDolar*precioServicio*cantidadPromocionesAplicar).toFixed(2))));
										
										if(document.getElementById("cantidadPromociones")){
											document.getElementById("cantidadPromociones").value=cantidadPromocionesAplicar;
											document.getElementById("descripcionPromocion").value=titulo.toUpperCase();
											document.getElementById("montoPromocion").value=precioServicio*cantidadPromocionesAplicar*precioDolar;
										}
										
										celdaImportePromocion.style.textAlign='right';
										
										filaDetallePromociones.appendChild(celdaCantidadPromocion);
										filaDetallePromociones.appendChild(celdaDescripcionPromocion);
										filaDetallePromociones.appendChild(celdaSimboloPromocion);
										filaDetallePromociones.appendChild(celdaImportePromocion);
										
										cuerpoTablaDetallePromociones.appendChild(filaDetallePromociones);
										
										
										tablaDetallePromociones.appendChild(cuerpoTablaDetallePromociones);
										document.getElementById("contenidoPromociones").appendChild(tablaDetallePromociones);
										//HASTA AQUI
										
										var nuevoTotal = acumuladoAutomovil+acumuladoServicios+acumuladoSeguros;
										nuevoTotal = nuevoTotal-(precioDolar*precioServicio*cantidadPromocionesAplicar);
										
										if(parseInt(document.getElementById("pickupLocationIsAirport").value)==1){
											document.getElementById("celdaImpuestoAeropuerto").removeChild(document.getElementById("celdaImpuestoAeropuerto").firstChild);
											document.getElementById("celdaImpuestoAeropuerto").appendChild(document.createTextNode((nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100)).toFixed(2)));

											if(document.getElementById("impuestoAeropuerto")){
												document.getElementById("impuestoAeropuerto").value=(nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100));
											}
											nuevoTotal = nuevoTotal*(1+(parseFloat(document.getElementById("airportFee").value)/100));
										}
										
										
										document.getElementById("celdaImpuesto").removeChild(document.getElementById("celdaImpuesto").firstChild);
										document.getElementById("celdaImpuesto").appendChild(document.createTextNode((nuevoTotal*factorImpuesto).toFixed(2)));
										if(document.getElementById("impuesto")){
											document.getElementById("impuesto").value=nuevoTotal*factorImpuesto;
										}
										nuevoTotal = nuevoTotal * (1+factorImpuesto);
										
										document.getElementById("campoTotalReservacion").removeChild(document.getElementById("campoTotalReservacion").firstChild);
										if(precioDolar > 1.00){
											var denominacion = " MXN";
										} else {
											var denominacion = " USD";
										}
										document.getElementById("campoTotalReservacion").appendChild(document.createTextNode("$ "+(nuevoTotal.toFixed(2))+denominacion));
										if(document.getElementById("totalReservacion"))
											document.getElementById("totalReservacion").value=nuevoTotal.toFixed(2);
									}
									else{
										var lineaError = document.createElement("li");
										lineaError.appendChild(document.createTextNode("SELECTED CAR CATEGORY DOES NOT APPLY"));
										document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
									
								}
								
							}
							request_categoria.open('GET', 'http://www.alamomexico.com.mx/PHP/Automoviles/ConsultarAutomovil.php?idAutomovil='+document.getElementById("idAutomovil").value, true);
							request_categoria.send(null);
						}
						else{
							
							var lineaError = document.createElement("li");
							lineaError.appendChild(document.createTextNode("SELECTED LOCATION DOES NOT APPLY"));
							document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
					}
					else{
						alert('los dias validos para la promocion no corresponden a los de la reservacion');	
					}
				}
			}
		}
	}
	request_promocion.open('GET', 'http://www.alamomexico.com.mx/PHP/Promociones/ConsultarPromocion.php?idPromocion='+document.getElementById("idPromocion").value, true);
	request_promocion.send(null);
}

function promocionSeguroGratis(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto){
	while(document.getElementById("campoErroresPromocion").hasChildNodes()){
		document.getElementById("campoErroresPromocion").removeChild(document.getElementById("campoErroresPromocion").firstChild);
	}
	var request_promocion = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		request_promocion = new XMLHttpRequest();
		if (request_promocion.overrideMimeType) {
			request_promocion.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			request_promocion = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request_promocion = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!request_promocion) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}	
	
	request_promocion.onreadystatechange = function(){
		if(request_promocion.readyState == 4){	
			var xmldoc = request_promocion.responseXML;
			var promocion = xmldoc.getElementsByTagName('promocion');
			
			var id = promocion.item(0).childNodes.item(0).childNodes.item(0).data;
			var tipo = promocion.item(0).childNodes.item(1).childNodes.item(0).data;
			var titulo = promocion.item(0).childNodes.item(2).childNodes.item(0).data;
			var descripcion = promocion.item(0).childNodes.item(3).childNodes.item(0).data;
			var fInicioValidez = promocion.item(0).childNodes.item(4).childNodes.item(0).data;
			var categoriasValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('categoria').length;i++){
				categoriasValidas.push(promocion.item(0).getElementsByTagName('categoria').item(i).childNodes.item(0).data);	
			}
			var diasValidos = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('dia').length;i++){
				diasValidos.push(promocion.item(0).getElementsByTagName('dia').item(i).childNodes.item(0).data);
			}
			var ubicacionesValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('ubicacion').length;i++){
				ubicacionesValidas.push(promocion.item(0).getElementsByTagName('ubicacion').item(i).childNodes.item(0).data);
			}
			var fFinValidez;
			if(promocion.item(0).childNodes.item(5).childNodes.item(0)){
				fFinValidez = promocion.item(0).childNodes.item(5).childNodes.item(0).data;
			}
			var diasMinimos = promocion.item(0).childNodes.item(6).childNodes.item(0).data;
			var diasMaximos;
			if(promocion.item(0).childNodes.item(7).childNodes.item(0)){
				promocion.item(0).childNodes.item(7).childNodes.item(0).data;
			}
			var codigoPromocion = promocion.item(0).childNodes.item(8).childNodes.item(0).data;
			var valorPromocion = promocion.item(0).childNodes.item(10).childNodes.item(0).data;
			var aplicable = promocion.item(0).childNodes.item(14).childNodes.item(0).data;
			
			if(diasMinimos!=null && diasRenta < diasMinimos){
				var lineaError = document.createElement("li");
				lineaError.appendChild(document.createTextNode("MINIMUM DAYS ARE NOT MET"));
				document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
			else{
				if(diasMaximos!=null && diasRenta > diasMaximos){
					var lineaError = document.createElement("li");
					lineaError.appendChild(document.createTextNode("MAXIMUM DAYS ARE EXCEEDED"));
					document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
				else{
					var validarDias = true;
					if(diasRenta>6){
						validarDias = true;	
					}
					else{
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						if(aplicable==0){
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[x]==rangoDiasRenta[y]){
										validarDias = true;	
									}
								}
							}
						}
						else{
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[z]==rangoDiasRenta[y]){
										encontrado = true;
									}
								}
								if(!encontrado){
									validarDias=false;	
								}
							}
						}
					}
					if(validarDias){
						
						//CONTINUAR CON EL PROCESO
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								if(z>7)
									z=1;
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						var arregloIdentificar = new Array();
						
						var tamArreglo = rangoDiasRenta.length;
						
						arregloIdentificar[0]=0;
						for(var b=0;b<tamArreglo;b++){
							var valor = rangoDiasRenta.pop();
							var existe = false;
							for(var c=0;c<diasValidos.length;c++){
								if(valor==diasValidos[c]){
									if(arregloIdentificar[valor])
										arregloIdentificar[valor]++;
									else
										arregloIdentificar[valor]=1;
									existe = true;
								}
							}
							if(!existe){
								arregloIdentificar[valor]=0;
							}
						}
						
						
						
						var arregloComparar = new Array();
						var iArregloComparar = 0;
						for(var c=0;c<arregloIdentificar.length;c++){
							if(arregloIdentificar[c]){
								arregloComparar[iArregloComparar++]=arregloIdentificar[c];
							}	
						}
						
						var cantidadPromocionesAplicar;
						
						if(aplicable==0){
							cantidadPromocionesAplicar = 0;
							for(var b=0;b<arregloIdentificar.length;b++){
								if(arregloIdentificar[b]){
									cantidadPromocionesAplicar += arregloIdentificar[b];
								}
							}
						}
						else{
							cantidadPromocionesAplicar = arregloComparar.min();
						}
						
						var validarUbicacion = false;
						for(var x=0;x<ubicacionesValidas.length;x++){
							if(ubicacionesValidas[x]==idUbicacionRenta){
								validarUbicacion = true;	
							}
						}
						
						if(validarUbicacion){							
							
							var request_categoria = false;
							if (window.XMLHttpRequest) { // Mozilla, Safari,...
								request_categoria = new XMLHttpRequest();
								if (request_categoria.overrideMimeType) {
									request_categoria.overrideMimeType('text/xml');
								}
							} else if (window.ActiveXObject) { // IE
								try {
									request_categoria = new ActiveXObject("Msxml2.XMLHTTP");
								} catch (e) {
									try {
										request_categoria = new ActiveXObject("Microsoft.XMLHTTP");
									} catch (e) {}
								}
							}
							if (!request_categoria) {
								alert('Cannot create XMLHTTP instance');
								return false;
							}
							request_categoria.onreadystatechange = function(){
								if(request_categoria.readyState == 4){	
									var xmldoc = request_categoria.responseXML;
									var automovil = xmldoc.getElementsByTagName('automovil');
									var clasificacionAutomovil = automovil.item(0).childNodes.item(9).childNodes.item(0).data;
									var validarCategoria = false;
									for(var i=0;i<categoriasValidas.length;i++){
										if(categoriasValidas[i]==clasificacionAutomovil){
											validarCategoria = true;
											break;
										}
									}
									
									if(validarCategoria){
										document.getElementById("campoErroresPromocionBloque").style.display='none';
										//CONTINUAR CON EL PROCESO
										document.getElementById("contenedorEstadoPromociones").style.display='inline';
										
										for(var i=0;i<document.getElementsByName("seguro").length;i++){
											if(!document.getElementsByName("seguro").item(i).checked&&document.getElementsByName("seguro").item(i).value==valorPromocion){
												document.getElementsByName("seguro").item(i).checked=true;
												desplegarDetalleReservacion(document.getElementById("idAutomovil").value);
												break;
											}
										}
										
										//DESDE AQUI
										var tablaDetallePromociones = document.createElement("table");
										tablaDetallePromociones.style.width='100%';
										tablaDetallePromociones.style.fontSize='11px';
										tablaDetallePromociones.style.lineHeight='15px';
										var cuerpoTablaDetallePromociones = document.createElement("tbody");
										var filaDetallePromociones = document.createElement("tr");
										
										var celdaCantidadPromocion = document.createElement("td");
										celdaCantidadPromocion.style.width='10px';
										celdaCantidadPromocion.style.textAlign='right';
										var celdaDescripcionPromocion = document.createElement("td");
										var celdaSimboloPromocion = document.createElement("td");
										var celdaImportePromocion = document.createElement("td");
										
										celdaCantidadPromocion.appendChild(document.createTextNode(cantidadPromocionesAplicar));
										celdaDescripcionPromocion.appendChild(document.createTextNode(titulo.toUpperCase()));
										
										celdaDescripcionPromocion.style.width='240px';
										celdaSimboloPromocion.appendChild(document.createTextNode("$"));
										var precioSeguro = promocion.item(0).childNodes.item(15).childNodes.item(0).data;
										celdaImportePromocion.appendChild(document.createTextNode("-"+(parseFloat(precioDolar*precioSeguro*cantidadPromocionesAplicar).toFixed(2))));
										
										if(document.getElementById("cantidadPromociones")){
											document.getElementById("cantidadPromociones").value=cantidadPromocionesAplicar;
											document.getElementById("descripcionPromocion").value=titulo.toUpperCase();
											document.getElementById("montoPromocion").value=precioSeguro*cantidadPromocionesAplicar*precioDolar;
										}
										
										celdaImportePromocion.style.textAlign='right';
										
										filaDetallePromociones.appendChild(celdaCantidadPromocion);
										filaDetallePromociones.appendChild(celdaDescripcionPromocion);
										filaDetallePromociones.appendChild(celdaSimboloPromocion);
										filaDetallePromociones.appendChild(celdaImportePromocion);
										
										cuerpoTablaDetallePromociones.appendChild(filaDetallePromociones);
										
										
										tablaDetallePromociones.appendChild(cuerpoTablaDetallePromociones);
										document.getElementById("contenidoPromociones").appendChild(tablaDetallePromociones);
										//HASTA AQUI
										
										
										var nuevoTotal = acumuladoAutomovil+acumuladoServicios+acumuladoSeguros;
										nuevoTotal = nuevoTotal-(precioSeguro*cantidadPromocionesAplicar);
										
										if(parseInt(document.getElementById("pickupLocationIsAirport").value)==1){
											document.getElementById("celdaImpuestoAeropuerto").removeChild(document.getElementById("celdaImpuestoAeropuerto").firstChild);
											document.getElementById("celdaImpuestoAeropuerto").appendChild(document.createTextNode((nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100)).toFixed(2)));

											if(document.getElementById("impuestoAeropuerto")){
												document.getElementById("impuestoAeropuerto").value=(nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100));
											}
											nuevoTotal = nuevoTotal*(1+(parseFloat(document.getElementById("airportFee").value)/100));
										}
										
										
										document.getElementById("celdaImpuesto").removeChild(document.getElementById("celdaImpuesto").firstChild);
										document.getElementById("celdaImpuesto").appendChild(document.createTextNode((nuevoTotal*factorImpuesto).toFixed(2)));
										if(document.getElementById("impuesto")){
											document.getElementById("impuesto").value=nuevoTotal*factorImpuesto;
										}
										nuevoTotal = nuevoTotal * (1+factorImpuesto);
										
										document.getElementById("campoTotalReservacion").removeChild(document.getElementById("campoTotalReservacion").firstChild);
										if(precioDolar > 1.00){
											var denominacion = " MXN";
										} else {
											var denominacion = " USD";
										}
										document.getElementById("campoTotalReservacion").appendChild(document.createTextNode("$ "+(nuevoTotal.toFixed(2))+denominacion));
										if(document.getElementById("totalReservacion"))
											document.getElementById("totalReservacion").value=nuevoTotal.toFixed(2);
									}
									else{
										var lineaError = document.createElement("li");
										lineaError.appendChild(document.createTextNode("SELECTED CAR CATEGORY DOES NOT APPLY"));
										document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
									
								}
								
							}
							request_categoria.open('GET', 'http://www.alamomexico.com.mx/PHP/Automoviles/ConsultarAutomovil.php?idAutomovil='+document.getElementById("idAutomovil").value, true);
							request_categoria.send(null);
						}
						else{
							var lineaError = document.createElement("li");
							lineaError.appendChild(document.createTextNode("SELECTED LOCATION DOES NOT APPLY"));
							document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
					}
					else{
						alert('los dias validos para la promocion no corresponden a los de la reservacion');	
					}
				}
			}
		}
	}
	request_promocion.open('GET', 'http://www.alamomexico.com.mx/PHP/Promociones/ConsultarPromocion.php?idPromocion='+document.getElementById("idPromocion").value, true);
	request_promocion.send(null);
}

function descuentoTarifaRentaNuevaTarifaPorcentaje(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto){
	while(document.getElementById("campoErroresPromocion").hasChildNodes()){
		document.getElementById("campoErroresPromocion").removeChild(document.getElementById("campoErroresPromocion").firstChild);
	}
	var request_promocion = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		request_promocion = new XMLHttpRequest();
		if (request_promocion.overrideMimeType) {
			request_promocion.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			request_promocion = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request_promocion = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!request_promocion) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	request_promocion.onreadystatechange = function(){
		if(request_promocion.readyState == 4){	
			var xmldoc = request_promocion.responseXML;
			var promocion = xmldoc.getElementsByTagName('promocion');
			
			var id = promocion.item(0).childNodes.item(0).childNodes.item(0).data;
			var tipo = promocion.item(0).childNodes.item(1).childNodes.item(0).data;
			var titulo = promocion.item(0).childNodes.item(2).childNodes.item(0).data;
			var descripcion = promocion.item(0).childNodes.item(3).childNodes.item(0).data;
			var fInicioValidez = promocion.item(0).childNodes.item(4).childNodes.item(0).data;
			var categoriasValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('categoria').length;i++){
				categoriasValidas.push(promocion.item(0).getElementsByTagName('categoria').item(i).childNodes.item(0).data);	
			}
			var diasValidos = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('dia').length;i++){
				diasValidos.push(promocion.item(0).getElementsByTagName('dia').item(i).childNodes.item(0).data);
			}
			var ubicacionesValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('ubicacion').length;i++){
				ubicacionesValidas.push(promocion.item(0).getElementsByTagName('ubicacion').item(i).childNodes.item(0).data);
			}
			var fFinValidez;
			if(promocion.item(0).childNodes.item(5).childNodes.item(0)){
				fFinValidez = promocion.item(0).childNodes.item(5).childNodes.item(0).data;
			}
			var diasMinimos = promocion.item(0).childNodes.item(6).childNodes.item(0).data;
			var diasMaximos;
			if(promocion.item(0).childNodes.item(7).childNodes.item(0)){
				promocion.item(0).childNodes.item(7).childNodes.item(0).data;
			}
			var codigoPromocion = promocion.item(0).childNodes.item(8).childNodes.item(0).data;
			var valorPromocion = promocion.item(0).childNodes.item(10).childNodes.item(0).data;
			var aplicable = promocion.item(0).childNodes.item(14).childNodes.item(0).data;
			
			if(diasMinimos!=null && diasRenta < diasMinimos){
				var lineaError = document.createElement("li");
				lineaError.appendChild(document.createTextNode("MINIMUM DAYS ARE NOT MET"));
				document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
			else{
				if(diasMaximos!=null && diasRenta > diasMaximos){
					var lineaError = document.createElement("li");
					lineaError.appendChild(document.createTextNode("MAXIMUM DAYS ARE EXCEEDED"));
					document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
				else{
					var validarDias = true;
					if(diasRenta>6){
						validarDias = true;	
					}
					else{
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						if(aplicable==0){
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[x]==rangoDiasRenta[y]){
										validarDias = true;	
									}
								}
							}
						}
						else{
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[z]==rangoDiasRenta[y]){
										encontrado = true;
									}
								}
								if(!encontrado){
									validarDias=false;	
								}
							}
						}
					}
					if(validarDias){
						
						//CONTINUAR CON EL PROCESO
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								if(z>7)
									z=1;
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						var arregloIdentificar = new Array();
						
						var tamArreglo = rangoDiasRenta.length;
						
						arregloIdentificar[0]=0;
						for(var b=0;b<tamArreglo;b++){
							var valor = rangoDiasRenta.pop();
							var existe = false;
							for(var c=0;c<diasValidos.length;c++){
								if(valor==diasValidos[c]){
									if(arregloIdentificar[valor])
										arregloIdentificar[valor]++;
									else
										arregloIdentificar[valor]=1;
									existe = true;
								}
							}
							if(!existe){
								arregloIdentificar[valor]=0;
							}
						}
						
						
						
						var arregloComparar = new Array();
						var iArregloComparar = 0;
						for(var c=0;c<arregloIdentificar.length;c++){
							if(arregloIdentificar[c]){
								arregloComparar[iArregloComparar++]=arregloIdentificar[c];
							}	
						}
						
						var cantidadPromocionesAplicar;
						
						if(aplicable==0){
							cantidadPromocionesAplicar = 0;
							for(var b=0;b<arregloIdentificar.length;b++){
								if(arregloIdentificar[b]){
									cantidadPromocionesAplicar += arregloIdentificar[b];
								}
							}
						}
						else{
							cantidadPromocionesAplicar = arregloComparar.min();
						}
						
						var validarUbicacion = false;
						for(var x=0;x<ubicacionesValidas.length;x++){
							if(ubicacionesValidas[x]==idUbicacionRenta){
								validarUbicacion = true;	
							}
						}
						
						if(validarUbicacion){							
							
							var request_categoria = false;
							if (window.XMLHttpRequest) { // Mozilla, Safari,...
								request_categoria = new XMLHttpRequest();
								if (request_categoria.overrideMimeType) {
									request_categoria.overrideMimeType('text/xml');
								}
							} else if (window.ActiveXObject) { // IE
								try {
									request_categoria = new ActiveXObject("Msxml2.XMLHTTP");
								} catch (e) {
									try {
										request_categoria = new ActiveXObject("Microsoft.XMLHTTP");
									} catch (e) {}
								}
							}
							if (!request_categoria) {
								alert('Cannot create XMLHTTP instance');
								return false;
							}
							request_categoria.onreadystatechange = function(){
								if(request_categoria.readyState == 4){	
									var xmldoc = request_categoria.responseXML;
									var automovil = xmldoc.getElementsByTagName('automovil');
									var clasificacionAutomovil = automovil.item(0).childNodes.item(9).childNodes.item(0).data;
									var validarCategoria = false;
									for(var i=0;i<categoriasValidas.length;i++){
										if(categoriasValidas[i]==clasificacionAutomovil){
											validarCategoria = true;
											break;
										}
									}
									
									if(validarCategoria){
										document.getElementById("campoErroresPromocionBloque").style.display='none';
										//CONTINUAR CON EL PROCESO
										document.getElementById("contenedorEstadoPromociones").style.display='inline';
										
										//DESDE AQUI
										var tablaDetallePromociones = document.createElement("table");
										tablaDetallePromociones.style.width='100%';
										tablaDetallePromociones.style.fontSize='11px';
										tablaDetallePromociones.style.lineHeight='15px';
										var cuerpoTablaDetallePromociones = document.createElement("tbody");
										var filaDetallePromociones = document.createElement("tr");
										
										var celdaCantidadPromocion = document.createElement("td");
										celdaCantidadPromocion.style.width='10px';
										celdaCantidadPromocion.style.textAlign='right';
										var celdaDescripcionPromocion = document.createElement("td");
										var celdaSimboloPromocion = document.createElement("td");
										var celdaImportePromocion = document.createElement("td");
										
										celdaCantidadPromocion.appendChild(document.createTextNode(cantidadPromocionesAplicar));
										celdaDescripcionPromocion.appendChild(document.createTextNode(titulo.toUpperCase()));
										
										celdaDescripcionPromocion.style.width='240px';
										celdaSimboloPromocion.appendChild(document.createTextNode("$"));
										
										celdaImportePromocion.appendChild(document.createTextNode("-"+(((precioDia*(valorPromocion/100))*cantidadPromocionesAplicar).toFixed(2))));
										
										if(document.getElementById("cantidadPromociones")){
											document.getElementById("cantidadPromociones").value=cantidadPromocionesAplicar;
											document.getElementById("descripcionPromocion").value=titulo.toUpperCase();
											document.getElementById("montoPromocion").value=(precioDia*(valorPromocion/100))*cantidadPromocionesAplicar*precioDolar;
										}
										
										celdaImportePromocion.style.textAlign='right';
										
										filaDetallePromociones.appendChild(celdaCantidadPromocion);
										filaDetallePromociones.appendChild(celdaDescripcionPromocion);
										filaDetallePromociones.appendChild(celdaSimboloPromocion);
										filaDetallePromociones.appendChild(celdaImportePromocion);
										
										cuerpoTablaDetallePromociones.appendChild(filaDetallePromociones);
										
										
										tablaDetallePromociones.appendChild(cuerpoTablaDetallePromociones);
										document.getElementById("contenidoPromociones").appendChild(tablaDetallePromociones);
										//HASTA AQUI
										
										
										
										var nuevoTotal = acumuladoAutomovil+acumuladoServicios+acumuladoSeguros;
										
										
										nuevoTotal = nuevoTotal-((precioDia*(valorPromocion/100))*cantidadPromocionesAplicar);
										
										
										if(parseInt(document.getElementById("pickupLocationIsAirport").value)==1){
											document.getElementById("celdaImpuestoAeropuerto").removeChild(document.getElementById("celdaImpuestoAeropuerto").firstChild);
											document.getElementById("celdaImpuestoAeropuerto").appendChild(document.createTextNode((nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100)).toFixed(2)));

											if(document.getElementById("impuestoAeropuerto")){
												document.getElementById("impuestoAeropuerto").value=(nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100));
											}
											nuevoTotal = nuevoTotal*(1+(parseFloat(document.getElementById("airportFee").value)/100));
										}										
										
										document.getElementById("celdaImpuesto").removeChild(document.getElementById("celdaImpuesto").firstChild);
										document.getElementById("celdaImpuesto").appendChild(document.createTextNode((nuevoTotal*factorImpuesto).toFixed(2)));
										if(document.getElementById("impuesto")){
											document.getElementById("impuesto").value=nuevoTotal*factorImpuesto;
										}
										nuevoTotal = nuevoTotal * (1+factorImpuesto);
										
										
										
										document.getElementById("campoTotalReservacion").removeChild(document.getElementById("campoTotalReservacion").firstChild);
										if(precioDolar > 1.00){
											var denominacion = " MXN";
										} else {
											var denominacion = " USD";
										}
										document.getElementById("campoTotalReservacion").appendChild(document.createTextNode("$ "+(nuevoTotal.toFixed(2))+denominacion));
										if(document.getElementById("totalReservacion"))
											document.getElementById("totalReservacion").value=nuevoTotal.toFixed(2);
									}
									else{
										var lineaError = document.createElement("li");
										lineaError.appendChild(document.createTextNode("SELECTED CAR CATEGORY DOES NOT APPLY"));
										document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
									
								}
								
							}
							request_categoria.open('GET', 'http://www.alamomexico.com.mx/PHP/Automoviles/ConsultarAutomovil.php?idAutomovil='+document.getElementById("idAutomovil").value, true);
							request_categoria.send(null);
						}
						else{
							var lineaError = document.createElement("li");
							lineaError.appendChild(document.createTextNode("SELECTED LOCATION DOES NOT APPLY"));
							document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
					}
					else{
						alert('los dias validos para la promocion no corresponden a los de la reservacion');	
					}
				}
			}
		}
	}
	request_promocion.open('GET', 'http://www.alamomexico.com.mx/PHP/Promociones/ConsultarPromocion.php?idPromocion='+document.getElementById("idPromocion").value, true);
	request_promocion.send(null);	
}

function descuentoTarifaRentaNuevaTarifa(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto){
	while(document.getElementById("campoErroresPromocion").hasChildNodes()){
		document.getElementById("campoErroresPromocion").removeChild(document.getElementById("campoErroresPromocion").firstChild);
	}
	var request_promocion = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		request_promocion = new XMLHttpRequest();
		if (request_promocion.overrideMimeType) {
			request_promocion.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			request_promocion = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request_promocion = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!request_promocion) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	request_promocion.onreadystatechange = function(){
		if(request_promocion.readyState == 4){	
			var xmldoc = request_promocion.responseXML;
			var promocion = xmldoc.getElementsByTagName('promocion');
			
			var id = promocion.item(0).childNodes.item(0).childNodes.item(0).data;
			var tipo = promocion.item(0).childNodes.item(1).childNodes.item(0).data;
			var titulo = promocion.item(0).childNodes.item(2).childNodes.item(0).data;
			var descripcion = promocion.item(0).childNodes.item(3).childNodes.item(0).data;
			var fInicioValidez = promocion.item(0).childNodes.item(4).childNodes.item(0).data;
			var categoriasValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('categoria').length;i++){
				categoriasValidas.push(promocion.item(0).getElementsByTagName('categoria').item(i).childNodes.item(0).data);	
			}
			var diasValidos = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('dia').length;i++){
				diasValidos.push(promocion.item(0).getElementsByTagName('dia').item(i).childNodes.item(0).data);
			}
			var ubicacionesValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('ubicacion').length;i++){
				ubicacionesValidas.push(promocion.item(0).getElementsByTagName('ubicacion').item(i).childNodes.item(0).data);
			}
			var fFinValidez;
			if(promocion.item(0).childNodes.item(5).childNodes.item(0)){
				fFinValidez = promocion.item(0).childNodes.item(5).childNodes.item(0).data;
			}
			var diasMinimos = promocion.item(0).childNodes.item(6).childNodes.item(0).data;
			var diasMaximos;
			if(promocion.item(0).childNodes.item(7).childNodes.item(0)){
				promocion.item(0).childNodes.item(7).childNodes.item(0).data;
			}
			var codigoPromocion = promocion.item(0).childNodes.item(8).childNodes.item(0).data;
			var valorPromocion = promocion.item(0).childNodes.item(10).childNodes.item(0).data;
			var aplicable = promocion.item(0).childNodes.item(14).childNodes.item(0).data;
			
			if(diasMinimos!=null && diasRenta < diasMinimos){
				var lineaError = document.createElement("li");
				lineaError.appendChild(document.createTextNode("MINIMUM DAYS ARE NOT MET"));
				document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	document.getElementById("campoErroresPromocionBloque").style.display='inline';	
			}
			else{
				if(diasMaximos!=null && diasRenta > diasMaximos){
					var lineaError = document.createElement("li");
					lineaError.appendChild(document.createTextNode("MAXIMUM DAYS ARE EXCEEDED"));
					document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
				else{
					var validarDias = true;
					if(diasRenta>6){
						validarDias = true;	
					}
					else{
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						if(aplicable==0){
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[x]==rangoDiasRenta[y]){
										validarDias = true;	
									}
								}
							}
						}
						else{
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[z]==rangoDiasRenta[y]){
										encontrado = true;
									}
								}
								if(!encontrado){
									validarDias=false;	
								}
							}
						}
					}
					if(validarDias){
						
						//CONTINUAR CON EL PROCESO
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								if(z>7){
									z=1;
									
								}
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						var arregloIdentificar = new Array();
						
						var tamArreglo = rangoDiasRenta.length;
						
						arregloIdentificar[0]=0;
						for(var b=0;b<tamArreglo;b++){
							var valor = rangoDiasRenta.pop();
							var existe = false;
							for(var c=0;c<diasValidos.length;c++){
								if(valor==diasValidos[c]){
									if(arregloIdentificar[valor])
										arregloIdentificar[valor]++;
									else
										arregloIdentificar[valor]=1;
									existe = true;
								}
							}
							if(!existe){
								arregloIdentificar[valor]=0;
							}
						}
						
						
						
						var arregloComparar = new Array();
						var iArregloComparar = 0;
						for(var c=0;c<arregloIdentificar.length;c++){
							if(arregloIdentificar[c]){
								arregloComparar[iArregloComparar++]=arregloIdentificar[c];
							}	
						}
						
						var cantidadPromocionesAplicar;
						
						if(aplicable==0){
							cantidadPromocionesAplicar = 0;
							for(var b=0;b<arregloIdentificar.length;b++){
								if(arregloIdentificar[b]){
									cantidadPromocionesAplicar += arregloIdentificar[b];
								}
							}
						}
						else{
							cantidadPromocionesAplicar = arregloComparar.min();
						}
						
						var validarUbicacion = false;
						for(var x=0;x<ubicacionesValidas.length;x++){
							if(ubicacionesValidas[x]==idUbicacionRenta){
								validarUbicacion = true;	
							}
						}
						
						if(validarUbicacion){							
							
							var request_categoria = false;
							if (window.XMLHttpRequest) { // Mozilla, Safari,...
								request_categoria = new XMLHttpRequest();
								if (request_categoria.overrideMimeType) {
									request_categoria.overrideMimeType('text/xml');
								}
							} else if (window.ActiveXObject) { // IE
								try {
									request_categoria = new ActiveXObject("Msxml2.XMLHTTP");
								} catch (e) {
									try {
										request_categoria = new ActiveXObject("Microsoft.XMLHTTP");
									} catch (e) {}
								}
							}
							if (!request_categoria) {
								alert('Cannot create XMLHTTP instance');
								return false;
							}
							request_categoria.onreadystatechange = function(){
								if(request_categoria.readyState == 4){	
									var xmldoc = request_categoria.responseXML;
									var automovil = xmldoc.getElementsByTagName('automovil');
									var clasificacionAutomovil = automovil.item(0).childNodes.item(9).childNodes.item(0).data;
									var validarCategoria = false;
									for(var i=0;i<categoriasValidas.length;i++){
										if(categoriasValidas[i]==clasificacionAutomovil){
											validarCategoria = true;
											break;
										}
									}
									
									if(validarCategoria){
										document.getElementById("campoErroresPromocionBloque").style.display='none';
										//CONTINUAR CON EL PROCESO
										document.getElementById("contenedorEstadoPromociones").style.display='inline';
										
										//DESDE AQUI
										var tablaDetallePromociones = document.createElement("table");
										tablaDetallePromociones.style.width='100%';
										tablaDetallePromociones.style.fontSize='11px';
										tablaDetallePromociones.style.lineHeight='15px';
										var cuerpoTablaDetallePromociones = document.createElement("tbody");
										var filaDetallePromociones = document.createElement("tr");
										
										var celdaCantidadPromocion = document.createElement("td");
										celdaCantidadPromocion.style.width='10px';
										celdaCantidadPromocion.style.textAlign='right';
										var celdaDescripcionPromocion = document.createElement("td");
										var celdaSimboloPromocion = document.createElement("td");
										var celdaImportePromocion = document.createElement("td");
										
										celdaCantidadPromocion.appendChild(document.createTextNode(cantidadPromocionesAplicar));
										celdaDescripcionPromocion.appendChild(document.createTextNode(titulo.toUpperCase()));
										
										celdaDescripcionPromocion.style.width='240px';
										celdaSimboloPromocion.appendChild(document.createTextNode("$"));
										celdaImportePromocion.appendChild(document.createTextNode("-"+(((precioDolar*precioDia-valorPromocion)*cantidadPromocionesAplicar).toFixed(2))));
										
										if(document.getElementById("cantidadPromociones")){
											document.getElementById("cantidadPromociones").value=cantidadPromocionesAplicar;
											document.getElementById("descripcionPromocion").value=titulo.toUpperCase();
											document.getElementById("montoPromocion").value=(precioDia-valorPromocion)*cantidadPromocionesAplicar*precioDolar;
										}
										
										celdaImportePromocion.style.textAlign='right';
										
										filaDetallePromociones.appendChild(celdaCantidadPromocion);
										filaDetallePromociones.appendChild(celdaDescripcionPromocion);
										filaDetallePromociones.appendChild(celdaSimboloPromocion);
										filaDetallePromociones.appendChild(celdaImportePromocion);
										
										cuerpoTablaDetallePromociones.appendChild(filaDetallePromociones);
										
										
										tablaDetallePromociones.appendChild(cuerpoTablaDetallePromociones);
										document.getElementById("contenidoPromociones").appendChild(tablaDetallePromociones);
										//HASTA AQUI
										
										
										var nuevoTotal = acumuladoAutomovil+acumuladoServicios+acumuladoSeguros;
										nuevoTotal = nuevoTotal-((precioDolar*precioDia-valorPromocion)*cantidadPromocionesAplicar);
										
										if(parseInt(document.getElementById("pickupLocationIsAirport").value)==1){
											document.getElementById("celdaImpuestoAeropuerto").removeChild(document.getElementById("celdaImpuestoAeropuerto").firstChild);
											document.getElementById("celdaImpuestoAeropuerto").appendChild(document.createTextNode((nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100)).toFixed(2)));

											if(document.getElementById("impuestoAeropuerto")){
												document.getElementById("impuestoAeropuerto").value=(nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100));
											}
											nuevoTotal = nuevoTotal*(1+(parseFloat(document.getElementById("airportFee").value)/100));
										}
										
										
										document.getElementById("celdaImpuesto").removeChild(document.getElementById("celdaImpuesto").firstChild);
										document.getElementById("celdaImpuesto").appendChild(document.createTextNode((nuevoTotal*factorImpuesto).toFixed(2)));
										if(document.getElementById("impuesto")){
											document.getElementById("impuesto").value=nuevoTotal*factorImpuesto;
										}
										nuevoTotal = nuevoTotal * (1+factorImpuesto);
										
										
										document.getElementById("campoTotalReservacion").removeChild(document.getElementById("campoTotalReservacion").firstChild);
										if(precioDolar > 1.00){
											var denominacion = " MXN";
										} else {
											var denominacion = " USD";
										}
										document.getElementById("campoTotalReservacion").appendChild(document.createTextNode("$ "+(nuevoTotal.toFixed(2))+denominacion));
										if(document.getElementById("totalReservacion"))
											document.getElementById("totalReservacion").value=nuevoTotal.toFixed(2);
									}
									else{
										var lineaError = document.createElement("li");
										lineaError.appendChild(document.createTextNode("SELECTED CAR CATEGORY DOES NOT APPLY"));
										document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
									
								}
								
							}
							request_categoria.open('GET', 'http://www.alamomexico.com.mx/PHP/Automoviles/ConsultarAutomovil.php?idAutomovil='+document.getElementById("idAutomovil").value, true);
							request_categoria.send(null);
						}
						else{
							var lineaError = document.createElement("li");
							lineaError.appendChild(document.createTextNode("SELECTED LOCATION DOES NOT APPLY"));
							document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
					}
					else{
						alert('los dias validos para la promocion no corresponden a los de la reservacion');	
					}
				}
			}
		}
	}
	request_promocion.open('GET', 'http://www.alamomexico.com.mx/PHP/Promociones/ConsultarPromocion.php?idPromocion='+document.getElementById("idPromocion").value, true);
	request_promocion.send(null);	
}

function descuentoGeneral(diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto){
	while(document.getElementById("campoErroresPromocion").hasChildNodes()){
		document.getElementById("campoErroresPromocion").removeChild(document.getElementById("campoErroresPromocion").firstChild);
	}
	var request_promocion = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		request_promocion = new XMLHttpRequest();
		if (request_promocion.overrideMimeType) {
			request_promocion.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			request_promocion = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request_promocion = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!request_promocion) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	request_promocion.onreadystatechange = function(){
		if(request_promocion.readyState == 4){	
			var xmldoc = request_promocion.responseXML;
			var promocion = xmldoc.getElementsByTagName('promocion');
			
			var id = promocion.item(0).childNodes.item(0).childNodes.item(0).data;
			var tipo = promocion.item(0).childNodes.item(1).childNodes.item(0).data;
			var titulo = promocion.item(0).childNodes.item(2).childNodes.item(0).data;
			var descripcion = promocion.item(0).childNodes.item(3).childNodes.item(0).data;
			var fInicioValidez = promocion.item(0).childNodes.item(4).childNodes.item(0).data;
			var categoriasValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('categoria').length;i++){
				categoriasValidas.push(promocion.item(0).getElementsByTagName('categoria').item(i).childNodes.item(0).data);	
			}
			var diasValidos = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('dia').length;i++){
				diasValidos.push(promocion.item(0).getElementsByTagName('dia').item(i).childNodes.item(0).data);
			}
			var ubicacionesValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('ubicacion').length;i++){
				ubicacionesValidas.push(promocion.item(0).getElementsByTagName('ubicacion').item(i).childNodes.item(0).data);
			}
			var fFinValidez;
			if(promocion.item(0).childNodes.item(5).childNodes.item(0)){
				fFinValidez = promocion.item(0).childNodes.item(5).childNodes.item(0).data;
			}
			var diasMinimos = promocion.item(0).childNodes.item(6).childNodes.item(0).data;
			var diasMaximos;
			if(promocion.item(0).childNodes.item(7).childNodes.item(0)){
				promocion.item(0).childNodes.item(7).childNodes.item(0).data;
			}
			var codigoPromocion = promocion.item(0).childNodes.item(8).childNodes.item(0).data;
			var valorPromocion = promocion.item(0).childNodes.item(10).childNodes.item(0).data;
			
			var aplicable = promocion.item(0).childNodes.item(14).childNodes.item(0).data;
			
			if(diasMinimos!=null && diasRenta < diasMinimos){
				var lineaError = document.createElement("li");
				lineaError.appendChild(document.createTextNode("MINIMUM DAYS ARE NOT MET"));
				document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
			else{
				if(diasMaximos!=null && diasRenta > diasMaximos){
					var lineaError = document.createElement("li");
					lineaError.appendChild(document.createTextNode("MAXIMUM DAYS ARE EXCEEDED"));
					document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
				else{
					var validarDias = true;
					if(diasRenta>6){
						validarDias = true;	
					}
					else{
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						if(aplicable==0){
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[x]==rangoDiasRenta[y]){
										validarDias = true;	
									}
								}
							}
						}
						else{
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[z]==rangoDiasRenta[y]){
										encontrado = true;
									}
								}
								if(!encontrado){
									validarDias=false;	
								}
							}
						}
					}
					if(validarDias){
						
						//CONTINUAR CON EL PROCESO
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								if(z>7)
									z=1;
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						var arregloIdentificar = new Array();
						
						var tamArreglo = rangoDiasRenta.length;
						
						arregloIdentificar[0]=0;
						for(var b=0;b<tamArreglo;b++){
							var valor = rangoDiasRenta.pop();
							var existe = false;
							for(var c=0;c<diasValidos.length;c++){
								if(valor==diasValidos[c]){
									if(arregloIdentificar[valor])
										arregloIdentificar[valor]++;
									else
										arregloIdentificar[valor]=1;
									existe = true;
								}
							}
							if(!existe){
								arregloIdentificar[valor]=0;
							}
						}
						
						
						
						var arregloComparar = new Array();
						var iArregloComparar = 0;
						for(var c=0;c<arregloIdentificar.length;c++){
							if(arregloIdentificar[c]){
								arregloComparar[iArregloComparar++]=arregloIdentificar[c];
							}	
						}
						
						var cantidadPromocionesAplicar;
						
						if(aplicable==0){
							cantidadPromocionesAplicar = 0;
							for(var b=0;b<arregloIdentificar.length;b++){
								if(arregloIdentificar[b]){
									cantidadPromocionesAplicar += arregloIdentificar[b];
								}
							}
						}
						else{
							cantidadPromocionesAplicar = arregloComparar.min();
						}						
						var validarUbicacion = false;
						for(var x=0;x<ubicacionesValidas.length;x++){
							if(ubicacionesValidas[x]==idUbicacionRenta){
								validarUbicacion = true;	
							}
						}
						
						if(validarUbicacion){							
							
							var request_categoria = false;
							if (window.XMLHttpRequest) { // Mozilla, Safari,...
								request_categoria = new XMLHttpRequest();
								if (request_categoria.overrideMimeType) {
									request_categoria.overrideMimeType('text/xml');
								}
							} else if (window.ActiveXObject) { // IE
								try {
									request_categoria = new ActiveXObject("Msxml2.XMLHTTP");
								} catch (e) {
									try {
										request_categoria = new ActiveXObject("Microsoft.XMLHTTP");
									} catch (e) {}
								}
							}
							if (!request_categoria) {
								alert('Cannot create XMLHTTP instance');
								return false;
							}
							request_categoria.onreadystatechange = function(){
								if(request_categoria.readyState == 4){	
									var xmldoc = request_categoria.responseXML;
									var automovil = xmldoc.getElementsByTagName('automovil');
									var clasificacionAutomovil = automovil.item(0).childNodes.item(9).childNodes.item(0).data;
									var validarCategoria = false;
									for(var i=0;i<categoriasValidas.length;i++){
										if(categoriasValidas[i]==clasificacionAutomovil){
											validarCategoria = true;
											break;
										}
									}
									
									if(validarCategoria){
										document.getElementById("campoErroresPromocionBloque").style.display='none';
										//CONTINUAR CON EL PROCESO
										document.getElementById("contenedorEstadoPromociones").style.display='inline';
										
										//DESDE AQUI
										var tablaDetallePromociones = document.createElement("table");
										tablaDetallePromociones.style.width='100%';
										tablaDetallePromociones.style.fontSize='11px';
										tablaDetallePromociones.style.lineHeight='15px';
										var cuerpoTablaDetallePromociones = document.createElement("tbody");
										var filaDetallePromociones = document.createElement("tr");
										
										var celdaCantidadPromocion = document.createElement("td");
										celdaCantidadPromocion.style.width='10px';
										celdaCantidadPromocion.style.textAlign='right';
										var celdaDescripcionPromocion = document.createElement("td");
										var celdaSimboloPromocion = document.createElement("td");
										var celdaImportePromocion = document.createElement("td");
										
										celdaCantidadPromocion.appendChild(document.createTextNode(cantidadPromocionesAplicar));
										celdaDescripcionPromocion.appendChild(document.createTextNode(titulo.toUpperCase()));
										
										celdaDescripcionPromocion.style.width='240px';
										celdaSimboloPromocion.appendChild(document.createTextNode("$"));
										celdaImportePromocion.appendChild(document.createTextNode("-"+(parseFloat(precioDolar*valorPromocion*cantidadPromocionesAplicar).toFixed(2))));
										
										if(document.getElementById("cantidadPromociones")){
											document.getElementById("cantidadPromociones").value=cantidadPromocionesAplicar;
											document.getElementById("descripcionPromocion").value=titulo.toUpperCase();
											document.getElementById("montoPromocion").value=valorPromocion*cantidadPromocionesAplicar*precioDolar;
										}
										
										celdaImportePromocion.style.textAlign='right';
										
										filaDetallePromociones.appendChild(celdaCantidadPromocion);
										filaDetallePromociones.appendChild(celdaDescripcionPromocion);
										filaDetallePromociones.appendChild(celdaSimboloPromocion);
										filaDetallePromociones.appendChild(celdaImportePromocion);
										
										cuerpoTablaDetallePromociones.appendChild(filaDetallePromociones);
										
										
										tablaDetallePromociones.appendChild(cuerpoTablaDetallePromociones);
										document.getElementById("contenidoPromociones").appendChild(tablaDetallePromociones);
										//HASTA AQUI
										
										var nuevoTotal = acumuladoAutomovil+acumuladoServicios+acumuladoSeguros;
										nuevoTotal = nuevoTotal-(precioDolar*valorPromocion*cantidadPromocionesAplicar);
										
										if(parseInt(document.getElementById("pickupLocationIsAirport").value)==1){
											document.getElementById("celdaImpuestoAeropuerto").removeChild(document.getElementById("celdaImpuestoAeropuerto").firstChild);
											document.getElementById("celdaImpuestoAeropuerto").appendChild(document.createTextNode((nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100)).toFixed(2)));

											if(document.getElementById("impuestoAeropuerto")){
												document.getElementById("impuestoAeropuerto").value=(nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100));
											}
											nuevoTotal = nuevoTotal*(1+(parseFloat(document.getElementById("airportFee").value)/100));
										}
										
										
										document.getElementById("celdaImpuesto").removeChild(document.getElementById("celdaImpuesto").firstChild);
										document.getElementById("celdaImpuesto").appendChild(document.createTextNode((nuevoTotal*factorImpuesto).toFixed(2)));
										if(document.getElementById("impuesto")){
											document.getElementById("impuesto").value=nuevoTotal*factorImpuesto;
										}
										nuevoTotal = nuevoTotal * (1+factorImpuesto);
										
										document.getElementById("campoTotalReservacion").removeChild(document.getElementById("campoTotalReservacion").firstChild);
										if(precioDolar > 1.00){
											var denominacion = " MXN";
										} else {
											var denominacion = " USD";
										}
										document.getElementById("campoTotalReservacion").appendChild(document.createTextNode("$ "+(nuevoTotal.toFixed(2))+denominacion));
										if(document.getElementById("totalReservacion"))
											document.getElementById("totalReservacion").value=nuevoTotal.toFixed(2);
									}
									else{
										var lineaError = document.createElement("li");
										lineaError.appendChild(document.createTextNode("SELECTED CAR CATEGORY DOES NOT APPLY"));
										document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
									
								}
								
							}
							request_categoria.open('GET', 'http://www.alamomexico.com.mx/PHP/Automoviles/ConsultarAutomovil.php?idAutomovil='+document.getElementById("idAutomovil").value, true);
							request_categoria.send(null);
						}
						else{
							var lineaError = document.createElement("li");
							lineaError.appendChild(document.createTextNode("SELECTED LOCATION DOES NOT APPLY"));
							document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
					}
					else{
						alert('los dias validos para la promocion no corresponden a los de la reservacion');	
					}
				}
			}
		}
	}
	request_promocion.open('GET', 'http://www.alamomexico.com.mx/PHP/Promociones/ConsultarPromocion.php?idPromocion='+document.getElementById("idPromocion").value, true);
	request_promocion.send(null);
}

function descuentoEnDias(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto){
	while(document.getElementById("campoErroresPromocion").hasChildNodes()){
		document.getElementById("campoErroresPromocion").removeChild(document.getElementById("campoErroresPromocion").firstChild);
	}
	var request_promocion = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		request_promocion = new XMLHttpRequest();
		if (request_promocion.overrideMimeType) {
			request_promocion.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			request_promocion = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request_promocion = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!request_promocion) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	request_promocion.onreadystatechange = function(){
		if(request_promocion.readyState == 4){	
			var xmldoc = request_promocion.responseXML;
			var promocion = xmldoc.getElementsByTagName('promocion');
			
			var id = promocion.item(0).childNodes.item(0).childNodes.item(0).data;
			var tipo = promocion.item(0).childNodes.item(1).childNodes.item(0).data;
			var titulo = promocion.item(0).childNodes.item(2).childNodes.item(0).data;
			var descripcion = promocion.item(0).childNodes.item(3).childNodes.item(0).data;
			var fInicioValidez = promocion.item(0).childNodes.item(4).childNodes.item(0).data;
			var categoriasValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('categoria').length;i++){
				categoriasValidas.push(promocion.item(0).getElementsByTagName('categoria').item(i).childNodes.item(0).data);	
			}
			var diasValidos = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('dia').length;i++){
				diasValidos.push(promocion.item(0).getElementsByTagName('dia').item(i).childNodes.item(0).data);
			}
			var ubicacionesValidas = new Array();
			for(var i=0;i<promocion.item(0).getElementsByTagName('ubicacion').length;i++){
				ubicacionesValidas.push(promocion.item(0).getElementsByTagName('ubicacion').item(i).childNodes.item(0).data);
			}
			var fFinValidez;
			if(promocion.item(0).childNodes.item(5).childNodes.item(0)){
				fFinValidez = promocion.item(0).childNodes.item(5).childNodes.item(0).data;
			}
			var diasMinimos = promocion.item(0).childNodes.item(6).childNodes.item(0).data;
			var diasMaximos;
			if(promocion.item(0).childNodes.item(7).childNodes.item(0)){
				promocion.item(0).childNodes.item(7).childNodes.item(0).data;
			}
			var codigoPromocion = promocion.item(0).childNodes.item(8).childNodes.item(0).data;
			var valorPromocion = promocion.item(0).childNodes.item(10).childNodes.item(0).data;
			
			var aplicable = promocion.item(0).childNodes.item(14).childNodes.item(0).data;
			
			if(diasMinimos!=null && diasRenta < diasMinimos){
				var lineaError = document.createElement("li");
				lineaError.appendChild(document.createTextNode("MINIMUM DAYS ARE NOT MET"));
				document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
			else{
				if(diasMaximos!=null && diasRenta > diasMaximos){
					var lineaError = document.createElement("li");
					lineaError.appendChild(document.createTextNode("MAXIMUM DAYS ARE EXCEEDED"));
					document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
				else{
					var validarDias = true;
					if(diasRenta>6){
						validarDias = true;	
					}
					else{
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						if(aplicable==0){
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[x]==rangoDiasRenta[y]){
										validarDias = true;	
									}
								}
							}
						}
						else{
							for(var z=0;z<diasValidos.length;z++){
								var encontrado = false;
								for(var y=0;y<rangoDiasRenta.length;y++){
									if(diasValidos[z]==rangoDiasRenta[y]){
										encontrado = true;
									}
								}
								if(!encontrado){
									validarDias=false;	
								}
							}
						}
					}
					if(validarDias){
						
						//CONTINUAR CON EL PROCESO
						var rangoDiasRenta = new Array();
						var z=1;
						for(var j=0;j<diasRenta;j++){
							if((new Date(fechaRenta).getDay()+j)>7){
								if(z>7)
									z=1;
								rangoDiasRenta.push(z++);
							}
							else{
								rangoDiasRenta.push(new Date(fechaRenta).getDay()+j);
							}
						}
						
						var arregloIdentificar = new Array();
						
						var tamArreglo = rangoDiasRenta.length;
						
						arregloIdentificar[0]=0;
						for(var b=0;b<tamArreglo;b++){
							var valor = rangoDiasRenta.pop();
							var existe = false;
							for(var c=0;c<diasValidos.length;c++){
								if(valor==diasValidos[c]){
									if(arregloIdentificar[valor])
										arregloIdentificar[valor]++;
									else
										arregloIdentificar[valor]=1;
									existe = true;
								}
							}
							if(!existe){
								arregloIdentificar[valor]=0;
							}
						}
						
						
						
						var arregloComparar = new Array();
						var iArregloComparar = 0;
						for(var c=0;c<arregloIdentificar.length;c++){
							if(arregloIdentificar[c]){
								arregloComparar[iArregloComparar++]=arregloIdentificar[c];
							}	
						}
						
						var cantidadPromocionesAplicar;
						
						if(aplicable==0){
							cantidadPromocionesAplicar = 0;
							for(var b=0;b<arregloIdentificar.length;b++){
								if(arregloIdentificar[b]){
									cantidadPromocionesAplicar += arregloIdentificar[b];
								}
							}
						}
						else{
							cantidadPromocionesAplicar = arregloComparar.min();
						}
						
						var validarUbicacion = false;
						for(var x=0;x<ubicacionesValidas.length;x++){
							if(ubicacionesValidas[x]==idUbicacionRenta){
								validarUbicacion = true;	
							}
						}
						
						if(validarUbicacion){							
							
							var request_categoria = false;
							if (window.XMLHttpRequest) { // Mozilla, Safari,...
								request_categoria = new XMLHttpRequest();
								if (request_categoria.overrideMimeType) {
									request_categoria.overrideMimeType('text/xml');
								}
							} else if (window.ActiveXObject) { // IE
								try {
									request_categoria = new ActiveXObject("Msxml2.XMLHTTP");
								} catch (e) {
									try {
										request_categoria = new ActiveXObject("Microsoft.XMLHTTP");
									} catch (e) {}
								}
							}
							if (!request_categoria) {
								alert('Cannot create XMLHTTP instance');
								return false;
							}
							request_categoria.onreadystatechange = function(){
								if(request_categoria.readyState == 4){	
									var xmldoc = request_categoria.responseXML;
									var automovil = xmldoc.getElementsByTagName('automovil');
									var clasificacionAutomovil = automovil.item(0).childNodes.item(9).childNodes.item(0).data;
									var validarCategoria = false;
									for(var i=0;i<categoriasValidas.length;i++){
										if(categoriasValidas[i]==clasificacionAutomovil){
											validarCategoria = true;
											break;
										}
									}
									
									if(validarCategoria){
										//CONTINUAR CON EL PROCESO
										document.getElementById("campoErroresPromocionBloque").style.display='none';
										document.getElementById("contenedorEstadoPromociones").style.display='inline';
										
										//DESDE AQUI
										var tablaDetallePromociones = document.createElement("table");
										tablaDetallePromociones.style.width='100%';
										tablaDetallePromociones.style.fontSize='11px';
										tablaDetallePromociones.style.lineHeight='15px';
										var cuerpoTablaDetallePromociones = document.createElement("tbody");
										var filaDetallePromociones = document.createElement("tr");
										
										var celdaCantidadPromocion = document.createElement("td");
										celdaCantidadPromocion.style.width='10px';
										celdaCantidadPromocion.style.textAlign='right';
										var celdaDescripcionPromocion = document.createElement("td");
										var celdaSimboloPromocion = document.createElement("td");
										var celdaImportePromocion = document.createElement("td");
										
										celdaCantidadPromocion.appendChild(document.createTextNode(cantidadPromocionesAplicar));
										celdaDescripcionPromocion.appendChild(document.createTextNode(titulo.toUpperCase()));
										
										celdaDescripcionPromocion.style.width='240px';
										celdaSimboloPromocion.appendChild(document.createTextNode("$"));
										celdaImportePromocion.appendChild(document.createTextNode("-"+(parseFloat(precioDolar*precioDia*valorPromocion*cantidadPromocionesAplicar).toFixed(2))));
										
										if(document.getElementById("cantidadPromociones")){
											document.getElementById("cantidadPromociones").value=cantidadPromocionesAplicar;
											document.getElementById("descripcionPromocion").value=titulo.toUpperCase();
											document.getElementById("montoPromocion").value=precioDia*valorPromocion*cantidadPromocionesAplicar*precioDolar;
										}
										
										celdaImportePromocion.style.textAlign='right';
										
										filaDetallePromociones.appendChild(celdaCantidadPromocion);
										filaDetallePromociones.appendChild(celdaDescripcionPromocion);
										filaDetallePromociones.appendChild(celdaSimboloPromocion);
										filaDetallePromociones.appendChild(celdaImportePromocion);
										
										cuerpoTablaDetallePromociones.appendChild(filaDetallePromociones);
										
										
										tablaDetallePromociones.appendChild(cuerpoTablaDetallePromociones);
										document.getElementById("contenidoPromociones").appendChild(tablaDetallePromociones);
										//HASTA AQUI
										
										var nuevoTotal = acumuladoAutomovil+acumuladoServicios+acumuladoSeguros;
										nuevoTotal = nuevoTotal-(precioDolar*precioDia*valorPromocion*cantidadPromocionesAplicar);
										
										if(parseInt(document.getElementById("pickupLocationIsAirport").value)==1){
											document.getElementById("celdaImpuestoAeropuerto").removeChild(document.getElementById("celdaImpuestoAeropuerto").firstChild);
											document.getElementById("celdaImpuestoAeropuerto").appendChild(document.createTextNode((nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100)).toFixed(2)));

											if(document.getElementById("impuestoAeropuerto")){
												document.getElementById("impuestoAeropuerto").value=(nuevoTotal*(parseFloat(document.getElementById("airportFee").value)/100));
											}
											nuevoTotal = nuevoTotal*(1+(parseFloat(document.getElementById("airportFee").value)/100));
										}
										
										
										document.getElementById("celdaImpuesto").removeChild(document.getElementById("celdaImpuesto").firstChild);
										document.getElementById("celdaImpuesto").appendChild(document.createTextNode((nuevoTotal*factorImpuesto).toFixed(2)));
										if(document.getElementById("impuesto")){
											document.getElementById("impuesto").value=nuevoTotal*factorImpuesto;
										}
										nuevoTotal = nuevoTotal * (1+factorImpuesto);
										
										document.getElementById("campoTotalReservacion").removeChild(document.getElementById("campoTotalReservacion").firstChild);
										if(precioDolar > 1.00){
											var denominacion = " MXN";
										} else {
											var denominacion = " USD";
										}
										document.getElementById("campoTotalReservacion").appendChild(document.createTextNode("$ "+(nuevoTotal.toFixed(2))+denominacion));
										if(document.getElementById("totalReservacion"))
											document.getElementById("totalReservacion").value=(nuevoTotal).toFixed(2);
									}
									else{
										var lineaError = document.createElement("li");
										lineaError.appendChild(document.createTextNode("SELECTED CAR CATEGORY DOES NOT APPLY"));
										document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
									
								}
								
							}
							request_categoria.open('GET', 'http://www.alamomexico.com.mx/PHP/Automoviles/ConsultarAutomovil.php?idAutomovil='+document.getElementById("idAutomovil").value, true);
							request_categoria.send(null);
						}
						else{
							var lineaError = document.createElement("li");
							lineaError.appendChild(document.createTextNode("SELECTED LOCATION DOES NOT APPLY"));
							document.getElementById("campoErroresPromocion").appendChild(lineaError);
				document.getElementById("campoErroresPromocionBloque").style.display='inline';	}
					}
					else{
						alert('los dias validos para la promocion no corresponden a los de la reservacion');	
					}
				}
			}
		}
	}
	request_promocion.open('GET', 'http://www.alamomexico.com.mx/PHP/Promociones/ConsultarPromocion.php?idPromocion='+document.getElementById("idPromocion").value, true);
	request_promocion.send(null);
}

function aplicarPromocion(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto,dolar){
	
	precioDolar = dolar;
	
	if(document.getElementById("tipoPromocion")){
		if(document.getElementById("tipoPromocion").value==6){
			descuentoEnDias(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto);
		}
		if(document.getElementById("tipoPromocion").value==5){
			descuentoGeneral(diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto);
		}
		if(document.getElementById("tipoPromocion").value==1){
			descuentoTarifaRentaNuevaTarifa(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto);
		}
		if(document.getElementById("tipoPromocion").value==2){
			descuentoTarifaRentaNuevaTarifaPorcentaje(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto);
		}
		if(document.getElementById("tipoPromocion").value==4){
			promocionSeguroGratis(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto);
		}
		if(document.getElementById("tipoPromocion").value==3){
			promocionServicioGratis(precioDia, diasRenta, idUbicacionRenta, fechaRenta, fechaEntrega, total, acumuladoAutomovil, acumuladoServicios, acumuladoSeguros, factorImpuesto);
		}
	}
}

function cargarPromocion(idPromocion){
	//LEER LA PROMOCION
	var request_promocion = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		request_promocion = new XMLHttpRequest();
		if (request_promocion.overrideMimeType) {
			request_promocion.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			request_promocion = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request_promocion = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!request_promocion) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	request_promocion.onreadystatechange = function(){
		if(request_promocion.readyState == 4){	
			var xmldoc = request_promocion.responseXML;
			var promocion = xmldoc.getElementsByTagName('promocion');
			
			var id = promocion.item(0).childNodes.item(0).childNodes.item(0).data;
			var tipo = promocion.item(0).childNodes.item(1).childNodes.item(0).data;
			var titulo = promocion.item(0).childNodes.item(2).childNodes.item(0).data;
			var descripcion = promocion.item(0).childNodes.item(3).childNodes.item(0).data;
			var fInicioValidez = promocion.item(0).childNodes.item(4).childNodes.item(0).data;
			var fFinValidez;
			if(promocion.item(0).childNodes.item(5).childNodes.item(0)){
				fFinValidez = promocion.item(0).childNodes.item(5).childNodes.item(0).data;
			}
			var diasMinimos = promocion.item(0).childNodes.item(6).childNodes.item(0).data;
			var diasMaximos;
			if(promocion.item(0).childNodes.item(7).childNodes.item(0)){
				promocion.item(0).childNodes.item(7).childNodes.item(0).data;
			}
			var codigoPromocion = promocion.item(0).childNodes.item(8).childNodes.item(0).data;
			var valor = promocion.item(0).childNodes.item(10).childNodes.item(0).data;
			
			if(document.getElementById("codigoPromocion")){
				document.getElementById("codigoPromocion").value=codigoPromocion;
			}
			if(document.getElementById("idPromocion")){
				document.getElementById("idPromocion").value=id;
			}
			else{
				var campoEscondidoIdPromocion = document.createElement("input");
				campoEscondidoIdPromocion.id='idPromocion';
				campoEscondidoIdPromocion.name='idPromocion';
				campoEscondidoIdPromocion.type='hidden';
				campoEscondidoIdPromocion.value=id;
				document.getElementById("formulario").appendChild(campoEscondidoIdPromocion);
				var campoEscondidoTipoPromocion = document.createElement("input");
				campoEscondidoTipoPromocion.id='tipoPromocion';
				campoEscondidoTipoPromocion.name='tipoPromocion';
				campoEscondidoTipoPromocion.type='hidden';
				campoEscondidoTipoPromocion.value=tipo;
				document.getElementById("formulario").appendChild(campoEscondidoTipoPromocion);
			}
			if(validarFormulario()){
				document.formulario.submit();
			}
			else{
				document.getElementById("campoErrorReservacionPaso1").style.display='inline';
				viewMoreOptions();
			}
		}
	}
	request_promocion.open('GET', 'http://www.alamomexico.com.mx/PHP/Promociones/ConsultarPromocion.php?idPromocion='+idPromocion, true);
	request_promocion.send(null);
}