home *** CD-ROM | disk | FTP | other *** search
/ CyberMycha 2003 October / cmycha200310.iso / NHL2004 / NHL2004Demo.exe / fe / COMMON / js / JAX_Cookie.js < prev    next >
Text File  |  2003-08-20  |  2KB  |  74 lines

  1. function JAX_Cookie(document, name, hours)
  2. {
  3.     var     protect            = this.createProtection(JAX_Object);
  4.         protect.document     = document;
  5.         protect.name         = name;
  6.         if (hours) {
  7.             protect.expiration = new Date((new Date()).getTime() + hours*3600000);
  8.         } else {
  9.             protect.expiration = null;
  10.         };
  11. };
  12. JAX_Cookie.prototype    = new JAX_Object();
  13. JAX_Cookie.prototype.save = function () 
  14. {
  15.     var protect        = this.getProtection(JAX_Cookie);
  16.     var cookieval         = "";
  17.     var prop            = null;
  18.     for(prop in this) 
  19.     {
  20.         if ((typeof this[prop] == 'object') || ((typeof this[prop]) == 'function')) continue;
  21.         if (cookieval != "") cookieval += '&';
  22.         cookieval += prop + ':' + escape(this[prop]);
  23.     };
  24.         var cookie = protect.name + '=' + cookieval;
  25.         if (protect.expiration) { cookie += '; expires=' + protect.expiration.toGMTString(); };
  26.         protect.document.cookie = cookie;
  27. };
  28. JAX_Cookie.prototype.load = function() 
  29.     var protect    = this.getProtection(JAX_Cookie);
  30.     var allcookies = protect.document.cookie;
  31.     if (allcookies == "") return false;
  32.     var start = allcookies.indexOf(protect.name + '=');
  33.     if (start == -1) return false;
  34.     start += protect.name.length + 1;
  35.     var end = allcookies.indexOf(';', start);
  36.     if (end == -1) end = allcookies.length;
  37.     var cookieval = allcookies.substring(start, end);
  38.    var a = cookieval.split('&');  
  39.     for(var i=0; i < a.length; i++) { a[i] = a[i].split(':'); };
  40.     for(var i = 0; i < a.length; i++) { this[a[i][0]] = unescape(a[i][1]); };
  41.     return true;    
  42. }
  43. JAX_Cookie.prototype.destroy = function() 
  44. {
  45.     var protect        = this.getProtection(JAX_Cookie);
  46.         var cookie;
  47.         cookie = protect.name + '=';
  48.         cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
  49.     protect.document.cookie = cookie;
  50. };
  51. JAX_Cookie.prototype.getCookieString    = function()
  52. {
  53.     var protect                    = this.getProtection(JAX_Cookie);
  54.     return unescape(protect.document.cookie);
  55. };
  56. JAX_Cookie.prototype.addItem            = function(propName, propValue, saveNow)
  57. {
  58.     if ( (typeof propValue != 'function') && (typeof propValue != 'object') ) {
  59.         this[propName] = propValue;
  60.         if (saveNow) {
  61.             this.save();
  62.         };
  63.     };
  64. };
  65. JAX_Cookie.prototype.deleteItem        = function(propName, saveNow)
  66. {
  67.     if (this[propName]) { 
  68.         delete this[propName]; 
  69.         if (saveNow) {
  70.             this.save();
  71.         };
  72.     };
  73. };