// HTTP requests / responses (AJAX)
// Author: Radu Buzea [www.hypnosis.ro]
// Version 2.04 [12 Feb. 2007]
// ================================================================================================

var show_error_alerts = false;

function ajaxInit() {
  var xhttp = null;
  if (window.XMLHttpRequest) {
    xhttp = new XMLHttpRequest();
    if (xhttp.overrideMimeType) xhttp.overrideMimeType("text/xml"); 
  } else if (window.ActiveXObject) { 
    try { xhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
    catch(e) {
      try { xhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch(e) {}
    }
  }
  return xhttp;
}

function ajaxDefaultResponse(response_xml, response_text) {
  // write response action
}

function ajaxRequestStatus(status_code, url) {
  if (status_code == 200 || !show_error_alerts) return;
  switch (status_code) {
    case 400: alert("HTTP 400 - Bad request: \n" + url); break;
    case 401: alert("HTTP 401 - Unauthorized. The request requires user authentication: \n" + url); break;
    case 403: alert("HTTP 403 - Forbidden: \n" + url); break;
    case 404: alert("HTTP 404 - File not found: \n" + url); break;
    case 405: alert("HTTP 405 - Request method not allowed: \n" + url); break;
    case 406: alert("HTTP 406 - Request not acceptable: \n" + url); break;
    case 407: alert("HTTP 407 - Proxy authentication required: \n" + url); break;
    case 408: alert("HTTP 408 - Request timeout: \n" + url); break;
    case 413: alert("HTTP 413 - Request entity too large: \n" + url); break;
    case 414: alert("HTTP 414 - Request-URI too long: \n" + url); break;
    case 415: alert("HTTP 415 - Unsupported media type: \n" + url); break;
    case 500: alert("HTTP 500 - Internal server error: \n" + url); break;
    case 501: alert("HTTP 501 - Not implemented: \n" + url); break;
    case 502: alert("HTTP 502 - Bad Gateway: \n" + url); break;
    case 503: alert("HTTP 503 - Service unavailable: \n" + url); break;
    case 504: alert("HTTP 504 - Gateway timeout: \n" + url); break;
    case 505: alert("HTTP 505 - HTTP version not supported: \n" + url); break;
    default:  alert("HTTP " + status_code + " \n" + url);
  }
}

function ajaxGetRequest(url, xml, callback_function) {
  var xhttp = ajaxInit();
  if (!xhttp) {
    if (show_error_alerts) alert("ERROR: Your browser does not suport AJAX features. ");
    return false;
  }
  // BEGIN OF: Callback function
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4) {
      if (xhttp.status == 200) {
        if (callback_function != "") {
          if (xml) eval(callback_function + "(xhttp.responseXML)");
          else eval(callback_function + "(xhttp.responseText)");
        } else ajaxDefaultResponse(xhttp.responseXML, xhttp.responseText);
      } else ajaxRequestStatus(xhttp.status, url);
    }
  } 
  // END OF: Callback function
  xhttp.open("GET", url, true); 
  xhttp.send(null);
  return true;
}

function ajaxPostRequest(url, content_type, submit_data, xml, callback_function) {
  var xhttp = ajaxInit();
  if (!xhttp) {
    if (show_error_alerts) alert("ERROR: Your browser does not suport AJAX features. ");
    return false;
  }
  if (!content_type || content_type == "") content_type = "application/x-www-form-urlencoded";
  xhttp.open("POST", url, true);
  xhttp.setRequestHeader("Content-Type", content_type);
  // BEGIN OF: Callback function
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4) {
      if (callback_function != "") {
        if (xml) eval(callback_function + "(xhttp.responseXML)");
        else eval(callback_function + "(xhttp.responseText)");
      } else ajaxDefaultResponse(xhttp.responseXML, xhttp.responseText);
    }
  }
  // END OF: Callback function
  xhttp.send(submit_data);
  return true;
}
