// populate a top-level select box such as make or state
function populateSelect(targetSelect, optionName, promptOption, preselect) {
	// optionName defaults to the name of the select box
	if (optionName == null) {
		optionName = targetSelect.name
	}
	
	// by default, the select is assumed to have a first option saying "Select something" but 
	// this parameter allows this to be disabled
	if (promptOption == null) {
		promptOption = true
	}

	// by default, do not preselect the user's setting for this property
	if (preselect == null) {
		preselect = false
	}

	if (promptOption) {
		promptOffset = 1
	} else {
		promptOffset = 0
	}

	// clear existing options except the first "Select a something" option
	targetSelect.options.length = promptOffset

	requestParams = {
		selectorName: optionName,
		preselect: preselect
	}

	// get new options list from server
	new Ajax.Request('execute/GetSelections', {
		method:'post',
		parameters: requestParams,
		
		onSuccess: function(transport){
			// put new options into the combo

			var response = transport.responseText.evalJSON();

			var n = response.options.size();
			for(i=0; i<n; i++) {
				var o = response.options[i];
				targetSelect.options[i+promptOffset] = new Option(o.label, o.value);
			}
			targetSelect.value = response.value
			
		},
		onFailure: function() { alert('failed to submit'); },
		asynchronous: false
	});	

}

// populate a select box depending on the value of a parent such as models for a given make
// populate the options of childSelect depending on the selected value of parentSelect
function populateChildSelect(childSelect, childOptionName, parentSelect, parentListName, promptOption, preselect) {
	
	// by default, do not preselect the user's setting for this property
	if (preselect == null) {
		preselect = false
	}

	if (promptOption) {
		promptOffset = 1
	} else {
		promptOffset = 0
	}

	// clear existing options except the first "Select a something" option
	childSelect.options.length = promptOffset;

	requestParams = {
		parentListName: parentListName,
		parentValue: parentSelect.value,
		childOptionName: childOptionName,
		preselect: preselect
	}

	// get new options list from server
	new Ajax.Request('execute/GetItemSelections', {
		method:'post',
		parameters: requestParams,
		
		onSuccess: function(transport){
			// put new options into the combo

			var response = transport.responseText.evalJSON();

			var n = response.options.size();
			for(i=0; i<n; i++) {
				var o = response.options[i];
				childSelect.options[i+promptOffset] = new Option(o.label, o.value);
			}
			childSelect.value = response.value
		},
		onFailure: function() { alert('failed to submit'); },
		asynchronous: false
	});	

}


