$(document).ready(function()
{
  
  // Also needs updating in region_dropdown_options.html and county_dropdown_options.html
  var map = {
    'Derbyshire'                    : 'East Midlands',
    'Leicestershire'                : 'East Midlands',
    'Lincolnshire'                  : 'East Midlands',
    'Northamptonshire'              : 'East Midlands',
    'Nottinghamshire'               : 'East Midlands',
    'Bedfordshire'                  : 'Eastern',
    'Cambridgeshire'                : 'Eastern',
    'Essex'                         : 'Eastern',
    'Hertfordshire'                 : 'Eastern',
    'Norfolk'                       : 'Eastern',
    'Suffolk'                       : 'Eastern',
    'Greater London'                : 'London',
    'Durham'                        : 'North East',
    'Northumberland'                : 'North East',
    'Tyne and Wear'                 : 'North East',
    'Cheshire'                      : 'North West',
    'Cumbria'                       : 'North West',
    'Lancashire'                    : 'North West',
    'Greater Manchester'            : 'North West',
    'Merseyside'                    : 'North West',
    'Berkshire'                     : 'South East',
    'Buckinghamshire'               : 'South East',
    'East Sussex'                   : 'South East',
    'Hampshire'                     : 'South East',
    'Kent'                          : 'South East',
    'Oxfordshire'                   : 'South East',
    'Surrey'                        : 'South East',
    'West Sussex'                   : 'South East',
    'Cornwall and Isles of Scilly'  : 'South West',
    'Devon'                         : 'South West',
    'Dorset'                        : 'South West',
    'Gloucestershire'               : 'South West',
    'Somerset'                      : 'South West',
    'Wiltshire'                     : 'South West',
    'Herefordshire'                 : 'West Midlands',
    'Shropshire'                    : 'West Midlands',
    'Staffordshire'                 : 'West Midlands',
    'Warwickshire'                  : 'West Midlands',
    'West Midlands'                 : 'West Midlands',
    'Worcestershire'                : 'West Midlands',
    'East Yorkshire'                : 'Yorkshire and the Humber',
    'North Yorkshire'               : 'Yorkshire and the Humber',
    'South Yorkshire'               : 'Yorkshire and the Humber',
    'West Yorkshire'                : 'Yorkshire and the Humber',
    'Aberdeenshire'                 : 'Scotland',
    'Angus'                         : 'Scotland',
    'Argyll'                        : 'Scotland',
    'Ayrshire'                      : 'Scotland',
    'Banffshire'                    : 'Scotland',
    'Berwickshire'                  : 'Scotland',
    'Bute'                          : 'Scotland',
    'Caithness'                     : 'Scotland',
    'Clackmannanshire'              : 'Scotland',
    'Dumfriesshire'                 : 'Scotland',
    'Dumbartonshire'                : 'Scotland',
    'East Lothian'                  : 'Scotland',
    'Fife'                          : 'Scotland',
    'Inverness'                     : 'Scotland',
    'Kincardineshire'               : 'Scotland',
    'Kinross-shire'                 : 'Scotland',
    'Kirkcudbrightshire'            : 'Scotland',
    'Lanarkshire'                   : 'Scotland',
    'Midlothian'                    : 'Scotland',
    'Moray'                         : 'Scotland',
    'Nairnshire'                    : 'Scotland',
    'Orkney'                        : 'Scotland',
    'Peebleshire'                   : 'Scotland',
    'Perthshire'                    : 'Scotland',
    'Renfrewshire'                  : 'Scotland',
    'Ross and Cromarty'             : 'Scotland',
    'Roxburghshire'                 : 'Scotland',
    'Selkirkshire'                  : 'Scotland',
    'Shetland'                      : 'Scotland',
    'Stirlingshire'                 : 'Scotland',
    'Sutherland'                    : 'Scotland',
    'Wigtownshire'                  : 'Scotland',
    'West Lothian'                  : 'Scotland',
    'Anglesey'                      : 'Wales',
    'Blaenau Gwent'                 : 'Wales',
    'Bridgend'                      : 'Wales',
    'Caernarvonshire'               : 'Wales',
    'Caerphilly'                    : 'Wales',
    'Cardiff'                       : 'Wales',
    'Carmarthenshire'               : 'Wales',
    'Ceredigion'                    : 'Wales',
    'Conwy'                         : 'Wales',
    'Denbighshire'                  : 'Wales',
    'Flintshire'                    : 'Wales',
    'Glamorgan'                     : 'Wales',
    'Gwynedd'                       : 'Wales',
    'Merthyr Tydfil'                : 'Wales',
    'Monmouthshire'                 : 'Wales',
    'Neath Port Talbot'             : 'Wales',
    'Newport'                       : 'Wales',
    'Pembrokeshire'                 : 'Wales',
    'Powys'                         : 'Wales',
    'Rhondda Cynon Taff'            : 'Wales',
    'Swansea'                       : 'Wales',
    'Torfaen'                       : 'Wales',
    'Wrexham'                       : 'Wales',
    'County Antrim'                 : 'Northern Ireland',
    'County Down'                   : 'Northern Ireland',
    'County Armagh'                 : 'Northern Ireland',
    'County Londonderry'            : 'Northern Ireland',
    'County Fermanagh'              : 'Northern Ireland',
    'County Tyrone'                 : 'Northern Ireland',
    'Carlow'                        : 'Republic of Ireland',
    'Cavan'                         : 'Republic of Ireland',
    'Clare'                         : 'Republic of Ireland',
    'Cork'                          : 'Republic of Ireland',
    'Donegal'                       : 'Republic of Ireland',
    'Dun Laoghaire-Rathdown'        : 'Republic of Ireland',
    'Fingal'                        : 'Republic of Ireland',
    'Galway'                        : 'Republic of Ireland',
    'Kerry'                         : 'Republic of Ireland',
    'Kildare'                       : 'Republic of Ireland',
    'Kilkenny'                      : 'Republic of Ireland',
    'Laois'                         : 'Republic of Ireland',
    'Leitrim'                       : 'Republic of Ireland',
    'Limerick'                      : 'Republic of Ireland',
    'Longford'                      : 'Republic of Ireland',
    'Louth'                         : 'Republic of Ireland',
    'Mayo'                          : 'Republic of Ireland',
    'Meath'                         : 'Republic of Ireland',
    'Monaghan'                      : 'Republic of Ireland',
    'North Tipperary'               : 'Republic of Ireland',
    'Offaly'                        : 'Republic of Ireland',
    'Roscommon'                     : 'Republic of Ireland',
    'Sligo'                         : 'Republic of Ireland',
    'South Dublin'                  : 'Republic of Ireland',
    'South Tipperary'               : 'Republic of Ireland',
    'Waterford'                     : 'Republic of Ireland',
    'Westmeath'                     : 'Republic of Ireland',
    'Wexford'                       : 'Republic of Ireland',
    'Wicklow'                       : 'Republic of Ireland',
    'All Republic of Ireland'       : 'Republic of Ireland',
    'All UK and RoI'                : 'All UK and RoI',
    'Mainland Europe'               : 'Mainland Europe',
    'Rest of World'                 : 'Rest of World'
  }
  
  var regionSelect = $("select[name=region]");
  var countySelect = $("select[name=county]");
  
  var testMode = false;
  if (testMode)
  {
    regionSelect.attr("disabled", true);
  }
  else
  {
    regionSelect
      .parent().hide() // Hide the <div>
      .prev().hide();  // Hide the <label>
  }
  
  function autoRegion()
  {
    var county = countySelect.val();
    var region = (map[county] ? map[county] : "");
    regionSelect.val(region);
  }
  
  countySelect.bind("change", autoRegion);
  autoRegion();
  
  // If there's an error message because region is blank, it doesn't make
  // sense to show it since they can't see that field!
  var error = $("#add-error");
  var errorText = error.text().replace(/region/g, "county").replace(/Region/g, "County");
  error.text(errorText);
  
});

