/*
 * Dbg:
 *   pe(txt): prints errors
 *   pw(txt): prints warnings
 *   pi(txt): print informational messages
 *   pd(txt): prints further messages
 *
 * Browser: --> static methods for browser dependend functions.
 *   encodeURIComponent(text)
 *   createArray() --> Array with methode push and pop
 *   invokeEventHandler(node, name, event) --> Executes the eventhandler at node with the given event
 *   getKeyCode(event) --> gets the key code
 *
 * createRequest():
 *   creates an HTTPRequest object
 *
 * createUID():
 *   returns an uid
 *
 * notifyRemoveNode(node):
 *   Should be called, before a node is removed (e.g. to execute onmouseout events)
 *
 * HTTPRequest: (encapsulated)
 *   Methods:
 *     setRequestHeader(key,value)
 *     open(method,url,asynchron)
 *     send(postdata)
 *     getResponseHeader(key)
 *   Fields:
 *     status
 *     responseText
 *   Callbacks:
 *     onreadystatechange --> function()
 */

// Constants
var ajaxPrefixes = /WEB0|log&/;

var ajaxEnabledLevel = 1;
// IE5 doesn't support in keyword
// Netscape6 crashes if we use window.XMLHttpRequest
if (!(window.XMLHttpRequest || window.ActiveXObject))
    ajaxEnabledLevel = 2;

var remoteLogID = null;
var postDocumentHook = null;
var pageLoadedHandlers = null;

// Todo: make javascript dynamic and multilingual
// Todo: encapsulate ajax connection into a class

// Console
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// provides function Dbg.pe, .pw, .pd and .pi
// Logs to firebug plugin and framework console if in localhost

function DbgClass() {
    this.log = function(what, text) {
        intlog(what, text);
    };
    this.pw = function(text) {
        this.log("w", text);
    };
    this.pe = function(text) {
        this.log("e", text);
    };
    this.pd = function(text) {
        this.log("d", text);
    };
    this.pi = function(text) {
        this.log("i", text);
    };
}

function BrowserClass() {
    this.encodeURIComponent = window.ActiveXObject ? function(text) {
        text = "" + text;
        return text.replace(/&/, "&amp;").replace(/ /, "%20");
    } : encodeURIComponent;

    this.createArray = window.ActiveXObject ? function() {
        var array = new Array();
        array.push = function(elem) {
            array[array.length] = elem;
        }
        array.pop = function(elem) {
            var elem = array[array.length - 1];
            array.length = array.length - 1;
            return elem;
        }
        return array;
    } : function() {
        return new Array();
    };
    this.getKeyCode = function(event) {
        if (!event)
            event = window.event;
        if (event.which)
            return event.which;
        return event.keyCode;
    }
    this.invokeEventHandler = function(node, what, event) {
        if (window.event || !event)
            return node['on' + what]();
        else
            return node['on' + what](event);
    }
}

// Wie in java :)
var Dbg = new DbgClass();
var Browser = new BrowserClass();

function encodeURIComponent(text) {
    text = "" + text;
    return text.replace(/&/, "&amp;").replace(/ /, "%20");
}

function intlog(level, text) {
    if (self.location.host.indexOf("localhost") == 0
            || self.location.host.indexOf("127.") == 0
            || self.location.host.indexOf("l:") == 0
            || self.location.host == "l") {
        var request = makeRequest("http://" + self.location.host + "/log&"
                + level + "&" + encodeURIComponent(text));
        if (request)
            request.onreadystatechange = function() {
            };
    } else if (remoteLogID != null) {
        var request = makeRequest(self.location.href.substring(0,
                self.location.href.lastIndexOf('?') + 1)
                + "/log&"
                + level
                + "&"
                + encodeURIComponent(text)
                + "&id="
                + escape(remoteLogID));
        if (request)
            request.onreadystatechange = function() {
            };
    }
    if (window.console) {
        switch (level) {
        case 'w':
            window.console.warn(text);
            break;
        case 'e':
            window.console.error(text);
            break;
        case 'd':
            window.console.debug(text);
            break;
        case 'i':
            window.console.info(text);
            break;
        }
        window.console.trace();
    }
}

// Version 1
// Selfhtml
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// See Licence.txt

var DHTML = false, DOM = false, MSIE4 = false, NS4 = false, OP = false;

