function $(element)
{
	return document.getElementById(element);
}
 
function $v(element)
{
	return document.getElementById(element).value;
}

function $0(element)
{
	$(element).style.display = 'none';
}

function $1(element)
{
	$(element).style.display = '';
}

function altePest(n, m, id)
{
	if( n=='descripcion' )
	{
		$0('observando'); $('obser').className = 'inactiu';
		$1('descripcion'); $('descrip').className = 'actiu';
	}
	else if( n=='observando' )
	{
		$0('descripcion'); $('descrip').className = 'inactiu';
		$1('observando'); $('obser').className = 'actiu';

		if($('observando_txt').style.display == 'none')
		{
			var req = new Request({
				method: 'get',
				url: m,
				data: {'action' : 'DESCRIPCION_COSAS', 'idpaquete' : id },
				onComplete: function(response) {

					$('observando_txt').innerHTML = response;
					
					Element.setStyle($('observando_loader'), 'display', 'none');
					Element.setStyle($('observando_txt'), 'display', '');
					
				},
				onFailure: function() {

					Element.setStyle($('observando_loader'), 'display', 'none');
					Element.setStyle($('observando_txt'), 'display', '');
					
				}
			}).send();			
		}
	}
}

function loadServicios(m, id)
{
	if(id=='')
		return false;
	// make the ajax call
	var req = new Request({
		method: 'get',
		url: m,
		data: {'action' : 'SERVICIOS', 'idpaqueteciudadsalida' : id },
		onRequest: function() {
			
			//$('divSelectSalidas').setStyle('display','none');
			
			Element.setStyle($('divSelectSalidas'), 'display', 'none');
			Element.setStyle($('divLoadingServicios'), 'display', '');
			
		},
		onComplete: function(response) {

			//$('txtHint').set('html',response);
			
			$('txtHint').innerHTML = response;
			
			Element.setStyle($('divLoadingServicios'), 'display', 'none');
			Element.setStyle($('divSelectSalidas'), 'display', '');
			
		},
		onFailure: function() {

			Element.setStyle($('divLoadingServicios'), 'display', 'none');
			Element.setStyle($('divSelectSalidas'), 'display', '');
			
		}
	}).send();

}


var fichaActual = '';
function showFicha(i)
{
	if(i == fichaActual)
	{
		closeFicha(i);
		return false;
	}
	
	if (fichaActual != '')
	{
		$0('ficha'+fichaActual);
		alteIco(fichaActual,'+');
	}
	$1('ficha'+i);
	alteIco(i,'-');
	fichaActual = i;
}

function closeFicha(i)
{
	$0('ficha'+i);
	alteIco(i,'+');
	fichaActual = '';
}

function alteIco(i,o)
{
	if(o == '+')
		$('ico'+i).src = 'img/plus_icon.gif';
	else
		$('ico'+i).src = 'img/minus_icon.gif';
}

function ir_configuracion(m, i)
{
	window.location = m+'?action=FORM_CONFIGURACION&opc='+$('selectSalida'+i).value;
}

function add_per(n)
{
	if(cur_salida != '')
	{
		$0('div_salida'+cur_salida);
		$1('div_salida'+n);
		cur_salida = n;
	}
	
	if(cur_servicio != '')
	{
		$0('div_servicio'+cur_servicio);
		$0('habita'+cur_servicio);
		$1('div_servicio'+n);
		$1('habita'+n);
		cur_servicio = n;
	}

	if(cur_tasa != '')
	{
		$0('div_tasa'+cur_tasa);
		$1('div_tasa'+n);
		cur_tasa = n;
	}
	
	calcula(n);
	
}

var suma_sup=0;

function add_sup(s, n)
{
	suma_sup = 0;
	for( var i = 0 ; i < $v('numSup') ;  i++)
		suma_sup = suma_sup + $v('precioSup'+i) * $v('selectSup'+i);

	for( var i = 0 ; i < $v('numSup') ;  i++)
		for( var j = 1 ; j <= $v('maxPasa') ;  j++)
			$0('div_sup'+i+j)
	
	if(suma_sup == 0)
	{
		$0('div_suplementos');
	}
	else
	{
		for( var i = 0 ; i < $v('numSup') ;  i++)
			if( $v('selectSup'+i) > 0 )
				$1('div_sup'+i+$v('selectSup'+i));
		
		$1('div_suplementos');
	}
	
	calcula($v('numPer'));
}

function calcula(n)
{
	var pt = n*$v('preciobase') + n*$v('ciudadsalida_precio') + n*$v('tasaaeropuerto_precio') + suma_sup;
	$('ptotal').value = pt + ' €';
}

function deplegar_presupuesto()
{
	scroll_bottom();
}

