
/*
  class: HttpUtils

  This is a utility class to handle cookie
*/
var HttpUtils = {
    /*
      Function: getCookie

      Return the cookie value if it exists

      Parameters:
       name - *String* The name of the cookie to retrieve

      Returns:
       *String* The value of the cookie
    */
    getCookie: function (name) {
        if (name && document.cookie.length > 0) {
            var kies = document.cookie.split(';');
            var re = new RegExp('' + name + '=([^;=]+)');
            for (var x=0; x<kies.length; x++) {
                var kie = re.exec(kies[x]);
                if (kie) {
                    return unescape(kie[1]);
                }
            }
        }
        return null;
    },
    
    /*
      Function: setCookie

      Create a new cookie

      Parameters:
       name - *String* The name of the cookie to create
       value - *String* The value of this cookie
       expiredays - *int* The expiration time of this cookie in days
    */
    setCookie: function(name, value, days) {
        var expires = "";
        if (days) {
            var expDate = new Date();
            expDate.setTime(expDate.getTime()+(days*24*60*60*1000));
            expires = "; expires="+expDate.toGMTString();
        }
        document.cookie = name+"="+escape(value)+expires+"; path=/";
    },

    /*
      Function: removeCookie

      Remove the cookie specified in parameters

      Parameters:
       name - *String* The name of the cookie to retrieve
    */
    removeCookie: function(name) {
        HttpUtils.setCookie(name, "", -1);
    },

    /*
      Function: getParameter

      Get the parameter value of the speficied name

      Parameters:
       paramName - *String* The name of the get parameter

      Returns:
       *String* The value of the parameter
    */
    getParameter: function(paramName) {
        var loc = window.location.search;

        if (loc != "") {
            var queryString = loc.substring(1);
            var queryParams = queryString.split("&");

            for (var i=0; i<queryParams.length; i++) {
                var tmpParam = queryParams[i].split("=");
                var tmpName = tmpParam[0];
                var tmpValue = tmpParam[1];

                if (tmpName == paramName) {
                    return tmpValue;
                }
            }
        }
        return null;
    }
}