﻿//Cookie name
var tabCookieName = "ctkCurrentTab";

function saveTabIndex(tabText) {
    //Call JS function to save cookie name, tab text,
    //and days before cookie should expire
    setCookie(tabCookieName, tabText, 1);
}

//Cookie operation helper function
//Save the value to a cookie and set expiration date
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function loadTabIndex(tabstrip) {
    //If tabstrip reference isn't null
    if (tabstrip != null) {
        //Get cookie value
        var tabText = getCookie(tabCookieName);
        //If text from the cookie exists
        if (tabText != "" || tabText != null) {
            //Set tabstrip selected index
            var tab = tabstrip.findTabByText(tabText); //Get tab object
            if (tab != null) {
                tab.select(); //Select tab
            }
        }
    }
}

//Cookie helper function
//Gets a cookie based on supplied name and returns value
//as a string
function getCookie(c_name) {
    try {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
    }
    catch (err) { }
    //If there is an error or no cookie
    //return an empty string
    return "";
}
