function saveNotepad() {
	ajFetch('?proc_footer_element=notepad&notepad_body=' + urlEncode(document.getElementById('notepad_body').value), 'footer_elements', false, 0);
}
function notepadStateChange() {
	document.getElementById('notepad_save_button').className='submit';
	document.getElementById('notepad_save_button').disabled=false;
}
// Prevents tabs flicker //
document.write('<style type="text/css">.tabber{display:none;}<\/style>');
function tabberObj(argsObj)
{
  var arg; 
  this.div = null;
  this.classMain = "tabber";
  this.classMainLive = "tabberlive";
  this.classTab = "tabbertab";
  this.classTabDefault = "tabbertabdefault";
  this.classNav = "tabbernav";
  this.classTabHide = "tabbertabhide";
  this.classNavActive = "tabberactive";
  this.titleElements = ['h2','h3','h4','h5','h6'];
  this.titleElementsStripHTML = true;
  this.removeTitle = true;
  this.addLinkId = false;
  this.linkIdFormat = '<tabberid>nav<tabnumberone>';
  for (arg in argsObj) { this[arg] = argsObj[arg]; }
  this.REclassMain = new RegExp('\\b' + this.classMain + '\\b', 'gi');
  this.REclassMainLive = new RegExp('\\b' + this.classMainLive + '\\b', 'gi');
  this.REclassTab = new RegExp('\\b' + this.classTab + '\\b', 'gi');
  this.REclassTabDefault = new RegExp('\\b' + this.classTabDefault + '\\b', 'gi');
  this.REclassTabHide = new RegExp('\\b' + this.classTabHide + '\\b', 'gi');
  this.tabs = new Array();
  if (this.div) {
	this.init(this.div);
    this.div = null;
  }
}
tabberObj.prototype.init = function(e)
{
 var
  childNodes, 
  i, i2, 
  t, 
  defaultTab=0, 
  DOM_ul, 
  DOM_li, 
  DOM_a, 
  aId, 
  headingElement; 
  if (!document.getElementsByTagName) { return false; }
  if (e.id) {
    this.id = e.id;
  }
  this.tabs.length = 0;
  childNodes = e.childNodes;
  for(i=0; i < childNodes.length; i++) {
    if(childNodes[i].className &&
       childNodes[i].className.match(this.REclassTab)) {
      t = new Object();
      t.div = childNodes[i];
      this.tabs[this.tabs.length] = t;
      if (childNodes[i].className.match(this.REclassTabDefault)) {
	defaultTab = this.tabs.length-1;
      }
    }
  }
  DOM_ul = document.createElement("ul");
  DOM_ul.className = this.classNav;
  for (i=0; i < this.tabs.length; i++) {

    t = this.tabs[i];
    t.headingText = t.div.title;
    if (this.removeTitle) { t.div.title = ''; }

    if (!t.headingText) {
      for (i2=0; i2<this.titleElements.length; i2++) {
	headingElement = t.div.getElementsByTagName(this.titleElements[i2])[0];
	if (headingElement) {
	  t.headingText = headingElement.innerHTML;
	  if (this.titleElementsStripHTML) {
	    t.headingText.replace(/<br>/gi," ");
	    t.headingText = t.headingText.replace(/<[^>]+>/g,"");
	  }
	  break;
	}
      }
    }

    if (!t.headingText) {
      t.headingText = i + 1;
    }
    DOM_li = document.createElement("li");
    t.li = DOM_li;
    DOM_a = document.createElement("a");
    DOM_a.appendChild(document.createTextNode(t.headingText));
    DOM_a.href = "javascript:void(null);";
    DOM_a.title = t.headingText;
    DOM_a.onclick = this.navClick;
    DOM_a.tabber = this;
    DOM_a.tabberIndex = i;
    if (this.addLinkId && this.linkIdFormat) {
      aId = this.linkIdFormat;
      aId = aId.replace(/<tabberid>/gi, this.id);
      aId = aId.replace(/<tabnumberzero>/gi, i);
      aId = aId.replace(/<tabnumberone>/gi, i+1);
      aId = aId.replace(/<tabtitle>/gi, t.headingText.replace(/[^a-zA-Z0-9\-]/gi, ''));

      DOM_a.id = aId;
    }
    DOM_li.appendChild(DOM_a);
    DOM_ul.appendChild(DOM_li);
  }
  e.insertBefore(DOM_ul, e.firstChild);
  e.className = e.className.replace(this.REclassMain, this.classMainLive);
  this.tabShow(defaultTab);
  if (typeof this.onLoad == 'function') {
    this.onLoad({tabber:this});
  }

  return this;
};


