// JavaScript Document
var isDOM = (document.getElementById) ? true : false;
var isIE = (document.all) ? true : false;
var isNS4 = (document.layers) ? true : false;

var popUp;

function openPopUp(URL, name, props)
{
	var	width = getValueFromString(props, 'width', ',');
	var	height = getValueFromString(props, 'height', ',');
	var center = getValueFromString(props, 'center', ',');
	
	var centerStr = (center == 'yes') ? ','+getCenterParam(width, height) : '';

	popUp = window.open(URL, name, 'width='+width+',height='+height+',scrollbars=yes,directories=no,location=no,menubar=no,status=yes,resizable=yes,toolbar=no,personalbar=no,modal=yes'+centerStr);
	window.onfocus = checkModal;
}

function closePopUp(hash)
{
	if (hash) {
		var url = 
			window.opener.location.protocol + '//'
		+	window.opener.location.host
		+	window.opener.location.pathname;

		if (window.opener.location.search) {
			url += (window.opener.location.search.substr(0, 1) == '?' ? '' : '?')
			+	window.opener.location.search;
		}

		url += '#' + hash;

		window.opener.location.href = url;
	}

	window.opener.location.reload(true);

	window.opener.onfocus = null;
	window.close();
}

function checkModal()
{
	if (!popUp.closed) {
		popUp.focus();
	}
}

function getCenterParam(width, height)
{
	var left = (screen.availWidth / 2) - (width / 2);
	var top = (screen.availHeight / 2) - (height / 2);
	
	if (isNS4)
		return 'screenX='+left+',screenY='+top;
	else
		return 'left='+left+',top='+top;
}

function getValueFromString(str, varName, sep)
{
	var parts = str.split(sep);
	
	for (var i=0; i<parts.length; i++) {
		var subParts = parts[i].split('=');
		if (subParts[0].small() == varName.small()) {
			return subParts[1];
		}
	}
	
	return '';
}

function html_GetPosition(hElement) {
	var iLeft = 0;
	var iTop = 0;

	// recursivly get the offsets
	do {
		iLeft += hElement.offsetLeft;
		iTop += hElement.offsetTop;
		hElement = hElement.offsetParent;
	} while (hElement.offsetParent);

	return {x: iLeft, y: iTop};
}

function html_gotoAnchor(id) {
	var element = document.getElementById(id.substr(1));

	if (element) {
		var pos = html_GetPosition(element);
		document.documentElement.scrollLeft = pos.x;
		document.documentElement.scrollTop = pos.y;

		if (document.documentElement.scrollTop != pos.y) {
			window.scrollTo(0, pos.y);
		}
	}
}

function html_ChildrenRemoveAll(element) {
	while (element.childNodes.length) {
		element.removeChild(element.childNodes[0]);
	}
}

/**
 * Checks if given date is in the past
 *
 * @param Date the date to verify
 * @return Boolean true if in past, false otherwise
 */
function date_IsInPast(date) {
	return new Date().getTime() >= date.getTime();
}

/**
 * Reads values from several inputs and returns a Date object
 *
 * The inputs have to have all the same ID scheme and these indexes:
 * * Year
 * * Day
 * * Month
 *
 * The elements can either be type text or select (option's value is the actual value)
 *
 * Example:
 * <input type="text" id="birthdayDay"/>
 * <input type="text" id="birthdayMonth"/>
 * <select id="birthdayYear"><option value="2009">This year</option></select>
 * date_FromInput('birthday');
 *
 * Hint: Hour, Minute, Seconds and Milliscond are all set to their maximum
 *
 * @param String name of elements
 * @return Date|Bool the date value or false on error
 */
function date_FromInput(name) {
	var elementYear = document.getElementsByName(name + '[y]')[0];
	var elementMonth = document.getElementsByName(name + '[m]')[0];
	var elementDay = document.getElementsByName(name + '[d]')[0];

	var year, month, day;

	year = parseInt(elementYear.tagName == 'INPUT' ? elementYear.value : elementYear.options[elementYear.selectedIndex].value);
	month = parseInt(elementMonth.tagName == 'INPUT' ? elementMonth.value : elementMonth.options[elementMonth.selectedIndex].value) - 1;
	day = parseInt(elementDay.tagName == 'INPUT' ? elementDay.value : elementDay.options[elementDay.selectedIndex].value);

	if (isNaN(year) || isNaN(month) || isNaN(day) || year < 0 || month < 0 || day < 0) {
		return false;
	} else {
		return new Date(year, month, day, 23, 59, 59, 999);
	}
}

function addEvent(obj, eventName, listenerFunction)
{
	var listenerFn = listenerFunction;
	if (obj.addEventListener)
	{
		obj.addEventListener(eventName, listenerFn, false);
	}
	else if (obj.attachEvent)
	{	
		listenerFn = function() { listenerFunction(window.event); }
		obj.attachEvent("on" + eventName, listenerFn);
	}
	
	var event = {
		instance: obj,
		name: eventName,
		listener: listenerFn
	}
	return event;
}

window.onLoad = function () {
	html_gotoAnchor(window.location.hash);
}

addEvent(window, "load", window.onLoad);

