--- /dev/null
+/**\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
+ * Copyright (C) 2002-2007 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ *\r
+ *\r
+ * This page contains xmlHTTPRequest functions for:\r
+ * - AutoSaveDraft\r
+ *\r
+ *\r
+ * Usage:\r
+ * - Add in the page before the form open tag:\r
+ * <script type="text/javascript" src="javascript/xmlhttprequest.js"></script>\r
+ * - Add in the page behind the form close tag:\r
+ * var xmlhttprequest = new Array();\r
+ * xmlhttprequest[0] = createHTTPHandler(); // AutoDraft handler\r
+ * xmlhttprequest[1] = createHTTPHandler(); // UpdateTicket handler\r
+ * var seconds = now(); // Last AutoDraft time\r
+ * var checks = 0; // Number of checks since last AutoDraft\r
+ * var addform = document.getElementById('addform'); // The form id\r
+ * var goal = document.getElementById('lastsaved'); // The html div id where 'Last saved: date time' must come\r
+ * var goalurl = 'action.php'; // The PHP file where the content must be posted to (action.php)\r
+ * var lastsavedtext = 'Last saved'; // The language variable for 'Last saved'\r
+ * var formtype = 'add'; // Add or edit form\r
+ * - Add to the form tag:\r
+ * id="addform"\r
+ * - Add to the textarea's and text fields:\r
+ * onkeyup="doMonitor();"\r
+ * - Add tot the selectboxes and radio buttons\r
+ * onchange="doMonitor();"\r
+ * - Add to the form:\r
+ * <input type="hidden" name="draftid" value="0" />\r
+ * - Optionally a autosave now button can be add:\r
+ * <input type="button" name="autosavenow" value="AutoSave now" onclick="autoSaveDraft();" />\r
+ *\r
+ *\r
+ * $Id: xmlhttprequest.js 1116 2007-02-03 08:24:29Z kimitake $\r
+ * $NucleusJP: xmlhttprequest.js,v 1.1 2007/02/28 21:34:18 kimitake Exp $\r
+ */\r
+\r
+/**\r
+ * Creates the xmlHTTPRequest handler\r
+ */\r
+function createHTTPHandler() {\r
+ var httphandler = false;\r
+ /*@cc_on @*/\r
+ /*@if (@_jscript_version >= 5)\r
+ // JScript gives us Conditional compilation, we can cope with old IE versions.\r
+ // and security blocked creation of the objects.\r
+ try {\r
+ httphandler = new ActiveXObject("Msxml2.XMLHTTP");\r
+ }\r
+ catch (e) {\r
+ try {\r
+ httphandler = new ActiveXObject("Microsoft.XMLHTTP");\r
+ }\r
+ catch (E) {\r
+ httphandler = false;\r
+ }\r
+ }\r
+ @end @*/\r
+ if (!httphandler && typeof XMLHttpRequest != 'undefined') {\r
+ httphandler = new XMLHttpRequest();\r
+ }\r
+ return httphandler;\r
+}\r
+\r
+/**\r
+ * Auto saves as draft\r
+ */\r
+function autoSaveDraft() {\r
+ checks = 0;\r
+ seconds = now();\r
+\r
+ var title = encodeURI(addform.title.value);\r
+ var body = encodeURI(addform.body.value);\r
+ var catid = addform.catid.options[addform.catid.selectedIndex].value;\r
+ var more = encodeURI(addform.more.value);\r
+ var closed = 0;\r
+ if (addform.closed[0].checked) {\r
+ closed = addform.closed[0].value;\r
+ }\r
+ else if (addform.closed[1].checked) {\r
+ closed = addform.closed[1].value;\r
+ }\r
+ var ticket = addform.ticket.value;\r
+\r
+ var querystring = 'action=autodraft';\r
+ querystring += '&title=' + title;\r
+ querystring += '&body=' + body;\r
+ querystring += '&catid=' + catid;\r
+ querystring += '&more=' + more;\r
+ querystring += '&closed=' + closed;\r
+ querystring += '&ticket=' + ticket;\r
+ if (formtype == 'edit') {\r
+ querystring += '&itemid=' + addform.itemid.value;\r
+ querystring += '&type=edit';\r
+ }\r
+ else {\r
+ querystring += '&blogid=' + addform.blogid.value;\r
+ querystring += '&type=add';\r
+ }\r
+ if (addform.draftid.value > 0) {\r
+ querystring += '&draftid=' + addform.draftid.value;\r
+ }\r
+\r
+ xmlhttprequest[0].open('POST', goalurl, true);\r
+ xmlhttprequest[0].onreadystatechange = checkMonitor;\r
+ xmlhttprequest[0].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');\r
+ xmlhttprequest[0].send(querystring);\r
+\r
+ var querystring = 'action=updateticket&ticket=' + ticket;\r
+\r
+ xmlhttprequest[1].open('POST', goalurl, true);\r
+ xmlhttprequest[1].onreadystatechange = updateTicket;\r
+ xmlhttprequest[1].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');\r
+ xmlhttprequest[1].send(querystring);\r
+}\r
+\r
+/**\r
+ * Monitors the edits\r
+ */\r
+function doMonitor() {\r
+ if (checks * (now() - seconds) > 120 * 1000 * 50) {\r
+ autoSaveDraft();\r
+ }\r
+ else {\r
+ checks++;\r
+ }\r
+}\r
+\r
+/**\r
+ * Checks the process of the saving\r
+ */\r
+function checkMonitor() {\r
+ if (xmlhttprequest[0].readyState == 4) {\r
+ if (xmlhttprequest[0].responseText) {\r
+ if (xmlhttprequest[0].responseText.substr(0, 4) == 'err:') {\r
+ goal.innerHTML = xmlhttprequest[0].responseText.substr(4) + ' (' + formattedDate() + ')';\r
+ }\r
+ else {\r
+ addform.draftid.value = xmlhttprequest[0].responseText;\r
+ goal.innerHTML = lastsavedtext + ' ' + formattedDate();\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+/**\r
+ * Checks the process of the ticket updating\r
+ */\r
+function updateTicket() {\r
+ if (xmlhttprequest[1].readyState == 4) {\r
+ if (xmlhttprequest[1].responseText) {\r
+ if (xmlhttprequest[1].responseText.substr(0, 4) == 'err:') {\r
+ goal.innerHTML = xmlhttprequest[1].responseText.substr(4) + ' (' + formattedDate() + ')';\r
+ }\r
+ else {\r
+ addform.ticket.value = xmlhttprequest[1].responseText;\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+/**\r
+ * Gets now in milliseconds\r
+ */\r
+function now() {\r
+ var now = new Date();\r
+ return now.getTime();\r
+}\r
+\r
+/**\r
+ * Gets now in the local dateformat\r
+ */\r
+function formattedDate() {\r
+ var now = new Date();\r
+ return now.toLocaleDateString() + ' ' + now.toLocaleTimeString();\r
+}
\ No newline at end of file