root/trunk/default_templates/javascript.mtml @ 3531

Revision 3531, 25.9 kB (checked in by fumiakiy, 9 months ago)

Merged sockfish to trunk. "svn merge -r3114:3527 http://code.sixapart.com/svn/movabletype/branches/sockfish/ ."

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