$(document).ready(function(){

//	$('select[id$="_box"]').each(function(idx) {
//		if ($(this).children().length == 1)
//		{
//			var field_name = $(this).attr('name');
//			var field_value = $(this).val();
//			$(this).append('<input type="hidden" id="so_' + $(this).attr('id') + '" name="'+ field_name +'" value="'+ field_value +'" />');
//			$(this).attr('disabled', 'disabled');
//		}
//	});
    load_region();
});



function load_region()
{
	$.get(
		'/ajax/regions/',
		{
			'country': $('#country_box').attr('value'),
			'rq_date': $('#departure').attr('value'),
			'delta': $('#delta_box').attr('value'),
            'selected': $('#region_box').attr('value')
		},
		function(data)
		{
			setOptions('region_box', data);
			load_city();
		}
	);
}

function load_city()
{
	$.get(
		'/ajax/cities/',
		{
			'country': $('#country_box').attr('value'),
			'region': $('#region_box').attr('value'),
			'rq_date': $('#departure').attr('value'),
			'delta': $('#delta_box').attr('value'),
            'selected': $('#city_box').attr('value')
		},
		function(data)
		{
			setOptions('city_box', data);
			load_theme();
		}
	);
}

function load_theme()
{
	$.get(
		'/ajax/tourthemes/',
		{
			'country': $('#country_box').attr('value'),
			'region': $('#region_box').attr('value'),
			'city': $('#city_box').attr('value'),
			'rq_date': $('#departure').attr('value'),
			'delta': $('#delta_box').attr('value'),
            'selected': $('#theme_box').attr('value')
		},
		function(data)
		{
			setOptions('theme_box', data);
			load_hotel_type();
		}
	);
}

function load_hotel_type()
{
	$.get(
		'/ajax/hoteltypes/',
		{
			'country': $('#country_box').attr('value'),
			'region': $('#region_box').attr('value'),
			'city': $('#city_box').attr('value'),
			'tourtheme': $('#theme_box').attr('value'),
			'rq_date': $('#departure').attr('value'),
			'delta': $('#delta_box').attr('value'),
            'selected': $('#hotel_type_box').attr('value')
		},
		function(data)
		{
			setOptions('hotel_type_box', data);
			load_formula();
			load_stars();
		}
	);
}

function load_formula()
{
	$.get(
		'/ajax/formulas/',
		{
			'adults': $('#adults_box').attr('value'),
			'children': $('#children_box').attr('value'),
			'hoteltype': $('#hotel_type_box').attr('value'),
            'selected': $('#formula_box').attr('value')
		},
		function(data)
		{
			setOptions('formula_box', data);
		}
	);
}

function load_stars()
{
	$.get(
		'/ajax/hotelratings/',
		{
			'country': $('#country_box').attr('value'),
			'region': $('#region_box').attr('value'),
			'city': $('#city_box').attr('value'),
			'tourtheme': $('#theme_box').attr('value'),
			'rq_date': $('#departure').attr('value'),
			'delta': $('#delta_box').attr('value'),
			'hoteltype': $('#hotel_type_box').attr('value'),
            'selected': $('#stars_box').attr('value')
		},
		function(data)
		{
			setOptions('stars_box', data);
			load_meal();
		}
	);
}

function load_meal()
{
	$.get(
		'/ajax/meals/',
		{
			'country': $('#country_box').attr('value'),
			'region': $('#region_box').attr('value'),
			'city': $('#city_box').attr('value'),
			'rq_date': $('#departure').attr('value'),
			'delta': $('#delta_box').attr('value'),
            'selected': $('#meal_box').attr('value'),
            'hoteltype': $('#hotel_type_box').attr('value'),
            'hotelrating': $('#stars_box').attr('value'),
            'night_count': $('#night_count_box').attr('value')
		},
		function (data)
		{
			setOptions('meal_box', data);
			load_night_count();
		}
	);
}

function load_night_count()
{
	$.get(
		'/ajax/night_count/',
		{
			'country': $('#country_box').attr('value'),
			'region': $('#region_box').attr('value'),
			'city': $('#city_box').attr('value'),
			'rq_date': $('#departure').attr('value'),
			'delta': $('#delta_box').attr('value'),
            'selected': $('#night_count_box').attr('value'),
            'hoteltype': $('#hotel_type_box').attr('value')
		},
		function (data)
		{
			setOptions('night_count_box', data);
			set_dates();
		}
	);
	$('#night_count_box').load();
}

