
var hotel = {
	id : null,
	costs : null,
	elm : {
		price : 'hotel_reservation_price',
		no_nights : 'hotel_reservation_no_night'
	},
	select : function (id) {
	
		if (this.id) {
			$(this.id).style.background = '';
		}
		
		if (id) {
			$(id).style.background = 'yellow';
			this.id = id;
			
			this.__update__price($(id).innerHTML);
		}
	},	
	__update__price : function (price) {
		$(this.elm.price).innerHTML = price;
	}
}

function go_to(url) {
	window.location=url;
}

function specific_lodging_facility(element, p) 
{
	p = 'show_hide_accomadations';
	if (! element.value) {
		$(p).show();
		$('own_accomadation_mess').hide();
	} else {
		$(p).hide();
		$('own_accomadation_mess').show();
	}
}

function accompanying_person(elm) 
{
	if (elm.value != '0') {
		$('acc_per_naw').show();
	} else {
		$('acc_per_naw').hide();
	}
}

function tour_change_options(elm)
{
	var _selectedIndex = elm.value;

	elm.options.length = 0;

	elm.options[0] = new Option('None', '0');
	elm.options[1] = new Option('First Registrant', '1');
	
	if ($('acc_first_name').value != '' && $('acc_last_name').value != '')
	{
		elm.options[2] = new Option('Accompanying person', '3');
		elm.options[3] = new Option('Both', '2');
	}

	elm.selectedIndex = _selectedIndex;
}

function format_ammount(ammount) {


	if (ammount == 0) return ammount;

	ammount = ammount.toString().split('.');
	
	if (ammount[1] && ammount[1].toString().length == 1)
	{
		ammount[1] = ammount[1].toString() + '0';
	}

	return ammount.join('.');
}

function calc()
{
	if ($('__SUBTOTAL_HOTELS'))
		$('__SUBTOTAL_HOTELS').innerHTML = _calc('hotels');
	$('__SUBTOTAL_TOURS').innerHTML = _calc('tours');
	$('__SUBTOTAL_FEE').innerHTML = _calc('fee');	
	
	$('__TOTAL_FEE').innerHTML = _calc('fee');
 	$('__TOTAL_HOTELS').innerHTML = _calc('hotels'); 
	$('__TOTAL_TOURS').innerHTML = _calc('tours');
	$('__TOTAL').innerHTML = _calc('total');
		
	$('_total_fee_').value = _calc('fee');
	$('_total_hotel_').value = _calc('hotels');
	$('_total_tour_').value = _calc('tours');
	$('_total_total_').value = _calc('total');
}

function _calc (sort) 
{
	var __TOTAL = 0;

	/*
	** FEE
	*/
	if (sort == 'fee')
	{
		$A($('FEE').getElementsByTagName("SELECT")).each(function(elm) 
		{
			if (elm.getAttribute("price") && elm.value == 'yes')
			{
				__TOTAL = __TOTAL + (elm.getAttribute("price") / 1);
			}
		});
	}

	/*
	** HOTELS
	*/
	else if (sort == 'hotels')
	{
		$A($('__HOTELS').getElementsByTagName("INPUT")).each(function(_checkbox)
		{
			if (_checkbox.getAttribute('ttype') == 'hotel' && _checkbox.value != '')
			{
				if (_checkbox.checked)
				{
					__TOTAL = __TOTAL + ((_checkbox.getAttribute('price') * days_range) / 1);
				}
			}
		});
	}

	/*
	** TOURS
	*/
	else if (sort == 'tours')
	{
		$A($('__TOURS').getElementsByTagName("SELECT")).each(function(_select)
		{
			if (_select.getAttribute('ttype') == 'tour' && _select.value != '')
			{
				$A(_select.getElementsByTagName("OPTION")).each(function(_option) 
				{
					if (_option.selected)
					{
						if (_option.value == '0')
							times = 0
						else if (_option.value == '1')
							times = 1
						else if (_option.value == '2')
							times = 2
						else if (_option.value == '3')
							times = 1
						
						__TOTAL = __TOTAL + ((_select.getAttribute('price') / 1) * times);
					}
				});
			}
		});
	}
	
	/*
	** TOTAL
	*/
	else if (sort == 'total')
	{
		__TOTAL = (__EVENT_PRICE / 1) + (_calc('hotels')/1) + (_calc('tours')/1) + (_calc('fee')/1);
	}
		
	return format_ammount(__TOTAL);
}

function search_accomadation(check_in_out, results_container, results_url)
{
	if ( ! check_in_out.value)
	{
		$(results_container).hide();
		$(results_container).innerHTML = '';
	}
	else
	{	
		check_in_out = check_in_out.value.split('-');
		
		results_url = results_url.replace('%check_in%', check_in_out[0]).replace('%check_out%', check_in_out[1]);
		
		new Ajax.Request(results_url, {
			onComplete: function(t) {
				$(results_container).show();
				$(results_container).innerHTML = t.responseText;
			}
		});
	}
}

var days_range;

function search_suitable_accommodation (url, element)
{
	var select_arrival = $('select_arrival');
	var select_departure = $('select_departure');
	
	days_range = (select_departure.value / 1) - (select_arrival.value / 1);
	days_range = (days_range / (1 * 60 * 60 * 24));

	new Ajax.Request(url, {
		method: 'post',
		parameters: 'arrival='+select_arrival.value+'&departure='+select_departure.value,
		onComplete: function(t) {
			$(element).innerHTML = t.responseText;
			$(element).show();
		}
	});
}




function update_courses(element)
{
	$A($('FEE').getElementsByTagName("SELECT")).each(function(elm) 
	{
		if (elm.getAttribute("mbtype") == 'courses')
		{
			if (elm.id != element.id)
			{
				$A(elm.options).each(function(option) 
				{
					if (option.value == element.value && option.value != '0')
					{
						option.disabled = true; //hide();
					}
					else
					{
						option.disabled = false; //.show();
					}						
				});
			}			
		}
	});
}

function make_submit()
{
	$('submit_button').disabled = false;
}

function agree_payment()
{
	calc();
	
	if ($('agree_this').checked)	
		return true;
	else {
		alert('You must agree to the terms and conditions.');
		return false;
	}
		
}

