function ucFirst(s)
{
	var c = s.charAt(0);

	if (parseInt(s.length)==1){
	return c.toUpperCase();
	}
	else
	{
	return c.toUpperCase() + s.slice(1).toLowerCase();
	}
}

function getCategories()
{
/*
	 $('#country').html('<option value="">Alege tara</option>');//empty country select
	 
	  var categories = '<option value="">Alege categoria</option>';

	  for ( key in cat )//get key => value
	  {	
			categories += '<option value="'+ key + '">' + key + '</option>';
	  }


	 $('#service').html( categories );	
*/
	 $('#service').bind('change', function ()
	 {
		getCountries( this.value );
	});
	 $('#service').trigger('change');
}

function getCountries( category )
{
	 $('#city').html('<option value="">Alege orasul</option>');//empty city select
	 
	  var countries = '<option value="">Alege tara</option>';


	  for ( key in cat[category] )//get key => value
	  {	
				var selected;
				if ( key == defaultCountry )
				{
					selected = 'selected';
				} else
				{
					selected = '';
				}

				countries += '<option value="'+  key + '" '  + selected + '>' + ucFirst(key) + '</option>';
	  }

	 $('#country').html( countries );	
	 
	 $('#country').bind('change', function ()
	 {
		getCities( $('#service').val(), this.value );
	});
	
	if(defaultCountry != null)
    {
		getCities( $('#service').val(), defaultCountry );
	}
}

function getCities( category, country )
{

	 if ( country != '' )
	 {
		 var cities = '<option value="">Alege orasul</option>';

		 for ( key in cat[category][country] )//get key => value
		 {	
				var selected;
				if ( cat[category][country][key] == defaultCity )
				{
					selected = 'selected';
				} else
				{
					selected = '';
				}
				cities += '<option value="' + cat[category][country][key] + '" ' + selected + '>' + ucFirst(cat[category][country][key]) + '</option>';
		 }

		 $('#city').html( cities );//insert the option into the select
  	 }
	 
}

$(function()//on page load
{
	getCategories();//load all countries
});
