
/********************
* General Functions *
*********************/

function AJAXUpdateRequest(updateobjectid, url) {
   AJAXRequest(url, {updatediv:updateobjectid}, AJAXUpdateRequestCallback);
}

function AJAXUpdateRequestCallback(funcoptions, responsevalue) {
  var updatediv = document.getElementById(funcoptions.updatediv);
  updatediv.innerHTML = responsevalue;
  //$("#" + funcoptions.updatediv).replaceWith(responsevalue);
}

function AJAXRequest(url, funcoptions) {
  var page_request = false;
  var debug = false;

  // Set the vars
  if (funcoptions != null) {
    if (typeof(funcoptions.debug) != "undefined") {
      debug = funcoptions.debug;
    }
    if ((typeof(funcoptions.indicatordiv) != "undefined") && (typeof(funcoptions.containerdiv) != "undefined")) {
      AJAXDivUpdateStart(funcoptions.indicatordiv, funcoptions.containerdiv);
    }
  }
  
  if (window.XMLHttpRequest) { // if Mozilla, Safari etc
    page_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // if IE
    try {
      page_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        page_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  } else {
    return false;
  }
  page_request.onreadystatechange = function() { AJAXRequestFinish(page_request, funcoptions); }
  anticache = (url.indexOf("?") != -1) ? "&" + (new Date().getTime()) : "?" + (new Date().getTime());
  if (debug) {
    prompt("AJAXRequest", url);
  }
  page_request.open("GET", url + anticache, true);
  page_request.send(null);
}

function AJAXRequestPost(url, postdata, funcoptions) {
  var page_request = false;
  var debug = false;

  // Set the vars
  if (funcoptions != null) {
    if (typeof(funcoptions.debug) != "undefined") {
      debug = funcoptions.debug;
    }
    if ((typeof(funcoptions.indicatordiv) != "undefined") && (typeof(funcoptions.containerdiv) != "undefined")) {
      AJAXDivUpdateStart(funcoptions.indicatordiv, funcoptions.containerdiv);
    }
  }
  
  if (window.XMLHttpRequest) { // if Mozilla, Safari etc
    page_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // if IE
    try {
      page_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        page_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  } else {
    return false;
  } 
  page_request.onreadystatechange = function() { AJAXRequestFinish(page_request, funcoptions); }
  anticache = (url.indexOf("?") != -1) ? "&" + (new Date().getTime()) : "?" + (new Date().getTime());
  if (debug) {
    prompt("AJAXRequest", url);
  }
  try {
    page_request.open("POST", url + anticache, true);
    page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    page_request.setRequestHeader("Content-length", postdata.length);
    page_request.setRequestHeader("Connection", "close");
    page_request.send(postdata);
  } catch (e) {
    alert("err:" + e);
  }
}

function AJAXRequestFinish(page_request, funcoptions) {
  if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1)) {
    if (funcoptions != null) {
      if (typeof(funcoptions.updatediv) != "undefined") {
        var updatediv = document.getElementById(funcoptions.updatediv);
        updatediv.innerHTML = page_request.responseText;
      }
      if (typeof(funcoptions.callbackfunc) != "undefined") {
        funcoptions.callbackfunc(funcoptions, page_request.responseText);
      }
      if ((typeof(funcoptions.indicatordiv) != "undefined")) {
        AJAXDivUpdateEnd(funcoptions.indicatordiv);
      }
    }
  }
}

function AJAXDivUpdateStart(indicatorid, containerid) {
  var progressDiv = document.getElementById(indicatorid);
  var divView = document.getElementById(containerid);
  progressDiv.style.width = divView.offsetWidth + "px";
  progressDiv.style.height = divView.offsetHeight + "px";
  $("#" + indicatorid).fadeTo("fast", 0.5);
  //$("#" + indicatorid).show();
}

function AJAXDivUpdateEnd(indicatorid) {
  $("#" + indicatorid).hide();
}

