var loginPane = null;

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

function docbody(){
	return (document.body != null)?document.body:document.getElementsByTagName("body").item(0);
}

function getObj (nr)
{
	if (document.layers) { return document.layers[nr];	}
	else if (document.all)	{	return document.all[nr]; }
	else if (document.getElementById) { return document.getElementById(nr); }
	else { return false;	}
}

function addLoginPane()
{
  var objBody = docbody();
  loginPane = document.createElement("div");
  loginPane.id = "loginElement";
  loginPane.visibility = "hidden";
  objBody.insertBefore(loginPane, getObj('sig'));
  
  var head = document.getElementsByTagName("head")[0];
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = "inc/md5.js";
  head.appendChild(script);
  
  script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = "inc/login.js";
  head.appendChild(script);
  
  script = document.createElement('link');
  script.rel = 'stylesheet';
  script.type = 'text/css';
  script.href = "inc/login.css";
  head.appendChild(script);
  
  var lpaneLoader = new sack("pageserv.php?type=loginPanel");
  lpaneLoader.elementObj = loginPane;
  lpaneLoader.onCompletion = function() {  
    toggleLoginPane();
	if(!((document.all) && (navigator.userAgent.indexOf('Opera')== -1)))
		regDraggable(loginPane, loginPane);
  }
  lpaneLoader.runAJAX();
}

function toggleLoginPane(force) {
	if(loginPane == null && force != 0)
		addLoginPane();
	else if(loginPane.open != 1 && force != 0)
	{
		loginPane.style.visibility = "visible";
		loginPane.open = 1;
		setTimeout("focusName()", 10);
	}
	else if (force != 1)
	{
		loginPane.style.visibility = "hidden";
		loginPane.open = 0;
		if(getObj("LPloginname") != null) getObj("LPloginname").blur();
		if(getObj("LPloginbutton") != null) getObj("LPloginbutton").blur();
	}
}

function focusName()
{
	if(getObj("LPloginname") != null) getObj("LPloginname").focus();
	else if(getObj("LPloginbutton") != null) getObj("LPloginbutton").focus();
}

connectionsPane = new connectionsPaneClass();
function connectionsPaneClass() {
	this.obj = null;
	this.shown = false;
	
	this.show = function()
	{
		if(!this.obj)
		{
			if(!getObj('connbar'))	return;
			else	this.obj = getObj('connbar');
		}
		this.obj.style.right = "0px";
		this.obj.style.width = "inherit";
	}

	this.hide = function()
	{
	  if(!this.shown)
	  {
      if(!this.obj)
      {
        if(!getObj('connbar'))	return;
        else	this.obj = getObj('connbar');
      }
      this.obj.style.right = "inherit";
      this.obj.style.width = "16px";
    }
	}
	
	this.toggle = function()
	{
	  if(!this.shown) { this.shown = true; this.show(); }
	  else { this.shown = false; this.hide(); }
	}
}
function initcpane() {
	if(getObj("connbar"))
	{
		getObj("connbar").onmouseover = function() { connectionsPane.show(); }
		getObj("connbar").onmouseout = function() { connectionsPane.hide(); }
		
		connections.onUpdate = function() {
			if(this.openConnections > 0)
				getObj("connbar").style.visibility = "visible";
			else
				getObj("connbar").style.visibility = "hidden";
			getObj("connbarmsg").innerHTML = "";
			for(x in this.openConnectionArr)
			{
				getObj("connbarmsg").innerHTML += this.openConnectionArr[x]+", ";
			}
			if(getObj("connbarmsg").innerHTML.length > 2)
				getObj("connbarmsg").innerHTML = getObj("connbarmsg").innerHTML.substring(0, getObj("connbarmsg").innerHTML.length - 2);
		}
	}
}
addLoadEvent(initcpane);

