// Div. for actual-css:

var IE4 = false;
var NS4 = false;
var DOM = false;
var DHTML = false;

function setActiveStyleSheet(title) {
    var i, a, main;
    this.str_title = title;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
}

function getActiveStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
    }
    return null;
}

function getPreferredStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
    }
    return null;
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

window.onload = function(e) {
    //var cookie = readCookie("style"); // $$$
    //var title = cookie ? cookie : getPreferredStyleSheet();
    //setActiveStyleSheet(title);

    this.str_cookie = readCookie("style");
    this.str_title = this.str_cookie ? this.str_cookie : getPreferredStyleSheet();
    setActiveStyleSheet(this.str_title);

    if (document.getElementById) { DOM = true; }
    else {
        if (document.layers) { NS4 = true; }
        else { IE4 = true; }
    }
    DHTML = DOM || NS4 || IE4;

    if (!DHTML) { alert("Browser unterstützt kein DHTML! Webseite nur eingeschränkt zugängig!"); }
}

function getElById(eID) {
    if (!DHTML) { return void (0); }

    if (DOM) { return document.getElementById(eID); }
    else if (NS4) { return document.layers[eID]; }
    else if (IE4) { return document.all[eID]; }
}

function getStyleElById(eID) {
    var elementObject = getElById(eID);
    if (NS4) { return elementObject; }
    else { return elementObject.style; }

    //alert("eID= " + eID + "  /  Object= " + elementObject.id);
}

// Div. for menu:

function ClickShowHideMenu(id) {
    this.box1Hover = true;
    this.box2Hover = true;
    this.highlightActive = false;

    this.init = function() {
        if (!document.getElementById(this.id)) {
            alert("Element '"+this.id+"' does not exist in this document. ClickShowHideMenu cannot be initialized");
            return;
        }
        this.parse(document.getElementById(this.id).childNodes, this.tree, this.id);
        this.load();
        if (window.attachEvent) {
            window.attachEvent("onunload", function(e) { self.save(); });
        } else if (window.addEventListener) {
            window.addEventListener("unload", function(e) { self.save(); }, false);
        }
    }

    this.parse = function(nodes, tree, id) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) {
                continue;
            }
            if (nodes[i].className) {
                if ("box1" == nodes[i].className.substr(0, 4)) {
                    nodes[i].id = id + "-" + tree.length;
                    tree[tree.length] = new Array();
                    eval('nodes[i].onmouseover = function() { self.box1over("'+nodes[i].id+'"); }');
                    eval('nodes[i].onmouseout = function() { self.box1out("'+nodes[i].id+'"); }');
                    eval('nodes[i].onclick = function() { self.box1click("'+nodes[i].id+'"); }');
                }
                if ("section" == nodes[i].className) {
                    id = id + "-" + (tree.length - 1);
                    nodes[i].id = id + "-section";
                    tree = tree[tree.length - 1];
                }
                if ("box2" == nodes[i].className.substr(0, 4)) {
                    nodes[i].id = id + "-" + tree.length;
                    tree[tree.length] = new Array();
                    eval('nodes[i].onmouseover = function() { self.box2over("'+nodes[i].id+'", "'+nodes[i].className+'"); }');
                    eval('nodes[i].onmouseout = function() { self.box2out("'+nodes[i].id+'", "'+nodes[i].className+'"); }');
                }
            }
            if (this.highlightActive && nodes[i].tagName && nodes[i].tagName == "A") {
                if (document.location.href == nodes[i].href) {
                    nodes[i].className = (nodes[i].className ? ' active' : 'active')
                }
            }
            if (nodes[i].childNodes) {
                this.parse(nodes[i].childNodes, tree, id);
            }
        }
    }

    this.box1over = function(id) {
        if (!this.box1Hover) return;
        if (!document.getElementById(id)) return;
        document.getElementById(id).className = (this.id_openbox == id ? "box1-open-hover" : "box1-hover");
    }

    this.box1out = function(id) {
        if (!this.box1Hover) return;
        if (!document.getElementById(id)) return;
        document.getElementById(id).className = (this.id_openbox == id ? "box1-open" : "box1");
    }

    this.box1click = function(id) {
        if (!document.getElementById(id)) {
            return;
        }
        var id_openbox = this.id_openbox;
        if (this.id_openbox) {
            if (!document.getElementById(id + "-section")) {
                return;
            }
            this.hide();
            if (id_openbox == id) {
                if (this.box1hover) {
                    document.getElementById(id_openbox).className = "box1-hover";
                } else {
                    document.getElementById(id_openbox).className = "box1";
                }
            } else {
                document.getElementById(id_openbox).className = "box1";
            }
        }
        if (id_openbox != id) {
            this.show(id);
            var className = document.getElementById(id).className;
            if ("box1-hover" == className) {
                document.getElementById(id).className = "box1-open-hover";
            }
            if ("box1" == className) {
                document.getElementById(id).className = "box1-open";
            }
        }
    }

    this.box2over = function(id, className) {
        if (!this.box2Hover) return;
        if (!document.getElementById(id)) return;
        document.getElementById(id).className = className + "-hover";
    }

    this.box2out = function(id, className) {
        if (!this.box2Hover) return;
        if (!document.getElementById(id)) return;
        document.getElementById(id).className = className;
    }

    this.show = function(id) {
        if (document.getElementById(id + "-section")) {
            document.getElementById(id + "-section").style.display = "block";
            this.id_openbox = id;
        }
    }

    this.hide = function() {
        document.getElementById(this.id_openbox + "-section").style.display = "none";
        this.id_openbox = "";
    }

    this.save = function() {
        if (this.id_openbox) {
            this.cookie.set(this.id, this.id_openbox, 0);
        } else {
            this.cookie.del(this.id);
        }
        this.cookie.set("style", getActiveStyleSheet(), 365);
    }

    this.load = function() {
        var id_openbox = this.cookie.get(this.id);
        if (id_openbox) {
            this.show(id_openbox);
            if (document.getElementById(id_openbox) != null)
            { document.getElementById(id_openbox).className = "box1-open"; }
        }
    }

    function Cookie() {
        this.get = function(name) {
            var cookies = document.cookie.split(";");
            for (var i = 0; i < cookies.length; i++) {
                var a = cookies[i].split("=");
                if (a.length == 2) {
                    a[0] = a[0].trim();
                    a[1] = a[1].trim();
                    if (a[0] == name) {
                        return unescape(a[1]);
                    }
                }
            }
            return "";
        }
        this.set = function(name, value, days) {
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            document.cookie = name + "=" + escape(value) + expires + "; path=/";
        }
        this.del = function(name) {
            document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    }

    var self = this;
    this.id = id;
    this.tree = new Array();
    this.cookie = new Cookie();
    this.id_openbox = "";
    // For actual-css:
    this.str_cookie = readCookie("style");
    this.str_title = this.str_cookie ? this.str_cookie : getPreferredStyleSheet();
    setActiveStyleSheet(this.str_title);
}

