// http://www.tek-tips.com/faqs.cfm?fid=4766
function clearSelect (e) {
	var objSelect = document.getElementById(e);
	while (objSelect.options.length > 1) {
		objSelect.remove(1);
	}
	return objSelect;
}

function createOption (e, newValue, newText) {
	var objSelect = document.getElementById(e);
	var objOption = document.createElement("option");
	objOption.text = newText
	objOption.value = newValue
	
	if(document.all && !window.opera)
	  {objSelect.add(objOption);}
	 else
	  {objSelect.add(objOption, null);};
	
}  

function updateTimeDropdownPPC (officeID, dateID, timeID, spinnerID) {
	// get dropdowns
	var officeSelect = document.getElementById(officeID).value;
	var dateSelect = document.getElementById(dateID).value;

	// hide time dropdown and show spinnner
	var timeSelect = document.getElementById(timeID).style.display = "none";
	var spinner = document.getElementById(spinnerID).style.display = "inline";
	
	// call AJAX
	// get back open and close hours for this date
	jQuery.post("/js/lookupOfficeHours.php", { officeID: officeSelect, date: dateSelect },
		function (data) {
			//alert ("Data returned: " + data);
			openHr = data.openHr;
			closeHr = data.closeHr;
			closeHrStart = data.closeHrStart;
			// pass to function that updates the time drop down 
			// (can't "remember" open/close times outside of this function and script runs beyond this function
			// before ajax has returned the open/close times from the database.
			getErDone (openHr, closeHr, closeHrStart, 'timeSpecial', 'ppcSpinner');
		}, "json");
}

function updateTimeDropdownSidebar (officeID, dateID, timeID, spinnerID) {
	// get dropdowns
	var officeSelect = document.getElementById(officeID).value;
	var dateSelect = document.getElementById(dateID).value;

	// hide time dropdown and show spinnner
	var timeSelect = document.getElementById(timeID).style.display = "none";
	var spinner = document.getElementById(spinnerID).style.display = "inline";
	
	// call AJAX
	// get back open and close hours for this date
	jQuery.post("/js/lookupOfficeHours.php", { officeID: officeSelect, date: dateSelect },
		function (data) {
			//alert ("Data returned: " + data);
			openHr = data.openHr;
			closeHr = data.closeHr;
			closeHrStart = data.closeHrStart;
			// pass to function that updates the time drop down 
			// (can't "remember" open/close times outside of this function and script runs beyond this function
			// before ajax has returned the open/close times from the database.
			getErDone (openHr, closeHr, closeHrStart, 'sidebarTime', 'sidebarSpinner');
		}, "json");
}

function updateTimeDropdownOfficePage (officeIDhidden, dateID, timeID, spinnerID) {
	// get dropdowns
	var dateSelect = document.getElementById(dateID).value;

	// hide time dropdown and show spinnner
	var timeSelect = document.getElementById(timeID).style.display = "none";
	var spinner = document.getElementById(spinnerID).style.display = "inline";
	
	// call AJAX
	// get back open and close hours for this date
	jQuery.post("/js/lookupOfficeHours.php", { officeID: officeIDhidden, date: dateSelect },
		function (data) {
			//alert ("Data returned: " + data);
			openHr = data.openHr;
			closeHr = data.closeHr;
			closeHrStart = data.closeHrStart;
			// pass to function that updates the time drop down 
			// (can't "remember" open/close times outside of this function and script runs beyond this function
			// before ajax has returned the open/close times from the database.
			getErDone (openHr, closeHr, closeHrStart, 'timeOffice', 'officePageSpinner');
		}, "json");
}

function updateTimeDropdownVideo (dateID, timeID, spinnerID) {
	// get dropdowns
	var dateSelect = document.getElementById(dateID).value;

	// hide time dropdown and show spinnner
	var timeSelect = document.getElementById(timeID).style.display = "none";
	var spinner = document.getElementById(spinnerID).style.display = "inline";
	
	// call AJAX
	// get back open and close hours for this date
	jQuery.post("/js/lookupOfficeHours.php", { officeID: 'Video Conference', date: dateSelect },
		function (data) {
			//alert ("Data returned: " + data);
			openHr = data.openHr;
			closeHr = data.closeHr;
			closeHrStart = data.closeHrStart;
			// pass to function that updates the time drop down 
			// (can't "remember" open/close times outside of this function and script runs beyond this function
			// before ajax has returned the open/close times from the database.
			getErDone (openHr, closeHr, closeHrStart, 'timeVideo', 'videoPageSpinner');
		}, "json");
}

function getErDone (openHr, closeHr, closeHrStart, timeID, spinnerID) {
	// now clear existing time options
	clearSelect (timeID);
	
	// process -1 values
	// both -1 = closed on this day
	// set open = 13 to skip am, close = 0 to skip pm
	if (openHr == -1 && closeHr == -1) {
		createOption (timeID, '', 'Closed');
		openHr = 13;
		closeHr = 0;
	}
	// open -1 == closed during the am
	else if (openHr == -1) {
		openHr = 13;
	}
	// closed -1 == closed during the pm
	else if (closeHr == -1) {
		closeHr = 0;
	}

	// special case 8.5 = 8:30
	//var openI = 8.5;
	var openStr = openHr.toString();	// so we can use match()
	if (openStr.match(".5")) {			// includes .5?
		openInt = parseInt(openStr);	// convert to int, will round down
		time = openInt + ':30 AM';		// add ':30 AM' so 8 becomes 8:30 AM
		createOption (timeID, time, time);
	}
	// round to decimals to ints so our drop down values look good
	openHr = Math.ceil(openHr);

	
	// and add new ones
	// am times
	for (i=openHr; i<=12; i++) {
		for (j=0; j<=30; j+=30) {
			// build value for easy selected logic
			time = i + ':' + j;
			time += (j==0) ? '0' : '';
			time += (i==12) ? ' PM' : ' AM';
			
			createOption (timeID, time, time);
		}
	}
	// pm times
	for (i=closeHrStart; i<=closeHr; i++) {
		for (j=0; j<=30; j+=30) {
			// don't include final X:30 PM
			if (i==closeHr && j > 0) break;
			
			// build value for easy selected logic
			time = i + ':' + j;
			time += (j==0) ? '0' : '';
			time += ' PM';
			
			createOption (timeID, time, time);
		}
	}

	// hide spinner, show updated drop down
	var spinner = document.getElementById(spinnerID).style.display = "none";
	var timeSelect = document.getElementById(timeID).style.display = "inline";
}