/** 
 * Java script file for cstabmanager component
 *
 */

    // Boolean variable specified if alert should be displayed if cookie exceeds 4KB
    var caution = false
    
    /** 
     * @param string name Name of the cookie
     * @param string value Value of the cookie
     * @param string expires Expiration date of the cookie (defaults to end of current session)
     * @param string path Path for which the cookie is valid (defaults to path of calling document)
     * @param string domain Domain for which the cookie is valid (defaults to domain of calling document)
     * @param string secure Boolean value indicating if the cookie transmission requires a secure transmission
     */
    function setCookie(name, value, expires, path, domain, secure) 
    {
        var curCookie = name + "=" + escape(value) +
                      ((expires) ? "; expires=" + expires.toGMTString() : "") +
                      ((path) ? "; path=" + path : "") +
                      ((domain) ? "; domain=" + domain : "") +
                      ((secure) ? "; secure" : "");
                      
        if (!caution || (name + "=" + escape(value)).length <= 4000) {
            document.cookie = curCookie
        } else if (confirm("Cookie exceeds 4KB and will be cut!")) {
            document.cookie = curCookie;
        }
    }
    
    /**
     * Return string containing value of specified cookie or null if cookie does not exist
     *
     * @param string name Name of the desired cookie
     */
    function getCookie(name)
    {
        var prefix = name + "=";
        var cookieStartIndex = document.cookie.indexOf(prefix);
        
        if (cookieStartIndex == -1) return null;
        
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
        if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
        
    return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
    }

    /** 
     * @param string name Name of the cookie
     * @param string path Path of the cookie (must be same as path used to create cookie)
     * @param string domain Domain of the cookie (must be same as domain used to create cookie)
     * path and domain default if assigned null or omitted if no explicit argument proceeds
     */
    function deleteCookie(name, path, domain)
    {
        if (getCookie(name)) {
            document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    }

    /**
     * You should hand all instances of the Date object to this function for "repairs"     
     *
     * @param Date date any instance of the Date object
     */
    function fixDate(date)
    {
        var base = new Date(0)
        var skew = base.getTime()
        if (skew > 0) date.setTime(date.getTime() - skew)
    }


    function setActivePage(objectName, pageName)
    {
        currPage = document.getElementById(objectName + "_ActivePage").value;
        
        // if (pageName != currPage) {
            document.getElementById(objectName + "_ActivePage").value = pageName;
            document.getElementById(objectName + "_" + currPage).style.display = 'none';
            document.getElementById(objectName + "_" + currPage  + "_tabh").className = 'tab';
            
            document.getElementById(objectName + "_" + pageName).style.display = 'block';
            document.getElementById(objectName + "_" + pageName + "_tabh").className = 'tab_selected';
            
            /* --- Sets coockie for 60 seconds --- */
            var now = new Date()
            fixDate(now)
            now.setTime(now.getTime() + 24 * 60 * 60)
            setCookie(objectName + "_ActivePage", pageName, now);
        // }
    }
