$(document).ready(function(){

	// Inicio de galeria de imagenes
	if(window.restauraBotones) restauraBotones();
	if(window.colocaImagenes) colocaImagenes();
	if(window.mueveImagenes) intval=window.setInterval("mueveImagenes(-160, 1000)",5000);


	// Carga inicial de paises, si no
	air = $("input[name='solo_aero']:checked").val(); 

	if (pickupDest=='' || pickupDest=='null')
	{
		if (pickupCountry){
			cargaSelector('pais',$('#Country'),pickupCountry);
			cargaSelector('destinos',$('#destinooculto'),pickupCountry, air);
			cargaSelector('destinos',$('#destinosdevolucion'),pickupCountry, air);
		} else {
			cargaSelector('paises',$('#Country'));
			yaCargado=false;
		}
	} else {
		cargaSelector('paisDestino',$('#Country'), pickupDest);
		cargaSelector('destino',$('#destinooculto'), pickupDest);
		cargaSelector('destino',$('#destinosdevolucion'), dropoffDest);
		yaCargado=false;
	}

	// Si se selecciona pais se carga y habilita todo
	$('#Country').change(function () {
		air = $("input[name='solo_aero']:checked").val(); 
		 if ($("#Country option:selected").val()=='reload')
		 {
			 cargaSelector('paises',$('#Country'));
		 } else {
			 idPais = $("#Country option:selected").val();
			 cargaSelector('destinos',$('#destinooculto'),idPais, air);
			 cargaSelector('destinos',$('#destinosdevolucion'),idPais, air);
			 $('#form_btn').attr("disabled",false);
			 yaCargado=true;
		 }
		 
     })

		// Si se cambia casilla aeropuertos

	$("input[@name='solo_aero']").change(function () {
		air = $("input[name='solo_aero']:checked").val(); 
		idPais = $("#Country option:selected").val();

		if (idPais!='null' && idPais!='reload')
		{
			cargaSelector('destinos',$('#destinooculto'),idPais, air);
			cargaSelector('destinos',$('#destinosdevolucion'),idPais, air);
			$('#form_btn').attr("disabled",false);
			yaCargado=true;
		}
		 
     })

	 // Si se cambia destino de recogida
	 $('#destinooculto').change(function () {
		air = $("input[name='solo_aero']:checked").val(); 
		if ($("#destinooculto option:selected").val()=='reload')
		{
			idPais = $("#Country option:selected").val();
			 cargaSelector('destinos',$('#destinooculto'),idPais, air);
			 cargaSelector('destinos',$('#destinosdevolucion'),idPais, air);
			 $('#form_btn').attr("disabled",false);
			 yaCargado=true;

		} else {
			selVal = $("#destinooculto option:selected").text();
			$("#destinosdevolucion option:contains("+selVal+")").attr("selected",true);
		}
	 })

		 // Si se cambia destino de devolucion a reload
	 $('#destinosdevolucion').change(function () {
		 air = $("input[name='solo_aero']:checked").val(); 
		if ($("#destinosdevolucion option:selected").val()=='reload')
		{
			idPais = $("#Country option:selected").val();
			 cargaSelector('destinos',$('#destinosdevolucion'),idPais, air);
			 $('#form_btn').attr("disabled",false);
			 yaCargado=true;

		} 
	 })
});


function cargaSelector(fuente,destino,cod, air){
	switch (fuente){
		case 'pais':
			action = 'getPais';
			relationCod = '&countryCod='+cod;
		break;

		case 'paises':
			action = 'getPaises';
			relationCod = '';
		break;

		case 'destinos':
			action = 'getDestinos';
			relationCod = '&parent='+cod+'&air='+air;
		break;

		case 'paisDestino':
			action = 'getPaisDestino';
			relationCod = '&locationCod='+cod;
		break;

		case 'destino':
			action = 'getDestino';
			relationCod = '&locationCod='+cod;
		break;
	}
	
	// Vacia receptor
	destino.attr('disabled',true);
	if (destino.attr('id')=='destinooculto' || destino.attr('id')=='destinosdevolucion')
		destino.append('<option selected>------ loading --------</option>');

	// Llamada AJAX
	//alert (dominio + "includes/motor-selector.php?action="+action+"&idioma="+idioma+relationCod);
	$.get(dominio + "includes/motor-selector.php?action="+action+"&idioma="+idioma+relationCod,{},function(xml){
		destino.empty();
		

		// Recorre documento XML y asigna valores
		$('option',xml).each(function(i) {
			name = $(this).find("name").text();
			value = $(this).find("value").text();
			selected = $(this).attr("selected");
			//alert (name+value); 
			optionHTML = themeOption(value, name, selected);
			destino.append(optionHTML);
			//alert (optionHTML);
		});

		destino.attr("disabled",false);
		
	});

}


function themeOption(value,name,selected){
	selTxt='';
	if (selected==1) selTxt = ' selected';
	retHTML = '<option value="' + value + '"' + selTxt + '>' + name + '</option>';
	return retHTML;
}

function reloadPickup(pVal){
	pVal = (pVal) ? 'on' : '';
	document.getElementById('cpickup').value = pVal;
	document.formu.submit();
}

function validateSrchFrm(){
	numError = 0;
	//alert ('valida');

	if (document.getElementById('destinooculto')){
		elm = document.getElementById('destinooculto');
		idx = document.getElementById('destinooculto').selectedIndex;
		txt = elm.options[idx].value;
		if (txt=='null')
		{
			alert('Please, select pickup location');
			return false;
		}
	}

	if (document.getElementById('destinosdevolucion')){
		elm = document.getElementById('destinosdevolucion');
		idx = document.getElementById('destinosdevolucion').selectedIndex;
		txt = elm.options[idx].value;
		if (txt=='null')
		{
			alert('Please, select dropoff location');
			return false;
		}
	}
}