
var onLoadFunctions = null;

window.onload = function() {

	if (!onLoadFunctions || onLoadFunctions.length < 1)
		return;
		
	for (var i = 0; i < onLoadFunctions.length; ++i)
		onLoadFunctions[i]();
		
}

function addToLoadFunctions(func) {
	if (!onLoadFunctions)
		onLoadFunctions = new Array();
		
	onLoadFunctions[onLoadFunctions.length] = func;
}

function removeLoadFunction(func) {

	if (!onLoadFunctions || onLoadFunctions.length < 1)
		return;

	for (var i = 0; i < onLoadFunctions.length; ++i)
		if (onLoadFunctions[i] == func) {
			onLoadFunctions.splice(i, 1);
			--i;
		}
}	

var onMouseMoveFunctions = null;
document.onmousemove = function(e) {
	if (!onMouseMoveFunctions || onMouseMoveFunctions.length < 1)
		return;
		
	for (var i = 0; i < onMouseMoveFunctions.length; ++i)
		onMouseMoveFunctions[i](e);
}

function addMouseMoveFunction(func) {
	if (!onMouseMoveFunctions)
		onMouseMoveFunctions = new Array();
		
	onMouseMoveFunctions[onMouseMoveFunctions.length] = func;
}

function removeMouseMoveFunction(func) {

	if (!onMouseMoveFunctions || onMouseMoveFunctions.length < 1)
		return;

	for (var i = 0; i < onMouseMoveFunctions.length; ++i)
		if (onMouseMoveFunctions[i] == func) {
			onMouseMoveFunctions.splice(i, 1);
			--i;
		}
}

function getElementHeight(elem) {
		return elem.offsetHeight;
}

function getElementWidth(elem) {
		return elem.offsetWidth;
}

function getElementLeft(elem) {
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
		while (tempEl != null) {
			xPos += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
		}
			
	return xPos;
}


function getElementTop(elem) {
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
 		
	return yPos;
}

function getElementPosition(elem) {
	return { top: getElementTop(elem), left: getElementLeft(elem) };
}

function confirmAndDelete(deleteUrl, message) {
	if (confirm(message)) {
		document.location = deleteUrl;
	}
}

function getParentByTag(element, parentTag) {
	var parent = element.parentNode;
	while (parent.nodeName.toLowerCase() != parentTag.toLowerCase()) {
		parent = parent.parentNode;
	}
	
	return parent;
}

function getParentByClass(element, parentClass) {
	var parent = element.parentNode;
	
	var oRegExp = new RegExp("(^|\\s)" + className + "(\\s|$)");
	
	while (!oRegExp.test(element.className)) {
		parent = parent.parentNode;
	}
	
	return parent;
}

function Blur(element, value) {
    if (element.value == "")
        element.value = value;
}

function Focus(element, value) {
    if (element.value == value)
        element.value = "";
}

function makeMailLink(user, domain, subject) {
	if (subject)
		document.write('<a href="mailto:' + user + "@" + domain + "?subject=" + subject + '">' + user + "@" + domain + "</a>");
	else 
		document.write('<a href="mailto:' + user + "@" + domain + '">' + user + "@" + domain + "</a>");
}

function makeCvMailLink(user) {
	makeMailLink(user, "cv.lt");
}


function getElementsByName(element, name, tagName) {

	var all = (!tagName || tagName == '*') && element.all ? element.all : element.getElementsByTagName(tagName ? tagName : '*');
	var ret = new Array();
	
	for (var i = 0; i < all.length; ++i)
		if (all[i].name == name)
			ret[ret.length] = all[i];
			
	return ret;
}

function toggleAllCheckboxes(controlCheckbox, name) {
	var all = getElementsByName(controlCheckbox.form, name);
	
	for (var i = 0; i < all.length; ++i)
		all[i].checked = controlCheckbox.checked;
}

function getElementsByClassName(element, className, tagName) {

	if (!tagName)
		tagName = "*";

	var arrElements = (tagName == "*" && element.all)? element.all : element.getElementsByTagName(tagName);
	
	var arrReturnElements = new Array();
	className = className.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + className + "(\\s|$)");

	for(var i = 0; i < arrElements.length; ++i) {
		if(oRegExp.test(arrElements[i].className))
			arrReturnElements.push(arrElements[i]);
	}
	
	return (arrReturnElements)
}

function NewWindow(mypage, myname, w, h, scroll, resizable) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	
	winprops = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',scrollbars=' + scroll + ',resizable=' + resizable;
	win = window.open(mypage, myname, winprops);
	
	if (parseInt(navigator.appVersion) >= 4) 
		win.window.focus();
}

function getMouseCoords(e){

	var posx = 0;
	var posy = 0;
	
	if (!e) 
		e = window.event;
		
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft	+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	return {
		x: posx,
		y: posy
	};
}

function getScreenSize() {
	var width = 0;
	var height = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
    	//Non-IE
    	width = window.innerWidth;
    	height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    	//IE 6+ in 'standards compliant mode'
	    width = document.documentElement.clientWidth;
    	height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    width = document.body.clientWidth;
	    height = document.body.clientHeight;
	}

	return {
		width: width,
		height: height
	};
}


function getScreenScroll() {

	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 {
  		x: scrOfX,
  		y: scrOfY 
  	};
}

function getScreenCenter() {

	var size = getScreenSize();
	var scroll = getScreenScroll();

	return {
		x: scroll.x + size.width / 2,
		y: scroll.y + size.height / 2
	};
}

function reloadCaptcha(id, params)
{
	var image = document.getElementById(id);
	var source = "/jcaptcha.do?s=" + (new Date()).getTime();
	if (params)
		source += '&' + params;
	image.src = source;
}
