if(typeof(window.JSONREQUEST_DEFINED) == 'undefined') {
    window.JSONREQUEST_DEFINED = true;
    
    function JSONscriptRequest(fullUrl) {
        // REST request path
        this.fullUrl = fullUrl;
        // Keep IE from caching requests
        this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
        // Get the DOM location to put the script tag
        this.headLoc = document.getElementsByTagName("head").item(0);
        // Generate a unique script tag id
        this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
    }

    // Static script ID counter
    JSONscriptRequest.scriptCounter = 1;

    // buildScriptTag method
    //
    JSONscriptRequest.prototype.buildScriptTag = function () {
        // Create the script tag
        this.scriptObj = document.createElement("script");

        // Add script object attributes
        this.scriptObj.setAttribute("type", "text/javascript");
        this.scriptObj.setAttribute("charset", "utf-8");
        this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
        this.scriptObj.setAttribute("id", this.scriptId);
    };

    // removeScriptTag method
    //
    JSONscriptRequest.prototype.removeScriptTag = function () {
        // Destroy the script tag
        this.headLoc.removeChild(this.scriptObj);
    };

    // addScriptTag method
    //
    JSONscriptRequest.prototype.addScriptTag = function () {
        // Create the script tag
        this.headLoc.appendChild(this.scriptObj);
    };

    function handleJSonResponse(json_data, fields) {
        if(json_data && fields && fields.length) {
            for(var i = 0; i < fields.length; i++) {
                var field = fields[i];
                var value = json_data.view[field];

                if(value) {
                    // GMX benutzt $ui
                    var jq = (typeof($ui) == 'function') ? $ui : $;

                    var element = document.getElementById(field);

                    if(element && element != 'undefined') {
                        if(typeof setElementValue == 'function') {
                            setElementValue(element, value);
                        } else {
                            element.innerHTML = value;
                        }                        	
                    }

                    if(field.substr(0, 1) != '.') {
                        field = '.' + field;
                    }

                    var elements = jq(field);

                    if(elements && elements != 'undefined' && elements.length > 0) {
                        for(var j = 0; j < elements.length; j++) {
                            element = elements[j];
                            if(typeof setElementValue == 'function') {
                                setElementValue(element, value);
                            } else {
                                element.innerHTML = value;
                            }                        	
                        }
                    }
                }
            }
        }
    }
}
