 // initialize Linked Select Elements on load.
function init(){
	
	from = document.getElementById('country');
	to = document.getElementById('countySelect');
	countyText = document.getElementById('county');
	chooseText = "** PLEASE CHOOSE **";

	var options = new Array();
	(from.style || from).visibility = "visible";
	for (var i=0; i < to.options.length; i++) {
		options[i] = new Array(to.options[i].text,to.options[i].value);
	}
	
	from.onchange = function() {
		var fromCode = from.options[from.selectedIndex].value;
		to.options.length = 0;
					
		
		// count how many matching counties there are
		var noofcounties = 0;
		for (i=0; i < options.length; i++) {
			var countryCode = fromCode + "-"; 
			var countryCodeLength = countryCode.length;		
			if (options[i][1].substr(0,countryCodeLength) == countryCode) {
			   noofcounties++;
			 } 
		} 
		
		if (noofcounties == 0){
			// If there are no counties to show, hide the select and show the counties text field
			to.style.display = 'none';
			countyText.style.display = 'block';
			document.getElementById('countryHasMultipleCounties').value = "no";
			
			
		} else if (noofcounties > 0){
			to.style.display = 'block';
			countyText.style.display = 'none';
			document.getElementById('countryHasMultipleCounties').value = "yes";
			
			to.options[to.options.length] = new Option(chooseText,"");
		}
		
		
		// Go through all the counties, if they match the countries, insert em
		for (i=0; i < options.length; i++) {
			
			countryCode = fromCode + "-"; 
			countryCodeLength = countryCode.length;
						
			if (options[i][1].substr(0,countryCodeLength) == countryCode) {
			   to.options[to.options.length] = new Option(options[i][0],options[i][1]);
			 }
			 
		} 
		 
		if(to.options.length > 0) {
			 to.options[0].selected = true;
		}
	};
		 from.onchange();

 }
 
 
 function submitregform(){
	 if(document.getElementById('countryHasMultipleCounties').value == "yes"){

		var w = to.selectedIndex;
		var selected_text = to.options[w].text;
		 
		if(selected_text == chooseText){
			selected_text = "";
		}
			countyText.value = selected_text;
		
		// alert(countyText.value);
	}
 }
