1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
var ajaxrequestnum=0;
var boundarylength=30;
// Returns a 30-character random alphanumeric string
function makemultipartboundary () {
var boundary='';
var chars='abcdefghijklmnopqrstuvwxyz0123456789';
var i;
while (boundary.length < boundarylength) {
i=Math.floor(Math.random()*chars.length);
boundary+=chars.substring(i,i+1);
}
return boundary;
}
// Given a two-dimensional array where the first
function makepostdata(data) {
boundary=makemultipartboundary();
output='';
data[data.length]=['ajax', 'yes'];
for(var i=0; i<data.length; i++) {
output+='--'+boundary+"\r\n"+'Content-disposition: form-data; name="'+data[i][0]+'"'+"\r\n\r\n"+data[i][1]+"\r\n";
}
output+="--"+boundary+"--\r\n";
return output;
}
// Do a full AJAX request
function doajaxrequest(page,data,callback) {
requestnum=ajaxrequestnum++;
content=makepostdata(data);
<?php if ($S['conf']['debug']) { ?>
debug('ajax:'+requestnum,'Sending to '+url(page)+':<br/>'+content);
<?php } ?>
httpobj=GetXmlHttpObject();
httpobj.open("POST", url(page), true);
httpobj.setRequestHeader("Content-type", "multipart/form-data; boundary="+content.substring(2,boundarylength+2));
httpobj.setRequestHeader("Content-length", content.length);
httpobj.setRequestHeader("Connection", "close");
httpobj.onreadystatechange=function () {
if (this.readyState==4 || this.readyState=="complete") {
if (this.responseXML != null && this.responseXML.documentElement.nodeName==page.replace('/','')+'response') {
xml=this.responseXML.documentElement;
<?php if ($S['conf']['debug']) { ?>
debug('ajax:'+requestnum, 'Got response:<br/><pre>'+this.responseText.replace(/</g, '<').replace(/>/g, '>')+'</pre>');
if (debugenabled) {
for (var i=0; i<xml.getElementsByTagName('debug').length; i++) {
row=xml.getElementsByTagName('debug')[i];
var type=row.getElementsByTagName('type').length!=0?row.getElementsByTagName('type')[0].childNodes[0].nodeValue:null;
text=row.getElementsByTagName('text')[0].childNodes[0].nodeValue;
if (type == null) {
debug(page, text);
} else {
debug(page+":"+type, text);
}
}
}
<?php } ?>
callback(xml);
<?php if ($S['conf']['debug']) { ?>
} else {
if (this.responseText.length == 0) {
debug('ajax'+requestnum, 'zero-length response received');
} else {
debug('ajax'+requestnum, 'Non-XML response received:<br/><pre>'+this.responseText.replace(/</g, '<').replace(/>/g, '>')+'</pre>');
}
<?php } ?>
}
}
};
httpobj.send(content);
return httpobj;
}
// Takes an XML node and returns the equivalent HTML node
// (so you can take stuff from the XML DOM in an AJAX response and insert it directly into the document)
function XMLtoHTML(xml, notrecursive) {
switch(xml.nodeType) {
case 1: // Element Node
var i;
var html=document.createElement(xml.nodeName);
for (i=0; i<xml.attributes.length; i++) {
attr=xml.attributes.item(i);
if (attr.name == 'class') {
html.className=attr.value;
} else if (attr.name.match(/^on/)) {
var handlerfunc=new Function('event', attr.value);
<?php if ($S['conf']['debug']) { ?>
debug('XMLtoHTML', 'setting '+xml.nodeName+(xml.id?'#'+xml.id:'')+'.'+attr.name+':'+"\r\n"+handlerfunc);
<?php } ?>
html[attr.name]=handlerfunc;
} else {
<?php if ($S['conf']['debug']) { ?>
debug('XMLtoHTML', 'setting '+xml.nodeName+(xml.attributes.getNamedItem('id')?'#'+xml.attributes.getNamedItem('id').value:'')+'.'+attr.name+'='+attr.value);
<?php } ?>
html.setAttribute(attr.name, attr.value);
}
}
if (!notrecursive) {
for (i=0; i<xml.childNodes.length; i++) {
html.appendChild(XMLtoHTML(xml.childNodes[i]));
}
}
return html;
case 3: // Text Node
return document.createTextNode(xml.nodeValue);
}
}
function FormToArray(form) {
var data=[];
for (var i=0; i<form.elements.length; i++) {
element=form.elements[i];
switch(element.tagName) {
case 'INPUT':
if ((element.type == 'radio' || element.type == 'checkbox') && !element.checked) {
break;
} else if (element.type == 'button' || element.type == 'submit' || element.type == 'reset') {
break;
}
case 'SELECT':
case 'TEXTAREA':
data[data.length]=[element.name, element.value]
}
}
return data;
}
|