/*
 *  cookieHelper.js 
 *  
 *  @role: Cookie Object
 *  
 *  Copyright (c) 2001 - 2005, BCE Inc. This program contains proprietary and
 *  confidential information which is protected by copyright. All rights are
 *  reserved. No part of this program may be photocopied, reproduced or
 *  translated into another language, or disclosed to a third party without the
 *  prior written consent of BCE, Inc.
 *
 *  @author Robert Rascalon (CMTek Inc.)
 *
 *  @created by Rascalon Robert (CMTek Inc.)June 9th, 2005 (version 1.0)
 *  @updated by Bruno Da Silva (CMTek Inc.) June 22th, 2005 (version 1.1) 
 *
 */
 
/* - Functions ------------------------------------------------------------ */

/* 
 * Role: Returns the cookie value
 *
 * @param: offset
 *
 * @return: cookie value
 */
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    
    if(endstr == -1) {
        endstr = document.cookie.length;
    }
    
    return unescape(document.cookie.substring(offset, endstr));
}

/* 
 * Role: Returns the cookie
 *
 * @return: The cookie
 */
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;
} 

/* 
 * Role: Removes a given cookie
 *
 * @param: Cookie name
 * @param: Cookie domain
 * @param: Cookie path
 */
function DeleteCookie(name, domain, path) {
    if(GetCookie(name)) {
        document.cookie =   name + "=" +
                            ((domain) ? "; domain=" + domain :"") +
                            ((path) ? "; path=" + path : "") +
                            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/* 
 * Role: Creates a cookie
 *
 * @param: Cookie name
 * @param: Cookie value
 * @param: Cookie max age (in minutes)
 * @param: Cookie domain
 * @param: Cookie path
 */
function GenerateCookie(name, value, expires, domain, path) {
    var expiresTime = new Date ();   
             
    expiresTime.setTime(expiresTime.getTime() + (expires * 60 * 1000)); 

    document.cookie =   name + "=" + escape (value) +
                        "; expires=" + expiresTime.toGMTString()+
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +"";
} 

/* - Methods -------------------------------------------------------------- */

/* 
 * Role: Get the name for a cookie field
 *
 * @return: name for a cookie field
 */
function CookieField_getName() {
    return this.cookieName;
}

/* 
 * Role: Get the values for a cookie field
 *
 * @return: values for a cookie field
 */
function CookieField_getValues() {
    return this.cookieValues;
}

/* 
 * Role: Get the cookie parameter count
 *
 * @return: cookie parameter count
 */
 function CookieField_getCount() {
    if(this.cookieValues) {
        return this.cookieValues.length;
    }
    else {
        return 0;
    }
}

/* 
 * Role: Get the indexed cookie object
 *
 * @parameter: the index
 *
 * @return: the indexed cookie object
 */
function CookieField_get(index) {
    if(this.cookieValues) {
        if(index < this.cookieValues.length) {
            return this.cookieValue[index];
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
}

/* 
 * Role: Cookie Object Constructor
 *
 * @parameter: field name
 * @parameter: field values
 *
 * @return: cookie object instance
 */
function CookieField(cookieName, cookieValues) {

    this.cookieName                 =    cookieName;
    this.cookieValues               = cookieValues;
    
    CookieField.prototype.getName 	= CookieField_getName;
    CookieField.prototype.getValues	= CookieField_getValues;
    CookieField.prototype.getCount	= CookieField_getCount;
    CookieField.prototype.get		= CookieField_get;
}

/* 
 * Role: Returns the list of cookie fields
 *
 * @return: the cookie field list
 */
function DataCookie_getList() {
    return this.list;
}

/* 
 * Role: Get the list count
 *
 * @return: the list count
 */
function DataCookie_getCount() {
    if(this.list) {
        return this.list.length;
    }
    else {
        return 0;
    }
}

/* 
 * Role: Get the indexed list
 *
 * @return: the indexed list
 */
function DataCookie_get(index) {
    if(this.list) {
        if(index < this.list.length) {
            return this.list[index];
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
}

/* - Object/Constructor --------------------------------------------------- */

/* 
 * Role: Data Cookie Object Constructor
 *
 * @parameter: name for the cookie
 * @parameter: flag for decoding the cookie string
 *
 * @return: data cookie object instance
 */
function DataCookie(name, codecHandling) {

    this.name               = name;
    this.codecHandling		= codecHandling;
    this.valid          	= false;
    this.dataCookie         = "";
    this.originalCookieData	= "";
    this.list               = new Array();
    
    this.ENC_EQUAL          = "%3D";
    this.ENC_FIELD_SEP      = "%3A"; 
    this.RAY_SEP            = "%7C";
    this.ENC_ARRAY_OPEN     = "%7B";
    this.ENC_ARRAY_CLOSE    = "%7D";
    
    var cookieItems 		= document.cookie;
    
    var cookieList  		= cookieItems.split(";");
    var found               = false;
    var cookieLine          = "";
                        
    for(i = 0; i < cookieList.length; i++) {
        var iPos = parseInt(cookieList[i].indexOf(this.name + "="));

        if(iPos != -1) {   
            var start 		= iPos + this.name.length + 1;
            cookieLine  	= cookieList[i].substring(start, cookieList[i].length); 
            found		= true;
        }
    }
    
    if(found == true) {
        this.originalCookieData = new String(cookieLine);
        this.dataCookie         = new String(cookieLine);
        var chainWork           = new String(this.originalCookieData);
        
        if(this.codecHandling == true) {
            codec = new CodecHelper(chainWork);
            
            codec.decode64();
            
            this.datCookie = new String(codec.getString());
            
            codec = null;
        }
        
        this.valid = true;
    }
    
    if(this.valid == true) {
        if(this.datCookie.length <= 0) {
            this.valid = false;
        }
    }    
    
    if(this.valid == true) {
        
        var cookieArray = this.datCookie.split(this.ENC_FIELD_SEP);
        
        for(j = 0; j < cookieArray.length; j++) {
            var oneField    = cookieArray[j];                        
            var fieldArray  = oneField.split(this.ENC_EQUAL);
            var cFieldName  = fieldArray[0];
            var cFieldValue = fieldArray[1];
            var arrayVal    = new Array();
            
            if(cFieldValue.indexOf(this.ENC_ARRAY_OPEN) == -1) {
                arrayVal.push(cFieldValue);
            }
            else {
                var tValue = new String(cFieldValue);
                            
                tValue = tValue.substring(3, tValue.length);
                tValue = tValue.substring(0, tValue.length - 3);
                            
                arrayVal = tValue.split(this.RAY_SEP);
            }
            
            oneCookie = new CookieField(cFieldName, arrayVal);
            
            this.list.push(oneCookie);
        }
    }
    
    DataCookie.prototype.getList	= DataCookie_getList;
    DataCookie.prototype.getCount	= DataCookie_getCount;
    DataCookie.prototype.get		= DataCookie_get;
}
