﻿var tc;
var ac;

var tcCookieName = 'tabIndex';

var acTransitionDuration = 100;

function pageLoad()
{
    tc = $find(tcId);
    ac = $find(acId + "_AccordionExtender");

    if (tc != null)
    {
        TabsRestoreIndex();
        tc.add_activeTabChanged( function() { TabsSaveIndex(); });
    }

    if (ac != null)
    {
        WGroupsAccordionRestoreIndex();
        ac.add_selectedIndexChanged(function() { WGroupsAccordionSaveIndex(); });
    }

}

function TabsSaveIndex()
{
    DeleteCookie(tcCookieName);
    SetCookie(tcCookieName, tc.get_activeTabIndex());
}

function TabsRestoreIndex()
{
    var ti = GetTabIndexFromCookie();
    var co = tc.get_tabs().length - 1;
//    alert("ti=" + ti +"; count = "+ co);
    if (ti > co) tc.set_activeTabIndex(0); 
    else tc.set_activeTabIndex(ti);
}

function GetTabIndexFromCookie()
{
    var r = parseFloat(GetCookie(tcCookieName));
    return (!r) ? 0 : r;
}

function WGroupsAccordionRestoreIndex()
{
    var ti = GetTabIndexFromCookie();
    /*alert("ti=" + ti +"; count = "+ac.get_Count());*/
    if (ti > (ac.get_Count()-1)) return;
    ac.set_TransitionDuration(1);
    ac.set_SelectedIndex(ti);
    ac.set_TransitionDuration(acTransitionDuration);
}

function WGroupsAccordionSaveIndex()
{
    DeleteCookie(tcCookieName);
    SetCookie(tcCookieName, ac.get_SelectedIndex());
}

function GetCookie(name)
{   
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while(i < clen)
    {
      var j = i + alen;
      if(document.cookie.substring(i,j) == arg) return GetCookieVal(j);
      i = document.cookie.indexOf(" ",i) + 1;
      if (i == 0) break;   
    }   
    return null;
}

function GetCookieVal(offset)
{
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}   

function SetCookie(name, value, expires, path, domain, secure)
{
    if (!path) path = '/';
    if (!expires) expires = cExpireDate;

    document.cookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
}

function DeleteCookie(name, path, domain)
{
    if (GetCookie(name))
    {
        document.cookie = name + "=" +
          ((path) ? "; path=" + path : "") +
          "; expires=The,01-Jan-70   00:00:01   GMT";
    }
}

function FixCookieDate(date)
{
    var base = new Date(0);
    var skew = base.getTime(); // dawn of (Unix) time - should be 0
    if (skew > 0)  // Except on the Mac - ahead of its time
        date.setTime(date.getTime() - skew);
}

var cExpireDate = new Date();
FixCookieDate(cExpireDate);
cExpireDate.setTime(cExpireDate.getTime() + (365 * 24 * 60 * 60 * 1000));

function xoUpdateScrollY() {
    if (document.all)
    {
        if (!document.documentElement.scrollTop) document.forms[0].exxoScrollY.value = document.body.scrollTop;
        else document.forms[0].exxoScrollY.value = document.documentElement.scrollTop;
    }
    else document.forms[0].exxoScrollY.value = window.pageYOffset;
}

function xoRestoreScrollY(sy) {
    window.scrollTo(0, sy);
}


