/*
A library that eases the pain of using cookies.

Based on a Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)
Original Author: Nadav Savio
*/

if (typeof DSala == 'undefined') {
    var DSala = new Object();
}

DSala.Cookies = { 

    /**
     * tests if cookies are accepted
     */
    test : function () {
        var acceptsCookies = false;
        if(document.cookie == '') {
            document.cookie = 'DSala.Cookies.acceptsCookies=yes';
            if(document.cookie.indexOf('DSala.Cookies.acceptsCookies=yes') != -1) {
                acceptsCookies = true; 
            }
        } else {
          acceptsCookies = true;
        }
        return acceptsCookies;
    },


    set : function (name, value, hours, path, domain, secure) {

        if (hours) {
            if ( (typeof(hours) == 'string') && Date.parse(hours) ) {
            var numHours = hours;
            } else if (typeof(hours) == 'number') {
            var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
            }
        }
        document.cookie = name + '=' + escape(value) +
            ((numHours)?(';expires=' + numHours):'') +
            ((path)?';path=' + path:'') +
            ((domain)?';domain=' + domain:'') +
            ((secure && (secure == true))?'; secure':'');
    },


    get : function (name) {
        if(document.cookie == '') {
            return undefined; 
        } else {
            var firstChar, lastChar;
            var theBigCookie = document.cookie;
            firstChar = theBigCookie.indexOf(name);
            var NN2Hack = firstChar + name.length;
            if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) {
                firstChar += name.length + 1;
                lastChar = theBigCookie.indexOf(';', firstChar);
                if(lastChar == -1) lastChar = theBigCookie.length;
                return unescape(theBigCookie.substring(firstChar, lastChar));
            } else {
                return undefined;
            }
        }	
    },

    remove : function (name, path, domain) {
        var theValue = DSala.Cookies.get(name);
        if (theValue) {
            document.cookie = name + '=' + theValue +
                '; expires=Fri, 13-Apr-1970 00:00:00 GMT' +
                ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
        }
    }

};

