function cms_switch_tab(id,tab) {
	if (e = gE(id)) {
		for(i=0;i<e.childNodes.length;i++) {
			f = e.childNodes[i];
			if (!f.id) continue;
			if (f.id == tab) {
				f.className='active';
				sD(gE(f.id+'_div'));
			} else {
				f.className='';
				hD(gE(f.id+'_div'));
			}
		}
	}
}

function cms_switch_panel(id,tab) {
	cms_switch_tab(id+'_menu',id+'_lnk_'+tab);
	if (e = gE(id)) {
		for(i=0;i<e.childNodes.length;i++) {
			f = e.childNodes[i];
			if (!f.id) continue;
			if (f.id == id+'_'+tab) {
				f.className='tab_show';
				sD(gE(f.id+'_div'));
			} else if (f.id != id+'_menu') {
				f.className='tab_hide';
				hD(gE(f.id+'_div'));
			}
		}
	}
}


// CSS helper functions
CSS = {
    // Adds a class to an element.
    AddClass: function (e, c) {
        if (!e.className.match(new RegExp("\\b" + c + "\\b", "i")))
            e.className += (e.className ? " " : "") + c;
    },

    // Removes a class from an element.
    RemoveClass: function (e, c) {
        e.className = e.className.replace(new RegExp(" \\b" + c + "\\b|\\b" + c + "\\b ?", "gi"), "");
    }
};

// Functions for handling tabs.
Tabs = {
    // Changes to the tab with the specified ID.
    GoTo: function (contentId, skipReplace) {
        // This variable will be true if a tab for the specified
        // content ID was found.
        var foundTab = false;

        // Get the TOC element.
        var toc = document.getElementById("tab_toc");
        if (toc) {
            var lis = toc.getElementsByTagName("li");
            for (var j = 0; j < lis.length; j++) {
                var li = lis[j];

                // Give the current tab link the class "current" and
                // remove the class from any other TOC links.
                var anchors = li.getElementsByTagName("a");
                for (var k = 0; k < anchors.length; k++) {
                    if (anchors[k].hash == "#" + contentId) {
                        CSS.AddClass(li, "active");
                        foundTab = true;
                        break;
                    } else {
                        CSS.RemoveClass(li, "active");
                    }
                }
            }
        }

        // Show the content with the specified ID.
        var divsToHide = [];
        var divs = $$('.tab_panel');
        for (var i = 0; i < divs.length; i++) {
            var div = divs[i];

            if (div.id == "sub_" + contentId)
	            div.removeClass('tab_hide');
            else
                divsToHide.push(div);
        }

        // Hide the other content boxes.
        for (var i = 0; i < divsToHide.length; i++)
            divsToHide[i].addClass('tab_hide');

        // Change the address bar.
        if (!skipReplace) window.location.replace("#" + contentId);
        
        // change form submit
        try {
			fact = document.cms_admin_form;
			if ((i = fact.action.indexOf('#')) >= 0) {
				fact.action = fact.action.substring(0,i+1)+contentId;
			} else {
				fact.action += '#'+contentId;
			}
		} catch(e) {
		}
    },

    OnClickHandler: function (e) {
        // Stop the event (to stop it from scrolling or
        // making an entry in the history).
        var found = false;
        var tab = this.hash.substring(1);
        var toc = document.getElementById("tab_toc");
        if (toc) {
            var lis = toc.getElementsByTagName("li");
        	for (var j = 0; j < lis.length; j++) {
                var li = lis[j];
                var anchors = li.getElementsByTagName("a");
                for (var k = 0; k < anchors.length; k++) {
                    if (anchors[k].hash == "#" + tab) {
                    	found = true;
                    	break;
                    }
                }
            }
        }
        
        if (!found) {
        	return true;
        }
        
        if (!e) e = window.event;
        if (e.preventDefault) e.preventDefault(); else e.returnValue = false;

        // Get the name of the anchor of the link that was clicked.
        Tabs.GoTo(tab);
    },

    Init: function () {
        if (!$('tab_toc')) return;

        // Attach an onclick event to all the anchor links on the page.
        var anchors = $$("a");
        for (var i = 0; i < anchors.length; i++) {
            var a = anchors[i];
            if (a.hash.length > 1) a.onclick = Tabs.OnClickHandler;
        }

        var contentId;
        if (window.location.hash) contentId = window.location.hash.substring(1);

        if (!contentId) {
        	// open first panel
        	divs = $$('#tab_toc a');
			if (divs.length) {
				contentId = divs[0].hash.substring(1);
			}
        }
        Tabs.GoTo(contentId, true);
    }
};

// Hook up the OnLoad event to the tab initialization function.
// window.onload = Tabs.Init;

// Hide the content while waiting for the onload event to trigger.
var contentId = window.location.hash;

/*
if (document.createStyleSheet) {
	//$('tab_'+contentId).setStyle('display','block');
    var style = document.createStyleSheet();
    style.addRule("div.content", "display: none;");
    style.addRule("div" + contentId, "display: block;");
} else {
    var head = document.getElementsByTagName("head")[0];
    if (head) {
        var style = document.createElement("style");
        style.setAttribute("type", "text/css");
        style.appendChild(document.createTextNode("div.content { display: none; }"));
		style.appendChild(document.createTextNode("div" + contentId + " { display: block; }"));
        head.appendChild(style);
    }
}
*/