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