// xmtHttp functions ***********************************


var xmlHttp = false;
// Mozilla, Opera, Safari, Internet Explorer 7
if (typeof(XMLHttpRequest) != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}
if (!xmlHttp) {
    // Internet Explorer <= 6
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            xmlHttp = false;
        }
    }
}

var xmlHttpActions = new Array();
var xmlHttpTodo = new Array();



// content: 'type.item.property'
// action: 'chgdiv:id' || 'func:function'

function sendXmlHttp(contentcode, action, parameter) {
    if (typeof sessionid == 'undefined' || sessionid.length == 0) {
        alert("SessionID not set!");
    }

    if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {
        xmlHttpActions[contentcode] = action;
        var uv="";
        if(typeof urlVari1 != 'undefined'){
            uv+="&urlVari1="+urlVari1;
        }
        if(typeof urlVari2 != 'undefined'){
            uv+="&urlVari2="+urlVari2;
        }
        xmlHttp.open('get', '/remote?action=itva&itva=' + contentcode + uv +'&session=' + sessionid + '&' + parameter);

        var x = 0;

        while (getCookie('clip' + x) != null) {
            xmlHttp.setRequestHeader("Cookie", 'clip' + x + '=' + getCookie('clip' + x));
            x++;
        }
        deleteClipCookies();
        xmlHttp.onreadystatechange = handleXmlHttpResponse;
        xmlHttp.send(null);
    }
    else {
        xmlHttpTodo[contentcode] = action + "|" + parameter; // queue
    }
}

function handleXmlHttpResponse() {
    if (xmlHttp.readyState == 4) {
        var response = xmlHttp.responseText;
        var tagstart = response.indexOf('<xmlHttp>');
        var tagend = response.indexOf('</xmlHttp>');
        if (tagstart > -1 && tagend > -1) {
            var content = response.substring(tagend + 11);
            var contentcode = response.substring(tagstart + 9, tagend);
            var action = xmlHttpActions[contentcode];
            var actiondetail = action.split(':');

            if (actiondetail[0] == 'chgdiv') {
                if (content.length > 0) {
                    if (content == "_clearAnHide") {
                        //Element.update(actiondetail[1], ""); //--Prototype
                        //Element.hide(actiondetail[1]); //--Prototype
						$("div#"+actiondetail[1]).html("");
						$("div#"+actiondetail[1]).hide();
                    } else {
                        //Element.update(actiondetail[1], content);
						$("div#"+actiondetail[1]).html(content);
                        var func = "afterupdate_" + actiondetail[1];
                        if (window[func]) eval(func + "()");
                    }
                }
            }
            if (actiondetail[0] == 'func') {
                var func = actiondetail[1] + "(content)";
                eval(func);
            }
        }

        for (var contentcode in xmlHttpTodo) {

            if (contentcode.indexOf('.') > -1) {
                if (xmlHttpTodo[contentcode] != null) {
                    var ap = xmlHttpTodo[contentcode].split('|');
                    xmlHttpTodo[contentcode] = null;
                    sendXmlHttp(contentcode, ap[0], ap[1]);
	        	  	//alert("Fertig! habe noch was aufzuholen: "+contentcode+" -> "+xmlHttpTodo[contentcode]);
                    break;
                }
            }
        }
    }
}