function capturekeyDown(e) 
{
  var evt=(e)?e:(window.event)?window.event:null;
  if(evt)
  {
    var keycode=(evt.charCode)?evt.charCode:
      ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
    
    if(keycode ==  76 && evt.altKey && evt.ctrlKey)
    {
      toggleLoginPane(1);
	  return;
    }
	if(keycode == 13 && (loginPane == null || loginPane.open != 1) && (getObj("terminalContent") != null))
	{
		str = getObj("terminalContent").innerHTML;
		if(str.substring(str.length - 5, str.length) == "login")
		{
       		toggleLoginPane(1);
			return;
		}
	}
		
		if(keycode == 67 && evt.altKey && evt.ctrlKey)
			connectionsPane.toggle();
    
    if(keycode == 27 && loginPane != null && loginPane.open == 1)
        toggleLoginPane(0);
	if(getObj("terminalContent"))
	{
		if(keycode == 13 && loginPane != null && loginPane.open == 1 && getObj("LPloginbutton") != null)
			getObj("LPloginbutton").click();
		
		if(keycode >= 65 && keycode <= 90 && (loginPane == null || loginPane.open != 1))
			getObj("terminalContent").innerHTML += String.fromCharCode(keycode+32);
		if(keycode == 32 && (loginPane == null || loginPane.open != 1))
			getObj("terminalContent").innerHTML += " ";
		if(keycode == 8 && (loginPane == null || loginPane.open != 1))
		{
			str = getObj("terminalContent").innerHTML;
			if(str.substring(str.length-6, str.length-5) != "&gt;")
				getObj("terminalContent").innerHTML = str.substring(0, str.length-1);
			return false;
		}
	}
	else
		return true;
  }
}


var offsetVar = [0,0];
var mousePos = [0,0];
var followMouseObj = null;
var followMouseFunc = null;
var followMouseUpFunc = null;
function mouseHandler(e) {
	if (typeof e != "undefined")
		mousePos = [e.pageX, e.pageY];
	else if (typeof window.event != "undefined")
		mousePos = [event.clientX + docbody().scrollLeft, event.clientY + docbody().scrollTop];
	
	
	if(followMouseFunc != null)
	{
		followMouseFunc();
	}
	else if(followMouseObj != null)
	{
		var lmove = (mousePos[0] - offsetVar[0]);
		if(lmove < 0) lmove = 0;
		var tmove = (mousePos[1] - offsetVar[1]);
		if(tmove < 0) tmove = 0;
		followMouseObj.style.left = lmove+"px";
		followMouseObj.style.top = tmove+"px";
	}
}
document.onmousemove = mouseHandler;
document.onmouseup = function() { followMouseObj = null; followMouseFunc = null; if(followMouseUpFunc) {followMouseUpFunc(); followMouseUpFunc = null;} }
function regDraggable(dobj, dobjh)
{
	if(loginPane != null)
		tobj = loginPane;
	else
		tobj = getObj("sig");
	//if(!((document.all) && (navigator.userAgent.indexOf('Opera')== -1)))
	//	regFocusable(dobj, tobj);
	dobjh.onmousedown = function(e) {
		offsetVar[0] = mousePos[0]-dobj.offsetLeft;
		offsetVar[1] = mousePos[1]-dobj.offsetTop;
		followMouseObj = dobj;
		//if((document.all) && (navigator.userAgent.indexOf('Opera')== -1))
			docbody().insertBefore(dobj, tobj);
		return true;
	}
}
function regFocusable(dobj, tobj) { //This messes things up in some cases
	dobj.onmousedown = function(e) {
		docbody().insertBefore(dobj, tobj);
		return true;
	}
}
function initDraggable() { regDraggable(getObj("terminal"), getObj("terminalHeader")); }
addLoadEvent(initDraggable);


function base64Decode(str)
{
	var indexBase64 = new Array(
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
		52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
		-1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
		15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
		-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
		41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
	);

	var out = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	// trim invalid characters in the beginning and in the end of the string

	str = str.replace(/^[^a-zA-Z0-9\+\/\=]+|[^a-zA-Z0-9\+\/\=]+$/g,"")

	var len = str.length;

	do
	{
		enc1 = indexBase64[str.charCodeAt(i++)];
		enc2 = indexBase64[str.charCodeAt(i++)];
		enc3 = indexBase64[str.charCodeAt(i++)];
		enc4 = indexBase64[str.charCodeAt(i++)];

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		out += String.fromCharCode(chr1);

		if (enc3 != -1)
		{
			out += String.fromCharCode(chr2);
		}
		if (enc4 != -1)
		{
			out += String.fromCharCode(chr3);
		}
	}
	while (i < len);
	
	if (i != len)
	{
		alert("Base64 is broken");
		return "";
	}

	return out;
}

nn=(document.layers)?true:false;
if(nn) document.captureEvents(Event.KEYDOWN);
document.onkeydown = capturekeyDown;
