// Storing the current advisors for each appointment
// var apptStates = new Array(100);
var apptStates = getApptStatesFromServer();

// Information about the advisor currently making selections
var nameInDB = "";
var name = "";
var room = "";

// Allows or prevents changes to the appointments
var lock = false;

var days = ["monday", "tuesday", "wednesday", "thursday"];

/* Parallel arrays to match the name stored in the database table
   to the correct class name and display information for the advisor */
var advisorNamesInDB = ["LHanson", "HHarris", "TBattles", "no"];
var advisorClassNames = ["linda", "hal", "tommy", ""];
var advisorRooms = ["Dobyns - 426", "Harris - 433", "Battles - 427", "Available"];

// Determine number of days by length of array -- CHANGE THIS FOR DB
var dayTotal = days.length;
	
// One set of blocks on the hour, one set on the half-hour
var blocksPerHour = dayTotal * 2;

// Temporary function to initialize all appointments to be available
function fillApptStates(){
	for(var i = 0; i < apptStates.length; i++)
		apptStates[i] = "no";
}

/* This function will translate from the field value in the database
   to the class and room names I use in the HTML and CSS */
function lookupAdvisor(anAdvisor){
	var found = false;
	for(var i = 0; i < advisorNamesInDB.length; i++) {
		if(advisorNamesInDB[i] == anAdvisor) {
			found = true;
			nameInDB = advisorNamesInDB[i];
			name = advisorClassNames[i];
			room = advisorRooms[i];
			i = advisorNamesInDB.length;
		}
	}
	if(!found){
		name = "";
		room = "Available";
	}
}

/* This function clears the style from one of the advisor selection
   spans at the top */
function resetApptSelect(aPersonSelect){
	document.getElementById(aPersonSelect).className = "apptSelect";
}

/* This function clears the style from all of the advisor selection
   spans at the top */
function resetAll(){
	resetApptSelect("lindaSelect");
	resetApptSelect("halSelect");
	resetApptSelect("tommySelect");
}

/* This function performs the change from advisor to another when
   selecting advising blocks */
function setName(theSpan, anAdvisor){
	if(anAdvisor != nameInDB){
		lookupAdvisor(anAdvisor);
		resetAll();
		if(name != "")
			theSpan.className += " " + name;
	}
}

// This function responds to a click on an appointment block
function changeDiv(aDiv, day, time, id){
	// Check to see if changes are allowed and if an advisor is working
	if(!lock && name != ""){
		// If the appointment is available, set it for the advisor
		if(apptStates[id] == "no"){
			aDiv.className += " " + name;
			aDiv.innerHTML = room;
			apptStates[id] = nameInDB;
		}
		// If the appointment is currently set for the advisor, clear it
		else if(apptStates[id] == nameInDB){
			aDiv.className = "appt " + day + " " + time;
			aDiv.innerHTML = "Available";
			apptStates[id] = "no";
		}
		// If the appointment is set for another advisor, warn the user
		else{
			alert("You cannot change another advisor's times!");
		}
	}
}

// This function creates a div block for an initial appointment state
function showOneAppt(day, time, apptID){
	document.write("<div class='appt " + day + " " + time);
	if(nameInDB != "no")
		document.write(" " + name);
	document.write("' ");
	document.write("id='" + day + time + "' ");
	document.write("onclick='changeDiv(this, \"" + day + "\", \"" + time +
				   "\", " + apptID + ");'>");
	document.write("Available</div>");
}

function getAmOrPm(block){
	var amOrPm;
	
	// The first four hours are a.m.
	var morningBlocks = blocksPerHour * 4;

	if(block < morningBlocks)
		amOrPm = "am";
	else
		amOrPm = "pm";
	
	return amOrPm;
}

function getHour(block){
	var hour;
	
	// The first five hours count up from 8; the last start at 1
	var earlyBlocks = blocksPerHour * 5;
	
	// Determine the hour based on before 1 or after 1
	if(block < earlyBlocks)
		hour = 8 + Math.floor(block / blocksPerHour);
	else
		hour = 1 + Math.floor((block - earlyBlocks) / blocksPerHour);
		
	return hour;
}

function getMinute(block){
	var minute;
	
	/* Determine the minute based on block within hour
	   - The lower half blocks are at minute "00"
	   - The upper half blocks are at minute "30" */
	if(block % blocksPerHour < dayTotal)
		minute = "00";
	else
		minute = "30";
 
	return minute;
}   

function showAvailableAppts(){
	var time;
	var day;
	
	// Until we connect to the DB, let's set them all up to be available
//	fillApptStates();
	
	// There are 10 hours available for advising
	for(var sub = 0; sub < blocksPerHour * 10; sub++){
		// Determine the advisor for the current appointment
		lookupAdvisor(apptStates[sub]);
		
		// Create the time string with am or pm
		time = getAmOrPm(sub) + "" + getHour(sub) + "" + getMinute(sub);
		day = sub % dayTotal;
		
		// Call the function to create the current div
		showOneAppt(days[day], time, sub);
	}
}

function toggleOneAvailable(day, time){
	var oneDiv = document.getElementById(day + time);
	if(oneDiv.style.display == "none")
		oneDiv.style.display = "block";
	else
		oneDiv.style.display = "none";
}

function toggleAvailableAppts(mySpan){
	var time;
	var day;
	
	// There are 10 hours available for advising
	for(var sub = 0; sub < blocksPerHour * 10; sub++){
		if(apptStates[sub] == "no"){
			// Create the time string with am or pm
			time = getAmOrPm(sub) + "" + getHour(sub) + "" + getMinute(sub);
			day = sub % dayTotal;
		
			// Call the function to toggle the current div's visibility
			toggleOneAvailable(days[day], time);
		}
	}
	
	if(mySpan.innerHTML == "Lock"){
		lock = true;
		name = "";
		resetAll();
		mySpan.innerHTML = "Unlock";
	}
	else{
		lock = false;
		mySpan.innerHTML = "Lock";
	}
}
