// 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;
}

// is the <form> contains changed elements?
function formIsChanged(f) {
	for (i = 0; i < f.length; i++) {
		var elm = f.elements[i];
		if (elm && elm.type) {
			if ((elm.type == 'text' || elm.type == 'file' || elm.type == 'password' || elm.type == 'textarea') &&
				elm.defaultValue != elm.value) return true;
			if ((elm.type == 'checkbox' || elm.type == 'radio') &&
				elm.defaultChecked != elm.checked) return true;
			if (elm.type == 'select' || elm.type == 'select-one' || elm.type == 'select-multiple') {
				for (j = 0; j < elm.length; j++)
					if (elm.options[j] && elm.options[j].defaultSelected != elm.options[j].selected) return true;
			}
		}
	}
	return false;
}



// 

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;
}

function changePicture(id, src, w, h) {
	_pic = $(id);
	_pic.src = src;
	_pic.width = w;
	_pic.height = h;
}

// next three functions taken from here:
// http://www.quirksmode.org/js/cookies.html

function createCookie(name, value, days) {
        if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
}

function eraseCookie(name) {
        createCookie(name, "", -1);
}

// next function based on this one:
// http://www.weberdev.com/get_example-4437.html
function filterInput(evt, allowed) {
	var keyCode, chr, inputField;
	
	// Get the Key Code of the Key pressed if possible else - allow
	if (window.event) {
		keyCode = window.event.keyCode;
		evt = window.event;
	} else if (evt) keyCode = evt.which;
	else return true;

	if(allowed == '') return true;

	// Get the Element that triggered the Event
	inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget;

	// If the Key Pressed is a CTRL key like Esc, Enter etc - allow
	if (keyCode == null || keyCode == 0 || keyCode == 8 || keyCode == 9 ||
		keyCode == 13 || keyCode == 27) return true;

	// Get the Pressed Character
	chr = String.fromCharCode(keyCode);

	// If the Character is a number - allow
	if (allowed.indexOf(chr) > -1) return true;
	else return false;
}

// next function based on this one:
// http://snipplr.com/view/1696/get-elements-by-class-name/
function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var ret = new Array();
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0, l=els.length; i<l; i++)
		if(re.test(els[i].className))
			ret.push(els[i]);
	return ret;
}
