1/**
2 * Javascript functionality for the discussion plugin
3 */
4
5/**
6 * Check if a field is blank
7 */
8function isBlank(s){
9  if ((s === null) || (s.length === 0)){
10    return true;
11  }
12
13  for (var i = 0; i < s.length; i++){
14    var c = s.charAt(i);
15	  if ((c != ' ') && (c != '\n') && (c != '\t')){
16	    return false;
17    }
18  }
19  return true;
20}
21
22/**
23 * Validate an input field
24 */
25function validate(form){
26    if(!form) return;
27
28    if (isBlank(form.name.value)){
29        form.name.focus();
30        form.name.style.backgroundColor = '#fcc';
31        return false;
32    } else {
33        form.name.style.backgroundColor = '#fff';
34    }
35    if (isBlank(form.mail.value) || form.mail.value.indexOf("@") == -1){
36        form.mail.focus();
37        form.mail.style.backgroundColor = '#fcc';
38        return false;
39    } else {
40        form.mail.style.backgroundColor = '#fff';
41    }
42    if (isBlank(form.text.value)){
43        form.text.focus();
44        form.text.style.borderColor = '#fcc';
45        return false;
46    }
47}
48
49/**
50 * AJAX preview
51 *
52 * @author Michael Klier <chi@chimeric.de>
53 */
54function discussion_ajax_preview() {
55    var $textarea = jQuery('#discussion__comment_text');
56    var comment = $textarea.val();
57
58    var $preview = jQuery('#discussion__comment_preview');
59    if (!comment) {
60        $preview.hide();
61        return;
62    }
63    $preview.html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" />');
64    $preview.show();
65
66    jQuery.post(DOKU_BASE + 'lib/exe/ajax.php',
67        {
68            'call': 'discussion_preview',
69            'comment': comment
70        },
71        function (data) {
72            if (data === '') {
73                $preview.hide();
74                return;
75            }
76            $preview.html(data);
77            $preview.show();
78            $preview.css('visibility', 'visible');
79            $preview.css('display', 'inline');
80        }, 'html');
81}
82
83jQuery(function() {
84    // init toolbar
85    if(typeof window.initToolbar == 'function') {
86        initToolbar("discussion__comment_toolbar", "discussion__comment_text", toolbar);
87    }
88
89    // init preview button
90    jQuery('#discussion__btn_preview').click(discussion_ajax_preview);
91
92    // init field check
93    jQuery('#discussion__comment_form').submit(function() { return validate(this); });
94
95    // toggle section visibility
96    jQuery('#discussion__btn_toggle_visibility').click(function() {
97        jQuery('#comment_wrapper').toggle();
98    });
99});
100