/*************************************************

**TO USE AJAX:
<script language="JavaScript" type="text/javascript">
  function callbackFunction(text, xml, error) {
    var root_node = xml.getElementsByTagName("root").item(0);
    alert(root_node.firstChild.data);
  }

  function beforeRequest() {
    alert('This function is called before the request is made.');
  }

  function afterRequest(success) {
    alert('This function is called after the request has been made.');
  }
</script>

<a href="javascript://" onClick="makeRequest('/ajax_test.php', callbackFunction, beforeRequest, afterRequest, 'Loading...')">Click Me</a>

**TO USE LOADING IMAGE:
<script language="JavaScript" type="text/javascript">writeLoadingImg();</script>
*************************************************/

var hasLoadingImg = false;
var loadingDivId = "loadingAjax";
var loadingMsgDivId = "loadingMsgAjax";

function getRequestObject() {
  var http_request = false;

  if (window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType)
      http_request.overrideMimeType('text/xml');
  } else if (window.ActiveXObject) {
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
      }
    }
  }

  return http_request;
}

function makeRequestMaster(requestType, url, data, callback, preCall, postCall, loadingMsg) {
  if (preCall)
    preCall();

  showLoading(loadingMsg);

  var http_request = getRequestObject();

  if (!http_request) {
    callback(null, null, 'Cannot create an XMLHTTP object instance.');
    hideLoading();
    if (postCall)
      postCall(false);
    return false;
  }

  http_request.onreadystatechange = function() {
    handleStateChange(http_request, callback, preCall, postCall, loadingMsg);
  }

  if (requestType.toUpperCase() == 'POST') {
    http_request.open('POST', url, true);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(data);
  } else {
    http_request.open('GET', url, true);
    http_request.send(null);
  }
}

function makeRequest(url, callback, preCall, postCall, loadingMsg) {
  makeRequestMaster('GET', url, null, callback, preCall, postCall, loadingMsg);
}

function makePostRequest(url, data, callback, preCall, postCall, loadingMsg) {
  makeRequestMaster('POST', url, data, callback, preCall, postCall, loadingMsg);
}

function sendForm(form_obj, callback, preCall, postCall, loadingMsg) {
  if (typeof form_obj == 'string')
    form_obj = document.getElementById(form_obj);

  var action = form_obj.getAttribute('action');
  var fields = form_obj.elements;
  var data = '';

  for (var i=0; i<fields.length; i++) {
    var field = fields[i];

    if (field.getAttribute('name') != null) {
      data += '&'+field.getAttribute('name')+'='+field.value;
    }
  }

  makePostRequest(action, data, callback, preCall, postCall, loadingMsg);
}

function handleStateChange(http_request, callback, preCall, postCall, loadingMsg) {
  if (http_request.readyState == 4) {
    var success;
    if (http_request.status == 200) {
      success = true;
      callback(http_request.responseText, http_request.responseXML, null);
    } else {
      success = false;
      callback(null, null, 'Status came back unknown: '+http_request.status);
    }
    hideLoading();
    if (postCall)
      postCall(success);
  } else {
    //still loading probably
  }
}

function writeLoadingImg() {
  hasLoadingImg = true;
  document.write('<div id="'+loadingDivId+'" style="display:none; padding:5px"><img src="/images/ajax_loading_big.gif" width="32" height="32" border="0" alt="Loading..." style="background-color:#fff; border:0" /><div id="'+loadingMsgDivId+'"></div></div>');
}

function displayDiv(obj_id, display) {
  var obj = document.getElementById(obj_id);
  obj.style.display = display;
}

function hideDiv(obj_id) {
  displayDiv(obj_id, "none");
}

function showDiv(obj_id) {
  displayDiv(obj_id, "");
}

function hideLoading() {
  if (!hasLoadingImg) return;
  hideDiv(loadingDivId);
}

function showLoading(loadingMsg) {
  if (!hasLoadingImg) return;
  if (loadingMsg == null) return;

  document.getElementById(loadingMsgDivId).innerHTML = loadingMsg;

  showDiv(loadingDivId);
}
