/*------------------- TOV "IRT". Copyright 2007 ----------------------*/
Function.prototype.andThen = function(g, text, args) {
    var f = this;
    return function() {
        f();
        g(text, args);
    }
};
/*---------- Set Request Options ---------*/
function RequestOptions() {
    //Callback definitions function and method
    this.completeFunction_Args = '';
    this.errorFunction_Args = '';
    this.readyStateChangeFunction_Args = '';
    this.abortFunction_Args = '';
    
    this.completeFunction = null;
    this.errorFunction = null;
    this.readyStateChangeFunction = null;
    this.abortFunction = null;
    
    this.callback = function () {}; 
    this.registerCallback = function(callbackFunction, text, args) {
        this.callback = function () {};
        this.callback = (this.callback).andThen(callbackFunction, text, args);
    };
    
    this.registerCallbackFunction_OnComplete = function(text, args) { 
        this.registerCallback(this.completeFunction, text, args);
        this.callback();
    };
    
    this.registerCallbackFunction_OnError = function(text, args) {
        this.registerCallback(this.errorFunction, text, args);
        this.callback();
    };
    
    this.registerCallbackFunction_OnReadyStateChange = function(text, args) {
        this.registerCallback(this.readyStateChangeFunction, text, args);
        this.callback();
    };
    
    this.reqisterCallbackFunction_OnAbort = function() {
        this.registerCallback(this.abortFunction, httpRequest, 'abort');
        this.callback();
    }
    
    this.reqTimeout = 5000;
    this.controlType = ''; //control('DropDownList', 'HTML', e.t.c.) from response text
    
    this.requestPage = ''; //page to complete request
    
    var requestPages = new Object();
    requestPages["basket"] = "http://medservis.in.ua/request_basket.aspx";
    
    this.RequestPageUrl = function() {
        return requestPages[this.requestPage];
    }
    
    var params  = new Object();
   
    this.Add = function(id, value) {
        params[id] = value;   
    }
    this.Get = function(id) {
        return params[id];
    }
    this.GetAll = function() {
        var strParams = '';
        for (var i in params) {
            if (params[i] != '')
		        strParams += i + '=' + params[i] + '&';
	    }
	    if (strParams != '') strParams = strParams.substring(0, strParams.length - 1);
	    return strParams;
    }
}
/*---------- End Request Options ---------*/
/*---------- Global Variable ---------------*/
var httpRequest;
var httpReqTimeout;
var requestOptions = null;
/*---------- End Global Variable -------------*/
function createRequest() {
    try {
        httpRequest = new XMLHttpRequest();
    } 
    catch (trymicrosoft) {
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (othermicrosoft) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed) {
                httpRequest = false;
            }
        }
    }
    if (!httpRequest) {
        alert("Ваш браузер не підтримує деякі функції!");
        return false;
    }
    else {
        return true;
    }
}

function processRequest(roObj) {
    requestOptions = roObj;
    var url = prepareUrl();
    httpRequest.open("GET", url, true);
    httpRequest.onreadystatechange = processRequestChange;
    httpRequest.send(null);
    if (requestOptions.abortFunction != null) {
        httpReqTimeout = setTimeout("requestOptions.reqisterCallbackFunction_OnAbort();", requestOptions.reqTimeout);
    }
    else {
        httpReqTimeout = setTimeout("httpRequest.abort(); window.alert('Не вдалося отримати дані. Перевірте підключення до Internet');", requestOptions.reqTimeout);
    }
}
function processRequestChange() {
    if (httpRequest.readyState == 4) {
        clearTimeout(httpReqTimeout);
        if (httpRequest.status == 200) {
            if (requestOptions.completeFunction != null) {
                requestOptions.registerCallbackFunction_OnComplete(httpRequest.responseText, 
                    requestOptions.completeFunction_Args);
            }
        } 
        else {
            if (requestOptions.errorFunction != null) {
                requestOptions.registerCallbackFunction_OnError(httpRequest.statusText, "Error");
            }
        }
    }
    if (requestOptions.readyStateChangeFunction != null) {
        requestOptions.registerCallbackFunction_OnReadyStateChange(httpRequest.readyState, '123');
    }
}
function prepareUrl() {
    var pageUrl = requestOptions.RequestPageUrl();
    if (document.location.href.indexOf("www.") != -1) {
        pageUrl = "http://www." + pageUrl.substr(pageUrl.indexOf("//") + 2);
    }
    var random = Math.random();
    var params = requestOptions.GetAll();
    if (params != '') {
        params = '?' + params + '&random=' + random;
    }
    else {
        params = '?random=' + random;
    }
    
    return pageUrl + params;
}
