function TMap__put(name, val) {
	if (name==null) return;
	
	//remove any previous version
	this.remove(name);
	
	//now add a new pair
	this.arr[this.arr.length] = new TPair(name, val);
}
function TMap__get(name) {
	for (var i=0;i<this.arr.length;i++) {
		if (this.arr[i].name == name) {
			return this.arr[i].value;
		}
	}
	return null;
}
function TMap__getCount() {
	var count = 0;
	for (var i=0;i<this.arr.length;i++) {
		if (this.arr[i].name != null) count++;
	}
	return count;
}
function TMap__getKeys() {
	var arrKeys = new Array();
	for (var i=0;i<this.arr.length;i++) {
		if (this.arr[i].name != null) arrKeys[arrKeys.length] = this.arr[i].name;
	}
	return arrKeys;
}
function TMap__remove(name) {
	for (var i=0;i<this.arr.length;i++) {
		if (this.arr[i].name == name) {
			this.arr[i].name = null;
			return true;
		}
	}
	return false;
}
function TMap__clear() {
	this.arr = new Array();
}

function TListener__post(msg, obj) {
	if (this.listeners==null) return;
	
	//post the message to the listeners
	var keys = this.listeners.getKeys();
	for (var i=0;i<keys.length;i++) {
		if (this.listeners.get(keys[i])!=null) {
			this.listeners.get(keys[i]).listenEvent(msg, obj);
		}
	}
}
function TListener__add(name, obj) {
	if (this.listeners==null) {
		this.listeners = new TMap();
	}
	
	this.listeners.put(name, obj);
}
function TListener__remove(name) {
	return this.listeners.remove(name);
}
	
function TEventManager__addEvent(id, action) {
	this.arr[this.arr.length] = new TEventObject(id, action);
}
function TEventManager__getEvents(id) {
	var arrEvents = new Array();
	for (var i=0;i<this.arr.length;i++) {
		if (this.arr[i].id == id)
			arrEvents[arrEvents.length] = this.arr[i];
	}
	return arrEvents;
}
function TEventManager__write() {
	var fns = "";

	/**
	 * generic events by all browsers
	 */
	 	
	var arr = this.getEvents(this.DOCUMENT_FORM_SUBMIT);
	if (arr.length>0) {
		//write document.[this.formName].onsubmit events
		fns = "var res=null;\n";
		for (var i=0;i<arr.length;i++)
			fns += "res = " + arr[i].action + "\nif (res!=null && !res) return res;\n";
	
		fns += "return true;\n";
		
		//create the function
		document.frmBody.onsubmit = new Function("documentFormSubmit",fns);
	}
	
	arr = this.getEvents(this.WINDOW_LOAD);
	if (arr.length>0) {
		//write window.onload events
		fns = "";
		for (var i=0;i<arr.length;i++)
			fns += arr[i].action + "\n";
		
		//add the function call to the event
		window.onload = new Function("windowOnload",fns);
	}
	
	arr = this.getEvents(this.WINDOW_RESIZE);
	if (arr.length>0) {
		//write window.onresize events
		fns = "";
		for (var i=0;i<arr.length;i++)
			fns += arr[i].action + "\n";
	
		window.onresize = new Function("windowResize",fns);
	}

	arr = this.getEvents(this.WINDOW_FOCUS);
	if (arr.length>0) {
		//write window.onfocus events
		fns = "";
		for (var i=0;i<arr.length;i++)
			fns += arr[i].action + "\n";
	
		window.onfocus = new Function("windowFocus",fns);
	}

	arr = this.getEvents(this.DOCUMENT_CLICK);
	if (arr.length>0) {
		//write document.onclick events
		fns = "";
		for (var i=0;i<arr.length;i++)
			fns += arr[i].action + "\n";
	
		document.onclick = new Function("documentClick",fns);
	}

	arr = this.getEvents(this.DOCUMENT_MOUSEDOWN);
	if (arr.length>0) {
		//write document.onmousedown events
		fns = "";
		for (var i=0;i<arr.length;i++)
			fns += arr[i].action + "\n";
	
		document.onmousedown = new Function("documentMouseDown",fns);
	}

	/**
	 * browser-specific events
	 */

	arr = this.getEvents(this.WINDOW_SCROLL);
	if (arr.length>0 && document.all) {
		//write window.onresize events
		fns = "";
		for (var i=0;i<arr.length;i++)
			fns += arr[i].action + "\n";
	
		window.onscroll = new Function("windowScroll",fns);
	}

	//bit of a hack - test for objects
	if (document.all || document.layers || document.getElementById) {
		arr = this.getEvents(this.WINDOW_UNLOAD);
		if (arr.length>0) {
			//write window.onunload events
			fns = "";
			for (var i=0;i<arr.length;i++)
				fns += arr[i].action + "\n";
		
			//add the function call to the event
			window.onunload = new Function("windowOnUnload", fns);
		}
	}
}

