﻿
// Div
function Resize(divId) {
    Resize(divId, 82);
}
function Resize(divId, marginBottom) {
    var div = document.getElementById(divId);
    if (div) {
        if (typeof (marginBottom) == 'undefined')
            marginBottom = 82;
        var h = getViewportHeight() - div.offsetTop - marginBottom;
        var w = getViewportWidth() - div.offsetLeft - 10;

        if (div.offsetParent != null) {
            h -= div.offsetParent.offsetTop;
            w -= div.offsetParent.offsetLeft;
        }

        if (div.offsetParent.offsetParent != null) {
            h -= div.offsetParent.offsetParent.offsetTop;
            w -= div.offsetParent.offsetParent.offsetLeft;
        }
        
        if (h >= 0)
            div.style.height = h + "px";
        if (w >= 0)
            div.style.width = w + "px";
    }
}

function AddDivResizeOnAjaxPostback(divId) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(EndRequest);

    function EndRequest(sender, args) {
        Resize(divId);
    }
}

function PrintDiv(strid, autoclose) {
    var prtContent = document.getElementById(strid);
    if (prtContent) {
        var WinPrint = window.open('', '', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
        WinPrint.document.write('<html xmlns="http://www.w3.org/1999/xhtml">');
        WinPrint.document.write('<head>');
        WinPrint.document.write('<link href="../SwissDer.css" rel="Stylesheet" type="text/css" />');
        WinPrint.document.write('</head><body>');
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.write('</body></html>');
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        if (autoclose) {
            WinPrint.close();
        }
    }
}

// Grid
function hlRow(row, highlight) {
    if (highlight) {
        if (row.className.indexOf('over') == -1) {
            row.oldClassName = row.className;
            row.className += 'over';
        }
    }
    else {
        if (row.oldClassName)
            row.className = row.oldClassName;
    }
}


// Array
Array.prototype.remove = function(index) {
    return this.splice(index, 1);
}

// Events
function addEvent(obj, evName, fct) {
    // IE
    if (obj.attachEvent) {
        var r = obj.attachEvent('on' + evName, fct);
        return r;
    }
    // Gecko / W3C
    else if (obj.addEventListener) {
        obj.addEventListener(evName, fct, true);
        return true;
    }
    else {
        //el['on' + evName] = func;
        //return true;
        return false;
    }
}
function removeEvent(obj, evName, fct) {
    // IE
    if (obj.detachEvent) {
        var r = obj.detachEvent('on' + evName, fct);
        return r;
    }
    // Gecko / W3C
    else if (obj.removeEventListener) {
        obj.removeEventListener(evName, fct, true);
        return true;
    }
    else {
        //el['on' + evName] = null;
        //return true;
        alert('Handler could not be removed');
    }
}

function DeferCall() {
    if (arguments.length == 0)
        return null;

    var args = arguments;

    var fct = null;
    eval("if (typeof(" + args[0] + ") == 'function') { fct = " + args[0] + "; }");

    if (fct == null)
        return null;

    if (args.length == 1) return fct();
    else if (args.length == 2) return fct(args[1]);
    else if (args.length == 3) return fct(args[1], args[2]);
    else if (args.length == 4) return fct(args[1], args[2], args[3]);
    else if (args.length == 5) return fct(args[1], args[2], args[3], args[4]);
    else if (args.length == 6) return fct(args[1], args[2], args[3], args[4], args[5]);
    else if (args.length == 7) return fct(args[1], args[2], args[3], args[4], args[5], args[6]);
    else if (args.length == 8) return fct(args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
    else if (args.length == 9) return fct(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
    else if (args.length == 10) return fct(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
    else
        alert("Too many arguments passed to DeferCall.");

    return null;
}

function FindElementByAttribute(el, tagName, attributeName) {
    var str = el.getAttribute(attributeName);
    while (el != null && (str == null || str == "")) {
        el = GetSelectedElement(el.parentNode, tagName);
        if (el != null)
            str = el.getAttribute(attributeName);
    }
    return el;
}

function GetElementPosition(el, relativeEl) {
    var result = new Object();
    result.x = 0;
    result.y = 0;
    result.width = 0;
    result.height = 0;

    if (el.offsetParent) {
        var parent = el;
        while (parent != null &&
			   parent != relativeEl) {
            result.x += parent.offsetLeft;
            result.y += parent.offsetTop;
            // AdjustScrollPosition(parent, relativeEl, result);
            var parentTagName = parent.tagName.toLowerCase();
            if (parentTagName != "table" &&
				parentTagName != "body" &&
				parentTagName != "html" &&
				parentTagName != "div" &&
				parent.clientTop &&
				parent.clientLeft) {
                result.x += parent.clientLeft;
                result.y += parent.clientTop;
            }
            //		  if (browseris.ie && parentTagName=="td")
            //		  {
            //			  if (parent.runtimeStyle.borderTopStyle != "none" ||
            //				  parent.currentStyle.borderTopStyle != "none")
            //			  {
            //				  var shift;
            //				  if (parent.runtimeStyle.borderTopWidth != "")
            //					  shift = parseInt(parent.runtimeStyle.borderTopWidth);
            //				  else
            //					  shift = parseInt(parent.currentStyle.borderTopWidth);
            //				  if (!isNaN(shift))
            //					  result.y += shift;
            //			  }
            //			  if (parent.runtimeStyle.borderLeftStyle != "none" ||
            //				  parent.currentStyle.borderLeftStyle != "none")
            //			  {
            //				  var shift;
            //				  if (parent.runtimeStyle.borderLeftWidth != "")
            //					  shift = parseInt(parent.runtimeStyle.borderLeftWidth);
            //				  else
            // 	 	 	 	 	  shift = parseInt(parent.currentStyle.borderLeftWidth);
            // 	 	 	 	  if (!isNaN(shift))
            // 	 	 	 	 	  result.x += shift;
            // 	 	 	  }
            // 	 	  }
            parent = parent.offsetParent;
        }
    }
    else if (el.left && el.top) {
        result.x = el.left;
        result.y = el.top;
    }
    else {
        if (el.x)
            result.x = el.x;
        if (el.y)
            result.y = el.y;
    }
    if (el.offsetWidth && el.offsetHeight) {
        result.width = el.offsetWidth;
        result.height = el.offsetHeight;
    }
    else if (el.style && el.style.pixelWidth && el.style.pixelHeight) {
        result.width = el.style.pixelWidth;
        result.height = el.style.pixelHeight;
    }
    return result;
}

function GetFirstChildElement(el) {
    for (var i = 0; i < el.childNodes.length; i++) {
        if (el.childNodes[i].nodeType == 1)
            return el.childNodes[i];
    }
    return null;
}
function GetLastChildElement(el) {
    for (var i = el.childNodes.length - 1; i >= 0; i--) {
        if (el.childNodes[i].nodeType == 1)
            return el.childNodes[i];
    }
    return null;
}

function GetSelectedElement(el, tagName) {
    while (el != null && el.tagName != tagName)
        el = el.parentNode;
    return el;
}

function getViewportHeight() {
    if (window.innerHeight != window.undefined)
        return window.innerHeight;
    if (document.compatMode == 'CSS1Compat')
        return document.documentElement.clientHeight;
    if (document.body)
        return document.body.clientHeight;
    return window.undefined;
}
function getViewportWidth() {
    if (window.innerWidth != window.undefined)
        return window.innerWidth;
    if (document.compatMode == 'CSS1Compat')
        return document.documentElement.clientWidth;
    if (document.body)
        return document.body.clientWidth;
    return window.undefined;
}

function ieTrueBody() {
    return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

function indexOf(arr, val) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == val)
            return i;
    }
    return -1;
}

function trim(text) {
    return (text || "").replace(/^\s+|\s+$/g, "");
}

function togglePanel(divHeader, divContentId, stateTxtId) {
    var state;
    var divContent = document.getElementById(divContentId);
    var txtState = document.getElementById(stateTxtId);
    //	if (divHeader == null || divContent == null || txtState == null) return;
    if (divHeader == null || divContent == null) return;

    if (divContent.className == 'contentclosed')
        state = 'open';
    else
        state = 'closed';

    // Remember the state
    //	txtState.value = state;

    divHeader.className = 'header' + state;
    divContent.className = 'content' + state;
}

// Debug
function ToggleDebugVisibility(panelId) {
    var div = document.getElementById(panelId);
    if (!div)
        return;

    if (div.className == 'debughidden') {
        div.className = 'debug';
        div.style.left = '0px';
    }
    else {
        div.className = 'debughidden';
        div.style.left = (5 - div.clientWidth) + 'px';
    }
}

