// dhtmlcommon.js
// Author: Bernard Chan
// Common routines required in DHTML (+ AJAX?)

// ***** Events Section *****

function getEvent(evt) {
	var rt_evt = null;
	try {	// Assume IE
		rt_evt = window.event;
		if (rt_evt == null) {	// Hack: Firefox seems not to fire error
			throw "Try another way to get event";
		}
	} catch (error) {
		rt_evt = evt;
	}
	return rt_evt;
}

// Cancel event bubbling
function cancelEventBubbling(evt) {
	rt_evt = getEvent(evt);
	if (rt_evt != null) {
		rt_evt.cancelBubble = true;
	}
}

// Set event handler with event bubbling
function setEventHandler(obj, eventname, handler) {
	if (obj == null) return;
	try {		// Assume IE
		obj.attachEvent('on' + eventname, handler);
	} catch (error) {
		obj.addEventListener(eventname, handler, false);
	}
}

// Remove event handler
function removeEventHandler(obj, eventname, handler) {
	if (obj == null) return;
	try {		// Assume IE
		obj.detachEvent('on' + eventname, handler);
	} catch (error) {
		obj.removeEventListener(eventname, handler, false);
	}
}


// Disable default action associated with the event
function cancelDefaultAction(evt) {
	evt = getEvent(evt);
	try {
		evt.preventDefault();
	} catch (error) {
		evt.returnValue = false;
	}
}

// ***** Visual Formatting/Appearance *****

// Position an object at the given position in the client window
// On browsers that support "position: fixed", it will move as you scroll
// This includes Konqueror 3.3+, Opera, Mozilla 1/Netscape 6+.
// IE 5.x and 6.x will use absolute position since it doesn't support yet.
function setFixedPosInWin(obj, pos) {
	if (obj == null) return;
	body = document.body;
	// TODO We cannot cleanly implement "position: fixed" for IE yet
	// There are hacks but maybe we will not use for now
	ieREGEX = navigator.appVersion.match(/MSIE\ (\d+\.?\d+);/);
	var ie;
	if (ieREGEX) {
		ie = ieREGEX[1];
	}
	obj.style.position = ((ie && ie < 7.0)?'absolute':'fixed');
	setCSSTop(obj, pos.top?(
		(obj.style.position == 'fixed')? pos.top : (body.scrollTop + pos.top)
	):null);
	setCSSLeft(obj, pos.left?(
		(obj.style.position == 'fixed')? pos.left : (body.scrollLeft + pos.left)
	):null);
}

// http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		// Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		// DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		// IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

// Center an object in a window
function setCenterInWin(obj) {
	cr = getClientAreaRect();
	try {
		objdim = [document.defaultView.getComputedStyle(obj, null).height, document.defaultView.getComputedStyle(obj, null).width];
	} catch (error) {
		try {
			objdim = [obj.offsetHeight + 'px', obj.offsetWidth + 'px'];
		} catch (error) {
			objdim = [obj.style.height, obj.style.width];
		}
	}	
	// TODO we now assume only 'px' calculation
	a_ch = objdim[0].match(/^(\d+)/);
	a_cw = objdim[1].match(/^(\d+)/);
	//setFixedPosInWin(obj, {top: (cr.height - a_ch[1]) / 2, left: (cr.width - a_cw[1]) / 2});
	var scrollXY = getScrollXY();
	setCSSTop(obj, scrollXY[1] + (cr.height - a_ch[1]) / 2);
	setCSSLeft(obj, scrollXY[0] + (cr.width - a_cw[1]) / 2);
}

// Try to retrieve the client area dimensions, in pixels
// http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getClientAreaRect() {
	var rect;
	try {	// Konqueror 3.3 apparently needs this
		rect = {width: window.innerWidth, height: window.innerHeight};
		if (window.innerHeight == null)
			throw "Try again with the other way";	// IE: A hack to make IE test the other way
	} catch (error) {
		rect = {width: document.documentElement.clientWidth, height: document.documentElement.clientHeight};
		if (!rect.height) {
			rect = {width: document.body.clientWidth, height: document.body.clientHeight};
		}
	}
	return rect;
}

// Set CSS "top" value for given object, in pixels
function setCSSTop(obj, top) {
	if (top != null && obj != null)
		obj.style.top = top + 'px';
}

// Set CSS "left" value for given object, in pixels
function setCSSLeft(obj, left) {
	if (left != null && obj != null)
		obj.style.left = left + 'px';
}

// Set CSS "width" value for given object, in pixels
function setCSSWidth(obj, width) {
	if (width != null && obj != null)
		obj.style.width = width + 'px';
}

// Set CSS "height" value for given object, in pixels
function setCSSHeight(obj, height) {
	if (height != null && obj != null)
		obj.style.height = height + 'px';
}

// Retrieve XMLHttpRequest object
function getXMLHttpRequest() {
	var req = null;
	try {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (error) {
		try {
			req = new XMLHttpRequest();
		} catch (error) {
			return null;
		}
	}
	return req;
}

// Fill textbox with given ID with given text
function fillTextbox(textbox_id, text) {
	box = document.getElementById(textbox_id);
	if (box != null) {
		box.value = text;
	}
}

// FROM PROTOTYPE

Object.extend = function(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
};


