var CCookie = new _CCookie(null, null, null , null );

_CCookie.prototype.getInstance = _CCookie_getInstance;
_CCookie.prototype.getName = _CCookie_getName;
_CCookie.prototype.getValue = _CCookie_getValue;
_CCookie.prototype.getPath = _CCookie_getPath;
_CCookie.prototype.getDomain = _CCookie_getDomain;
_CCookie.prototype.setName = _CCookie_setName;
_CCookie.prototype.setValue = _CCookie_setValue;
_CCookie.prototype.setPath = _CCookie_setPath;
_CCookie.prototype.setDomain = _CCookie_setDomain;
_CCookie.prototype.save = _CCookie_save;
_CCookie.prototype.savePersistant = _CCookie_savePersistant;
_CCookie.prototype.toString = _CCookie_toString;
_CCookie.prototype.getCookie = _CCookie_getCookie;

function _CCookie(name, value, path , domainName)
{
    this.cookieName = name;
    this.cookieValue = value;
    this.cookiePath = path;
    this.domainName = domainName ;
    return this;
}

function _CCookie_getCookie(m_document, cookieName)
{
    var message = "CCookie.getCookie() : \n";
    if( !m_document )
    {
        message += "  m_document can not be null\n" ;
    }
    if( !cookieName )
    {
        message += "  cookieName can not be null\n" ;
    }
    if( !m_document || !cookieName )
    {
        alert( message );
        return null;
    }

    var cooks = m_document.cookie;
    if ( !cooks || cooks == "")
    {
        return null;
    }
    var start = cooks.indexOf(cookieName + "=");
    if (start == -1)
    {
        return null;
    }
    start = start + (cookieName + "=").length;
    var end = cooks.indexOf(';', start);
    if (end == -1)
    {
        end = cooks.length;
    }
    var cookieValue = cooks.substring(start, end);
    return new _CCookie(cookieName, cookieValue, "/" , m_document.cookiedomain);
}

function _CCookie_getInstance(name, value, path, domainName)
{
    if( name && value != null && path )
    {
        return new _CCookie(name, value, path , domainName);
    }
    var message = "CCookie._CCookie_getInstance() : \n";
    if( name == null )
    {
        message += "  Cookie-name can not be null\n" ;
    }
    if( value == null )
    {
        message += "  Cookie-value can not be null\n" ;
    }
    if( !path )
    {
        message += "  Cookie-path can not be null\n" ;
    }
    if( !domainName )
    {
        message += "  Cookie-Domain Name can not be null\n" ;
    }
    if( !name  || value == null || !path || !domainName )
    {
        alert( message );
    }
    return null;
}

function _CCookie_getName()
{
    return this.cookieName;
}

function _CCookie_getValue()
{
    return this.cookieValue;
}

function _CCookie_getPath()
{
    return this.cookiePath;
}

function _CCookie_getDomain()
{
    return this.domainName;
}

function _CCookie_setName(_cookieName)
{
    return this.cookieName = _cookieName;
}

function _CCookie_setValue(_cookieValue)
{
    return this.cookieValue = _cookieValue;
}

function _CCookie_setPath(_cookiePath)
{
    return this.cookiePath = _cookiePath;
}

function _CCookie_setDomain(_domainName)
{
    return this.domainName = _domainName;
}

function _CCookie_save()
{
    document.cookie = this.toString();
}

function _CCookie_savePersistant()
{
    var today = new Date();
    var expires = new Date();
    expires.setDate(today.getDate() + 30);
    //saving for 30 days
    document.cookie = this.toString() + ";expires=" + expires.toGMTString();;
}

function _CCookie_toString()
{
    var str = "";
    if( this.cookieName )
    {
        str = this.cookieName + '=';
    }
    if( this.cookieValue )
    {
        str += this.cookieValue;
    }
    if (this.cookiePath)
    {
        str += '; path=' + this.cookiePath;
    }


    this.domainName = window.cookiedomain ;

    if (this.domainName &&
            this.domainName.indexOf('.') != -1)
    {
        str += '; domain=' + this.domainName + ';';
    }

    return str;
}


/*-------------------CMultiNVPairCookie.js------------------------*/

var CMultiNVPairCookie = new _CMultiNVPairCookie(null);

_CMultiNVPairCookie.prototype.getInstance = _CMultiNVPairCookie_getInstance;
_CMultiNVPairCookie.prototype.serialize = _CMultiNVPairCookie_serialize;
_CMultiNVPairCookie.prototype.deserialize = _CMultiNVPairCookie_deserialize;
_CMultiNVPairCookie.prototype.getValue = _CMultiNVPairCookie_getValue;
_CMultiNVPairCookie.prototype.setValue = _CMultiNVPairCookie_setValue;
_CMultiNVPairCookie.prototype.removeValue = _CMultiNVPairCookie_removeValue;
_CMultiNVPairCookie.prototype.removeAll = _CMultiNVPairCookie_removeAll;
_CMultiNVPairCookie.prototype.writeCookie = _CMultiNVPairCookie_writeCookie;
_CMultiNVPairCookie.prototype.writePersistantCookie = _CMultiNVPairCookie_writePersistantCookie;
_CMultiNVPairCookie.prototype.encrypt = _CMultiNVPairCookie_encryptCookie;
_CMultiNVPairCookie.prototype.decrypt = _CMultiNVPairCookie_decryptCookie;
_CMultiNVPairCookie.prototype.size = _CMultiNVPairCookie_size;
_CMultiNVPairCookie.prototype.toString = _CMultiNVPairCookie_toString;

