//window.onerror = handleError; //bug in IE if cancelling onunload

/* WINDOW FUNCTIONS */
function openChild(file, windowname, width, height) {
	childWindow = open(
			file,
			windowname,
			'width='
					+ width
					+ ',height='
					+ height
					+ ', toolbar = no, location = no, directories = no, status = no, mnubar = no, scrollbars = yes, resizable = yes');
	if (childWindow == null) {
		alert('It seems that you have a popup-blocker installed. Please allow popups for this site');
		return null;
	}
	if (childWindow.opener == null) {
		childWindow.opener = self;
	}
	if (childWindow.focus) {
		childWindow.focus();
	}
	return childWindow;
}

function updateParent() {
	opener.window.location.reload();
	self.close();
	return false;
}

function detectCloseSelf(id) {
	var node = $('#' + id);
    while (node.get(0).tagName.toLowerCase() != 'html') {
    	var nodeID = node.attr('id');
        if (nodeID == 'fancybox-inner') {
        	$.fancybox.close();
        	break;
        } else if (node.hasClass('ui-dialog-content')) {
        	$('#' + id).dialog('close');
        	break;
        }
        node = node.parent();
	}
}

// Returns the URL parameters as an associative array
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}



/* MISC FUNCTIONS */
function disableElement(elID, isDisabled) {
	if (isDisabled) {
		document.getElementById(elID).setAttribute('disabled', 'disabled');
	} else {
		document.getElementById(elID).removeAttribute('disabled');
	}
}

/* MULTIPLE-EVENT FUNCTIONS */

var _onunloadArray = new Array();

function onunloadPush(string) {
	document.onunload = onunloadExecute;
	_onunloadArray[_onunloadArray.length] = string;
}

function onunloadExecute() {

	var string = '';
	for (var i = 0; i < _onunloadArray.length; ++i) {
		string += _onunloadArray[i];
	}
	eval(string);
}

function handleError() {
	return true;
}

var _onbeforeunloadArray = new Array();

function onbeforeunloadPush(string) {
	window.onbeforeunload = onbeforeunloadExecute;
	_onbeforeunloadArray[_onbeforeunloadArray.length] = string;
}

function onbeforeunloadExecute() {
	var string = '';
	for (var i = 0; i < _onbeforeunloadArray.length; ++i) {
		string += _onbeforeunloadArray[i];
	}
	if (string) {
		return eval(string);
	}
	return '';
}

/* Functions for setting multiple onclick-statements on document */
var _onclickArray = new Array();

function onclickPush(string) {
	document.onclick = onclickExecute;
	_onclickArray[_onclickArray.length] = string;
}

function onclickExecute() {
	var string = '';
	for (var i = 0; i < _onclickArray.length; ++i) {
		string += _onclickArray[i];
	}
	eval(string);
}

var _ontimerArray = new Array();
var _timerId = 0;

function ontimerPush(string) {
	if (!_timerId) {
		_timerId = window.setInterval(ontimerExecute, 500);
	}
	_ontimerArray[_ontimerArray.length] = string;
	return _ontimerArray.length - 1;
}

function ontimerExecute() {
	var string = '';

	for (var i = 0; i < _ontimerArray.length; ++i) {
		string += _ontimerArray[i];
	}
	eval(string);
}

function ontimerRemoveat(id) {
	if (id >= 0 && id < _ontimerArray.length) {
		_ontimerArray[id] = '';
	}
}
function ontimerReset() {
	window.clearInterval(_timerId);
}

/* CONTEXT-SENSITIVE MENU FUNCTIONS */

function getPosition(e) {
	var left = 0;
	var top = 0;

	while (e.offsetParent) {
		left += e.offsetLeft;
		top += e.offsetTop;
		e = e.offsetParent;
	}

	left += e.offsetLeft;
	top += e.offsetTop;

	return {
		x :left,
		y :top
	};
}

function mouseCoords(ev) {
	if (ev.pageX || ev.pageY) {
		return {
			x :ev.pageX,
			y :ev.pageY
		};
	}
	return {
		x :ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y :ev.clientY + document.body.scrollTop - document.body.clientTop
	};
}

function windowSize() {
	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 {
		x :myWidth,
		y :myHeight
	};
}
function bodySize() {
	var pageWidth = 0, pageHeight = 0;
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {// all
		// but
		// Explorer
		// Mac
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		pageWidth = document.body.offsetWidth + document.body.offsetLeft;
		pageHeight = document.body.offsetHeight + document.body.offsetTop;
	}
	return {
		x :pageWidth,
		y :pageHeight
	};
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 16;
	var randomstring = '';
	for ( var i = 0; i < string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum, rnum + 1);
	}
	return randomstring;
}

function rand(n) {
	return (Math.floor(Math.random() * n + 1));
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function treeReceived(ajaxID, command, content) {
	switch (command) {
	case 'expand':
	case 'expandFromHere':
		$('#ref' + ajaxID).html(content);
		$('#exp' + ajaxID).css('display', 'none');
		$('#col' + ajaxID).css('display', 'inline');
		$('#reftr' + ajaxID).css('display', 'table-row');
		$('#line' + ajaxID).css('background-image', 'url(/modules/Core/images/line.png)');
		break;
	case 'collapse':
	case 'collapseFromHere':
		$('#ref' + ajaxID).html('');
		$('#exp' + ajaxID).css('display', 'inline');
		$('#col' + ajaxID).css('display', 'none');
		$('#reftr' + ajaxID).css('display', 'none');
		$('#line' + ajaxID).css('background-image', 'none');
		break;
	}
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}


