/*
 * Pre-defined Vars ("const")
 */
var TYPE_UNKNOWN = 0;
var TYPE_STRING = 1;
var TYPE_DATE = 2;
var TYPE_IP = 3;

var TYPE_MIN = 0;
var TYPE_MAX = 3;


/*
 * FUNCTIONS
 */


/* Parms:
 *  ip_addr: unsigned 32 bit interger
 * Return: string (dotted IP address)
 */
function inet_ntoa(ip_addr)
{
	return ((ip_addr >> 24) & 0xFF)	+ '.' + ((ip_addr >> 16) & 0xFF) + '.' + ((ip_addr >> 8) & 0xFF) + '.' + (ip_addr & 0xFF);
}

/* Parms:
 *  number: interger
 *  min_length: integer
 * Return: padded string
 */
function left_pad(number, min_length)
{
	var temp_number = number;
	var temp_string = '';
	var length = 0;
	do
	{
		length++;
		temp_number = temp_number / 10;
	} while(temp_number >= 1);

	while (length++ < min_length)
			temp_string += '0';

	return temp_string + number.toString();
}

/* Parms:
 *  date: Date object
 * Return: formatted string
 */
function date_to_string(date)
{
	return date.getFullYear()
		+ '-'
		+ left_pad(date.getMonth() + 1, 2)
		+ '-'
		+ left_pad(date.getDate(), 2)
		+ ' '
		+ left_pad(date.getHours(), 2)
		+ ':'
		+ left_pad(date.getMinutes(), 2)
		+ ':'
		+ left_pad(date.getSeconds(), 2)
		+ ' GMT '
		+ date.getTimezoneOffset()/-60;
}

/* Parms:
 *  cell: TabelCell object
 *  guid: 32 byte hex string representation
 * Return: null
 */
function formatCell_GUID(cell, guid)
{
	if (cell == null)
		return;

	cell.innerHTML = guid;
	cell.style.cursor = 'pointer';
	cell.style.fontFamily = 'Lucida Console';
	cell.style.letterSpacing = '1px';
	var funcPopUp = null;
	eval('funcPopUp = function() {popUp(\'accounthistory.php?guid=' + guid + '\');}');
	cell.onclick = funcPopUp;
}

/* Parms:
 *  cell: TabelCell object
 *  address: unsigned 32 bit interger
 *  port: unsigned 16 bit integer
 * Return: null
 */
function formatCell_ServerIP(cell, address, port)
{
	if (cell == null)
		return;

	cell.innerHTML = inet_ntoa(address);
	if (port != null)
		cell.innerHTML += ':' + port;
	cell.style.cursor = 'pointer';
	var funcPopUp = null;
	eval('funcPopUp = function() {popUp(\'server.php?ip=' + inet_ntoa(address) + ':' + (port == null ? '1716' : port) + '\');}');
	cell.onclick = funcPopUp;
}

/* Parms:
 *  cell: TabelCell object
 *  mac: 10/12 byte hex string representation
 * Return: null
 */
function formatCell_MAC(cell, mac)
{
	if (cell == null)
		return;

	if (mac == '')
	{
		cell.innerHTML = '<i>Unknown</i>';
		return;
	}

	cell.style.fontFamily = 'Lucida Console';
	cell.style.letterSpacing = '1px';

	var i = 0;
	while (i < mac.length - 2)
	{
		cell.innerHTML += mac.charAt(i) + mac.charAt(i+1) + ':';
		i += 2;
	}
	cell.innerHTML += mac.charAt(i) + mac.charAt(i+1);
	if (mac.length == 10)
		cell.innerHTML +=':<i>XX</i>';
}



/*
 * CLASSES
 */


/*Class defTable*/
/* Parms:
 *  table: Table object
 *  columns: defColumn object array
 */
function defTable(table, columns)
{
	this.table = table;
	this.columns = columns;
}

/*Prototype defTable::sort*/
/* Parms:
 *  column: integer
 */
