jQuery.noConflict();

// Construct 2D array of counties (keys are country names)
var counties = new Array();

counties["England"] = new Array("Avon","Bedfordshire","Berkshire","Buckinghamshire","Cambridgeshire","Cheshire","Cleveland","Cornwall","Cumbria","Derbyshire","Devon","Dorset","Durham","East Sussex","Essex","Gloucestershire","Hampshire","Herefordshire","Hertfordshire","Isle of Wight","Kent","Lancashire","Leicestershire","Lincolnshire","London","Merseyside","Middlesex","Norfolk","Northamptonshire","Northumberland","North Humberside","North Yorkshire","Nottinghamshire","Oxfordshire","Rutland","Shropshire","Somerset","South Humberside","South Yorkshire","Staffordshire","Suffolk","Surrey","Tyne and Wear","Warwickshire","West Midlands","West Sussex","West Yorkshire","Wiltshire","Worcestershire");
counties["Northern Ireland"] = new Array("Antrim","Armagh","Down","Fermanagh","Londonderry","Tyrone");
counties["Scotland"] = new Array("Aberdeenshire","Angus","Argyll","Ayrshire","Banffshire","Berwickshire","Bute","Caithness","Clackmannanshire","Dumfriesshire","Dunbartonshire","East Lothian","Fife","Inverness-shire","Kincardineshire","Kinross-shire","Kirkcudbrightshire","Lanarkshire","Midlothian","Moray","Nairnshire","Orkney","Peeblesshire","Perthshire","Renfrewshire","Ross-shire","Roxburghshire","Selkirkshire","Shetland","Stirlingshire","Sutherland","West Lothian","Wigtownshire");
counties["Wales"] = new Array("Clwyd","Dyfed","Gwent","Gwynedd","Mid Glamorgan","Powys","South Glamorgan","West Glamorgan");

// Loads relevant counties into the County box
function loadCounties(countryName)
{
    // Remove existing counties
    jQuery("select#county").children().remove();

    // Check the given country name has counties
    if (counties[countryName])
    {
        jQuery("select#county").append("<option>Please choose...</option>")

        jQuery.each(counties[countryName], function(idx, val) {
            jQuery("select#county").append("<option>" + val + "</option>");
        });

        jQuery("select#county").attr("disabled", "");
    }
    else
    {
        jQuery("select#county").append("<option>non-UK</option>");
        jQuery("select#county").attr("disabled", "disabled");
    }
}

jQuery(document).ready(function() {
    jQuery("select#country").change(function() {
        loadCounties(jQuery("select#country option:selected").html());
    });
});
