/* ************************************************************************
 * Functions for population of select options (drop-down values) dynamically
 * based on another element.
 *************************************************************************** 
 */
	
	var pleaseSelectStr = '----------- Please select ----------';

	function refreshSubject(subjectPrefixElem) { 
		var subjectElem = document.getElementById("subject");
		var selected = subjectPrefixElem.options[subjectPrefixElem.selectedIndex].value;
		document.getElementById("subject").value = selected + document.getElementById("subject_suffix").value;
	}

	function holdBranchSelection(idx) {
		document.getElementById("branchChosenIndex").value = idx;
	}
	
	/* 
	 * Taking a county selection as input, determines the appropriate set of branches
	 * for a branches drop-down.
	 * This depends on the id of the branch selection being "branch".
	 * Sample usage: 
	 *   &lt;select name="text05_location" id="county" onchange="javascript:refreshBranchOptions(this)"&gt; 
	 *	   &lt;option value='empty'&gt;----------- Please select ----------&lt;/option&gt;
	 *	   &lt;option value='carlow'&gt;Carlow&lt;/option&gt;
	 *	   &lt;option value='cavan'&gt;Cavan&lt;/option&gt;
	 *     ...
	 *	   &lt;option value='wicklow'&gt;Wicklow&lt;/option&gt;
	 *   &lt;/select&gt;
	 * 	 
	 *   &lt;select name="text06_local_branch" id="branch"&gt;
	 *	   &lt;option value='empty'&gt;----------- Please select ----------&lt;/option&gt;
	 *   &lt;/select&gt;
	 */
	function refreshBranchOptions(countyElem) { 
		var branchElem = document.getElementById("branch");
		var selected = countyElem.options[countyElem.selectedIndex].value;
		var branchArray = decideBranchArray(selected);
		while (branchArray.length < branchElem.options.length) { 
			branchElem.options[(branchElem.options.length - 1)] = null; 
		}
		
		var option = "", optionValue = "", newOptionStr = "";
		var isDefault = false, isSelected=false;
		for (var i=0; i < branchArray.length; i++) { 
			option = "'" + branchArray[i] + "'";
			// Include an option value in the new Option() call for later comparisons
			optionValue = (branchArray[i]==pleaseSelectStr) ? "empty" : removeSpaces(branchArray[i]);
			optionValue = "'" + optionValue + "'";
			isDefault = (i==0);
			isSelected = (document.getElementById("branchChosenIndex").value == i);
			newOptionStr = "new Option(" + option + ", " + optionValue + ", " + isDefault + ", " + isSelected + ")";
			//eval("branchElem.options[i]=" + "new Option(" + option + ", " + optionValue + ")"); 
			eval("branchElem.options[i]=" + newOptionStr); 
		} 
	}
	
	/* 
	 * Builds array of branches for specified county.
	 */
	function decideBranchArray(countyName){
		// eval(selected + "Array");
		var branchArray;
		
		switch (countyName) {
			case "carlow" :
				branchArray =  new Array(pleaseSelectStr, 
					"Carlow Town", 
					"Kilkenny");
				break;
			case "cavan" :
				branchArray =  new Array(pleaseSelectStr, 
					"Cavan Town", 
					"Kells", 
					"Monaghan Town", 
					"Dundalk");
				break;
			case "clare" :
				branchArray =  new Array(pleaseSelectStr, 
					"Ennis", 
					"Nenagh", 
					"Limerick");
				break;
			case "cork" :
				branchArray =  new Array(pleaseSelectStr, 
					"Cork City", 
					"Clonakilty", 
					"Fermoy", 
					"Macroom", 
					"Mallow", 
					"Midleton", 
					"Skibbereen", 
					"Newcastlewest", 
					"Killarney");
				break;
			case "donegal" :
				branchArray =  new Array(pleaseSelectStr,  
					"Letterkenny", 
					"Sligo Town");
				break;
			case "dublin" :
				branchArray =  new Array(pleaseSelectStr, 
					"Bluebell-Dublin 12", 
					"Naas", 
					"Trim", 
					"Wicklow");
				break;
			case "galway" :
				branchArray =  new Array(pleaseSelectStr, 
					"Galway City", 
					"Athenry", 
					"Castlebar", 
					"Athlone", 
					"Roscommon");
				break;
			case "kerry" :
				branchArray =  new Array(pleaseSelectStr, 
					"Killarney", 
					"Tralee", 
					"Newcastlewest");
				break;
			case "kildare" :
				branchArray =  new Array(pleaseSelectStr, 
					"Naas", 
					"Portlaoise", 
					"Bluebell-Dublin 12");
				break;
			case "kilkenny" :
				branchArray =  new Array(pleaseSelectStr, 
					"Kilkenny Town", 
					"Waterford", 
					"Carlow Town", 
					"Clonmel");
				break;
			case "laois" :
				branchArray =  new Array(pleaseSelectStr, 
					"Portlaoise", 
					"Tullamore", 
					"Carlow");
				break;
			case "leitrim" :
				branchArray =  new Array(pleaseSelectStr, 
					"Cavan", 
					"Sligo",
					"Mullingar");
				break;
			case "limerick" :
				branchArray =  new Array(pleaseSelectStr, 
					"Limerick City", 
					"Newcastlewest", 
					"Ennis", 
					"Nenagh");
				break;
			case "longford" :
				branchArray =  new Array(pleaseSelectStr, 
					"Mullingar", 
					"Cavan", 
					"Athlone", 
					"Roscommon");
				break;
			case "louth" :
				branchArray =  new Array(pleaseSelectStr, 
					"Drogheda", 
					"Dundalk", 
					"Kells", 
					"Bluebell-Dublin 12");
				break;
			case "mayo" :
				branchArray =  new Array(pleaseSelectStr, 
					"Castlebar", 
					"Galway", 
					"Roscommon");
				break;
			case "meath" :
				branchArray =  new Array(pleaseSelectStr, 
					"Navan", 
					"Drogheda", 
					"Naas", 
					"Mullingar");
				break;
			case "monaghan" :
				branchArray =  new Array(pleaseSelectStr, 
					"Monaghan Town", 
					"Dundalk", 
					"Cavan");
				break;
			case "offaly" :
				branchArray =  new Array(pleaseSelectStr, 
					"Tullamore", 
					"Portlaoise", 
					"Mullingar", 
					"Athlone", 
					"Nenagh");
				break;
			case "roscommon" :
				branchArray =  new Array(pleaseSelectStr, 
					"Roscommon Town", 
					"Athlone", 
					"Castlebar", 
					"Athenry");
				break;
			case "sligo" :
				branchArray =  new Array(pleaseSelectStr, 
					"Sligo Town", 
					"Castlebar");
				break;
			case "tipperary" :
				branchArray =  new Array(pleaseSelectStr, 
					"Clonmel", 
					"Nenagh", 
					"Thurles", 
					"Limerick", 
					"Tullamore", 
					"Athlone", 
					"Ennis");
				break;
			case "waterford" :
				branchArray =  new Array(pleaseSelectStr, 
					"Waterford City", 
					"Clonmel", 
					"Midletown");
				break;
			case "westmeath" :
				branchArray =  new Array(pleaseSelectStr, 
					"Athlone", 
					"Mullingar", 
					"Trim", 
					"Kells", 
					"Tullamore", 
					"Roscommon");
				break;
			case "wexford" :
				branchArray =  new Array(pleaseSelectStr, 
					"Wexford Town", 
					"Wicklow", 
					"Waterford", 
					"Carlow", 
					"Kilkenny");
				break;
			case "wicklow" :
				branchArray =  new Array(pleaseSelectStr, 
					"Wicklow Town", 
					"Bluebell-Dublin12", 
					"Carlow", 
					"Naas", 
					"Enniscorthy");
				break;
			default : 
				branchArray =  new Array(pleaseSelectStr);			
		}
		return branchArray;
	}
	
