/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2002 Click2Learn Corporation
//
// The copyright to the computer software herein is proprietary and remains
// the property of Click2Learn, USA. It may be used and/or copied only with
// the written consent of Click2Learn or in accordance with the terms and
// conditions stipulated in the agreement/contract under which this software
// has been supplied.
//
// $Id: SYS_cookie.js,v 1.1.1.1 2002/03/05 01:27:04 ananda Exp $
// $Date: 2002/03/05 01:27:04 $
// $RCSfile: SYS_cookie.js,v $
// $Revision: 1.1.1.1 $
//
// Abstract:
//
//    Browser class to identify browsers and operating system
//
// Revision History:
//
//    2002-02-25   Anand Arvind    Created file
//
/////////////////////////////////////////////////////////////////////////////

function _ObjCookie(document, name, hours, path, domain, secure)
{

    this.$document   = document ;
    this.$name       = name ;
    this.$expiration = null ;
    this.$path       = null ;
    this.$domain     = null ;
    this.$secure     = false ;

    if ( hours )  this.$expiration = new Date((new Date()).getTime() + hours * 360000);
    if ( path )   this.$path = path;
    if ( domain ) this.$domain = domain ;
    if ( secure ) this.$secure = true  ;

    //
    // Get named cookie and store these as properties of the object
    //
    _ObjCookie.prototype.load = function()
    {

        var i, nStart, nEnd, objList ;
        var strCookies = this.$document.cookie ;

        if ( strCookies == "" )
            return false ;

        nStart = strCookies.indexOf(this.$name + "=");
        if ( nStart == -1 )
            return false ;
        nStart += this.$name.length + 1 ;

        nEnd = strCookies.indexOf(';', nStart);
        if ( nEnd == -1 )
            nEnd = strCookies.length ;
        var strCookieVal = strCookies.substring(nStart, nEnd);

        objList = strCookieVal.split('&');
        for ( var i = 0 ; i < objList.length ; i++ )
            objList[i] = objList[i].split('=');

        for ( var i = 0 ; i < objList.length ; i++ )
            this[objList[i][0]] = unescape(objList[i][1]);

        return true ;

    }

    //
    // Generate cookie
    //
    _ObjCookie.prototype.generate = function()
    {

        var strCookieVal = "";
        var prop ;

        for ( var prop in this )
        {
            if ( prop.charAt(0) == "$" || ((typeof this[prop]) == "function" ))
                continue ;
            if ( strCookieVal != "" ) strCookieVal += "&";
            strCookieVal += prop + "=" + escape(this[prop]);
        }

        var strCookie = this.$name + "=" + strCookieVal ;
        if ( this.$expiration )
            strCookie += "; expires=" + this.$expiration.toGMTString();
        if ( this.$path )
            strCookie += "; path=" + this.$path ;
        if ( this.$domain )
            strCookie += "; domain=" + this.$domain ;
        if ( this.$secure )
            strCookie += "; secure";

        return strCookie ;

    }

    //
    // Store the updated cookie in the document
    //
    _ObjCookie.prototype.store = function()
    {

        this.$document.cookie = this.generate() ;

    }

    //
    // print cookie
    //
    _ObjCookie.prototype.print = function()
    {

        alert('Cookie is ' + this.generate());

    }

}
