
$(document).ready(function() {
		
	$('select#country').displayFlag();	
	
});

$.fn.displayFlag = function() {
	return this.each(function() {
		if(this.selectedIndex > 0) {
			$('span.flag').show();
			var flag = $("select#country option:selected").attr("class").toLowerCase();
			var countryName = $("select#country option:selected").attr("value");
			$("span.flag").html('<img class="' + flag + '" src="/i/forms/flags/' + flag + '.gif" alt="" title="' + countryName + ' Flag" onError="$(this).remove();" />');
		} else if (this.selectedIndex == 0) {
			$('span.flag').hide();
		};
	})
	.change(function() {
		if(this.selectedIndex > 0) {
			$('span.flag').show();
			var flag = $("select#country option:selected").attr("class").toLowerCase();
			var countryName = $("select#country option:selected").attr("value");
			$("span.flag").html('<img class="' + flag + '" src="/i/forms/flags/' + flag + '.gif" alt="" title="' + countryName + ' Flag" onError="$(this).remove();" />');
		} else if (this.selectedIndex == 0) {
			$('span.flag').hide();
		};
	});
};


/*w
$.fn.displayFlag = function() {
	return this.each(function() {
		if(this.selectedIndex > 0) { // if a coutry is selected
			$('span.flag').show();
			var flag = $("select#country option:selected").attr("class").toLowerCase();
			var countryName = $("select#country option:selected").attr("value");
			$("span.flag").html('<img class="' + flag + '" src="http://matthewbranthwaite.co.uk/projects/skischools/contact/forms/flags/' + flag + '.gif" alt="" title="' + countryName + ' Flag" />');
		} else if (this.selectedIndex == 0) { // if the default option is selected
			$('span.flag').hide(); // remove the flag
		};
	})
	.change(function() {
		if(this.selectedIndex > 0) {
			$('span.flag').show();
			var flag = $("select#country option:selected").attr("class").toLowerCase();
			var countryName = $("select#country option:selected").attr("value");
			$("span.flag").html('<img class="' + flag + '" src="http://matthewbranthwaite.co.uk/projects/skischools/contact/forms/flags/' + flag + '.gif" alt="" title="' + countryName + ' Flag" />');
		} else if (this.selectedIndex == 0) {
			$('span.flag').hide();
		};
	});
};
*/
