//------------------------------------------------------------------------------
// Wettpunkt JavaScript - This javascript is for all webserver contents the same so deploy it on jboss
//
// Author : Alexandru Pintilie (c) 2008 Xion IT Systems AG
// Created: 25.06.2008 
// Changed: @author Iris Attensam 15.07.2008 (c) 2008 Wettpunkt Betriebsges.m.b.H.
//  		- function retyped
//	 		- top.WETTPUNKT var removed  - top is a window object property!
//          - top.WETTPUNKT var added  can be used as Global Object in every frame => top.WETTPUNKT
//            references the upper frame for the wettpunkt application
//			@author Thomas Fichtenbauer 16.07.2008
//			- Redesign
//------------------------------------------------------------------------------

/**
 * Reads a cookie with the specified name.
 * 
 * @param name
 *            the name of the cookie to be read.
 * @return the value of the found cookie or undefiend otherwise.
 */
function readCookie(name) {

	// Find the specified cookie and return its value
	var searchName = name + "=";
	var cookies = document.cookie.split(';');

	for ( var i = 0; i < cookies.length; i++) {
		var c = cookies[i];

		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}

		if (c.indexOf(searchName) == 0) {
			return c.substring(searchName.length, c.length);
		}
	}
}

/**
 * Writes a cookie with a specified name, value and days until expiration.
 * 
 * @param name
 *            the name of the cookie.
 * @param value
 *            the value for the cookie.
 * @param days
 *            number of days the cookie is valid.
 */
function writeCookie(name, value, days) {

	// By default, there is no expiration so the cookie is temporary
	var expires = "";

	// Specifying a number of days makes the cookie persistent
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	}

	// Set the cookie to the name, value, and expiration date
	document.cookie = name + "=" + value + expires + "; path=/";
}

/**
 * Constructs the path to the given frame as string.
 * 
 * @param currentFrame
 *            the frame to genereate the path for
 * @return the path to the given frame
 */
function findPath(currentFrame) {
	var path = "";

	while (currentFrame != top) {
		path = "." + currentFrame.name + path;
		currentFrame = currentFrame.parent;
	}

	return "top" + path;
}

/**
 * Writes a cookie named topRef with the value of the path from the given frame
 * 
 * @param frame
 *            reference of the frame for which the path is written into the
 *            cookie
 */
function initTopRef(frame) {
	writeCookie("topRef", findPath(frame));
}

/**
 * Returns the reference of the starting frame for the wettpunkt application.
 * 
 * The path to the frame is read from a cookie which must be writte before with
 * the method setTopRef.
 * 
 * Normally the setTopRef method is called in the Frameset.jsp
 * 
 * @return a reference to the top frame of the wettpuntk application
 */
function readTopRef() {
	return eval(readCookie("topRef"));
}

// Beware that Frameset.jsp MUST be the first call to Wettpunkt.js, otherwise APPLICATION CRASH !!!
if(!readTopRef()) {
	initTopRef(self);	
}

var topRef = readTopRef();

/**
 * Shows a popup window with the content from the page with the given url
 * 
 */
function popitup(url, name, h, w) {
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var newwindow = window.open (url, name, "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width="+w+", height="+h+", top="+top+", left="+left);
	if (window.focus) {newwindow.focus();}
	return false;
}

