if (!document.getElementById) {
    document.getElementById = function() {
        return null;
    }
}

function getRawObject(obj) {
    var theObj;
    if (typeof obj == "string") {
        theObj = document.getElementById(obj);
    } else {
        // pass through object reference
        theObj = obj;
    }
    return theObj;
}

function getElementStyle(obj, IEStyleProp, CSSStyleProp) {
    var elem = getRawObject(obj);
    if (elem) {
        if (elem.currentStyle) {
            return elem.currentStyle[IEStyleProp];
        } else if (window.getComputedStyle) {
            var compStyle = window.getComputedStyle(elem, "");
            return compStyle.getPropertyValue(CSSStyleProp);
        }
    }
    return "";
}

function toggleBgColor(elem) {
    var bgColor = getElementStyle(elem, "backgroundColor", "background-color");
    bgColor = (bgColor == "" ? "transparent" : bgColor);
    if (bgColor == "transparent") {
        elem.style.backgroundColor = bgColor;
    } else {
        elem.style.backgroundColor = "transparent";
    }
}

function toggleTextDecoration(elem) {
    if (elem.style.textDecoration == "underline") {
        elem.style.textDecoration = "none";
    } else {
        elem.style.textDecoration = "underline";
    }
}

function openNewWindow(url, windowName, windowFeatures) {
    var attributes = (windowFeatures && windowFeatures != "") ? windowFeatures : "width=500,height=700,menubar,resizable,scrollbars,status";
    var newWindow = window.open(url, windowName, attributes);
    if (window.focus) {
        newWindow.focus();
    }
}

function openNewPicWindow(url, windowName) { // Querformat
    var attributes = "width=470,height=560,menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewPanelPicWindow(url, windowName) { // Hochformat    
    var attributes = "width=470,height=600,menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewMapWindow(url, windowName) { // Karte
    var attributes = "width=770,height=740,menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewMovieWindow(url, windowName) { // Flash
    var attributes = "width=485,height=550,menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function focusElem(elemId) {
    var elem = document.getElementById(elemId);
    if (elem && elem.focus) {
        elem.focus();
    }
}

// Global variable for subwindow reference
var printWindow;

function openPrintWindow(url, target) {
    var windowUrl = url ? url : window.location.href;
    var windowName = target ? target : "_blank";
    printWindow = window.open(windowUrl, windowName, "width=730,height=800,scrollbars,resizable,menubar,toolbar,left=0,top=0");
    printWindow.focus();
    if (window.print) {
        window.setTimeout("printWindow.print()", 1000);
    }
}

/* Switch actual stylesheet to print and trigger print dialog if supported */
function setupPrint() {
    // hide whole body to avoid flickering while disabling and enabling the stylesheets
    document.body.style.display = "none";
    if (document.getElementsByTagName) {
        var elem;
        for(var i = 0; (elem = document.getElementsByTagName("link")[i]); i++) {
            if (elem.getAttribute("rel").indexOf("style") != -1 && elem.getAttribute("media") != "print") {
                elem.disabled = true;
                // if link is an alternate stylesheet and has both screen and print media type
                if (elem.getAttribute("rel").indexOf("alt") != -1 && elem.getAttribute("title").indexOf("Druckvorschau") != -1) {
                    // enable stylesheet
                    elem.disabled = false;
                }
            }
        }
    }
    // show body again
    document.body.style.display = "block";
    if (window.print) {
        window.setTimeout("window.print()", 1000);
    }
}

function validateChoice(elem) {
    var isValid = true;
    if (elem && elem.type == "select-one") {
        if (elem.options[elem.selectedIndex].value == "") {
            isValid = false;
        }
    }
    return isValid;
}

function prepareInputButtons() {
    if (document.getElementsByTagName) {
        var inputElems = document.body.getElementsByTagName("INPUT");
        for (var i = 0; i < inputElems.length; i++) {
            var inputElem = inputElems[i];
            if ((inputElem.type == "submit" || inputElem.type == "reset" || inputElem.type == "button") && inputElem.className == "subbtn") {
        		// set toggleTextDecoration for suitable events
        		// existing events are saved
                var cachedOnMouseOver = inputElem.onmouseover;
                var cachedOnMouseOut = inputElem.onmouseout;
                var cachedOnFocus = inputElem.onfocus;
                var cachedOnBlur = inputElem.onblur;
                
                inputElem.onmouseover = function() {
                    toggleTextDecoration(this);
                    if (cachedOnMouseOver) {
                        return cachedOnMouseOver();
                    }
                }
                inputElem.onmouseout = function() {
                    toggleTextDecoration(this);
                    if (cachedOnMouseOut) {
                        return cachedOnMouseOut();
                    }
                }
                inputElem.onfocus = function() {
                    toggleTextDecoration(this);
                    if (cachedOnFocus) {
                        return cachedOnFocus();
                    }
                }
                inputElem.onblur = function() {
                    toggleTextDecoration(this);
                    if (cachedOnBlur) {
                        return cachedOnBlur();
                    }
                }
            }
        }
    }
}

window.onload = prepareInputButtons;
