if (!window.A8) var A8 = {}
if (!A8.Misc) A8.Misc = {}
if (!A8.Widgets) A8.Widgets = {}
if (!A8.Widgets.Objs) A8.Widgets.Objs = [];
if (!A8.Widgets.Object) A8.Widgets.Object = {}
if (!A8.Widgets.Text) A8.Widgets.Text = {}
if (!A8.Widgets.Checkbox) A8.Widgets.Checkbox = {}
if (!A8.Widgets.Radiobutton) A8.Widgets.Radiobutton = {}
if (!A8.Widgets.Button) A8.Widgets.Button = {}


A8.Misc.Defaultstyle = 'default';

A8.Misc.getLeft = function(obj) {
	if (!document.layers) {
		var x = 0;
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var lastOffset = 0;
		while (obj){
			if (obj.leftMargin && ! onWindows ) x += parseInt(obj.leftMargin);
			if ((obj.offsetLeft != lastOffset) && obj.offsetLeft ) x += parseInt(obj.offsetLeft);
			if (obj.offsetLeft != 0 ) lastOffset = obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		return obj.x;
	}
	return x;
}

A8.Misc.getTop = function(obj) {
	if(!document.layers) {
		var y = 0;
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var lastOffset = 0;
		while (obj){
			if (obj.topMargin && !onWindows ) y += parseInt(obj.topMargin);
			if ((obj.offsetTop != lastOffset) && obj.offsetTop ) y += parseInt(obj.offsetTop);
			if (obj.offsetTop != 0 ) lastOffset = obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (img.y >= 0) {
		return obj.y;
	}
	return y;
}
A8.Misc.fixE = function(e) {
	if (!e) { e = window.event; }
	return e;
}
A8.Misc.stopEvent = function(e) {
	e.cancelBubble = true;
	if (e.stopPropagation) {
		e.stopPropagation();
	}
}
A8.Misc.addEvent = function(obj, event, callback) {
	if (obj.addEventListener) {
		obj.addEventListener(event, callback, false);
	} else {
		obj.attachEvent('on' + event, callback);
	}
}
A8.Misc.removeEvent = function(obj,event,callback) {
	if (obj.removeEventListener) {
		obj.removeEventListener(event, callback, false);
	} else {detachEvent
		obj.detachEvent('on' + event, callback);
	}
}

/********** Main **********/
A8.Widgets.FOCUS		= 0x01;
A8.Widgets.MOUSEOVER	= 0x02;
A8.Widgets.MOUSEDOWN	= 0x04;
A8.Widgets.DISABLED		= 0x08;
A8.Widgets.OPEN			= 0x10;
A8.Widgets.BUTTON_SUBMIT	= 1;
A8.Widgets.BUTTON_RESET		= 2;
A8.Widgets.BUTTON_NORMAL	= 3;
A8.Widgets.Objs = [];

/********** Object **********/
A8.Widgets.Object = function() {}
A8.Widgets.Object.prototype.init = function(name, parent) {
	// Set name and get parent
	this.name = name;
	this.parent = document.getElementById(parent);
	// Get a place in the Objs array
	this.id = A8.Widgets.Objs.length;
	A8.Widgets.Objs[this.id] = this;
	// Set default settings
	this.element = null;
	this.style = A8.Misc.Defaultstyle;
	this.value = null;
	this.state = 0;
	this.caption = '';
	this.rendered = false;
	this.callback_change = null;
}
A8.Widgets.Object.prototype.Style = function(style) {
	if (style) {
		this.style = style;
		if (this.rendered && this._update) {
			this._update();
		}
	} else {
		return this.style;
	}
}
A8.Widgets.Object.prototype.Value = function(value) {
	if (this._value) {
		return this._value(value);
	}
	if (value) {
		this.value = value;
		if (this.rendered && this._update) {
			this._update();
		}
	} else {
		return this.value;
	}
}
A8.Widgets.Object.prototype.Caption = function(caption) {
	if (caption) {
		this.caption = caption;
		if (this.rendered && this._update) {
			this._update();
		}
	} else {
		return this.caption;
	}
}
A8.Widgets.Object.prototype.Disabled = function(state) {
	if (state) {
		this.state |= A8.Widgets.DISABLED;
	} else {
		this.state &= ~A8.Widgets.DISABLED;
	}
	if (this.rendered && this._update) {
		this._update();
	}
}
A8.Widgets.Object.prototype.Render = function() {
	if (this._render) {
		this._render();
	}
	this.rendered = true;
}
A8.Widgets.Object.prototype.Callback_Change = function(func) {
	this.callback_change = func;
}

/********** Text **********/
A8.Widgets.Text = function(name, parent, multiline) {
	this.init(name, parent);
	this.multiline = multiline;
	return this;
}
A8.Widgets.Text.MouseOver = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state |= A8.Widgets.MOUSEOVER;
	if (!(elm.state & A8.Widgets.FOCUS) && !(elm.state & A8.Widgets.DISABLED)) {
		elm.element.className = elm.style + '_' + (elm.multiline ? 'textarea' : 'textfield') + '_over';
	}
}
A8.Widgets.Text.MouseOut = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state &= ~A8.Widgets.MOUSEOVER;
	if (!(elm.state & A8.Widgets.FOCUS) && !(elm.state & A8.Widgets.DISABLED)) {
		elm.element.className = elm.style + '_' + (elm.multiline ? 'textarea' : 'textfield');
	}
}
A8.Widgets.Text.Focus = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.value = elm.element.value = '';
	elm.state |= A8.Widgets.FOCUS;
	if (!(elm.state & A8.Widgets.DISABLED)) {
		elm.element.className = elm.style + '_' + (elm.multiline ? 'textarea' : 'textfield') + '_focus';
	}
}
A8.Widgets.Text.Blur = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state &= ~A8.Widgets.FOCUS;
	if (!(elm.state & A8.Widgets.DISABLED)) {
		if (elm.state & A8.Widgets.MOUSEOVER) {
			elm.element.className = elm.style + '_' + (elm.multiline ? 'textarea' : 'textfield') + '_over';
		} else {
			elm.element.className = elm.style + '_' + (elm.multiline ? 'textarea' : 'textfield');
		}
	}
}
A8.Widgets.Text.Change = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.value = elm.element.value;
	if (elm.callback_change) {
		elm.callback_change(elm);
	}
}