defTable.prototype.sortttt = function(column) {
	if (this.table == null)
		alert('NULL OBJECT WARNING');
	var max = 0;
	var swap = null;
	for (i = 0; i < this.table.rows.length - 1; i++)
	{
		max = i;
		for (j = i + i; j < this.table.rows.length; j++)
		{
			if (this.table.rows[max].innerHTML < this.table.rows[j].innerHTML)
				max = j;
		}

		if (max != i)
		{
			swap = this.table.rows[i].innerHTML;
			this.table.rows[i].innerHTML = this.table.rows[max].innerHTML;
			this.table.rows[max].innerHTML = swap;

			//swap = this.table.rows[i].className;
			//this.table.rows[i].className = this.table.rows[max].className;
			//this.table.rows[max].className = swap;
;
			swap = null;
		}
	}

	return true;
}

/*Class defColumns*/
/* Parms:
 *  type: TYPE_*
 *  title: string
 */
function defColumns(type, title)
{
	this.type = (type >= TYPE_MIN && type <= TYPE_MAX) ? type : TYPE_UNKNOWN;
	this.title = title;	
}



/*Class entryLogin*/
/* Parms:
 *  name: string
 *  guid: 32 byte hex string representation
 *  ip: unsigned 32 bit interger
 *  server_ip: unsigned 32 bit interger
 *  time: 32 bit unix timestamp (stored as Date object)
 *  css_class: string
 */
function entryLogin(name, guid, ip, server_ip, time, css_class)
{
	this.name = name;
	this.guid = guid.toUpperCase();
	this.ip = ip;
	this.server_ip = server_ip;
	this.time = new Date(time * 1000);
	this.css_class = css_class;
}

/*Prototype entryLogin::WriteRowToTable*/
/* Parms:
 *  table: Table object
 * Return: bool
 */
entryLogin.prototype.WriteRowToTable = function(table) {
	if (table == null)
		return false;

	var row = table.insertRow(table.rows.length);

	if (row == null)
		return false;

	row.className = this.css_class;

	//Insert 5 cells
	row.insertCell(0);
	row.insertCell(1);
	row.insertCell(2);
	row.insertCell(3);
	row.insertCell(4);

	if (row.cells[0])
		row.cells[0].innerHTML = this.name;

	if (row.cells[1])
		formatCell_GUID(row.cells[1], this.guid);

	if (row.cells[2])
		row.cells[2].innerHTML = inet_ntoa(this.ip);

	if (row.cells[3])
		formatCell_ServerIP(row.cells[3], this.server_ip, null);

	if (row.cells[4])
		row.cells[4].innerHTML = (this.time.getTime() > 0) ? date_to_string(this.time) : '<i>Unknown</i>';

	return true;
}



/*Class entryLogTable*/
/* Parms:
 *  name: string
 *  guid: 32 byte hex string representation
 *  ip: unsigned 32 bit interger
 *  server_ip: unsigned 32 bit interger
 *  time: 32 bit unix timestamp (stored as Date object)
 *  log_line: string
 *  css_class: string
 */
function entryLogTable(name, guid, ip, server_ip, time, log_line, css_class)
{
	this.name = name;
	this.guid = guid.toUpperCase();
	this.ip = ip;
	this.server_ip = server_ip;
	this.time = new Date(time * 1000);
	this.log_line = log_line;
	this.css_class = css_class;
}

/*Prototype entryLogTable::WriteRowToTable*/
/* Parms:
 *  table: Table object
 * Return: bool
 */
