/****************************************lista macrozona***********************************************************************/
function listando_macrozona(div){
	//Div contenedor
	combopais=document.getElementById(div);
	
	select=document.createElement("select");
	select.className="inputs";
	select.setAttribute("id",'zonaplan')
	select['onchange']=new Function("lista_pais('listapais',this.value);");
	//select.setAttribute("disabled",'true');
	select.style.width="135px";
	combopais.appendChild(select);
	
	var conexion = conexionajax();
	conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/principal.php');
	conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	conexion.onreadystatechange = function() {
		if (conexion.readyState==4) {
			var texto = conexion.responseXML;
			/*Numero total de zonas*/
			zonas=texto.getElementsByTagName("numero")[0].firstChild.nodeValue;
			select.innerHTML="";
			//select.setAttribute("disabled",'false');
			//alert(paises);	
			option=document.createElement("option");
			option.setAttribute("value",'0');
			option.innerHTML="Seleccione una zona";
			select.appendChild(option);
			for( var i=0; i<zonas;i++){
				option=document.createElement("option");
				option.setAttribute("value",texto.getElementsByTagName("value")[i].firstChild.nodeValue);
				option.innerHTML=texto.getElementsByTagName("nombre")[i].firstChild.nodeValue;
				select.appendChild(option);
			}
		}else{
			option=document.createElement("option");
			option.setAttribute("value",'');
			option.innerHTML="Cargando";
			select.appendChild(option);
		}
	}
	//generando y creando cookie
	conexion.send("acciones=listar_macrozonas");
}

/****************************************lista país***********************************************************************/

function lista_pais(div,zona){
	//Div contenedor
	combopais=document.getElementById(div);
	combopais.innerHTML="";
	
	select=document.createElement("select");
	select.className="inputs";
	select.setAttribute("id",'paisplan');
	select['onchange']=new Function("lista_estado('listaestado',this.value);");
	//select.setAttribute("disabled",'true');
	select.style.width="135px";
	combopais.appendChild(select);
	
	var conexion = conexionajax();
	conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/principal.php');
	conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	conexion.onreadystatechange = function() {
		if (conexion.readyState==4) {
			var texto = conexion.responseXML;
			/*Numero total de zonas*/
			paises=texto.getElementsByTagName("numero")[0].firstChild.nodeValue;
			select.innerHTML="";
			//select.setAttribute("disabled",'false');
			//alert(paises);	
			option=document.createElement("option");
			option.setAttribute("value",'0');
			option.innerHTML="Seleccione una pa&iacute;s";
			select.appendChild(option);
			for( var i=0; i<paises;i++){
				option=document.createElement("option");
				option.setAttribute("value",texto.getElementsByTagName("value")[i].firstChild.nodeValue);
				option.innerHTML=texto.getElementsByTagName("nombre")[i].firstChild.nodeValue;
				select.appendChild(option);
			}
		}else{
			option=document.createElement("option");
			option.setAttribute("value",'');
			option.innerHTML="Cargando";
			select.appendChild(option);
		}
	}
	//generando y creando cookie
	conexion.send("acciones=listaplan_pais&macrozona="+zona);
}

/****************************************lista estados***********************************************************************/

function lista_estado(div,pais){
	//Div contenedor
	combopais=document.getElementById(div);
	combopais.innerHTML="";
	
	select=document.createElement("select");
	select.className="inputs";
	select.setAttribute("id",'estadoplan');
	//select.setAttribute("disabled",'true');
	select.style.width="135px";
	combopais.appendChild(select);
	
	var conexion = conexionajax();
	conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/principal.php');
	conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	conexion.onreadystatechange = function() {
		if (conexion.readyState==4) {
			var texto = conexion.responseXML;
			/*Numero total de estados*/
			estados=texto.getElementsByTagName("numero")[0].firstChild.nodeValue;
			select.innerHTML="";
			//select.setAttribute("disabled",'false');
			//alert(paises);	
			option=document.createElement("option");
			option.setAttribute("value",'0');
			option.innerHTML="Seleccione un estado";
			select.appendChild(option);
			for( var i=0; i<estados;i++){
				option=document.createElement("option");
				option.setAttribute("value",texto.getElementsByTagName("value")[i].firstChild.nodeValue);
				option.innerHTML=texto.getElementsByTagName("nombre")[i].firstChild.nodeValue;
				select.appendChild(option);
			}
		}else{
			option=document.createElement("option");
			option.setAttribute("value",'');
			option.innerHTML="Cargando";
			select.appendChild(option);
		}
	}
	//generando y creando cookie
	conexion.send("acciones=listar_estados&pais="+pais);
}