A8.Widgets.Text.prototype = new A8.Widgets.Object();
A8.Widgets.Text.prototype._render = function() {
	if (this.multiline) {
		this.element = document.createElement('textarea');
		this.element.className = this.style + '_textarea' + ((this.state & A8.Widgets.DISABLED) ? '_disabled' : '');
	} else {
		this.element = document.createElement('input');
		this.element.type = 'text';
		this.element.className = this.style + '_textfield' + ((this.state & A8.Widgets.DISABLED) ? '_disabled' : '');
	}
	this.element.name = this.name;
	this.element.id = this.name;
	this.element.oid = this.id;
	this.element.disabled = (this.state & A8.Widgets.DISABLED) ? true : false;
	this.element.value = (this.value) ? this.value : '';

	this.element.onmouseover = function() { A8.Widgets.Text.MouseOver(this.oid); }
	this.element.onmouseout = function() { A8.Widgets.Text.MouseOut(this.oid); }
	this.element.onfocus = function() { A8.Widgets.Text.Focus(this.oid); }
	this.element.onblur = function() { A8.Widgets.Text.Blur(this.oid); }
	this.element.onchange = function() { A8.Widgets.Text.Change(this.oid); }

	this.parent.appendChild(this.element);

}
A8.Widgets.Text.prototype._update = function() {
}



