
function showErrorMessage() {
}


//  crearea obiectului ajax in functie de browserul folosit
function getHttpObject() {
  var xmlhttp;

  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        var xmlhttp = false;
      }
    }
  @else
    var xmlhttp = false;
  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      var xmlhttp = new XMLHttpRequest();
    } catch (e) {
      var xmlhttp = false;
    }
  }
  return xmlhttp;
}
//  end crearea obiectului ajax in functie de browserul folosit

var httpObject = new Array();
var http_request_timeout = new Array();
var get_page_is_working = new Array();
var send_data_is_working = new Array();

function get_page_abort(httpObject, url, fnct, indentificator) {
  httpObject[indentificator].abort();
  if(fnct) fnct(false);
  js_error_message = 'Error: timeout occured when trying to load ' + url + '\n';
  get_page_is_working[indentificator] = false;
  showErrorMessage();
}

function get_page_function(httpObject, url, fnct, http_request_timeout, indentificator) {
  this.functia = fnct;
  this.http_response_text = "";
  if(get_page_is_working[indentificator]) {
    if(httpObject[indentificator].readyState == 1 || httpObject[indentificator].readyState == 2 || httpObject[indentificator].readyState == 3) {
    } else if(httpObject[indentificator].readyState == 4) {
      window.clearTimeout(http_request_timeout[indentificator]);
      try {
        if(httpObject[indentificator].status == 200 && httpObject[indentificator].statusText == "OK") {
          this.http_response_text = httpObject[indentificator].responseText;
          if(this.http_response_text.substring(0, 6) == "Error:") js_error_message = this.http_response_text + ' when trying to load ' + url + '\n';
          else if(fnct)  this.functia(this.http_response_text)  //fnct(http_response_text);
        } else {
          if(fnct) fnct(false);
          js_error_message = 'Error: ' + httpObject.statusText + ' when trying to load ' + url + '\n';
        }
      } catch(error) {
        js_error_message = 'Error: ' + error + ' when trying to load ' + url + '\n';
      }
      delete(get_page_is_working[indentificator]);
      get_page_is_working[indentificator] = false;
      showErrorMessage();
    }
  }
}

function get_page(timeout, url, param, syncron, fnct, indentificator) {
 
  if(!get_page_is_working[indentificator]) {
    httpObject[indentificator] = new Array;
    httpObject[indentificator] = getHttpObject();
//    if(httpObject[indentificator] != false) {
      var indentificator = indentificator;
      http_request_timeout[indentificator] = window.setTimeout(function () { get_page_abort(httpObject, url, fnct, indentificator); }, timeout);
      try
      {
        httpObject[indentificator].open('POST', url + '?' + param, syncron);
        httpObject[indentificator].onreadystatechange = function() { get_page_function(httpObject, url, fnct, http_request_timeout, indentificator); };
        get_page_is_working[indentificator] = true;
        httpObject[indentificator].send(null);
      }
      catch(e)
      {
        js_error_message = 'Error: ' + e + ' when trying ' + this.url + '\n Trebuie adaugate urmatoarele linii in fisierul <i>prefs.js</i>\n';
        js_error_message += '<b>user_pref("capability.principal.codebase.p0.granted", "UniversalBrowserWrite UniversalBrowserRead");\n';
        js_error_message += 'user_pref("capability.principal.codebase.p0.id", "https://abs-01.astral.ro/");\n';
        js_error_message += 'user_pref("capability.principal.codebase.p1.granted", "UniversalBrowserWrite UniversalBrowserRead");\n';
        js_error_message += 'user_pref("capability.principal.codebase.p1.id", "https://abs-01.astral.ro/");</b>\n';
        try {
          showErrorMessage();
        } catch(error) {
        }
      }
//    }
  }
}

function send_data_abort(httpObject, url, fnct, indentificator) {
  httpObject[indentificator].abort();
  if(fnct) fnct(false);
  js_error_message = 'Error: timeout occured when trying to load ' + url + '\n';
  send_data_is_working[indentificator] = false;
  showErrorMessage();
}

function send_data_function(httpObject, url, fnct, http_request_timeout, indentificator) {
  this.functia = fnct;
  this.http_response_text = "";
  if(send_data_is_working[indentificator]) {
    if(httpObject[indentificator].readyState == 1 || httpObject[indentificator].readyState == 2 || httpObject[indentificator].readyState == 3) {
    } else if(httpObject[indentificator].readyState == 4) {
      window.clearTimeout(http_request_timeout[indentificator]);
      try {
        if(httpObject[indentificator].status == 200 && httpObject[indentificator].statusText == "OK") {
          this.http_response_text = httpObject[indentificator].responseText;
          if(this.http_response_text.substring(0, 6) == "Error:") js_error_message = this.http_response_text + ' when trying to load ' + url + '\n';
          else if(fnct)  this.functia(this.http_response_text)  //fnct(http_response_text);
        } else {
          if(fnct) fnct(false);
          js_error_message = 'Error: ' + httpObject.statusText + ' when trying to load ' + url + '\n';
        }
      } catch(error) {
        js_error_message = 'Error: ' + error + ' when trying to load ' + url + '\n';
      }
      delete(send_data_is_working[indentificator]);
      send_data_is_working[indentificator] = false;
      showErrorMessage();
    }
  }
}

function send_data(timeout, url, param, syncron, fnct, indentificator) {
 
  if(!send_data_is_working[indentificator]) {
    httpObject[indentificator] = new Array;
    httpObject[indentificator] = getHttpObject();
//    if(httpObject[indentificator] != false) {
      var indentificator = indentificator;
      http_request_timeout[indentificator] = window.setTimeout(function () { send_data_abort(httpObject, url, fnct, indentificator); }, timeout);
      try
      {
        httpObject[indentificator].open('PUT', url, syncron);
        httpObject[indentificator].onreadystatechange = function() { send_data_function(httpObject, url, fnct, http_request_timeout, indentificator); };
        send_data_is_working[indentificator] = true;
        httpObject[indentificator].setRequestHeader("Content-Type", "multipart/form-data");
//        httpObject[indentificator].setRequestHeader("Content-Length", "50");
//        httpObject[indentificator].setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpObject[indentificator].send(param);
      }
      catch(e)
      {
        js_error_message = 'Error: ' + e + ' when trying ' + this.url + '\n Trebuie adaugate urmatoarele linii in fisierul <i>prefs.js</i>\n';
        js_error_message += '<b>user_pref("capability.principal.codebase.p0.granted", "UniversalBrowserWrite UniversalBrowserRead");\n';
        js_error_message += 'user_pref("capability.principal.codebase.p0.id", "https://abs-01.astral.ro/");\n';
        js_error_message += 'user_pref("capability.principal.codebase.p1.granted", "UniversalBrowserWrite UniversalBrowserRead");\n';
        js_error_message += 'user_pref("capability.principal.codebase.p1.id", "https://abs-01.astral.ro/");</b>\n';
        try {
          showErrorMessage();
        } catch(error) {
        }
      }
//    }
  }
}