tabberObj.prototype.navClick = function(event)
{


  var
  rVal, 
  a, 
  self, 
  tabberIndex, 
  onClickArgs; 

  a = this;
  if (!a.tabber) { return false; }

  self = a.tabber;
  tabberIndex = a.tabberIndex;
  a.blur();
  if (typeof self.onClick == 'function') {

    onClickArgs = {'tabber':self, 'index':tabberIndex, 'event':event};
    if (!event) { onClickArgs.event = window.event; }

    rVal = self.onClick(onClickArgs);

    if (rVal === false) { return false; }
  }

  self.tabShow(tabberIndex);

  return false;
};


tabberObj.prototype.tabHideAll = function()
{
  var i; 
  for (i = 0; i < this.tabs.length; i++) {
    this.tabHide(i);
  }
};


tabberObj.prototype.tabHide = function(tabberIndex)
{
  var div;

  if (!this.tabs[tabberIndex]) { return false; }
  div = this.tabs[tabberIndex].div;
  if (!div.className.match(this.REclassTabHide)) {
    div.className += ' ' + this.classTabHide;
  }
  this.navClearActive(tabberIndex);

  return this;
};


tabberObj.prototype.tabShow = function(tabberIndex)
{

  var div;

  if (!this.tabs[tabberIndex]) { return false; }
  this.tabHideAll();
  div = this.tabs[tabberIndex].div;
  div.className = div.className.replace(this.REclassTabHide, '');
  this.navSetActive(tabberIndex);
  if (typeof this.onTabDisplay == 'function') {
    this.onTabDisplay({'tabber':this, 'index':tabberIndex});
  }

  return this;
};

tabberObj.prototype.navSetActive = function(tabberIndex)
{
  this.tabs[tabberIndex].li.className = this.classNavActive;

  return this;
};


tabberObj.prototype.navClearActive = function(tabberIndex)
{
  this.tabs[tabberIndex].li.className = '';

  return this;
};

function tabberAutomatic(tabberArgs)
{

  var
    tempObj, 
    divs, 
    i; 

  if (!tabberArgs) { tabberArgs = {}; }

  tempObj = new tabberObj(tabberArgs);
  divs = document.getElementsByTagName("div");
  for (i=0; i < divs.length; i++) {
    if (divs[i].className &&
	divs[i].className.match(tempObj.REclassMain)) {
      tabberArgs.div = divs[i];
      divs[i].tabber = new tabberObj(tabberArgs);
    }
  }
  
  return this;
}


function tabberAutomaticOnLoad(tabberArgs)
{
  var oldOnLoad;

  if (!tabberArgs) { tabberArgs = {}; }
  oldOnLoad = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = function() {
      tabberAutomatic(tabberArgs);
    };
  } else {
    window.onload = function() {
      oldOnLoad();
      tabberAutomatic(tabberArgs);
    };
  }
}

if (typeof tabberOptions == 'undefined') {

    tabberAutomaticOnLoad();

} else {

  if (!tabberOptions['manualStartup']) {
    tabberAutomaticOnLoad(tabberOptions);
  }

}

function showDebug() {
	var debugDiv = document.getElementById('debug'); 
	if (debugDiv.style.visibility == 'hidden') {
		debugDiv.style.visibility = 'visible';
	} 
	
	else debugDiv.style.visibility = 'hidden';
}

function clearDebug() {
	document.getElementById('debug').innerHTML = '<table width="100%" border="0"> <tr> <td><h2>DO NOT REPORT!</h2></td><td align="right"><a href="javascript:clearDebug();">Clear</a></td></tr></table>';
	document.getElementById('debugLink').innerHTML = '<a href="javascript:showDebug();">Debug</a>';
	var debugDiv = document.getElementById('debug');
	debugDiv.style.visibility = 'hidden';
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=550');");
}

