/* FRESH */// Phorum object. Other JavaScript code for Phorum can extend
// this one to implement functionality without risking name
// name space collissions.
Phorum = {};
/* Added by module "core", file "include/ajax/client.js.php" */
// Create the Phorum object if it's not available. It it created in the
// core javascript.php, but when loading this code from an external
// page, it might not be available.
if (!document.Phorum || Phorum == undefined) Phorum = {};
Phorum.Ajax = {};
// The version of this lib
Phorum.Ajax.version = '1.0.0';
// The URL that we use to access the Phorum Ajax layer.
Phorum.Ajax.URL = '/forum/ajax.php';
// Storage for Ajax call return data. This acts as a local cache
// for keeping track of already retrieved items.
Phorum.Ajax.cache = {};
/**
* Create an XMLHttpRequest object.
* Used internally by Phorum.Ajax.call().
* Raise an onFailure event in case no object can be created.
* Return either an object or null if the object creation failed.
*/
Phorum.Ajax.getXMLHttpRequest = function(req)
{
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
var versions = [
'MSXML2.XMLHttp.5.0',
'MSXML2.XMLHttp.4.0',
'MSXML2.XMLHttp.3.0',
'MSXML2.XMLHttp',
'Microsoft.XMLHttp'
];
for (var i=0; i < versions.length; i++) {
try { xhr = new ActiveXObject(versions[i]); } catch (e) { }
}
}
if (xhr) {
return xhr;
}
if (req.onFailure) req.onFailure(
'Phorum: Unable to create an XMLHttpRequest object',
-1, null
);
return null;
}
/**
* Execute an Ajax Phorum call.
*/
Phorum.Ajax.call = function(req)
{
// If the store property is set for the request, then check
// if the data for the request is already available in the
// local cache. If yes, then return the data immediately.
if (req.store) {
if (req.store != null && Phorum.Ajax.cache[req.store]) {
if (req.onSuccess) {
// true = data retrieved from cache.
req.onSuccess(Phorum.Ajax.cache[req.store], true);
}
return;
}
}
// Check the request data.
if (! req['call']) {
if (req.onFailure) req.onFailure(
'Phorum.Ajax.call() error: missing property ' +
'"call" for the request object.',
-1, null
);
return;
}
// Check if there is an XMLHttpRequest object available.
var xhr = Phorum.Ajax.getXMLHttpRequest(req);
if (! xhr) return;
// Convert the request object to JSON.
var json = Phorum.JSON.encode(req);
// Notify the start of the request loading stage.
if (req.onRequest) req.onRequest(json);
xhr.open("post", Phorum.Ajax.URL, true);
xhr.setRequestHeader("Content-Type", "text/x-json");
xhr.onreadystatechange = function()
{
if (req.onReadStateChange) req.onReadyStateChange(req);
switch (xhr.readyState)
{
case 1:
if (req.onLoading) req.onLoading(xhr);
break;
case 2:
if (req.onLoaded) req.onLoaded(xhr);
break;
case 3:
if (req.onInteractive) req.onInteractive(xhr);
break;
case 4:
if (req.onComplete)req.onComplete(xhr);
if (req.onResponse) req.onResponse(xhr.responseText);
if (xhr.status == 200) {
// Evaluate the returned JSON code. If evaluation fails,
// then run the onFailure event for the Phorum.Ajax.call.
try {
var res = Phorum.JSON.decode(xhr.responseText);
} catch (e) {
if (req.onFailure) req.onFailure(
'Ajax Phorum API call succeeded, but the return ' +
'data could not be parsed as JSON data.',
xhr.status, xhr.responseText
);
return;
}
// If the req.store property is set, then we store
// the result data in the Phorum cache.
if (req.store) Phorum.Ajax.cache[req.store] = res;
// false = data not retrieved from store.
if (req.onSuccess) req.onSuccess(res, false);
} else {
if (req.onFailure) req.onFailure(
'The Ajax Phorum API call failed',
xhr.status, xhr.responseText
);
}
break;
}
};
xhr.send(json);
}
// Invalidate a single cache item of the full cache.
Phorum.Ajax.invalidateCache = function(key)
{
if (key) {
Phorum.Ajax.cache[key] = null;
} else {
Phorum.Ajax.cache = new Array();
}
}
// Parse out javascript blocks from the data to eval them. Adding them
// to the page using innerHTML does not invoke parsing by the browser.
Phorum.Ajax.evalJavaScript = function(data)
{
var cursor = 0;
var start = 1;
var end = 1;
while (cursor < data.length && start > 0 && end > 0) {
start = data.indexOf('