/**
 *   ClientCache
 *        saves client side data that persists through page refreshes
 *
 *   These scripts need to be included:  (http://www.devpro.it/JSON/files/JSTONE-js.html)
 *
 *   <script type="text/javascript" src="Scripts/json2.js"></script>
 *   <script type="text/javascript" src="Scripts/JSTONE.js"></script>
 */

var ClientCache = new ClientCacheClass();

function ClientCacheClass()
{
    var ClientCache_internal;
    
    this.Init = m_Init;
    this.Save = m_Save;
    this.Retrieve = m_Retrieve;
    this.setClientCookie = setClientCookie;
    this.getClientCookieByName = getClientCookieByName;
    this.getClientCookie = getClientCookie;
    
    /**
     * Constructor - Internal initializer for client cache
     */
    function m_Init()
    {
        if (!ClientCache_internal && getQueryVariable("state") != "clear")
        {
            try
            {
                ClientCache_internal = new JSTONE(JSON);
//                ClientCache_internal = new JSTONE(
//                    {       
//                        encode:function(obj)
//                        {
//                            return JSON.stringify(obj);
//                        },
//                               
//                        decode:function(str)
//                        {
//                            return JSON.parse(str);
//                        }
//                    });
            }
            catch(err)
            {}
        }     
    }

    /**
     * Save key/value pair to cache
     *
     * @param key (string) 
     * @param value (string)
     */
    function m_Save(key, value)
    {
        this.Init();
        
        if (ClientCache_internal == undefined || ClientCache_internal == null)
        {
            return '';
        }
        ClientCache_internal.write(key, value);
    }

    /**
     * Retrieve key/value pair from cache
     *
     * @param key (string) 
     */
    function m_Retrieve(key)
    {
        this.Init();
        if (ClientCache_internal == undefined || ClientCache_internal == null)
        {
            return '';
        }
        return ClientCache_internal.read(key);
    }

    function setClientCookie(name, value, expiration, path, domain, secure) {
        var cookieParams = new Array();
        if (name != null && value != null)
            cookieParams.push(strip(name) + '=' + strip(value));
        if (expiration != null)
            cookieParams.push('expires=' + expiration);
        if (path != null)
            cookieParams.push('path=' + path);
        if (domain != null)
            cookieParams.push('domain=' + domain);
        if (secure != null && secure)
            cookieParams.push('secure');
        document.cookie = cookieParams.join('; ');
    }
    
    // strip all possible injection characters except for ';' and '|'
    function simpleStrip(val) {
        if (val != null && typeof(val) == 'string') {
            val = val.replace(/'/gi, "");
            val = val.replace(/#/gi, "");
            val = val.replace(/\$/gi, "");
            val = val.replace(/%/gi, "");
            val = val.replace(/\^/gi, "");
            val = val.replace(/!/gi, "");
            val = val.replace(/</gi, "");
            val = val.replace(/>/gi, "");
            val = val.replace(/~/gi, "");
            val = val.replace(/"/gi, "");
            val = val.replace(/\//gi, "");
            val = val.replace(/\*/gi, "");
        }
        return val;
    }

    // strip ';' and '|'
    function strip(val) {
        if (val != null && typeof (val) == 'string') {
            val = simpleStrip(val);
            //val = val.replace(/\|/gi, "");
            val = val.replace(/;/gi, "");
        }
        return val;
    }
    
    function getClientCookie() {
        return simpleStrip(document.cookie);
    }

    function getClientCookieByName(name) {

        /*var queries=new Array();
        var cookies=document.cookie.split(';');
        var cookieName='search'+type+'Cookie=';
        var contents=null;
        
        //pull out the search cookie data
        for(var i=0; i < cookies.length; i++)
        {
        var cookie=cookies[i];
        while(cookie.charAt(0) == ' ')
        {
        cookie=cookie.substring(1,cookie.length);
        }
            
        if(cookie.indexOf(cookieName) == 0)
        {
        contents=cookie.substring(cookieName.length,cookie.length);
        }
        }*/
        var cookies = document.cookie.split(';');
        var cookieName = name+'=';
        var contents = null;

        //pull out the search cookie data
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i];
            
            while (cookie.charAt(0) == ' ') {
                cookie = cookie.substring(1, cookie.length);
            }
            if (cookie.indexOf(cookieName) == 0) {
                contents = cookie.substring(cookieName.length, cookie.length);
            }
        }

        return simpleStrip(contents);
    }
}


