/** * copyright (c) 2005 - 2010, james auldridge * all rights reserved. * * licensed under the bsd, mit, and gpl (your choice!) licenses: * http://code.google.com/p/cookies/wiki/license * */ var jaaulde = window.jaaulde || {}; jaaulde.utils = jaaulde.utils || {}; jaaulde.utils.cookies = (function () { var resolveoptions, assembleoptionsstring, parsecookies, constructor, defaultoptions = { expiresat: null, path: '/', domain: null, secure: false }; resolveoptions = function (options) { var returnvalue, expiredate; if (typeof options !== 'object' || options === null) { returnvalue = defaultoptions; } else { returnvalue = { expiresat: defaultoptions.expiresat, path: defaultoptions.path, domain: defaultoptions.domain, secure: defaultoptions.secure }; if (typeof options.expiresat === 'object' && options.expiresat instanceof date) { returnvalue.expiresat = options.expiresat; } else if (typeof options.hourstolive === 'number' && options.hourstolive !== 0) { expiredate = new date(); expiredate.settime(expiredate.gettime() + (options.hourstolive * 60 * 60 * 1000)); returnvalue.expiresat = expiredate; } if (typeof options.path === 'string' && options.path !== '') { returnvalue.path = options.path; } if (typeof options.domain === 'string' && options.domain !== '') { returnvalue.domain = options.domain; } if (options.secure === true) { returnvalue.secure = options.secure; } } return returnvalue; }; assembleoptionsstring = function (options) { options = resolveoptions(options); return ((typeof options.expiresat === 'object' && options.expiresat instanceof date ? '; expires=' + options.expiresat.togmtstring() : '') + '; path=' + options.path + (typeof options.domain === 'string' ? '; domain=' + options.domain : '') + (options.secure === true ? '; secure' : '')); }; parsecookies = function () { var cookies = {}, i, pair, name, value, separated = document.cookie.split(';'), unparsedvalue; for (i = 0; i < separated.length; i = i + 1) { pair = separated[i].split('='); name = pair[0].replace(/^\s*/, '').replace(/\s*$/, ''); try { value = decodeuricomponent(pair[1]); } catch (e1) { value = pair[1]; } if (typeof json === 'object' && json !== null && typeof json.parse === 'function') { try { unparsedvalue = value; value = json.parse(value); } catch (e2) { value = unparsedvalue; } } cookies[name] = value; } return cookies; }; constructor = function () {}; constructor.prototype.get = function (cookiename) { var returnvalue, item, cookies = parsecookies(); if (typeof cookiename === 'string') { returnvalue = (typeof cookies[cookiename] !== 'undefined') ? cookies[cookiename] : null; } else if (typeof cookiename === 'object' && cookiename !== null) { returnvalue = {}; for (item in cookiename) { if (typeof cookies[cookiename[item]] !== 'undefined') { returnvalue[cookiename[item]] = cookies[cookiename[item]]; } else { returnvalue[cookiename[item]] = null; } } } else { returnvalue = cookies; } if(returnvalue=="[]" || returnvalue=="undefined" || returnvalue=='""'){ returnvalue=null; } return returnvalue; }; constructor.prototype.filter = function (cookienameregexp) { var cookiename, returnvalue = {}, cookies = parsecookies(); if (typeof cookienameregexp === 'string') { cookienameregexp = new regexp(cookienameregexp); } for (cookiename in cookies) { if (cookiename.match(cookienameregexp)) { returnvalue[cookiename] = cookies[cookiename]; } } return returnvalue; }; constructor.prototype.set = function (cookiename, value, options) { if (typeof options !== 'object' || options === null) { options = {}; } if (typeof value === 'undefined' || value === null) { value = ''; options.hourstolive = -8760; } else if (typeof value !== 'string') { if (typeof json === 'object' && json !== null && typeof json.stringify === 'function') { value = json.stringify(value); //alert(value) } else { throw new error('cookies.set() received non-string value and could not serialize.'); } } var optionsstring = assembleoptionsstring(options); document.cookie = cookiename + '=' + encodeuricomponent(value) + optionsstring; }; constructor.prototype.del = function (cookiename, options) { var allcookies = {}, name; if (typeof options !== 'object' || options === null) { options = {}; } if (typeof cookiename === 'boolean' && cookiename === true) { allcookies = this.get(); } else if (typeof cookiename === 'string') { allcookies[cookiename] = true; } for (name in allcookies) { if (typeof name === 'string' && name !== '') { this.set(name, null, options); } } }; constructor.prototype.test = function () { var returnvalue = false, testname = 'ct', testvalue = 'data'; this.set(testname, testvalue); if (this.get(testname) === testvalue) { this.del(testname); returnvalue = true; } return returnvalue; }; constructor.prototype.setoptions = function (options) { if (typeof options !== 'object') { options = null; } defaultoptions = resolveoptions(options); }; return new constructor(); })(); (function () { if (window.jquery) { (function ($) { $.cookies = jaaulde.utils.cookies; var extensions = { cookify: function (options) { return this.each(function () { var i, nameattrs = ['name', 'id'], name, $this = $(this), value; for (i in nameattrs) { if (!isnan(i)) { name = $this.attr(nameattrs[i]); if (typeof name === 'string' && name !== '') { if ($this.is(':checkbox, :radio')) { if ($this.attr('checked')) { value = $this.val(); } } else if ($this.is(':input')) { value = $this.val(); } else { value = $this.html(); } if (typeof value !== 'string' || value === '') { value = null; } $.cookies.set(name, value, options); break; } } } }); }, cookiefill: function () { return this.each(function () { var n, getn, nameattrs = ['name', 'id'], name, $this = $(this), value; getn = function () { n = nameattrs.pop(); return !!n; }; while (getn()) { name = $this.attr(n); if (typeof name === 'string' && name !== '') { value = $.cookies.get(name); if (value !== null) { if ($this.is(':checkbox, :radio')) { if ($this.val() === value) { $this.attr('checked', 'checked'); } else { $this.removeattr('checked'); } } else if ($this.is(':input')) { $this.val(value); } else { $this.html(value); } } break; } } }); }, cookiebind: function (options) { return this.each(function () { var $this = $(this); $this.cookiefill().change(function () { $this.cookify(options); }); }); } }; $.each(extensions, function (i) { $.fn[i] = this; }); })(window.jquery); } })();