
// ----- global variable for the proxies to webservices. -----
var proxies = new Object();
proxies.current = null;
proxies.xmlhttp = null;

var ajax = new Object();
ajax.current = null;
ajax.option = null;
ajax.queue = new Array();
ajax.options = new Array();
ajax.timer = null;
ajax.progress = false;
ajax.progressTimer = null;

ajax.Start = function (action, options) 
{
ajax.Add(action, options);
if ((ajax.current == null) && (ajax.timer == null))
ajax._next(false);
}
ajax.Add = function (action, options) 
{
if (action == null) {
alert("ajax.Start: Argument action must be set.");
return;
}
if (typeof(action.call) == "string") action.call = eval(action.call);
if (typeof(action.prepare) == "string") action.prepare = eval(action.prepare);
if (typeof(action.finish) == "string") action.finish = eval(action.finish);
if ((action.queueClear != null) && (action.queueClear == true)) {
ajax.queue = new Array();
ajax.options = new Array();
} else if ((ajax.queue.length > 0) && ((action.queueMultiple == null) || (action.queueMultiple == false))) {
if ((ajax.timer != null) && (ajax.queue[0] == action)) {
window.clearTimeout(ajax.timer);
ajax.timer = null;
}
var n = 0;
while (n < ajax.queue.length) {
if (ajax.queue[n] == action) {
ajax.queue.splice(n, 1);
ajax.options.splice(n, 1);
} else {
n++;
}
}
}
if ((action.queueTop == null) || (action.queueTop == false)) {
ajax.queue.push(action);
ajax.options.push(options);
} else {
ajax.queue.unshift(action);
ajax.options.unshift(options);
}
}

ajax._next = function (forceStart) 
{
var ca = null // current action
var co = null // current opptions
var data = null;
if (ajax.current != null)
return;
if (ajax.timer != null)
return;
if (ajax.queue.length == 0)
return;
ca = ajax.queue[0];
co = ajax.options[0];
if ((forceStart == true) || (ca.delay == null) || (ca.delay == 0)) {
ajax.current = ca;
ajax.queue.shift();
ajax.option = co;
ajax.options.shift();
if (ca.prepare != null)
try {
data = ca.prepare(co);
if (ca.call != null) {
ajax.StartProgress();
ca.call.func = ajax.Finish;
ca.call.onException = ajax.Exception;
if ((data.constructor == Array) && (data.multi != null)) // 19.05.2007
ca.call.apply(ca, data);
else
ca.call(data);
if (ca.timeout != null)
ajax.timer = window.setTimeout(ajax.Cancel, ca.timeout * 1000);
} else if (ca.postUrl != null) {
} else {
ajax.Finish(data);
}
} catch (ex) { 
ajax.Cancel();
}
} else {
ajax.timer = window.setTimeout(ajax.EndWait, ca.delay);
}
}
ajax.EndWait = function() {
ajax.timer = null;
ajax._next(true);
}
ajax.Cancel = function() {
proxies.cancel(false);
ajax.timer = null;
ajax.current = null;
ajax.option = null;
ajax.EndProgress();
window.setTimeout(ajax._next, 200);
}
ajax.Finish = function (data) {
if (ajax.timer != null) {
window.clearTimeout(ajax.timer);
ajax.timer = null;
}
try {
if ((ajax.current != null) && (ajax.current.finish != null))
ajax.current.finish(data, ajax.option);
} catch (ex) { }
ajax.current = null;
ajax.option = null;
ajax.EndProgress();
ajax._next(false)
}
ajax.Exception = function (ex) {
if (ajax.current.onException != null)
ajax.current.onException(ex, ajax.option);
ajax.current = null;
ajax.option = null;
ajax.EndProgress();
}
ajax.CancelAll = function () {
ajax.Cancel();
ajax.queue = new Array();
ajax.options = new Array();
}

