root/branches/release-39/default_templates/javascript.mtml @ 2532

Revision 2532, 25.3 kB (checked in by bsmith, 18 months ago)

bugzid:80054 - Define additional js legacy variables in default templates javascript file

  • Property svn:keywords set to Id Revision
Line 
1<mt:Ignore>
2/*  The following functions and variables are here to support legacy MT templates.
3    If you have refreshed your JavaScript template but still use older MT comment
4    templates, you may need to uncomment this block in order for those templates
5    to work properly. To use, simply remove the 'mt:Ignore' tags wrapping this
6    block of code.
7*/
8    function hideDocumentElement(id) { return mtHide(id) }
9    function showDocumentElement(id) { return mtShow(id) }
10    function individualArchivesOnLoad() { return mtEntryOnLoad() }
11    function writeCommenterGreeting() { return mtShowGreeting() }
12    function rememberMe(f) { return mtRememberMe(f) }
13    function forgetMe(f) { return mtForgetMe(f) }
14    var commenter_name;
15    var commenter_id;
16    var commenter_url;
17    var commenter_blog_ids;
18    var mtcmtmail;
19    var mtcmtauth;
20    var mtcmthome;
21    var captcha_timer;
22</mt:Ignore>
23
24// The cookie name to use for storing the blog-side comment session cookie.
25var mtCookieName = "<$mt:UserSessionCookieName$>";
26var mtCookieDomain = "<$mt:UserSessionCookieDomain$>";
27var mtCookiePath = "<$mt:UserSessionCookiePath$>";
28var mtCookieTimeout = <$mt:UserSessionCookieTimeout$>;
29
30<mt:Ignore>
31/***
32 * Simple routine for showing a DOM element (applying a CSS display
33 * attribute of 'none').
34 */
35</mt:Ignore>
36function mtHide(id) {
37    var el = (typeof id == "string") ? document.getElementById(id) : id;
38    if (el) el.style.display = 'none';
39}
40
41<mt:Ignore>
42/***
43 * Simple routine for showing a DOM element (applying a CSS display
44 * attribute of 'block').
45 */
46</mt:Ignore>
47function mtShow(id) {
48    var el = (typeof id == "string") ? document.getElementById(id) : id;
49    if (el) el.style.display = 'block';
50}
51
52<mt:Ignore>
53/***
54 * A utility function for assigning/adding handlers to window events.
55 */
56</mt:Ignore>
57function mtAttachEvent(eventName,func) {
58    var onEventName = 'on' + eventName;
59    var old = window[onEventName];
60    if( typeof old != 'function' )
61        window[onEventName] = func;
62    else {
63        window[onEventName] = function( evt ) {
64            old( evt );
65            return func( evt );
66        };
67    }
68}
69
70<mt:Ignore>
71/***
72 * Calls the event named, if there are handlers for it.
73 */
74</mt:Ignore>
75function mtFireEvent(eventName,param) {
76    var fn = window['on' + eventName];
77    if (typeof fn == 'function') return fn(param);
78    return;
79}
80
81<mt:Ignore>
82/***
83 * Displays a relative date.
84 * 'ts' is a Date object, 'fds' is a string of the date which
85 * will be displayed if the given date is older than 1 week.
86 */
87</mt:Ignore>
88function mtRelativeDate(ts, fds) {
89    var now = new Date();
90    var ref = ts;
91    var delta = Math.floor((now.getTime() - ref.getTime()) / 1000);
92
93    var str;
94    if (delta < 60) {
95        str = '<__trans phrase="moments ago" escape="js">';
96    } else if (delta <= 86400) {
97        // less than 1 day
98        var hours = Math.floor(delta / 3600);
99        var min = Math.floor((delta % 3600) / 60);
100        if (hours == 1)
101            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="1" escape="js">';
102        else if (hours > 1)
103            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="2" escape="js">'.replace(/2/, hours);
104        else if (min == 1)
105            str = '<__trans phrase="[quant,_1,minute,minutes] ago" params="1" escape="js">';
106        else
107            str = '<__trans phrase="[quant,_1,minute,minutes] ago" params="2" escape="js">'.replace(/2/, min);
108    } else if (delta <= 604800) {
109        // less than 1 week
110        var days = Math.floor(delta / 86400);
111        var hours = Math.floor((delta % 86400) / 3600);
112        if (days == 1)
113            str = '<__trans phrase="[quant,_1,day,days] ago" params="1" escape="js">';
114        else if (days > 1)
115            str = '<__trans phrase="[quant,_1,day,days] ago" params="2" escape="js">'.replace(/2/, days);
116        else if (hours == 1)
117            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="1" escape="js">';
118        else
119            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="2" escape="js">'.replace(/2/, hours);
120    }
121    return str ? str : fds;
122}
123
124<mt:Ignore>
125/***
126 * Used to display an edit link for the given entry.
127 */
128</mt:Ignore>
129function mtEditLink(entry_id, author_id) {
130    var u = mtGetUser();
131    if (! u) return;
132    if (! entry_id) return;
133    if (! author_id) return;
134    if (u.id != author_id) return;
135    var link = '<a href="<$mt:AdminScript$>?__mode=view&amp;_type=entry&amp;id=' + entry_id + '"><__trans phrase="Edit" escape="js"></a>';
136    document.write(link);
137}
138
139<mt:Ignore>
140/***
141 * Called when an input field on the comment form receives focus.
142 */
143</mt:Ignore>
144function mtCommentFormOnFocus() {
145    // if CAPTCHA is enabled, this causes the captcha image to be
146    // displayed if it hasn't been already.
147    mtShowCaptcha();
148}
149
150<mt:Ignore>
151/***
152 * Displays a captcha field for anonymous commenters.
153 */
154</mt:Ignore>
155var mtCaptchaVisible = false;
156function mtShowCaptcha() {
157    var u = mtGetUser();
158    if ( u && u.is_authenticated ) return;
159    if (mtCaptchaVisible) return;
160    var div = document.getElementById('comments-open-captcha');
161    if (div) {
162        div.innerHTML = '<$mt:CaptchaFields$>';
163        mtCaptchaVisible = true;
164    }
165}
166
167<mt:Ignore>
168/* user object
169    -- saved in user cookie --
170    u.name (display name)
171    u.url (link to home page)
172    u.email (for anonymous only)
173    u.userpic (url for commenter/author)
174    u.profile (link to profile)
175    u.is_trusted (boolean)
176    u.is_author (user has posting rights)
177    u.is_banned (banned status; neither post/comment perms)
178    u.can_post (has permission to post)
179    u.can_comment (has permission to comment)
180
181    -- status fields --
182    u.is_authenticated (boolean)
183    u.is_anonymous (user is anonymous)
184*/
185</mt:Ignore>
186
187var is_preview;
188var user;
189<mt:Ignore>
190/***
191 * Assigns a user object as the actively logged in user; also saves the
192 * user information in a browser cookie.
193 */
194</mt:Ignore>
195function mtSetUser(u) {
196    if (u) {
197        // persist this
198        user = u;
199        mtSaveUser();
200        // sync up user greeting
201        mtFireEvent('usersignin');
202    }
203}
204
205<mt:Ignore>
206/***
207 * Simple function that escapes single quote characters for storing
208 * in a cookie.
209 */
210</mt:Ignore>
211function mtEscapeJS(s) {
212    s = s.replace(/'/g, "&apos;");
213    return s;
214}
215
216<mt:Ignore>
217/***
218 * Simple function that unescapes single quote characters that were
219 * stored in a cookie.
220 */
221</mt:Ignore>
222function mtUnescapeJS(s) {
223    s = s.replace(/&apos;/g, "'");
224    return s;
225}
226
227<mt:Ignore>
228/***
229 * Serializes a user object into a string, suitable for storing as a cookie.
230 */
231</mt:Ignore>
232function mtBakeUserCookie(u) {
233    var str = "";
234    if (u.name) str += "name:'" + mtEscapeJS(u.name) + "';";
235    if (u.url) str += "url:'" + mtEscapeJS(u.url) + "';";
236    if (u.email) str += "email:'" + mtEscapeJS(u.email) + "';";
237    if (u.is_authenticated) str += "is_authenticated:'1';";
238    if (u.profile) str += "profile:'" + mtEscapeJS(u.profile) + "';";
239    if (u.userpic) str += "userpic:'" + mtEscapeJS(u.userpic) + "';";
240    if (u.sid) str += "sid:'" + mtEscapeJS(u.sid) + "';";
241    str += "is_trusted:'" + (u.is_trusted ? "1" : "0") + "';";
242    str += "is_author:'" + (u.is_author ? "1" : "0") + "';";
243    str += "is_banned:'" + (u.is_banned ? "1" : "0") + "';";
244    str += "can_post:'" + (u.can_post ? "1" : "0") + "';";
245    str += "can_comment:'" + (u.can_comment ? "1" : "0") + "';";
246    str = str.replace(/;$/, '');
247    return str;
248}
249
250<mt:Ignore>
251/***
252 * Unserializes a user cookie and returns a user object with the restored
253 * state.
254 */
255</mt:Ignore>
256function mtUnbakeUserCookie(s) {
257    if (!s) return;
258
259    var u = {};
260    var m;
261    while (m = s.match(/^((name|url|email|is_authenticated|profile|userpic|sid|is_trusted|is_author|is_banned|can_post|can_comment):'([^']+?)';?)/)) {
262        s = s.substring(m[1].length);
263        if (m[2].match(/^(is|can)_/)) // boolean fields
264            u[m[2]] = m[3] == '1' ? true : false;
265        else
266            u[m[2]] = mtUnescapeJS(m[3]);
267    }
268    if (u.is_authenticated) {
269        u.is_anonymous = false;
270    } else {
271        u.is_anonymous = true;
272        u.can_post = false;
273        u.is_author = false;
274        u.is_banned = false;
275        u.is_trusted = false;
276    }
277    return u;
278}
279
280<mt:Ignore>
281/***
282 * Retrieves an object of the currently logged in user's state.
283 * If no user is logged in or cookied, this will return null.
284 */
285</mt:Ignore>
286function mtGetUser() {
287    if (!user) {
288        var cookie = mtGetCookie(mtCookieName);
289        if (!cookie) return;
290        user = mtUnbakeUserCookie(cookie);
291        if (! user) {
292            user = {};
293            user.is_anonymous = true;
294            user.can_post = false;
295            user.is_author = false;
296            user.is_banned = false;
297            user.is_trusted = false;
298        }
299    }
300    return user;
301}
302
303<mt:Ignore>
304/***
305 * Issues a request to the MT comment script to retrieve the currently
306 * logged-in user (if any).
307 */
308</mt:Ignore>
309var mtFetchedUser = false;
310function mtFetchUser(cb) {
311    if (!cb) cb = 'mtSetUser';
312    if ( ( cb == 'mtSetUser' ) && mtGetUser() ) {
313        var url = document.URL;
314        url = url.replace(/#.+$/, '');
315        url += '#comments-open';
316        location.href = url;
317    } else {
318        // we aren't using AJAX for this, since we may have to request
319        // from a different domain. JSONP to the rescue.
320        mtFetchedUser = true;
321        var script = document.createElement('script');
322        script.src = '<$mt:CGIPath$><$mt:CommentScript$>?__mode=session_js&blog_id=<$mt:BlogID$>&jsonp=' + cb;
323        (document.getElementsByTagName('head'))[0].appendChild(script);
324    }
325}
326
327<mt:Ignore>
328/***
329 * Called when the 'Remember me' checkbox is changed. If the checkbox
330 * is cleared, the cached user cookie is immediately cleared.
331 */
332</mt:Ignore>
333function mtRememberMeOnClick(b) {
334    if (!b.checked)
335        mtClearUser(b.form);
336    return true;
337}
338
339<mt:Ignore>
340/***
341 * Called when comment form is sent.
342 * Required parameter: Form DOM object of comment form.
343 * If form has a 'bakecookie' member, it will be used to signal
344 * storing the anonymous commenter information to a cookie.
345 * If form has a 'armor' member, it will be used to store
346 * a token that is checked by the comment script.
347 */
348</mt:Ignore>
349var mtRequestSubmitted = false;
350function mtCommentOnSubmit(f) {
351    if (!mtRequestSubmitted) {
352        mtRequestSubmitted = true;
353
354        if (f.armor)
355            f.armor.value = '<$mt:BlogSitePath encode_sha1="1"$>';
356        if (f.bakecookie && f.bakecookie.checked)
357            mtSaveUser(f);
358
359        // disable submit buttons
360        if (f.preview_button) f.preview_button.disabled = true;
361        if (f.post) f.post.disabled = true;
362
363        var u = mtGetUser();
364        if ( !is_preview && ( u && u.is_authenticated ) ) {
365            // validate session; then submit
366            mtFetchedUser = false;
367            mtFetchUser('mtCommentSessionVerify');
368            return false;
369        }
370
371        return true;
372    }
373    return false;
374}
375
376function mtCommentSessionVerify(app_user) {
377    var u = mtGetUser();
378    var f = document['comments_form'];
379    if ( u && app_user && app_user.sid && ( u.sid == app_user.sid ) ) {
380        f.submit();
381    } else {
382        alert('<__trans phrase="Your session has expired. Please sign in again to comment." escape="js">');
383        mtClearUser();
384        mtFireEvent('usersignin');
385<mt:IfRegistrationRequired>
386        mtShow('comments-form');
387        mtHide('comments-open-footer');
388</mt:IfRegistrationRequired>
389    }
390}
391
392function mtUserOnLoad() {
393    var u = mtGetUser();
394
395    // if the user is authenticated, hide the 'anonymous' fields
396    // and any captcha input if already shown
397    if ( document.getElementById('comments-form')) {
398        if ( u && u.is_authenticated ) {
399            mtShow('comments-form');
400            mtHide('comments-open-data');
401            if (mtCaptchaVisible)
402                mtHide('comments-open-captcha');
403        } else {
404<mt:IfRegistrationRequired>
405            mtHide('comments-form');
406</mt:IfRegistrationRequired>
407        }
408        if ( u && u.is_banned )
409            mtHide('comments-form');
410
411        // if we're previewing a comment, make sure the captcha
412        // field is visible
413        if (is_preview)
414            mtShowCaptcha();
415        else
416            mtShowGreeting();
417
418        // populate anonymous comment fields if user is cookied as anonymous
419        var cf = document['comments_form'];
420        if (cf) {
421            if (u && u.is_anonymous) {
422                if (u.email) cf.email.value = u.email;
423                if (u.name) cf.author.value = u.name;
424                if (u.url) cf.url.value = u.url;
425                if (cf.bakecookie)
426                    cf.bakecookie.checked = u.name || u.email;
427            } else {
428                if (u && u.sid && cf.sid)
429                    cf.sid.value = u.sid;
430            }
431            if (cf.post.disabled)
432                cf.post.disabled = false;
433            if (cf.preview_button.disabled)
434                cf.preview_button.disabled = false;
435            mtRequestSubmitted = false;
436        }
437    }
438}
439
440<mt:Ignore>
441/***
442 * Called when an entry archive page is loaded.
443 * This routine controls which elements of the comment form are shown
444 * or hidden, depending on commenter type and blog configuration.
445 */
446</mt:Ignore>
447function mtEntryOnLoad() {
448    <mt:Unless tag="IfPingsAccepted">mtHide('trackbacks-info');</mt:Unless>
449    <mt:Unless tag="IfCommentsAccepted">mtHide('comments-open');</mt:Unless>
450    mtFireEvent('usersignin');
451}
452
453mtAttachEvent('usersignin', mtUserOnLoad);
454
455<mt:Ignore>
456/***
457 * Handles the action of the "Sign in" link. First clears any existing
458 * user cookie, then directs to the MT comment script to sign the user in.
459 */
460</mt:Ignore>
461function mtSignIn() {
462    var doc_url = document.URL;
463    doc_url = doc_url.replace(/#.+/, '');
464    var url = '<$mt:SignInLink$>';
465    if (is_preview) {
466        if ( document['comments_form'] ) {
467            var entry_id = document['comments_form'].entry_id.value;
468            url += '&entry_id=' + entry_id;
469        } else {
470            url += '&return_url=<$MTBlogURL encode_url="1"$>';
471        }
472    } else {
473        url += '&return_url=' + encodeURIComponent(doc_url);
474    }
475    mtClearUser();
476    location.href = url;
477}
478
479function mtSignInOnClick(sign_in_element) {
480    var el;
481    if (sign_in_element) {
482        // display throbber
483        el = document.getElementById(sign_in_element);
484        if (!el)  // legacy MT 4.x element id
485            el = document.getElementById('comment-form-external-auth');
486    }
487    if (el)
488        el.innerHTML = '<__trans phrase="Signing in..." escape="js"> <img src="<$mt:StaticWebPath$>images/indicator.white.gif" height="16" width="16" alt="" />';
489
490    mtClearUser(); // clear any 'anonymous' user cookie to allow sign in
491    mtFetchUser('mtSetUserOrLogin');
492    return false;
493}
494
495function mtSetUserOrLogin(u) {
496    if (u && u.is_authenticated) {
497        mtSetUser(u);
498    } else {
499        // user really isn't logged in; so let's do this!
500        mtSignIn();
501    }
502}
503
504<mt:Ignore>
505/***
506 * Handles sign out from the web site.
507 * First clears any existing user cookie, then direts to the MT comment
508 * script to sign the user out.
509 */
510</mt:Ignore>
511function mtSignOut(entry_id) {
512    mtClearUser();
513    var doc_url = document.URL;
514    doc_url = doc_url.replace(/#.+/, '');
515    var url = '<$mt:SignOutLink$>';
516    if (is_preview) {
517        if ( document['comments_form'] ) {
518            var entry_id = document['comments_form'].entry_id.value;
519            url += '&entry_id=' + entry_id;
520        } else {
521            url += '&return_url=<$MTBlogURL encode_url="1"$>';
522        }
523    } else {
524        url += '&return_url=' + encodeURIComponent(doc_url);
525    }
526    location.href = url;
527}
528
529<mt:Ignore>
530/***
531 * Handles the action of the "Sign out" link.
532 */
533</mt:Ignore>
534function mtSignOutOnClick() {
535    mtSignOut();
536    return false;
537}
538
539<mt:Ignore>
540/***
541 * Handles the display of the greeting message, depending on what kind of
542 * user is logged in and blog comment policy.
543 */
544</mt:Ignore>
545function mtShowGreeting() {
546<mt:IfRegistrationAllowed>
547    var reg_reqd = <mt:IfRegistrationRequired>true<mt:Else>false</mt:IfRegistrationRequired>;
548
549    var cf = document['comments_form'];
550    if (!cf) return;
551
552    var el = document.getElementById('comment-greeting');
553    if (!el)  // legacy MT 4.x element id
554        el = document.getElementById('comment-form-external-auth');
555    if (!el) return;
556
557    var eid = cf.entry_id;
558    var entry_id;
559    if (eid) entry_id = eid.value;
560
561    var phrase;
562    var u = mtGetUser();
563
564    if ( u && u.is_authenticated ) {
565        if ( u.is_banned ) {
566            phrase = '<__trans phrase="You do not have permission to comment on this blog. ([_1]sign out[_2])" params="<a href="javascript:void(0);" onclick="return mtSignOutOnClick();">%%</a>" escape="js">';
567        } else {
568            var user_link;
569            if ( u.is_author ) {
570                user_link = '<a href="<$mt:CGIPath$><$mt:CommentScript$>?__mode=edit_profile&blog_id=<$mt:BlogID$>';
571                if (entry_id)
572                    user_link += '&entry_id=' + entry_id;
573                user_link += '">' + u.name + '</a>';
574            } else {
575                // registered user, but not a user with posting rights
576                if (u.url)
577                    user_link = '<a href="' + u.url + '">' + u.name + '</a>';
578                else
579                    user_link = u.name;
580            }
581            // TBD: supplement phrase with userpic if one is available.
582            phrase = '<__trans phrase="Thanks for signing in, __NAME__. ([_1]sign out[_2])" params="<a href="javascript:void(0)" onclick="return mtSignOutOnClick();">%%</a>" escape="js">';
583            phrase = phrase.replace(/__NAME__/, user_link);
584        }
585    } else {
586        if (reg_reqd) {
587            phrase = '<__trans phrase="[_1]Sign in[_2] to comment." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick('comment-greeting')">%%</a>" escape="js">';
588        } else {
589            phrase = '<__trans phrase="[_1]Sign in[_2] to comment, or comment anonymously." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick('comment-greeting')">%%</a>" escape="js">';
590        }
591    }
592    el.innerHTML = phrase;
593<mt:Else>
594    mtShowCaptcha();
595</mt:IfRegistrationAllowed>
596}
597
598<mt:Ignore>
599/***
600 * Handles the action of the 'Reply' links.
601 */
602</mt:Ignore>
603function mtReplyCommentOnClick(parent_id, author) {
604    mtShow('comment-form-reply');
605
606    var checkbox = document.getElementById('comment-reply');
607    var label = document.getElementById('comment-reply-label');
608    var text = document.getElementById('comment-text');
609
610    // Populate label with new values
611    var reply_text = '<__trans phrase="Replying to <a href="[_1]" onclick="[_2]">comment from [_3]</a>" params="#comment-__PARENT__%%location.href=this.href; return false%%__AUTHOR__" escape="js">';
612    reply_text = reply_text.replace(/__PARENT__/, parent_id);
613    reply_text = reply_text.replace(/__AUTHOR__/, author);
614    label.innerHTML = reply_text;
615
616    checkbox.value = parent_id;
617    checkbox.checked = true;
618    try {
619        // text field may be hidden
620        text.focus();
621    } catch(e) {
622    }
623
624    mtSetCommentParentID();
625}
626
627<mt:Ignore>
628/***
629 * Sets the parent comment ID when replying to a comment.
630 */
631</mt:Ignore>
632function mtSetCommentParentID() {
633    var checkbox = document.getElementById('comment-reply');
634    var parent_id_field = document.getElementById('comment-parent-id');
635    if (!checkbox || !parent_id_field) return;
636
637    var pid = 0;
638    if (checkbox.checked == true)
639        pid = checkbox.value;
640    parent_id_field.value = pid;
641}
642
643<mt:Ignore>
644/***
645 * Persists a copy of the current user cookie into the browser cookie stash.
646 */
647</mt:Ignore>
648function mtSaveUser(f) {
649    // We can't reliably store the user cookie during a preview.
650    if (is_preview) return;
651
652    var u = mtGetUser();
653
654    if (f && (!u || u.is_anonymous)) {
655        if ( !u ) {
656            u = {};
657            u.is_authenticated = false;
658            u.can_comment = true;
659            u.is_author = false;
660            u.is_banned = false;
661            u.is_anonymous = true;
662            u.is_trusted = false;
663        }
664        if (f.author != undefined) u.name = f.author.value;
665        if (f.email != undefined) u.email = f.email.value;
666        if (f.url != undefined) u.url = f.url.value;
667    }
668
669    if (!u) return;
670
671    var cache_period = mtCookieTimeout * 1000;
672
673    // cache anonymous user info for a long period if the
674    // user has requested to be remembered
675    if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
676        cache_period = 365 * 24 * 60 * 60 * 1000;
677
678    var now = new Date();
679    mtFixDate(now);
680    now.setTime(now.getTime() + cache_period);
681
682    var cmtcookie = mtBakeUserCookie(u);
683    mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
684        location.protocol == 'https:');
685}
686
687<mt:Ignore>
688/***
689 * Clears the blog-side user cookie.
690 */
691</mt:Ignore>
692function mtClearUser() {
693    user = null;
694    mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain,
695        location.protocol == 'https:');
696}
697
698<mt:Ignore>
699/***
700 * Sets a browser cookie.
701 */
702</mt:Ignore>
703function mtSetCookie(name, value, expires, path, domain, secure) {
704    if (domain && domain.match(/^\.?localhost$/))
705        domain = null;
706    var curCookie = name + "=" + escape(value) +
707        (expires ? "; expires=" + expires.toGMTString() : "") +
708        (path ? "; path=" + path : "") +
709        (domain ? "; domain=" + domain : "") +
710        (secure ? "; secure" : "");
711    document.cookie = curCookie;
712}
713
714<mt:Ignore>
715/***
716 * Retrieves a browser cookie.
717 */
718</mt:Ignore>
719function mtGetCookie(name) {
720    var prefix = name + '=';
721    var c = document.cookie;
722    var cookieStartIndex = c.indexOf(prefix);
723    if (cookieStartIndex == -1)
724        return '';
725    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
726    if (cookieEndIndex == -1)
727        cookieEndIndex = c.length;
728    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
729}
730
731<mt:Ignore>
732/***
733 * Deletes a browser cookie.
734 */
735</mt:Ignore>
736function mtDeleteCookie(name, path, domain, secure) {
737    if (mtGetCookie(name)) {
738        if (domain && domain.match(/^\.?localhost$/))
739            domain = null;
740        document.cookie = name + "=" +
741            (path ? "; path=" + path : "") +
742            (domain ? "; domain=" + domain : "") +
743            (secure ? "; secure" : "") +
744            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
745    }
746}
747
748function mtFixDate(date) {
749    var skew = (new Date(0)).getTime();
750    if (skew > 0)
751        date.setTime(date.getTime() - skew);
752}
753
754<mt:Ignore>
755/***
756 * Returns a XMLHttpRequest object (for Ajax operations).
757 */
758</mt:Ignore>
759function mtGetXmlHttp() {
760    if ( !window.XMLHttpRequest ) {
761        window.XMLHttpRequest = function() {
762            var types = [
763                "Microsoft.XMLHTTP",
764                "MSXML2.XMLHTTP.5.0",
765                "MSXML2.XMLHTTP.4.0",
766                "MSXML2.XMLHTTP.3.0",
767                "MSXML2.XMLHTTP"
768            ];
769
770            for ( var i = 0; i < types.length; i++ ) {
771                try {
772                    return new ActiveXObject( types[ i ] );
773                } catch( e ) {}
774            }
775
776            return undefined;
777        };
778    }
779    if ( window.XMLHttpRequest )
780        return new XMLHttpRequest();
781}
782
783// BEGIN: fast browser onload init
784// Modifications by David Davis, DWD
785// Dean Edwards/Matthias Miller/John Resig
786// http://dean.edwards.name/weblog/2006/06/again/?full#comment5338
787
788function mtInit() {
789    // quit if this function has already been called
790    if (arguments.callee.done) return;
791
792    // flag this function so we don't do the same thing twice
793    arguments.callee.done = true;
794
795    // kill the timer
796    // DWD - check against window
797    if ( window._timer ) clearInterval(window._timer);
798
799    // DWD - fire the window onload now, and replace it
800    if ( window.onload && ( window.onload !== window.mtInit ) ) {
801        window.onload();
802        window.onload = function() {};
803    }
804}
805
806/* for Mozilla/Opera9 */
807if (document.addEventListener) {
808    document.addEventListener("DOMContentLoaded", mtInit, false);
809}
810
811/* for Internet Explorer */
812/*@cc_on @*/
813/*@if (@_win32)
814document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
815var script = document.getElementById("__ie_onload");
816script.onreadystatechange = function() {
817    if (this.readyState == "complete") {
818        mtInit(); // call the onload handler
819    }
820};
821/*@end @*/
822
823/* for Safari */
824if (/WebKit/i.test(navigator.userAgent)) { // sniff
825    _timer = setInterval(function() {
826        if (/loaded|complete/.test(document.readyState)) {
827            mtInit(); // call the onload handler
828        }
829    }, 10);
830}
831
832/* for other browsers */
833window.onload = mtInit;
834
835// END: fast browser onload init
836
837<mt:IfRegistrationAllowed>
838/***
839 * If request contains a '#_login' or '#_logout' hash, use this to
840 * also delete the blog-side user cookie, since we're coming back from
841 * a login, logout or edit profile operation.
842 */
843var clearCookie = ( window.location.hash && window.location.hash.match( /^#_log(in|out)/ ) ) ? true : false;
844if (clearCookie) {
845    // clear any logged in state
846    mtClearUser();
847    if (RegExp.$1 == 'in')
848        mtFetchUser();
849} else {
850    <mt:Ignore>
851    /***
852     * Uncondition this call to fetch the current user state (if available)
853     * from MT upon page load if no user cookie is already present.
854     * This is okay if you have a private install, such as an Intranet;
855     * not recommended for public web sites!
856     */
857    </mt:Ignore>
858    if ( is_preview && !user )
859        mtFetchUser();
860}
861</mt:IfRegistrationAllowed>
Note: See TracBrowser for help on using the browser.