
// Marcar los días seleccionados
function setFecha(idOferta, fecha, fechaInicio, fechaFin) {
	
	document.getElementById("calendario").innerHTML = '<div class="cargandodatos"><img src="/img/es/' + sPortal + '/aplicaciones/cargandoDatos.gif" align="absmiddle" alt="" /> cargando datos...</div>';	

	// Registrar los días en el formulario
	document.forms[1].fechaInicio.value = fechaInicio;	
	document.forms[1].fechaFin.value = fechaFin;

	// Cargar el calendario		
	
	//setTimeout('limpiarcapa();', 3000);
	
	setTimeout('ajaxCal("/inc/aplicacionesPerso/ofertas/calendario.asp","?idOferta=' + idOferta + '&fecha=' + fecha + '&fechaInicio=' + fechaInicio + '&fechaFin=' + fechaFin + '","calendario","a_alertaCalendario", false)', 2000);	
	
	//ANTES
	//ajaxCal("/inc/aplicacionesPerso/ofertas/calendario.asp","?idOferta=" + idOferta + "&fecha=" + fecha + "&fechaInicio=" + fechaInicio + "&fechaFin=" + fechaFin,"calendario","a_alertaCalendario", false);
	
}

// Mostrar el resumen de los días seleccionados
function mostrarResumen() {
	idOferta = document.forms[1].oferta.value;
	fechaInicio = document.forms[1].fechaInicio.value;
	fechaFin = document.forms[1].fechaFin.value;
	
	ajaxCal("/inc/aplicacionesPerso/ofertas/resumen.asp","?idOferta=" + idOferta + "&fechaInicio=" + fechaInicio + "&fechaFin=" + fechaFin,"a_resumen","",false);
}

//---------------------------------------------------------------------
// Carga una página web mediante AJAX.
// Parámetros:
// - Url a cargar
// - Parámetros para la url
// - Capa para cargar el resultado (NO OBLIGATORIO)
// - Capa para cargar los errores (NO OBLIGATORIO)
// - Petición asincrona (true/false) (NO OBLIGATORIO, ASINCRONO POR DEFECTO) 
//		ATENCIÓN -- EN EL FIREFOX FUNCIONA DE FORMA ASINCRONA --
// - Objeto representando al formulario a validar (NO OBLIGATORIO)
//---------------------------------------------------------------------
function ajaxCal(){
	
	var sFichero	= arguments[0];
	var sParametros	= arguments[1];
	var sDiv		= arguments[2];
	var sDivError	= arguments[3];
	var bAsincrono	= arguments[4] ? arguments[4] : true;
	var oForm		= arguments[5];
	
	var peticion = false;
	if (window.XMLHttpRequest){
		peticion = new XMLHttpRequest();
	}else if (window.ActiveXObject) {
		peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}

	//prompt('',fichero+parametros);

	if(peticion) {
		if(!oForm){
	  		peticion.open("GET", sFichero+sParametros, bAsincrono);
	  	}else{
	  		peticion.open("POST", sFichero);
	  		peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		}
		if (sDivError!="") {devolverResultadoCal(peticion, sDiv, sDivError, oForm);}
	  	else if (sDiv!=""){devolverResultadoCal(peticion, sDiv, oForm);}
		if(!oForm){
			peticion.send(null);
		}else{
			peticion.send(sParametros); 
		}
	}
}

//---------------------------------------------------------------------
// Devuelve el resultado de la petición AJAX.
// Parámetros:
// - Objeto httpRequest
// - Nombre de la capa para escribir los resultados
// - Nombre de la capa para escribir los errores
// - Objeto representado al formulario a validar (NO OBLIGATORIO)
//		Submita el formulario si no hay errores de validación
//---------------------------------------------------------------------
function devolverResultadoCal(){
	
	var peticion	= arguments[0];
	var sDiv		= arguments[1];
	var sDivError	= arguments[2];
	var oFormulario	= arguments[3];
	
	var obj = document.getElementById(sDiv);

	if(sDivError!="" && document.getElementById(sDivError)) var objError = document.getElementById(sDivError);
	
	if(obj){
		peticion.onreadystatechange = function(){
			if (peticion.readyState == 4){
				
				// Submitar el formulario
				if (!peticion.responseText && oFormulario) oFormulario.submit();
				else {
					
					// Seleccionar en que capa se mostrará resultado / error
					if ( peticion.responseText.substr(0,sDiv.length) == sDiv ) {
						obj.innerHTML = peticion.responseText.substr(sDiv.length,peticion.responseText.length-sDiv.length);
						if(objError) objError.innerHTML = "";
						
						// Ejecutar eventos
						if (sDiv=="calendario") mostrarResumen();
						
					}
					else objError.innerHTML = peticion.responseText;
						
					if(oFormulario!="") {
						// Ocultar la capa de los errores de ASP
						if(document.getElementById("errorvalidacion") && obj.innerHTML=="") document.getElementById("errorvalidacion").style.display = "none";
		
						// Mostrar alertas 
						obj.style.display='block';
						if(oFormulario) location.hash = "a_alerta";
					}
				}
			}
		}
	}
}

function limpiarcapa() {
	
	document.getElementById("calendario").innerHTML = '';
	
}
