
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) {
		oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop;
}
function getAbsLeft(o) {
	oLeft = o.offsetLeft;
	while(o.offsetParent!=null) {
		oParent = o.offsetParent;
		oLeft += oParent.offsetLeft;
		o = oParent;
	}
	return oLeft;
}

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*/){
    var len = arr.length;

    var from = Number(arguments[1]) || 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;
    }
    return -1;
}

// checks for integers.
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;
}