/* Terminal Utility Library
 * Copyright 2005, Steve Blinch
 *
 * Part of the BBS Web Terminal
 * http://bbs.blitzaffe.com
 *
 * Not licensed for redistribution
 */
var term = null;
var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;
var W3C = (document.getElementById) ? 1 : 0;

var tiptimer = null;

function set_visible(name,visible) {
	if (W3C) {
		document.getElementById(name).style.display = (visible ? 'block' : 'none');
	} else if (NS4) {
		document.layers[name].display = (visible ? 'block' : 'none');
	} else {
		document.all[name].style.display = (visible ? 'block' : 'none');
	}
}

function get_obj(name) {
	if (W3C) {
		return document.getElementById(name);
	} else if (NS4) {
		return document.layers[name];
	} else {
		return document.all[name];
	}
}

function lz(i) {
	s = i.toString();
	if (s.length<2) s = '0'+s;
	return s;
}

function test_ansi(debugplayback) {
	ansifeed.location = 'ansi.php?highascii=' + (term.highascii?1:0) + '&playback=' + (debugplayback?1:0);
}

function assign_token(new_token) {
	term.token = new_token;
}

function handle_keypress(e) {
	return term.kb.key_press(e);
}

function handle_keychange(e) {
	return term.kb.key_change(e);
}

function notify_loaded() {
	if (!term) return;
	term.set_connected(false);
}

function start() {
	term = new Terminal();
	term.start();
	
	for (var i = 0; i<document.forms.length; i++) {
		for (j=0; j<document.forms[i].elements.length; j++) {
			document.forms[i].elements[j].onkeydown = this.handle_keychange;
			document.forms[i].elements[j].onkeyup = this.handle_keychange;
			document.forms[i].elements[j].onkeypress = this.handle_keypress;
			
		}
	}
}

function send_focus() {
	// wrapper function; necessary as an onfocus event may fire 
	// prior to terminal instantiation
	if (term && term.kb) term.kb.send_focus();
}

function toggle_high_ascii() {
	term.set_highascii(!term.highascii);
}

function about_box(doclose) {
	el = get_obj('aboutbox');
	if (!el) return;
	el.style.display = (doclose ? 'none' : 'block');	
}

function tip_clear() {
	tip('',true);
}

function tip(msg,force) {
	if (!msg.length && !force) {
		tiptimer = setTimeout('tip_clear()',500);
		return;
	}
	
	if (tiptimer) {
		clearTimeout(tiptimer);
		tiptimer = null;
	}
	
	el = get_obj('tiparea');
	if (el) {
		if (msg.length>0) msg = "<div class='tooltip'>" + msg + "</div>";
		el.innerHTML = msg;
	}
}

function check_unload() {
	if (!term.connected) return;

	return "You will be disconnected from the BBS if you continue.";
}