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