
function trim(stringToTrim) {
	if(!stringToTrim || stringToTrim.length==0) return stringToTrim;
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function getAbsTop(o) {
	oTop = o.offsetTop;
	while(o.offsetParent!=null && getCurrentStyle(o.offsetParent,'position')!='absolute') {
		oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop;
}
function getAbsLeft(o) {
	oLeft = o.offsetLeft;
	while(o.offsetParent!=null && getCurrentStyle(o.offsetParent,'position')!='absolute') {
		oParent = o.offsetParent;
		oLeft += oParent.offsetLeft;
		o = oParent;
	}
	return oLeft;
}

function getCurrentStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}


function isParentOf(parent,child){
	if(!parent || !child) return false;
	if(parent == child) return true;
	if(child.parentNode){
		while(child = child.parentNode){
			if(parent == child) return true;			
		}
	}
	return false;
}

function indexOf(arr, elt /*, from*/){
    if(typeof(arr) == 'Array'){
		var len = arr.Length;
		var from = Number(arguments[2]) || 0;
		from = (from < 0)?Math.ceil(from):Math.floor(from);
		if (from < 0) from += len;
		for (; from < len; from++){
			if (from in arr && arr[from] === elt) return from;
		}
    }else if(typeof(arr) == 'object'){
		for(key in arr){
			if(arr[key] == elt) return key;
		}
    }
    return -1;
}

// checks for a positive integer (or 0)
function isnumeric(val){
	var check = /^[\d]+$/;
	return check.test(trim(val));
}

function basename(path, suffix) { 
    var b = path.replace(/^.*[\/\\]/g, '');    
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }    
    return b;
}

function MousePosition(e){
	this.posX = 0;
	this.posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY){
		this.posx = e.pageX;
		this.posy = e.pageY;
	}
	else if (e.clientX || e.clientY){
		this.posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft - document.documentElement.clientLeft;
		this.posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop - document.documentElement.clientTop;
	}
}

// Designed to be used to restrict a field to integers only. 
// Note that this function will not allow negative numbers or floating point numbers
// Usage: <input type="text" onkeypress="return NumbersOnly(event)">
function NumbersOnly(e){
	var keynum,keychar,numcheck;
	if(window.event) keynum = e.keyCode;
	else if(e.which) keynum = e.which;
	if(keynum == undefined) return true; // FF - a special character was pressed (eg: delete, tab, etc.)
	if(keynum == 8) return true; // backspace
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return numcheck.test(keychar);
}

// use this function to set the value of a form element.
function setElementValue(targetid, value){
	var target = document.getElementById(targetid);
	if(target){
		if(target.tagName.toLowerCase() == 'input' || target.tagName.toLowerCase() == 'textarea'){
			target.value = value==null?'':value;
			return true;
		}else if(target.tagName.toLowerCase() == 'select'){
			for(j=0;j<target.length;j++){
				if(target[j].value==value){
					target.selectedIndex = j;
					return true;
				}
			}
			target.selectedIndex = 0;	
		}else{
			target.innerHTML = value==null?'':value;
			return true;
		}
	}
	return false;
}

function getRandomCode(){
	var string = Math.round(Math.random() * 100000000);
	return '?' + string;
}

/*
Useful Event Manager, stolen from http://v3.thewatchmakerproject.com
Usage: to add an event, simply use Event.add(object,eventtype(without the on),function(name or code));
	   ro remove an event, use Event.remove(object,eventtype(without the on),function(name or code));
*/
var Event = {
	add: function(obj,type,fn) {
		if(typeof(obj) == 'string') obj = document.getElementById(obj);
		if (obj.attachEvent) {
			obj['e'+type+fn] = fn;
			obj[type+fn] = function() { obj['e'+type+fn](window.event); }
			obj.attachEvent('on'+type,obj[type+fn]);
		} else
		obj.addEventListener(type,fn,false);
	},
	remove: function(obj,type,fn) {
		if(typeof(obj) == 'string') obj = document.getElementById(obj);
		if (obj.detachEvent) {
			obj.detachEvent('on'+type,obj[type+fn]);
			obj[type+fn] = null;
		} else
		obj.removeEventListener(type,fn,false);
	}
}