entryLogTable.prototype.WriteRowToTable = function(table) {
	if (table == null)
		return false;

	var row = table.insertRow(table.rows.length);

	if (row == null)
		return false;

	row.className = this.css_class;

	//Insert 6 cells
	row.insertCell(0);
	row.insertCell(1);
	row.insertCell(2);
	row.insertCell(3);
	row.insertCell(4);
	row.insertCell(5);

	if (row.cells[0])
		row.cells[0].innerHTML = this.name;

	if (row.cells[1])
		formatCell_GUID(row.cells[1], this.guid);

	if (row.cells[2])
		row.cells[2].innerHTML = inet_ntoa(this.ip);

	if (row.cells[3])
	{
		row.cells[3].innerHTML = this.log_line;
		row.cells[3].style.fontFamily = 'Lucida Console';
		row.cells[3].style.letterSpacing = '1px';
	}

	if (row.cells[4])
		formatCell_ServerIP(row.cells[4], this.server_ip, null);


	if (row.cells[5])
		row.cells[5].innerHTML = (this.time.getTime() > 0) ? date_to_string(this.time) : '<i>Unknown</i>';

	return true;
}



/*Class entryAccountTable*/
/* Parms:
 *  name: string
 *  guid: 32 byte hex string representation
 *  ip: unsigned 32 bit interger
 *  mac: 12 byte hex string representation
 *  time: 32 bit unix timestamp (stored as Date object)
 *  css_class: string
 */
function entryAccountTable(name, guid, ip, mac, time, css_class)
{
	this.name = name;
	this.guid = guid.toUpperCase();
	this.ip = ip;
	this.mac = mac;
	this.time = new Date(time * 1000);
	this.css_class = css_class;
}

/*Prototype entryAccountTable::WriteRowToTable*/
/* Parms:
 *  table: Table object
 * Return: bool
 */
entryAccountTable.prototype.WriteRowToTable = function(table) {
	if (table == null)
		return false;

	var row = table.insertRow(table.rows.length);

	if (row == null)
		return false;

	row.className = this.css_class;

	//Insert 5 cells
	row.insertCell(0);
	row.insertCell(1);
	row.insertCell(2);
	row.insertCell(3);
	row.insertCell(4);

	if (row.cells[0])
		row.cells[0].innerHTML = this.name;

	if (row.cells[1])
		formatCell_GUID(row.cells[1], this.guid);

	if (row.cells[2])
		row.cells[2].innerHTML = (this.ip > 0) ? inet_ntoa(this.ip) : '<i>Unknown</i>';

	if (row.cells[3])
		formatCell_MAC(row.cells[3], this.mac);

	if (row.cells[4])
		row.cells[4].innerHTML = (this.time.getTime() > 0) ? date_to_string(this.time) : '<i>Unknown</i>';

	return true;
}



/*Class entryLinkedAccountTable*/
/* Parms:
 *  name: string
 *  name_linked: string
 *  guid: 32 byte hex string representation
 *  guid_linked: 32 byte hex string representation
 *  ip: unsigned 32 bit interger
 *  ip_linked: unsigned 32 bit interger
 *  mac: 12 byte hex string representation
 *  mac_linked: 12 byte hex string representation
 *  time: 32 bit unix timestamp (stored as Date object)
 *  css_class: string
 */
function entryLinkedAccountTable(name, name_linked, guid, guid_linked, ip, ip_linked, mac, mac_linked, time, css_class)
{
	this.name = name;
	this.guid = guid.toUpperCase();
	this.ip = ip;
	this.mac = mac;
	this.time = new Date(time * 1000);
	this.css_class = css_class;

	this.name_linked = name_linked;
	this.guid_linked = guid_linked.toUpperCase();
	this.ip_linked = ip_linked;
	this.mac_linked = mac_linked;
}

/*Prototype entryLinkedAccountTable::WriteRowToTable*/
/* Parms:
 *  table: Table object
 * Return: bool
 */
