if (!document.getElementByName) {
    document.getElementByName = function() {
        return null;
    }
}

function getRawObject(obj) {
    var theObj;
    if (typeof obj == "string") {
        theObj = document.getElementByName(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 windowWidth = getWinWidth(500);
	var windowHeight = getWinHeight(700);
    var attributes = (windowFeatures && windowFeatures != "") ? windowFeatures : "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    var newWindow = window.open(url, windowName, attributes);
    if (window.focus) {
        newWindow.focus();
    }
}

function openNewPicWindow(url, windowName, intWindoscrWidth) { // Querformat
	if (!intWindoscrWidth || intWindoscrWidth < 450) { intWindoscrWidth = 450; }
	var windowWidth = getWinWidth(intWindoscrWidth) + 50;
	var windowHeight = getWinHeight(560);
	var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewPanelPicWindow(url, windowName, intWindoscrWidth) { // Hochformat    
	if (!intWindoscrWidth) { intWindoscrWidth = 470; }
	var windowWidth = getWinWidth(intWindoscrWidth);
	var windowHeight = getWinHeight(600);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewMapWindow(url, windowName) { // Karte
	var windowWidth = getWinWidth(770);
	var windowHeight = getWinHeight(620);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function openNewMovieWindow(url, windowName) { // Flash
	var windowWidth = getWinWidth(485);
	var windowHeight = getWinHeight(550);
    var attributes = "width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status";
    openNewWindow(url, windowName, attributes);
}

function focusElem(elemId) {
    var elem = document.getElementByName(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";
	var windowWidth = getWinWidth(730);
	var windowHeight = getWinHeight(600);
    printWindow = window.open(windowUrl, windowName, "width=" + windowWidth + ",height=" + windowHeight + ",scrollbars,resizable,menubar,toolbar,left=0,top=0");
    printWindow.focus();    
}


/* 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();
                    }
                }
            }
        }
    }
}

function getWinWidth(favWidth) {
	var sh;
	if (!favWidth) { favWidth = 600 } // assume a window that fits in smallest common screen
    if (screen.width) {
        sw = screen.width;
    } else {
		sw = 640; // assume smallest common screen resolution
	}
	if (favWidth < sw) { // if window fits in screen
		return favWidth; // return the preferred width
	} else {
		return sw - 50; // return screen width minus a padding to screen border
	}
}

function getWinHeight(favHeight) {
	var sh;
	if (!favHeight) { favHeight = 400 } // assume a window that fits in smallest common screen
    if (screen.height) {
        sh = screen.height;
    } else {
		sh = 480; // assume smallest common screen resolution
	}
	if (favHeight < sh) { // if window fits in screen
		return favHeight; // return the preferred height
	} else {
		return sh - 50; // return screen height minus a padding for windows taskbar etc.
	}
}


window.onload = prepareInputButtons;