﻿function HttpConnection(oProperties) {
    this.Properties = oProperties;
    this.SetXmlHttpObject();
    if (this.XmlHttpObject == null)
        return;

    // Set default properties
    if (this.Properties.asynchronous == null) this.Properties.asynchronous = true;
    if (this.Properties.method == null) this.Properties.method = 'GET';

    // Append query string to prevent browser cache problems
    this.AppendQuerystring(escape(new Date()));
}

HttpConnection.prototype.AppendQuerystring = function(sProperty, sValue) {
    if (this.XmlHttpObject == null)
        return;

    this.Properties.queryString = (!this.Properties.queryString) ? '?' : this.Properties.queryString + '&';
    this.Properties.queryString += sProperty;
    if (sValue != null && sValue != '')
        this.Properties.queryString += '=' + sValue;
}

HttpConnection.prototype.GetResponse = function() {
    if (this.XmlHttpObject == null)
        return;

    if (this.Properties.asynchronous && this.XmlHttpObject.onreadystatechange == null)
        throw 'HttpConnection.GetResponse: Please set the asynchronous flag to false, or first set a callback method using SetCallbackMethod()';

    if (!this.Properties.asynchronous && top.oCanvas != null)
        top.oCanvas.OpenWaitDialog('De opdracht wordt verwerkt, een moment geduld...');

    this.Properties.method = this.Properties.method.toUpperCase();
    switch (this.Properties.method) {
        case 'POST':
            this.XmlHttpObject.open(this.Properties.method, this.Properties.url, this.Properties.asynchronous);
            this.XmlHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            this.XmlHttpObject.send(this.Properties.queryString);
            break;

        case 'GET':
            this.XmlHttpObject.open(this.Properties.method, this.Properties.url + this.Properties.queryString, this.Properties.asynchronous);
            this.XmlHttpObject.send(null);
            break;

        default:
            throw 'Invalid HTTP request method \'' + this.Properties.method + '\'';
    }

    if (!this.Properties.asynchronous) {
        // TODO: Check if this.XmlHttpObject.status is 200, otherwise handle error

        if (top.oCanvas != null)
            top.oCanvas.CloseWaitDialog();

        // Return response text
        return this.XmlHttpObject.responseText;
    }
}

HttpConnection.prototype.SetCallbackMethod = function(mMethod, oParams) {
    if (this.XmlHttpObject == null || mMethod == null)
        return;

    var xmlHttpObject = this.XmlHttpObject;
    xmlHttpObject.onreadystatechange = function() {
        // TODO: Check if this.XmlHttpObject.status is 200, otherwise handle error

        mMethod(xmlHttpObject, oParams);
    }
}

HttpConnection.prototype.SetXmlHttpObject = function() {
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try { this.XmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
        try { this.XmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (E) { this.XmlHttpObject = null; }
    }
    @end@*/
    if (this.XmlHttpObject == null && typeof XMLHttpRequest != 'undefined')
        try { this.XmlHttpObject = new XMLHttpRequest(); } catch (e) { this.XmlHttpObject = null; }
    if (this.XmlHttpObject == null && window.createRequest)
        try { this.XmlHttpObject = window.createRequest(); } catch (e) { this.XmlHttpObject = null; }
}