function getXmlNodeValue(obj){
	if (typeof(obj.innerText) != "undefined"){ return obj.innerText; } //should be safari and ie, but not working
	else if (typeof(obj.textContent) != "undefined"){ return obj.textContent; } //ff
	else { return obj.childNodes[0].nodeValue; }  //safari and ie, something weird
}

function updateTimesVolunteers(){
	var total_hours = 6;

	//disable all selects as a reset
	for(var hour=1; hour<total_hours; hour++){
		var opt=$('hour'+hour).options.length
		for (var i=opt; i >= 0; i--) {
			$('hour'+hour).options[i] = null;
		}		
		$('hour'+hour).disable();
	}

	new Ajax.Request("../util/updateTimesVolunteers.php?location="+$('location_hourly').value, {
	method: 'get',
	onSuccess: function(transport) {
		var newTimes = transport.responseXML.documentElement;
		var hasSlots = false;

		//print lesser of workable hours OR hours available to work
		for(var hour=1; hour<total_hours && hour<=newTimes.childNodes.length; hour++){
			
			//enable select
			$('hour'+hour).enable();
			var opt=$('hour'+hour).options.length
			for (var i=opt; i >= 0; i--) {
				$('hour'+hour).options[i] = null;
			}

			$('hour'+hour).options[0] = new Option("Select...","");
		
			for(var i=0;i<newTimes.childNodes.length;i++){
				var slot = newTimes.childNodes[i];
				var slotsRemaining = (parseInt(getXmlNodeValue(slot.childNodes[2]))) - 
										(parseInt(getXmlNodeValue(slot.childNodes[1])));	
				if(slotsRemaining > 0){
					$('hour'+hour).options[i+1] = new Option(getXmlNodeValue(slot.childNodes[0]),getXmlNodeValue(slot.childNodes[0]));
					//+ ' ----- '+slotsRemaining+' slots remaining',getXmlNodeValue(slot.childNodes[0])) ;

					hasSlots = true;
				}
			}
			if(!hasSlots){
				for(var hour=1; hour<total_hours; hour++){
					$('hour'+hour).options[1] = new Option("No slots available. Please select another location.","");
				}
			}
		}
  	},
	onFailure: function() {
		for(var hour=1; hour<total_hours; hour++){		
			$('hour'+hour).options[0] = new Option('Error: could not retrieve times from database','error');
			$('hour'+hour).disable();
		}
	}
	});
	return;
}