/**
 * Region javascript for volunteernow.org.nz
 *
 *  @author Nigel McNie
 */

var regions = new Array();


regions[1] = new Array(
	'All Auckland regions', 0,
	'Auckland City', 1,
	'Far North', 11,
	'Franklin District', 5,
	'Greater Auckland Region', 10,
	'Manukau City', 3,
	'North Shore City', 2,
	'Papakura District', 6,
	'Rodney District', 7,
	'The Gulf Islands', 9,
	'Waitakere City', 4,
	'Whangarei District', 8
);
regions[2] = new Array(
	'All Canterbury regions', 0,
	'Addington', 12,
	'Aranui', 13,
	'Ashburton', 14,
	'Avonhead', 15,
	'Banks Peninsula', 16,
	'Beckenham', 17,
	'Belfast', 18,
	'Bishopdale', 19,
	'Bromley', 20,
	'Burnside', 21,
	'Burwood', 22,
	'Canterbury North', 23,
	'Cashmere', 24,
	'Christchurch Central', 25,
	'Christchurch Various', 26,
	'Dallington', 27,
	'Fendalton', 28,
	'Governors Bay', 29,
	'Halswell', 30,
	'Harewood', 31,
	'Heathcote/Ferrymead', 32,
	'Hei Hei', 33,
	'Hoon Hay', 34,
	'Hornby', 35,
	'Ilam', 36,
	'Kaiapoi', 37,
	'Leeston', 126,
	'Lincoln', 109,
	'Linwood', 38,
	'Little River', 39,
	'Mairehau', 40,
	'Malvern', 127,
	'Merivale', 41,
	'Middleton', 42,
	'New Brighton', 43,
	'Opawa', 133,
	'Own Home', 44,
	'Papanui', 45,
	'Parklands', 46,
	'Prebbleton', 128,
	'Rangiora', 47,
	'Riccarton', 48,
	'Riccarton Upper', 49,
	'Richmond', 50,
	'Rolleston', 108,
	'Shirley', 51,
	'Sockburn', 52,
	'Somerfield', 53,
	'Spreydon', 54,
	'St Albans', 55,
	'St Martins', 56,
	'Sumner/Redcliffs', 57,
	'Sydenham', 58,
	'Templeton', 59,
	'Temuka', 60,
	'Timaru', 61,
	'Waimairi', 62,
	'Wainoni', 63,
	'Waltham', 64,
	'Woolston', 65,
	'Yaldhurst', 110
);
regions[10] = new Array(
	'All Hawkes Bay regions', 0,
	'Hastings', 106,
	'Napier', 105,
	'Taradale', 107
);
regions[11] = new Array(
	'All Mid and South Canterbury regions', 0,
	'Ashburton', 125,
	'Fairlie', 111,
	'Geraldine', 112,
	'Hinds', 113,
	'Mackenzie', 114,
	'Makikihi', 115,
	'Mt Cook', 116,
	'Pleasant Point', 117,
	'Rakaia', 118,
	'St Andrews', 119,
	'Tekapo', 120,
	'Temuka', 124,
	'Timaru', 123,
	'Twizel', 121,
	'Waimate', 122
);
regions[4] = new Array(
	'All Otago regions', 0,
	'Central Otago', 68,
	'Central Southland', 69,
	'Coastal Otago', 70,
	'Dunedin All Areas', 71,
	'Dunedin Central City', 72,
	'Dunedin North', 73,
	'Dunedin South', 74,
	'Fiordland', 75,
	'Gore/Eastern Southland', 76,
	'Invercargill', 77,
	'Mosgiel/Taieri', 78,
	'North Otago/Oamaru', 79,
	'Northern Southland', 80,
	'Queenstown', 81,
	'South Otago', 83,
	'Wanaka', 82,
	'Western Southland', 84
);
regions[8] = new Array(
	'All Wanganui regions', 0,
	'Bulls', 91,
	'Feilding', 130,
	'Hunterville', 92,
	'Marton', 93,
	'Marton', 131,
	'Ohakune', 94,
	'Palmerston North', 132,
	'Raetihi', 95,
	'Taihape', 96,
	'Taumaranui', 129,
	'Waiouru', 97,
	'Wanganui', 98
);
regions[3] = new Array(
	'All Wellington regions', 0,
	'Johnsonville', 104,
	'Kapiti', 103,
	'Lower Hutt', 100,
	'Porirua', 101,
	'Upper Hutt', 102,
	'Wellington City', 99
);
regions[5] = new Array(
	'All Western Bay of Plenty regions', 0,
	'Katikati', 85,
	'Mount Maunganui', 86,
	'Papamoa', 87,
	'Tauranga - Urban areas', 88,
	'Te Puke', 89,
	'WBoP - Rural areas', 90
);



function regionChanged (region_select)
{
	var subregion_select = document.getElementById('subregion_id');
	var region_id = region_select.options[region_select.selectedIndex].value;
	
    if (0 == region_id) {
    	// Default option "All Regions" chosen
        subregion_select.options.length = 0;
    } else if (999 == region_id) {
    	// Remove all subregion options
        subregion_select.options.length = 0;
    } else {
    	// A specific region has been chosen, so populate the subregion select with correct values

        subregion_select.options.length = 0;
        var list = regions[region_id];
        
        if (list) {
	        for (i = 0; i < list.length; i += 2) {
	        	subregion_select.options[i / 2] = new Option(list[i], list[i + 1]);
	        }
	    }
    }

	// Disable the subregion input if it has no options (unnecessary but good for user interface)    
    if (0 == subregion_select.options.length) {
    	subregion_select.disabled = "disabled";
    } else {
    	subregion_select.disabled = "";
    }
}

function initRegions (subregion_id)
{
	if (typeof(subregion_id) != "object") {
		var tmp = new Array();
		tmp[0] = subregion_id;
		subregion_id = tmp;
	}
	
	var region_select    = document.getElementById('region_id');
	var subregion_select = document.getElementById('subregion_id');
	
	if (region_select) {
		var region_id        = region_select.options[region_select.selectedIndex].value;
		
		regionChanged(region_select);
		
		// Now to set the correct subregion
		for (i in subregion_select.options) {
			for (j in subregion_id) {
				if (subregion_select.options[i].value == subregion_id[j]) {
					subregion_select.options[i].selected = "selected";
					break;
				}
			}
		}
	}
}

