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