// A status indicator div that can contain information as to whether
// information is being loaded or saved.
var statusIndicator=document.createElement('div');
statusIndicator.id="statusIndicator";

function encodeHTML(html) {
   encodedHtml=escape(html);
   return encodedHtml;
}
/**
 * In Internet Explorer, to create the object using new ActiveXObject("Msxml2.XMLHTTP") 
 * or new ActiveXObject("Microsoft.XMLHTTP")
 * depending on the version of MSXML installed. 
 * In Mozilla and Safari, you use new XMLHttpRequest()
 */
function getXmlHttpRequestObject() {
   if(window.XMLHttpRequest) {
      return new XMLHttpRequest();
   } else if(window.ActiveXObject) {
      return new ActiveXObject("Microsoft.XMLHTTP")
   } else {
      alert("can't make xmlhttprequest");
   }
}
/**
 * xmlData: xml data
 *   <?xml version="1.0" encoding="UTF-8"?>
 *   <request>
 *      <command>save|load</command> 
 *      <SID>SecureID</SID> 
 *      <CID>contractIdentifier</CID>
 *      <saveName>name for this save</saveName>
 *      <section>what sort of info</section>
 *      <other>additional metadata</other>
 *      <html (or data)>
 *         Data to save (make sure it is escaped: you can use encodeHTML())
 *      </html (or data)>
 *   </request>
 * 
 * destinationURL: where to post data to.
 * 
 * statusIndicatorParent: parent of where you want the status indicator to go.
 *   It will be added using insertBefore() and you should dispose of it.
 *
 * statusText: what text you want in the status indicator.
 *
 * callbackFunc: what function needs to be called when the request is
 *   answered. The parameters are the request object followed by the status
 *   indicator object.
 *
 *   If passed in as null (false), it will initiate a synchronous httprequest
 */
function httpRequest(postData,destinationURL,statusIndicatorParent,statusText,callbackFunc) {
   if(SID!=false) {
      statusIndicator.innerHTML=statusText;
      if(statusIndicatorParent)
         statusIndicatorParent.insertBefore(statusIndicator,statusIndicatorParent.firstChild);
      var req = getXmlHttpRequestObject();
      // only required if last open param is missing or true
      if(callbackFunc!=false)
         req.onreadystatechange = function() { httpRequestCallback(req,callbackFunc);}
		if(destinationURL) {
	      req.open("POST",destinationURL,callbackFunc!=false);
   	   req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      	req.send(postData);
		}
		// If no destinationURL provided, log an error.
		// October 6, 2011 - Chris Poirier
		else {
			var date = new Date();
			var userData = '(SID="'+SID+'"';
			if(IP) userData += ', IP='+IP;
			userData += ')';
			var postDataErr = 'log_file='+escape('ajaxURL.err')+'&logged_data='+escape('['+date.toGMTString()+'] '+userData+' Destination URL empty in /common/ajax1.0.js.');
			httpRequest(postDataErr, '/log_service.php', null, '', _donothing, true);
		}
   } else {
      alert("There was a problem reading your user ID. Please go to our contact page and notify technical support.");
   }
}
function _donothing() {}
function checkSaveBlank() {}
function httpRequestCallback(req,callbackFunc) {
   if(req && req.readyState==4) {
      if(req && req.status==200) {
         callbackFunc(req,statusIndicator,true);
      } else {
   //alert("There was a problem transfering information ("+req.status+": "+req.statusText+")");
         callbackFunc(req,statusIndicator,false);
      }
   }
}
function str2XML(str) {
   var xml;
   if(window.ActiveXObject) {
      xml = new ActiveXObject("MSXML2.DOMDocument");
      xml.loadXML(str);
      xml.setProperty("SelectionLanguage","XPath");
   } else {
      xml = (new DOMParser).parseFromString(str, "text/xml");
   }
   return xml;
}

