// ################################################
// OWN AJAX FUNCTIONS
// ################################################

var LChttp = new Array();
var LCLastURL = new Array();
var LCLastContent = new Array();

// requests the content of the given URL and starts
// asynchronous technique (LoadOutput) to insert result to
// a DOM element with ID "ID".

// By default, there is nothing loaded if it was the same URL.
// You can suppress this behaviour, if you set the optional
// ForceReload = true (default = false) to let the content
// be reloaded every time.

// Set PostDataForm = "FormName" (default = ""), if you like to
// send all form data using POST method to the request-URL.

// If the returned HTML contains <script... </script> tags,
// the contained JavaScript will get executed (once after loading).
function LoadContent(ID, URL, ForceReload, PostDataForm) {
    
    var ElementID;
    var params = "";
    
    if(!ForceReload) {
      var ForceReload = true;
    }
    if(!PostDataForm) {
      var PostDataForm = "";
    }

    document.getElementById(ID).style.display = 'none';

    if (LCLastURL[ID] == URL && ForceReload == false && PostDataForm == "") {
        // same as before
        document.getElementById(ID).innerHTML = LCLastContent[ID];
    } else {
        // request new content
        if (window.XMLHttpRequest) {
            LChttp[ID] = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            LChttp[ID] = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (LChttp[ID] != null) {
            ElementID = ID;
            // generate URL time add to force reloading
            if (ForceReload == true || PostDataForm != "") {
                // ensure a different URL
                if (URL.indexOf("?") > 0) {
                    // ? already there
                    URL = URL + "&t=" + new Date().getTime() + Math.random();
                } else {
                    // no parameters at all. Use ?
                    URL = URL + "?t=" + new Date().getTime() + Math.random();
                }
            }
            if (PostDataForm == "") {
                // use simple GET method
                document.getElementById(ID).innerHTML = "<center><img src=\"/images/warten.gif\" border=\"0\"></center>";
                LChttp[ID].open("GET", URL, true);
                LChttp[ID].onreadystatechange = LoadOutput;
                LChttp[ID].send(null);
            } else {
                // use POST
                params = formData2QueryString(PostDataForm);
                document.getElementById(ID).innerHTML = "<center><img src=\"/images/warten.gif\" border=\"0\"></center>";
                LChttp[ID].open("POST", URL, true);
                LChttp[ID].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                LChttp[ID].setRequestHeader("Content-length", params.length);
                LChttp[ID].setRequestHeader("Connection", "close");
                LChttp[ID].onreadystatechange = LoadOutput;
                LChttp[ID].send(params);
            }
            LCLastURL[ID] = URL;
        }
    }

      // outputs the result to the ElementID element
      function LoadOutput() {
        if (LChttp[ElementID].readyState == 4) {

            document.getElementById(ElementID).style.display = 'none';

            var Content = LChttp[ElementID].responseText;
            document.getElementById(ElementID).innerHTML = Content;
            LCLastContent[ElementID] = Content;
            // call script.acoul.us effect after successful loading
            // new Effect.Highlight(document.getElementById(ElementID), { startcolor: '#5a3628', endcolor: '#000000' });
            new Effect.Appear(ElementID);
            // check, if there is some javascript that needs to get started
            StartJS(Content);
        }
      }
      
      // reads all form data and returns a query string
      // needed for POST method in LoadContent().
      function formData2QueryString(docForm) {
        var submitString = '';
        var formElement = '';
        var lastElementName = '';
        var Form;
        Form = document.forms[docForm];
        if (!Form) { alert("Error. Form name " + docForm + " not found!"); }
        for(i = 0 ; i < Form.elements.length ; i++) {
            formElement = Form.elements[i];
            switch(formElement.type) {
                case 'text' :
                case 'button' :
                case 'select-one' :
                case 'hidden' :
                case 'password' :
                case 'textarea' :
                    if (formElement.name == '__VIEWSTATE' || formElement.name.substr(0,4) == 'ww__') {
                        //  input name begins with __VIEWSTATE or ww__
                        break;
                    }
                    submitString += formElement.name + '=' + paramEscape(formElement.value) + '&';
                    break;
                case 'radio' :
                    if(formElement.checked) {
                        submitString += formElement.name + '=' + paramEscape(formElement.value) + '&';
                    }
                    break;
                case 'checkbox' :
                    if(formElement.checked) {
                        if(formElement.name == lastElementName) {
                            if(submitString.lastIndexOf('&') == submitString.length - 1) {
                                submitString = submitString.substring(0, submitString.length - 1);
                            }
                            submitString += ',' + paramEscape(formElement.value);
                        } else {
                            submitString += formElement.name + '=' + paramEscape(formElement.value);
                        }
                        submitString += '&';
                        lastElementName = formElement.name;
                    }
                    break;
            }
        }
        submitString = submitString.substring(0, submitString.length - 1);
        return submitString;
      }
      
      function paramEscape(paramValue) {
        return encodeURIComponent(paramValue);
      }
}

// parse HTML code for valid JavaScript code and start
// this code using eval().
// JavaScript code needs to start with "<script"and needs to
// end using "</script>".
function StartJS(HTMLCode) {
    HTMLCodeSmall = HTMLCode.toLowerCase();
    PosStart = 0
    while (PosStart > -1) {
        var PosStart = HTMLCodeSmall.indexOf("<script", PosStart);
        if (PosStart > -1) {
            // found start script
            var PosTag = HTMLCodeSmall.indexOf(">", PosStart) - PosStart + 1;
            var PosEnd = HTMLCodeSmall.indexOf("</script>", PosStart);
            if (PosEnd > -1) {
                // found end of script
                var Script = HTMLCode.substring(PosStart + PosTag, PosEnd);
                // execute script
                eval(Script);
            }
            PosStart = PosEnd;
        }
    }
}

