fix the progressbar bug with multiple progressbar forms on a page
[freeside.git] / httemplate / elements / xmlhttp.html
1 <%
2   my ( %opt ) = @_;
3
4   my $url = $opt{'url'};
5   my $method = exists($opt{'method'}) ? $opt{'method'} : 'GET';
6   #my @subs = @{ $opt{'subs'};
7   my $key = exists($opt{'key'}) ? $opt{'key'} : '';
8
9   $url .= ( ($url =~ /\?/) ? '&' : '?' )
10     if $method eq 'GET';
11
12 %>
13
14 <SCRIPT TYPE="text/javascript">
15
16   function rs_init_object() {
17     var A;
18     try {
19       A=new ActiveXObject("Msxml2.XMLHTTP");
20     } catch (e) {
21       try {
22         A=new ActiveXObject("Microsoft.XMLHTTP");
23       } catch (oc) {
24         A=null;
25       }
26     }
27     if(!A && typeof XMLHttpRequest != "undefined")
28       A = new XMLHttpRequest();
29     if (!A)
30       alert("Can't create XMLHttpRequest object");
31     return A;
32
33   }
34
35   <% foreach my $func ( @{$opt{'subs'}} ) { 
36
37        my $furl = $url;
38        $furl =~ s/\"/\\\\\"/; #javascript escape
39
40   %>
41
42     function <%=$key%><%=$func%>() {
43         // count args; build URL
44         var url = "<%=$furl%>";
45         var a = <%=$key%><%=$func%>.arguments;
46
47         var args;
48         var len;
49         var content = 'sub=<%= uri_escape($func) %>';
50         if ( a && typeof a  == 'object'  && a[0].constructor == Array ) {
51             args = a[0];
52             len = args.length
53         } else {
54             args = a;
55             len = args.length - 1;
56         }
57         for (var i = 0; i < len; i++) 
58             content = content + "&arg=" + escape(args[i]);
59         content = content.replace( /[+]/g, '%2B'); // fix unescaped plus signs 
60
61         if ( '<%=$method%>' == 'GET' ) {
62           url = url + content;
63         }
64
65         //alert('<%=$method%> ' + url);
66
67         var xmlhttp = rs_init_object();
68         xmlhttp.open("<%=$method%>", url, true);
69
70         xmlhttp.onreadystatechange = function() {
71             if (xmlhttp.readyState != 4) 
72                 return;
73
74             if (xmlhttp.status != 200) {
75               alert(xmlhttp.status + " status connecting to " + url);
76             } else {
77               var data = xmlhttp.responseText;
78               //alert('received response: ' + data);
79               a[a.length-1](data);
80               if ( data.indexOf("<b>System error</b>") > -1 ) {
81                 var w;
82                 if ( w = window.open("about:blank") ) {
83                   w.document.write(data);
84                 } else {
85                   // popup blocking?  should use an overlib popup instead 
86                   alert("Error popup disabled; try disabling popup blocking to see");
87                 }
88               }
89             }
90         }
91
92         if ( '<%=$method%>' == 'POST' ) {
93
94           xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
95           xmlhttp.send(content);
96
97         } else {
98
99           xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
100           xmlhttp.send(null);
101
102         }
103
104         //rs_debug("x_$func_name url = " + url);
105         //rs_debug("x_$func_name waiting..");
106     }
107
108   <% } %>
109
110 </SCRIPT>