﻿
// Create Singleton Instance of RealAge.Utils.Cookie Class (Only 1 Instance Allowed)
if(!RealAge.Utils.Cookie) RealAge.Utils.Cookie = new function()
{
    // Public Properties
    this.keyValuePairs = new Array();
    
    this.Initialize = function(qs)
    {
        // Traverse through cookie and extract value from key
        if(qs.length > 1) qs = qs.substring(1, qs.length);
        else qs = null;
        
        if(qs) 
        {
            for(var i=0; i < qs.split("&").length; i++) 
            {
                this.keyValuePairs[i] = qs.split("&")[i];
            }
        }
        
    }
    
    // Parse value from queryString //
    this.queryString = function(qsKey)
    {
        return unescape(this.getValue(qsKey)); 
    }

    this.getKeyValuePairs = function() { return this.keyValuePairs; }
        
    this.getValue = function(qsKey) 
    {
        
        for(var j=0; j < this.keyValuePairs.length; j++) {
            if(this.keyValuePairs[j].split("=")[0] == qsKey)
            return this.keyValuePairs[j].split("=")[1];
        }

        return '';
    }

    this.getParameters = function() 
    {
        var a = new Array(this.getLength());

        for(var j=0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }

        return a;
    }

    this.getLength = function() { return this.keyValuePairs.length; } 

    // Gets cookie value based on name //
    this.getCookie = function(c_name)
    {
        if (document.cookie.length>0)
        {
            var c_start=document.cookie.indexOf(c_name + "=");

            if (c_start!=-1)
            { 
                c_start=c_start + c_name.length+1; 
                var c_end=document.cookie.indexOf(";",c_start);

                if (c_end==-1) c_end=document.cookie.length;

                return unescape(document.cookie.substring(c_start,c_end));
            } 
        }
        return "";
    }

    // Gets memberId value from cookie //
    this.getMemberIdFromCookie = function(co)
    {

        var getCo = this.getCookie(co);
        
        if (getCo)
        {
            var memIdCo = getCo.split("&");
            var memIdSplit = memIdCo[0].split("=");
            var memId = memIdSplit[1];

            return memId;
        }
        
        return "";
    }
    
    this.Initialize(window.location.search);
}