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

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