// JavaScript Document
//---------------------  File Header  -----------------------
//  onsiteButtonHandler.php
//
// Purpose:		Handles the screen buttons
//					Note: this file only makes the decisions. work is done
//						in "onsiteCookieHandler.js"
//
//	Defines:		global variables
//
//	Functions:	
//					loadButton ()				handles the load button
//					saveButton ()				handles the save button
//					deleteButton ()			handles the detete button
//					newButton()					handles the new button
//					helpButton()				handles the help button
//
// Internal:	
//
//	Requires:	

// =========================  load Button  ====================================
function loadButton ()
// ----------  Function Header  --------------
//	Name:			loadButton
//	Purpose:		load a saved eval into memory and display
//	Inputs:		selected, saved cookie with evaluation
//	Process:		Verify there's a valid, saved evaluation selected.
//					if current evaluation has changed
//						ask for confirmation: proceed of okay
//					Confirm they really want to load
//					parse the cookie
//					display 
//					set up global.
//	Outputs:		global and display
//	-------------------------------------------
{
	var okay = false;
	var evalId;
	
	// ---------------  Verify they've selected a valid saved evaluation  -----------
	evalId = F_G.savedList.value;
	if (evalId == 99)
	{
		alert ("Please select a saved evaluation from the pick list, then click 'Load' again.");
		return;
	}
	
	// ------ verify that current evaluation is unchanged  (ok to overwrite) ------
	if (evalChanged_G)
	{
		okay = confirm ("Current evaluation has changed. Do you want to loose your changes?");
		if (!okay) return;
	}
	
	// Ask for confirmation to proceed
	if (!okay)		// if okay, then the user has already confirmed.
	{
		okay = confirm ("Please confirm that you really want me to load the selected evaluation.");
		if (!okay) return;
	}
	//
	// --- If here, then we're confirmed for the load. 
	//
	loadEval (evalId);
}

// ==========================  saveButton  ====================================
function saveButton ()
// ----------  Function Header  --------------
//	Name:			saveButton
//	Purpose:		Saves the current screen in a cookie
//	Inputs:		none
//	Process:		If it's already saved, just resave
//					Determine if we have space (max cookies)
//					if not, tell user and exit
//					else: save this evaluation
//	Outputs:		new cookie (if successful
//	-------------------------------------------
{
	evalVendors();
	if (evalSaved_G)
	{
		saveEval(); // formats and saves the current cookie
	}
	else
	{
		if (numEvals_G == maxCookies_GS)
			alert ("You can only save " + maxCookies_GS + " evaluations.");
		else
			saveNewEval();  // formats and saves the new cookie
	}
	F_G.submit();
}

// ============================== deleteButton ==================================
function deleteButton ()
// ----------  Function Header  --------------
//	Name:			deleteButton
//	Purpose:		Deletes a saved evaluation
//	Inputs:		selected evaluation in the select list
//	Process:		If an item isn't selected
//						tell operator and exit
//					Confirm deletion of the selected item
//					Delete the item
//	Outputs:
//	-------------------------------------------
{
	var okay = false;
	var evalId;
	var deleteCurrent = false; 
	
	// ---------------  Verify they've selected a valid saved evaluation  -----------
	evalId = F_G.savedList.value;
		if (evalId == 99)
	{
		alert ("Please select a saved evaluation from the pick list, then click 'Delete' again.");
		return;
	}
	
	// ----------------------  see if we're deleting the currently-active one  -----------------
	if (evalSaved_G)									// if the current eval is saved
	{
		if (currCookieO_G.name == (cookieNamePrefix_GS + evalId))	// then we're deleting the current one
		{
			deleteCurrent = true;
			okay = confirm ("Do you really want to delete the current saved evaluation");
			if (!okay) return;
		}
	}
		
	// --------------------- ask for confirmation  --------------------------
	if (!okay)		// if okay, then the user already confirmed
		if (!confirm("Please confirm that you want to delete the selected evaluation.")) return;
	
	//  -------------------------  Delete the evaluation  ---------------------
	deleteEval (evalId);
	if (deleteCurrent)
	{
		currCookieI_G = null;
		currCookieO_G = null;
		setNotSaved();
	}
}

// ============================= newButton  =======================================

function newButton ()
// ----------  Function Header  --------------
//	Name:		
//	Purpose:	
//	Inputs:		
//	Process:		
//	Outputs:
//	-------------------------------------------
{
	var okay = false;
	
	if (evalChanged_G)
	{
		okay = confirm("You've changed the current evaluation. Do you really want to loose those changes and clear the screen?");
		if (!okay) return;
	}
	
	// initialize global memory, then display it.
	osvTitle_G = ""
	osvGoal_G = "";
	// leave discipline alone
	
	for (i=0; i<numVendors_GS; i++)
	{
		osvName_G [i] = "";
		osvLink_G [i] = "";
		osvVendScore_G [i] = nullScore_GS;
		
		for (j=0; j<maxCategories_GS; j++)
		{
			osvCatScore_G [i][j] = nullScore_GS;
			for (k=0; k<maxScores_GS; k++)
				osvValue_G [i][j][k]= nullScore_GS;
		}
	}
	setUnChanged();
	setNotSaved();
	setCookieGlobal (null);
	displayGlobal ();
}

function evalButton()
{
	evalVendors();
	F_G.saveFocus.value = "evalButtonN";
	F_G.submit();
}

function helpButton()
{
	helpWindow = window.open("osvHelp.html", "osvHelp");
	helpWindow.focus();
}