if (typeof String.prototype.trim == "undefined") {
    String.prototype.trim = function() {
        var s = this.replace(/^\s*/, "");
        return s.replace(/\s*$/, "");
    }
}

// For actual-css: // $$$
//var cookie = readCookie("style");
//var title = cookie ? cookie : getPreferredStyleSheet();
//setActiveStyleSheet(title);


function enabledAUFLNR(ID_AUFTYP_Value, eID_AUFLNR) {
    var bReadOnly = (ID_AUFTYP_Value == "1");
    getElById(eID_AUFLNR).readOnly = bReadOnly;
    //getElById(eID_AUFLNR).disabled = bReadOnly;
    getElById(eID_AUFLNR).className = bReadOnly ? "disabledCtrl" : "inActInpCtrl";
}

function enabledMIT_XX(ID_TAETID_Value, eID_NRMIAU, eID_MIT_XX) {
    var bReadOnly = (ID_TAETID_Value.toString().substr(0,1) == "A");
    getElById(eID_NRMIAU).readOnly = bReadOnly;
    //getElById(eID_NRMIAU).disabled = bReadOnly;
    getElById(eID_NRMIAU).className = bReadOnly ? "disabledCtrl" : "inActInpCtrl";

    getElById(eID_MIT_XX).readOnly = bReadOnly;
    //getElById(eID_MIT_XX).disabled = bReadOnly;
    getElById(eID_MIT_XX).className = bReadOnly ? "disabledCtrl" : "inActInpCtrl";
}

//function CheckISBN13(o, e) {
function CheckISBN13(strValue) {
    var isbn     = strValue;
    var strISBN  = "";
    var bIsValid = false;

    if (isbn == undefined || isbn.length == 0) return true;
    
    if (isbn.length > 0) {
        for (i = 0; i < isbn.length; i++) {
            if ("0123456789".indexOf( isbn.charAt(i)) > -1 ||
                (isbn.charAt(i) == "X" && i == 9)) {
                strISBN += isbn.charAt(i);
            }
        }
        isbn = strISBN;  
    }

    if (parseInt(isbn) == NaN || isbn.length != 13) {
        bIsValid = false;
//        return true;
    }
    else {
        var summe = 0, i = 0;
        for (i = 0; i <= 11; i++) {
            if (i % 2 != 0) {
                summe += 3 * parseInt(isbn.charAt(i));
            }
            else {
                summe += parseInt(isbn.charAt(i));
            }
        }
        var pruefziffer = 10 - (summe % 10);
        if ((isbn.charAt(12) == "0" && pruefziffer == 10) || pruefziffer == isbn.charAt(12))
            bIsValid = true;
        else
            bIsValid = false;
//        return true;
    }

    if (!bIsValid) {
        alert(unescape("Hinweis: Die ISBN ist ung%FCltig%21 Sie sollte aus 13 Ziffern%2C exkl. Trennzeichen bestehen%21"));
        bIsValid = true;
    }
    
    return true;
}

function CheckAUFLNR2(o, e, eID_AUFLNR) {
    //return ((getElById(eID_AUFLNR).readOnly && getElById(eID_AUFLNR).disabled) || (e.Value != undefined && e.Value.trim().length > 0));
    return ((getElById(eID_AUFLNR).readOnly) || (e.Value != undefined && e.Value.trim().length > 0));
}

//function retHref() {
//    return location.href;
//}

function showPopup(sUrl, iWidth, iHeight) {
    var sWindowName = sUrl;
    if (sWindowName.length > 20) sWindowName = sWindowName.substring(0, 20);
    sWindowName = "wPop_" + sWindowName.replace(/\W/g, "_");
    var iLeft = (screen.width - iWidth) / 2;
    var iTop = (screen.height - iHeight) / 2;
    var oWindow = window.open(sUrl, sWindowName, ",left=" + iLeft + ",top=" + iTop + ",width=" + iWidth + ",height=" + iHeight + ",resizable=yes,scrollbars=yes,status=yes", false);
    oWindow.focus();
}


