//
//Common vars.
var isIE = false;
if (navigator.userAgent.toLowerCase().indexOf("msie") != -1 && navigator.userAgent.toLowerCase().indexOf("opera") == -1) {
    isIE = true;
};


//
//Set quicklink box events on start page.
function switchQuicklist(_box, _link) {
    var _boxcount = document.boxcount;  //is set dynamically in html code
    var _currentNumber = _box.split("quicklist");
    
    if (document.getElementById && document.getElementsByTagName) {
        var _quicklinkbox = document.getElementById("quicklinkbox");
        var _quicklinks = _quicklinkbox.getElementsByTagName("A");
        //
        //Set all quicklink boxes to invisible ...
        for (i=1; i <= _boxcount; i++) {
            if (i != _currentNumber[1]) {
                document.getElementById("quicklist"+i).style.display = "none";
            }
        }
        //... and activate/deactivate current box.
        document.getElementById(_box).style.display = (document.getElementById(_box).style.display == 'block') ? 'none' : 'block';
        //
        //Set all main quicklinks to normal style ...
        for (j=0; j < _quicklinks.length; j++) {
            if (_quicklinks[j].className.indexOf("quicklink") != -1) {
                _quicklinks[j].className = "quicklink";
            }
        }
        //
        //... and activate/deactivate current link by triggering display property of currently shown quicklink box.
        document.getElementById("quicklink"+_currentNumber[1]).className = (document.getElementById(_box).style.display == 'block') ? "quicklink-active" : "quicklink";
    }
    return false;
};


//
//Switch between "Five Reasons" images at startpage
function switchReason(num, languageKey) {
    if (document.getElementById) {
        _img = document.getElementById("fivereasons-img");
        _tmp = _img.src.split("reason_");    //get img path
        _imagepath = _tmp[0];
        _img.src = _imagepath +"reason_"+ languageKey + "_" + num +".gif";
    }
    return false;
};


//
//Init all appointmentboxes with display:none
function setAllAppointmentboxesToInvisible() {
    if (document.getElementsByTagName) {
        _tmp = document.getElementsByTagName("DIV");
        for (i=0; i < _tmp.length; i++) {
            if (_tmp[i].id.indexOf("appointmentbox") != -1) {
                _tmp[i].style.display = "none";
            }
        }
    }
};


//
//Count all appointment boxes to have an inital amount (helper function)
function countAllAppointmentboxes() {
    var _count = 0;
    if (document.getElementsByTagName) {
        _tmp = document.getElementsByTagName("DIV");
        for (i=0; i < _tmp.length; i++) {
            if (_tmp[i].id.indexOf("appointmentbox") != -1) {
                _count++;
            }
        }
    }
    return _count;
};


//
//Set appointment box events
function switchAppointmentBox(_box) {
    var _boxcount = countAllAppointmentboxes();
    var _currentNumber = _box.split("appointmentbox");
    //Store currently opened box so we can have it opened in the print page.
    document.currentAppointmentBox = _currentNumber[1];

    if (document.getElementById && document.getElementsByTagName) {
        //
        //Set all appointment boxes to invisible ...
        for (i=1; i <= _boxcount; i++) {
            if (i != _currentNumber[1]) {
                document.getElementById("appointmentbox"+i).style.display = "none";
            }
            document.getElementById("appointment"+i).style.display = "block";
        }
        //        
        //... and activate/deactivate current detail box ...
        if (document.getElementById(_box).style.display == "block") {
            document.getElementById(_box).style.display =  "none";
            document.currentAppointmentBox = "";    //Reset current box identifier for print page.
        } else {
            document.getElementById(_box).style.display = "block";
        }
        //
        //... then activate/deactivate current linked appointment box.
        if (document.getElementById(_box).style.display == 'block') {
            document.getElementById("appointment"+_currentNumber[1]).style.display =  "none";
        } else {
            document.getElementById("appointment"+_currentNumber[1]).style.display =  "block";
        }
    }
    return false;
};


