var select_country = null;
var select_province = null;
var select_city = null;
var select_hospital = 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 );
	}
}

function city_changed ( page_load )
{
	disable ( select_hospital );
	if ( select_city.val () != "" )
	{
		city_class = ".hospital_item.c_id_" + select_city.val ();
		$(".hospital_item").appendTo ( $("#hospital_holder") );
		$(city_class, $("#hospital_holder") ).appendTo ( select_hospital );
		if ( page_load == false ) select_hospital.val ( "" );
		enable ( select_hospital );
		
	}
}
	
		
$(document).ready(function() { 
	$('table.blueStripe table td').addClass('plain');
	
	$('label').click(function() { 
		$(this).parent().parent().find('input').attr("selected", "selected");
		});
		
	$('input[@type=radio].star').rating();
	
	enable ( select_country = $("select#country_select") );			
	disable ( select_province = $("select#privProvSelect") );			
	disable ( select_city = $("select#City") );
	disable ( select_hospital = $("select#Hospital") );
	
	select_country.change ( function () { country_changed ( false ); } );
	select_province.change ( function () { province_changed ( false ); } );
	select_city.change ( function () { city_changed ( false ); } );
	
	select_hospital.change ( function ()
		{
			if ( select_hospital.val () == "" ) return;
			var url = "/patients/hospital_rate/" + select_hospital.val () + "/";
			window.location.replace ( url );
		}
	);

	if ( select_country.val () != "" )
	{
		country_changed ( true );
		province_changed ( true );
		city_changed ( true );
	}
});