function showUserPicture(pictureFile) {
	document.getElementById('shadeWrapper').style.visibility = 'visible';
	document.getElementById('noShadeContent').style.visibility = 'visible';
	document.getElementById('dynamicUserPictureViewer').src = 'system/image.php?file=' + pictureFile + '&width=400';
}

function hideUserPicture() {
	document.getElementById('shadeWrapper').style.visibility = 'hidden';
	document.getElementById('noShadeContent').style.visibility = 'hidden';
}

function rankEval(hoverState, objectNumber) {
	
	if (objectNumber > 0) {
		
		for (i=1; i <= objectNumber; i++) {
			document.getElementById('voteObject'+i).className = 'votehover';
		}
		
		if (objectNumber+1 < 5) {
			for (i=objectNumber+1; i<=5; i++) {
				document.getElementById('voteObject'+i).className = 'voteclear';
			}
		}
		
		document.getElementById('voteInt').innerHTML = objectNumber + '/5';
		
	} else {
		for (i=1; i<=5; i++) {
			document.getElementById('voteObject'+i).className = 'voteclear';
		}
		document.getElementById('voteInt').innerHTML = '';
	}
}
function bookmark(title, url) {
  if (document.all) window.external.AddFavorite(url, title);
  else if (window.sidebar) window.sidebar.addPanel(title, url, "");
}
var paused = false;

function resumeLane() {
	document.getElementById("laneContainer").innerHTML = "";
	paused = false;
}

function launchLANE() {
	ajFetch('?L=lane.lane&chromeless=1','displayLaneContent', true, 30000);
}

function displayLaneContent(dataContent) {
	
	if (dataContent != '') {
		document.getElementById("laneContainer").innerHTML = dataContent;
		paused = true;
	}
}
var pin = Math.random();
var tritonProcState = false;

function launchTRITON() {
	if (!tritonProcState) {
		ajFetch('?L=chat.dac&chromeless=1&pin=' + pin, 'appendChatContent', true, 3000);
		document.getElementById('textInput').focus();
		tritonProcState = true;
	}
}

var usersInChat = new Array;
var stringBuffer = '';
var stringBufferActual = '';

function appendChatContent(dataContent) {
	/* Explode the data content at each carriage return */
	streamLines = dataContent.split('\n');
	
	/* Run through each lines */
	for (var n=0; n < streamLines.length; n++) {
		
		/* Don't act if the streamLine is blank */
		if (trim(streamLines[n]) != '') {
		
			/* Split the line into content chunks, divided by ":" */
			contentArray = streamLines[n].split(':');
			
			/* Data innput modes switching */
			switch(contentArray[0]) {
				
				/* CHATCONTENT MODE */
				case 'CHATCONTENT':
					var chatContainer = document.getElementById('chatContent');
					
					if (chatContainer.innerHTML.length > 5120) {
						chatContainer.removeChild(chatContainer.childNodes[0]);
					}
					
					chatContainer.innerHTML += urlDecode(contentArray[1]);
					scrollDown();
				break;
				
				case 'NICKLIST':
					document.getElementById('nickList').innerHTML = urlDecode(contentArray[1]);
				break;
				
				case 'NICKLISTPUSH':
					if (!document.getElementById('nickElement_'+contentArray[1])) {
						var containerElement = document.getElementById('nickList');
						var newNickContainer = document.createElement('div');
						newNickContainer.setAttribute('id', 'nickElement_'+contentArray[1]);
						newNickContainer.innerHTML = urlDecode(contentArray[2]);
						containerElement.appendChild(newNickContainer);
						
						usersInChat.push(contentArray[1]);
					}
					
				break;
				
				case 'NICKLISTPOP':
					if (document.getElementById('nickElement_'+contentArray[1])) {
						var containerElement = document.getElementById('nickList');
						containerElement.removeChild('nickElement_'+contentArray[1]);
					}
					
					array_pop(usersInChat, contentArray[1]);
					
				break;
				
				case 'TOPIC':
					document.getElementById('topicHandle').innerHTML = urlDecode(contentArray[1]);
				break;

				case 'HEADER':
					document.getElementById('topicHeader').innerHTML = urlDecode(contentArray[1]);
				break;
				
				default:
					document.getElementById('debug').innerHTML += streamLines[n] + " ";
					
					var debuglen = document.getElementById('debugLink').innerHTML.length;
					document.getElementById('debugLink').innerHTML = '<a href="javascript:showDebug();">Debug (' + debuglen + ')</a>';
				break;
			}
		}
	}
}

