var B2CUKINT = {
    /**
    * This object provides all the methods to manage cookies
    */
    COOKIE : {
        /**
        * Returns the domain based on the current location. Taking the specified number of parts
        * parts: number of parts to be extracted from the domain. This field can be empty.
        *
        * Ussage: B2CUKINT.COOKIE.getDomain(); // This one is the same as getDomain(3);
        *
        * author: Nestor Alvaro
        */
        getDomain: function (parts) {
            var currlocation = document.domain;
            var arrlocation = currlocation.split(".");
            var domain = "";
            // By default we use the last 3 parts
            if (!parts) {
                domain = arrlocation[arrlocation.length - 3] + "." + arrlocation[arrlocation.length - 2] + "." + arrlocation[arrlocation.length - 1];
            } else {
                for (;parts > 0; parts --) {
                    domain = domain + arrlocation[arrlocation.length - parts] + ".";
                }
                // Remove the last "."
                domain = domain.substring(0, domain.length-1)
            }
            return domain;
        },
        /**
        * Creates a cookie receiving the name, value, expiration date and the domain parts to be taken
        * name: name of the cookie
        * value: value that the cookie will have
        * days: Number of days that this cookie will be valid. By default it's set until the browser is closed. This field can be empty.
        * domainParts: Domain where the cookie is valid. This field can be empty.
        *
        * Ussage: B2CUKINT.COOKIE.createCookie('testCookie', 'example', '30', 3);
        *
        * author: Nestor Alvaro + http://www.quirksmode.org/js/cookies.html
        */
        createCookieForDomain: function (name, value, days, domainParts) {  
            if (!name) {
                B2CUKINT.COMMON.showMessage('error', 'NAME can NOT be empty');
                return false;
            }
            if (!value) {
                B2CUKINT.COMMON.showMessage('warn', 'VALUE is empty');
            }
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                expires = "; expires="+date.toGMTString();
            }
            document.cookie = name + "=" + value + expires + "; path=/;";
        },
        /**
        * Creates a cookie receiving the name, value and expiratio date
        * name: name of the cookie
        * value: value that the cookie will have
        * days: Number of days that this cookie will be valid
        *
        * Ussage: B2CUKINT.COOKIE.createCookie('testCookie', 'example', '30');
        *
        * author: Nestor Alvaro + http://www.quirksmode.org/js/cookies.html
        */
        createCookie: function (name, value, days) {
        
            if (!name) {
                B2CUKINT.COMMON.showMessage('error', 'NAME can NOT be empty');
                return false;
            }
            if (!value) {
                B2CUKINT.COMMON.showMessage('warn', 'VALUE is empty');
            }
            this.createCookieForDomain(name, value, days, 3);
        },
        /**
        * Reads the value of a cookie
        * name: name of the cookie that is going to be read
        *
        * Ussage: B2CUKINT.COOKIE.readCookie('testCookie');
        *
        * author: http://www.quirksmode.org/js/cookies.html
        */
        readCookie: function (name) {
            if (!name) {
                B2CUKINT.COMMON.showMessage('error', 'NAME can NOT be empty');
                return false;
            }
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ')
                    c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) === 0) {
                    return c.substring(nameEQ.length,c.length);
                }
            }
            return null;
        },
        /**
        * Reads all Cookies
        *
        * Ussage: B2CUKINT.COOKIE.readAllCookies();
        *
        * author: Nestor Alvaro
        */
        readAllCookies : function() {
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                B2CUKINT.COMMON.showMessage('log', "cookie [" + i + "]: " + c);
            }
        },
        /**
        * Erases a cookie receiving the name and the domain parts to be taken
        * name: name of the cookie
        * domainParts: Domain where the cookie is valid. This field can be empty.
        *
        * Ussage: B2CUKINT.COOKIE.eraseCookieForDomain('testCookie', 3);
        *
        * author: Nestor Alvaro
        */
        eraseCookieForDomain: function (name, domainParts) {
            if (!name) {
                B2CUKINT.COMMON.showMessage('error', 'Name can NOT be empty');
                return false;
            }
            this.createCookieForDomain(name, "", -1, domainParts)
        },
        /**
        * Erases a cookie
        * name: name of the cookie that is going to be erased
        *
        * Ussage: B2CUKINT.COOKIE.eraseCookie('testCookie');
        *
        * author: http://www.quirksmode.org/js/cookies.html
        */
        eraseCookie: function (name) {
            if (!name) {
                B2CUKINT.COMMON.showMessage('error', 'Name can NOT be empty');
                return false;
            }
            this.eraseCookieForDomain(name);
        },
        /**
        * Deletes all Cookies
        * domainParts: Domain where the cookie is valid. This field can be empty.
        *
        * Ussage: B2CUKINT.COOKIE.eraseAllCookiesForDomain(3);
        *
        * author: Nestor Alvaro
        */
        eraseAllCookiesForDomain: function(domainParts) {
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                // Key for the cookie
                var cookieKey = c.substring(0, c.indexOf("="));
                // Value of the cookie for the key above
                var cookieVal = c.substring(c.indexOf("=") + 1);
                this.eraseCookieForDomain(cookieKey, domainParts);
            }
        }
    }, // \COOKIE
    /**
    * This object provides all the common methods for the framework
    */
    COMMON : {
        /**
        * Performs a custom redirection or writes the url if the user is logged in
        * url: url where the user has to be redirected.
        * user: user that is logged in. This field can be empty.
        * time: millisecs until the redirection is performed (500 millis if no time is indicated). This field can be empty.
        *
        * Ussage: B2CUKINT.COMMON.doRedirect('http://www.google.com', 'test', 1500);
        *
        * author: Nestor Alvaro
        */
        doRedirect : function(url, user, time){
            if (!url) {
                B2CUKINT.COMMON.showMessage('error', 'URL can NOT be empty');
                return false;
            }
            if (user && user == 'test@liferay.com') {
                document.write('<a data-ajax=false href="' + url +'">' + url + '</a>');
            } else {
                setTimeout('window.location="' + url+'"', time || 500);
            }
        },
        /**
        * Enables the variable console
        *
        * Ussage: B2CUKINT.COMMON.enableConsole();
        *
        * author: Nestor Alvaro
        */
        enableConsole : function() {
            showConsole = true;
        },
        /**
        * Disables the variable console.
        * IMPORTANT: This method is executed by default disabling the console.
        *
        * Ussage: B2CUKINT.COMMON.disableConsole();
        *
        * author: Nestor Alvaro
        */        


        disableConsole : function() {
            showConsole = false;
        }(),
        /**
        * Shows messages if the console is enabled
        * level: console level for the message
        * message: the message to be outputted
        *
        * Ussage: B2CUKINT.COMMON.showMessage('warn', 'warning message');
        *
        * author: Nestor Alvaro
        */
        showMessage : function(level, message){
            if (showConsole) {
                switch(level) {
                    case 'error':
                        console.error(message);
                        break;
                    case 'warn':
                        console.warn(message);
                        break;
                    case 'log':
                        console.log(message);
                        break;
                    case 'alert':
                        alert(message);
                        break;
                }
            }
        },
        /**
        * Gets the value for a given key on the queryString
        * key: name of the key to search
        * default_: the default value in case the searched value is not found
        *
        * Ussage: B2CUKINT.COMMON.getQuerystringValue('keyToGet', 'defaultValue');
        *
        * author: http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx
        */        
        getQuerystringValue : function (key, default_){
          if (default_==null) default_="";
          key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
          var qs = regex.exec(window.location.href);
          if(qs == null)
            return default_;
          else
            return qs[1];
        }
    }// \COMMON
}

