function openPopup(url, width, height) {
	var win = window.open(url, 'Digital', 'width='+width+',height='+height);
	win.focus();
}

function openPopUpWindow( side, width, height, top, left, scrollbars )
{
    if (side && side != null)
    {
        var win = document.open(side, "_blank", "width=" + width + ",height=" + height + ",top="+ top + ",left=" + left + ",toolbar=no,status=no,location=no,menubar=no,directories=no,resizable=yes,scrollbars=" + scrollbars + ",scrolling=");
		win.focus();
    }
    return;
}

function openPopUpWindowInside( side, width, height, top, left, scrollbars )
{
    if (side && side != null)
    {
        var win = window.open(side, "Detail", "width=" + width + ",height=" + height + ",top="+ top + ",left=" + left + ",toolbar=no,status=no,location=no,menubar=no,directories=no,resizable=yes,scrollbars=" + scrollbars + ",scrolling=");
		if (win) win.focus();
    }
    return;
}

function toggleElementVisibility(el) {
	if (el) {
        if (el.style.display != 'block') {
            showElement(el);
        } else {
            hideElement(el);
        }
    }
}

function hideElement(el) {
    if (el) {
        el.style.display = 'none';
    }
}

function showElement(el) {
    if (el) {
        el.style.display = 'block';
    }
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// attrib_name may be
// id|class|readOnly
// or a style property name like 'display'
function setAttributeForElement(id, attrib_name, attrib_value)
{
	var element = document.getElementById(id);
	if (!element) return;
	
	if (attrib_name == 'class') {
		element.className = attrib_value;
	}
	else if (attrib_name == 'id') {
		element.id = attrib_value;
	}
	else if (attrib_name == 'readOnly') {
		element.readOnly = attrib_value;
	}
	else {
		element.style[attrib_name] = attrib_value;
	}
	return 1;
}

/** Magic Dropdowns **/

var magicdropdowns = new Array();
var magicdropdownButtonHandled = false;

function magicdropdownButtonClick(tag) {
	magicdropdownButtonHandled = true;
	// TODO: hide other dropdowns

	// show requested dropdown
	el = magicdropdowns[tag];
	toggleElementVisibility(el);
}

function magicdropdownOptionClick(tag, optionValue, optionText) {
	// change text
	var textEl = document.getElementById('magicdropdown-text-'+tag);
	if (textEl) {
        textEl.innerHTML = optionText;
        textEl.style.color = '#000000';
    }

	// change hidden form value to optionValue
    var valueEl = document.getElementById('magicdropdown-value-'+tag);
    if (valueEl) {
        valueEl.value = optionValue;
    }

	// hide dropdown
	hideElement(magicdropdowns[tag]);
}


function magicdropdownClickHandler(event) {
	if (!magicdropdownButtonHandled) {
		// TODO: hide all dropdowns
	}

	magicdropdownButtonHandled = false;
}

function magicdropdownInit() {
	document.onclick = magicdropdownClickHandler;
	magicdropdowns = new Array();
}

function magicdropdownRegister(tag) {
	magicdropdowns[tag] = document.getElementById('magicdropdown-dropdown-'+tag);
}


var bikky = document.cookie;

function getCookie(name) { // use: getCookie("name");
    var index = bikky.indexOf(name + "=");
    if (index == -1) return null;
    index = bikky.indexOf("=", index) + 1;
    var endstr = bikky.indexOf(";", index);
    if (endstr == -1) endstr = bikky.length;
    return unescape(bikky.substring(index, endstr));
}

var today = new Date();
var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days

function setCookie(name, value) { // use: setCookie("name", value);
    if (value != null && value != "")
      document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString()+"; path=/";
    bikky= document.cookie; // update bikky
}

function testCookie() {
    var yourName = getCookie ("yourName");
    if (yourName == null) yourName = prompt ("What is your name?", "");
    alert ("Hello, " + yourName + "\nWelcome to my Website");
    setCookie ("yourName", yourName);
}


function checkCommunityBoxLinks() {
    if (getCookie('digital_community_login')) {
        hideElement(document.getElementById('community_box_login_link'));
        showElement(document.getElementById('community_box_logout_link'));
    }
}

// FIXME: Adjust path
function doTeatri(directory)
{
	if (directory && window.teatri != '') {
		document.write('<img src="http://ps.digital.de/cgi-bin/digitalps.cgi?pfad=' + directory
			+ window.teatri + '" width="1" height="1" align="right">');
	}
}

// Erweiterung Boris Schönewolf, 13.8.2005
// Funktion zum Löschen von vorbefüllten Input-Feldern
// Aufruf: 
function ClearInput(id) {
	var element = document.getElementById(id);
	if (element) {
		element.value = '';
	}
}

function check_vpq(){
	var erg = 20 * 2.5 * 15;
	document.write('<input type=hidden name=check value='+erg+'>');
}

/* Channel Navi Highlighting */

// Make sure that all compared paths end on a slash.
// It does not cause a problem here if a slash is appended to a filename.
function channelNaviHighlight(urls, ids, ids2block)
{
	var host = window.location.host;
	var pathname = window.location.pathname;

	var channel_match = host.match('^www\.(.+)\.de');
	var channel = channel_match ? channel_match[1] : '';
	
	// Domain issues
	if (! new RegExp('^/' + channel).test(pathname)) pathname = '/' + channel + pathname;
	if (! pathname.match('/$')) pathname += '/';

	// window.defaultStatus = pathname;

	for (var i = 0; i < urls.length; i++) {
		var url = urls[i];
		if (! url.match('/$')) url += '/';

		if (url && new RegExp('^' + url).test(pathname)) {
			var id = ids[i];
			var anchor = document.getElementById('c' + id);
			if (anchor) {
				var classname = anchor.className;
				anchor.className = classname ? classname + '-highlighted' : 'highlighted';
			}
			var navi_block_id = ids2block[i];
			if (navi_block_id != null) {
				var navi_block = document.getElementById('b' + navi_block_id);
				if (navi_block) {
					var classname = navi_block.className;
					navi_block.className = classname + '-highlighted';
				}
				// window.defaultStatus = 'navi block for link id c' + id + ' is b' + navi_block_id;
			}
			else {
				// window.defaultStatus = 'no navi block b' + navi_block_id + ' found for link id c' + id;
			}
			break;
		}
	}
}

function hideCurrentSenderLink()
{
	var host = window.location.host;
	var channel_match = host.match('^www\.(.+)\.de');
	
	if (channel_match) {
		var channel = channel_match[1];
		var current_sender = document.getElementById('sender-' + channel);
		if (current_sender) current_sender.style.display = 'none';
	}
	else {
		var pathname = window.location.pathname;
		var channel_match = pathname.match('^/([^/]+)');

		if (channel_match) {
			var channel = channel_match[1];
			var current_sender = document.getElementById('sender-' + channel);
			if (current_sender) current_sender.style.display = 'none';
		}
	}
}

// Bug introduced with sevensenses launch.
// Function is needed for sevensenses only but was called from all channels.
if (!window.setClassElement) window.setClassElement = function() {return};