function keyEventHandler(key) {
	switch (key) {
		case 9: 
			
			i=0;
			
			var textField = document.getElementById('textInput');
			if (textField.value != "") {
				
				words = textField.value.split(' ');
				var wordLen = words[words.length-1].length;
				var wholeLen = document.getElementById('textInput').value.length;
				
				if (trim(words[words.length-1]) != "") {
				
					while (key < usersInChat.length) {
						if (words[words.length-1].toLowerCase() == usersInChat[i].substr(0, wordLen).toLowerCase()) {
							textField.value += usersInChat[i].substr(wordLen, usersInChat[i].length-wordLen).toLowerCase() + " ";
							break;
						}
						i++;
					}
				}
		  	}
			void(0);
		break;

		case 13: 
			sendData();
		break;
		
		case 37: 
			// 
		break;
		
		case 38: 
			stringBufferActual = document.getElementById('textInput').value;
			document.getElementById('textInput').value = stringBuffer;
		break;	
		
		case 39: 
			//
		break;
		
		case 40: 
			document.getElementById('textInput').value = stringBufferActual;
		break;
		
		default:
			data = document.getElementById('textInput').value;
			if (data.substr(0, 1) == "/") {
				ajFetch('?L=chat.comhelper&chromeless=1&str=' + urlEncode(data), 'commandHelper', false, 0);
				document.getElementById('textInput').focus();
			}
			
			else {
				
				var helper = document.getElementById('commandsHelper');
				helper.style.visibility = 'hidden';
				helper.innerHTML = '';
				
				document.getElementById('textInput').focus();
			}
		break;
	}
}

function sendData() {
	data = document.getElementById('textInput').value;
	if (data != null && trim(data) != '') {

		stringBuffer = data;

		//data = data.replace('+', '&#43');
		ajFetch('?L=chat.dac&chromeless=1&pin=' + pin + '&sendChatData=' + urlEncode(data), 'appendChatContent', false, 0);

		document.getElementById('textInput').value = '';
		
		setTimeout("document.getElementById('commandsHelper').style.visibility = 'hidden';", 500);
		
	}
}

function scrollDown() {
	content = document.getElementById('chatContent');
	content.scrollTop = content.scrollHeight;
}