//
//Open current appointment box in print page.
function openCurrentAppointmentBox() {
    if (opener) {
        if (opener.document.currentAppointmentBox && document.getElementById) {
            if (document.getElementById("appointment" + opener.document.currentAppointmentBox) != "" && document.getElementById("appointmentbox" + opener.document.currentAppointmentBox)) {
                document.getElementById("appointment" + opener.document.currentAppointmentBox).style.display = "none";
                document.getElementById("appointmentbox" + opener.document.currentAppointmentBox).style.display = "block";
            }
        }
    }
};


//
//Set <html> id to "top" so browsers other than IE can have "up to page top" links.
function setPageTopId() {
    if (document.getElementsByTagName) {
        if (!isIE) {
            _tmp = document.getElementsByTagName("html");
            _tmp[0].id = "top";
        }
    }
};


//
//Set alternating background color for table rows in <div.articletable>.
function setArticleTableRowBackgrounds() {
    var _tables;
    var _rows;
    var _cells;
    //Get all tables, cells and its rows from article container.
    if (document.getElementById && document.getElementsByTagName) {
        if (document.getElementById("content")) {
            _container = document.getElementById("content");
            _items = _container.getElementsByTagName("DIV");
            for (i=0; i < _items.length; i++) {
                if (_items[i].className == "articletable") {
                    if (_items[i].getElementsByTagName("TABLE")) {
                        _tables = _items[i].getElementsByTagName("TABLE");
                        for (j=0; j < _tables.length; j++) {
                            _rows = _tables[j].getElementsByTagName("TR");
                            for (k=0; k < _rows.length; k++) {
                                _cells = _rows[k].getElementsByTagName("TD");
                                for (m=0; m < _cells.length; m++) {
                                    //_cells[m].style.padding = "4px";
                                }
                                if (k % 2 == 0) {    // set != 0 if table shall start with a white row
                                    for (m=0; m < _cells.length; m++) {
                                        _cells[m].style.background = "#f0f0f0";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
};


//
//Reset borders for <a href> including images in <div.articletable>.
function resetBordersForImageLinks() {
    var _links;
    var _images;
    //Get all links from article container.
    if (document.getElementById && document.getElementsByTagName) {
        if (document.getElementById("content")) {
            _container = document.getElementById("content");
            _items = _container.getElementsByTagName("DIV");
            for (i=0; i < _items.length; i++) {
                if (_items[i].className == "article") {
                    _links = _items[i].getElementsByTagName("A");
                    for (j=0; j < _links.length; j++) {
                        //If links contain images, dont apply border or text-decoration styles.
                        if (_links[j].getElementsByTagName("IMG")) {
                            _images = _links[j].getElementsByTagName("IMG");
                            for (k=0; k < _images.length; k++) {
                                _images[k].parentNode.style.border = "0 none";
                                _images[k].parentNode.style.textDecoration = "none";
                            }
                        }
                    }
                }
            }
        }
    }
};


//
//Script to open a print popup
function openPrintWindow(address) {
    newWin = window.open(address, "Printwindow", "width=700,height=500,scrollbars=yes,menubar=yes,resizable=yes");
    newWin.focus();
    return false;
}


//
//Clear input field of search form by clicking into it.
function clearSearchField() {
    if (document.getElementById) {
        if (document.getElementById("searchfield")) {
            _field = document.getElementById("searchfield");
            _field.onfocus = function() { this.value = ""; }
        }
    }
};


//
//Set all checkboxes and radio buttons to width:auto (IE only)
function resetFormFieldWidthsInIE() {
    if (isIE) {
        if (document.getElementsByTagName) {
            var _forms = document.getElementsByTagName("FORM");
            var _fields;
            for (i=0; i < _forms.length; i++) {
                _fields = _forms[i].getElementsByTagName("INPUT");
                for (j=0; j < _fields.length; j++) {
                    if (_fields[j].type == "checkbox" || _fields[j].type == "radio" ) {
                        _fields[j].style.width = "auto";
                        _fields[j].style.border = "0 none";
                    }
                }
            }
        }
    }
};


//
//Functions needed to be executed on page load.
function executeOnLoad() {
    //Add functions here.
    setPageTopId();
    setAllAppointmentboxesToInvisible();
    openCurrentAppointmentBox();
    clearSearchField();
    resetFormFieldWidthsInIE();
    setArticleTableRowBackgrounds();
    resetBordersForImageLinks();
};
window.onload = executeOnLoad;
