function removeSubstring(str, strRemove)
{
    if (str == null)
        return "";
            
    var     nStart = -1,
            nEnd   = -1;
    var     strReturn = str;
    var     bFound = false;
    
    nStart = str.indexOf(strRemove);
    if (nStart != -1)
    {
    do
    {
            strReturn = str.substring(0, nStart);
            
            nEnd = strRemove.length + nStart;
            if (nEnd >= str.length)
            {
                // strReturn already has everything we need, 'cause
                // we're chopping off the last segment.
                bFound = true;
                break;
            }
            if ((str.charAt(nEnd) != '|' && str.charAt(nEnd) != '%'))
            {
                // we matched a list member that's an initial substring of 
                // what we're looking for - ignore it!!
                continue;
            }

            // found a match in the middle of the list...
            bFound = true;
            strReturn += str.substring(nEnd+1);
            break;
            
    }while ((nStart = str.indexOf(nStart+nEnd+1, strRemove)) != -1)
    }
    if (!bFound)
        strReturn = str;
    return strReturn;
}        

function removeFromExpiredList(cookie, qs, domain)
{
    var list = cookie.list;
    if (list == null)
        return;
    else
        list = list.toLowerCase();
        
    list = removeSubstring(list, qs)
    
    cookie.list = list;
    //alert("new cookie list: " + cookie.list);
    
    
    // get rid of the old cookie...
    cookie.$path="/";
    cookie.$domain=domain;
    cookie.remove();
    
    // restore the new cookie...
    cookie.store();
}



function isInExpiredList(cookie, qs)
{
    var bReturn = false;
    var list = cookie.list;
    if (list != null)
    {
        list = list.toLowerCase();        
        var nStart = list.indexOf(qs);
        
        if (nStart != -1)
        {
            var nEnd = nStart + qs.length;
            if (list.length == nEnd || list.charAt(nEnd) == '|')
            {
                bReturn = true;
            }
        }
        if (list.indexOf("refresh_all_tabs") != -1)
            bReturn = true;
    }
    return bReturn;
}

// JavaScript function to return all cookie names in an array.
function getAllCookieNames() 
{
    var tempArray = document.cookie.split(";");
    var cookieNames = new Array();
    for (i in tempArray)
    {
         var iEnd = tempArray[i].indexOf("="); 
      	 cookieNames[i] = tempArray[i].substring(0,iEnd);
      	 if (cookieNames[i].substring(0,1) == " ")
      	     cookieNames[i] = cookieNames[i].substring(1);
      	 
    }
    return cookieNames;
}


function removeSomeRefreshcookies(all_cookies, cookieName, domain)
{
    var cName = (unescape(cookieName)).toLowerCase();
    var iTotal = 0;
    for (i in all_cookies)
    {
       if (all_cookies[i].length > 40)
       {
           var CurCookieName = (unescape(all_cookies[i])).toLowerCase();
           var LastEight=CurCookieName.substr(CurCookieName.length - 8,8);
           // last eight characters in cookie name ends with "\refresh" and cookie name > 40 characters
           if (LastEight == "/refresh" && CurCookieName != cName)
           {
	         // but don't delete the current one
	         // alert('will delete cookie CurCookieName='+CurCookieName);
                 var pageExpired = new Cookie(document, all_cookies[i]);
                 pageExpired.$path="/";
                 pageExpired.$domain=domain;
                 pageExpired.remove();
                 iTotal++;
                 if (iTotal > 3)  // remove 4 refresh cookies
                       break;
	   }
	}
    }
}

function addToExpiredList(cookie, qs, domain)
{
    if (qs == null || qs == "")
        return;
    var list = cookie.list;
    if (list != null)
        list = list.toLowerCase();

    if (!isInExpiredList(cookie, qs))
    {
        if (list == null || list.length == 0)
            list = qs;
        else
        {
            if (list.lastIndexOf("|") == (list.length-1))
                list = list + qs;
            else    
                list = list + "|" + qs;
        }
        cookie.list = list;
        // get rid of the old cookie...
        cookie.$path="/";
        cookie.$domain=domain;
        cookie.remove();
        // restore cookie...
        cookie.store();
    }
}

function removeFromCookie(cookie, qs, domain)
{
    removeFromExpiredList(cookie, qs, domain)
    var list = cookie.list;
    if (list == null || list == "")
    {
        cookie.$domain=domain;
        cookie.$path="/";   
        cookie.remove();
    }
}

function refreshOnExpired(cookieName, qs, domain)
{
var all_cookies = getAllCookieNames();
// remove some refresh cookies if the number of cookies >=17
if (all_cookies.length >= 17)
     removeSomeRefreshcookies(all_cookies, cookieName, domain);
var toRefresh = refreshOnExpired2(cookieName,qs,domain);
if ( !toRefresh ) {
	return false;
}
// delay refresh for IE5.0 when using https to eliminate security warning
if (navigator.appVersion.indexOf("MSIE 5.0")>0) {
     var hRefPrefix = document.location.protocol.toLowerCase();
     if (hRefPrefix == "https:") {
          setTimeout("document.location.reload();",1);
	  return true;
     }
}
     document.location.reload();
     return true;
}

function refreshOnExpired2(cookieName, qs, domain)
{
    cookieName = cookieName.toLowerCase();
    qs = qs.toLowerCase();
    var pageExpired = new Cookie(document,  cookieName);
    
    if (pageExpired.load())
    {
        //alert("found cookie");
        if (isInExpiredList(pageExpired, qs))
        {
            //alert("found cookie, reloading page");
            removeFromCookie(pageExpired, qs, domain);            
            return true;
        }
    }
    else
    { 
           //alert("cookie not found, ignore");
            return false;
    }
  
	return false;
}


function setRefreshPage(cookieName, qs, domain)
{
   cookieName = cookieName.toLowerCase();

   var pageExpired = new Cookie(document, cookieName);
    if (!pageExpired.load())
    {
        pageExpired = new Cookie(document, cookieName, 120, "/", domain);
    }

    qs = qs.toLowerCase();
    
    if (qs == null)
        qs ="?tab=DEFAULT";
    addToExpiredList(pageExpired, qs, domain);
}