PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/Quản lý công ty du lịch khách sạn PHP/sgncnew/installation1/includes/js/xajax_uncompressed.js

https://gitlab.com/phamngsinh/baitaplon_sinhvien
JavaScript | 551 lines | 494 code | 37 blank | 20 comment | 152 complexity | 24ccbf09ddbcb2f819dab721d120cc58 MD5 | raw file
  1. function Xajax()
  2. {
  3. if (xajaxDebug) this.DebugMessage = function(text) { alert("Xajax Debug:\n " + text) };
  4. this.workId = 'xajaxWork'+ new Date().getTime();
  5. this.depth = 0;
  6. //Get the XMLHttpRequest Object
  7. this.getRequestObject = function()
  8. {
  9. if (xajaxDebug) this.DebugMessage("Initializing Request Object..");
  10. var req;
  11. try
  12. {
  13. req=new ActiveXObject("Msxml2.XMLHTTP");
  14. }
  15. catch (e)
  16. {
  17. try
  18. {
  19. req=new ActiveXObject("Microsoft.XMLHTTP");
  20. }
  21. catch (e2)
  22. {
  23. req=null;
  24. }
  25. }
  26. if(!req && typeof XMLHttpRequest != "undefined")
  27. req = new XMLHttpRequest();
  28. if (xajaxDebug) {
  29. if (!req) this.DebugMessage("Request Object Instantiation failed.");
  30. }
  31. return req;
  32. }
  33. // xajax.$() is shorthand for document.getElementById()
  34. this.$ = function(sId)
  35. {
  36. if (!sId) {
  37. return null;
  38. }
  39. var returnObj = document.getElementById(sId);
  40. if (xajaxDebug && !returnObj && sId != this.workId) {
  41. this.DebugMessage("Element with the id \"" + sId + "\" not found.");
  42. }
  43. return returnObj;
  44. }
  45. // xajax.include(sFileName) dynamically includes an external javascript file
  46. this.include = function(sFileName)
  47. {
  48. var objHead = document.getElementsByTagName('head');
  49. var objScript = document.createElement('script');
  50. objScript.type = 'text/javascript';
  51. objScript.src = sFileName;
  52. objHead[0].appendChild(objScript);
  53. }
  54. // xajax.addHandler adds an event handler to an element
  55. this.addHandler = function(sElementId, sEvent, sFunctionName)
  56. {
  57. if (window.addEventListener)
  58. {
  59. eval("this.$('"+sElementId+"').addEventListener('"+sEvent+"',"+sFunctionName+",false);");
  60. }
  61. else
  62. {
  63. eval("this.$('"+sElementId+"').attachEvent('on"+sEvent+"',"+sFunctionName+",false);");
  64. }
  65. }
  66. // xajax.removeHandler removes an event handler from an element
  67. this.removeHandler = function(sElementId, sEvent, sFunctionName)
  68. {
  69. if (window.addEventListener)
  70. {
  71. eval("this.$('"+sElementId+"').removeEventListener('"+sEvent+"',"+sFunctionName+",false);");
  72. }
  73. else
  74. {
  75. eval("this.$('"+sElementId+"').detachEvent('on"+sEvent+"',"+sFunctionName+",false);");
  76. }
  77. }
  78. // xajax.create creates a new child node under a parent
  79. this.create = function(sParentId, sTag, sId)
  80. {
  81. var objParent = this.$(sParentId);
  82. objElement = document.createElement(sTag);
  83. objElement.setAttribute('id',sId);
  84. objParent.appendChild(objElement);
  85. }
  86. // xajax.insert inserts a new node before another node
  87. this.insert = function(sBeforeId, sTag, sId)
  88. {
  89. var objSibling = this.$(sBeforeId);
  90. objElement = document.createElement(sTag);
  91. objElement.setAttribute('id',sId);
  92. objSibling.parentNode.insertBefore(objElement, objSibling);
  93. }
  94. this.getInput = function(sType, sName, sId)
  95. {
  96. var Obj;
  97. if (sType == "radio" && !window.addEventListener)
  98. {
  99. Obj = document.createElement('<input type="radio" id="'+sId+'" name="'+sName+'">');
  100. }
  101. else
  102. {
  103. Obj = document.createElement('input');
  104. Obj.setAttribute('type',sType);
  105. Obj.setAttribute('name',sName);
  106. Obj.setAttribute('id',sId);
  107. }
  108. return Obj;
  109. }
  110. // xajax.createInput creates a new input node under a parent
  111. this.createInput = function(sParentId, sType, sName, sId)
  112. {
  113. var objParent = this.$(sParentId);
  114. var objElement = this.getInput(sType, sName, sId);
  115. objParent.appendChild(objElement);
  116. }
  117. // xajax.insertInput creates a new input node before another node
  118. this.insertInput = function(sBeforeId, sType, sName, sId)
  119. {
  120. var objSibling = this.$(sBeforeId);
  121. var objElement = this.getInput(sType, sName, sId);
  122. objSibling.parentNode.insertBefore(objElement, objSibling);
  123. }
  124. // xajax.remove deletes an element
  125. this.remove = function(sId)
  126. {
  127. objElement = this.$(sId);
  128. if (objElement.parentNode && objElement.parentNode.removeChild)
  129. {
  130. objElement.parentNode.removeChild(objElement);
  131. }
  132. }
  133. //xajax.replace searches for text in an attribute of an element and replaces it
  134. //with a different text
  135. this.replace = function(sId,sAttribute,sSearch,sReplace)
  136. {
  137. var bFunction = false;
  138. if (sAttribute == "innerHTML")
  139. sSearch = this.getBrowserHTML(sSearch);
  140. eval("var txt=document.getElementById('"+sId+"')."+sAttribute);
  141. if (typeof txt == "function")
  142. {
  143. txt = txt.toString();
  144. bFunction = true;
  145. }
  146. if (txt.indexOf(sSearch)>-1)
  147. {
  148. var newTxt = '';
  149. while (txt.indexOf(sSearch) > -1)
  150. {
  151. x = txt.indexOf(sSearch)+sSearch.length+1;
  152. newTxt += txt.substr(0,x).replace(sSearch,sReplace);
  153. txt = txt.substr(x,txt.length-x);
  154. }
  155. newTxt += txt;
  156. if (bFunction)
  157. {
  158. eval("newTxt =" + newTxt);
  159. eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');
  160. }
  161. else if (this.willChange(sId,sAttribute,newTxt))
  162. {
  163. eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');
  164. }
  165. }
  166. }
  167. // xajax.getFormValues() builds a query string XML message from the elements of a form object
  168. this.getFormValues = function(frm)
  169. {
  170. var objForm;
  171. var submitDisabledElements = false;
  172. if (arguments.length > 1 && arguments[1] == true)
  173. submitDisabledElements = true;
  174. if (typeof(frm) == "string")
  175. objForm = this.$(frm);
  176. else
  177. objForm = frm;
  178. var sXml = "<xjxquery><q>";
  179. if (objForm && objForm.tagName == 'FORM')
  180. {
  181. var formElements = objForm.elements;
  182. for( var i=0; i < formElements.length; i++)
  183. {
  184. if (formElements[i].type && (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && formElements[i].checked == false)
  185. continue;
  186. if (formElements[i].disabled && formElements[i].disabled == true && submitDisabledElements == false) continue;
  187. var name = formElements[i].name;
  188. if (name)
  189. {
  190. if (sXml != '<xjxquery><q>')
  191. sXml += '&';
  192. if(formElements[i].type=='select-multiple')
  193. {
  194. for (var j = 0; j < formElements[i].length; j++)
  195. {
  196. if (formElements[i].options[j].selected == true) sXml += name+"="+encodeURIComponent(formElements[i].options[j].value)+"&";
  197. }
  198. }
  199. else
  200. {
  201. sXml += name+"="+encodeURIComponent(formElements[i].value);
  202. }
  203. }
  204. }
  205. }
  206. sXml +="</q></xjxquery>";
  207. return sXml;
  208. }
  209. // Generates an XML message that xajax can understand from a javascript object
  210. this.objectToXML = function(obj)
  211. {
  212. var sXml = "<xjxobj>";
  213. for (i in obj)
  214. {
  215. try
  216. {
  217. if (i == 'constructor')
  218. continue;
  219. if (obj[i] && typeof(obj[i]) == 'function')
  220. continue;
  221. var key = i;
  222. var value = obj[i];
  223. if (value && typeof(value)=="object" &&
  224. (value.constructor == Array
  225. ) && this.depth <= 50)
  226. {
  227. this.depth++;
  228. value = this.objectToXML(value);
  229. this.depth--;
  230. }
  231. sXml += "<e><k>"+key+"</k><v>"+value+"</v></e>";
  232. }
  233. catch(e)
  234. {
  235. if (xajaxDebug) this.DebugMessage(e);
  236. }
  237. }
  238. sXml += "</xjxobj>";
  239. return sXml;
  240. }
  241. // Sends a XMLHttpRequest to call the specified PHP function on the server
  242. // * sRequestType is optional -- defaults to POST
  243. this.call = function(sFunction, aArgs, sRequestType)
  244. {
  245. var i,r,postData;
  246. if (document.body && xajaxWaitCursor)
  247. document.body.style.cursor = 'wait';
  248. if (xajaxStatusMessages == true) window.status = 'Sending Request...';
  249. if (xajaxDebug) this.DebugMessage("Starting xajax...");
  250. if (sRequestType == null) {
  251. var xajaxRequestType = xajaxDefinedPost;
  252. }
  253. else {
  254. var xajaxRequestType = sRequestType;
  255. }
  256. var uri = xajaxRequestUri;
  257. var value;
  258. switch(xajaxRequestType)
  259. {
  260. case xajaxDefinedGet:{
  261. var uriGet = uri.indexOf("?")==-1?"?xajax="+encodeURIComponent(sFunction):"&xajax="+encodeURIComponent(sFunction);
  262. if (aArgs) {
  263. for (i = 0; i<aArgs.length; i++)
  264. {
  265. value = aArgs[i];
  266. if (typeof(value)=="object")
  267. value = this.objectToXML(value);
  268. uriGet += "&xajaxargs[]="+encodeURIComponent(value);
  269. }
  270. }
  271. uriGet += "&xajaxr=" + new Date().getTime();
  272. uri += uriGet;
  273. postData = null;
  274. } break;
  275. case xajaxDefinedPost:{
  276. postData = "xajax="+encodeURIComponent(sFunction);
  277. postData += "&xajaxr="+new Date().getTime();
  278. if (aArgs) {
  279. for (i = 0; i <aArgs.length; i++)
  280. {
  281. value = aArgs[i];
  282. if (typeof(value)=="object")
  283. value = this.objectToXML(value);
  284. postData = postData+"&xajaxargs[]="+encodeURIComponent(value);
  285. }
  286. }
  287. } break;
  288. default:
  289. alert("Illegal request type: " + xajaxRequestType); return false; break;
  290. }
  291. r = this.getRequestObject();
  292. if (!r) return false;
  293. r.open(xajaxRequestType==xajaxDefinedGet?"GET":"POST", uri, true);
  294. if (xajaxRequestType == xajaxDefinedPost)
  295. {
  296. try
  297. {
  298. r.setRequestHeader("Method", "POST " + uri + " HTTP/1.5");
  299. r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  300. }
  301. catch(e)
  302. {
  303. alert("Your browser does not appear to support asynchronous requests using POST.");
  304. return false;
  305. }
  306. }
  307. r.onreadystatechange = function()
  308. {
  309. if (r.readyState != 4)
  310. return;
  311. if (r.status==200)
  312. {
  313. if (xajaxDebug && r.responseText.length < 1000) xajax.DebugMessage("Received:\n" + r.responseText);
  314. else if (xajaxDebug) xajax.DebugMessage("Received:\n" + r.responseText.substr(0,1000)+"...\n[long response]\n...</xajax>");
  315. if (r.responseXML)
  316. xajax.processResponse(r.responseXML);
  317. else {
  318. alert("Error: the XML response that was returned from the server is invalid.");
  319. document.body.style.cursor = 'default';
  320. if (xajaxStatusMessages == true) window.status = 'Invalid XML response error';
  321. }
  322. }
  323. delete r;
  324. }
  325. if (xajaxDebug) this.DebugMessage("Calling "+sFunction +" uri="+uri+" (post:"+ postData +")");
  326. r.send(postData);
  327. if (xajaxStatusMessages == true) window.status = 'Waiting for data...';
  328. delete r;
  329. return true;
  330. }
  331. //Gets the text as it would be if it were being retrieved from
  332. //the innerHTML property in the current browser
  333. this.getBrowserHTML = function(html)
  334. {
  335. tmpXajax = this.$(this.workId);
  336. if (tmpXajax == null)
  337. {
  338. tmpXajax = document.createElement("div");
  339. tmpXajax.setAttribute('id',this.workId);
  340. tmpXajax.style.display = "none";
  341. tmpXajax.style.visibility = "hidden";
  342. document.body.appendChild(tmpXajax);
  343. }
  344. tmpXajax.innerHTML = html;
  345. var browserHTML = tmpXajax.innerHTML;
  346. tmpXajax.innerHTML = '';
  347. return browserHTML;
  348. }
  349. // Tests if the new Data is the same as the extant data
  350. this.willChange = function(element, attribute, newData)
  351. {
  352. if (!document.body)
  353. {
  354. return true;
  355. }
  356. var oldData;
  357. if (attribute == "innerHTML")
  358. {
  359. newData = this.getBrowserHTML(newData);
  360. }
  361. eval("oldData=document.getElementById('"+element+"')."+attribute);
  362. if (newData != oldData)
  363. return true;
  364. return false;
  365. }
  366. //Process XML xajaxResponses returned from the request
  367. this.processResponse = function(xml)
  368. {
  369. if (xajaxStatusMessages == true) window.status = 'Processing...';
  370. var tmpXajax = null;
  371. xml = xml.documentElement;
  372. if (xml == null) {
  373. alert("Error: the XML response that was returned from the server cannot be processed.");
  374. document.body.style.cursor = 'default';
  375. if (xajaxStatusMessages == true) window.status = 'XML response processing error';
  376. return;
  377. }
  378. for (i=0; i<xml.childNodes.length; i++)
  379. {
  380. if (xml.childNodes[i].nodeName == "cmd")
  381. {
  382. var cmd;
  383. var id;
  384. var property;
  385. var data;
  386. var search;
  387. var type;
  388. var before;
  389. for (j=0; j<xml.childNodes[i].attributes.length; j++)
  390. {
  391. if (xml.childNodes[i].attributes[j].name == "n")
  392. {
  393. cmd = xml.childNodes[i].attributes[j].value;
  394. }
  395. if (xml.childNodes[i].attributes[j].name == "t")
  396. {
  397. id = xml.childNodes[i].attributes[j].value;
  398. }
  399. if (xml.childNodes[i].attributes[j].name == "p")
  400. {
  401. property = xml.childNodes[i].attributes[j].value;
  402. }
  403. if (xml.childNodes[i].attributes[j].name == "c")
  404. {
  405. type = xml.childNodes[i].attributes[j].value;
  406. }
  407. }
  408. if (xml.childNodes[i].childNodes.length > 1)
  409. {
  410. for (j=0; j<xml.childNodes[i].childNodes.length; j++)
  411. {
  412. if (xml.childNodes[i].childNodes[j].nodeName == "s")
  413. {
  414. if (xml.childNodes[i].childNodes[j].firstChild)
  415. search = xml.childNodes[i].childNodes[j].firstChild.nodeValue;
  416. }
  417. if (xml.childNodes[i].childNodes[j].nodeName == "r")
  418. {
  419. if (xml.childNodes[i].childNodes[j].firstChild)
  420. data = xml.childNodes[i].childNodes[j].firstChild.data;
  421. }
  422. }
  423. }
  424. else if (xml.childNodes[i].firstChild)
  425. data = xml.childNodes[i].firstChild.nodeValue;
  426. else
  427. data = "";
  428. var objElement = this.$(id);
  429. try
  430. {
  431. if (cmd=="al")
  432. {
  433. alert(data);
  434. }
  435. if (cmd=="js")
  436. {
  437. eval(data);
  438. }
  439. if (cmd=="in")
  440. {
  441. this.include(data);
  442. }
  443. if (cmd=="as")
  444. {
  445. if (this.willChange(id,property,data))
  446. {
  447. eval("objElement."+property+"=data;");
  448. }
  449. }
  450. if (cmd=="ap")
  451. {
  452. eval("objElement."+property+"+=data;");
  453. }
  454. if (cmd=="pp")
  455. {
  456. eval("objElement."+property+"=data+objElement."+property);
  457. }
  458. if (cmd=="rp")
  459. {
  460. this.replace(id,property,search,data)
  461. }
  462. if (cmd=="rm")
  463. {
  464. this.remove(id);
  465. }
  466. if (cmd=="ce")
  467. {
  468. this.create(id,data,property);
  469. }
  470. if (cmd=="ie")
  471. {
  472. this.insert(id,data,property);
  473. }
  474. if (cmd=="ci")
  475. {
  476. this.createInput(id,type,data,property);
  477. }
  478. if (cmd=="ii")
  479. {
  480. this.insertInput(id,type,data,property);
  481. }
  482. if (cmd=="ev")
  483. {
  484. eval("this.$('"+id+"')."+property+"= function(){"+data+";}");
  485. }
  486. if (cmd=="ah")
  487. {
  488. this.addHandler(id, property, data);
  489. }
  490. if (cmd=="rh")
  491. {
  492. this.removeHandler(id, property, data);
  493. }
  494. }
  495. catch(e)
  496. {
  497. alert(e);
  498. }
  499. delete objElement;
  500. delete cmd;
  501. delete id;
  502. delete property;
  503. delete search;
  504. delete data;
  505. delete type;
  506. delete before;
  507. }
  508. }
  509. delete xml;
  510. document.body.style.cursor = 'default';
  511. if (xajaxStatusMessages == true) window.status = 'Done';
  512. }
  513. }
  514. var xajax = new Xajax();