// r1g.org utility scripts

// Check this var to ensure that scripts have loaded.
var r1g_doc_id = r1g_id;
var r1g_id = "";

// Add an event handler preserving existing handlers for the same event
function addEvent(target, event, handler) 
{
  var oldHandler = target[event];
	
  if (typeof oldHandler != "function") 
	{
    target[event] = handler;
  } 
	else 
	{
    target[event] = function(e) 
		{
      oldHandler(e);
      handler(e);
    }
  }
}

// Get a httpDocument over HTTP
function getHTTP(url, callback) 
{
	httpDocument = false;
	
	// send r1g document ID if present
	if(r1g_doc_id != undefined) url += "&doc=" + r1g_docid;
    
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) 
	{
		try 
		{
			httpDocument = new XMLHttpRequest();
		} 
		catch(e) 
		{
			httpDocument = false;
		}
	} 
	
	// branch for IE/Windows ActiveX version
	else if(window.ActiveXObject) 
	{
		try 
		{
			httpDocument = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
				httpDocument = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e) 
			{
				httpDocument = false;
			}
		}
	}
	
	if(httpDocument) 
	{
			httpDocument.onreadystatechange = callback;
			httpDocument.open("GET", url, true);
			httpDocument.send(""); 
	}
	
	return httpDocument;
}