function TSelectListLoader__load() {
	var arr = this.map.getKeys();
	for (var i=0;i<arr.length;i++) {
		var selectName = arr[i];
		//get the element
		var selectEl = this.getSelectElement(selectName);
		if (selectEl!=null) {
			var options = this.map.get(selectName);
			selectEl.length = options.length;
			var selectedIndex = 0;
			for (var j=0;j<options.length;j++) {
				var pair = options[j];
				selectEl.options[j] = new Option(pair.name, pair.value);
				if (pair.selected) {
					selectedIndex = j;
				}
			}
			//select the selectedIndex!
			selectEl.options[selectedIndex].selected = true;
		}
	}
}
function TSelectListLoader__add(listname, optname, optval, isSelected) {
	var opt = new TSelectListOption(optname, optval, (isSelected)?true:false);
	var arrOpts = this.map.get(listname);
	if (arrOpts==null) {
		arrOpts = new Array();
	}
	arrOpts[arrOpts.length] = opt;
	this.map.put(listname, arrOpts);
}
function TSelectListLoader__getSelectElement(name) {
	if (this.formName==null) {
		for (var i=0;i<document.forms.length;i++) {
			var el = document.forms[i].elements[name];
			if (el!=null) {
				return el;
			}
		}
		return null;
	} else {
		return document.forms[this.formName].elements[name];
	}
}

function TPanel__AddToControl(owner) {
	//currently, only ie5
	if (!(document.all&&document.getElementById)) return;
	
	//can't add more than once
	if (this.elementReference!=null) { return; }
	
	var newElement = document.createElement("DIV");
	newElement.id = "TPanel__ELEMENT__"+this.Name;
	
	newElement.innerHTML = this.Contents;
	newElement.style.visibility = "hidden";
	newElement.style.position = "absolute";
	
	//size
	if (this.Size.Height>0) {
		newElement.style.height = this.Size.Height;
		newElement.height = this.Size.Height;
	}
	if (this.Size.Width>0) {
		newElement.style.width = this.Size.Width;
		newElement.width = this.Size.Width;
	}

	//add to the document
	var insertBeforeObject = owner;
	if (insertBeforeObject==null) {
		insertBeforeObject = document.body;
	}
	this.elementReference = insertBeforeObject.appendChild(newElement);
}
function TPanel__Repaint(e /* the event in NS + Moz */) {
	//currently, only ie5
	if (!(document.all&&document.getElementById)) return;

	if (this.Position.MousePosition!=null) {
		var mouseX = 0;
		var mouseY = 0;
		if (document.all) {
			mouseX = window.event.clientX+window.document.body.scrollLeft;
			mouseY = window.event.clientY+window.document.body.scrollTop;
		} else {
			if (e!=null) {
				mouseX = e.pageX;
				mouseY = e.pageY;
			}
		}
		
		var left = 0;
		var top = 0;
		if (this.Position.MousePosition=='topleft') {
			left = mouseX + this.Position.X;
			top = mouseY + this.Position.Y;
		} else if (this.Position.MousePosition=='topright') {
			left = mouseX - this.Position.X - this.elementReference.clientWidth;
			top = mouseY + this.Position.Y;
		} else if (this.Position.MousePosition=='bottomleft') {
			left = mouseX + this.Position.X;
			top = mouseY - this.Position.Y - this.elementReference.clientHeight;
		} else if (this.Position.MousePosition=='bottomright') {
			left = mouseX - this.Position.X - this.elementReference.clientWidth;
			top = mouseY - this.Position.Y - this.elementReference.clientHeight;
		}

		if (top<window.document.body.scrollTop) {
			top = window.document.body.scrollTop;
		}
		if (left<window.document.body.scrollLeft) {
			left = window.document.body.scrollLeft;
		}

		this.elementReference.style.left = left+'px';
		this.elementReference.style.top = top+'px';
	} else {
		this.elementReference.style.left = this.Position.X+'px';
		this.elementReference.style.top = this.Position.Y+'px';
	}
}
function TPanel__Hide(e) {
	this.elementReference.style.visibility = "hidden";
	this.Repaint(e);
}
function TPanel__Show(e) {
	this.elementReference.style.visibility = "visible";
	this.Repaint(e);
}

//event object constuction
if (document.customObjects==null) {
	document.customObjects = new TMap();
}
document.customObjects.put("eventHandler", new TEventManager());
document.customObjects.put("listener", new TListener());
document.customObjects.put("listLoader", new TSelectListLoader());
//add the listLoader load event
document.customObjects.get("eventHandler").addEvent(
	document.customObjects.get("eventHandler").WINDOW_LOAD,
	'document.customObjects.get("listLoader").load();'
);


