//for debugging purposes
function tellerror(msg, url, linenumber){
	alert('Error message= '+msg+'\nURL= '+url+'\nLine Number= '+linenumber);
	return true;
}
//window.onerror=tellerror;

function sprintf() {
	if (!arguments || arguments.length < 1 || !RegExp) {
		return;
	}
	var str = arguments[0];
	var oldstr = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str)) {
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		numMatches++;
		if (pType == '%') {
			subst = '%';
		} else {
			numSubstitutions++;
			if (numSubstitutions >= arguments.length) {
				return oldstr;
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			else if (pPad) pad = pPad;
			var justifyRight = true;
			if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
			if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
			if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
			if (pType == 'b') subst = parseInt(param).toString(2);
			else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			else if (pType == 'u') subst = Math.abs(param);
			else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			else if (pType == 'o') subst = parseInt(param).toString(8);
			else if (pType == 's') subst = param;
			else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

//returns a valid event name string, epending on the browser
function UNI_treatType (type) {
	//mozilla events are without 'on'
	if (typeof document.addEventListener != 'undefined') {
		type = type.replace(/^on/, '');
	} else if (typeof document.attachEvent != 'undefined' && !type.match(/^on/)) {
		type = 'on' + type;
	}
	return type;
}

//creates a new function, from the two functions passed. THE SECOND FUNCTION WILL BE CALLED FIRST
function UNI_appendToFunction(f1, f2) {
	var me = function (e) {
		f2(e);
		if (f1) {
			f1(e);
		}
	}
	return me;
}

//attaches an event to an element, before or after the rest
function UNI_attachEvent(where, type, what, when, capture) {
	if (capture == undefined)
		capture = false;
	type = UNI_treatType(type);
	if (when == 0) {
		var oldHandler = null;
		eval('oldHandler = where.on'+type.replace(/^on/, '') + ';');
		what = UNI_appendToFunction(oldHandler, what);
		eval('where.on'+type.replace(/^on/, '') + ' = null;');
		if (navigator.userAgent.toLowerCase().indexOf('opera')+1) {
			eval('where.on'+type.replace(/^on/, '') + ' = what;');
		}
	}
	if (typeof where.addEventListener != 'undefined') { //Mozilla
		where.addEventListener(type, what, capture);
	} else if (typeof where.attachEvent != 'undefined') { //IE
		where.attachEvent(type, what);
	}
}


//gets absolute position for an element
function DOM_getAbsolutePos(el) {
	var scrollleft = 0, scrolltop = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft){
		scrollleft = el.scrollLeft;
	}
	if (is_div && el.scrollTop){
		scrolltop = el.scrollTop;
	}

	var r = { x: el.offsetLeft - scrollleft, y: el.offsetTop - scrolltop};

	if (el.offsetParent){
		var tmp = DOM_getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}

	return r;
};

function DOM_getVisibility(el) {
	var value = el.style.visibility;
	if (!value) {
		if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // moz, opera
			value = document.defaultView.getComputedStyle(el, "").getPropertyValue("visibility");
		} else if (el.currentStyle) { // IE
			value = el.currentStyle.visibility;
		} else {
			value = '';
		}
	}
	return value;
}

function DOM_getDisplay(el) {
	var value = el.style.display;

	if (!value) {
		if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // moz, opera
			value = document.defaultView.getComputedStyle(el, "").getPropertyValue("display");
		} else if (el.currentStyle) { // IE
			value = el.currentStyle.display;
		} else {
			value = '';
		}
	}
	return value;
}

function DOM_getStyleProperty(el, property) {
	var value = el.style[property];
	if (!value) {
		if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // moz, opera
			value = document.defaultView.getComputedStyle(el, "").getPropertyValue(property);
		} else if (el.currentStyle) { // IE
			value = el.currentStyle[property];
		} else {
			value = '';
		}
	}
	return value;
}


//event handlers
function DOM_setEventVars(e) {
	var targ, relTarg, posx=0, posy=0;
	if (!e) var e = window.event;

	if (e.relatedTarget) relTarg = e.relatedTarget;
	else if (e.fromElement) relTarg = e.fromElement;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	//position
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
	//mouse button
	if (window.event) {
		var leftclick = (e.button == 1);
		var middleclick = (e.button == 4);
		var rightclick = (e.button == 2);
	} else {
		var leftclick = (e.button == 0);
		var middleclick = (e.button == 1);
		var rightclick = (e.button == 2);
	}
	o = {
		'e': e, 
		'relTarg': relTarg, 
		'targ': targ, 
		'posx': posx, 
		'posy': posy, 
		'leftclick': leftclick, 
		'middleclick': middleclick, 
		'rightclick': rightclick,
		'type':e.type
	};
	
	return o;
}

function DOM_cancelEvent(e) {
	e.cancelBubble = true;
	e.returnValue = false;

	if (e.preventDefault) e.preventDefault();
	if (e.stopPropagation) e.stopPropagation();
	return false;

}


function dumpVar(obj, reg) {
	if (reg == undefined) {
		reg = '';
	}
	tm = "";
	if (typeof(obj) == "object") {
		for (i in obj) {
			if (i.toString().indexOf(reg)>=0) {
				tm += '' + i + ":{ " + dumpVar(obj[i], '' + '  ') + "}\n";
			}
		}
		return tm;
	} 
	if (typeof(obj) == "function") return '' + typeof(obj) + "\n";
	return '' + obj + "";
}

//UNI_attachEvent(window, 'load', HDR_univalLoad, 1);

