/**
 * Copyright (c) 2008, Libox. All rights reserved.
 */
var Libox = {};

Libox.RequestArguments = new function(){
    // Parse the URL and retrieve the fields passed via GET method.
    var arguments = {};
    var myURL = "" + document.location;
    var i = myURL.indexOf("?");
    if (i > 0) {
        var tmp = myURL.substring(i + 1);
        arguments.argsString = tmp;
        var argsArray = tmp.split("&");
        for (var i = 0; i < argsArray.length; i++) {
            var s = argsArray[i];
            var t = s.split("=");
            if (t.length == 2) {
                arguments[t[0]] = unescape(t[1]);
            }
        }
    }
    return arguments;
}

Libox.UsingNamespace = function(ns){
    var names = ns.split(".");
    var scope = this;
    for (var i = 0; i < names.length; i++) {
        var name = names[i];
        if ((i == 0) && (name == "Libox")) // skip Libox
            continue;
        if (!scope[name]) 
            scope[name] = {};
        scope = scope[name];
    }
    return scope;
}

Libox.CookieManager = new function(){
    var path = "/";
    var domain = null;
    var secure = null;
    
    this.GetCookie = function(name){
        var result = null;
		for(var i = 0; i < document.cookie.split('; ').length; i++)
        {
            var crumb = document.cookie.split('; ')[i].split('=');
            if (crumb[0] == name && crumb[1] != null)
            {
                result = crumb[1];
                break;
            }
        }
        return  (result)? unescape(result) : null;
    }
    
    this.SetCookie = function(name, value, shelfLife){
        if ((name == null) || (name == "") || (value == null)) 
            return;
        var today = new Date();
        today.setTime(today.getTime());
        if (shelfLife) {
            shelfLife = shelfLife * 1000 * 60 * 60 * 24;
        }
        var expires_date = new Date(today.getTime() + (shelfLife));
       
        document.cookie = name + '=' + escape(value) +
        ((shelfLife) ? ';expires=' + expires_date.toGMTString() : '') +
        ((path) ? ';path=' + path : '') +
        ((domain) ? ';domain=' + domain : '') +
        ((secure) ? ';secure' : '');
    }
    
    this.SetSessionCookie = function(name, value){
       document.cookie = escape(name) + "=" + escape(value) + "; path=/";
    }
    
    this.DeleteCookie = function(name){
        if (this.getCookie(name)) 
            document.cookie = name + '= ' + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    }
    
    this.ResetCookies = function(){
        var cookies = document.cookie.split("; ");
        for (var i = 0; i < cookies.length; i++) {
            var tmp = cookies[i].split("=");
            if (tmp.length > 0) 
                this.deleteCookie(tmp[0]);
        }
    }
}
