<?

// Includes

include('../shared.inc.php');

// Functions

function areaString($checkSecurity = TRUE)
{
	$areas = areas();
	array_unshift($areas['name'], "home");
	array_unshift($areas['display'], "Home");
	array_unshift($areas['permissions'], "");
	array_unshift($areas['menu'], TRUE);
	array_unshift($areas['link'], "");
	
	$numberAreas = count($areas['name']);
	if ($checkSecurity)
		$security = security();

	echo "areas = new Array(";

	for ($i = 0; $i < $numberAreas; $i++) {
		$currentName	= $areas['name'][$i];
		$currentDisplay	= $areas['display'][$i];
		$currentLink	= $areas['link'][$i];

		if (! $i || ((($checkSecurity && $security[$areas['name'][$i]]['read']) || ! $checkSecurity) && $areas['menu'][$i])) {
			$string .= "'" . $areas['name'][$i] . "'";
			if ($i != $numberAreas - 1)
				$string .= ", ";
		}
	}
	$string .= ");";
	
	return $string . "\n";
}

?>

/*-----------------------------------------------------------------------------
|                                                                             |
|  shared.js                                                                  |
|                                                                             |
|  Think Computer Corporation                                                 |
|  SecondBase CS                                                              |
|  JavaScript Library                                                         |
|                                                                             |
|  Copyright © 2001-2005 Think Computer Corporation. All Rights Reserved.     |
|                                                                             |
-----------------------------------------------------------------------------*/

// Rollovers

netscape = navigator.appName == "Netscape";
netscape4 = netscape && parseInt(navigator.appVersion) <= 4;
ie = navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) >= 4;
hover = netscape || ie;

function preload(img)
{
	var a = new Image();
	a.src = img;
	return a;
}

if (hover) {
	arrowgray	= preload('<? echo $_SESSION['shared']; ?>icons/arrowgray.gif');
	arrowpurple	= preload('<? echo $_SESSION['shared']; ?>icons/arrowpurple.gif');
	arrowgreen	= preload('<? echo $_SESSION['shared']; ?>icons/arrowgreen.gif');
	arrowred	= preload('<? echo $_SESSION['shared']; ?>icons/arrowred.gif');
}

<?

if (isset($_SESSION['personid'])) {
	echo areaString();
} else {
	die();
}

?>

if (hover) {
	var currentFile	= currentFile();
	var path	= '<? echo $_SESSION['shared']; ?>menu/';
	var extension	= '';

	for (i = 0; i < areas.length; i++) {
		for (j = 1; j <= 2; j++) {
			filename = areas[i] + j;
			if (j == 3) {
				extension = 'jpg';
			} else {
				extension = 'gif';
			}
			eval(filename + " = preload('" + path + filename + "." + extension + "');");
		}
		if (currentFile == areas[i])
			var predefined = 1;
	}
}

function currentFile()
{
	pathParts = location.pathname.split('/');
	fileParts = pathParts[pathParts.length - 1].split('.');
	if (fileParts[0] == 'index') {
		if (pathParts[pathParts.length - 2] == '') {
			currentFile = 'home';
		} else {
			currentFile = pathParts[pathParts.length - 2];
		}
	} else {
		currentFile = fileParts[0];
	}
	
	return currentFile;
}

function defaultState()
{
	if (hover) {
		for (i = 0; i < areas.length; i++) {
			eval("document['" + areas[i] + "'].src=" + areas[i] + "1.src;");
		}
	}
}

function rollover(name, rollBack)
{
	if (hover) {
		defaultState();
		if (rollBack)
			eval("document['" + name + "'].src=" + name + "2.src;");
	}
}

// Pop-Up Windows

function openWindow(path, name, width, height, format)
{
	window.open(path, name, "width=" + width + ",height=" + height + format);
}

// List Boxes

