/*
 *  codecHelper.js
 *  
 *  @role: 64 bits encoding/decoding 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 June 9th, 2005
 *
 *  @version 1.0
 */
 
/* - Methods -------------------------------------------------------------- */

/* 
 * Role: 64 bits decoding string function
 *
 * @return: the decoded string or an empty string
 */
function string_decode64() {
    var chr1      = "";
    var chr2      = "";
    var chr3      = "";
    var enc1      = "";
    var enc2      = "";
    var enc3      = "";
    var enc4      = "";
    var i         = 0;
    var input	  = new String(this.string);
    this.string	  = "";
    
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do {
        enc1 = this.keyStr.indexOf(input.charAt(i++));
        enc2 = this.keyStr.indexOf(input.charAt(i++));
        enc3 = this.keyStr.indexOf(input.charAt(i++));
        enc4 = this.keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        this.string = this.string + String.fromCharCode(chr1);

        if(enc3 != 64) {
            this.string = this.string + String.fromCharCode(chr2);
        }
        
        if (enc4 != 64) {
            this.string = this.string + String.fromCharCode(chr3);
        }

        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } 
    while (i < input.length);

    this.status = "decoded";
}

/* 
 * Role: 64 bits encoding string function
 *
 * @return: the encoded string or an empty string
 */
function string_encode64() {
    var chr1      = "";
    var chr2      = "";
    var chr3      = "";
    var enc1      = "";
    var enc2      = "";
    var enc3      = "";
    var enc4      = "";
    var i         = 0;
    var input	  = new String(this.string);
    this.string   = "";
    
    do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if(isNaN(chr2)) {
            enc3 = enc4 = 64;
        } 
        else if (isNaN(chr3)) {
            enc4 = 64;
        }

        this.string = 	this.string +    
                        this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) + 
                        this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4);
                                
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } 
    while (i < input.length);

    this.status = "encoded";
}

/* 
 * Role: 64 bits decoding string function
 *
 * @return: Return the codec status: none, encoded, decoded
 */
function string_getCodecStatus() {
    return this.status;
}

/* 
 * Role: Set the codec status to none
 *
 * @return: nothing
 */
function string_resetCodecStatus() {
    this.status = "none";
}

/* 
 * Role: Returns the encoded/decoded result.
 *
 * @return: the decoded string or an empty string
 */
function string_getString() {
    return this.string;
}

/* 
 * Role: Set the string to encode/decode
 *
 * @parameter: the string to encode/decode
 *
 * @return: nothing
 */
function string_setString(inString) {
    this.status = "none";
    this.string	= inString;
}

/* - Object/Constructor --------------------------------------------------- */

/* 
 * Constructor or the CodecHelper object
 *
 * @parameter: the string to work with
 *
 * @return: an instance of CodecHelper
 */
function CodecHelper(string) {

    this.string                         = string;
    this.status                         = "none";
    this.keyStr                         =  "ABCDEFGHIJKLMNOP" +
                                           "QRSTUVWXYZabcdef" +
                                           "ghijklmnopqrstuv" +
                                           "wxyz0123456789+/" +
                                           "=";
                                    
    CodecHelper.prototype.decode64      = string_decode64;
    CodecHelper.prototype.encode64      = string_encode64;
    CodecHelper.prototype.getStatus     = string_getCodecStatus;
    CodecHelper.prototype.resetStatus	= string_resetCodecStatus;
    CodecHelper.prototype.getString     = string_getString;
    CodecHelper.prototype.setString     = string_setString;
}
