// 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 updateTimeDropdown (page, officeID, dateID, timeID, spinnerID) {
	// page will be Office or Video or EmailAppt, etc.  
	// PHP logic will figure out the appropriate session 
	// variable to lookup for remembering the selected option
	
	// get dropdowns
	var dateSelect = document.getElementById(dateID).value;

	// special cases require office value lookup
	if (officeID == 'officeEmailAppt') {
		officeID = document.getElementById('officeEmailAppt').value;
	} else if (officeID == 'officeSpecial') {
		officeID = document.getElementById('officeSpecial').value;
	} else if (officeID == 'sidebarOffice') {
		officeID = document.getElementById('sidebarOffice').value;
	}
	
	// set these field names as globals so we can pass them to getErDone
	fieldTime = timeID;
	fieldSpinner = spinnerID;

	// hide time dropdown and show spinnner
	document.getElementById(timeID).style.display = "none";
	document.getElementById(spinnerID).style.display = "inline";
	
	// call AJAX, get back open and close hours for this date
	jQuery.post("/js/lookupOfficeHours.php", { formPage: page, officeID: officeID, date: dateSelect },
		function (data) {
			//alert ("Data returned: " + data);
			openHr = data.openHr;
			closeHr = data.closeHr;
			closeHrStart = data.closeHrStart;
			selected = data.selected;
			// 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, fieldTime, fieldSpinner, selected);
		
		}, "json");
}


function getErDone (openHr, closeHr, closeHrStart, timeID, spinnerID, selectedVal) {
	// clear existing time options
	clearSelect (timeID);
	
	// process negative values
	// set open = 13 to skip am, close = 0 to skip pm
	if (openHr == -5 && closeHr == -5) {
		createOption (timeID, '', 'Select an Office');
		openHr = 13;
		closeHr = 0;
		
	} else if (openHr == -4 && closeHr == -4) {
		createOption (timeID, '', 'Select a Date');
		openHr = 13;
		closeHr = 0;
		
	} else if (openHr == -3 && closeHr == -3) {
		createOption (timeID, '', 'Invalid Date');
		openHr = 13;
		closeHr = 0;
		
	} else if (openHr == -2 && closeHr == -2) {
		createOption (timeID, '', 'By Appointment Only');
		openHr = 13;
		closeHr = 0;
		
	} else if (openHr == -1 && closeHr == -1) {
		createOption (timeID, '', 'Closed');
		openHr = 13;
		closeHr = 0;
		
	} else if (openHr == -1) {
		openHr = 13;
		
	} else if (closeHr == -1) {
		closeHr = 0;
	}

	// special case 8.5 = 8:30
	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);

	// add options
	// 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);
		}
	}

	// select option if user has already chosen time
	var selTime = document.getElementById(timeID);
	for (var x=0; x<selTime.length; x++) {
		if (selTime.options[x].text == selectedVal) {
			selTime.selectedIndex = x;
			break;
		} 
	}

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