if (document.getElementById) {
    DHTML = true;
    DOM = true;
} else {
    if (document.all) {
        DHTML = true;
        MSIE4 = true;
    } else {
        if (document.layers) {
            DHTML = true;
            NS4 = true;
        }
    }
}
if (window.opera) {
    OP = true;
}

function gotit(http_request, nothingurl) {
    try {
        unMarkWaiting();
        if (http_request.readyState == 4) {
            if (http_request.status == 200 || http_request.status == 0) {
                var seperator = http_request.getResponseHeader("ajaxseperator");
                var contenttype = http_request
                        .getResponseHeader("Content-Type");
                if (seperator) {
                    swapHTML(seperator, http_request.responseText, http_request);
                } else if (http_request.responseText
                        .indexOf("---AJAXREDIRECT---") == 0) {
                    // todo: chaning location is filtered on some proxies
                    self.location = self.location.href.substring(0,
                            self.location.href.lastIndexOf('/') + 1)
                            + http_request.responseText.substring(18);
                } else {
                    Dbg.pw("No seperator found!")
                    if (nothingurl != "ignore")
                        self.location = nothingurl;
                }
            } else {
                Dbg.pw("Error: ", http_request.status)
                if (nothingurl != "ignore")
                    self.location = nothingurl;
            }
        }
    } catch (e) {
        if (nothingurl != "ignore") {
            Dbg.pe(e);
            self.location = nothingurl;
        } else {
            Dbg.pw(e);
        }
    }
};

function getElement(Mode, Identifier, ElementNumber) {
    var Element, ElementList;
    if (DOM) {
        if (Mode.toLowerCase() == "id") {
            Element = document.getElementById(Identifier);
            if (!Element) {
                Element = false;
            }
            return Element;
        }
        if (Mode.toLowerCase() == "name") {
            ElementList = document.getElementsByName(Identifier);
            Element = ElementList[ElementNumber];
            if (!Element) {
                Element = false;
            }
            return Element;
        }
        if (Mode.toLowerCase() == "tagname") {
            ElementList = document.getElementsByTagName(Identifier);
            Element = ElementList[ElementNumber];
            if (!Element) {
                Element = false;
            }
            return Element;
        }
        return false;
    }
    if (MSIE4) {
        if (Mode.toLowerCase() == "id" || Mode.toLowerCase() == "name") {
            Element = document.all(Identifier);
            if (!Element) {
                Element = false;
            }
            return Element;
        }
        if (Mode.toLowerCase() == "tagname") {
            ElementList = document.all.tags(Identifier);
            Element = ElementList[ElementNumber];
            if (!Element) {
                Element = false;
            }
            return Element;
        }
        return false;
    }
    if (NS4) {
        if (Mode.toLowerCase() == "id" || Mode.toLowerCase() == "name") {
            Element = document[Identifier];
            if (!Element) {
                Element = document.anchors[Identifier];
            }
            if (!Element) {
                Element = false;
            }
            return Element;
        }
        if (Mode.toLowerCase() == "layerindex") {
            Element = document.layers[Identifier];
            if (!Element) {
                Element = false;
            }
            return Element;
        }
        return false;
    }
    return false;
}

function getAttribute(Mode, Identifier, ElementNumber, AttributeName) {
    var Attribute;
    var Element = getElement(Mode, Identifier, ElementNumber);
    if (!Element) {
        return false;
    }
    if (DOM || MSIE4) {
        Attribute = Element.getAttribute(AttributeName);
        return Attribute;
    }
    if (NS4) {
        Attribute = Element[AttributeName]
        if (!Attribute) {
            Attribute = false;
        }
        return Attribute;
    }
    return false;
}

function getContent(Mode, Identifier, ElementNumber) {
    var Content;
    var Element = getElement(Mode, Identifier, ElementNumber);
    if (!Element) {
        return false;
    }
    if (DOM && Element.firstChild) {
        if (Element.firstChild.nodeType == 3) {
            Content = Element.firstChild.nodeValue;
        } else {
            Content = "";
        }
        return Content;
    }
    if (MSIE4) {
        Content = Element.innerText;
        return Content;
    }
    return false;
}

