/*
'Author					: Stefan Kruger
'Date Created			: 05-Dec-2005
'Last Changed by		: Stefan Kruger
'Date Changed			: 03-Jan-2006
'Version				: 1.0.03012006
'Comments/Changes:
'Date:			Person:					Desc:
'------------------------------------------------------------------------------------------------------
'05-Dec-2005	Stefan Kruger			Created file

'ToDo List:
'Date:			Person:					Desc:
'------------------------------------------------------------------------------------------------------
*/

/***************************************************
Section related to history management
***************************************************/

//Definitions
var historyArray = new Array();
var currentLocation;
var historySize = 10;
var multidimseperator = "|^||^|";

function addHistory(value1,value2){
	//Limit size and indexing
	if (historyArray.length == historySize) {
		historyArray[0] = null;
		historyArray = compactArray(historyArray);
	}
	//If a current location is defined, remove all items after current
	if (currentLocation || currentLocation == 0) {
		for (i = currentLocation + 1; i < historyArray.length; i++) {
			historyArray[i] = null;
		}
		historyArray = compactArray(historyArray);
	}
	//Add history at the end
	var value = new Array();
	value = [value1,value2];
	historyArray.push(value);
	value = null;
	//Update current location
	currentLocation = historyArray.length - 1;
}

/*
This function is used to "compact" an array 
(ie: remove null items and restart numbering from 0)
*/
function compactArray(arrayToCompact){
	var returnArray = new Array();
	//Run through original array
	for (k = 0; k < arrayToCompact.length; k++) {
		//Copy non-null items to return array
		if (arrayToCompact[k]){
			returnArray.push(arrayToCompact[k]);
		}
	}
	return returnArray;
}

//This function is used as a readable toString() for 2-dimensional arrays
function toStringMultiArray(arrayToWrite){
	var output = "";
	try {
		var commaflag = false;
		for (j = 0; j < arrayToWrite.length; j++){
			if (commaflag){
				output += ",";
			}
			output += arrayToWrite[j].toString().replace(/,/g,multidimseperator);
			commaflag = true;
		}
	}
	catch (ex) {
		alert("Error in multi-dimension array toString() function.");
	}
	return output;
}

function getHistory(index){
	//Check for the start of history
	if (index >= 0){
		//Check for the end of history
		if (index < (historyArray.length)){
			//Update current location to new value
			currentLocation = index;
			//Return data
			//alert("Moved to index value:" + index + "\nRequested history value is:" + historyArray[index]);
			//This would be more something like...
			try {
				var str = "getPage('" + historyArray[index][0] + "','" + historyArray[index][1] + "',true);"
				//alert(str);
				eval(str);
			}
			catch(ex) {}
			//showHistory();
		}
		else {
			alert("Request is past the END of history.");
		}
	}
	else {
		alert("Request is past the START of history.");
	}
}

function goBack(count){
	//Check for history
	if (currentLocation || currentLocation == 0) {
		//Check for valid value
		if (!(isNaN(count)) && count.toString() != "") {
			getHistory(currentLocation - parseInt(count));
		}
		else {
			alert("Please enter a valid amount to go back.");
		}
	}
	else {
		alert("No history has been created yet.");
	}
}

function goForward(count){
	//Check for history
	if (currentLocation || currentLocation == 0) {
		//Check for valid value
		if (!(isNaN(count)) && count != "") {
			getHistory(currentLocation + parseInt(count));
		}
		else {
			alert("Please enter a valid amount to go forward.");
		}
	}
	else {
		alert("No history has been created yet.");
	}
}

function showHistory(){
	if (historyArray.length && (currentLocation || currentLocation == 0)) {
		alert("History:\n" + toStringMultiArray(historyArray).replace(/,/g,"\n") + "\n\nCurrent array location:" + currentLocation  + "\nCurrent value:" + historyArray[currentLocation]);
	}
	else {
		alert("No history has been created yet.");
	}
}