/*------------------------------------------------------------------------
'Method: AddToCookie()
'
'Description:
'   Adds a value to a cookie 
'
'Parameters:
'   name - string
'   value - string
'
'Returns:
'   null
'
'Notes:
'   <none>
'
'ToDo:
'
'----------------------------------------------------------------------*/
function AddToCookie (pstrName, pstrValue) 
{  
    var strNewValue = GetCookie(pstrName) + "["+pstrValue+"]";
    SetCookie(pstrName, strNewValue);
    
    return null;
 }

/*------------------------------------------------------------------------
'Method: RemoveFromCookie()
'
'Description:
'   Removes a value from the cookie
'
'Parameters:
'   name - string
'
'Returns:
'   null
'
'Notes:
'   <none>
'
'ToDo:
'
'----------------------------------------------------------------------*/
function RemoveFromCookie(pstrName, pstrValue) 
{  
    var strCurrentValue = GetCookie(pstrName);
    var strNewValue = strCurrentValue.replace("["+pstrValue+"]","");
    
    SetCookie(pstrName, strNewValue);
    return null;
 }
 
    
/*------------------------------------------------------------------------
'Method: GetCookie()
'
'Description:
'   Loads a cookie based on the name
'
'Parameters:
'   name - string
'
'Returns:
'   String = Success
'   Null = Failure
'
'Notes:
'   <none>
'
'ToDo:
'
'----------------------------------------------------------------------*/
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;
}
        
/*------------------------------------------------------------------------
'Method: SetCookie()
'
'Description:
'   Saves a cookie based on Name and Value
'
'Parameters:
'   Name - string
'   Value - string
'
'Returns:
'   void
'
'Notes:
'   <none>
'
'ToDo:
'
'----------------------------------------------------------------------*/
function SetCookie(name, value) 
{  
    var expDays = 30;
    var exp = new Date(); 
        exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

            
    var argv = SetCookie.arguments;  
    var argc = SetCookie.arguments.length;  
    var expires = (argc > 2) ? argv[2] : exp;  
    var path = (argc > 3) ? argv[3] : null;  
    var domain = (argc > 4) ? argv[4] : null;  
    var secure = (argc > 5) ? argv[5] : false;  
            
    document.cookie = name + "=" + escape (value) + 
                    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
                    ((path == null) ? "" : ("; path=" + path)) +  
                    ((domain == null) ? "" : ("; domain=" + domain)) +    
                    ((secure == true) ? "; secure" : "");
}


/*------------------------------------------------------------------------
'Method: DeleteCookie()
'
'Description:
'   Delete cookie by name
'
'Parameters:
'   name - string
'
'Returns:
'   void
'
'Notes:
'   <none>
'
'ToDo:
'
'----------------------------------------------------------------------*/
function DeleteCookie(name) 
{  
    var cval = GetCookie (name);
    var exp = new Date();  
        exp.setTime (exp.getTime() - 1);  
            
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
    
    
/*------------------------------------------------------------------------
'Method: getCookieVal()
'
'Description:
'   Loads a cookie value based on offset
'
'Parameters:
'   offset - int
'
'Returns:
'   String = Success
'
'Notes:
'   <none>
'
'ToDo:
'
'----------------------------------------------------------------------*/
function getCookieVal(offset) 
{  
    var endstr = document.cookie.indexOf (";", offset);  
    if (endstr == -1)    
        endstr = document.cookie.length;  

    return unescape(document.cookie.substring(offset, endstr));
}