function scroll_bottom()
{
	var myFx = new Fx.Scroll(window).toBottom();
}

function sus_registrar(m)
{
	var e = $v('input_sus');
	
	// make the ajax call
	var req = new Request({
		method: 'post',
		url: m,
		data: {'action' : 'REGISTRAR', 'email' : e, 'ajax' : true },
		onRequest: function() {
			
			$0("div_sus_ok");
			$0("div_sus_error");
			$0("div_sus_warn");
			$0("sobre_caja");
			$1("loader_sus");
			
		},
		onComplete: function(response) {

			// convert the response to an object
			var json = eval(response);

			// verify in the server indicates any error
			if(json.header == 'error')
			{
				$0('loader_sus');
				$1('sobre_caja');
				$("div_sus_error").innerHTML = json.message;
				$1("div_sus_error");
			}
			else if(json.header == 'registrado')
			{
				$0('loader_sus');
				$1('sobre_caja');
				$("div_sus_ok").innerHTML = json.message;
				$1("div_sus_ok");
			} 
			else if(json.header == 'yaexiste')
			{
				$0('loader_sus');
				$1('sobre_caja');
				$("div_sus_warn").innerHTML = json.message;
				$1("div_sus_warn");
			} 
			else if(json.header == 'sinconfirmar')
			{
				$0('loader_sus');
				$1('sobre_caja');
				$("div_sus_warn").innerHTML = json.message;
				$1("div_sus_warn");
			} 
			else
			{
				$0('loader_sus');
				$1('sobre_caja');
				$("div_sus_error").innerHTML = "Ocurrió un error recibiendo la respuesta del servidor, intenta de nuevo"
				$1("div_sus_error");
			}
			
		},
		onFailure: function() {

			$0('loader_sus');
			$1('sobre_caja');
			$("div_sus_error").innerHTML = "Ocurrió un error enviando los datos, intenta de nuevo";
			$1("div_sus_error");
			
		}
	}).send();
}

function desc_vermas()
{
	$0('a_vermas');
	$1('div_desclarga2');
	$1('a_vermenos');
}

function desc_vermenos()
{
	$0('div_desclarga2');
	$0('a_vermenos');
	$1('a_vermas');
}


/**
 * código para pasar las imágenes de paquete automáticamente
 */

var d=document;
var imgs = new Array();
var zInterval = null;
var current=0;
var pause=false; 
var intervalPaquetes=4000;

function slide_imgPaquetes()
{
	if(!d.getElementById || !d.createElement)return;
	
	imgs = d.getElementById("imageContainerPaquetes").getElementsByTagName("img");
	
	for( i=1; i<imgs.length; i++)
		imgs[i].xOpacity = 0;
	
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	setTimeout(so_xfadePaquetes,intervalPaquetes);	
}

function so_xfadePaquetes()
{
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;
	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		current = nIndex;
		setTimeout(so_xfadePaquetes,intervalPaquetes);
	} else {
		setTimeout(so_xfadePaquetes,50);
	}
	
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}

function ordenarBusqueda(u)
{
	window.location = u;	
}

var curPais = 0; 
var curCat = 0;
var curFecha = 0;

function bmini_update(m, combo, value)
{
	curPais = $v('selectPais'); 
	curCat = $v('selectCat');
	curFecha = $v('selectFecha');
	
	
	// make the ajax call
	var req = new Request({
		method: 'get',
		url: m,
		data: {'action' : 'BUSCADOR_MINI_ACTUALIZAR', 'combo' : combo, 'value' : value, 'ajax' : true },
		onRequest: function() {
			
			bmini_loading(combo);
			
		},
		onComplete: function(response) {

			// convert the response to an object
			var json = eval(response);

			// verify in the server indicates any error
			if(json.header == 'ok')
			{
				bmini_fill(combo, json);
			}
			else
			{
				bmini_failure(combo);
			} 
		},
		onFailure: function() {

			bmini_failure(combo);
			
		}
	}).send();	
	
}


function bmini_loading(combo)
{
	switch(combo)
	{
		case 'Pais':
			$('selectCat').disabled = true;
			$('selectFecha').disabled = true;
			break;
			
		case 'Cat':
			$('selectPais').disabled = true;
			$('selectFecha').disabled = true;
			break;

		case 'Fecha':
			$('selectPais').disabled = true;
			$('selectCat').disabled = true;
			break;
	}
}

function bmini_failure(combo)
{
	switch(combo)
	{
		case 'Pais':
			$('selectCat').disabled = false;
			$('selectFecha').disabled = false;
			break;
			
		case 'Cat':
			$('selectPais').disabled = false;
			$('selectFecha').disabled = false;
			break;

		case 'Fecha':
			$('selectPais').disabled = false;
			$('selectCat').disabled = false;
			break;
	}
}