ajax.StartProgress = function() {
ajax.progress = true;
if (ajax.progressTimer != null)
window.clearTimeout(ajax.progressTimer);
ajax.progressTimer = window.setTimeout(ajax.ShowProgress, 220);
}
ajax.EndProgress = function () {
ajax.progress = false;
if (ajax.progressTimer != null)
window.clearTimeout(ajax.progressTimer);
ajax.progressTimer = window.setTimeout(ajax.ShowProgress, 20);
}
ajax.ShowProgress = function() {
ajax.progressTimer = null;
var a = document.getElementById("AjaxProgressIndicator");
if (ajax.progress && (a != null)) {
a.style.top = document.documentElement.scrollTop + 2 + "px";
a.style.display = "";
} else if (ajax.progress) {
var path = "../ajaxcore/"
for (var n in document.scripts) {
s = document.scripts[n].src;
if ((s != null) && (s.length >= 7) && (s.substr(s.length -7).toLowerCase() == "ajax.js"))
path = s.substr(0,s.length -7);
}
a = document.createElement("div");
a.id = "AjaxProgressIndicator";
a.style.position = "absolute";
a.style.right = "2px";
a.style.top = document.documentElement.scrollTop + 2 + "px";
a.style.width = "98px";
a.style.height = "16px"
a.style.padding = "2px";
a.style.verticalAlign = "bottom";
a.style.backgroundColor="#51c77d";
a.innerHTML = "<img style='vertical-align:bottom' src='" + path + "ajax-loader.gif'>&nbsp;please wait...";
document.body.appendChild(a);
} else if (a) {
a.style.display="none";
}
}

