/* Terminal Library
 * Copyright 2005, Steve Blinch
 *
 * Part of the BBS Web Terminal
 * http://bbs.blitzaffe.com
 *
 * Not licensed for redistribution
 */
function Terminal() {
	this.onlinetime = 0;
	this.token = '';
	this.debugenabled = false;
	this.connected = false;
	this.connectedto = '';
	this.ac = null;
	this.highascii = IE4 ? false : true;
	
	this.debug = false;

	
	return this;
}

Terminal.prototype.start = function() {
	this.status = new StatusBar(this);
	this.status.build();

	this.set_highascii(this.highascii);
	
	this.show_bbsoffline();
	
	cmDrawFromText ('bbsHtmlMenu', 'hbl', cmThemeBBS, 'ThemeBBS');
	
	this.ac = new ANSIConsole('console');
	
	this.ansifeed = window.ansifeed;

	this.kb = new Keyboard(this,'sendframe','sendfield');
	this.kb.send_focus();
}

Terminal.prototype.connect = function(bbsid,bbstitle) {
	this.ac.clear();
	this.ac.write("Dialing...",true);
	this.ac.write("",true);
	//	ansifeed.location='bbs.php';
	//	ansifeed.location='ansi.php';
	this.ansifeed.location='proxybbs.php?bbs='+bbsid+'&highascii='+(this.highascii?1:0);
	
	el = get_obj('ansifeed');
	el.onload = notify_loaded;	
	
	this.connectedto = bbsid;

	this.status.write('status_name',bbstitle,16);
	
	this.set_connected(true);
	
	this.kb.send_focus();
	return false;
}


Terminal.prototype.show_bbsoffline = function() {
	el = get_obj('btn_disconnect');
	el.style.visibility = 'hidden';
}

Terminal.prototype.show_bbsconnected = function() {
	el = get_obj('btn_disconnect');
	el.style.visibility = 'visible';
}

Terminal.prototype.get_online_time = function() {
	var hours = lz( Math.floor( this.onlinetime / 3600 ) );
	var mins =  lz( Math.floor( (this.onlinetime % 3600) / 60 ) );
	var secs =  lz( (this.onlinetime % 3600) % 60 );
	
	return hours+':'+mins+':'+secs;
}

Terminal.prototype.update_timer = function() {
	if (this.connected) {
		this.status.write('status_online','ONLINE '+this.get_online_time(),15);

		this.onlinetime++;
		setTimeout('term.update_timer()',1000);
	} else {
		this.status.write('status_online','',15);
	}
}

Terminal.prototype.set_connected = function(isconnected) {
	this.connected = isconnected;

	this.onlinetime = 0;
	
	if (this.connected) {
		this.update_timer();

		this.show_bbsconnected();
	} else {
		this.status.write('status_online','',15);

		this.show_bbsoffline();
	}
}

Terminal.prototype.disconnect = function() {
	if (!this.connected) return false;
	
	this.kb.sendindex++;
	
	url = 'proxysend.php?bbs=' + this.connectedto + '&disconnect=1&token=' + this.token + '&ie=' + this.kb.sendindex;
	document.getElementById('sendframe').src = url

	return false;
}

Terminal.prototype.dial_bbs = function(bbsid,bbstitle) {
	if (bbsid.length==0) {
		this.kb.send_focus();
		return false;
	}
	
	if (!this.connected) {
		this.connect(bbsid,bbstitle);
	} else {
		alert('You are already connected to a BBS.  To connect to a different BBS, logout (or hang up), then try again.');
	}
}


Terminal.prototype.toggle_debug = function() {
	this.debugenabled = !this.debugenabled;
	set_visible('ansifeed',this.debugenabled);
	set_visible('sendframe',this.debugenabled);

	this.kb.send_focus();
}

Terminal.prototype.show_benchmark = function() {
	this.status.write('status_info1',this.ac.benchmarktime,11);
}

Terminal.prototype.set_highascii = function(enabled) {
	if (IE4 && enabled) {
		if (!confirm('Warning: You appear to be using Internet Explorer.  Explorer has difficulty displaying high ASCII characters, and may operate very slowly or even crash with high ASCII enabled.  (A better alternative is to use this site under Firefox or Netscape.)\n\nAre you sure you want to enable high ASCII?  Click OK to enable it, or Cancel to leave it disabled.')) return;
	}
	this.highascii = enabled;
	
	var ha = this.highascii ? 'ASC' : '';
	this.status.write('status_info2',ha,3);
}

