<?

/*
** SellaNMS
** $Id: util.php,v 1.19 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

function hiddenFrame($data) {
	global $config, $prefs;

	printf("<HTML>\n\t<HEAD>\n\t\t<TITLE>%s %s</TITLE>\n\t\t<LINK REL=\"SHORTCUT ICON\" HREF=\"favicon.ico\">\n\t\t<META HTTP-EQUIV=\"refresh\" CONTENT=\"%d; URL=sella_nms.php?reload=1&cur=%s\">\n", $config['program'], $config['version'], $config['interval'], "none");
	jsLoadFrame(false);
	printf("\t</HEAD>\n");
	if (!isset($data['reload'])) {
		$file = moduleFile($config['defaultModule']);
		printf("\t<BODY onLoad=\"loadFrame('header', 'sella_nms.php?reload=0&visible=1'); loadFrame('body', '%s');\">", $file);
	} else {
		$reload = "";
		while(list(, $str) = each($data['frames'])) {
			if (strstr($str, ';')) {
				$res = explode(';', $str, 3);
			
				$frame = strrchr($res[0], '.');
				if ($frame != false && $_SESSION[substr($frame, 1)]['refresh'] == true) {
					$reload .= sprintf("if (top.%s.document.location.pathname.substring(top.%s.document.location.pathname.lastIndexOf('/') + 1) == '%s') { loadFrame('%s', '%s', %d) };", $res[0], $res[0], basename($res[1]), $res[0], $res[1], $res[2]);
				}
			} else {
				$reload .= sprintf("loadFrame('%s');", $str);
			}
		}
		printf("\t<BODY onLoad=\"%s\">", $reload);
	}
	printf("</BODY>\n</HTML>\n");
}

function headerFrame($data) {
	global $config, $prefs;

?>
<HTML>
	<HEAD>
		<TITLE><? printf("%s %s", $config['program'], $config['version']); ?></TITLE>
		<LINK REL="SHORTCUT ICON" HREF="favicon.ico">
		<?	printf("<LINK REL=\"stylesheet\" HREF=\"common/%s.css\" TYPE=\"text/css\">\n", isset($config['css']) ? $config['css'] : 'default'); ?>
<? jsLoadFrame(true); ?>
	</HEAD>
	<BODY TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
		<TABLE CLASS="maintitle" WIDTH="100%" HEIGHT="60" CELLSPACING="0" CELLPADDING="0" BORDER="0">
			<TR>
				<TD WIDTH="100%">&nbsp;<? printf("%s %s", $config['program'], $config['version']); ?></TD>
				<TD NOWRAP>
<?
	$db = mysql_connect($config['databaseServer'], $config['databaseUsername'], $config['databasePassword']);
	mysql_select_db($config['databaseName'], $db);

	if (isset($_SESSION['view_id'])) {
		$sql = sprintf('SELECT name FROM gui_view WHERE gui_view.id = %d', $_SESSION['view_id']);
		$result = mysql_query($sql, $db);
		if ($result != false && mysql_num_rows($result) > 0) {
			list($view) = mysql_fetch_row($result);
		}
	} else {
			$view = 'None';
	}
	reset($config['modules']);
	while (list($desc, $module) = each($config['modules'])) {
		if ($module == 'prefs') {
			printf("\t\t\t\t\tUser: %s &nbsp; Current View: <A CLASS=\"viewlink\" HREF=\"javascript:loadFrame('body', '%s');\">%s</A>&nbsp;\n", $_SESSION['user'], moduleFile($module), $view);

			break;
		}
	}
	
?>
				</TD>
			</TR>
			<TR>
				<TD COLSPAN="2"><IMG SRC="images/white_space.gif" ALT="" WIDTH="100%" HEIGHT="1" BORDER="0"></TD>
			</TR>
			<TR>
				<TD CLASS="mainmenu" COLSPAN="2">
<?
	printf("\t\t\t\t\t");
	reset($config['modules']);
	while (list($desc, $module) = each($config['modules'])) {
		printf("[<A CLASS=\"mainlink\" HREF=\"javascript:loadFrame('body', '%s');\">%s</A>] ", moduleFile($module), $desc);
	}
	printf("\n");
?>
				</TD>
			</TR>
			<TR>
				<TD COLSPAN="2"><IMG SRC="images/white_space.gif" ALT="" WIDTH="100%" HEIGHT="1" BORDER="0"></TD>
			</TR>
			<TR>
				<TD COLSPAN="2"><IMG SRC="images/white_space.gif" ALT="" WIDTH="0" HEIGHT="1" BORDER="0"></TD>
			</TR>
		</TABLE>
	</BODY>
</HTML>
<?
}

function jsLoadFrame($useRand) {
	global $config;
?>
		<SCRIPT>
			function loadFrame(targetFrame, url, focus) {
				var x, str;
				var newDate = new Date();
	
				if (typeof targetFrame != "undefined") {
					var target = top;
					var targs = targetFrame.split('.');
					for (str in targs) {
						target = target.frames[targs[str]];
					}
		
					/* Load new URL or reload the old. */
					if (typeof url != "undefined" && url != '') {
						var timer = setTimeout("", 1);
						var newurl = url + "?rand=" + newDate.getTime();  //<? printf("'?rand=%d'", rand(0,getrandmax())); ?>;
			
					} else {
						var newurl = "?rand=" + newDate.getTime(); // <? printf("'?rand=%d'", rand(0,getrandmax())); ?>;
					}

					<? if ($config['javascript_debug']) { ?> alert(targetFrame + ": " + newurl); <?php } ?>

					target.location.href = newurl;
		
					/* Grab window focus. Freaking annoying though. */
					if (typeof focus != "undefined" && focus != 0) {
						target.window.focus();
					}
				}
			}
		</SCRIPT>
<?
}

function moduleFile($module) {
	global $config;

	$file = $config['cacheDir'] . $config['fileSeparator'] . 'sella_nms-' . $module . '.php';
	if (!is_readable($file)) {
		$file = 'sella_nms-' . $module . '.php';
		if (!is_readable($file)) {
			$file = $config['cacheDir'] . $config['fileSeparator'] . 'sella_nms-' . $module . '.html';
			if (!is_readable($file)) {
				$file = 'sella_nms-' . $module . '.html';
				if (!is_readable($file)) {
					$file = 'blank.html';
				}
			}
		}
	}

	return $file;
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/

?>
