Add error and info popups
[infodrom.org/service.infodrom.org] / src / infodrom.js
1 function ajax_request(funcname, params, callback, error_callback)
2 {
3     if (params === null)
4         params = 'func='+funcname;
5     else if (typeof params == 'object')
6         params['func'] = funcname
7     else if (typeof params == 'string')
8         params = 'func='+funcname+'&'+params
9     else
10         return false;
11
12     $.post('/ajax.php', params,
13            function(data){
14                if (!data.status) {
15                    if (typeof data.error == 'string')
16                        var error = data.error;
17                    else
18                        var error = 'An error occurred'
19                    alert(error);
20                    if (typeof error_callback == 'function')
21                        error_callback(data);
22                    return;
23                }
24                if (typeof callback == 'function')
25                    callback(data);
26            });
27 }
28
29 function show_message(text, timeout)
30 {
31     if (typeof timeout == 'undefined') timeout = 3;
32
33     var div = $('#message_div');
34     if (!div.length) {
35         div = $('<div>');
36         div.attr('id', 'message_div').css('z-index','1000');
37         div.hide();
38         $(document.body).append(div);
39     }
40
41     div.text(text);
42     div.center();
43     div.show();
44
45     window.setTimeout(hide_message,timeout*1000);
46 }
47
48 function hide_message(text)
49 {
50     $('#message_div').hide();
51 }
52
53 function show_error(text, timeout)
54 {
55     var div = $('#error_div');
56     if (!div.length) {
57         div = $('<div>');
58         div.attr('id', 'error_div').css('z-index','1000');
59         div.hide();
60         div.append($('<p ><img src="/pix/close.gif" title="close" onclick="return hide_error()"/></p>'));
61         div.append($('<div />'));
62         $(document.body).append(div);
63     }
64
65     div.find('div').html(text);
66     div.center();
67     div.show();
68
69     if (typeof timeout != 'undefined')
70         window.setTimeout(hide_error,timeout*1000);
71 }
72
73 function hide_error(text)
74 {
75     $('#error_div').hide();
76     return false;
77 }