function changeSequence(list, to)
{
	var index = list.selectedIndex;
	var total = list.options.length - 1;
	if (index == -1) return false;
	if (to == +1 && index == total) return false;
	if (to == -1 && index == 0) return false;
	var items = new Array;
	var values = new Array;

	for (i = 0; i <= total; i++) {
		items[i] = list.options[i].text;
		values[i] = list.options[i].value;
	}

	for (i = 0; i <= total; i++) {
		list.options[i] = new Option(items[i], values[i]);
	}
	list.options[index + to] = new Option(items[index], values[index], 0, 1);
	list.options[index] = new Option(items[index + to], values[index + to]);

	list.focus();
}

function selectAll(list)
{
	for (i = 0; i <= list.options.length - 1; i++) { 
		list.options[i].selected = true; 
	}
}

// Confirmation Dialogs

function deleteRecord(phpself, id)
{
	if (confirm("Are you sure you want to delete this record?")) {
		window.location = phpself + '?delete=1&' + id;
	}
}

function deleteRecords(formName)
{
	if (confirm("Are you sure you want to delete the selected records?")) {
		document.forms[formName].button.value = 'delete';
		document.forms[formName].submit();
	}
}

function approveRecords(formName)
{
	if (confirm("Are you sure you want to approve the selected records?")) {
		document.forms[formName].button.value = 'approve';
		document.forms[formName].submit();
	}
}

function changeStatus(formName, step, numberChoices)
{
	document.forms[formName].elements['status'].value = step;
	for (i = 1; i <= numberChoices; i++) {
		if (i <= step) {
			document['statusArrow' + i].src = arrowgreen.src;
		} else {
			document['statusArrow' + i].src = arrowgray.src;
		}
	}
}

// Arrows

function turnPurple(formName, imageName, hiddenName, id)
{
	if (hover) {
		if (document.forms[formName].elements[hiddenName + '[' + id + ']'].value == id) {
			document[imageName + id].src = arrowgray.src;
			document.forms[formName].elements[hiddenName + '[' + id + ']'].value = '';
		} else {
			document[imageName + id].src = arrowpurple.src;
			document.forms[formName].elements[hiddenName + '[' + id + ']'].value = id;
		}
	}
}

function turnColor(formName, imageName, hiddenName, color)
{
	if (hover) {
		stringLength = hiddenName.length;
		elementArray = document.forms[formName].elements;
		for (i = 0; i < elementArray.length; i++) {
			if (elementArray[i].name.substr(0, stringLength) == hiddenName) {
				openBracket = elementArray[i].name.search('\\[');
				closeBracket = elementArray[i].name.search('\\]');
				arrayIndex = elementArray[i].name.substring(openBracket + 1, closeBracket);
				if (elementArray[i].value == arrayIndex) {
					eval('document[imageName + arrayIndex].src = arrow' + color + '.src');
				}
			}
		}
	}
}

function actionPurple(formName, imageName, hiddenName, id, action)
{
	if (hover) {
		if (document.forms[formName].elements[hiddenName + '[' + id + ']'].value == id) {
			document[imageName + id].src = arrowgray.src;
			document.forms[formName].elements[hiddenName + '[' + id + ']'].value = '';
		} else {
			document[imageName + id].src = arrowpurple.src;
			document.forms[formName].elements[hiddenName + '[' + id + ']'].value = id;
		}
		eval(action);
	}
}

function selectAllArrows(formName, imageName, hiddenName)
{
	if (hover) {
		stringLength = hiddenName.length;
		elementArray = document.forms[formName].elements;
		for (i = 0; i < elementArray.length; i++) {
			if (elementArray[i].name.substr(0, stringLength) == hiddenName) {
				openBracket = elementArray[i].name.search('\\[');
				closeBracket = elementArray[i].name.search('\\]');
				arrayIndex = elementArray[i].name.substring(openBracket + 1, closeBracket);
				document[imageName + arrayIndex].src = arrowpurple.src;
				document.forms[formName].elements[hiddenName + '[' + arrayIndex + ']'].value = arrayIndex;
			}
		}
	}
}