entryLinkedAccountTable.prototype.WriteRowToTable = function(table) {
	if (table == null)
		return false;

	var row = table.insertRow(table.rows.length);

	if (row == null)
		return false;

	row.className = this.css_class;

	//Insert 6 cells
	row.insertCell(0);
	row.insertCell(1);
	row.insertCell(2);
	row.insertCell(3);
	row.insertCell(4);
	row.insertCell(5);

	if (row.cells[0])
	{
		row.cells[0].innerHTML = this.name;
		if (this.name == this.name_linked)
			row.cells[0].style.backgroundColor = '#688502';
	}

	if (row.cells[1])
		formatCell_GUID(row.cells[1], this.guid);

	if (row.cells[2])
	{
		row.cells[2].innerHTML = (this.ip > 0) ? inet_ntoa(this.ip) : '<i>Unknown</i>';
		if (this.ip > 0 && this.ip == this.ip_linked)
			row.cells[2].style.backgroundColor = '#688502';
	}

	if (row.cells[3])
	{
		formatCell_MAC(row.cells[3], this.mac);
		if (this.mac.length > 0 && this.mac == this.mac_linked)
			row.cells[3].style.backgroundColor = '#688502';
	}

	if (row.cells[4])
		row.cells[4].innerHTML = (this.time.getTime() > 0) ? date_to_string(this.time) : '<i>Unknown</i>';

	if (row.cells[5])
		row.cells[5].innerHTML = '<form action="logincmp.php" method="POST" style="margin: 0px;"><input type="hidden" name="searchquery" value="' + this.guid_linked + '"><input type="hidden" name="searchquery2" value="' + this.guid + '"><input type="submit" value="View Link Info"></form>';

	return true;
}



/*Class entryServerTable*/
/* Parms:
 *  name: string
 *  address: unsigned 32 bit interger
 *  port: unsigned 16 bit interger
 *  version: string
 *  source: string
 *  user: string
 *  time: 32 bit unix timestamp (stored as Date object)
 *  css_class: string
 */
function entryServerTable(name, address, port, version, source, user, time, css_class)
{
	this.name = name;
	this.address = address;
	this.port = port;
	this.version = version;
	this.source = source;
	this.user = user;
	this.time = new Date(time * 1000);
	this.css_class = css_class;
}

/*Prototype entryServerTable::WriteRowToTablew*/
/* Parms:
 *  table: Table object
 * Return: bool
 */
entryServerTable.prototype.WriteRowToTable = function(table) {
	if (table == null)
		return false;

	var row = table.insertRow(table.rows.length);

	if (row == null)
		return false;

	row.className = this.css_class;

	//Insert 9 cells
	row.insertCell(0);
	row.insertCell(1);
	row.insertCell(2);
	row.insertCell(3);
	row.insertCell(4);
	row.insertCell(5);
	row.insertCell(6);
	row.insertCell(7);
	row.insertCell(8);

	if (row.cells[0])
		row.cells[0].innerHTML = (this.name != '') ? this.name : '<i>Unknown</i>';

	if (row.cells[1])
		formatCell_ServerIP(row.cells[1], this.address, this.port);

	if (row.cells[2])
		row.cells[2].innerHTML = this.version;

	if (row.cells[3])
		row.cells[3].innerHTML = this.source;

	if (row.cells[4])
		row.cells[4].innerHTML = (this.user != '') ? this.user : '<i>Unknown</i>';

	if (row.cells[5])
		row.cells[5].innerHTML = (this.time.getTime() > 0) ? date_to_string(this.time) : '<i>Unknown</i>';

	row.cells[6].innerHTML = '<a href="./logins.php?searchquery=' + inet_ntoa(this.address) + '&amp;searchtype=4"><img src="./images/links/logins.gif" alt="Logins" title="Logins" border="0"></a>';
	row.cells[7].innerHTML = '<a href="./violations.php?searchquery=' + inet_ntoa(this.address) + '&amp;searchtype=5"><img src="./images/links/violations.gif" alt="Logins" title="Logins" border="0"></a>';
	row.cells[8].innerHTML = '<a href="./kicks.php?searchquery=' + inet_ntoa(this.address) + '&amp;searchtype=5"><img src="./images/links/kicks.gif" alt="Logins" title="Logins" border="0"></a>';

	return true;
}