function fechacalculo(){
	var resultado = 0;
	if(document.getElementById("lst_diainicio").value!="" &&
	document.getElementById("lst_mesinicio").value!="" &&
	document.getElementById("lst_anioinicio").value!=""
	){
		var inicio = new Date (document.getElementById("lst_anioinicio").value,document.getElementById("lst_mesinicio").value-1,document.getElementById("lst_diainicio").value);
	}
	
	if(document.getElementById("lst_diafin").value!="" &&
	document.getElementById("lst_mesfin").value!="" &&
	document.getElementById("lst_aniofin").value!=""
	){
		var fin = new Date (document.getElementById("lst_aniofin").value,document.getElementById("lst_mesfin").value-1,document.getElementById("lst_diafin").value);
	}
	
	if(
	document.getElementById("lst_diainicio").value!="" &&
	document.getElementById("lst_mesinicio").value!="" &&
	document.getElementById("lst_anioinicio").value!="" &&
	document.getElementById("lst_diafin").value!="" &&
	document.getElementById("lst_mesfin").value!="" &&
	document.getElementById("lst_aniofin").value!=""
	){
		if(fin > inicio){
			resultado= daysBetween(inicio, fin);
		}else{
			document.getElementById("lst_diainicio").value="";
			document.getElementById("lst_mesinicio").value="";
			document.getElementById("lst_anioinicio").value="";
			document.getElementById("lst_diafin").value="";
			document.getElementById("lst_mesfin").value="";
			document.getElementById("lst_aniofin").value="";
			alert("La fecha de inicio debe ser menor a la fecha de fin");
		}
	}
	
	if(resultado!=0){
		document.getElementById("diasnoches").innerHTML=(resultado+1)+" d&iacute;as y "+resultado+" noches";	
		document.getElementById("Totaldias").style.display="block";
	}
}

function daysBetween (inicio, fin){
	var DSTadjust=0;
	//Constantes para ser utilizadas para los c�lculos siguientes
	oneMinute=1000*60;
	var oneDay = oneMinute * 60 * 24;
	//iguala las horas en caso de que los objetos la incluyan
	inicio.setHours(0);
	inicio.setMinutes(0);
	inicio.setSeconds(0);
	fin.setHours(0);
	fin.setMinutes(0);
	fin.setSeconds(0);
	// tenemos en cuenta los ajustes del horario de verano
	if(fin > inicio){
		DSTadjust =(fin.getTimezoneOffset() - inicio.getTimezoneOffset())* oneMinute;
	}else{
		DSTadjust =(inicio.getTimezoneOffset() - fin.getTimezoneOffset())* oneMinute;
	}
	
	var diff = Math.abs(fin.getTime() - inicio.getTime()) - DSTadjust;
	return Math.ceil (diff/oneDay);
}

/********************************buscador************************************************/
function buscar_plan(){
	listado=document.getElementById("atributos").getElementsByTagName('input').length;	
	atributos="";
	for(var y=0;y<listado;y++){
		if(document.getElementById("atributos").getElementsByTagName('input')[y].checked==true){
			atributos+=document.getElementById("atributos").getElementsByTagName('input')[y].value+";";
		}
	}
	if(document.getElementById("zonaplan").value!="0"){
		var conexion = conexionajax();
		conexion.open('POST', '..'+document.getElementById("carpeta_raiz").innerHTML+'includes/planificador.php');
		conexion.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		conexion.onreadystatechange = function() {
			if (conexion.readyState==4) {
				var texto = conexion.responseText;
				document.getElementById("cuerpo").innerHTML=texto;
			}else{
				document.getElementById("cuerpo").innerHTML=document.getElementById("cargando").innerHTML;	
			}
		}
		data="";
		data="acciones=buscar_plan";
		if(document.getElementById("zonaplan")){
			data+="&zonaplan="+document.getElementById("zonaplan").value;	
		}
		if(document.getElementById("paisplan")){
			data+="&paisplan="+document.getElementById("paisplan").value;
		}
		if(document.getElementById("estadoplan")){
			data+="&estadoplan="+document.getElementById("estadoplan").value;	
		}
		
		data+="&txt_palabra="+document.getElementById("txt_palabra").value;
		data+="&txt_palabra="+document.getElementById("txt_palabra").value;
		data+="&lst_diainicio="+document.getElementById("lst_diainicio").value;
		data+="&lst_mesinicio="+document.getElementById("lst_mesinicio").value;
		data+="&lst_anioinicio="+document.getElementById("lst_anioinicio").value;
		data+="&lst_diafin="+document.getElementById("lst_diafin").value;
		data+="&lst_aniofin="+document.getElementById("lst_aniofin").value;
		data+="&lst_capacidad="+document.getElementById("lst_capacidad").value;
		data+="&lst_divisa="+document.getElementById("lst_divisa").value;
		data+="&txt_precio="+document.getElementById("txt_precio").value;
		data+="&atributos="+atributos;
		conexion.send(data);
	}else{
		alert("debe ingresar por lo menos una zona para filtrar su búsqueda");
	}
}

function paginado_busqueda(pagina){
	paginas=parseInt(pagina);
	filas=document.getElementById("listado_busqueda").getElementsByTagName('tr').length;	
	numero=3;//Numero de registros visibles por vez
	inicio=paginas*numero;
	if(inicio<=1){
		inicio=1;
	}
	fin=inicio+numero;
	if(fin>=filas){
		fin=filas;
	}
	
	if(paginas<0){
		paginas=0;
	}
	if(paginas>document.getElementById('pgina_total').value){
		paginas=document.getElementById('pgina_total').value;
	}
	document.getElementById("pgina_act").value=paginas;
	//document.getElementById("pac"+paginas).style="font-size:13px";
	for(var y=1;y<filas;y++){
		if(y>=inicio && y<fin){
			document.getElementById("listado_busqueda").getElementsByTagName('tr')[y].style.display="table-row";
		}else{
			document.getElementById("listado_busqueda").getElementsByTagName('tr')[y].style.display="none";
		}
	}
}