/********** Button **********/
A8.Widgets.Button = function(name, parent, type, callback) {
	this.init(name,parent);
	this.type = type;
	this.callback = callback;
	this.element = new Array();
}
A8.Widgets.Button.MouseOver = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state |= A8.Widgets.MOUSEOVER;
	if (!(elm.state & A8.Widgets.FOCUS) && !(elm.state & A8.Widgets.DISABLED) && !(elm.state & A8.Widgets.MOUSEDOWN)) {
		elm.element['left'].className = elm.style + '_button_left_over';
		elm.element['center'].className = elm.style + '_button_over';
		elm.element['right'].className = elm.style + '_button_right_over';
	} else if (!(elm.state & A8.Widgets.DISABLED) && elm.state & A8.Widgets.MOUSEDOWN) {
		elm.element['left'].className = elm.style + '_button_left_down';
		elm.element['center'].className = elm.style + '_button_down';
		elm.element['right'].className = elm.style + '_button_right_down';
	}
}
A8.Widgets.Button.MouseOut = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state &= ~(A8.Widgets.MOUSEOVER | A8.Widgets.MOUSEDOWN);
	if (!(elm.state & A8.Widgets.FOCUS) && !(elm.state & A8.Widgets.DISABLED)) {
		elm.element['left'].className = elm.style + '_button_left';
		elm.element['center'].className = elm.style + '_button';
		elm.element['right'].className = elm.style + '_button_right';
	}
}
A8.Widgets.Button.Focus = function(id) {
	var elm = A8.Widgets.Objs[id];
	if (!(elm.state & A8.Widgets.DISABLED)) {
		elm.element['left'].className = elm.style + '_button_left_focus';
		elm.element['center'].className = elm.style + '_button_focus';
		elm.element['right'].className = elm.style + '_button_right_focus';
	}
}
A8.Widgets.Button.Blur = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state &= ~A8.Widgets.FOCUS;
	if (!(elm.state & A8.Widgets.DISABLED)) {
		if (elm.state & A8.Widgets.MOUSEOVER) {
			elm.element['left'].className = elm.style + '_button_left_over';
			elm.element['center'].className = elm.style + '_button_over';
			elm.element['right'].className = elm.style + '_button_right_over';
		} else {
			elm.element['left'].className = elm.style + '_button_left';
			elm.element['center'].className = elm.style + '_button';
			elm.element['right'].className = elm.style + '_button_right';
		}
	}
}
A8.Widgets.Button.MouseDown = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state |= A8.Widgets.MOUSEDOWN;
	if (!(elm.state & A8.Widgets.DISABLED)) {
		elm.element['left'].className = elm.style + '_button_left_down';
		elm.element['center'].className = elm.style + '_button_down';
		elm.element['right'].className = elm.style + '_button_right_down';
	}
}
A8.Widgets.Button.MouseUp = function(id) {
	var elm = A8.Widgets.Objs[id];
	if (elm.state & A8.Widgets.MOUSEDOWN) {
		elm.state &= ~A8.Widgets.MOUSEDOWN;
		if (!(elm.state & A8.Widgets.DISABLED)) {
			elm.element['left'].className = elm.style + '_button_left';
			elm.element['center'].className = elm.style + '_button';
			elm.element['right'].className = elm.style + '_button_right';
			elm.callback();
		}
	}
}
A8.Widgets.Button.Click = function(id) {
	var elm = A8.Widgets.Objs[id];
	if (!(elm.state & A8.Widgets.DISABLED)) {
		elm.callback();
	}
}
A8.Widgets.Button.prototype = new A8.Widgets.Object();
A8.Widgets.Button.prototype._render = function() {
	// Create form element
	this.element['input'] = document.createElement('input');
	this.element['input'].type = (this.type == A8.Widgets.BUTTON_SUBMIT) ? 'submit' : ((this.type == A8.Widgets.BUTTON_RESET) ? 'reset' : 'button');
	this.element['input'].value = (this.value) ? this.value : '';
	this.element['input'].id = this.name;
	this.element['input'].oid = this.id;
	this.element['input'].name = this.name;
	this.element['input'].className = this.style + '_hidden';
	this.element['input'].disabled = (this.state & A8.Widgets.DISABLED) ? true : false;
	this.element['input'].onmouseover = function() { A8.Widgets.Button.MouseOver(this.oid); }
	this.element['input'].onmouseout = function() { A8.Widgets.Button.MouseOut(this.oid); }
	this.element['input'].onfocus = function() { A8.Widgets.Button.Focus(this.oid); }
	this.element['input'].onblur = function() { A8.Widgets.Button.Blur(this.oid); }
	this.element['input'].onmousedown = function() { A8.Widgets.Button.MouseDown(this.oid); }
	this.element['input'].onmouseup = function() { A8.Widgets.Button.MouseUp(this.oid); }
	this.element['input'].onclick = function() { A8.Widgets.Button.Click(this.oid); }
	this.parent.appendChild(this.element['input']);

	// Create holder table
	var ttable = document.createElement('table');
	ttable.className = this.style + '_button_table';
	ttable.cellPadding = 0;
	ttable.cellSpacing = 0;
	var tbody = ttable.appendChild(document.createElement('tbody'));
	var ttr = tbody.appendChild(document.createElement('tr'));

	// Left side
	this.element['left'] = document.createElement('td');
	this.element['left'].oid = this.id;
	this.element['left'].className = this.style + '_button_left' + ((this.state & A8.Widgets.DISABLED) ? '_disabled' : '');
	this.element['left'].onmouseover = function() { A8.Widgets.Button.MouseOver(this.oid); }
	this.element['left'].onmouseout = function() { A8.Widgets.Button.MouseOut(this.oid); }
	this.element['left'].onmousedown = function() { A8.Widgets.Button.MouseDown(this.oid); }
	this.element['left'].onmouseup = function() { A8.Widgets.Button.MouseUp(this.oid); }
	// Center side
	this.element['center'] = document.createElement('td');
	this.element['center'].oid = this.id;
	this.element['center'].className = this.style + '_button' + ((this.state & A8.Widgets.DISABLED) ? '_disabled' : '');
	this.element['center'].onmouseover = function() { A8.Widgets.Button.MouseOver(this.oid); }
	this.element['center'].onmouseout = function() { A8.Widgets.Button.MouseOut(this.oid); }
	this.element['center'].onmousedown = function() { A8.Widgets.Button.MouseDown(this.oid); }
	this.element['center'].onmouseup = function() { A8.Widgets.Button.MouseUp(this.oid); }
	this.element['center'].innerHTML = this.caption;
	// Right center
	this.element['right'] = document.createElement('td');
	this.element['right'].oid = this.id;
	this.element['right'].className = this.style + '_button_right' + ((this.state & A8.Widgets.DISABLED) ? '_disabled' : '');
	this.element['right'].onmouseover = function() { A8.Widgets.Button.MouseOver(this.oid); }
	this.element['right'].onmouseout = function() { A8.Widgets.Button.MouseOut(this.oid); }
	this.element['right'].onmousedown = function() { A8.Widgets.Button.MouseDown(this.oid); }
	this.element['right'].onmouseup = function() { A8.Widgets.Button.MouseUp(this.oid); }
	// Add to table and show it
	ttr.appendChild(this.element['left']);
	ttr.appendChild(this.element['center']);
	ttr.appendChild(this.element['right']);
	this.parent.appendChild(ttable);
}