function _CMultiNVPairCookie(cookieName)
{
    this.m_nameValuePairs = null ;
    this.cookieName = cookieName ;
    
    //if cookie name is null, send it back.   
    if( cookieName == null )
    {
        return this;
    }

    var errorMessage = "_CMultiNVPairCookie() :";
    var isError = false ;
    if( typeof(CCookie) == "undefined" ){
        errorMessage += "\n  /default/javascript/core/CCookie.js";
        isError = true ;
    }
    if( typeof(Hashtable) == "undefined"
        || typeof(Vector) == "undefined"
        || typeof(CURLEncoder) == "undefined"
        || typeof(CURLDecoder) == "undefined"
        || typeof(CStringTokenizer) == "undefined" )
    {
        errorMessage += "\n  /default/javascript/util/common.js";
        isError = true ;
    }
    if(isError)
    {
        alert(errorMessage + "\nfiles are not included");
        return this;
    }

    
    this.m_nameValuePairs = Hashtable.getInstance();
    var cookie = CCookie.getCookie(window.document, cookieName) ;
    if( cookie == null )
    {
        return this;
    }
    this.deserialize( cookie.getValue() );
    return this;
}

function _CMultiNVPairCookie_getInstance(cookieName)
{
    if( cookieName == null )
    {
        alert( "CMultiNVPairCookie.CMultiNVPairCookie_getInstance() : \n"
            + "Cookie-name can not be null." );
        return null;
    }
    if(window.cookiesuffix) cookieName = cookieName + window.cookiesuffix ;
    return new _CMultiNVPairCookie( cookieName );
}

var _problemEncodings = Vector.getInstance(); 
_problemEncodings.addElement("op_");
_problemEncodings.addElement("zd_");
_problemEncodings.addElement("dk_");
_problemEncodings.addElement("pk_");
_problemEncodings.addElement("fo_");
_problemEncodings.addElement("dq_");
_problemEncodings.addElement("fu_");

function _CMultiNVPairCookie_deserialize(cookieValue)
{
    cookieValue = this.decrypt( cookieValue );
    var nvPairs = CStringTokenizer.getInstance(cookieValue, "!%NVP_SEP^!");

    while( nvPairs.hasMoreElements() )
    {
        var nvPair = nvPairs.nextElement();
        var nv = CStringTokenizer.getInstance(nvPair, "!%EQUALS^!");
        var n = null;
        var v = null;
        if( nv.hasMoreElements() )
        {
            n = nv.nextElement();
        }
        if( nv.hasMoreElements() )
        {
            v = nv.nextElement();
            for(var x=0;x<_problemEncodings.size();x++)
            {
                if(v.indexOf(_problemEncodings.elementAt(x)) != -1){
                    v = CUtils.replaceAll(v, "~"+_problemEncodings.elementAt(x)+"~", 
                        stringReverse(_problemEncodings.elementAt(x))) ;
                }

            }
        }
        this.m_nameValuePairs.put(n, v);
    }
    return cookieValue;
}

function stringReverse(str){
    var splitext = str.split("");
    var revertext = splitext.reverse();
    var reversed = revertext.join("");
    return reversed;
}

function _CMultiNVPairCookie_serialize()
{
    var e = this.m_nameValuePairs.keys();
    var cookieValue = "";
    while( e.hasMoreElements() )
    {
        var name = e.nextElement();
        var value = this.m_nameValuePairs.get(name);
        cookieValue += name + "!%EQUALS^!" + value;
        if( e.hasMoreElements() )
        {
            cookieValue += "!%NVP_SEP^!";
        }
    }
    return this.encrypt(cookieValue);
}

function _CMultiNVPairCookie_getValue(key)
{
    return this.m_nameValuePairs.get(key);
}

function _CMultiNVPairCookie_removeValue(key)
{
    this.m_nameValuePairs.remove(key);
}

function _CMultiNVPairCookie_removeAll()
{
    this.m_nameValuePairs = Hashtable.getInstance();
}

function _CMultiNVPairCookie_setValue(key, value)
{
    return this.m_nameValuePairs.put(key, value);
}


function _CMultiNVPairCookie_encryptCookie(value)
{
    if( value == null )
    {
        return null;
    }
    return encodeURIComponent(rot13Crypt(value));
}

function _CMultiNVPairCookie_decryptCookie(encodedValue)
{
    if( encodedValue ==  null )
    {
        return null;
    }
    return rot13Crypt( CURLDecoder.decode(encodedValue) );
}