function bmini_fill(combo, json)
{
	switch(combo)
	{
		case 'Pais':
			
			bmini_fill_categorias(json.categorias);
			bmini_fill_fechas(json.fechas);
			
			if(curPais == 0)
				bmini_fill_paises(json.paises);
			
			break;
			
		case 'Cat':
			
			bmini_fill_paises(json.paises);
			bmini_fill_fechas(json.fechas);
			
			if(curCat == 0)
				bmini_fill_categorias(json.categorias);
			
			break;

		case 'Fecha':
			
			bmini_fill_paises(json.paises);
			bmini_fill_categorias(json.categorias);
			
			if(curFecha == 0)
				bmini_fill_fechas(json.fechas);
			
			break;
	}
}


function bmini_fill_paises(paises)
{
	// opción seleccionada finalmente
	var paisSeleccion = 0; 

	// objeto combo
	var comboPais = $('selectPais');
	
	// remover todas las opciones
	combo_vaciar(comboPais);
	
	// ingresar el primer elemento
	comboPais.options[0] = new Option( "Elija país de interés..." , 0 );
	
	if(paises)
	{
		// ingresar las otras opciones
		var j = 1;
		for (var i = 0 ; i < paises.length ; i++)
		{
			var arrStrPais = String(paises[i].pais).split(';');
			var id = arrStrPais[0];
			var nombre = arrStrPais[1];
			
			// agregar
			comboPais.options[j] = new Option( nombre , id );
			
			// verificar si la opción seleccionada está disponible
			if(curPais == id)
				paisSeleccion = j;
			
			j++;
		}
	}
	
	// seleccionar el elemento
	comboPais.selectedIndex = paisSeleccion;

	// habilitar combo
	comboPais.disabled = false;
}

function bmini_fill_categorias(categorias)
{
	// opción seleccionada finalmente
	var catSeleccion = 0; 

	// objeto combo
	var comboCat = $('selectCat');
	
	// remover todas las opciones
	combo_vaciar(comboCat);
	
	// ingresar el primer elemento
	comboCat.options[0] = new Option( "Elija categoría del viaje..." , 0 );
	
	if(categorias)
	{
		// ingresar las otras opciones
		var j = 1;
		
		for (var i = 0 ; i < categorias.length ; i++)
		{
			var arrStrCat = String(categorias[i].cat).split(';');
			var id = arrStrCat[0];
			var nombre = arrStrCat[1];
			
			// agregar
			comboCat.options[j] = new Option( nombre, id);
			
			// verificar si la opción seleccionada está disponible
			if(curCat == id)
				catSeleccion = j;
			
			j++;
		}
	}
	
	// seleccionar el elemento
	comboCat.selectedIndex = catSeleccion;

	// habilitar combo
	comboCat.disabled = false;
	
}

function bmini_fill_fechas(fechas)
{
	// opción seleccionada finalmente
	var fechaSeleccion = 0; 

	// objeto combo
	var comboFecha = $('selectFecha');
	
	// remover todas las opciones
	combo_vaciar(comboFecha);
	
	// ingresar el primer elemento
	comboFecha.options[0] = new Option( "Seleccione Mes..." , 0 );
	
	if(fechas)
	{
		// ingresar las otras opciones
		var j = 1;
		for (var i = 0 ; i < fechas.length ; i++)
		{
			var arrStrFecha = String(fechas[i].fecha).split(';');
			var numeros = arrStrFecha[0];
			var nombre = arrStrFecha[1];
			
			// agregar
			comboFecha.options[j] = new Option( nombre , numeros );
			
			// verificar si la opción seleccionada está disponible
			if(curFecha == numeros)
				fechaSeleccion = j;
			
			j++;
		}
	}
	
	// seleccionar el elemento
	comboFecha.selectedIndex = fechaSeleccion;

	// habilitar combo
	comboFecha.disabled = false;
	
}

function combo_vaciar(select)
{
 	while (select.length> 0) {
 		select.remove(0);
 	} 		

}

function obs_focus(m) 
{ 
	if($v('text_obs') == m) 
		$('text_obs').value = ""; 
} 
function obs_blur(m) 
{ 
	if($v('text_obs') == "") 
		$('text_obs').value = m; 
}

function scroll_consulta()
{
	var myFx = new Fx.Scroll(window).toElement('div_cons_error');
}

function cons_focus(m) 
{ 
	if($v('input_cons_text') == m) 
		$('input_cons_text').value = ""; 
} 
function cons_blur(m) 
{ 
	if($v('input_cons_text') == "") 
		$('input_cons_text').value = m; 
}

