﻿var Mackolik = Mackolik || {};
Mackolik.CookieManager =
    function(cookie_name, cookie_expire, cookie_path, cookie_secure, cookie_seperator) {
        var domain = document.domain;
        firstDotIndex = domain.indexOf(".");
        if (domain.substring(0, firstDotIndex).indexOf("www") >= 0) {
            domain = domain.substring(firstDotIndex);
        }
        this.cookie_domain = domain;
        this.cookie_name = cookie_name;
        this.cookie_expire = cookie_expire;
        this.cookie_path = cookie_path;
        this.cookie_secure = cookie_secure;
        this.cookie_seperator = cookie_seperator;
    };
Mackolik.CookieManager.prototype = {
    delete_Cookie: function() {
        var name = this.cookie_name;
        var path = this.cookie_path;
        var domain = this.cookie_domain;
        if (this.get_Cookie()) {
            document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
        }
    },
    set_Cookie: function(value) {
        var name = this.cookie_name;
        var expires = this.cookie_expire;
        var path = this.cookie_path;
        var secure = this.cookie_secure;
        var domain = this.cookie_domain;

        var today = new Date();
        /*
        firstDotIndex = domain.indexOf(".");
        if (domain.substring(0, firstDotIndex).indexOf("www") >= 0) {
        domain = domain.substring(firstDotIndex);
        }*/
        today.setTime(today.getTime());

        if (expires) {
            expires = expires * 1000 * 60 * 60 * 24;
        }

        var expires_date = new Date(today.getTime() + expires);
        value = escape(value);

        document.cookie = name + "=" + value +
            (expires ? ";expires=" + expires_date.toGMTString() : "") +
            (path ? ";path=" + path : "") +
            (domain ? ";domain=" + domain : "") +
            (secure ? ";secure" : "");
    },
    add_Cookie: function(value) {
        var name = this.cookie_name;
        var expires = this.cookie_expire;
        var path = this.cookie_path;
        var secure = this.cookie_secure;
        var domain = this.cookie_domain;

        var today = new Date();
        /*
        firstDotIndex = domain.indexOf(".");
        if (domain.substring(0, firstDotIndex).indexOf("www") >= 0) {
        domain = domain.substring(firstDotIndex);
        }*/
        today.setTime(today.getTime());

        if (expires) {
            expires = expires * 1000 * 60 * 60 * 24;
        }

        var expires_date = new Date(today.getTime() + expires);
        var oldCookie = this.get_Cookie(name);

        if (null != oldCookie && oldCookie.length > 0) {
            while (oldCookie.length > 3200) {
                var firstCookieEndIndex = oldCookie.indexOf(this.cookie_seperator);
                oldCookie = oldCookie.substring(firstCookieEndIndex + 1);
            }
            value = escape(oldCookie + this.cookie_seperator + value);
        } else {
            value = escape(value);
        }

        document.cookie = name + "=" + value +
            (expires ? ";expires=" + expires_date.toGMTString() : "") +
            (path ? ";path=" + path : "") +
            (domain ? ";domain=" + domain : "") +
            (secure ? ";secure" : "");
    },
    get_Cookie: function() {
        var check_name = this.cookie_name;
        var a_all_cookies = document.cookie.split(';');
        var a_temp_cookie = '';
        var cookie_name = '';
        var cookie_value = '';
        var b_cookie_found = false;

        for (i = 0; i < a_all_cookies.length; i++) {
            a_temp_cookie = a_all_cookies[i].split("=");
            cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
            if (cookie_name == check_name) {
                b_cookie_found = true;
                if (a_temp_cookie && a_temp_cookie.length > 1) {
                    cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
                }
                return cookie_value;
                break;
            }
            a_temp_cookie = null;
            cookie_name = '';
        }
        if (!b_cookie_found) {
            return null;
        }
    }
    /*
    setCookie : function(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) + 
    ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    ((secure == null) ? "" : "; secure");
    },
    getCookie : function(name) {
    var cname = name + "=";
    var dc = document.cookie;
    	
    if (dc.length > 0) {
    begin = dc.indexOf(cname);
    if (begin != -1) {
    begin += cname.length;
    end = dc.indexOf(";", begin);
    if (end == -1) end = dc.length;
    return unescape(dc.substring(begin, end));
    }
    }
    return null;
    }*/
};