function detachChat() {
	ajStop('appendChatContent');
	document.getElementById('detachedInfoDiv').style.visibility = 'visible';
	document.getElementById('chatBlockWrapper').style.visibility = 'hidden';	
	
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('?L=chat.chatroom&channel=general&chromeless=1', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=700');");
}

var selectedUser = "";

function pickUser(username) {

	var i=0;
	while (i < usersInChat.length) {
    	if (usersInChat[i] != username) {
			document.getElementById('nickEntityContainer_' + usersInChat[i]).className = 'chatNickEntity';
		}
		i++;
	}
	document.getElementById('nickEntityContainer_'+username).className = 'selectedChatNickEntity';
	selectedUser = username;
}

function actUser() {
	if (selectedUser != "") {
		var action = document.getElementById('userAction').value;
		document.getElementById('textInput').value = '/' + action + " " + selectedUser;
		sendData();
	}
	document.getElementById('userAction').selectedIndex = 0;
}

function commandHelper(dataContent) {

	var helper = document.getElementById('commandsHelper');

	if (trim(dataContent) != "") {
		
		helper.innerHTML = dataContent;
		
		helper.style.position = 'absolute';
		helper.style.top =  (findPosY(document.getElementById('textInput'))+3)+"px";
		helper.style.left = (findPosX(document.getElementById('textInput'))+2)+"px";
		helper.style.visibility = 'visible';
	}
	
	else {
		helper.style.visibility = 'hidden';
		helper.innerHTML = '';
	}
}
var arAjx = new Array(1);
var arStops = new Array;
function ajFetch(url, destination, cycle, delay, noloading) {
	var timerOne = new Date();
	floatInt = timerOne.getTime();
	//
	
	var curInstance;
	if (cycle == null) {
		cycle = false;	
	}
	if (noloading == null) {
		noloading = false;	
	}
	
	if (arAjx.length == 1) {
		curInstance = 1;
		arAjx[1] = new Ajax;
	} else {
		var found = false;
		for (i=1;i<arAjx.length;i++) {
			if (arAjx[i].Is_Free()) {
				curInstance = i;
				found = true;
			}
		}
		
		if (!found) {
			arAjx.push(new Ajax);
			curInstance = arAjx.length;
			arAjx[curInstance] = new Ajax;
		}
	}
	
	if (noloading) { arAjx[curInstance].Set_Loading(false); }
	
	if (paused) {
		arAjx[curInstance].HTTP_Get(url+"&pause=1", destination);
	} else {
		arAjx[curInstance].HTTP_Get(url, destination);
	}
	
	if (arStops[destination] != true) {
		if (cycle) {
			setTimeout("ajFetch('"+url+"','"+destination+"',true,"+delay+");",delay);
		}
	} else {
		arStops[destination] = null;
	}
}
function Ajax() {
	this.HTTP_Get = HTTP_Get;
	this.Is_Free = Is_Free;
	this.Set_Loading = Set_Loading;
		
	var ajobj = XMLHTTP_object();
	var http_destobj = null;
	var free = true;
	var show_loading = true;

	function HTTP_Get(url, destination) {
		if (free && (url != null) && (destination != null) && ((ajobj.readyState == 0) || (ajobj.readyState == 4))) {
			http_destobj = destination;
			
			ajobj.open("GET", url, true);
			ajobj.onreadystatechange = HTTP_Get_CallBack;
			ajobj.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
			ajobj.send(null);
			free = false;
		} else {
			return false;
		}
	}
	
	function HTTP_Get_CallBack() {
		if (ajobj.readyState == 4) {
			var results = ajobj.responseText;
			
			if (typeof(results)=='string' && results != null && results != '') {
			
				if (eval("typeof(" + http_destobj + ") == 'function'")) { 
				
					eval(http_destobj + "(results);");
				} else {
				
					if (typeof document.getElementById(http_destobj).innerHTML != "undefined") {
						document.getElementById(http_destobj).innerHTML = results;
					}
				}
			}
			free = true;
		}
	}
	

	function Is_Free() {
		return free;	
	}
	
	function Set_Loading(value) {
		show_loading = value;
	}
	

	function XMLHTTP_object() {
	  var xmlhttp;
	  /*@cc_on
	  @if (@_jscript_version >= 5)
		try {
		  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		  try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		  } catch (E) {
			xmlhttp = false;
		  }
		}
	  @else
	  xmlhttp = false;
	  @end @*/
	  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
		  xmlhttp = new XMLHttpRequest();
		} catch (e) {
		  xmlhttp = false;
		}
	  }
	  return xmlhttp;
	}
}

function ajStop(destination) {
	arStops[destination] = true;
}

function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop += obj.offsetHeight;
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
		curtop += obj.height;
	}
	return curtop;
}
	
function array_pop(array, itemname) {
	var i = 0;
	while (i < array.length) {
		if (array[i] == itemname) array.splice(i, 1);
		i++;
  	}
	return array;
}

function urlDecode(str){
    str=str.replace(new RegExp('\\+','g'), ' ');
   	return unescape(str);
}
	
function urlEncode(str){
	str = escape(str);
	str=str.replace(new RegExp('\\+','g'), 'PPP910918271');
	str=str.replace(new RegExp('%20','g'), '+');
	return str;
}
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(input) {
	if (input != null && input != '' && input != ' ') {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		do {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
			keyStr.charAt(enc3) + keyStr.charAt(enc4);
		} while (i < input.length);
   		
		return output;
	}
}
function decode64(input) {
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	do {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output = output + String.fromCharCode(chr1);

		if (enc3 != 64) {
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output = output + String.fromCharCode(chr3);
		}
	} while (i < input.length);
	
	return output;
}


function trim(str) {
	/* Removes white spaces on the beginning and on the end of a string */
	if (str != null && str != '') {
		while (str.charAt(0) == ' ')
			str = str.substring(1);
		while (str.charAt(str.length - 1) == ' ')
			str = str.substring(0, str.length - 1);
	}
	return str;
}