
/* $Id: tabset.js 18732 2008-06-11 01:37:05Z pvandijk $ */

    var tabs = new Array();

    function isFieldVisible(element) {
        vis = true;

        if((typeof(element) == 'object') && (typeof(element.style) == 'object')) {

            if(element.style.display=='none') {
                vis = false;
            }

            if(element.type && element.type.toLowerCase() == 'hidden') {
                vis = false;
            } else if(element.type && element.type.toLowerCase() == 'radio') {
                vis = false;
            }

            if (vis) {
                el = element.parentElement;
                while(el != null){
                    if (el.style.display == 'none') {
                        vis = false;
                    }
                    el = el.parentElement;
                }
            }
        }

        return vis;
    }

    function isFieldOnScreen(element)
    {
        var y = findPosY(element) + element.offsetHeight;
        if (ns) {
            var h = window.innerHeight;
        } else {
            var h = document.body.offsetHeight
        }
        return y < h;
    }

    // used by quickform
    function focusTabField(field_name, has_steps, oForm){
        if (field_name) {
            if( document.getElementsByName(field_name) ){
                field = (typeof(oForm) == 'undefined') ? document.getElementsByName(field_name) : oForm[field_name];
                if(typeof(field) == 'object' && (typeof(field.tagName) != 'undefined') ) {
                    // get the tab obj
                    var tabstr = getTabObjFromField(field).id.split('_');

                    var tab = document.getElementById(tabstr[0] + '_' + tabstr[1] + '_' + tabstr[2]);
                    j = 0;

                    tab_idx = 0;

                    // enable all the tabs because we would have been submitted
                    while(tabs[j]) {
                        enableTab(tabs[j]);
                        if(tabs[j] == tab) {
                            tab_idx = j+1;
                        }
                        j++;
                    }

                    // switch
                    switchTab(tab);
                    if(tab_idx > 0 && typeof(qf_disable_div_tabs) != 'undefined') {
                        qfSwitchTab(tab_idx);
                    }

                    // javascript decides it wants to forget the value in 'field' here, so we need to reset it:
                    field = (typeof(oForm) == 'undefined') ? document.getElementsByName(field_name) : oForm[field_name];

                    if (isFieldVisible(field) && field.tagName != "SELECT" && !field.disabled) {
                        field.focus();
                    } else if (field.tagName == 'SELECT') {
                        // we still want to focus select fields.
                        field.focus();
                    }
                }
            }
        }
    }

    function getTabObjFromField(par){
        while(par != null && par.id.indexOf('_tab_')==-1){
            par = par.parentNode;
        }
        return par;
    }


    function switchTab(tab_obj, obj, skip_wizard_check) {

        // get the real id from the tab obj
        tab_id = tab_obj.id.split('_');
        tab_id = tab_id[1] + '_' + tab_id[2];

        var tab_result = true;

        if (typeof(window.onTabChange) != 'undefined') {
            tab_result = onTabChange(tab_obj.id);
        }

        if (tab_result) {
            if (!wizard || (wizard && wizard.element.id == tab_id) || (wizard && obj && wizard.element.id == obj.id) || skip_wizard_check) {
                if(tab_obj.tab_disabled) {
                    //
                } else {
                    deactivateAllTabs();
                    activateTab(tab_obj);

                    // Force reposition of info bubble if required
                    if (skip_wizard_check && wizard && wizard.info_bubble && wizard.info_bubble.reposition) {
                        wizard.info_bubble.reposition(true);
                    }
                }
            } else {
                wizard.confirmLeave();
            }
        }

        repositionDisableDivs();
        showHideProgressIndicators();
        showHideSliders();
        showLegend();

        if (typeof(window.onTabChangeDone) != 'undefined') {
            onTabChangeDone(tab_obj.id);
        }

    }

    function deactivateAllTabs() {
        for (i in tabs) {
            if (typeof(tabs[i]) == 'function') continue;
            deactivateTab(tabs[i]);
        }
    }

    function enableTab(obj) {
        obj.tab_disabled = false;
        if (obj.className == "disabledTab") {
            obj.className = 'inactiveTab';
        }
    }

    function disableTab(obj) {
        obj.tab_disabled = true;
        obj.className="disabledTab";
    }

    function activateTab(obj) {
        if(!obj.tab_disabled) {

            if(obj.cookie_select) {
                document.cookie = obj.cookie_name + '='+obj.id+';';
            } else {
                date = new Date(1);
                document.cookie = obj.cookie_name + '=0; expires='+date.toGMTString()+';';
            }
            obj.className="activeTab";
            showPage(obj.id + '_page');
        }
    }

    function showLegend() {
        var tabs_len = tabs.length, tab_data = '', active_legend = false;
        for (var i = 0; i < tabs_len; i++) {
            tab_data = tabs[i].id.split('_');
            active_legend = document.getElementById(tab_data[0] + '_legend_' + tab_data[1] + '_' + tab_data[2]);
            if (active_legend) {
                active_legend.style.display = (tabIsActive(tabs[i]) ? 'block' : 'none');
            }
        }
    }

    function deactivateTab(obj) {
        if(obj.tab_disabled) {
            obj.className="disabledTab";
            hidePage(obj.id + '_page');
        } else {
            obj.className="inactiveTab";
            hidePage(obj.id + '_page');
        }
    }

    function tabIsActive(obj) {
        return obj.className == "activeTab";
    }

    function registerTab(tab_id, cookie_select, disabled, cookie_name) {

        tab_obj = document.getElementById(tab_id);

        if(tabs.length == 0) {
            tab_obj.firstTab = true;
            tab_obj.lastTab  = true;
            tab_obj.style.backgroundColor = "";
        } else {
            tabs[tabs.length-1].lastTab == false;
            if(!tabs[tabs.length-1].firstTab) {
                tabs[tabs.length-1].style.backgroundColor = "";
            }

            tab_obj.style.backgroundColor = "";
            tab_obj.firstTab = false;
            tab_obj.lastTab  = true;


        }
        tab_obj.cookie_select = cookie_select;
        tab_obj.cookie_name = cookie_name;
        tabs[tabs.length] = tab_obj;
        deactivateTab(tab_obj);
        if(disabled) {
            disableTab(tab_obj);
        }
    }

    var activeTabIdx = 0;
    function _tab_init(cookie_name) {
        var found = false;
        cookies_array = document.cookie.split(';');
        var i, j;
        for(i = 0; i < cookies_array.length; i++) {
            if(!found) {
                tab_cookie = cookies_array[i].split('=');
                // if in wizard mode, allways activate the first tab by default.
                // set in tabset class
                if (wizard_mode && wizard_mode != 'undefined') {
                    var active_tab_cookie = tab_cookie[1].split('_');
                    tab_cookie[1] = active_tab_cookie[0] + '_tab_1';
                }
                if(tab_cookie[0].trim() == cookie_name) {
                    tab = document.getElementById(tab_cookie[1]);
                    if(tab) {
                        found = true;
                        // enable all the tabs before current
                        j = 0;
                        while(tabs[j] && tabs[j] != tab) {
                            enableTab(tabs[j]);
                            j++;
                        }
                        enableTab(tab);
                        activateTab(tab);
                    }
                }
            }
        }

        if(!found) {
            enableTab(tabs[activeTabIdx]);
            activateTab(tabs[activeTabIdx]);
        }

        event_handler.addMethod(ONLOAD, "repositionDisableDivs()");
        event_handler.addMethod(ONLOAD, "showHideProgressIndicators()");
        event_handler.addMethod(ONLOAD, "showLegend()");
    }

    function showPage(page_id) {

        page_obj = document.getElementById(page_id);
        page_obj.style.display = "";
        forminputs = page_obj.getElementsByTagName('INPUT');
        if(forminputs.length > 0) {
            for(var i = 0; i < forminputs.length; i++) {
                field = forminputs[i];
                if (!field.disabled && isFieldVisible(field) && isFieldOnScreen(field) && (typeof(field.tagName) != 'undefined') && (field.getAttribute('no_focus') != 'true')) {
                    try {
                        field.focus();
                        break;
                    } catch(e) {
                    }
                }
            }
        }

        // Netscape WYSIWYG Fix, when swaping tabs the abillity to edit
        if (ns) {
            var iframes = page_obj.getElementsByTagName('IFRAME');
            for (f=0; f<iframes.length; f++) {
                if (iframes[f].id.indexOf('_editor') != -1) {
                    iframes[f].contentDocument.designMode = 'on';
                }
            }
        }
    }

    function hidePage(page_id) {
        page_obj = document.getElementById(page_id);
        page_obj.style.display= "none";
    }
