-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmt.js
1192 lines (975 loc) · 34.4 KB
/
mt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The cookie name to use for storing the blog-side comment session cookie.
var mtCookieName = "mt_blog_user";
var mtCookieDomain = ".perrin3.com";
var mtCookiePath = "/";
var mtCookieTimeout = 14400;
function mtHide(id) {
var el = (typeof id == "string") ? document.getElementById(id) : id;
if (el) el.style.display = 'none';
}
function mtShow(id) {
var el = (typeof id == "string") ? document.getElementById(id) : id;
if (el) el.style.display = 'block';
}
function mtAttachEvent(eventName,func) {
var onEventName = 'on' + eventName;
var old = window[onEventName];
if( typeof old != 'function' )
window[onEventName] = func;
else {
window[onEventName] = function( evt ) {
old( evt );
return func( evt );
};
}
}
function mtFireEvent(eventName,param) {
var fn = window['on' + eventName];
if (typeof fn == 'function') return fn(param);
return;
}
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');};}}());
var MT = window.MT || {};
MT.cons = function () {
return {
LOG : 'log',
WARN : 'warn',
DEBUG : 'debug',
INFO : 'info',
ERR : 'error',
JSON : 'json'
};
}();
MT.core = function (o) {
var _debug = false;
return {
connect : function (url,respType,respHandler) {
var xh = mtGetXmlHttp();
if (!xh) return false;
xh.onreadystatechange = function() {
if ( xh.readyState == 4 ) {
if ( xh.status && ( xh.status != 200 ) ) {
// error - ignore
} else {
switch (respType) {
case 'json':
respHandler(JSON.parse(xh.responseText));
break;
case 'xml':
break;
case 'text':
break;
}
}
}
};
xh.open('GET',url);
xh.send(null);
},
getEl : function (el) {
return MT.util.checkNodeType(el)==='element' ? id : (document.getElementById(el) || false);
},
addEvent : function (el,type,func,obj) {
if(!obj && document.addEventListener) {
el.addEventListener(type,func,false);
} else if(obj && document.addEventListener) {
el.addEventListener(type,function () {
func.call(obj,event);
},false);
} else {
if(obj) {
el.attachEvent('on' + type,function () {
func.call(obj,event);
});
} else {
el.attachEvent('on' + type,function () {
func.call(el,event);
});
}
}
},
log : function (level,msg) {
if(_debug && window.console) {
switch(level) {
case 'warn':
case 'debug':
case 'info':
case 'error':
case 'log':
console[level](msg);
break;
default:
return false;
}
} else {
return false;
}
}
}
}();
MT.util = function () {
return {
toggleVisibility : {
show : function () {
var i = arguments.length;
while(i--) {
if(MT.util.checkNodeType(arguments[i])==='element') {
arguments[i].style.visibility = 'visible';
} else {
MT.core.getEl(arguments[i]).style.visibility = 'visible';
}
}
},
hide : function () {
var i = arguments.length;
while(i--) {
if(MT.util.checkNodeType(arguments[i])==='element') {
arguments[i].style.visibility = 'hidden';
} else {
MT.core.getEl(arguments[i]).style.visibility = 'hidden';
}
}
}
},
toggleDisplay : {
show : function () {
var i = arguments.length;
while(i--) {
if(MT.util.checkNodeType(arguments[i])==='element') {
arguments[i].style.display = '';
} else {
MT.core.getEl(arguments[i]).style.display = '';
}
}
},
hide : function () {
var i = arguments.length;
while(i--) {
if(MT.util.checkNodeType(arguments[i])==='element') {
arguments[i].style.display = 'none';
} else {
MT.core.getEl(arguments[i]).style.display = 'none';
}
}
}
},
findDefiningParent : function (origin) {
if(MT.util.checkNodeType(origin)==='element') {
for(var node=origin.parentNode;node.parentNode;node=node.parentNode) {
if((node.hasAttribute && node.hasAttribute('id')) || node.getAttribute('id')) {
return node;
}
}
}
return false;
},
checkNodeType : function (obj) {
if (obj && obj.nodeName){
switch (obj.nodeType) {
case 1: return 'element';
case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
}
}
}
}
}();
(function () {
var M = MT.core,
c = MT.cons,
u = MT.util,
cache,
isLoading,
direction,
currentComments,
commentAnchor,
commentArrId,
commentsPerPage,
commentsTotalPages,
loadingIcon,
pageNum,
commentsOffset,
totalComments,
entryID,
commentContentDiv,
topNav,
nav,
currentCommentsSpan,
topCurrentCommentsSpan;
M.addEvent(window,'load',_init);
/**
* Initializes the class
*
* @return void
*/
function _init () {
if(!MT.entryCommentCount) {
return;
}
_initializeVariables();
_setCommentOffset(false);
_checkForAnchor();
_setCurrentComments();
_toggleNavLinks();
_initializeEvents();
}
function _initializeVariables() {
cache = {};
isLoading = false;
commentAnchor = '';
commentArrId = '';
commentsPerPage = MT.commentsPerPage || 50;
currentComments = '';
direction = 'ascend';
entryID = MT.entryID;
totalComments = MT.entryCommentCount;
commentsTotalPages = Math.ceil(totalComments / commentsPerPage);
pageNum = 1;
loadingIcon = "<img title='Loading...' src='http://perr.in/mt-static/images/indicator.white.gif' alt='Loading' />";
commentContentDiv = M.getEl("comments-content");
topNav = M.getEl("top-comment-nav");
nav = M.getEl("comment-nav");
currentCommentsSpan = M.getEl("current-comments");
topCurrentCommentsSpan = M.getEl("top-current-comments");
}
function _initializeEvents() {
if (commentsPerPage < totalComments) {
M.addEvent(nav,'click',_handleEvents);
M.addEvent(topNav,'click',_handleEvents);
}
}
function _checkForAnchor() {
var found = String(window.location.hash).match( /comment-(\d{1,6})/ );
if (found) {
M.log(c.DEBUG,found);
if (!M.getEl(found[0]).hasOwnProperty('className')) {
if (_findIdMatch(found[1])) {
pageNum = Math.floor(commentArrId / commentsPerPage) + 1;
M.log(c.DEBUG,'Comment Array Id: ' + commentArrId);
M.log(c.DEBUG,'Comments Per Page: ' + commentsPerPage);
M.log(c.DEBUG,'Page Number: ' + pageNum);
M.log(c.DEBUG,'Comment Offset: ' + _getCommentOffset());
_updateComments();
}
}
}
}
function _setCommentOffset() {
commentsOffset = commentsPerPage * (pageNum-1);
}
function _getCommentOffset() {
return commentsOffset;
}
function _handleEvents (e) {
var origin = e.target || e.srcElement,
parentId;
// stupid IE
origin = origin.id && M.getEl(origin.id) || false;
if(origin) {
parentId = u.checkNodeType(origin.parentNode)==='element' && origin.parentNode.getAttribute('id') && origin.parentNode.id;
} else {
return false;
}
switch(origin.nodeName) {
case 'A':
switch (parentId) {
case 'prev-comments':
case 'top-prev-comments':
if(e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
if(!isLoading) {
_previousPage();
}
break;
case 'next-comments':
case 'top-next-comments':
if(e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
if(!isLoading) {
_nextPage();
}
break;
}
break;
}
}
function _toggleNavLinks () {
M.log(c.DEBUG,M.getEl('top-prev-comments'));
if(pageNum <= commentsTotalPages && pageNum !== 1) {
u.toggleVisibility.show('prev-comments');
u.toggleVisibility.show('top-prev-comments');
}
if(pageNum >= 1 && pageNum !== commentsTotalPages) {
u.toggleVisibility.show('next-comments');
u.toggleVisibility.show('top-next-comments');
}
if(pageNum===1 || nav.style.visibility==='hidden') {
u.toggleVisibility.hide('prev-comments');
u.toggleVisibility.hide('top-prev-comments');
}
if(pageNum===commentsTotalPages || nav.style.visibility==='hidden') {
u.toggleVisibility.hide('next-comments');
u.toggleVisibility.hide('top-next-comments');
}
}
function _nextPage () {
if(pageNum < commentsTotalPages) {
pageNum++;
_updateComments();
}
}
function _previousPage() {
if(pageNum > 1) {
pageNum--;
_updateComments();
}
}
function _findIdMatch (id) {
var len = MT.commentIds.length;
while (len--) {
if (MT.commentIds[len] == id) {
commentAnchor = "comment-" + id;
commentArrId = len;
return true;
}
}
return false;
}
function _setCurrentComments() {
var commentsOnPage = pageNum != commentsTotalPages ? commentsOffset + commentsPerPage : totalComments;
_setCurrentCommentsContent([commentsOffset+1," - ",commentsOnPage].join(''));
}
function _setCurrentCommentsContent(currentCommentsHTML) {
currentCommentsSpan.innerHTML = currentCommentsHTML;
topCurrentCommentsSpan.innerHTML = currentCommentsHTML;
}
function _setCommentContent(commentHTML) {
commentContentDiv.innerHTML = commentHTML;
}
function _updateComments() {
var comments, jsonUrl;
isLoading = true;
_setCurrentCommentsContent(loadingIcon);
_setCommentOffset();
jsonUrl = [
"http://perr.in/cgi-bin/mt/mt-comments.cgi?__mode=comment_listing&direction=",
direction,
"&entry_id=",
entryID,
"&limit=",
commentsPerPage,
"&offset=",
_getCommentOffset()
].join('');
if (!commentAnchor) {
commentAnchor = "comments-content";
}
if(cache.hasOwnProperty(jsonUrl)) {
_refreshComments(cache[jsonUrl]);
isLoading = false;
} else {
M.connect(jsonUrl,c.JSON,function (json) {
cache[jsonUrl] = json.comments;
_refreshComments(json.comments);
isLoading = false;
});
}
}
function _refreshComments(commentData) {
_setCommentContent(commentData);
_setCurrentComments();
window.location.hash = 'reset';
window.location.hash = commentAnchor;
_toggleNavLinks();
}
})();
function mtRelativeDate(ts, fds) {
var now = new Date();
var ref = ts;
var delta = Math.floor((now.getTime() - ref.getTime()) / 1000);
var str;
if (delta < 60) {
str = 'moments ago';
} else if (delta <= 86400) {
// less than 1 day
var hours = Math.floor(delta / 3600);
var min = Math.floor((delta % 3600) / 60);
if (hours == 1)
str = '1 hour ago';
else if (hours > 1)
str = '2 hours ago'.replace(/2/, hours);
else if (min == 1)
str = '1 minute ago';
else
str = '2 minutes ago'.replace(/2/, min);
} else if (delta <= 604800) {
// less than 1 week
var days = Math.floor(delta / 86400);
var hours = Math.floor((delta % 86400) / 3600);
if (days == 1)
str = '1 day ago';
else if (days > 1)
str = '2 days ago'.replace(/2/, days);
else if (hours == 1)
str = '1 hour ago';
else
str = '2 hours ago'.replace(/2/, hours);
}
return str ? str : fds;
}
function mtEditLink(entry_id, author_id) {
}
function mtCommentFormOnFocus() {
// if CAPTCHA is enabled, this causes the captcha image to be
// displayed if it hasn't been already.
mtShowCaptcha();
}
var mtCaptchaVisible = false;
function mtShowCaptcha() {
var u = mtGetUser();
if ( u && u.is_authenticated ) return;
if (mtCaptchaVisible) return;
var div = document.getElementById('comments-open-captcha');
if (div) {
div.innerHTML = '';
mtCaptchaVisible = true;
}
}
var is_preview;
var user;
function mtSetUser(u) {
if (u) {
// persist this
user = u;
mtSaveUser();
// sync up user greeting
mtFireEvent('usersignin');
}
}
function mtEscapeJS(s) {
s = s.replace(/'/g, "'");
return s;
}
function mtUnescapeJS(s) {
s = s.replace(/'/g, "'");
return s;
}
function mtBakeUserCookie(u) {
var str = "";
if (u.name) str += "name:'" + mtEscapeJS(u.name) + "';";
if (u.url) str += "url:'" + mtEscapeJS(u.url) + "';";
if (u.email) str += "email:'" + mtEscapeJS(u.email) + "';";
if (u.is_authenticated) str += "is_authenticated:'1';";
if (u.profile) str += "profile:'" + mtEscapeJS(u.profile) + "';";
if (u.userpic) str += "userpic:'" + mtEscapeJS(u.userpic) + "';";
if (u.sid) str += "sid:'" + mtEscapeJS(u.sid) + "';";
str += "is_trusted:'" + (u.is_trusted ? "1" : "0") + "';";
str += "is_author:'" + (u.is_author ? "1" : "0") + "';";
str += "is_banned:'" + (u.is_banned ? "1" : "0") + "';";
str += "can_post:'" + (u.can_post ? "1" : "0") + "';";
str += "can_comment:'" + (u.can_comment ? "1" : "0") + "';";
str = str.replace(/;$/, '');
return str;
}
function mtUnbakeUserCookie(s) {
if (!s) return;
var u = {};
var m;
while (m = s.match(/^((name|url|email|is_authenticated|profile|userpic|sid|is_trusted|is_author|is_banned|can_post|can_comment):'([^']+?)';?)/)) {
s = s.substring(m[1].length);
if (m[2].match(/^(is|can)_/)) // boolean fields
u[m[2]] = m[3] == '1' ? true : false;
else
u[m[2]] = mtUnescapeJS(m[3]);
}
if (u.is_authenticated) {
u.is_anonymous = false;
} else {
u.is_anonymous = true;
u.can_post = false;
u.is_author = false;
u.is_banned = false;
u.is_trusted = false;
}
return u;
}
function mtGetUser() {
if (!user) {
var cookie = mtGetCookie(mtCookieName);
if (!cookie) return;
user = mtUnbakeUserCookie(cookie);
if (! user) {
user = {};
user.is_anonymous = true;
user.can_post = false;
user.is_author = false;
user.is_banned = false;
user.is_trusted = false;
}
}
return user;
}
var mtFetchedUser = false;
function mtFetchUser(cb) {
if (!cb) cb = 'mtSetUser';
if ( ( cb == 'mtSetUser' ) && mtGetUser() ) {
var url = document.URL;
url = url.replace(/#.+$/, '');
url += '#comments-open';
location.href = url;
} else {
// we aren't using AJAX for this, since we may have to request
// from a different domain. JSONP to the rescue.
mtFetchedUser = true;
var u = mtGetUser();
var script = document.createElement('script');
var ts = new Date().getTime();
script.src = 'http://perr.in/cgi-bin/mt/mt-comments.cgi?__mode=userinfo&blog_id=2&jsonp=' + cb + '&ts=' + ts + '&sid=' + u.sid;
(document.getElementsByTagName('head'))[0].appendChild(script);
}
}
function mtVerifySession(cb) {
if (!cb) return;
var script = document.createElement('script');
var ts = new Date().getTime();
var u = mtGetUser();
script.src = 'http://perr.in/cgi-bin/mt/mt-comments.cgi?__mode=verify_session&blog_id=2&jsonp=' + cb + '&ts=' + ts + '&sid=' + u.sid;
(document.getElementsByTagName('head'))[0].appendChild(script);
}
function mtRememberMeOnClick(b) {
if (!b.checked)
mtClearUser(b.form);
return true;
}
var mtRequestSubmitted = false;
function mtCommentOnSubmit(f) {
if (!mtRequestSubmitted) {
mtRequestSubmitted = true;
if (f.armor)
f.armor.value = 'b60299e50f35e5c15811bcd05e4f6cb920a6266d';
if (f.bakecookie && f.bakecookie.checked)
mtSaveUser(f);
// disable submit buttons
if (f.preview_button) f.preview_button.disabled = true;
if (f.post) f.post.disabled = true;
var u = mtGetUser();
if ( !is_preview && ( u && u.is_authenticated ) ) {
// validate session; then submit
mtVerifySession('mtCommentSessionVerify');
return false;
}
return true;
}
return false;
}
function mtCommentSessionVerify(app_user) {
var f = document['comments_form'];
if ( app_user && app_user.verified ) {
f.submit();
} else {
alert('Your session has expired. Please sign in again to comment.');
mtClearUser();
mtFireEvent('usersignin');
mtShow('comments-form');
mtHide('comments-open-footer');
}
}
function mtUserOnLoad() {
var u = mtGetUser();
// if the user is authenticated, hide the 'anonymous' fields
// and any captcha input if already shown
if ( document.getElementById('comments-form')) {
if ( u && u.is_authenticated ) {
mtShow('comments-form');
mtHide('comments-open-data');
if (mtCaptchaVisible)
mtHide('comments-open-captcha');
} else {
mtHide('comments-form');
}
if ( u && u.is_banned )
mtHide('comments-form');
// if we're previewing a comment, make sure the captcha
// field is visible
if (is_preview)
mtShowCaptcha();
else
mtShowGreeting();
// populate anonymous comment fields if user is cookied as anonymous
var cf = document['comments_form'];
if (cf) {
if (u && u.is_anonymous) {
if (u.email) cf.email.value = u.email;
if (u.name) cf.author.value = u.name;
if (u.url) cf.url.value = u.url;
if (cf.bakecookie)
cf.bakecookie.checked = u.name || u.email;
} else {
if (u && u.sid && cf.sid)
cf.sid.value = u.sid;
}
if (cf.post && cf.post.disabled)
cf.post.disabled = false;
if (cf.preview_button && cf.preview_button.disabled)
cf.preview_button.disabled = false;
mtRequestSubmitted = false;
}
}
}
function mtEntryOnLoad() {
var cf = document['comments_form'];
if (cf && cf.preview) cf.preview.value = '';
mtHide('trackbacks-info');
mtFireEvent('usersignin');
}
function mtEntryOnUnload() {
if (mtRequestSubmitted) {
var cf = document['comments_form'];
if (cf) {
if (cf.post && cf.post.disabled)
cf.post.disabled = false;
if (cf.preview_button && cf.preview_button.disabled)
cf.preview_button.disabled = false;
}
mtRequestSubmitted = false;
}
return true;
}
mtAttachEvent('usersignin', mtUserOnLoad);
function mtSignIn() {
var doc_url = document.URL;
doc_url = doc_url.replace(/#.+/, '');
var url = 'http://perr.in/cgi-bin/mt/mt-cp.cgi?__mode=login&blog_id=2';
if (is_preview) {
if ( document['comments_form'] ) {
var entry_id = document['comments_form'].entry_id.value;
url += '&entry_id=' + entry_id;
} else {
url += '&return_url=http%3A%2F%2Fperrin3.com%2F';
}
} else {
url += '&return_url=' + encodeURIComponent(doc_url);
}
mtClearUser();
location.href = url;
}
function mtSignInOnClick(sign_in_element) {
var el;
if (sign_in_element) {
// display throbber
el = document.getElementById(sign_in_element);
if (!el) // legacy MT 4.x element id
el = document.getElementById('comment-form-external-auth');
}
if (el)
el.innerHTML = 'Signing in... <span class="status-indicator"> </span>';
mtClearUser(); // clear any 'anonymous' user cookie to allow sign in
mtSignIn();
return false;
}
function mtSetUserOrLogin(u) {
if (u && u.is_authenticated) {
mtSetUser(u);
} else {
// user really isn't logged in; so let's do this!
mtSignIn();
}
}
function mtSignOut(entry_id) {
mtClearUser();
var doc_url = document.URL;
doc_url = doc_url.replace(/#.+/, '');
var url = 'http://perr.in/cgi-bin/mt/mt-cp.cgi?__mode=logout&static=0&blog_id=2';
if (is_preview) {
if ( document['comments_form'] ) {
var entry_id = document['comments_form'].entry_id.value;
url += '&entry_id=' + entry_id;
} else {
url += '&return_url=http%3A%2F%2Fperrin3.com%2F';
}
} else {
url += '&return_url=' + encodeURIComponent(doc_url);
}
location.href = url;
}
function mtSignOutOnClick() {
mtSignOut();
return false;
}
function mtShowGreeting() {
var reg_reqd = true;
var cf = document['comments_form'];
if (!cf) return;
var el = document.getElementById('comment-greeting');
if (!el) // legacy MT 4.x element id
el = document.getElementById('comment-form-external-auth');
if (!el) return;
var eid = cf.entry_id;
var entry_id;
if (eid) entry_id = eid.value;
var phrase;
var u = mtGetUser();
if ( u && u.is_authenticated ) {
if ( u.is_banned ) {
phrase = 'You do not have permission to comment on this blog. (\<a href=\"javas\cript:void(0);\" onclick=\"return mtSignOutOnClick();\"\>sign out\<\/a\>)';
} else {
var user_link;
if ( u.is_author ) {
user_link = '<a href="http://perr.in/cgi-bin/mt/mt-comments.cgi?__mode=edit_profile&blog_id=2&return_url=' + encodeURIComponent( location.href );
user_link += '">' + u.name + '</a>';
} else {
// registered user, but not a user with posting rights
if (u.url)
user_link = '<a href="' + u.url + '">' + u.name + '</a>';
else
user_link = u.name;
}
// TBD: supplement phrase with userpic if one is available.
phrase = 'Thanks for signing in, __NAME__. (\<a href=\"javas\cript:void(0)\" onclick=\"return mtSignOutOnClick();\"\>sign out\<\/a\>)';
phrase = phrase.replace(/__NAME__/, user_link);
}
} else {
if (reg_reqd) {
phrase = '\<a href=\"javas\cript:void(0)\" onclick=\"return mtSignInOnClick(\'comment-greeting\')\"\>Sign in\<\/a\> to comment.';
} else {
phrase = '\<a href=\"javas\cript:void(0)\" onclick=\"return mtSignInOnClick(\'comment-greeting\')\"\>Sign in\<\/a\> to comment, or comment anonymously.';
}
}
el.innerHTML = phrase;
}
function mtReplyCommentOnClick(parent_id, author) {
mtShow('comment-form-reply');
var checkbox = document.getElementById('comment-reply');
var label = document.getElementById('comment-reply-label');
var text = document.getElementById('comment-text');
// Populate label with new values
var reply_text = 'Replying to \<a href=\"#comment-__PARENT__\" onclick=\"location.href=this.href; return false\"\>comment from __AUTHOR__\<\/a\>';
reply_text = reply_text.replace(/__PARENT__/, parent_id);
reply_text = reply_text.replace(/__AUTHOR__/, author);
label.innerHTML = reply_text;
checkbox.value = parent_id;
checkbox.checked = true;
try {
// text field may be hidden
text.focus();
} catch(e) {
}
mtSetCommentParentID();
}
function mtSetCommentParentID() {
var checkbox = document.getElementById('comment-reply');
var parent_id_field = document.getElementById('comment-parent-id');
if (!checkbox || !parent_id_field) return;
var pid = 0;
if (checkbox.checked == true)
pid = checkbox.value;
parent_id_field.value = pid;
}
function mtSaveUser(f) {
// We can't reliably store the user cookie during a preview.
if (is_preview) return;
var u = mtGetUser();
if (f && (!u || u.is_anonymous)) {
if ( !u ) {
u = {};
u.is_authenticated = false;
u.can_comment = true;
u.is_author = false;
u.is_banned = false;
u.is_anonymous = true;
u.is_trusted = false;
}
if (f.author != undefined) u.name = f.author.value;
if (f.email != undefined) u.email = f.email.value;
if (f.url != undefined) u.url = f.url.value;
}
if (!u) return;
var cache_period = mtCookieTimeout * 1000;
// cache anonymous user info for a long period if the
// user has requested to be remembered
if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
cache_period = 365 * 24 * 60 * 60 * 1000;
var now = new Date();
mtFixDate(now);
now.setTime(now.getTime() + cache_period);
var cmtcookie = mtBakeUserCookie(u);
mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
location.protocol == 'https:');
}
function mtClearUser() {
user = null;
mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain,
location.protocol == 'https:');
}
function mtSetCookie(name, value, expires, path, domain, secure) {
if (domain && domain.match(/^\.?localhost$/))
domain = null;
var curCookie = name + "=" + encodeURIComponent(value) +
(expires ? "; expires=" + expires.toGMTString() : "") +
(path ? "; path=" + path : "") +
(domain ? "; domain=" + domain : "") +
(secure ? "; secure" : "");
document.cookie = curCookie;