function logoff_request() {
	var msg;
	var display_msg;

	if (top.test_edit) {
		msg = top.test_edit();
		if (msg.length > 0) {
			display_msg = "The following projects have been changed and not saved:\n" + msg + "\n\nAre you sure you want to log off?";
			if (confirm(display_msg) == false) {
				return;
			}
		}
	}
	top.clear_session("logoff");
	return;
}

function get_session_error() {
	top.close_window("logon_window",top.window_tout);
}

function logon_request() {
	var i;
	var d;
	var ds = top.frame_sess;
	var mywin;
	var winoptions = "width=300,height=120,resizable=yes";
	var CPD = window.open("","CPD");
	var now= new Date();

	mywin = top.open_window("logon_window","",winoptions);
	mywin.focus();
	d = mywin.document;
	d.open();
	d.write('<html>');
	d.write('<head>');
	d.write('<title>Edit Access Logon</title>');
	d.write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">');
	d.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">');
	d.write('<style>');
	d.write('<!--');
	d.write('@import url(../styles/gen_styles.css);');
	d.write('-->');
	d.write('</style>');
	d.write('</head>');
	d.write('<script language="JavaScript" type="text/JavaScript">');
	d.write('<!--');
	d.write('var mywin;\n');
	d.write('\n');
	d.write('function set_mywin() {\n');
	d.write('mywin=window.open("","CPD")\n;');
	d.write('}\n');
	d.write('//-->');
	d.write('</script>');
	d.write('<body class="default_body" onload="set_mywin()">');
	d.write('<form id="proj_logon" name="proj_logon" method="POST" action="/cgi-bin/process_logon.cgi">');
	d.write('<table cellSpacing=5 cellPadding=0 border=0>');
  	d.write('<tbody>');
  	d.write('<tr>');
	d.write('<td>Username:</td>');
	d.write('<td><input type="text" id="username" name="username"></td>');
  	d.write('</tr><tr>');
	d.write('<td>Password:</td>');
	d.write('<td><input type="password" id="pword" name="pword" onchange="mywin.encrypt_pword()"></td>');
  	d.write('</tr>');
  	d.write('</tbody>');
	d.write('</table>');
	d.write('<input type="button" id="submit_logon" name="submit_logon" value="Submit" onclick="top.proj_logon.submit()"></td>');
	d.write('<input type="button" id="cancel_logon" name="cancel_logon" value="Cancel" onclick="mywin.cancel_logon()"></td>');
	d.write('</form>');
	d.write('</body>');
	d.write('</html>');
	d.close();
	d.getElementById("username").focus();
}

function encrypt_pword () {
	var mywin = top.get_window("logon_window");
	var e = mywin.document.getElementById("pword");
	var mypassword;


	mypassword = hex_sha1(e.value);
	e.value = mypassword;
}

function cancel_logon () {
	var ds = top.frame_sess;
	var mywin = top.get_window("logon_window");
	var d;

	top.close_window("logon_window");
}

function process_logon() {
	var ds = top.frame_sess;
	var mywin = top.get_window("logon_window");
	var d;
	var tval;
	var now = new Date();
	
	d = mywin.document;

	ds.session_id.value = d.getElementById("session_id").value;
	ds.sessgroup.value = d.getElementById("session_group").value;
	ds.client_timeout_val.value = d.getElementById("client_timeout").value;
	ds.server_timeout_val.value = d.getElementById("server_timeout").value;
	ds.client_timeout_warn.value = d.getElementById("client_warning").value;
	ds.server_timeout_warn.value = d.getElementById("server_warning").value;
	ds.client_timestamp.value = now.getTime();
	ds.server_timestamp.value = now.getTime();
	tval = ds.client_timeout_val.value - ds.client_timeout_warn.value;
	ds.client_timeout_id.value = setTimeout("top.client_pre_tout()",tval);
	tval = ds.server_timeout_val.value - ds.server_timeout_warn.value;
	ds.server_timeout_id.value = setTimeout("top.server_pre_tout()",tval);
	top.set_session();
	top.close_window("logon_window",top.window_tout);
}

function client_pre_tout () {
	var ds = top.frame_sess;
	var now = new Date();
	var mytout = ds.client_timeout_val.value - (now.getTime() - ds.client_timestamp.value);
	var tsecs = Math.round(mytout/1000);
	var secs = tsecs % 60;
	var mins = (tsecs - secs)/60;

	if (top.test_session()) {
		ds.client_timeout_id.value = setTimeout("top.client_tout()",mytout);
		top.open_window("client_tout","You have not entered anything for some time.\nYour logon will be timed out in " + mins + "mins " + secs + "secs.\nAny data previously entered will be lost.");
		top.close_window("client_tout",top.window_tout);
	}
}

function client_tout () {
	var ds = top.frame_sess;

	if (top.test_session()) {
		top.open_window("client_tout","Your logon has been timed out due to client inactivity");
		top.close_window("client_tout",top.window_tout);
		clearTimeout(ds.server_timeout_id.value);

		top.clear_session("client_timeout");
	}
}

function server_pre_tout () {
	var ds = top.frame_sess;
	var now = new Date();
	var mytout = ds.server_timeout_val.value - (now.getTime() - ds.server_timestamp.value);
	var tsecs = Math.round(mytout/1000);
	var secs = tsecs % 60;
	var mins = (tsecs - secs)/60;

	if (top.test_session()) {
		top.open_window("server_tout","You have not submitted anything to the server for some time.\nYour logon will be timed out in " + mins + "mins " + secs + "secs.\nAny data previously entered will be lost.");
		top.close_window("server_tout",top.window_tout);
		ds.server_timeout_id.value = setTimeout("top.server_tout()",mytout);
	}
}