function _CMultiNVPairCookie_size()
{
	return this.m_nameValuePairs.size();
}


function _CMultiNVPairCookie_writeCookie()
{
    var cookie = CCookie.getInstance( this.cookieName, this.serialize(), "/" , window.document.domain);
    if( cookie != null )
    {
        cookie.save();
    }
}

function _CMultiNVPairCookie_writePersistantCookie()
{
    var cookie = CCookie.getInstance(this.cookieName, this.serialize(), "/", window.document.domain);
    if(cookie != null)
    {
        //storing for 30 days
        cookie.savePersistant();
    }
}

function _CMultiNVPairCookie_toString()
{
    var cookie = CCookie.getInstance( this.cookieName, this.serialize(), "/" , window.document.domain);
    if( cookie != null )
    {
        return cookie.toString();
    }
    else
    {
        return "";
    }
}

var TRANSFORM_CHAR = new Array() ;
TRANSFORM_CHAR['a'] = 'n';
TRANSFORM_CHAR['b'] = 'o';
TRANSFORM_CHAR['c'] = 'p';
TRANSFORM_CHAR['d'] = 'q';
TRANSFORM_CHAR['e'] = 'r';
TRANSFORM_CHAR['f'] = 's';
TRANSFORM_CHAR['g'] = 't';
TRANSFORM_CHAR['h'] = 'u';
TRANSFORM_CHAR['i'] = 'v';
TRANSFORM_CHAR['j'] = 'w';
TRANSFORM_CHAR['k'] = 'x';
TRANSFORM_CHAR['l'] = 'y';
TRANSFORM_CHAR['m'] = 'z';
TRANSFORM_CHAR['n'] = 'a';
TRANSFORM_CHAR['o'] = 'b';
TRANSFORM_CHAR['p'] = 'c';
TRANSFORM_CHAR['q'] = 'd';
TRANSFORM_CHAR['r'] = 'e';
TRANSFORM_CHAR['s'] = 'f';
TRANSFORM_CHAR['t'] = 'g';
TRANSFORM_CHAR['u'] = 'h';
TRANSFORM_CHAR['v'] = 'i';
TRANSFORM_CHAR['w'] = 'j';
TRANSFORM_CHAR['x'] = 'k';
TRANSFORM_CHAR['y'] = 'l';
TRANSFORM_CHAR['z'] = 'm';

TRANSFORM_CHAR['A'] = 'N';
TRANSFORM_CHAR['B'] = 'O';
TRANSFORM_CHAR['C'] = 'P';
TRANSFORM_CHAR['D'] = 'Q';
TRANSFORM_CHAR['E'] = 'R';
TRANSFORM_CHAR['F'] = 'S';
TRANSFORM_CHAR['G'] = 'T';
TRANSFORM_CHAR['H'] = 'U';
TRANSFORM_CHAR['I'] = 'V';
TRANSFORM_CHAR['J'] = 'W';
TRANSFORM_CHAR['K'] = 'X';
TRANSFORM_CHAR['L'] = 'Y';
TRANSFORM_CHAR['M'] = 'Z';
TRANSFORM_CHAR['N'] = 'A';
TRANSFORM_CHAR['O'] = 'B';
TRANSFORM_CHAR['P'] = 'C';
TRANSFORM_CHAR['Q'] = 'D';
TRANSFORM_CHAR['R'] = 'E';
TRANSFORM_CHAR['S'] = 'F';
TRANSFORM_CHAR['T'] = 'G';
TRANSFORM_CHAR['U'] = 'H';
TRANSFORM_CHAR['V'] = 'I';
TRANSFORM_CHAR['W'] = 'J';
TRANSFORM_CHAR['X'] = 'K';
TRANSFORM_CHAR['Y'] = 'L';
TRANSFORM_CHAR['Z'] = 'M';

function rot13Crypt(value)
{
    var transFormed = "";
    for( var i=0; i<value.length; i++ )
    {
        var c = value.charAt(i);
        if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) )
        {
            c = TRANSFORM_CHAR[c];
        }
        transFormed += c;
    }
    return transFormed;
}

function replacePictureIDInCookie(cookieValue, pictureID)
{
     var INNER_SEPARATOR = "!%EQUALS^!";
     var PICTURE_ID = "PictureID";
     var regexp1 = /PictureID!%EQUALS\^!(\d*)/g;
     var result = cookieValue.replace(regexp1, PICTURE_ID + INNER_SEPARATOR + "24629292");
     return result;
}

function updatePictureIDInCookie()
{
	var cookie = CMultiNVPairCookie.getInstance("SF_SESSION_COOKIE");
    var gobackurl =  cookie.getValue("urltogoback");

	if(gobackurl)
	{
		pictId = getCurrentPictOID();
		 var regexp1 = /PictureID=(\d*)/g;
		 var result = gobackurl.replace(regexp1, "PictureID=" + pictId);
		 cookie.setValue("urltogoback" , result );
		 cookie.writeCookie() ;
    }
}
