1 | function phpWGOpenWindow(theURL,winName,features) |
---|
2 | { |
---|
3 | img = new Image(); |
---|
4 | img.src = theURL; |
---|
5 | if (img.complete) |
---|
6 | { |
---|
7 | var width=img.width+40, height=img.height+40; |
---|
8 | } |
---|
9 | else |
---|
10 | { |
---|
11 | var width=640, height=480; |
---|
12 | img.onload = function () { newWin.resizeTo( img.width+50, img.height+100); }; |
---|
13 | } |
---|
14 | newWin = window.open(theURL,winName,features+',left=2,top=1,width=' + width + ',height=' + height); |
---|
15 | } |
---|
16 | |
---|
17 | function popuphelp(url) |
---|
18 | { |
---|
19 | window.open( url, 'dc_popup', |
---|
20 | 'alwaysRaised=yes,dependent=yes,toolbar=no,height=420,width=500,menubar=no,resizable=yes,scrollbars=yes,status=no' |
---|
21 | ); |
---|
22 | } |
---|
23 | |
---|
24 | function pwgBind(object, method) { |
---|
25 | var args = Array.prototype.slice.call(arguments,2); |
---|
26 | return function() { |
---|
27 | return method.apply(object, args.concat(Array.prototype.slice.call(arguments,0)) ); |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | function PwgWS(urlRoot) |
---|
32 | { |
---|
33 | this.urlRoot = urlRoot; |
---|
34 | this.options = { |
---|
35 | method: "GET", |
---|
36 | async: true, |
---|
37 | onFailure: null, |
---|
38 | onSuccess: null |
---|
39 | }; |
---|
40 | }; |
---|
41 | |
---|
42 | PwgWS.prototype = { |
---|
43 | callService : function(method, parameters, options) |
---|
44 | { |
---|
45 | if (options) |
---|
46 | { |
---|
47 | for (var prop in options) |
---|
48 | this.options[prop] = options[prop]; |
---|
49 | } |
---|
50 | try { this.xhr = new XMLHttpRequest();} |
---|
51 | catch(e) { |
---|
52 | try { this.xhr = new ActiveXObject('Msxml2.XMLHTTP'); } |
---|
53 | catch(e) { |
---|
54 | try { this.xhr = new ActiveXObject('Microsoft.XMLHTTP'); } |
---|
55 | catch (e){ |
---|
56 | this.error(0, "Cannot create request object"); |
---|
57 | return; |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | this.xhr.onreadystatechange = pwgBind(this, this.onStateChange); |
---|
62 | |
---|
63 | var url = this.urlRoot+"ws.php?format=json&method="+method; |
---|
64 | |
---|
65 | var body = ""; |
---|
66 | if (parameters) |
---|
67 | { |
---|
68 | for (var prop in parameters) |
---|
69 | { |
---|
70 | if ( typeof parameters[prop] == 'object' && parameters[prop]) |
---|
71 | { |
---|
72 | for (var i=0; i<parameters[prop].length; i++) |
---|
73 | body += prop+"[]="+encodeURIComponent(parameters[prop][i])+"&"; |
---|
74 | } |
---|
75 | else |
---|
76 | body += prop+"="+encodeURIComponent(parameters[prop])+"&"; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | if (this.options.method != "POST" ) |
---|
81 | { |
---|
82 | url += "&"+body; |
---|
83 | body = null; |
---|
84 | } |
---|
85 | this.xhr.open(this.options.method, url, this.options.async); |
---|
86 | if (this.options.method == "POST" ) |
---|
87 | this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); |
---|
88 | try { |
---|
89 | this.xhr.send(body); |
---|
90 | } catch(e) { |
---|
91 | this.error(0, e.message); |
---|
92 | } |
---|
93 | }, |
---|
94 | |
---|
95 | onStateChange: function() { |
---|
96 | var readyState = this.xhr.readyState; |
---|
97 | if (readyState==4) |
---|
98 | { |
---|
99 | try { |
---|
100 | this.respondToReadyState(readyState); |
---|
101 | } finally { |
---|
102 | this.cleanup(); |
---|
103 | } |
---|
104 | } |
---|
105 | }, |
---|
106 | |
---|
107 | error: function( httpCode, text ) |
---|
108 | { |
---|
109 | !this.options.onFailure || this.options.onFailure( httpCode, text); |
---|
110 | this.cleanup(); |
---|
111 | }, |
---|
112 | |
---|
113 | respondToReadyState: function(readyState) |
---|
114 | { |
---|
115 | var xhr = this.xhr; |
---|
116 | if (readyState==4 && xhr.status == 200) |
---|
117 | { |
---|
118 | var resp; |
---|
119 | try { |
---|
120 | resp = window.JSON && window.JSON.parse ? window.JSON.parse( xhr.responseText ) : (new Function("return " + xhr.responseText))(); |
---|
121 | } |
---|
122 | catch (e) { |
---|
123 | this.error( 200, e.message + '\n' + xhr.responseText.substr(0,512) ); |
---|
124 | } |
---|
125 | if (resp!=null) |
---|
126 | { |
---|
127 | if (resp.stat==null) |
---|
128 | this.error( 200, "Invalid response" ); |
---|
129 | else if (resp.stat=='ok') |
---|
130 | !this.options.onSuccess || this.options.onSuccess( resp.result ); |
---|
131 | else |
---|
132 | this.error( 200, resp.err + " " + resp.message); |
---|
133 | } |
---|
134 | } |
---|
135 | if (readyState==4 && xhr.status != 200) |
---|
136 | this.error( xhr.status, xhr.statusText ); |
---|
137 | }, |
---|
138 | |
---|
139 | cleanup: function() |
---|
140 | { |
---|
141 | if (this.xhr) this.xhr.onreadystatechange = null; |
---|
142 | this.xhr = null; |
---|
143 | this.options.onFailure = this.options.onSuccess = null; |
---|
144 | }, |
---|
145 | |
---|
146 | xhr: null |
---|
147 | } |
---|
148 | |
---|
149 | function pwgAddEventListener(elem, evt, fn) |
---|
150 | { |
---|
151 | if (window.addEventListener) |
---|
152 | elem.addEventListener(evt, fn, false); |
---|
153 | else |
---|
154 | elem.attachEvent('on'+evt, fn); |
---|
155 | } |
---|