ajax.postData = function (url, data, func) {
var x = proxies._getXHR();
var cs = document.location.href.match(/\/\(.*\)\//);
if (cs != null) {
url = url.split('/');
url[3] += cs[0].substr(0, cs[0].length-1);
url = url.join('/');
}
x.open("POST", url, (func != null));
if (func != null) {
x.onreadystatechange = func;
x.send(data);
} else {
x.send(soap);
return(x.responseText);
}
}

proxies.callSoap = function (args) {
var p = args.callee;
var x = null;
if (p._cache != null) {
if ((p.params.length == 1) && (args.length == 1) && (p._cache[args[0]] != null)) {
if (p.func != null) {
p.func(p._cache[args[0]]);
return(null);
} else {
return(p._cache[args[0]]);
}
} else {
p._cachekey = args[0];
}
}
proxies.current = p;
x = proxies._getXHR();
proxies.xmlhttp = x;
var soap = "<?xml version='1.0' encoding='utf-8'?>"
+ "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
+ "<soap:Body>"
+ "<" + p.fname + " xmlns='" + p.service.ns + "'>";
for (n = 0; (n < p.params.length) && (n < args.length); n++) {
var val = args[n];
var typ = p.params[n].split(':');
if ((typ.length == 1) || (typ[1] == "string")) {
val = String(args[n]).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
} else if (typ[1] == "int") {
val = parseInt(args[n]);
} else if (typ[1] == "float") {
val = parseFloat(args[n]);
} else if ((typ[1] == "x") && (typeof(args[n]) == "string")) {
val = args[n];
} else if ((typ[1] == "x") && (typeof(XMLSerializer) != "undefined")) {
val = (new XMLSerializer()).serializeToString(args[n].firstChild);
} else if (typ[1] == "x") {
if (args[n] != null)
val = args[n].xml;
} else if ((typ[1] == "bool") && (typeof(args[n]) == "string")) {
val = args[n].toLowerCase();
} else if (typ[1] == "bool") {
val = String(args[n]).toLowerCase();
} else if (typ[1] == "date") {
var s, ret;
ret = String(val.getFullYear());
ret += "-";
s = String(val.getMonth() + 1);
ret += (s.length == 1 ? "0" + s : s);
ret += "-";
s = String(val.getDate());
ret += (s.length == 1 ? "0" + s : s);
ret += "T";
s = String(val.getHours());
ret += (s.length == 1 ? "0" + s : s);
ret += ":";
s = String(val.getMinutes());
ret += (s.length == 1 ? "0" + s : s);
ret += ":";
s = String(val.getSeconds());
ret += (s.length == 1 ? "0" + s : s);
val = ret;
} else if (typ[1] == "s[]") {
val = "<string>" + args[n].join("</string><string>") + "</string>";
} else if (typ[1] == "int[]") {
val = "<int>" + args[n].join("</int><int>") + "</int>";
} else if (typ[1] == "float[]") {
val = "<float>" + args[n].join("</float><float>") + "</float>";
} else if (typ[1] == "bool[]") {
val = "<boolean>" + args[n].join("</boolean><boolean>") + "</boolean>";
}
soap += "<" + typ[0] + ">" + val + "</" + typ[0] + ">"
}
soap += "</" + p.fname + ">"
+ "</soap:Body>"
+ "</soap:Envelope>";
var u = p.service.url;
var cs = document.location.href.match(/\/\(.*\)\//);
if (cs != null) {
u = p.service.url.split('/');
u[3] += cs[0].substr(0, cs[0].length-1);
u = u.join('/');
}
x.open("POST", u, (p.func != null));
x.setRequestHeader("SOAPAction", p.action);
x.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
if (p.corefunc != null) {
x.onreadystatechange = p.corefunc;
x.send(soap);
} else if (p.func != null) {
x.onreadystatechange = proxies._response;
x.send(soap);
} else {
x.send(soap);
return(proxies._response());
}
}
// raise: set raise to false to prevent raising an exception
proxies.cancel = function(raise) {
var cc = proxies.current;
var cx = proxies.xmlhttp;
if (raise == null) raise == true;
if (proxies.xmlhttp != null) {
proxies.xmlhttp.onreadystatechange = function() { };
proxies.xmlhttp.abort();
if (raise && (proxies.current.onException != null))
proxies.current.onException("WebService call was canceled.")
proxies.current = null;
proxies.xmlhttp = null;
}
}
proxies.EnableCache = function (px) {
px._cache = new Object();
}
proxies.IsActive = function () {
return(proxies.xmlhttp != null);
}

proxies._response = function () {
var ret = null;
var x = proxies.xmlhttp;
var cc = proxies.current;
var rtype = null;
if (cc && cc.rtype && (cc.rtype.length > 0) && (cc.rtype[0] != null))
rtype = cc.rtype[0].split(':');
if ((x != null) && (x.readyState == 4)) {
if (x.status == 200) {
var xNode = null;
if (rtype != null)
xNode = x.responseXML.getElementsByTagName(rtype[0])[0];
if (xNode == null) {
ret = null;
} else if (xNode.firstChild == null) { // 27.12.2005: empty string return values
ret = ((rtype.length == 1) || (rtype[1] == "string") ? "" : null);
} else if ((rtype.length == 1) || (rtype[1] == "string")) {
ret = xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue;
} else if (rtype[1] == "bool") {
ret = xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue;
ret = (ret == "true");
} else if (rtype[1] == "int") {
ret = xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue;
ret = parseInt(ret);
} else if (rtype[1] == "float") {
ret = xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue;
ret = parseFloat(ret);
} else if ((rtype[1] == "x") && (typeof(XMLSerializer) != "undefined")) {
ret = (new XMLSerializer()).serializeToString(xNode.firstChild);
ret = ajax._getXMLDOM(ret);
} else if ((rtype[1] == "ds") && (typeof(XMLSerializer) != "undefined")) {
ret = (new XMLSerializer()).serializeToString(xNode);
ret = ajax._getXMLDOM(ret);
} else if (rtype[1] == "x") {
ret = xNode.firstChild.xml;
ret = ajax._getXMLDOM(ret);
} else if (rtype[1] == "ds") {
ret = xNode.xml;
ret = ajax._getXMLDOM(ret);
} else if (rtype[1] == "s[]") {
ret = new Array();
xNode = xNode.firstChild;
while (xNode != null) {
try {
ret.push(xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue);
} catch (excep) {
ret.push("");
}
xNode = xNode.nextSibling;
}
} else if (rtype[1] == "int[]") {
ret = new Array();
xNode = xNode.firstChild;
while (xNode != null) {
ret.push(parseInt(xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue));
xNode = xNode.nextSibling;
}
} else if (rtype[1] == "float[]") {
ret = new Array();
xNode = xNode.firstChild;
while (xNode != null) {
ret.push(parseFloat(xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue));
xNode = xNode.nextSibling;
}
} else if (rtype[1] == "bool[]") {
ret = new Array();
xNode = xNode.firstChild;
while (xNode != null) {
ret.push((xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue).toLowerCase() == "true");
xNode = xNode.nextSibling;
}
} else  {
ret = xNode.textContent || xNode.innerText || xNode.text || xNode.childNodes[0].nodeValue;
}
if ((cc._cache != null) && (cc._cachekey != null)) {
cc._cache[cc._cachekey] = ret;
cc._cachekey = null;
}
proxies.xmlhttp = null;
proxies.current = null;
if (cc.func == null) {
return(ret);
} else {
cc.func(ret);
return(null);
}
} else if (proxies.current.onException == null) {
} else {
ret = new Error();
if (x.status == 404) {
ret.message = "The webservice could not be found.";
} else if (x.status == 500) {
ret.name = "SoapException";
var n = x.responseXML.documentElement.firstChild.firstChild.firstChild;
while (n != null) {
if (n.nodeName == "faultcode") ret.message = n.firstChild.nodeValue;
if (n.nodeName == "faultstring") ret.description = n.firstChild.nodeValue;
n = n.nextSibling;
}
} else if ((x.status == 502) || (x.status == 12031)) {
ret.message = "The server could not be found.";
} else {
ret.message = "Result-Status:" + x.status + "\n" + x.responseText;
}
proxies.current.onException(ret);
}
proxies.xmlhttp = null;
proxies.current = null;
}
}

proxies.alertResult = function () {
var x = proxies.xmlhttp;
if (x.readyState == 4) {
if (x.status == 200) {
if (x.responseXML.documentElement.firstChild.firstChild.firstChild == null)
alert("(no result)");
else
alert(x.responseXML.documentElement.firstChild.firstChild.firstChild.firstChild.nodeValue);
} else if (x.status == 404) { alert("Error!\n\nThe webservice could not be found.");
} else if (x.status == 500) {
var ex = new Error();
ex.name = "SoapException";
var n = x.responseXML.documentElement.firstChild.firstChild.firstChild;
while (n != null) {
if (n.nodeName == "faultcode") ex.message = n.firstChild.nodeValue;
if (n.nodeName == "faultstring") ex.description = n.firstChild.nodeValue;
n = n.nextSibling;
}
alert("The server threw an exception.\n\n" + ex.message + "\n\n" + ex.description);
} else if (x.status == 502) { alert("Error!\n\nThe server could not be found.");
} else {
alert("Result-Status:" + x.status + "\n" + x.responseText);
}
proxies.xmlhttp = null;
proxies.current = null;
}
}

proxies.alertResponseText = function () {
if (proxies.xmlhttp.readyState == 4)
alert("Status:" + proxies.xmlhttp.status + "\nRESULT:" + proxies.xmlhttp.responseText);
}
proxies.alertException = function(ex) {
var s = "Exception:\n\n";
if (ex.constructor == String) {
s = ex;
} else {
if ((ex.name != null) && (ex.name != ""))
s += "Type: " + ex.name + "\n\n";
if ((ex.message != null) && (ex.message != ""))
s += "Message:\n" + ex.message + "\n\n";
if ((ex.description != null) && (ex.description != "") && (ex.message != ex.description))
s += "Description:\n" + ex.description + "\n\n";
}
alert(s);
}

proxies._getXHR = function () {
var x = null;
if (window.XMLHttpRequest) {
x = new XMLHttpRequest()
} else if (window.ActiveXObject) {
try { x = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
if (x == null)
try { x = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
}
return(x);
}

ajax._getXMLDOM = function (xmlText) {
var obj = null;
if ((document.implementation != null) && (typeof document.implementation.createDocument == "function")) {
var parser = new DOMParser();
obj = parser.parseFromString(xmlText, "text/xml");
} else {    
try {
obj = new ActiveXObject("MSXML2.DOMDocument");
} catch (e) { }
if (obj == null) {
try {
obj = new ActiveXObject("Microsoft.XMLDOM");
} catch (e) { }
}
if (obj != null) {
obj.async = false;
obj.validateOnParse = false;
}
obj.loadXML(xmlText);
}
return(obj);
}

function inspectObj(obj) {
var s = "InspectObj:";
if (obj == null) {
s = "(null)"; alert(s); return;
} else if (obj.constructor == String) {
s = "\"" + obj + "\"";
} else if (obj.constructor == Array) {
s += " _ARRAY";
} else if (typeof(obj) == "function") {
s += " [function]" + obj;
} else if ((typeof(XMLSerializer) != "undefined") && (obj.constructor == XMLDocument)) {
s = "[XMLDocument]:\n" + (new XMLSerializer()).serializeToString(obj.firstChild);
alert(s); return;
} else if ((obj.constructor == null) && (typeof(obj) == "object") && (obj.xml != null)) {
s = "[XML]:\n" + obj.xml;
alert(s); return;
}
for (p in obj) {
try {
if (obj[p] == null) {
s += "\n" + String(p) + " (...)";
} else if (typeof(obj[p]) == "function") {
s += "\n" + String(p) + " [function]";
} else if (obj[p].constructor == Array) {
s += "\n" + String(p) + " [ARRAY]: " + obj[p];
for (n = 0; n < obj[p].length; n++)
s += "\n  " + n + ": " + obj[p][n];
} else {
s += "\n" + String(p) + " [" + typeof(obj[p]) + "]: " + obj[p];
}
} catch (e) { s+= e;}
}
alert(s);
}