function reload_box(entity, parent, parent_id)
{
	$.post( '/searchprice/ajax/select/'+ entity + '/' + parent + '/' + parent_id + '/'+ $('#departure').value +'/'+ $('#delta').value +'/',
			{},
			function(data)
			{
				setOptions(entity+'_box', xhr.responseText);
				$('#'+entity+'_box').value = 0;
			}
	);
}

function load_xact_formula()
{
	$.post(
		'/searchprice/ajax/fetch/xact_formula/',
		{
			'date': $('#departure').val(),
			'country': $('#country_box').val(), 
			'region': $('#region_box').val(), 
			'city': $('#city_box').val(), 
			'meal': $('#meal_box').val(), 
			'nights': $('#night_count_box').val(), 
			'theme': $('#theme_box').val(), 
			'stars': $('#star_box').val(),
			'adults': $('#adults_box').val(),
			'children': $('#children_box').val()
		},
		function (data)
		{
			setOptions('formula_box', data)
		}
	);
}

function allowNum()
{
    var key = window.event.keyCode;
    if ((key > 47 && key < 58) || key == 8)
        return;
    else
        window.event.returnValue = null;
}

function allowDate()
{
    var key = window.event.keyCode;
    if ((key > 47 && key < 58) || key == 8 || key == 46)
        return;
    else
        window.event.returnValue = null;
}

function look_for_hotels()
{
	var lookup_string = ($('#ignore_hotels').attr('checked')) ? '' : $('#ts-hotel-name').val();
	$.post('/searchprice/ajax/fetch/hotel_list/', { 
			'date': $('#departure').val(),
			'country': $('#country_box').val(), 
			'region': $('#region_box').val(), 
			'city': $('#city_box').val(), 
			'meal': $('#meal_box').val(), 
			'stars': $('#stars_box').val(),
			'nights': $('#night_count_box').val(), 
			'theme': $('#theme_box').val(), 
			'hotel_substr': lookup_string,
			'hotel_type': $('#hotel_type_box').attr('value')			
		},
		function(data) { $('#ts-hotel-list').html(data); hotel_lookup_timeout_set = false; }
	);
}

function reload_formula()
{
	if ($('#hotel_type_box').val() == 1) load_formula(); else load_xact_formula();
}

function adjust_date()
{
    $.get('/ajax/adjust_date/', {
       'rq_date': $('#departure').val(),
       'country': $('#country_box').val()
    },
    function(data) {
        $('#departure').val(data);
        load_region();
    });
}

function set_dates()
{
	$.get('/ajax/nearest_jdate/', {
			'current_date': $('#departure').val(),
			'country': $('#country_box').attr('value'),
			'region': $('#region_box').attr('value'),
			'city': $('#city_box').attr('value'),
			'meal': $('#meal_box').attr('value'),
			'nights': $('#night_count_box').attr('value'),
			'theme': $('#theme_box').attr('value')
		},
		function(data) {
			var reply = eval(data);
			$('#departure').val(reply[0]);
			fetch_valid_dates(reply[1], reply[2]);
			if ($('#hotel_type_box').val() == 2) load_xact_formula();
		}
	);
}

function is_a_valid_date(day, month, year)
{
	var i;
	for (i = 0; i < valid_dates.length; i++)
	{
		if ((valid_dates[i][0] == year) &&
			(valid_dates[i][1] == month + 1) &&
			(valid_dates[i][2] == day)) return true;
	}
	return false;
}

function fetch_valid_dates(vd_month, vd_year)
{
	$.get('/ajax/valid_dates/', {
			'year': vd_year,
			'month': vd_month,
			'country': $('#country_box').attr('disabled') ? 0 : $('#country_box').val(),
			'region': $('#region_box').attr('disabled') ? 0 : $('#region_box').val(),
			'city': $('#city_box').attr('disabled') ? 0 : $('#city_box').val(),
			'meal': $('#meal_box').attr('disabled') ? 0 : $('#meal_box').val(),
			'night_count': $('#night_count_box').attr('disabled') ? 0 : $('#night_count_box').val(),
			'tourtheme': $('#theme_box').attr('disabled') ? 0 : $('#theme_box').val(),
            'hotelrating': $('#stars_box').attr('disabled') ? 0 : $('#stars_box').val(),
            'hotel_type': $('#hotel_type_box').val()
		},
		function(data) { valid_dates = eval(data); $('#departure').datepicker('refresh'); }
	);
}

