1 // The cookie name to use for storing the blog-side comment session cookie.
2 var mtCookieName = "mt_blog_user";
3 var mtCookieDomain = ".defaria.com";
4 var mtCookiePath = "/";
5 var mtCookieTimeout = 14400;
9 var el = (typeof id == "string") ? document.getElementById(id) : id;
10 if (el) el.style.display = 'none';
15 var el = (typeof id == "string") ? document.getElementById(id) : id;
16 if (el) el.style.display = 'block';
20 function mtAttachEvent(eventName,func) {
21 var onEventName = 'on' + eventName;
22 var old = window[onEventName];
23 if( typeof old != 'function' )
24 window[onEventName] = func;
26 window[onEventName] = function( evt ) {
34 function mtFireEvent(eventName,param) {
35 var fn = window['on' + eventName];
36 if (typeof fn == 'function') return fn(param);
40 if(!this.JSON){JSON={};}(function(){function f(n){return n<10?'0'+n:n;}if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return this.getUTCFullYear()+'-'+f(this.getUTCMonth()+1)+'-'+f(this.getUTCDate())+'T'+f(this.getUTCHours())+':'+f(this.getUTCMinutes())+':'+f(this.getUTCSeconds())+'Z';};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);}if(typeof rep==='function'){value=rep.call(holder,key,value);}switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}v=partial.length===0?'[]':gap?'[\n'+gap+partial.join(',\n'+gap)+'\n'+mind+']':'['+partial.join(',')+']';gap=mind;return v;}if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==='string'){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' ';}}else if(typeof space==='string'){indent=space;}rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}return str('',{'':value});};}if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}return reviver.call(holder,key,value);}cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof reviver==='function'?walk({'':j},''):j;}throw new SyntaxError('JSON.parse');};}}());
42 var MT = window.MT || {};
44 MT.cons = function () {
56 MT.core = function (o) {
61 connect : function (url,respType,respHandler) {
62 var xh = mtGetXmlHttp();
63 if (!xh) return false;
65 xh.onreadystatechange = function() {
66 if ( xh.readyState == 4 ) {
67 if ( xh.status && ( xh.status != 200 ) ) {
72 respHandler(JSON.parse(xh.responseText));
89 getEl : function (el) {
90 return MT.util.checkNodeType(el)==='element' ? id : (document.getElementById(el) || false);
93 addEvent : function (el,type,func,obj) {
94 if(!obj && document.addEventListener) {
95 el.addEventListener(type,func,false);
96 } else if(obj && document.addEventListener) {
97 el.addEventListener(type,function () {
102 el.attachEvent('on' + type,function () {
103 func.call(obj,event);
106 el.attachEvent('on' + type,function () {
114 log : function (level,msg) {
115 if(_debug && window.console) {
136 MT.util = function () {
140 var i = arguments.length;
143 if(MT.util.checkNodeType(arguments[i])==='element') {
144 arguments[i].style.visibility = 'visible';
146 MT.core.getEl(arguments[i]).style.visibility = 'visible';
152 var i = arguments.length;
154 if(MT.util.checkNodeType(arguments[i])==='element') {
155 arguments[i].style.visibility = 'hidden';
157 MT.core.getEl(arguments[i]).style.visibility = 'hidden';
165 var i = arguments.length;
167 if(MT.util.checkNodeType(arguments[i])==='element') {
168 arguments[i].style.display = '';
170 MT.core.getEl(arguments[i]).style.display = '';
176 var i = arguments.length;
178 if(MT.util.checkNodeType(arguments[i])==='element') {
179 arguments[i].style.display = 'none';
181 MT.core.getEl(arguments[i]).style.display = 'none';
188 findDefiningParent : function (origin) {
189 if(MT.util.checkNodeType(origin)==='element') {
190 for(var node=origin.parentNode;node.parentNode;node=node.parentNode) {
191 if((node.hasAttribute && node.hasAttribute('id')) || node.getAttribute('id')) {
200 checkNodeType : function (obj) {
201 if (obj && obj.nodeName){
202 switch (obj.nodeType) {
203 case 1: return 'element';
204 case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
233 topCurrentCommentsSpan;
235 M.addEvent(window,'load',_init);
238 * Initializes the class
243 if(!MT.entryCommentCount) {
247 _initializeVariables();
248 _setCommentOffset(false);
250 _setCurrentComments();
256 function _initializeVariables() {
261 commentsPerPage = MT.commentsPerPage || 50;
262 currentComments = '';
263 direction = 'ascend';
264 entryID = MT.entryID;
265 totalComments = MT.entryCommentCount;
266 commentsTotalPages = Math.ceil(totalComments / commentsPerPage);
269 loadingIcon = "<img title='Loading...' src='http://defaria.com/mt/mt-static/images/indicator.white.gif' alt='Loading' />";
271 commentContentDiv = M.getEl("comments-content");
272 topNav = M.getEl("top-comment-nav");
273 nav = M.getEl("comment-nav");
275 currentCommentsSpan = M.getEl("current-comments");
276 topCurrentCommentsSpan = M.getEl("top-current-comments");
279 function _initializeEvents() {
280 if (commentsPerPage < totalComments) {
281 M.addEvent(nav,'click',_handleEvents);
282 M.addEvent(topNav,'click',_handleEvents);
287 function _checkForAnchor() {
288 var found = String(window.location.hash).match( /comment-(\d{1,6})/ );
291 M.log(c.DEBUG,found);
292 if (!Object.prototype.hasOwnProperty.call(M.getEl(found[0]), 'className')) {
293 if (_findIdMatch(found[1])) {
294 pageNum = Math.floor(commentArrId / commentsPerPage) + 1;
295 M.log(c.DEBUG,'Comment Array Id: ' + commentArrId);
296 M.log(c.DEBUG,'Comments Per Page: ' + commentsPerPage);
297 M.log(c.DEBUG,'Page Number: ' + pageNum);
298 M.log(c.DEBUG,'Comment Offset: ' + _getCommentOffset());
306 function _setCommentOffset() {
307 commentsOffset = commentsPerPage * (pageNum-1);
311 function _getCommentOffset() {
312 return commentsOffset;
316 function _handleEvents (e) {
317 var origin = e.target || e.srcElement,
321 origin = origin.id && M.getEl(origin.id) || false;
324 parentId = u.checkNodeType(origin.parentNode)==='element' && origin.parentNode.getAttribute('id') && origin.parentNode.id;
329 switch(origin.nodeName) {
332 case 'prev-comments':
333 case 'top-prev-comments':
334 if(e.preventDefault) {
337 e.returnValue = false;
343 case 'next-comments':
344 case 'top-next-comments':
345 if(e.preventDefault) {
348 e.returnValue = false;
360 function _toggleNavLinks () {
361 M.log(c.DEBUG,M.getEl('top-prev-comments'));
362 if(pageNum <= commentsTotalPages && pageNum !== 1) {
363 u.toggleVisibility.show('prev-comments');
364 u.toggleVisibility.show('top-prev-comments');
367 if(pageNum >= 1 && pageNum !== commentsTotalPages) {
368 u.toggleVisibility.show('next-comments');
369 u.toggleVisibility.show('top-next-comments');
372 if(pageNum===1 || nav.style.visibility==='hidden') {
373 u.toggleVisibility.hide('prev-comments');
374 u.toggleVisibility.hide('top-prev-comments');
377 if(pageNum===commentsTotalPages || nav.style.visibility==='hidden') {
378 u.toggleVisibility.hide('next-comments');
379 u.toggleVisibility.hide('top-next-comments');
384 function _nextPage () {
385 if(pageNum < commentsTotalPages) {
392 function _previousPage() {
400 function _findIdMatch (id) {
401 var len = MT.commentIds.length;
404 if (MT.commentIds[len] == id) {
405 commentAnchor = "comment-" + id;
415 function _setCurrentComments() {
416 var commentsOnPage = pageNum != commentsTotalPages ? commentsOffset + commentsPerPage : totalComments;
418 _setCurrentCommentsContent([commentsOffset+1," - ",commentsOnPage].join(''));
422 function _setCurrentCommentsContent(currentCommentsHTML) {
423 currentCommentsSpan.innerHTML = currentCommentsHTML;
424 topCurrentCommentsSpan.innerHTML = currentCommentsHTML;
428 function _setCommentContent(commentHTML) {
429 commentContentDiv.innerHTML = commentHTML;
433 function _updateComments() {
434 var comments, jsonUrl, cacheKey, offset;
436 _setCurrentCommentsContent(loadingIcon);
439 offset = _getCommentOffset();
440 cacheKey = [direction, entryID, commentsPerPage, offset]
442 .replace(/[^a-zA-Z0-9-]/g, '');
444 "http://defaria.com/mt/mt-comments.cgi?__mode=comment_listing&direction=",
454 "&jsonp=mtUpdateCommentsCallback",
459 if (!commentAnchor) {
460 commentAnchor = "comments-content";
463 if(cache.hasOwnProperty(cacheKey)) {
464 _refreshComments(cache[cacheKey]);
467 var script = document.createElement('script');
468 script.src = jsonUrl;
469 (document.getElementsByTagName('head'))[0].appendChild(script);
473 window['mtUpdateCommentsCallback'] = function(json) {
474 cache[json.cacheKey] = json.comments;
475 _refreshComments(json.comments);
480 function _refreshComments(commentData) {
481 _setCommentContent(commentData);
482 _setCurrentComments();
483 window.location.hash = 'reset';
484 window.location.hash = commentAnchor;
490 function mtRelativeDate(ts, fds) {
491 var now = new Date();
493 var delta = Math.floor((now.getTime() - ref.getTime()) / 1000);
498 } else if (delta <= 86400) {
500 var hours = Math.floor(delta / 3600);
501 var min = Math.floor((delta % 3600) / 60);
505 str = '2 hours ago'.replace(/2/, hours);
507 str = '1 minute ago';
509 str = '2 minutes ago'.replace(/2/, min);
510 } else if (delta <= 604800) {
512 var days = Math.floor(delta / 86400);
513 var hours = Math.floor((delta % 86400) / 3600);
517 str = '2 days ago'.replace(/2/, days);
521 str = '2 hours ago'.replace(/2/, hours);
523 return str ? str : fds;
527 function mtEditLink(entry_id, author_id) {
533 function mtCommentFormOnFocus() {
534 // if CAPTCHA is enabled, this causes the captcha image to be
535 // displayed if it hasn't been already.
540 var mtCaptchaVisible = false;
541 function mtShowCaptcha() {
543 if ( u && u.is_authenticated ) return;
544 if (mtCaptchaVisible) return;
545 var div = document.getElementById('comments-open-captcha');
548 mtCaptchaVisible = true;
557 function mtSetUser(u) {
562 // sync up user greeting
563 mtFireEvent('usersignin');
568 function mtEscapeJS(s) {
569 s = s.replace(/'/g, "'");
574 function mtUnescapeJS(s) {
575 s = s.replace(/'/g, "'");
580 function mtBakeUserCookie(u) {
582 if (u.name) str += "name:'" + mtEscapeJS(u.name) + "';";
583 if (u.url) str += "url:'" + mtEscapeJS(u.url) + "';";
584 if (u.email) str += "email:'" + mtEscapeJS(u.email) + "';";
585 if (u.is_authenticated) str += "is_authenticated:'1';";
586 if (u.profile) str += "profile:'" + mtEscapeJS(u.profile) + "';";
587 if (u.userpic) str += "userpic:'" + mtEscapeJS(u.userpic) + "';";
588 if (u.sid) str += "sid:'" + mtEscapeJS(u.sid) + "';";
589 str += "is_trusted:'" + (u.is_trusted ? "1" : "0") + "';";
590 str += "is_author:'" + (u.is_author ? "1" : "0") + "';";
591 str += "is_banned:'" + (u.is_banned ? "1" : "0") + "';";
592 str += "can_post:'" + (u.can_post ? "1" : "0") + "';";
593 str += "can_comment:'" + (u.can_comment ? "1" : "0") + "';";
594 str = str.replace(/;$/, '');
599 function mtUnbakeUserCookie(s) {
604 while (m = s.match(/^((name|url|email|is_authenticated|profile|userpic|sid|is_trusted|is_author|is_banned|can_post|can_comment):'([^']+?)';?)/)) {
605 s = s.substring(m[1].length);
606 if (m[2].match(/^(is|can)_/)) // boolean fields
607 u[m[2]] = m[3] == '1' ? true : false;
609 u[m[2]] = mtUnescapeJS(m[3]);
611 if (u.is_authenticated) {
612 u.is_anonymous = false;
614 u.is_anonymous = true;
618 u.is_trusted = false;
624 function mtGetUser() {
626 var cookie = mtGetCookie(mtCookieName);
628 user = mtUnbakeUserCookie(cookie);
631 user.is_anonymous = true;
632 user.can_post = false;
633 user.is_author = false;
634 user.is_banned = false;
635 user.is_trusted = false;
642 var mtFetchedUser = false;
644 function mtFetchUser(cb) {
645 if (!cb) cb = 'mtSetUser';
646 if ( ( cb == 'mtSetUser' ) && mtGetUser() ) {
647 var url = document.URL;
648 url = url.replace(/#.+$/, '');
649 url += '#comments-form';
652 // we aren't using AJAX for this, since we may have to request
653 // from a different domain. JSONP to the rescue.
654 mtFetchedUser = true;
656 var script = document.createElement('script');
657 var ts = new Date().getTime();
658 script.src = 'http://defaria.com/mt/mt-comments.cgi?__mode=userinfo&blog_id=8&jsonp=' + cb + '&ts=' + ts + '&sid=' + u.sid;
659 (document.getElementsByTagName('head'))[0].appendChild(script);
663 function mtVerifySession(cb) {
665 var script = document.createElement('script');
666 var ts = new Date().getTime();
668 script.src = 'http://defaria.com/mt/mt-comments.cgi?__mode=verify_session&blog_id=8&jsonp=' + cb + '&ts=' + ts + '&sid=' + u.sid;
669 (document.getElementsByTagName('head'))[0].appendChild(script);
674 function mtRememberMeOnClick(b) {
682 var mtRequestSubmitted = false;
683 function mtCommentOnSubmit(f) {
684 if (!mtRequestSubmitted) {
685 mtRequestSubmitted = true;
688 f.armor.value = 'daa5a781478cd9a33628b0de8974f6ec865c9477';
689 if (f.bakecookie && f.bakecookie.checked)
692 // disable submit buttons
693 if (f.preview_button) f.preview_button.disabled = true;
694 if (f.post) f.post.disabled = true;
697 if ( !is_preview && ( u && u.is_authenticated ) ) {
698 // validate session; then submit
699 mtVerifySession('mtCommentSessionVerify');
708 function mtCommentSessionVerify(app_user) {
709 var f = document['comments_form'];
710 if ( app_user && app_user.verified ) {
713 alert('Your session has expired. Please sign in again to comment.');
715 mtFireEvent('usersignin');
717 mtShow('comments-form');
718 mtHide('comments-open-footer');
723 function mtUserOnLoad() {
726 // if the user is authenticated, hide the 'anonymous' fields
727 // and any captcha input if already shown
728 if ( document.getElementById('comments-form')) {
729 if ( u && u.is_authenticated ) {
730 mtShow('comments-form');
731 mtHide('comments-open-data');
732 if (mtCaptchaVisible)
733 mtHide('comments-open-captcha');
736 mtHide('comments-form');
739 if ( u && u.is_banned )
740 mtHide('comments-form');
742 // if we're previewing a comment, make sure the captcha
749 // populate anonymous comment fields if user is cookied as anonymous
750 var cf = document['comments_form'];
752 if (u && u.is_anonymous) {
753 if (u.email) cf.email.value = u.email;
754 if (u.name) cf.author.value = u.name;
755 if (u.url) cf.url.value = u.url;
757 cf.bakecookie.checked = u.name || u.email;
759 if (u && u.sid && cf.sid)
760 cf.sid.value = u.sid;
762 if (cf.post && cf.post.disabled)
763 cf.post.disabled = false;
764 if (cf.preview_button && cf.preview_button.disabled)
765 cf.preview_button.disabled = false;
766 mtRequestSubmitted = false;
774 function mtEntryOnLoad() {
775 var cf = document['comments_form'];
776 if (cf && cf.preview) cf.preview.value = '';
777 mtHide('trackbacks-info');
778 mtHide('comment-greeting');
779 mtHide('comments-form');
780 mtFireEvent('usersignin');
783 function mtEntryOnUnload() {
784 if (mtRequestSubmitted) {
785 var cf = document['comments_form'];
787 if (cf.post && cf.post.disabled)
788 cf.post.disabled = false;
789 if (cf.preview_button && cf.preview_button.disabled)
790 cf.preview_button.disabled = false;
792 mtRequestSubmitted = false;
797 mtAttachEvent('usersignin', mtUserOnLoad);
801 function mtSignIn() {
802 var doc_url = document.URL;
803 doc_url = doc_url.replace(/#.+/, '');
804 var url = 'http://defaria.com/mt/mt-comments.cgi?__mode=login&blog_id=8';
806 if ( document['comments_form'] ) {
807 var entry_id = document['comments_form'].entry_id.value;
808 url += '&entry_id=' + entry_id;
810 url += '&return_url=http%3A%2F%2Fdefaria.com%2Fblogs%2FStatus%2F';
813 url += '&return_url=' + encodeURIComponent(doc_url);
819 function mtSignInOnClick(sign_in_element) {
821 if (sign_in_element) {
823 el = document.getElementById(sign_in_element);
824 if (!el) // legacy MT 4.x element id
825 el = document.getElementById('comment-form-external-auth');
828 el.innerHTML = 'Signing in... <span class="status-indicator"> </span>';
830 mtClearUser(); // clear any 'anonymous' user cookie to allow sign in
835 function mtSetUserOrLogin(u) {
836 if (u && u.is_authenticated) {
839 // user really isn't logged in; so let's do this!
845 function mtSignOut(entry_id) {
847 var doc_url = document.URL;
848 doc_url = doc_url.replace(/#.+/, '');
849 var url = 'http://defaria.com/mt/mt-comments.cgi?__mode=handle_sign_in&static=0&logout=1&blog_id=8';
851 if ( document['comments_form'] ) {
852 var entry_id = document['comments_form'].entry_id.value;
853 url += '&entry_id=' + entry_id;
855 url += '&return_url=http%3A%2F%2Fdefaria.com%2Fblogs%2FStatus%2F';
858 url += '&return_url=' + encodeURIComponent(doc_url);
864 function mtSignOutOnClick() {
871 function mtShowGreeting() {
875 var cf = document['comments_form'];
878 var el = document.getElementById('comment-greeting');
879 if (!el) // legacy MT 4.x element id
880 el = document.getElementById('comment-form-external-auth');
883 var eid = cf.entry_id;
885 if (eid) entry_id = eid.value;
890 if ( u && u.is_authenticated ) {
892 phrase = 'You do not have permission to comment on this blog. (\<a href=\"javas\cript:void(0);\" onclick=\"return mtSignOutOnClick();\"\>sign out\<\/a\>)';
896 user_link = '<a href="http://defaria.com/mt/mt-comments.cgi?__mode=edit_profile&blog_id=8&return_url=' + encodeURIComponent( location.href );
897 user_link += '">' + u.name + '</a>';
899 // registered user, but not a user with posting rights
901 user_link = '<a href="' + u.url + '">' + u.name + '</a>';
905 // TBD: supplement phrase with userpic if one is available.
906 phrase = 'Thanks for signing in, __NAME__. (\<a href=\"javas\cript:void(0)\" onclick=\"return mtSignOutOnClick();\"\>sign out\<\/a\>)';
907 phrase = phrase.replace(/__NAME__/, user_link);
911 phrase = '\<a href=\"javas\cript:void(0)\" onclick=\"return mtSignInOnClick(\'comment-greeting\')\"\>Sign in\<\/a\> to comment.';
913 phrase = '\<a href=\"javas\cript:void(0)\" onclick=\"return mtSignInOnClick(\'comment-greeting\')\"\>Sign in\<\/a\> to comment, or comment anonymously.';
916 el.innerHTML = phrase;
922 function mtReplyCommentOnClick(parent_id, author) {
923 mtShow('comment-form-reply');
925 var checkbox = document.getElementById('comment-reply');
926 var label = document.getElementById('comment-reply-label');
927 var text = document.getElementById('comment-text');
929 // Populate label with new values
930 var reply_text = 'Replying to \<a href=\"#comment-__PARENT__\" onclick=\"location.href=this.href; return false\"\>comment from __AUTHOR__\<\/a\>';
931 reply_text = reply_text.replace(/__PARENT__/, parent_id);
932 reply_text = reply_text.replace(/__AUTHOR__/, author);
933 label.innerHTML = reply_text;
935 checkbox.value = parent_id;
936 checkbox.checked = true;
938 // text field may be hidden
943 mtSetCommentParentID();
947 function mtSetCommentParentID() {
948 var checkbox = document.getElementById('comment-reply');
949 var parent_id_field = document.getElementById('comment-parent-id');
950 if (!checkbox || !parent_id_field) return;
953 if (checkbox.checked == true)
954 pid = checkbox.value;
955 parent_id_field.value = pid;
959 function mtSaveUser(f) {
960 // We can't reliably store the user cookie during a preview.
961 if (is_preview) return;
965 if (f && (!u || u.is_anonymous)) {
968 u.is_authenticated = false;
969 u.can_comment = true;
972 u.is_anonymous = true;
973 u.is_trusted = false;
975 if (f.author != undefined) u.name = f.author.value;
976 if (f.email != undefined) u.email = f.email.value;
977 if (f.url != undefined) u.url = f.url.value;
982 var cache_period = mtCookieTimeout * 1000;
984 // cache anonymous user info for a long period if the
985 // user has requested to be remembered
986 if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
987 cache_period = 365 * 24 * 60 * 60 * 1000;
989 var now = new Date();
991 now.setTime(now.getTime() + cache_period);
993 var cmtcookie = mtBakeUserCookie(u);
994 mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
995 location.protocol == 'https:');
999 function mtClearUser() {
1001 mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain,
1002 location.protocol == 'https:');
1006 function mtSetCookie(name, value, expires, path, domain, secure) {
1007 if (domain && ( domain.match(/^\.?localhost$/) || domain.match(/^[0-9\.]+$/) ))
1009 var curCookie = name + "=" + encodeURIComponent(value) +
1010 (expires ? "; expires=" + expires.toGMTString() : "") +
1011 (path ? "; path=" + path : "") +
1012 (domain ? "; domain=" + domain : "") +
1013 (secure ? "; secure" : "");
1014 document.cookie = curCookie;
1018 function mtGetCookie(name) {
1019 var prefix = name + '=';
1020 var c = document.cookie;
1021 var cookieStartIndex = c.indexOf(prefix);
1022 if (cookieStartIndex == -1)
1024 var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
1025 if (cookieEndIndex == -1)
1026 cookieEndIndex = c.length;
1027 var this_cookie = c.substring(cookieStartIndex + prefix.length, cookieEndIndex);
1028 if ( this_cookie.match(/\%u/i) ) {
1031 return decodeURIComponent( this_cookie );
1035 function mtDeleteCookie(name, path, domain, secure) {
1036 if (mtGetCookie(name)) {
1037 if (domain && domain.match(/^\.?localhost$/))
1039 document.cookie = name + "=" +
1040 (path ? "; path=" + path : "") +
1041 (domain ? "; domain=" + domain : "") +
1042 (secure ? "; secure" : "") +
1043 "; expires=Thu, 01-Jan-70 00:00:01 GMT";
1047 function mtFixDate(date) {
1048 var skew = (new Date(0)).getTime();
1050 date.setTime(date.getTime() - skew);
1054 function mtGetXmlHttp() {
1055 if ( !window.XMLHttpRequest ) {
1056 window.XMLHttpRequest = function() {
1058 "Microsoft.XMLHTTP",
1059 "MSXML2.XMLHTTP.5.0",
1060 "MSXML2.XMLHTTP.4.0",
1061 "MSXML2.XMLHTTP.3.0",
1065 for ( var i = 0; i < types.length; i++ ) {
1067 return new ActiveXObject( types[ i ] );
1074 if ( window.XMLHttpRequest )
1075 return new XMLHttpRequest();
1078 // BEGIN: fast browser onload init
1079 // Modifications by David Davis, DWD
1080 // Dean Edwards/Matthias Miller/John Resig
1081 // http://dean.edwards.name/weblog/2006/06/again/?full#comment5338
1084 // quit if this function has already been called
1085 if (arguments.callee.done) return;
1087 // flag this function so we don't do the same thing twice
1088 arguments.callee.done = true;
1091 // DWD - check against window
1092 if ( window._timer ) clearInterval(window._timer);
1094 // DWD - fire the window onload now, and replace it
1095 if ( window.onload && ( window.onload !== window.mtInit ) ) {
1097 window.onload = function() {};
1106 /* for Mozilla/Opera9 */
1107 if (document.addEventListener) {
1108 document.addEventListener("DOMContentLoaded", mtInit, false);
1111 /* for Internet Explorer */
1114 document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
1115 var script = document.getElementById("__ie_onload");
1116 script.onreadystatechange = function() {
1117 if (this.readyState == "complete") {
1118 mtInit(); // call the onload handler
1124 if (/WebKit/i.test(navigator.userAgent)) { // sniff
1125 _timer = setInterval(function() {
1126 if (/loaded|complete/.test(document.readyState)) {
1127 mtInit(); // call the onload handler
1132 /* for other browsers */
1133 window.onload = mtInit;
1135 // END: fast browser onload init
1140 function mtLoggedIn(ott) {
1141 var script = document.createElement('script');
1142 var ts = new Date().getTime();
1143 script.src = 'http://defaria.com/mt/mt-comments.cgi?__mode=userinfo&jsonp=mtSaveUserInfo&ott=' + ott;
1144 (document.getElementsByTagName('head'))[0].appendChild(script);
1147 function mtRefreshUserInfo(sid) {
1148 var script = document.createElement('script');
1149 var ts = new Date().getTime();
1150 script.src = 'http://defaria.com/mt/mt-comments.cgi?__mode=userinfo&jsonp=mtSaveUserInfo&sid=' + sid;
1151 (document.getElementsByTagName('head'))[0].appendChild(script);
1154 function mtSaveUserInfo (u) {
1157 alert('The sign-in attempt was not successful; Please try again.');
1162 var cmtcookie = mtBakeUserCookie(u);
1163 var now = new Date();
1164 var cache_period = mtCookieTimeout * 1000;
1166 // cache anonymous user info for a long period if the
1167 // user has requested to be remembered
1169 now.setTime(now.getTime() + cache_period);
1170 mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
1171 location.protocol == 'https:');
1172 mtFireEvent('usersignin');
1175 function mtInitCommenter () {
1177 * If request contains a '#_login' or '#_logout' hash, use this to
1178 * also delete the blog-side user cookie, since we're coming back from
1179 * a login, logout or edit profile operation.
1182 var hash = ( window.location.hash );
1183 hash.match( /^#_(.*)$/ );
1184 var command = RegExp.$1 || '';
1185 if ( command === 'refresh' ) {
1186 // Back from profile edit screen.
1187 // Reload userinfo with current session ID.
1188 var u = mtGetUser();
1190 mtRefreshUserInfo(u.sid);
1192 else if ( command === 'logout' ) {
1193 // clear any logged in state
1195 mtFireEvent('usersignin');
1197 else if ( command.match( /^login_(.*)$/ ) ) {
1198 var sid = RegExp.$1;
1202 mtFireEvent('usersignin');