function server_tout () {
	var ds = top.frame_sess;

	if (top.test_session()) {
		top.open_window("server_tout","Your logon has been timed out due to server inactivity");
		close_window("server_tout",top.window_tout);
		top.clear_session("server_timeout");
	}
}

function set_session () {
	var mygroup = top.get_group_id();
	top.set_edit_status();
	top.frame_head.hmenu.showItem("Admin");
	top.set_logon_menu();
}

function clear_session (cmd) {
	var ds = top.frame_sess;

	top.frame_head.hmenu.hideItem("Admin");
	if (ds.client_timeout_id.value > 0) {
		clearTimeout(ds.client_timeout_id.value);
		ds.client_timeout_id.value = 0;
	}
	if (ds.server_timeout_id.value > 0) {
		clearTimeout(ds.server_timeout_id.value);
		ds.server_timeout_id.value = 0;
	}
	top.submit_logoff(cmd);
	ds.sessgroup.value = 0;
	ds.session_id.value = 0;
	top.set_edit_status();
	top.set_logon_menu();
}

function set_logon_menu() {
	var app= top.frame_head.hmenu;
	if (top.secure == "Y") {
		if (top.test_session()) {
			app.setLabel("Logon","TEXT=Logoff;ID=Logon");
			app.setAction("Logon","SCRIPT:top.logoff_request()");
		}
		else {
			app.setLabel("Logon","TEXT=Logon;ID=Logon");
			app.setAction("Logon","SCRIPT:top.logon_request()");
		}
	}
}

function submit_logoff (cmd) {
	var dh = top.frame_hid.document;

	if (cmd == "server") {
		return;
	}

	dh.open();
	dh.write("<html>");
	dh.write('<head>');
	dh.write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">');
	dh.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">');
	dh.write('</head>');
	dh.write("<body>");
	dh.write('<form name = "submit_logoff" method="POST" action="/cgi-bin/process_logoff.cgi">');
	dh.write('<input type="hidden" id="command" name="command" value = "' + cmd + '">');
	dh.write('<input type="hidden" id= "session_id" name="session_id" value = '+ top.get_session_id() + '>');
	dh.write("</form>");
	dh.write("</body>");
	dh.write("</html>");
	dh.close();
	dh.submit_logoff.submit();
	setTimeout("top.delayed_logoff()",1000);
}
function delayed_logoff() {
	var dh = top.frame_hid.document;
	dh.location = "../fixed/dummy.htm";
}

function get_username () {
	var ds = top.frame_sess;
	return ds.username.value;
}

function get_session_id () {
	var ds = top.frame_sess;
	return ds.session_id.value;
}

function get_group_id () {
	var ds = top.frame_sess;
	return ds.sessgroup.value;
}

function test_session () {
	var ds = top.frame_sess;
	if (ds.session_id.value  == 0) {
		return false;
	}
	else {
		return true;
	}
}

function check_session(sessid,error_txt) {
	var ds = top.frame_sess;
	var tval;
	var now = new Date();
	if (sessid == 0) {
		top.open_window("error_message","Your logon session has been terminated by the server.  Please log on again<br>"+ error_txt);
		top.close_window("error_message",top.window_tout);
		top.clear_session("server");
		return false;
	}
	else if (sessid > 0) {
		ds.server_timestamp.value = now.getTime();
	}
	ds.client_timestamp.value = now.getTime();
	if (ds.client_timeout_id.value != 0) { clearTimeout(ds.client_timeout_id.value);}
	if (ds.server_timeout_id.value != 0) { clearTimeout(ds.server_timeout_id.value);}
	tval = ds.client_timeout_val.value - ds.client_timeout_warn.value;
	ds.client_timeout_id.value = setTimeout("top.client_pre_tout()",tval);
	tval = ds.server_timeout_val.value - ds.server_timeout_warn.value;
	ds.server_timeout_id.value = setTimeout("top.server_pre_tout()",tval);
	return true;
}

function set_submit () {
	var ds = top.frame_sess;
	clearTimeout(ds.server_timeout_id.value);
	clearTimeout(ds.client_timeout_id.value);
	ds.server_timeout_id.value = 0;
	ds.client_timeout_id.value = 0;
}

function set_client_edit() {
	var ds = top.frame_sess;
	var now = new Date();
	var tval = ds.client_timeout_val.value - ds.client_timeout_warn.value;

	if (ds.client_timeout_id.value && ds.client_timeout_id.value > 0) {
		clearTimeout(ds.client_timeout_id.value);
		ds.client_timestamp.value = now.getTime();
		ds.client_timeout_id.value = setTimeout("top.client_pre_tout()",tval);
	}
}

function check_refresh() {
	var ds = top.frame_sess;
	var now = new Date();
	var mynow = now.getTime();
	var mytout;
	var mywin;
	var winoptions = "width=300,height=120,resizable=yes";

	if (! top.applet_loaded) {
		setTimeout("top.check_refresh()",1000);
		return;
	}

	if (top.test_session()) {
		clearTimeout(ds.client_timeout_id.value);
		clearTimeout(ds.server_timeout_id.value);
		mytout = (ds.client_timestamp.value - mynow) + (ds.client_timeout_val.value - ds.client_timeout_warn.value);
		ds.client_timeout_id.value = setTimeout("top.client_pre_tout()",mytout);
		mytout = (ds.server_timestamp.value - mynow) + (ds.server_timeout_val.value - ds.server_timeout_warn.value);
		ds.server_timeout_id.value = setTimeout("top.server_pre_tout()",mytout);
		top.set_logon_menu();
		var mygroup = top.get_group_id();
		if (mygroup == 1 || mygroup == 2) top.frame_head.hmenu.showItem("Admin");
	}
}


