root/trunk/Vanilla/templates/vanilla+comments/javascript.mtml @ 919

Revision 919, 26.2 kB (checked in by bsmith, 16 months ago)

various updates

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