A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
9.9 KiB

12 years ago
  1. ////////////////////////////////////////////
  2. // PostBack and AjaxPostBack
  3. ////////////////////////////////////////////
  4. qcodo.postBack = function(strForm, strControl, strEvent, strParameter) {
  5. var objForm = document.getElementById(strForm);
  6. objForm.Qform__FormControl.value = strControl;
  7. objForm.Qform__FormEvent.value = strEvent;
  8. objForm.Qform__FormParameter.value = strParameter;
  9. objForm.Qform__FormCallType.value = "Server";
  10. objForm.Qform__FormUpdates.value = this.formUpdates();
  11. objForm.Qform__FormCheckableControls.value = this.formCheckableControls(strForm, "Server");
  12. objForm.submit();
  13. };
  14. qcodo.formUpdates = function() {
  15. var strToReturn = "";
  16. for (var strControlId in qcodo.controlModifications)
  17. for (var strProperty in qcodo.controlModifications[strControlId])
  18. strToReturn += strControlId + " " + strProperty + " " + qcodo.controlModifications[strControlId][strProperty] + "\n";
  19. qcodo.controlModifications = new Object;
  20. return strToReturn;
  21. };
  22. qcodo.formCheckableControls = function(strForm, strCallType) {
  23. var objForm = document.getElementById(strForm);
  24. var strToReturn = "";
  25. for (var intIndex = 0; intIndex < objForm.elements.length; intIndex++) {
  26. if (((objForm.elements[intIndex].type == "checkbox") ||
  27. (objForm.elements[intIndex].type == "radio")) &&
  28. ((strCallType == "Ajax") ||
  29. (!objForm.elements[intIndex].disabled))) {
  30. // CheckBoxList
  31. if (objForm.elements[intIndex].id.indexOf('[') >= 0) {
  32. if (objForm.elements[intIndex].id.indexOf('[0]') >= 0)
  33. strToReturn += " " + objForm.elements[intIndex].id.substring(0, objForm.elements[intIndex].id.length - 3);
  34. // RadioButtonList
  35. } else if (objForm.elements[intIndex].id.indexOf('_') >= 0) {
  36. if (objForm.elements[intIndex].id.indexOf('_0') >= 0)
  37. strToReturn += " " + objForm.elements[intIndex].id.substring(0, objForm.elements[intIndex].id.length - 2);
  38. // Standard Radio or Checkbox
  39. } else {
  40. strToReturn += " " + objForm.elements[intIndex].id;
  41. };
  42. };
  43. };
  44. if (strToReturn.length > 0)
  45. return strToReturn.substring(1);
  46. else
  47. return "";
  48. };
  49. qcodo.ajaxQueue = new Array();
  50. qcodo.postAjax = function(strForm, strControl, strEvent, strParameter, strWaitIconControlId) {
  51. // Only add if we're not unloaded
  52. if (!qc.unloadFlag) {
  53. if (qc.beforeUnloadFlag) {
  54. qc.beforeUnloadFlag = false;
  55. };
  56. // Figure out if Queue is Empty
  57. var blnQueueEmpty = false;
  58. if (qcodo.ajaxQueue.length == 0)
  59. blnQueueEmpty = true;
  60. // Enqueue the AJAX Request
  61. qcodo.ajaxQueue.push(new Array(strForm, strControl, strEvent, strParameter, strWaitIconControlId));
  62. // If the Queue was originally empty, call the Dequeue
  63. if (blnQueueEmpty)
  64. qcodo.dequeueAjaxQueue();
  65. };
  66. };
  67. qcodo.clearAjaxQueue = function() {
  68. qcodo.ajaxQueue = new Array();
  69. };
  70. qcodo.objAjaxWaitIcon = null;
  71. qcodo.dequeueAjaxQueue = function() {
  72. if (qcodo.ajaxQueue.length > 0) {
  73. strForm = this.ajaxQueue[0][0];
  74. strControl = this.ajaxQueue[0][1];
  75. strEvent = this.ajaxQueue[0][2];
  76. strParameter = this.ajaxQueue[0][3];
  77. strWaitIconControlId = this.ajaxQueue[0][4];
  78. // Display WaitIcon (if applicable)
  79. if (strWaitIconControlId) {
  80. this.objAjaxWaitIcon = this.getWrapper(strWaitIconControlId);
  81. if (this.objAjaxWaitIcon)
  82. this.objAjaxWaitIcon.style.display = 'inline';
  83. };
  84. var objForm = document.getElementById(strForm);
  85. objForm.Qform__FormControl.value = strControl;
  86. objForm.Qform__FormEvent.value = strEvent;
  87. objForm.Qform__FormParameter.value = strParameter;
  88. objForm.Qform__FormCallType.value = "Ajax";
  89. objForm.Qform__FormUpdates.value = qcodo.formUpdates();
  90. objForm.Qform__FormCheckableControls.value = this.formCheckableControls(strForm, "Ajax");
  91. var strPostData = "";
  92. for (var i = 0; i < objForm.elements.length; i++) {
  93. switch (objForm.elements[i].type) {
  94. case "checkbox":
  95. case "radio":
  96. if (objForm.elements[i].checked) {
  97. var strTestName = objForm.elements[i].name + "_";
  98. if (objForm.elements[i].id.substring(0, strTestName.length) == strTestName)
  99. strPostData += "&" + objForm.elements[i].name + "=" + objForm.elements[i].id.substring(strTestName.length);
  100. else
  101. // strPostData += "&" + objForm.elements[i].id + "=" + "1";
  102. strPostData += "&" + objForm.elements[i].id + "=" + objForm.elements[i].value;
  103. };
  104. break;
  105. case "select-multiple":
  106. var blnOneSelected = false;
  107. for (var intIndex = 0; intIndex < objForm.elements[i].options.length; intIndex++)
  108. if (objForm.elements[i].options[intIndex].selected) {
  109. strPostData += "&" + objForm.elements[i].name + "=";
  110. strPostData += objForm.elements[i].options[intIndex].value;
  111. };
  112. break;
  113. default:
  114. strPostData += "&" + objForm.elements[i].id + "=";
  115. // For Internationalization -- we must escape the element's value properly
  116. var strPostValue = objForm.elements[i].value;
  117. if (strPostValue) {
  118. strPostValue = strPostValue.replace(/\%/g, "%25");
  119. strPostValue = strPostValue.replace(/&/g, escape('&'));
  120. strPostValue = strPostValue.replace(/\+/g, "%2B");
  121. };
  122. strPostData += strPostValue;
  123. break;
  124. };
  125. };
  126. var strUri = objForm.action;
  127. var objRequest;
  128. if (window.XMLHttpRequest) {
  129. objRequest = new XMLHttpRequest();
  130. } else if (typeof ActiveXObject != "undefined") {
  131. objRequest = new ActiveXObject("Microsoft.XMLHTTP");
  132. };
  133. if (objRequest) {
  134. objRequest.open("POST", strUri, true);
  135. objRequest.setRequestHeader("Method", "POST " + strUri + " HTTP/1.1");
  136. objRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  137. objRequest.onreadystatechange = function() {
  138. if (objRequest.readyState == 4) {
  139. if (!qcodo.beforeUnloadFlag) {
  140. try {
  141. var objXmlDoc = objRequest.responseXML;
  142. // qcodo.logMessage(objRequest.responseText, true);
  143. // alert('AJAX Response Received');
  144. if (!objXmlDoc) {
  145. if(-1 != objRequest.responseText.indexOf('<!-- QCodo Error -->') ){
  146. var objErrorWindow = window.open('about:blank', 'qcodo_error','menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,width=1000,height=700,left=50,top=50');
  147. objErrorWindow.focus();
  148. objErrorWindow.document.write(objRequest.responseText);
  149. } else {
  150. if(objRequest.responseText.length > 0 && -1 == objRequest.responseText.indexOf('Qform__FormId') )
  151. alert('AJAX Response Error: ' + objRequest.responseText);
  152. //else
  153. // alert('AJAX Response Error - XML missing.');
  154. }
  155. window.location.href = strUri;
  156. return;
  157. } else {
  158. var intLength = 0;
  159. // Go through Controls
  160. var objXmlControls = objXmlDoc.getElementsByTagName('control');
  161. intLength = objXmlControls.length;
  162. for (var intIndex = 0; intIndex < intLength; intIndex++) {
  163. var strControlId = objXmlControls[intIndex].attributes.getNamedItem('id').nodeValue;
  164. var strControlHtml = "";
  165. if (objXmlControls[intIndex].textContent)
  166. strControlHtml = objXmlControls[intIndex].textContent;
  167. else if (objXmlControls[intIndex].firstChild)
  168. strControlHtml = objXmlControls[intIndex].firstChild.nodeValue;
  169. // Perform Callback Responsibility
  170. if (strControlId == "Qform__FormState") {
  171. var objFormState = document.getElementById(strControlId);
  172. objFormState.value = strControlHtml;
  173. } else {
  174. var objSpan = document.getElementById(strControlId + "_ctl");
  175. if (objSpan)
  176. objSpan.innerHTML = strControlHtml;
  177. };
  178. };
  179. // Go through Commands
  180. var objXmlCommands = objXmlDoc.getElementsByTagName('command');
  181. intLength = objXmlCommands.length;
  182. for (var intIndex = 0; intIndex < intLength; intIndex++) {
  183. if (objXmlCommands[intIndex] && objXmlCommands[intIndex].firstChild) {
  184. var strCommand = "";
  185. intChildLength = objXmlCommands[intIndex].childNodes.length;
  186. for (var intChildIndex = 0; intChildIndex < intChildLength; intChildIndex++)
  187. strCommand += objXmlCommands[intIndex].childNodes[intChildIndex].nodeValue;
  188. eval(strCommand);
  189. };
  190. };
  191. };
  192. } catch (objExc) {
  193. alert(objExc.message + "\r\non line number " + objExc.lineNumber + "\r\nin file " + objExc.fileName);
  194. alert("An error occurred during AJAX Response handling.\r\n\r\nThe error response will appear in a new popup.");
  195. var objErrorWindow = window.open('about:blank', 'qcodo_error','menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,width=1000,height=700,left=50,top=50');
  196. objErrorWindow.focus();
  197. objErrorWindow.document.write(objRequest.responseText);
  198. return;
  199. };
  200. };
  201. // Perform the Dequeue
  202. qcodo.ajaxQueue.shift();
  203. // Hid the WaitIcon (if applicable)
  204. if (qcodo.objAjaxWaitIcon)
  205. qcodo.objAjaxWaitIcon.style.display = 'none';
  206. // If there are still AjaxEvents in the queue, go ahead and process/dequeue them
  207. if (qcodo.ajaxQueue.length > 0)
  208. qcodo.dequeueAjaxQueue();
  209. };
  210. };
  211. objRequest.send(strPostData);
  212. };
  213. };
  214. };
  215. //////////////////
  216. // Qcodo Shortcuts
  217. //////////////////
  218. qc.pB = qcodo.postBack;
  219. qc.pA = qcodo.postAjax;