2 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
3 * Copyright (C) 2002-2011 The Nucleus Group
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * (see nucleus/documentation/index.html#license for more info)
12 * This page contains xmlHTTPRequest functions for:
17 * - Add in the page before the form open tag:
18 * <script type="text/javascript" src="javascript/xmlhttprequest.js"></script>
19 * - Add in the page behind the form close tag:
20 * var xmlhttprequest = new Array();
21 * xmlhttprequest[0] = createHTTPHandler(); // AutoDraft handler
22 * xmlhttprequest[1] = createHTTPHandler(); // UpdateTicket handler
23 * var seconds = now(); // Last AutoDraft time
24 * var checks = 0; // Number of checks since last AutoDraft
25 * var addform = document.getElementById('addform'); // The form id
26 * var goal = document.getElementById('lastsaved'); // The html div id where 'Last saved: date time' must come
27 * var goalurl = 'action.php'; // The PHP file where the content must be posted to (action.php)
28 * var lastsavedtext = 'Last saved'; // The language variable for 'Last saved'
29 * var formtype = 'add'; // Add or edit form
30 * - Add to the form tag:
32 * - Add to the textarea's and text fields:
33 * onkeyup="doMonitor();"
34 * - Add tot the selectboxes and radio buttons
35 * onchange="doMonitor();"
37 * <input type="hidden" name="draftid" value="0" />
38 * - Optionally a autosave now button can be add:
39 * <input type="button" name="autosavenow" value="AutoSave now" onclick="autoSaveDraft();" />
47 * Creates the xmlHTTPRequest handler
49 function createHTTPHandler() {
50 var httphandler = false;
52 /*@if (@_jscript_version >= 5)
53 // JScript gives us Conditional compilation, we can cope with old IE versions.
54 // and security blocked creation of the objects.
56 httphandler = new ActiveXObject("Msxml2.XMLHTTP");
60 httphandler = new ActiveXObject("Microsoft.XMLHTTP");
67 if (!httphandler && typeof XMLHttpRequest != 'undefined') {
68 httphandler = new XMLHttpRequest();
76 function autoSaveDraft() {
80 var title = encodeURI(addform.title.value);
81 var body = encodeURI(addform.body.value);
82 var catid = addform.catid.options[addform.catid.selectedIndex].value;
83 var more = encodeURI(addform.more.value);
85 if (addform.closed[0].checked) {
86 closed = addform.closed[0].value;
88 else if (addform.closed[1].checked) {
89 closed = addform.closed[1].value;
91 var ticket = addform.ticket.value;
93 var querystring = 'action=autodraft';
94 querystring += '&title=' + title;
95 querystring += '&body=' + body;
96 querystring += '&catid=' + catid;
97 querystring += '&more=' + more;
98 querystring += '&closed=' + closed;
99 querystring += '&ticket=' + ticket;
100 if (formtype == 'edit') {
101 querystring += '&itemid=' + addform.itemid.value;
102 querystring += '&type=edit';
105 querystring += '&blogid=' + addform.blogid.value;
106 querystring += '&type=add';
108 if (addform.draftid.value > 0) {
109 querystring += '&draftid=' + addform.draftid.value;
112 xmlhttprequest[0].open('POST', goalurl, true);
113 xmlhttprequest[0].onreadystatechange = checkMonitor;
114 xmlhttprequest[0].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
115 xmlhttprequest[0].send(querystring);
117 var querystring = 'action=updateticket&ticket=' + ticket;
119 xmlhttprequest[1].open('POST', goalurl, true);
120 xmlhttprequest[1].onreadystatechange = updateTicket;
121 xmlhttprequest[1].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
122 xmlhttprequest[1].send(querystring);
128 function doMonitor() {
129 if (checks * (now() - seconds) > 120 * 1000 * 50) {
138 * Checks the process of the saving
140 function checkMonitor() {
141 if (xmlhttprequest[0].readyState == 4) {
142 if (xmlhttprequest[0].responseText) {
143 if (xmlhttprequest[0].responseText.substr(0, 4) == 'err:') {
144 goal.innerHTML = xmlhttprequest[0].responseText.substr(4) + ' (' + formattedDate() + ')';
147 addform.draftid.value = xmlhttprequest[0].responseText;
148 goal.innerHTML = lastsavedtext + ' ' + formattedDate();
155 * Checks the process of the ticket updating
157 function updateTicket() {
158 if (xmlhttprequest[1].readyState == 4) {
159 if (xmlhttprequest[1].responseText) {
160 if (xmlhttprequest[1].responseText.substr(0, 4) == 'err:') {
161 goal.innerHTML = xmlhttprequest[1].responseText.substr(4) + ' (' + formattedDate() + ')';
164 addform.ticket.value = xmlhttprequest[1].responseText;
171 * Gets now in milliseconds
174 var now = new Date();
175 return now.getTime();
179 * Gets now in the local dateformat
181 function formattedDate() {
182 var now = new Date();
183 return now.toLocaleDateString() + ' ' + now.toLocaleTimeString();