
$(document).ready(LoadRegions);
$('#all_star').livequery("change",function()
	{
			Enable("stars","all_star");	
	}
);

$('#all_region').livequery("change",function()
	{
			Enable("regions","all_region");
			LoadRegions();
	}
);

$('#all_city').livequery("change",function()
	{
			Enable("cities","all_city");
			LoadCities();
	}
);

$('#all_hotel').livequery("change",function()
	{
			Enable("hotels","all_hotel");
			LoadHotels();
	}
);
function Enable(el_id,check_id)
{
var check = document.getElementById(check_id);
var enable = document.getElementById(el_id);
enable.disabled = check.checked;
}

function LoadRegions()
{
	LoadJSONInSelect('Sources/main_search.php?act=regions&parent_id=2','regions','all_region');
	Enable("stars","all_star");	
	LoadCities();
}

function LoadCities()
{
	var url			= 'Sources/main_search.php?act=cities' + GetParentIds('regions'); 
	LoadJSONInSelect(url,'cities','all_city');
	LoadHotels();
}

function LoadHotels()
{
	var url			= 'Sources/main_search.php?act=hotels' + GetParentIds('cities'); 
	LoadJSONInSelect(url,'hotels','all_hotel');
}

function GetSelectedItems(el_name)
{
var select = document.getElementById(el_name);
var selected = new Array();
for (var i = select.selectedIndex; i < select.length ; i++)
 {
	if (select.options[i].selected)
	 {
	 	selected.push(i);
	 }
 }
return selected;
}

function GetParentIds(el_id)
{
	var element = document.getElementById(el_id);
	var url = '';
		if (element.selectedIndex!=-1)
		{
		 	var indexes = GetSelectedItems(el_id);
			var i;
			for (i in indexes)
		 	 {url = url.concat('&parent_id[]=',element.options[indexes[i]].value);}
		}
	return url;
}

function LoadJSONInSelect(url,el_id,check_id)
{
	var json_id = "#"+el_id;
	$(json_id).hide();
	$(json_id+' option').remove();
	var sel   = document.getElementById(el_id);
	var check = document.getElementById(check_id);
	if (!check.checked)
	{
		$.getJSON(url,
		function(data) 
		{
			$.each(data, function(key,element)
				{
					var opt=document.createElement('option');
					opt.text  = element.value;
					opt.value = element.id;
					try
					  {
						  sel.add(opt,null); // standards compliant
					  }
					catch(ex)
					  {
						  sel.add(opt); // IE only
					  }
				}
			      );
	   }
	);
	$(json_id).show();
	}
}