/*
Filename: js\ajax.js
Programmer: Carlos Vazquez (carlos@mojointeractive.com)
Date: 
Purpose: A Collection of javscript functions that are usefull when using AJAX. Includes XML functions.
Document Location: 
*/

/* Performs a check for browser type */
var xmlhttp=false;
var AJAXReturnText = '';
var AJAXReturnXML = '';
var isIE = false;

 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  isIE = true;
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   isIE = true;
  } catch (E) {
   xmlhttp = false;
  }
 }

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
// End XML Init

// Check if DOMParser exists and define it if it doesn't. May not exist in IE
if (typeof DOMParser == "undefined") {
   DOMParser = function () {}

   DOMParser.prototype.parseFromString = function (str, contentType) {
	 if (typeof ActiveXObject != "undefined") {
	    var d = new ActiveXObject("MSXML.DomDocument");
	    d.loadXML(str);
	    return d;
	 } else if (typeof XMLHttpRequest != "undefined") {
	    var req = new XMLHttpRequest;
	    req.open("GET", "data:" + (contentType || "application/xml") +
					";charset=utf-8," + encodeURIComponent(str), false);
	    if (req.overrideMimeType) {
		  req.overrideMimeType(contentType);
	    }
	    req.send(null);
	    return req.responseXML;
	 }
   }
}

// This function accepts a string, converts it into a valid XML object and returns it
function createXMLFromString(string)
{
  var xmlParser, xmlDocument;
  xmlParser = new DOMParser();
  xmlDocument = xmlParser.parseFromString(string, 'text/xml');
  return xmlDocument;
}

// This function accepts a METHOD(POST or GET), URL of the script returning data, and a Boolean value for async
// The global. variable xmlReturnText is populated that contains the output of the URL called
function GetXMLrequest(oMETHOD, oURL, oBOOLEAN)
{
	xmlhttp.open(oMETHOD, oURL, oBOOLEAN);
	if (isIE)
	{
		xmlhttp.onreadystatechange =
		function()
		{
			if (xmlhttp.readyState==4)
			{
				if (xmlhttp.status==200)
				{
					AJAXReturnText = xmlhttp.responseText;
					AJAXReturnXML = xmlhttp.responseXML;
				}
				else if (xmlhttp.status==404)
				{
					alert("URL doesn't exist!");
				}
				else
				{
					alert("Status is "+xmlhttp.status);
				}
			}
		}
		xmlhttp.send();
	}
	else
	{
		//alert("In Firefox");
		xmlhttp.send(null);
		//alert("send");
		alert("xmlhttp.status = " +xmlhttp.status);
		if (xmlhttp.status == 200)
		{
			AJAXReturnText = xmlhttp.responseText;
		}
		else
		{
			AJAXReturnText = 'Error: ' + xmlhttp.status + ' ' + xmlhttp.statusText;
		}
		alert("AJAXReturnText = " + AJAXReturnText);
    }
}

/* Example of parsing xml
GetXMLrequest(pRequest, pURL, false);
if (AJAXReturnText.length > 0)
{
	var xmlDocument = createXMLFromString(AJAXReturnText);

	procedureElements = xmlDocument.getElementsByTagName('procedure');
	var procedureList = '';
	for (var i = 0; i < procedureElements.length; i++)
	{
		procedureList = procedureList + '\n' + xmlDocument.getElementsByTagName('procedure')[i].firstChild.data;
	}
	alert("procedureList:" + procedureList);
}
*/

