﻿var SHOW_TR_PROPERTY = "table-row";
var WEEKDAYS = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"];
var MONTHS = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];
var MONTHS_SHORT = ["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"];

//var handle;

/* ### on load ### */

var overlayFadeIn;
var overlayFadeOut;
var currentWindowId;

var windowScroll;

window.addEvents({
    domready:    function() {
    
        if($chk($("overlay")))
        {
            windowScroll = new Fx.Scroll(window, { offset: { x: -10, y: -10 } });
            
            overlayFadeIn = new Fx.Tween($("overlay"), { property: "opacity", onStart: function() { $("overlay").setStyle("display", "block"); } });
            overlayFadeOut = new Fx.Tween($("overlay"), { property: "opacity", onComplete: function() { $("overlay").setStyle("display", "none"); } });

            $$(".window").fade("hide");
            
            $("overlay").set("opacity", "0");
        }
        
    },
    resize:      updateWindowPosition
});


/* ### Requests ### */


function sendRequest(url, postData, handler)
{
	$("loadingIndicator").setStyle("display", "block");
    $("loadingIndicator").fade("in");
	
	//handle = handler;
	
    (new Request.JSON({ url: url, method: (postData == null ? "get" : "post"), data: postData, onSuccess: function(response){handleResponse(response,handler)}})).send();
}

function handleResponse(response, handle)
{
	handle(response);
	$("loadingIndicator").fade("out");
	$("loadingIndicator").setStyle("display", "none");
}





/* ### Windows ### */

function showWindow(id, w, onComplete)
{
    if($chk(currentWindowId))
    {
        return;
    }
    
    currentWindowId = id;
    
    $(id).setStyle("width", w + "px");
    $(id).setStyle("top", (window.getScroll().y + 50) + "px");
    
    $(id).setStyle("display", "block");
    updateWindowPosition();
    
    var windowFadeIn = new Fx.Tween($(currentWindowId), {
        property:    "opacity",
        onComplete:  onComplete
    });
    windowFadeIn.start("0", "1.0");
    
    overlayFadeIn.start("0", "0.6");
}

function updateWindowPosition()
{
    if($chk(currentWindowId))
    {
        $(currentWindowId).setStyle("left", (window.getSize().x / 2 - $(currentWindowId).getStyle("width").toInt() / 2) + "px");
    }
}

function closeWindow(onComplete)
{
    overlayFadeOut.start("0.6", "0");
    
    var windowFadeOut = new Fx.Tween($(currentWindowId), {
        property:    "opacity",
        onComplete:  function() {
        
            $(currentWindowId).setStyle("display", "none");
            currentWindowId = null;
            
            if($defined(onComplete))
            {
				try
				{
                	onComplete();
				}catch(e){
				}
			}
        }
    });
    
    windowFadeOut.start("1.0", "0");
}



/* ### Message ### */

function showMessage(id, msg, error)
{
    clearMessage(id);

    $(id).set("text", msg);
    $(id).set("opacity", "0");
    $(id).setStyle("display", "block");
    $(id).fade("in");
	
	if(typeof(error) != "undefined")
	{
		if(error)
		{
			document.getElementById(id).className = "message";
		}else{
			document.getElementById(id).className = "notification";
		}
	}
    
    var viewportTop = window.getScroll().y;
    var viewportBottom = viewportTop + window.getCoordinates().height;
    
    // (including margins)
    var elementTop = $(id).getPosition().y - 10;
    var elementBottom = elementTop + $(id).getCoordinates().height + 10;
    
    if(elementTop < viewportTop || elementBottom > viewportBottom)
    {
        windowScroll.toElement(id);
    }
}

function clearMessage(id)
{
    $(id).setStyle("display", "none");
}

function createValueLabeledInput(id, defaultText)
{
    getE(id).defaultText = defaultText;
    getE(id).isEmpty = false;
    refreshValueLabeledInput(getE(id));
}

function refreshValueLabeledInput(input)
{
    if(input.isEmpty)
    {
        input.isEmpty = false;
        input.style.color = "#000000";
        input.value = "";
    }
    else if(input.value == "")
    {
        input.isEmpty = true;
        input.style.color = "#808080";
        input.value = input.defaultText;
    }
}

function setValueLabeledInputValue(input, value)
{
    if(input.isEmpty)
    {
        refreshValueLabeledInput(input);
    }
    input.value = value;
    refreshValueLabeledInput(input);
}

function getE(id)
{
	return document.getElementById(id);	
}



function clearE(e)
{
    while(e.firstChild)
    {
        e.removeChild(e.firstChild);
    }
}

function requestXML(url, args, handler)
{
	$("loadingIndicator").setStyle("display", "block");
    $("loadingIndicator").fade("in");
	
    var httpRequest;
    
    if(window.XMLHttpRequest)
    {
        httpRequest = new XMLHttpRequest();
        if(httpRequest.overrideMimeType)
        {
            httpRequest.overrideMimeType("text/xml");
        }
    }
    else if(window.ActiveXObject)
    {
        try
        {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {}
        }
    }
    else
    {
        alert("Ihr Browser unterstützt kein AJAX.");
        return;
    }
    
    httpRequest.onreadystatechange = function()
    {
        if(httpRequest.readyState == 4)
        {
            if(httpRequest.status == 200)
            {
                handler(httpRequest.responseXML);
            }
            else
            {
                alert("Es ist ein Fehler bei der Anfrage aufgetreten. (HTTP-Status-Code: " + httpRequest.status + ")");
            }
        }
    }
    
    if(args == null)
    {
        httpRequest.open("GET", url, true);
        httpRequest.send("");
    }
    else
    {
        httpRequest.open("POST", url, true);
        httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        httpRequest.send(args);
    }
	
    $("loadingIndicator").fade("out");
	$("loadingIndicator").setStyle("display", "none");
}





//appended






function dom_getElementList(e, n)
{
    var list = new Array();
    
    for(var i = 0; i < e.childNodes.length; i++)
    {
        if(e.childNodes[i].nodeType == 1 && e.childNodes[i].nodeName.toLowerCase() == n.toLowerCase())
        {
            list.push(e.childNodes[i]);
        }
    }
    
    return list;
}

function dom_getElement(e, n)
{
    
    for(var i = 0; i < e.childNodes.length; i++)
    {
        if(e.childNodes[i].nodeType == 1 && e.childNodes[i].nodeName.toLowerCase() == n.toLowerCase())
        {
            return e.childNodes[i];
        }
    }
    
    return null;
}

function dom_getTextContent(e)
{
    if(e && e.firstChild && e.firstChild.nodeType == 3)
    {
        return e.firstChild.nodeValue;
    }
    return "";
}


function dom_getText(e, n)
{
    return dom_getTextContent(dom_getElement(e, n));
}

function dom_createElement(n, c)
{
    var e = document.createElement(n);
    if(arguments[1] != null)
    {
        e.className = arguments[1];
    }
    
    for(var i = 2; i < arguments.length; i++)
    {
        e.appendChild(arguments[i]);
    }
    
    return e;
}

function dom_appendText(e, t)
{
    e.appendChild(document.createTextNode(t));
}

function fadeIn(id, toggle)
{
	if(toggle)
	{
		$(id).setStyle("display", "block");
	}
	var fade = new Fx.Tween($(id), {
        property:    "opacity"
    });
    fade.start("0", "1.0");
}

function fadeOut(id, toggle)
{
	var fade = new Fx.Tween($(id), {
        property:    "opacity"
    });
	fade.start("1.0", "0");
	if(toggle)
	{
		$(id).setStyle("display", "none");
	}
}