jQuery.extend({	
	getJSON: function( url, success_callback, error_callback ){
		return jQuery.ajax({
			type: "GET",
			url: url,
			data: null,
			success: success_callback,
			error: error_callback,
			dataType: "json"
		});
	}
});

jQuery(document).ready(function()
{
	function getURLParameter( name ) {
		return unescape(
			(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,''])[1]
		);
	};
	
	// on country change
	jQuery('#location_country').change(function() {
		jQuery('#location_region').empty()
			.append('<option value="">' + loadingText + '</option>')
			.attr('disabled', 'disabled');
		if( jQuery('#location_country').val() == 'GB' ){
			jQuery('#location_region').hide();
			jQuery('#location_town').hide();
			jQuery('#location_town_uk').show();
		} else {
			jQuery('#location_region').show();
			jQuery('#location_town').show();
			jQuery('#location_town_uk').hide();
		}
		jQuery.getJSON( 
			baseURL + '/cache/jobLocations_' + this.value + '_' + languageCode + '.json', 
			function( data ) {
				jQuery('#location_region')
					.removeAttr('disabled')
					.empty()
					.append( '<option value="0">' + allRegionsText + '</option>' );
				jQuery('#location_town').empty()
					.append('<option value="0">' + allTownsText + '</option>');
				jQuery.each( data.jobLocations, function(key, val) {
					jQuery('#location_region').append( '<option value="' + key + '">' + val.name + '</option>' );
				});
			},
			function( request, status, error) {
				jQuery('#location_region')
					.removeAttr('disabled')
					.empty()
					.append( '<option value="0">' + allRegionsText + '</option>' );
			}
		);
		jQuery.ajax({ 
			url: baseURL + '/xpress/getSectorDropdownOptions.php?countryCode=' + this.value + '&specialismID=' + getURLParameter('sp'),
			success: function( data ) {
				jQuery('#rw_sector')
					.removeAttr('disabled')
					.empty()
					.append( data );
				jQuery('#rw_sub_sector').empty()
					.append('<option value="0">' + allSubSectorsText + '</option>');
			},
			error: function( request, status, error) {
				jQuery('#rw_sector')
					.removeAttr('disabled')
					.empty()
					.append( '<option value="0">' + allSectorsText + '</option>' );
			}
		});
		var currencyCodeAndSymbol = currencyCodesAndSymbols[ jQuery('#location_country').val() ];
		jQuery('#currencySymbol').html( currencyCodeAndSymbol.symbol );
		jQuery('#currencyCode').val( currencyCodeAndSymbol.code );
	});
	
	// on region change
	jQuery('#location_region').change(function() {
		if( this.value == 0 )
		{
			jQuery('#location_town').empty()
				.append('<option value="0">' + allTownsText + '</option>');
		}
		else
		{
			jQuery('#location_town').empty()
				.append('<option value="">' + loadingText + '</option>')
				.attr('disabled', 'disabled');
			jQuery.getJSON( 
				baseURL + '/cache/jobLocations_' + jQuery('#location_country').val() + '_' + languageCode + '.json', 
				function( data ) {
					jQuery('#location_town')
						.removeAttr('disabled')
						.empty().
						append( '<option value="0">' + allTownsText + '</option>' );
					jQuery.each( data.jobLocations[ jQuery('#location_region').val() ].towns, function(key, val) {
						jQuery('#location_town').append( '<option value="' + key + '">' + val + '</option>' );
					});
				},
				function( request, status, error) {
					jQuery('#location_town')
						.removeAttr('disabled')
						.empty()
						.append( '<option value="0">' + allTownsText + '</option>' );
				}
			);
		}
	});
	
	// on sector change
	jQuery('#rw_sector').change(function() {
		 
		if( this.value == 0 )
		{
			jQuery('#rw_sub_sector')
				.empty()
				.append('<option value="0">' + allSubSectorsText + '</option>');
		}
		else
		{
			/*
			alert('/xpress/getSubSectorDropdownOptions.php?countryCode=' + jQuery('#location_country').val() 
						+ '&sectorID=' + jQuery('#rw_sector').val());*/
						
			jQuery('#rw_sub_sector')
				.empty()
				.append('<option value="">' + loadingText + '</option>')
				.attr('disabled', 'disabled');
			jQuery.ajax({ 
				url: baseURL + '/xpress/getSubSectorDropdownOptions.php?countryCode=' + jQuery('#location_country').val() 
						+ '&sectorID=' + jQuery('#rw_sector').val(), 
				success: function( data ) {
					jQuery('#rw_sub_sector')
						.removeAttr('disabled')
						.empty()
						.append( data );
				},
				error: function( request, status, error) {
					jQuery('#rw_sub_sector')
						.removeAttr('disabled')
						.empty()
						.append( '<option value="0">' + allSubSectorsText + '</option>' );
				}
			});
		}
	});
	
	// on UK town focus/blur
	jQuery('#location_town_uk').focus( function( eventObj ) {
		if( eventObj.target.value == enterTownNameText ){
			eventObj.target.value = '';
		}
	});
	jQuery('#location_town_uk').blur( function( eventObj ) {
		if( eventObj.target.value == '' ){
			eventObj.target.value = enterTownNameText;
		}
	});
	
	// initialise
	if( jQuery('#location_town_uk').val() == '' ){
		jQuery('#location_town_uk').val( enterTownNameText );
	}
});
