/** shortcur for getElementById() **/
var $=function(id){return document.getElementById(id);};

/** onloadStack, allows for multiple events to happen onload **/
var __onloadStack = new Array();

window.onload = function() {
	for (i in __onloadStack) {
		__onloadStack[i]();
	}
}


/**
 * Prototype the function object with bind. This allows to execute the function
 * in the scope of an object instance
 */
Function.prototype.bind = function(object) {
	var _method = this;
	return function() { return _method.apply(object, arguments); }
}


/** string **/
function lTrim(string) { var space = ' ';
	for (var i=0; i<string.length; i++) {if (string.charAt(i) != space) { return string.substring(i,string.length); } }
	return '';
}
function rTrim(string) {
	var space = ' ';
	for (var i=string.length; i>0; i--) { if (string.charAt(i-1) != space) { return string.substring(0,i); } }
	return '';
}
function trim(string) { return lTrim(rTrim(string)); }
function createRandomId(prefix) {
	if (!prefix) { prefix = 'rnd'; }
	var rnd = Math.ceil(10000*Math.random());
	var id = '' + prefix + rnd;
	while (document.getElementById(id)) {
		var rnd = 1000*Math.random();
		id = '' + prefix + rnd;
	}
	return id;
}
/** Dom helper **/
function element(type) { return document.createElement(type); }
function plainElement(type, text) { var e = element(type); e.appendChild(document.createTextNode(text)); return e; }
function createLabel(forid, text) {	var l = plainElement('label', text); l.setAttribute('for', forid); return l; }
function createInput(type, id, value) { i = document.createElement('input'); i.type=type; i.id=id; i.name=id; if (value.length) { i.value=value; }	return i; }
function createTextArea(id, value) { var t = plainElement('textarea', value); t.id = id; return t; }

/***********************
 * Form helper
 ***********************/
function getSelectedVal(selectNode) {
	var val = false;
	if (selectNode.nodeName.toLowerCase() == 'select') {
		var val = selectNode.options[selectNode.selectedIndex].value;
	}
	return val;
}

function getSelectOptions(selectNode) {
	var val = false;
	if (selectNode.nodeName.toLowerCase() == 'select') {
		var val = new Array;

		for (i=0; i<selectNode.length; i++) {
			if (selectNode.options[i] && selectNode.options[i].value) {
				val.push(selectNode.options[i].value);
			}
		}
	}
	return val;
}
