// general javascript functions

// crossbrowser getElementById
function $(aID) {
	return (document.getElementById) ? document.getElementById(aID) : document.all[aID];
}

// change the action link of form
function formRelink(fID, newLink, newTarget) {
	elm = $(fID);
	elm.action = newLink;
	elm.target = newTarget;
}

// the sign of argument
function signOf(num) {
	var r;
	if (num > 0) r = 1; else if (num < 0) r = -1; else r = 0;
	return r;
}

// classic replace picture function
function replacePicture(iID, pSrc) {
	$(iID).src = pSrc;
}

// 

function getGeometry(eID) {
	var elm = $(eID);
	var w = elm.offsetWidth;
	var h = elm.offsetHeight;
	var x = 0;
	var y = 0;
	do {
		x = x + elm.offsetLeft;
		y = y + elm.offsetTop;
	} while	( elm = elm.offsetParent ) ;
	return {"x":x, "y":y, "w": w, "h":h};
}

function getMousePosition(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}

var mousePosition = { 'x':0, 'y':0 };

function traceMousePosition(e) {
	mousePosition = getMousePosition(e);
}

function gotoUrlPost(url,params,target) {
	var form = document.createElement('form');
	form.action = url;
	form.method="post";
	if ( target ) form.target = target;
	form.style.display = "none";
	var element = null;
	for (var propName in params) {
		element = document.createElement('input');
		element.type = "text";
		element.name = propName;
		element.value = params[propName];
		form.appendChild(element);
	}
	document.body.appendChild(form);
	form.submit();
}

// this function is taken from here:
// http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function innerSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return { "w":myWidth, "h":myHeight };
}

// this code is based on this one:
// http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function scrollPosition() {
	var x = 0, y = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
	//Netscape compliant
		y = window.pageYOffset;
		x = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	//DOM compliant
		y = document.body.scrollTop;
		x = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	//IE6 standards compliant mode
		y = document.documentElement.scrollTop;
		x = document.documentElement.scrollLeft;
	}
	return { "x":x, "y":y };
}

// this code is based on this one:
// http://www.thescripts.com/forum/thread91876.html
function documentSize() {
	if (typeof(document.height) != 'undefined') return { "w":document.width, "h":document.height };
	else if (document.compatMode && document.compatMode != 'BackCompat') 
		return { "w":document.documentElement.scrollWidth, "h":document.documentElement.scrollHeight };
	else if (document.body && typeof(document.body.scrollHeight) != 'undefined')
		return { "w":document.body.scrollWidth, "h":document.body.scrollHeight };
}

function bookmarkThisPage(msg) {
	title = document.title;
	url = self.location;
	if (document.all) window.external.AddFavorite(url, title);
	else {
		var agnt = navigator.userAgent.toLowerCase();
		var btn = (agnt.indexOf('mac') != -1) ? 'Command+' : 'Ctrl+';
		btn += (agnt.indexOf('konqueror') != -1) ? 'B' : 'D';
		alert(msg.replace(/\%b/, btn));
	}
}

// opacity funcions (c) Igor Tigirlas
// http://www.tigir.com/opacity.htm
function setElementOpacity(eID, nOpacity)
{
	var opacityProp = getOpacityProperty();
	var elem = $(eID);
	if (!elem || !opacityProp) return;
	if (opacityProp=="filter") { // Internet Exploder 5.5+
		nOpacity *= 100;
		var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
		if (oAlpha) oAlpha.opacity = nOpacity;
		else elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";
	} else elem.style[opacityProp] = nOpacity;
}

function getOpacityProperty()
{
	if (typeof document.body.style.opacity == 'string') return 'opacity'; // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
	else if (typeof document.body.style.MozOpacity == 'string') return 'MozOpacity'; // Mozilla 1.6-, Firefox 0.8 
	else if (typeof document.body.style.KhtmlOpacity == 'string')  return 'KhtmlOpacity'; // Konqueror 3.1, Safari 1.1
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) return 'filter'; // Internet Exploder 5.5+
	else return false;
}