/********** Select **********/
A8.Widgets.Select = function(name, parent, items) {
	this.init(name,parent);
	this.items = items;
	this.element = new Array();
}
A8.Widgets.Select._open = null;
A8.Widgets.Select.MouseOver = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state |= A8.Widgets.MOUSEOVER;
	if (!(elm.state & A8.Widgets.FOCUS) && !(elm.state & A8.Widgets.DISABLED)) {
		elm.element['left'].className = elm.style + '_select_left_over';
		elm.element['center'].className = elm.style + '_select_over';
		elm.element['right'].className = elm.style + '_select_right_over';
	} else if (!(elm.state & A8.Widgets.DISABLED) && elm.state & A8.Widgets.MOUSEDOWN) {
		elm.element['left'].className = elm.style + '_select_left_down';
		elm.element['center'].className = elm.style + '_select_down';
		elm.element['right'].className = elm.style + '_select_right_down';
	}
}
A8.Widgets.Select.MouseOut = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state &= ~(A8.Widgets.MOUSEOVER);
	if (!(elm.state & A8.Widgets.FOCUS) && !(elm.state & A8.Widgets.DISABLED)) {
		elm.element['left'].className = elm.style + '_select_left';
		elm.element['center'].className = elm.style + '_select';
		elm.element['right'].className = elm.style + '_select_right';
	}
}
A8.Widgets.Select.Focus = function(id) {
	var elm = A8.Widgets.Objs[id];
	if (!(elm.state & A8.Widgets.DISABLED)) {
		elm.element['left'].className = elm.style + '_select_left_focus';
		elm.element['center'].className = elm.style + '_select_focus';
		elm.element['right'].className = elm.style + '_select_right_focus';
	}
}
A8.Widgets.Select.Blur = function(id) {
	var elm = A8.Widgets.Objs[id];
	elm.state &= ~A8.Widgets.FOCUS;
	if (!(elm.state & A8.Widgets.DISABLED)) {
		if (elm.state & A8.Widgets.MOUSEOVER) {
			elm.element['left'].className = elm.style + '_select_left_over';
			elm.element['center'].className = elm.style + '_select_over';
			elm.element['right'].className = elm.style + '_select_right_over';
		} else {
			elm.element['left'].className = elm.style + '_select_left';
			elm.element['center'].className = elm.style + '_select';
			elm.element['right'].className = elm.style + '_select_right';
		}
	}
}
A8.Widgets.Select.Click = function(id) {
	var elm = A8.Widgets.Objs[id];
	if (elm.state & A8.Widgets.OPEN) {
		elm.state &= ~A8.Widgets.OPEN;
		elm.element['list'].style.visibility = 'hidden';
		A8.Misc.removeEvent(document, 'click', A8.Widgets.Select.Off);
	} else {
		elm.state |= A8.Widgets.OPEN;
		elm.element['list'].style.left = A8.Misc.getLeft(elm.element['table']) + 'px';
		elm.element['list'].style.top = (A8.Misc.getTop(elm.element['table']) + elm.element['table'].offsetHeight) + 'px';
		elm.element['list'].style.width = elm.element['center'].offsetWidth + 'px';
		elm.element['list'].style.visibility = 'visible';
		A8.Widgets.Select._open = elm;
		A8.Misc.addEvent(document, 'click', A8.Widgets.Select.Off);
	}
}
A8.Widgets.Select.Off = function(e) {
	A8.Misc.removeEvent(document, 'click', A8.Widgets.Select.Off);
	var elm = A8.Widgets.Select._open;
	elm.state &= ~A8.Widgets.OPEN;
	elm.element['list'].style.visibility = 'hidden';
}
A8.Widgets.Select.Select = function(id,item) {
	A8.Misc.removeEvent(document, 'click', A8.Widgets.Select.Off);
	var elm = A8.Widgets.Objs[id];
	if (!(elm.state & A8.Widgets.DISABLED)) {
		elm.state &= ~A8.Widgets.OPEN;
		if (elm.element['li'][elm.value-1]) {
			elm.element['li'][elm.value-1].selected = false;
			elm.element['li'][elm.value-1].className = '';
		}
		elm.element['li'][item].className = 'selected';
		elm.element['li'][item].selected = true;
		elm.value = item + 1;
		elm.element['input'].value = elm.items[item][0];
		elm.element['center'].innerHTML = elm.items[item][1];
		elm.element['list'].style.visibility = 'hidden';
		if (elm.callback_change) {
			elm.callback_change(elm);
		}
	}
}
A8.Widgets.Select.prototype = new A8.Widgets.Object();
A8.Widgets.Select.prototype._render = function() {
	var ttable, tbody, ttr;

	// *** Create form element
	this.element['input'] = document.createElement('input');
	this.element['input'].type = 'text';
	this.element['input'].readOnly = true;
	this.element['input'].id = this.name;
	this.element['input'].name = this.name;
	this.element['input'].oid = this.id;
	this.element['input'].className = this.style + '_hidden';
	this.element['input'].disabled = (this.state & A8.Widgets.DISABLED) ? true : false;
	this.element['input'].value = (this.value) ? this.items[this.value - 1][0] : '';
	this.element['input'].onfocus = function() { A8.Widgets.Select.Focus(this.oid); }
	this.element['input'].onblur = function() { A8.Widgets.Select.Blur(this.oid); }
	this.parent.appendChild(this.element['input']);

	// *** Create holder table and sides
	this.element['table'] = document.createElement('table');
	this.element['table'].className = this.style + '_select_table';
	this.element['table'].cellPadding = 0;
	this.element['table'].cellSpacing = 0;
	tbody = this.element['table'].appendChild(document.createElement('tbody'));
	ttr = tbody.appendChild(document.createElement('tr'));

	// Left side
	this.element['left'] = document.createElement('td');
	this.element['left'].oid = this.id;
	this.element['left'].className = this.style + '_select_left' + ((this.state & A8.Widgets.DISABLED) ? '_disabled' : '');
	this.element['left'].onmouseover = function() { A8.Widgets.Select.MouseOver(this.oid); }
	this.element['left'].onmouseout = function() { A8.Widgets.Select.MouseOut(this.oid); }
	this.element['left'].onclick = function(e) { e=A8.Misc.fixE(e);A8.Misc.stopEvent(e); A8.Widgets.Select.Click(this.oid); }
	// Center side
	this.element['center'] = document.createElement('td');
	this.element['center'].oid = this.id;
	this.element['center'].className = this.style + '_select' + ((this.state & A8.Widgets.DISABLED) ? '_disabled' : '');
	this.element['center'].onmouseover = function() { A8.Widgets.Select.MouseOver(this.oid); }
	this.element['center'].onmouseout = function() { A8.Widgets.Select.MouseOut(this.oid); }
	this.element['center'].onclick = function(e) { e=A8.Misc.fixE(e);A8.Misc.stopEvent(e);A8.Widgets.Select.Click(this.oid); }
	this.element['center'].innerHTML = (this.value) ? this.items[this.value-1][1] : '';
	// Right center
	this.element['right'] = document.createElement('td');
	this.element['right'].oid = this.id;
	this.element['right'].className = this.style + '_select_right' + ((this.state & A8.Widgets.DISABLED) ? '_disabled' : '');
	this.element['right'].onmouseover = function() { A8.Widgets.Select.MouseOver(this.oid); }
	this.element['right'].onmouseout = function() { A8.Widgets.Select.MouseOut(this.oid); }
	this.element['right'].onclick = function(e) { e=A8.Misc.fixE(e);A8.Misc.stopEvent(e); A8.Widgets.Select.Click(this.oid); }
	// Add to table and show it
	ttr.appendChild(this.element['left']);
	ttr.appendChild(this.element['center']);
	ttr.appendChild(this.element['right']);
	this.parent.appendChild(this.element['table']);


	// *** Create select list
	this.element['li'] = new Array();
	this.element['list'] = document.createElement('ul');
	this.element['list'].className = this.style + '_select_list';
	for (var i = 0; i < this.items.length; i++) {
		this.element['li'][i] = document.createElement('li');
		var txt = document.createTextNode(this.items[i][1]);
		this.element['li'][i].appendChild(txt);
		this.element['li'][i].oid = this.id;
		this.element['li'][i].i = i;
		if ((this.value - 1) == i) {
			this.element['li'][i].className = 'selected';
			this.element['li'][i].selected = true;
 		}
		this.element['li'][i].onclick = function(e) {e=A8.Misc.fixE(e);A8.Misc.stopEvent(e); A8.Widgets.Select.Select(this.oid, this.i); }
		this.element['li'][i].onmouseover = function() { this.className = 'hover'; }
		this.element['li'][i].onmouseout = function() { this.className = (this.selected) ?'selected' : ''; }
		if (this.items[i][2]) {
			this.element['li'][i].className = this.items[i][2];
		}
		this.element['list'].appendChild(this.element['li'][i]);
	}
	this.parent.appendChild(this.element['list']);
}
A8.Widgets.Select._update = function() {

}
A8.Widgets.Select.prototype._value = function(value) {
	if (value) {
		for (var i = 0; i < this.items.length; i++) {
			if (this.items[i][0] == value) {
				this.value = i + 1;
				if (this.rendered) {
					this._update();
				}
				return;
			}
		}
		this.value = 0;
	} else {
		return this.value;
	}
}

