root/branches/release-41/default_templates/javascript.mtml @ 2662

Revision 2662, 25.7 kB (checked in by bsmith, 17 months ago)

bugzid:80398 - updaing html semantics

  • 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;
310<mt:IfBlog>
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</mt:IfBlog>
328
329<mt:Ignore>
330/***
331 * Called when the 'Remember me' checkbox is changed. If the checkbox
332 * is cleared, the cached user cookie is immediately cleared.
333 */
334</mt:Ignore>
335function mtRememberMeOnClick(b) {
336    if (!b.checked)
337        mtClearUser(b.form);
338    return true;
339}
340
341<mt:Ignore>
342/***
343 * Called when comment form is sent.
344 * Required parameter: Form DOM object of comment form.
345 * If form has a 'bakecookie' member, it will be used to signal
346 * storing the anonymous commenter information to a cookie.
347 * If form has a 'armor' member, it will be used to store
348 * a token that is checked by the comment script.
349 */
350</mt:Ignore>
351<mt:IfBlog>
352var mtRequestSubmitted = false;
353function mtCommentOnSubmit(f) {
354    if (!mtRequestSubmitted) {
355        mtRequestSubmitted = true;
356
357        if (f.armor)
358            f.armor.value = '<$mt:BlogSitePath encode_sha1="1"$>';
359        if (f.bakecookie && f.bakecookie.checked)
360            mtSaveUser(f);
361
362        // disable submit buttons
363        if (f.preview_button) f.preview_button.disabled = true;
364        if (f.post) f.post.disabled = true;
365
366        var u = mtGetUser();
367        if ( !is_preview && ( u && u.is_authenticated ) ) {
368            // validate session; then submit
369            mtFetchedUser = false;
370            mtFetchUser('mtCommentSessionVerify');
371            return false;
372        }
373
374        return true;
375    }
376    return false;
377}
378
379function mtCommentSessionVerify(app_user) {
380    var u = mtGetUser();
381    var f = document['comments_form'];
382    if ( u && app_user && app_user.sid && ( u.sid == app_user.sid ) ) {
383        f.submit();
384    } else {
385        alert('<__trans phrase="Your session has expired. Please sign in again to comment." escape="js">');
386        mtClearUser();
387        mtFireEvent('usersignin');
388<mt:IfRegistrationRequired>
389        mtShow('comments-form');
390        mtHide('comments-open-footer');
391</mt:IfRegistrationRequired>
392    }
393}
394
395function mtUserOnLoad() {
396    var u = mtGetUser();
397
398    // if the user is authenticated, hide the 'anonymous' fields
399    // and any captcha input if already shown
400    if ( document.getElementById('comments-form')) {
401        if ( u && u.is_authenticated ) {
402            mtShow('comments-form');
403            mtHide('comments-open-data');
404            if (mtCaptchaVisible)
405                mtHide('comments-open-captcha');
406        } else {
407<mt:IfRegistrationRequired>
408            mtHide('comments-form');
409</mt:IfRegistrationRequired>
410        }
411        if ( u && u.is_banned )
412            mtHide('comments-form');
413
414        // if we're previewing a comment, make sure the captcha
415        // field is visible
416        if (is_preview)
417            mtShowCaptcha();
418        else
419            mtShowGreeting();
420
421        // populate anonymous comment fields if user is cookied as anonymous
422        var cf = document['comments_form'];
423        if (cf) {
424            if (u && u.is_anonymous) {
425                if (u.email) cf.email.value = u.email;
426                if (u.name) cf.author.value = u.name;
427                if (u.url) cf.url.value = u.url;
428                if (cf.bakecookie)
429                    cf.bakecookie.checked = u.name || u.email;
430            } else {
431                if (u && u.sid && cf.sid)
432                    cf.sid.value = u.sid;
433            }
434            if (cf.post.disabled)
435                cf.post.disabled = false;
436            if (cf.preview_button.disabled)
437                cf.preview_button.disabled = false;
438            mtRequestSubmitted = false;
439        }
440    }
441}
442</mt:IfBlog>
443
444<mt:Ignore>
445/***
446 * Called when an entry archive page is loaded.
447 * This routine controls which elements of the comment form are shown
448 * or hidden, depending on commenter type and blog configuration.
449 */
450</mt:Ignore>
451<mt:IfBlog>
452function mtEntryOnLoad() {
453    <mt:Unless tag="IfPingsAccepted">mtHide('trackbacks-info');</mt:Unless>
454    <mt:Unless tag="IfCommentsAccepted">mtHide('comments-open');</mt:Unless>
455    mtFireEvent('usersignin');
456}
457
458function mtEntryOnUnload() {
459    if (mtRequestSubmitted) {
460        var cf = document['comments_form'];
461        if (cf) {
462            if (cf.post && cf.post.disabled)
463                cf.post.disabled = false;
464            if (cf.preview_button && cf.preview_button.disabled)
465                cf.preview_button.disabled = false;
466        }
467        mtRequestSubmitted = false;
468    }
469    return true;
470}
471
472mtAttachEvent('usersignin', mtUserOnLoad);
473</mt:IfBlog>
474
475<mt:Ignore>
476/***
477 * Handles the action of the "Sign in" link. First clears any existing
478 * user cookie, then directs to the MT comment script to sign the user in.
479 */
480</mt:Ignore>
481function mtSignIn() {
482    var doc_url = document.URL;
483    doc_url = doc_url.replace(/#.+/, '');
484    var url = '<$mt:SignInLink$>';
485    if (is_preview) {
486        if ( document['comments_form'] ) {
487            var entry_id = document['comments_form'].entry_id.value;
488            url += '&entry_id=' + entry_id;
489        } else {
490            url += '&return_url=<$mt:BlogURL encode_url="1"$>';
491        }
492    } else {
493        url += '&return_url=' + encodeURIComponent(doc_url);
494    }
495    mtClearUser();
496    location.href = url;
497}
498
499function mtSignInOnClick(sign_in_element) {
500    var el;
501    if (sign_in_element) {
502        // display throbber
503        el = document.getElementById(sign_in_element);
504        if (!el)  // legacy MT 4.x element id
505            el = document.getElementById('comment-form-external-auth');
506    }
507    if (el)
508        el.innerHTML = '<__trans phrase="Signing in..." escape="js"> <span class="status-indicator">&nbsp;</span>';
509
510    mtClearUser(); // clear any 'anonymous' user cookie to allow sign in
511    mtFetchUser('mtSetUserOrLogin');
512    return false;
513}
514
515function mtSetUserOrLogin(u) {
516    if (u && u.is_authenticated) {
517        mtSetUser(u);
518    } else {
519        // user really isn't logged in; so let's do this!
520        mtSignIn();
521    }
522}
523
524<mt:Ignore>
525/***
526 * Handles sign out from the web site.
527 * First clears any existing user cookie, then direts to the MT comment
528 * script to sign the user out.
529 */
530</mt:Ignore>
531function mtSignOut(entry_id) {
532    mtClearUser();
533    var doc_url = document.URL;
534    doc_url = doc_url.replace(/#.+/, '');
535    var url = '<$mt:SignOutLink$>';
536    if (is_preview) {
537        if ( document['comments_form'] ) {
538            var entry_id = document['comments_form'].entry_id.value;
539            url += '&entry_id=' + entry_id;
540        } else {
541            url += '&return_url=<$mt:BlogURL encode_url="1"$>';
542        }
543    } else {
544        url += '&return_url=' + encodeURIComponent(doc_url);
545    }
546    location.href = url;
547}
548
549<mt:Ignore>
550/***
551 * Handles the action of the "Sign out" link.
552 */
553</mt:Ignore>
554function mtSignOutOnClick() {
555    mtSignOut();
556    return false;
557}
558
559<mt:Ignore>
560/***
561 * Handles the display of the greeting message, depending on what kind of
562 * user is logged in and blog comment policy.
563 */
564</mt:Ignore>
565<mt:IfBlog>
566function mtShowGreeting() {
567<mt:IfRegistrationAllowed>
568    var reg_reqd = <mt:IfRegistrationRequired>true<mt:Else>false</mt:IfRegistrationRequired>;
569
570    var cf = document['comments_form'];
571    if (!cf) return;
572
573    var el = document.getElementById('comment-greeting');
574    if (!el)  // legacy MT 4.x element id
575        el = document.getElementById('comment-form-external-auth');
576    if (!el) return;
577
578    var eid = cf.entry_id;
579    var entry_id;
580    if (eid) entry_id = eid.value;
581
582    var phrase;
583    var u = mtGetUser();
584
585    if ( u && u.is_authenticated ) {
586        if ( u.is_banned ) {
587            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">';
588        } else {
589            var user_link;
590            if ( u.is_author ) {
591                user_link = '<a href="<$mt:CGIPath$><$mt:CommentScript$>?__mode=edit_profile&return_url=' + encodeURIComponent( location.href );
592                user_link += '">' + u.name + '</a>';
593            } else {
594                // registered user, but not a user with posting rights
595                if (u.url)
596                    user_link = '<a href="' + u.url + '">' + u.name + '</a>';
597                else
598                    user_link = u.name;
599            }
600            // TBD: supplement phrase with userpic if one is available.
601            phrase = '<__trans phrase="Thanks for signing in, __NAME__. ([_1]sign out[_2])" params="<a href="javascript:void(0)" onclick="return mtSignOutOnClick();">%%</a>" escape="js">';
602            phrase = phrase.replace(/__NAME__/, user_link);
603        }
604    } else {
605        if (reg_reqd) {
606            phrase = '<__trans phrase="[_1]Sign in[_2] to comment." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick('comment-greeting')">%%</a>" escape="js">';
607        } else {
608            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">';
609        }
610    }
611    el.innerHTML = phrase;
612<mt:Else>
613    mtShowCaptcha();
614</mt:IfRegistrationAllowed>
615}
616</mt:IfBlog>
617
618<mt:Ignore>
619/***
620 * Handles the action of the 'Reply' links.
621 */
622</mt:Ignore>
623function mtReplyCommentOnClick(parent_id, author) {
624    mtShow('comment-form-reply');
625
626    var checkbox = document.getElementById('comment-reply');
627    var label = document.getElementById('comment-reply-label');
628    var text = document.getElementById('comment-text');
629
630    // Populate label with new values
631    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">';
632    reply_text = reply_text.replace(/__PARENT__/, parent_id);
633    reply_text = reply_text.replace(/__AUTHOR__/, author);
634    label.innerHTML = reply_text;
635
636    checkbox.value = parent_id;
637    checkbox.checked = true;
638    try {
639        // text field may be hidden
640        text.focus();
641    } catch(e) {
642    }
643
644    mtSetCommentParentID();
645}
646
647<mt:Ignore>
648/***
649 * Sets the parent comment ID when replying to a comment.
650 */
651</mt:Ignore>
652function mtSetCommentParentID() {
653    var checkbox = document.getElementById('comment-reply');
654    var parent_id_field = document.getElementById('comment-parent-id');
655    if (!checkbox || !parent_id_field) return;
656
657    var pid = 0;
658    if (checkbox.checked == true)
659        pid = checkbox.value;
660    parent_id_field.value = pid;
661}
662
663<mt:Ignore>
664/***
665 * Persists a copy of the current user cookie into the browser cookie stash.
666 */
667</mt:Ignore>
668function mtSaveUser(f) {
669    // We can't reliably store the user cookie during a preview.
670    if (is_preview) return;
671
672    var u = mtGetUser();
673
674    if (f && (!u || u.is_anonymous)) {
675        if ( !u ) {
676            u = {};
677            u.is_authenticated = false;
678            u.can_comment = true;
679            u.is_author = false;
680            u.is_banned = false;
681            u.is_anonymous = true;
682            u.is_trusted = false;
683        }
684        if (f.author != undefined) u.name = f.author.value;
685        if (f.email != undefined) u.email = f.email.value;
686        if (f.url != undefined) u.url = f.url.value;
687    }
688
689    if (!u) return;
690
691    var cache_period = mtCookieTimeout * 1000;
692
693    // cache anonymous user info for a long period if the
694    // user has requested to be remembered
695    if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
696        cache_period = 365 * 24 * 60 * 60 * 1000;
697
698    var now = new Date();
699    mtFixDate(now);
700    now.setTime(now.getTime() + cache_period);
701
702    var cmtcookie = mtBakeUserCookie(u);
703    mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
704        location.protocol == 'https:');
705}
706
707<mt:Ignore>
708/***
709 * Clears the blog-side user cookie.
710 */
711</mt:Ignore>
712function mtClearUser() {
713    user = null;
714    mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain,
715        location.protocol == 'https:');
716}
717
718<mt:Ignore>
719/***
720 * Sets a browser cookie.
721 */
722</mt:Ignore>
723function mtSetCookie(name, value, expires, path, domain, secure) {
724    if (domain && domain.match(/^\.?localhost$/))
725        domain = null;
726    var curCookie = name + "=" + escape(value) +
727        (expires ? "; expires=" + expires.toGMTString() : "") +
728        (path ? "; path=" + path : "") +
729        (domain ? "; domain=" + domain : "") +
730        (secure ? "; secure" : "");
731    document.cookie = curCookie;
732}
733
734<mt:Ignore>
735/***
736 * Retrieves a browser cookie.
737 */
738</mt:Ignore>
739function mtGetCookie(name) {
740    var prefix = name + '=';
741    var c = document.cookie;
742    var cookieStartIndex = c.indexOf(prefix);
743    if (cookieStartIndex == -1)
744        return '';
745    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
746    if (cookieEndIndex == -1)
747        cookieEndIndex = c.length;
748    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
749}
750
751<mt:Ignore>
752/***
753 * Deletes a browser cookie.
754 */
755</mt:Ignore>
756function mtDeleteCookie(name, path, domain, secure) {
757    if (mtGetCookie(name)) {
758        if (domain && domain.match(/^\.?localhost$/))
759            domain = null;
760        document.cookie = name + "=" +
761            (path ? "; path=" + path : "") +
762            (domain ? "; domain=" + domain : "") +
763            (secure ? "; secure" : "") +
764            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
765    }
766}
767
768function mtFixDate(date) {
769    var skew = (new Date(0)).getTime();
770    if (skew > 0)
771        date.setTime(date.getTime() - skew);
772}
773
774<mt:Ignore>
775/***
776 * Returns a XMLHttpRequest object (for Ajax operations).
777 */
778</mt:Ignore>
779function mtGetXmlHttp() {
780    if ( !window.XMLHttpRequest ) {
781        window.XMLHttpRequest = function() {
782            var types = [
783                "Microsoft.XMLHTTP",
784                "MSXML2.XMLHTTP.5.0",
785                "MSXML2.XMLHTTP.4.0",
786                "MSXML2.XMLHTTP.3.0",
787                "MSXML2.XMLHTTP"
788            ];
789
790            for ( var i = 0; i < types.length; i++ ) {
791                try {
792                    return new ActiveXObject( types[ i ] );
793                } catch( e ) {}
794            }
795
796            return undefined;
797        };
798    }
799    if ( window.XMLHttpRequest )
800        return new XMLHttpRequest();
801}
802
803// BEGIN: fast browser onload init
804// Modifications by David Davis, DWD
805// Dean Edwards/Matthias Miller/John Resig
806// http://dean.edwards.name/weblog/2006/06/again/?full#comment5338
807
808function mtInit() {
809    // quit if this function has already been called
810    if (arguments.callee.done) return;
811
812    // flag this function so we don't do the same thing twice
813    arguments.callee.done = true;
814
815    // kill the timer
816    // DWD - check against window
817    if ( window._timer ) clearInterval(window._timer);
818
819    // DWD - fire the window onload now, and replace it
820    if ( window.onload && ( window.onload !== window.mtInit ) ) {
821        window.onload();
822        window.onload = function() {};
823    }
824}
825
826/* for Mozilla/Opera9 */
827if (document.addEventListener) {
828    document.addEventListener("DOMContentLoaded", mtInit, false);
829}
830
831/* for Internet Explorer */
832/*@cc_on @*/
833/*@if (@_win32)
834document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
835var script = document.getElementById("__ie_onload");
836script.onreadystatechange = function() {
837    if (this.readyState == "complete") {
838        mtInit(); // call the onload handler
839    }
840};
841/*@end @*/
842
843/* for Safari */
844if (/WebKit/i.test(navigator.userAgent)) { // sniff
845    _timer = setInterval(function() {
846        if (/loaded|complete/.test(document.readyState)) {
847            mtInit(); // call the onload handler
848        }
849    }, 10);
850}
851
852/* for other browsers */
853window.onload = mtInit;
854
855// END: fast browser onload init
856
857<mt:IfBlog>
858<mt:IfRegistrationAllowed>
859/***
860 * If request contains a '#_login' or '#_logout' hash, use this to
861 * also delete the blog-side user cookie, since we're coming back from
862 * a login, logout or edit profile operation.
863 */
864var clearCookie = ( window.location.hash && window.location.hash.match( /^#_log(in|out)/ ) ) ? true : false;
865if (clearCookie) {
866    // clear any logged in state
867    mtClearUser();
868    if (RegExp.$1 == 'in')
869        mtFetchUser();
870} else {
871    <mt:Ignore>
872    /***
873     * Uncondition this call to fetch the current user state (if available)
874     * from MT upon page load if no user cookie is already present.
875     * This is okay if you have a private install, such as an Intranet;
876     * not recommended for public web sites!
877     */
878    </mt:Ignore>
879    if ( is_preview && !user )
880        mtFetchUser();
881}
882</mt:IfRegistrationAllowed>
883</mt:IfBlog>
Note: See TracBrowser for help on using the browser.