

var req = false;


/* source: http://www.webpasties.com/xmlHttpRequest/xmlHttpRequest_tutorial_1.html */

function getXMLHttpRequestObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {
      xmlhttp = new XMLHttpRequest();

    } catch (e) {
      xmlhttp = false;

    }
  }

  return xmlhttp;

}


// ??? not sure why there's two similar functions here ???

function getXmlHttpObj() { 

	var objXMLHttp=null

	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}

	return objXMLHttp

}



function processReqChange() {

	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			response = req.responseXML.documentElement;

			method = response.getElementsByTagName('method')[0].firstChild.data;

			result = response.getElementsByTagName('result')[0].firstChild.data;

			eval(method + '(result)');

		} else {

			alert("Error: status code is " + req.status);

		}
	}
}


function loadXMLDoc(url) {
	
	req = getXMLHttpRequestObject(); // We create the HTTP Object
	
	if (req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	}

}



function loadExternalFile (intoDiv,url) {

	var reqLoadExternalFile = null;
	reqLoadExternalFile = getXmlHttpObj();
	
	reqLoadExternalFile.onreadystatechange = function(){
		if (reqLoadExternalFile.readyState == 4){
			if (reqLoadExternalFile.status == 200){
//				getElmById(intoDiv).innerHTML = req.responseText;
				
				postProcess(reqLoadExternalFile, intoDiv);
//				document.getElementById(intoDiv).innerHTML = req.responseText;
			} else {
				reqLoadExternalFile.errorString = "An error prevented this view from loading.";
				postProcess(reqLoadExternalFile);
//				getElmById(intoDiv).innerHTML = "An error prevented this view from loading.";
//				document.getElementById(intoDiv).innerHTML = "There was an error... =(";
			}
		}
	};
	reqLoadExternalFile.open("GET", url, true);
	reqLoadExternalFile.send(null);

}



// don't care about response
function executeExternalFile (url) {

	var reqExecuteExternalFile = null;
	reqExecuteExternalFile = getXmlHttpObj();

	reqExecuteExternalFile.open("GET", url, true);
	reqExecuteExternalFile.send(null);

}



function clearExternalFile (intoDiv) {

	getElmById(intoDiv).innerHTML = '';
//	document.getElementById(intoDiv).innerHTML = '';

}

function getElmById (id) {

	var el = null; 

	if (document.getElementById) {
		// isMozilla || isIE5
		el = document.getElementById(id) 
	} else if (document.layers) {
		// isNetscape4
		el = document.layers[id] 
	} else if (document.all) {
		// isIE4
		el = document.all[id]; 
	}

	return el; 

}
