var select_country = null;
var select_province = null;
var select_city = null;
	
function enable ( elem )
{
	elem.removeAttr ( "disabled" );
}

function disable ( elem )
{
	elem.attr ( "disabled", "disabled" );
}

function country_changed ( page_load )
{

	disable ( select_province )
	if ( select_country.val() != '' )
	{
		var country_class = '.province_item.c_id_' + select_country.val ();
		$(".province_item").appendTo ( $("#province_holder") );
		$(country_class, $("#province_holder") ).appendTo ( select_province );
		if ( page_load == false ) 
		{
			select_province.val( '' );
			select_city.val( '' );
			disable ( select_province );
			disable ( select_city );
		}
		enable ( select_province );
	}
}

function province_changed ( page_load )
{
	disable ( select_city );
	if ( select_province.val () != "" )
	{
		province_class = ".city_item.p_id_" + select_province.val ();
		$(".city_item").appendTo ( $("#city_holder") );
		$(province_class, $("#city_holder") ).appendTo ( select_city );
		if ( page_load == false ) select_city.val ( "" );
		enable ( select_city );
	}
}
	
		
$(document).ready(function() { 
	$('table.blueStripe table td').addClass('plain');
	$('table.stripeMe tr:odd').find('td').addClass('odd');
	
	enable ( select_country = $("select#country_select") );			
	disable ( select_province = $("select#privProvSelect") );			
	disable ( select_city = $("select#City") );
	
	select_country.change ( function () { country_changed ( false ); } );
	select_province.change ( function () { province_changed ( false ); } );

	select_city.change ( function ()
		{
			if ( select_city.val () == "" ) return;
			var url = "/hospitals/city/" + select_city.val () + "/";
			window.location.replace ( url );
		}
	);
	
	if ( select_country.val () != "" )
	{
		country_changed ( true );
		province_changed ( true );
	}
});
