function initXMLHttpRequest(){
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Oops..Ajax is not working as expected. Please contact your system administrator.');
        return false;
    }else{
      return http_request;
    }
}

function makeGETRequest(url,vid,funcCallback,asyncSetting,showLoadingLayerInDiv){
	var vid = (vid==null) ? "" : vid;
    var http_request = null;
    var funcCallback = (funcCallback==null || funcCallback=="") ? "" : funcCallback;
    var async = (asyncSetting==null || asyncSetting=="") ?  true : asyncSetting;
    var showLoadingLayerInDiv = (showLoadingLayerInDiv==null) ? false : showLoadingLayerInDiv;
    http_request = initXMLHttpRequest();
    http_request.onreadystatechange = function(){
                                        updateContents(http_request,vid,true,funcCallback, showLoadingLayerInDiv);
                                      };
    http_request.open('GET', url, async);
    http_request.send(null);
}

function makeGETRequestNoError(url,vid,funcCallback,asyncSetting,showLoadingLayerInDiv){
	var vid = (vid==null) ? "" : vid;
    var http_request = null;
    var funcCallback = (funcCallback==null || funcCallback=="") ? "" : funcCallback;
    var async = (asyncSetting==null || asyncSetting=="") ?  true : asyncSetting;
    var showLoadingLayerInDiv = (showLoadingLayerInDiv==null) ? false : showLoadingLayerInDiv;
    http_request = initXMLHttpRequest();
    http_request.onreadystatechange = function(){
                                        updateContentsNoError(http_request,vid,true,funcCallback, showLoadingLayerInDiv);
                                      };
    http_request.open('GET', url, async);
    http_request.send(null);
}

function makeGETRequestWithoutLoadingLayer(url,vid,funcCallback,asyncSetting,showLoadingLayerInDiv){
	var vid = (vid==null) ? "" : vid;
    var http_request = null;
    var funcCallback = (funcCallback == null) ? "" : funcCallback;
    var async = (asyncSetting==null || asyncSetting=="") ?  true : asyncSetting;
    var showLoadingLayerInDiv = (showLoadingLayerInDiv==null) ? false : showLoadingLayerInDiv;
    http_request = initXMLHttpRequest();
    http_request.onreadystatechange = function(){
                                        updateContents(http_request,vid,false,funcCallback, showLoadingLayerInDiv);
                                      };
    http_request.open('GET', url, async);
    http_request.send(null);
}

function makePOSTRequest(url, parameters,vid,funcCallback) {
    var http_request = null;
    var funcCallback = (funcCallback == null) ? "" : funcCallback;
    http_request = initXMLHttpRequest();
    http_request.onreadystatechange = function(){updateContents(http_request,vid,true,funcCallback);};
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}

function updateContents(http_request,vid,vShowLoadingLayer,funcCallback, showLoadingLayerInDiv) {
	vid = (vid==null) ? "" : vid;
    var funcCallback = (funcCallback == null) ? "" : funcCallback;
    var showLoadingLayerInDiv = (showLoadingLayerInDiv==null) ? false : true;
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            //if(vid != ""){
            result = http_request.responseText;
			if (vid!=''){
	            if (document.getElementById(vid)){
	              document.getElementById(vid).innerHTML = result;
	            }
			}
            hideLoadingLayer();

            if (funcCallback!=''){
              eval(funcCallback+'(result)')
            }
            //}
        } else {
            //alert('There was a problem with the request.');
            hideLoadingLayer();
            showItSimple('divAjaxError');
        }
    }else{
      if (vShowLoadingLayer){
        if (showLoadingLayerInDiv){
		  if (vid!=''){		  
			if (document.getElementById(vid)){
				document.getElementById(vid).innerHTML = document.getElementById("divLoading").innerHTML;
			}
		  }
        }else{
		  if (vid!=''){
            displayLoadingLayer();
		  }
        }
      }
    }
}

function updateContentsNoError(http_request,vid,vShowLoadingLayer,funcCallback, showLoadingLayerInDiv) {
	vid = (vid==null) ? "" : vid;
    var funcCallback = (funcCallback == null) ? "" : funcCallback;
    var showLoadingLayerInDiv = (showLoadingLayerInDiv==null) ? false : true;
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            //if(vid != ""){
            result = http_request.responseText;
			if (vid!=''){
	            if (document.getElementById(vid)){
	              document.getElementById(vid).innerHTML = result;
	            }
			}
            hideLoadingLayer();

            if (funcCallback!=''){
              eval(funcCallback+'(result)')
            }
            //}
        } else {
            //alert('There was a problem with the request.');
            hideLoadingLayer();
            // showItSimple('divAjaxError');
        }
    }else{
      if (vShowLoadingLayer){
        if (showLoadingLayerInDiv){
		  if (vid!=''){		  
			if (document.getElementById(vid)){
				document.getElementById(vid).innerHTML = document.getElementById("divLoading").innerHTML;
			}
		  }
        }else{
		  if (vid!=''){
            displayLoadingLayer();
		  }
        }
      }
    }
}


/****************************************************************************/
function submitForm() {
  var content = convertFormDataToPostContent(window.document.theform);
  doPost('/office/UsageAnalyzer', content, 'processResult');
}

function processResult(result) {
  document.getElementById('content_area').innerHTML = result;
}


function doPost(url, content, callback_name) {
  var async_request = false; // Mozilla/Safari
  if (window.XMLHttpRequest) {
    async_request = new XMLHttpRequest();
    async_request.overrideMimeType('text/xml');
  } // IE
  else if (window.ActiveXObject) {
    async_request = new ActiveXObject("Microsoft.XMLHTTP");
  }

  async_request.open('POST', url, true);
  async_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  async_request.onreadystatechange = function() {
    if (async_request.readyState == 4) {
      response_content = async_request.responseText; eval(callback_name + '(response_content);');
    }
  }
  async_request.send(content);
}

function convertFormDataToPostContent(form_name) {
  var content_to_submit = '';
  var form_element;
  var last_element_name = '';
  for (i = 0; i < form_name.elements.length; i++) {
    form_element = form_name.elements[i];
    switch (form_element.type) {
      // Text fields, hidden form elements
      case 'text':
      case 'hidden':
      case 'password':
      case 'textarea':
      case 'select-one':
        content_to_submit += form_element.name + '='  + escape(form_element.value) + '&'
        break;

      // Radio buttons
      case 'radio':
        if (form_element.checked) {
          content_to_submit += form_element.name + '='  + escape(form_element.value) + '&'
        }
        break;

      // Checkboxes
      case 'checkbox': if (form_element.checked) {
            // Continuing multiple, same-name checkboxes
            if (form_element.name == last_element_name) {
              // Strip of end ampersand if there is one
              if (content_to_submit.lastIndexOf('&') ==  content_to_submit.length - 1) {
                content_to_submit = content_to_submit.substr( 0, content_to_submit.length - 1);
              } // Append value as comma-delimited string
              content_to_submit += ',' + escape(form_element.value);
            }else{
              content_to_submit += form_element.name + '='  + escape(form_element.value);
            }
            content_to_submit += '&'; last_element_name = form_element.name;
        }
        break;
      }
    }
    // Remove trailing separator
    content_to_submit = content_to_submit.substr(0, content_to_submit.length - 1);
    return content_to_submit;
}

			

