// JavaScript Document
// <!--
		
       	if ( window.MooTools ) {
			var  opt_loading  = new Option( ' Loading Destiny.. ', false );
			var  opt_empty    = new Option( ' Empty List ', false );
			
			window.addEvent('domready', function( ) {	
			
				var origen = $('destinos_o');
					origen.addEvent('change', function( ev ) {
						// Clear the List and Set to Loading
						
						
						var xhr = new XHR({
							autoCancel: true,
							encoding:   'iso-8859-1',
							method:     'get',
							
							onRequest: function ( ) {
								clearOptions('destinos_d', null, opt_loading);
								$('destinos_d').addClass('disabled').disabled = true;
								

							},
							
							onSuccess:  function( text, xml ) {
								if ( !text ) {
									clearOptions(subcats, null, opt_empty);
				  				} else {
									var resultados = text.split('|');
									if(resultados.length>0){
										for(var j=0; j<=resultados.length-1 ; j++){
											valores = resultados[j].split('/');
											if(valores[1]!=null){
												$('destinos_d').options[j] = new Option( valores[1], valores[0] );
												$('destinos_d').removeClass('disabled').disabled = false;
												$('destinos_d').fireEvent('change').focus();
											}
										}
									}else{
										clearOptions('destinos_d', null, opt_empty);
									}
																
									$('destinos_d').removeClass('disabled').disabled = false;
									
								
							  	}
							}
						
						});
						
						xhr.send('ajax_data.php','clave=' + origen.getValue() );
					});
					
					var sencillo = $('s');
						sencillo.addEvent('click', function() {
								$('llegada').addClass('disabled');
								$('llegada').setStyle('display','none');
								$('llegadatr').setStyle('display','none');
								
															});
					var redondo = $('r');
						redondo.addEvent('click', function() {
							   $('llegada').removeClass('disabled');
							   $('llegada').setStyle('display','');
							   $('llegadatr').setStyle('display','');
							   
														   });
						
					myCal = new Calendar({ llegada: 'd/m/Y' }, {direction: 1});
					myCal2 = new Calendar({ salida: 'd/m/Y' }, {direction: 1, tweak: { x: 6, y: 0 }});
				
				
				btnBuscar = $('buscar');
					btnBuscar.addEvent('click', function() {
						
						
						if($('destinos_o').getValue()==''){
							alert('Debe Seleccionar un Origen');
							return;
						}	
						
						if($('destinos_d').getValue()=='-1'){
							alert('Debe Seleccionar un Destino');
							return;
						}				
						
						if($('r').getValue()=="r"){
							
							if($('salida').getValue()==''){
								alert('Aun no ah seleccionado la fecha de salida');	
								return;
							}
							
							if($('llegada').getValue()==''){
								alert('Aun no ah seleccionado la fecha de llegada');	
								return;
							}
							
							
							sal_arr = $('salida').getValue().split('/');
							var salida = new Date(sal_arr[2], (sal_arr[1]*1-1), sal_arr[0]);	
							
							reg_arr = $('llegada').getValue().split('/');
							var regreso  = new Date(reg_arr[2], (reg_arr[1]*1-1), reg_arr[0]);	
							
							diferencia = regreso-salida;
							diferencia = Math.ceil(diferencia/1000/60/60/24);
							if(diferencia < 0 ){
								alert('La fecha de Llegada debe ser mayor a la de salida');	
								return;
							}

						}
						
						pasajeros = parseInt($('adt').getValue()) + parseInt($('men').getValue()) + parseInt($('inf').getValue());
						if(pasajeros<=0){
							alert('Debe elegir por lo menos 1 pasajero');
							return;
						}
						if(parseInt($('inf').getValue())>0 && parseInt($('adt').getValue())<=0){
							alert('Los infantes no pueden viajar sin un adulto');
							return;
						}
						
						
						if($('s').getValue()=="s"){
							if($('salida').getValue()==''){
								alert('Debe definir fecha de Salida');
								return;
							}
						}
						var ajax = new XHR({
							autoCancel: true,
							encoding:   'iso-8859-1',
							method:     'get',
							
							onRequest: function ( ) {
								$('calendario').setStyle('display','none');
								$('luis').setStyle('display', 'none');
								$('cargando').setStyle('display', '');
								$('vuelos').innerHTML = '<a href="consulta.php"><img src="../images/compra.png" width="275" height="200" border="0" /></a>';
							},
							
							onSuccess:  function( text, xml ) {

								if ( !text ) {
									$('calendario').innerHTML="<b>No Encontrado</b>";
									$('cargando').setStyle('display', 'none');
				  				} else {
									$('calendario').setStyle('display','');
									$('cargando').setStyle('display', 'none');
									$('calendario').innerHTML=text;
								
							  	}
							}
						
						});
						if($('r').getValue()=="r"){
							vuelo = 'r';
						}else{
							vuelo = 's';	
						}
						
						fsalida = $('salida').getValue().split('/');
						salida = fsalida[2]+'-'+fsalida[1]+'-'+fsalida[0];
						
						fllegada = $('llegada').getValue().split('/');
						llegada = fllegada[2]+'-'+fllegada[1]+'-'+fllegada[0];
						
						cadena = 'codigo='+$('codigo').getValue()+'&origen='+$('destinos_o').getValue()+'&destino='+$('destinos_d').getValue()+'&fsalida='+salida+'&fllegada='+llegada+'&adt='+$('adt').getValue()+'&men='+$('men').getValue()+ '&inf='+$('inf').getValue()+ '&vuelo='+vuelo;
						ajax.send('paso1.php',cadena );
						
						
					  });
				
		
			
			
				
			
			
			});
		
		
		};
		
		
		
		
		function clearOptions( select, func_after, option ) {
				if ( !(select = $(select)) ) {
					alert('No se encontró el elemento a limpiar');
					if ( func_after )  func_after.attempt([ select, option ]);
					return  false;
				}
				select.options.length = 0;
				if ( option ) {
					select.options[0] = option;
				}
				if ( func_after ) {
					func_after.delay(1, select, [ select, option ]);
				}
				return  true;
			}; // bool clearOptions()
			
			
		function doCheck(opt){
			switch(opt){
				case 1:
					alert('Debe Seleccionar Vuelo de Regreso');
					break;
				case 2:
					window.location='svuelos.php';
					break;
				case 3:
					alert('La fecha de llegada debe ser mayor a la de salida');
					break;
			}
		}
		
		
		function mouseover(obj){
			if(document.getElementById(obj).className !=  'diaselected'){	
				document.getElementById(obj).className = 'diahover';
			}
			
		}
		
		function mouseout(id, obj){
			moredata = obj.split('_');
			for (var i=1; i <= 31; i++ )
				{		 
					j = i.toString();
					if(j.length<2 && i<10){
						j = '0'+j;	
					}

					if (document.getElementById(id + 'cdia' + j + '_' + moredata[1] + '_' + moredata[2] )) {	 		    
							
								if(document.getElementById(id + 'cdia' + j + '_' + moredata[1] + '_' + moredata[2] ).className !=  'diaselected')	{
									document.getElementById(id + 'cdia' + j + '_' + moredata[1] + '_' + moredata[2] ).className = 'dia';	
								}
							
					}
				}
			
			}
		
		function chkdia(Obj,id){
				moredata = Obj.split('_');
				document.getElementById(Obj).className = 'diaselected';		
				for (var i=1; i <= 31; i++ )
				{		
				
					j = i.toString();
					if(j.length<2 && i<10){
						j = '0'+j;	
					}
					if (document.getElementById(id + 'cdia' + j  + '_' + moredata[1] + '_' + moredata[2]) ) {	 		    
							if ((id + 'cdia' + j + '_' + moredata[1] + '_' + moredata[2] ) != Obj)
								document.getElementById(id + 'cdia' + j + '_' + moredata[1] + '_' + moredata[2]).className = 'dia';
					}
				}
				

				var ajax_showvuelos = new XHR({
							autoCancel: true,
							encoding:   'iso-8859-1',
							method:     'get',
							
							onRequest: function ( ) {
								$('vuelos').innerHTML=$('cargando').innerHTML;
								
							},
							
							onSuccess:  function( text, xml ) {

								if ( !text ) {
									$('vuelos').innerHTML="<b>No Encontrado</b>";
									
				  				} else {
									
									$('vuelos').innerHTML=text;
								
							  	}
							}
						});
						ajax_showvuelos.send('vuelos.php','sal='+Obj );


			
		}
		


// -->