// Framework spezifisch
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function createRequest() {
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            // response is not xml-data:
            // http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/plain');
            // See note below about this line
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
            }
        }
    }
    var request = {
        setRequestHeader : function(a, b) {
            http_request.setRequestHeader(a, b);
        },
        open : function(a, b, c) {
            http_request.open(a, b, c);
        },
        send : function(a) {
            http_request.send(a);
        },
        getResponseHeader : function(a) {
            return http_request.getResponseHeader(a);
        }
    };
    http_request.onreadystatechange = function() {
        if (request.onreadystatechange) {
            request.readyState = http_request.readyState;
            if (request.readyState == 4) {
                try {
                    // In case of errors
                    request.status = http_request.status;
                    request.responseText = http_request.responseText;
                } catch(e) {
                }
            }
            request.onreadystatechange();
        }
    };
    return request;
}

function makeRequest(url, post, boundary) {
    if (url.search(ajaxPrefixes) < 0)
        return false;
    return makeRequestChecked(url, post, boundary);
}

function makeRequestChecked(url, post, boundary) {
    var http_request = createRequest();
    var i = url.indexOf("#");
    if (i > 0)
        url = url.substring(0, i);

    if (!http_request) {
        Dbg.pw('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    var dirdepth=0;
    for (var j=0; j<url.length-3; j+=3) {
        if (url.substring(j, j+3)=="../")
            dirdepth++;
        else
            break;
    }
    if (dirdepth>0)
        url=url+"&ajaxdd="+dirdepth;
    if (post) {
        http_request.open('POST', url, true);
        http_request.setRequestHeader("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        http_request.setRequestHeader("Content-Length", post.length);
        http_request.setRequestHeader("Ajax", "true");
        http_request.send(post + "\r\n");
    } else {
        http_request.open('GET', url, true);
        http_request.setRequestHeader("Content-Type", "application/ajax");
        http_request.setRequestHeader("Ajax", "true");
        http_request.send(null);
    }
    return http_request;
}

function executeAjaxRequest(url, post) {
    var r = makeRequestChecked(url, post, "--");
    r.onreadystatechange = function() {
        gotit(r, "ignore");
    };
}

var executeAfterSwaps = Browser.createArray();

function executeAfterSwap(myfunc) {
    executeAfterSwaps.push(myfunc);
}

function createJavascriptWindow(httprequest, elementnr) {
    var div = getElementById(elementnr);
    var exist = div != null;
    if (!exist) {
        div = document.createElement("div");
        div.id = elementnr;
    }
    httprequest.placeJavascriptWindow(div, exist);
}

function executeJavascript(code, httprequesta) {
    // todo: murks, man muss alles script-Tags klein schreiben
    var lcode = code /* .toLowerCase(); */
    var old = 0;
    var newcode = new String();
    while (true) {
        var l = lcode.indexOf("<script", old);
        if (l < 0)
            break;
        var j = lcode.indexOf("</script", l);
        if (j < 0)
            break;
        var k = lcode.indexOf(">", l);
        var m = lcode.indexOf(">", j);
        var script = code.substring(k + 1, j);
        var replacement = "";
        var httprequest = httprequesta;
        script = script.replace(/^ *<!--/, "");
        script = "var documentoldwrite=document.write;document.write=function(text) { replacement=replacement+text; };"
                + script + "\n; document.write=documentoldwrite;";
        eval(script);
        newcode = newcode.concat(code.substring(old, l));
        if (replacement != "")
            newcode = newcode.concat(replacement);
        old = m + 1;
    }
    newcode = newcode.concat(code.substring(old));
    return newcode;
}

function changeNode(bildbereich, code) {
    if (OP) {
        var old = bildbereich.style.display;
        bildbereich.style.display = "none";
        bildbereich.innerHTML = code;
        bildbereich.style.display = old;
    } else if (window.ActiveXObject) {
        var old = bildbereich.style.display;
        bildbereich.style.display = "none";
        bildbereich.innerHTML = code;
        bildbereich.style.display = old;
    } else {
    	/*var newEl = bildbereich.cloneNode(false);
    	newEl.innerHTML = code;
    	bildbereich.parentNode.replaceChild(newEl, bildbereich);*/
    	bildbereich.innerHTML = code;
    }
}

function notifyRemoveNode(node) {
    var myremoveHooks = removeHooks;
    for ( var j = 0; j < myremoveHooks.length; j++)
        if (isChildOf(myremoveHooks[j], node))
            myremoveHooks[j].removeHook(myremoveHooks[j]);
}

//var oldmarked=Browser.createArray();
function swapHTML(seperator, allcode, httprequest) {
    var old = 0;
    var ids = Browser.createArray();
    var sl = seperator.length;
//    for (var i=0; i<oldmarked.length; i++)
//        oldmarked[i].style.backgroundColor="transparent";
//    oldmarked=Browser.createArray();
    while (true) {
        var m = allcode.indexOf(seperator, old);
        if (m < 0)
            break;
        var n = allcode.indexOf(seperator, m + sl);
        if (n < 0)
            break;
        var id = allcode.substring(old, m);
        var code = allcode.substring(m + sl, n);
        old = n + sl;
        ids.push(id);
        code = executeJavascript(code, httprequest);
        bildbereich = getElement("id", id); // after executejavascript! see
        // GMouseOverComponent
        if (!bildbereich) {
            throw "Missing node: " + id;
        } else {
            notifyRemoveNode(bildbereich);
//            var d=new Date();
            changeNode(bildbereich, code);
//            Dbg.pi("Time: "+(new Date().getTime()-d.getTime()));
//            oldmarked.push(bildbereich);
//            bildbereich.style.backgroundColor="#8888FF";
        }
    }
    // execute post document hook (for mapping alt to title etc.)
    for ( var k = 0; k < executeAfterSwaps.length; k++)
        executeAfterSwaps[k]();
    executeAfterSwaps = Browser.createArray();

    if (postDocumentHook != null)
        postDocumentHook(ids);
}

function isChildOf(child, parent) {
    while (child) {
        if (child == parent)
            return true;
        child = child.parentNode;
    }
    return false;
}

var removeHooks = Browser.createArray();
/**
 * Registers an mouse hook for a div.
 *
 * @param div
 *            the div to register
 * @param func
 *            the function, which is called if this div is removed through ajax,
 *            the function get the div as parameter
 */
function registerRemoveHook(div, func) {
    if (!div.removeHook)
        removeHooks.push(div);
    div.removeHook = func;
}

/**
 * Registers the mouse out event of an html element (unregisters from the
 * mouseIn-List)
 *
 * @param div
 *            the div to unregister
 */
function unregisterRemoveHook(div) {
    var newremoveHooks = Browser.createArray();
    for ( var i = 0; i < removeHooks.length; i++)
        if (removeHooks[i] != div)
            newremoveHooks.push(removeHooks[i]);
    removeHooks = newremoveHooks;
    div.removeHook = null;
}

var waitingObject;
function markWaiting(object) {
    unMarkWaiting();
    waitingObject = object;
    // object.style.cursor = "wait";
    waitingObject.delayTimeout = window.setTimeout(function() {
        if (object != waitingObject)
            return;
        waitingObject.delayTimeout = null;
        document.body.style.cursor = "wait";
        waitingObject = waitingObject.parentNode;
        var turlyWurly = document.createElement("div");
        turlyWurly.innerHTML = "&nbsp;";
        turlyWurly.style.position = "absolute";
        // turlyWurly.style.display = "none";
            if (Browser.getPosition) {
                var relparent = waitingObject;
                while (relparent.parentNode != null) {
                    var pos = Browser.getStyle(relparent, "position");
                    if (pos == "absolute" || pos == "relative")
                        break;
                    relparent = relparent.parentNode;
                }
                var t1 = Browser.getPosition(object);
                var t2 = Browser.getPosition(relparent);
                var left = t1.left - t2.left;
                var top = t1.top - t2.top;
                turlyWurly.style.top = top + "px";
                turlyWurly.style.left = left + "px";
            } else {
                turlyWurly.style.top = "0px";
                turlyWurly.style.left = "0px";
            }
            turlyWurly.className = "turlyWurly";
            waitingObject.appendChild(turlyWurly);
            turlyWurly.style.display = "block";
            waitingObject.turlyWurly = turlyWurly;
            waitingObject.object = object;
        }, 250);
}

function unMarkWaiting() {
    if (waitingObject) {
        // waitingObject.object.style.cursor = "default";
        document.body.style.cursor = "auto";
        if (waitingObject.delayTimeout) {
            window.clearTimeout(waitingObject.delayTimeout);
            waitingObject.delayTimeout = null;
        }
        if (waitingObject.turlyWurly)
            waitingObject.removeChild(waitingObject.turlyWurly);
        waitingObject = null;
    }
}

function replaceFromServer(link, randnr, ajaxEnabled) {
    if (!replaceFromServerUrl(link.href, randnr, ajaxEnabled)) {
        markWaiting(link);
        return false;
    }
    return true;
}
function replaceFromServerUrl(url2, randnr, ajaxEnabled) {
    if (ajaxEnabled < ajaxEnabledLevel)
        return true;
    var url = url2 + "&ajaxrnd=" + randnr;
    try {
        var http_request = makeRequest(url, false);
        if (!http_request)
            return true;
        http_request.onreadystatechange = function() {
            gotit(http_request, url2);
        };
        return false;
    } catch (e) {
        Dbg.pe(e);
        return true;
    }
}
function fw_rfs(self, ajaxEnabledOpt) {
    if (!ajaxEnabledOpt && ajaxEnabledOpt != 0)
        ajaxEnabledOpt = 1;
    return replaceFromServer(self, Math.random(), ajaxEnabledOpt);
}

var fwSelectedElement = null;
var noformsubmit = false;

function preventFormularSubmit() {
    var my = new Object();
    noformsubmit = my;
    window.setTimeOut(function() {
        if (noformsubmit == my)
            noformsubmit = null;
    }, 10);
}

function checkAjaxFormular() {
    if (noformsubmit)
        return false;
    var button = fwSelectedElement;
    fwSelectedElement = null;
    if (button) {
        var form = button.form;
        var url = form.action;
        var post = "";
        var boundary = "------------------" + Math.random();
        var elements = form.elements;
        for ( var i = 0; i < elements.length; ++i) {
            var element = elements[i];
            if (!element || !element.type) {
            } else if (element.type == "submit") {
            } else if (!element.name || element.name == "") {
            } else if (element.type == "textarea" || element.type == "text"
                    || element.type == "hidden" || element.type == "password") {
                post += "--" + boundary + "\r\n"
                        + "Content-Disposition: form-data; name=\""
                        + element.name + "\"\r\n\r\n" + element.value + "\r\n";
            } else if (element.type == "select" || element.type == "select-one"
                    || element.type == "select-multiple") {
                for ( var j = 0; j < element.options.length; j++) {
                    if (element.options[j].selected == true)
                        post += "--" + boundary + "\r\n"
                                + "Content-Disposition: form-data; name=\""
                                + element.name + "\"\r\n\r\n"
                                + element.options[j].value + "\r\n";
                }
            } else if (element.type == "radio" || element.type == "checkbox") {
                if (element.checked)
                    post += "--" + boundary + "\r\n"
                            + "Content-Disposition: form-data; name=\""
                            + element.name + "\"\r\n\r\n" + element.value
                            + "\r\n";
            } else {
                Dbg.pw("Posting element-type '" + element.type + "' of "
                        + element.name + " not supported yet!");
                return true;
            }
        }
        post += "--"
                + boundary
                + "\r\n"
                + "Content-Disposition: form-data; name=\"ajaxrequest\"\r\n\r\ntrue\r\n";
        post += "--" + boundary + "\r\n"
                + "Content-Disposition: form-data; name=\"" + button.name
                + ".x\"\r\n\r\ntrue\r\n";
        post += "--" + boundary + "\r\n"
                + "Content-Disposition: form-data; name=\"" + button.name
                + ".y\"\r\n\r\ntrue\r\n";
        post += "--" + boundary + "--";
        try {
            var http_request = makeRequest(url, post, boundary);
            if (!http_request) {
                return true;
            }
            markWaiting(button);
            http_request.onreadystatechange = function() {
                // Todo: Resubmit of whole form in case of errors instead of a
                // refresh
                gotit(http_request, url);
            };
            return false;
        } catch (e) {
            Dbg.pe(e);
            return true;
        }
    } else {
        return true;
    }
}

function createPost(map, boundary) {
    if (!boundary)
        boundary = "--";
    var post = "";
    for ( var i in map) {
        if (map[i] != null) {
            post += "--" + boundary + "\r\n"
                    + "Content-Disposition: form-data; name=\"" + i
                    + "\"\r\n\r\n" + map[i] + "\r\n";
        }
    }
    post += "--"
            + boundary
            + "\r\n"
            + "Content-Disposition: form-data; name=\"ajaxrequest\"\r\n\r\ntrue\r\n";
    post += "--" + boundary + "--";
    return post;
}

function fw_caf() {
    return checkAjaxFormular();
}

function registerAjaxButton(self, ajaxEnabled) {
    if (ajaxEnabled < ajaxEnabledLevel)
        return true;
    fwSelectedElement = self;
}

function fw_rab(self, ajaxEnabledOpt) {
    if (!ajaxEnabledOpt && ajaxEnabledOpt != 0)
        ajaxEnabledOpt = 1;
    return registerAjaxButton(self, ajaxEnabledOpt);
}
// MouseOverComponent --------------------------------------------------------

function mouseOverMOC(self, time, href, ajaxEnabled) {
    if (ajaxEnabled < ajaxEnabledLevel)
        return;
    var popupElement = self.parentNode;
    if (!popupElement.open)
        popupElement.open = Browser.createArray();
    var exists = false;
    for ( var i = 0; i < popupElement.open.length; i++)
        if (popupElement.open[i] == self)
            exists = true;
    if (!exists)
        popupElement.open.push(self);
    if (popupElement.open.length == 1) {
        window.setTimeout(function() {
            if (!(popupElement.loaded)) {
                var http_request = makeRequest(href + "&moc");
                if (!http_request) {
                    // window.location=href;
                return;
            } else {
                http_request.onreadystatechange = function() {
                    gotit(http_request, "ignore");
                };
                popupElement.loaded = true;
            }
        }
        if (popupElement.open.length > 0) {
            popupElement.childNodes[1].className = "ajaxMouseOverShow";
            popupElement.className = "ajaxMouseOverPShow";
            popupElement.childNodes[1].style.top = popupElement.offsetHeight
                    + "px";
        }
    }, time);
    }
}

function mouseOutMOC(self) {
    var popupElement = self.parentNode;
    var newArray = Browser.createArray();
    if (popupElement.open)
        for ( var i = 0; i < popupElement.open.length; i++)
            if (popupElement.open[i] != self)
                newArray.push(popupElement.open[i]);
    popupElement.open = newArray;
    window.setTimeout(function() {
        if (!(popupElement.open) || popupElement.open.length == 0) {
            popupElement.childNodes[1].className = "ajaxMouseOverHide";
            popupElement.className = "ajaxMouseOverPHide";
        }
    }, 10);
}

function addPageLoadedHandler(func) {
    if (pageLoadedHandlers == null)
        pageLoadedHandlers = Browser.createArray();
    pageLoadedHandlers.push(func);
}

function pageLoaded() {
    if (pageLoadedHandlers != null) {
        for ( var i = 0; i < pageLoadedHandlers.length; i++) {
            pageLoadedHandlers[i]();
        }
        pageLoadedHandlers = null;
    }
}

var nextuid = 0;
function createUID() {
    nextuid++;
    return nextuid;
}

function evaluateJson(json) {
    eval("var result=" + json + ";");
    if (result.result)
        return result.result;
    else
        throw result.exception;
}

function executeJsonAsynchron(url, methodsname, parameters, callbackfunction,
        errorfunction) {
    var http_request = createRequest();
    if (!http_request)
        throw "Could not create ajax object";
    http_request.open('POST', url, callbackfunction != null);
    var json = "{" + methodsname + ": [" + parameters.join(",") + "]}";
    http_request.setRequestHeader("Content-Type", "application/json");
    http_request.setRequestHeader("Content-Length", json.length);
    http_request.send(json + "\r\n");
    if (callbackfunction != null) {
        http_request.onreadystatechange = function() {
            if (http_request.readyState == 4)
                if (http_request.status == 200 || http_request.status == 0)
                    callbackfunction(evaluateJson(http_request.responseText));
                else
                    errorfunction();
        };
    } else {
        return evaluateJson(http_request.responseText);
    }
}

function executeJsonSynchron(url, methodsname, parameters) {
    return executeJsonAsynchron(url, methodsname, parameters, null);
}
