// Feelgood - Funciones en general.

function ajaxRequestSend(url, params, method,id, fentrada, fsalida)
{
	var ajax;
	//document.getElementById("msgBuscando").style.display = "block";
	ajax = nuevoAjax();
	if (method == "GET")
	{

		ajax.open(method, url + '?' + params, true);

		ajax.onreadystatechange=function() 
		{
			if (ajax.readyState == 4) 
			{
				rta = ajax.responseText;
				//alert(rta);
				//return;
				if (rta != 'false')
				{	
					
					
					document.getElementById('precio_unitario_'+id).value =( rta );
					document.getElementById('cantidad_'+id).value =  1;
					document.getElementById('reserva_'+id).innerHTML =  (document.getElementById('precio_unitario_'+id).value) ;
					document.getElementById('precio_total_'+id).value =  (document.getElementById('precio_unitario_'+id).value);
calcular_precio_promedio(id,fentrada,fsalida);
				}
			}
		}
		ajax.send(null);
	}
	else
	{
		alert("Metodo "+ method +" no implementado.")
	}
}

function nuevoAjax()
{
	var xmlhttp = false;
 	try 
 	{
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} 
 	catch (e) 
 	{
 		try 
 		{
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} 
 		catch (E) 
 		{
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	{
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}


function mywinopen(pagina, ancho,alto)
{
	
	var izquierda = (screen.availWidth - ancho) / 2;
	var arriba = (screen.availHeight - alto) / 2;
	
	myWin=open(pagina,'Mapa','top='+arriba+',left='+izquierda+',width='+ancho+',height='+alto+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,titlebar=0');
}
/*** Funciones para calcular el precio de una reserva. **/

function calcularPrecio( cantidad , id )
{
	var precio,resp; 
	precio = document.getElementById('precio_unitario_'+id).value;
	precio=precio.replace(",",".");
	resp=Number(precio) * cantidad;
	resp = resp.toFixed(2);
	precio=resp.replace(".",",");
	document.getElementById('reserva_'+id).innerHTML = precio;
	document.getElementById('precio_total_'+id).value= precio;
}

function calcularPrecioPersonas( cantidad , id, fentrada, fsalida )
{
	document.getElementById('reserva_'+id).innerHTML = '<img src="/imagenes/loadinfo.net.gif" id="imgload" style="border:none; padding:0; margin:0;" />';
  	ajaxRequestSend('/modulos/catalogo/include/ajax.preciosxpersona.php', 'personas='+cantidad+'&tipohabitacion='+id+'&fentrada='+fentrada+'&fsalida='+fsalida, "GET",id,fentrada,fsalida);
}

/****************************/

function calcular_precio_promedio(id,fentrada,fsalida)
{
	var inicio, fin, dias;
	inicio = new Date(Number(fentrada.substr(0,4)),Number(fentrada.substr(5,2))-1,Number(fentrada.substr(8,2)));
	fin = new Date(Number(fsalida.substr(0,4)),Number(fsalida.substr(5,2))-1,Number(fsalida.substr(8,2)));
	dias=(fin-inicio)/(24*60*60*1000);
	var precio,resp; 
	resp=0;
	precio = document.getElementById('precio_total_'+id).value;
	precio=precio.replace(",",".");
	if(dias !=0)
		resp=Number(precio) / dias;
	resp = resp.toFixed(2);
	precio=resp.replace(".",",");
	document.getElementById('reserva2_'+id).innerHTML = precio;
}


function resetFechaEntrada(inicio)
{
	var hoy;
	
	var anio;
	
	if (inicio=="")
	{
		hoy=new Date();
	    
		anio=hoy.getYear();
		
		if (anio < 1000) anio+=1900;
		
		var mes=hoy.getMonth()+1;
		
		if (mes<10) mes="0"+mes;
		
		var dia=hoy.getDate();
		
		if (dia<10) dia="0"+dia;
	}
	else
	{
		dia=inicio.substring(8,10);
		mes=inicio.substring(5,7);
		anio=inicio.substring(0,4);
	}
	
	document.getElementById('dia_entrada').value = dia;
	document.getElementById('mes_entrada').value = anio+"-"+mes;
}
function resetFechaSalida(salida)
{
	var hoy;
	
	var anio;
	
	if (salida=="")
	{
		hoy=new Date();

		milisegundos=parseInt(1*24*60*60*1000);
 
 		tiempo=hoy.getTime();

		total=hoy.setTime(parseInt(tiempo+milisegundos));

		anio=hoy.getYear();
	
		if (anio < 1000) anio+=1900;
		
		var mes=hoy.getMonth()+1;
		
		if (mes<10) mes="0"+mes;
		
		var dia=hoy.getDate();
		
		if (dia<10) dia="0"+dia;
	}
	else
	{
		dia=salida.substring(8,10);
		mes=salida.substring(5,7);
		anio=salida.substring(0,4);
	}
	
	
	document.getElementById('dia_salida').value = dia;
	document.getElementById('mes_salida').value = anio+"-"+mes;
}

function verificarFechaEntrada()
{
	var hoy=new Date();
	var anio=hoy.getYear();
	if (anio < 1000) anio+=1900;
	var mes=hoy.getMonth()+1;
	if (mes<10) mes="0"+mes;
	var dia=hoy.getDate();
	if (dia<10) dia="0"+dia;
	
	var dia_entrada = document.getElementById('dia_entrada').value;

	var mes_anio = document.getElementById('mes_entrada').value.split('-');
	
	var mes_entrada = mes_anio[1];
	var anio_entrada = mes_anio[0];
		
	if( (Number(anio_entrada) < anio) )
	{
		return false;
	}
	else
	{
		if( (Number(mes_entrada) < mes) && (Number(anio_entrada) == anio) )
		{
			return false;
		}
		else
		{
			if( (Number(dia_entrada) < dia) && (Number(mes_entrada) == mes) && (Number(anio_entrada) == anio) )
			{
				return false;
			}
		}
	}
	return true;
}


function verificarFechaSalida()
{
	var hoy=new Date();
	var anio=hoy.getYear();
	if (anio < 1000) anio+=1900;
	var mes=hoy.getMonth()+1;
	if (mes<10) mes="0"+mes;
	var dia=hoy.getDate();
	if (dia<10) dia="0"+dia;
	
	var dia_salida = document.getElementById('dia_salida').value;
	var mes_anio = document.getElementById('mes_salida').value.split('-');
	var mes_salida = mes_anio[1];
	var anio_salida = mes_anio[0];
		
	if( (Number(anio_salida) < anio) )
	{
		return false;
	}
	else
	{
		if( (Number(mes_salida) < mes) && (Number(anio_salida) == anio) )
		{
			return false;
		}
		else
		{
			if( (Number(dia_salida) < dia) && (Number(mes_salida) == mes) && (Number(anio_salida) == anio) )
			{
				return false;
			}
		}
	}
	return true;
}

/*****************************/


/************** GALERIA DE IMAGENES *****************************/
function cambiarFoto(foto, url_softapart)
{
	document.getElementById('foto_media').src = url_softapart + '/upload/hcatalogo/medium/' + foto.alt;
	document.getElementById('enlace_foto_media').href = url_softapart + '/upload/hcatalogo/' + foto.alt;
}
