/*
 * Requires commondom.js, httpRequest.js
 */

/*
 * updateTarget element
 */
var updateTarget;

/*
 * Update the options of a select field from the selection of another
 *
 * @param element src Source element of change
 * @param string dest Destination element name
 * @param string url URI of web service providing result
 */
function updateOptions(src, dest, url) {

	var selected = src.value;

	updateTarget = gel(document, dest);


	if (updateTarget) {
		request = httpRequest(url, 'POST', 'updateHandler', 'value='+selected);
	}
}

/*
 * XmlHttprequest respose handler
 * Empties Options of target select element, and adds new child options
 *
 * @param XmlHttpRequest o Request object
 */
function updateHandler(o) {

	if (o) {
		var options = o.responseText.split('|');

		// Empty current options
		while(updateTarget.hasChildNodes()){
			updateTarget.removeChild(updateTarget.lastChild);
		}

		for (var i=0; i<options.length; i++) {
			var optionData = options[i].split(',');
			var oOption = document.createElement('option');
			oOption.value = optionData[0];
			oOption.innerHTML = optionData[1];
			updateTarget.appendChild(oOption);
			oOption = null;
		}

	}
}