/*

	Filename:  httpreq.js

	File Description:

		WBT Manager XMLHttpRequest functions.
		Functions allow HTML pages to send data to the server in the
		background without having to use an HTML POST or GET into a hidden window

	References:	ADL SCORM specification (www.adlnet.org)
				AICC CMI specification (www.aicc.org)

	Copyright:
	
		Copyright (C) 2007-2010. Integrity eLearning, all rights reserved

	Licensing:

		This software is a component of WBT Manager and its use is 
		covered by the WBT Manager license agreement.

	History:
	
		Date Changed   Author      Reason for Changes
		------------   ----------  --------------------------------------------
		20070718		MS			Added documentation


	Naming conventions:
	
		Global variable and constant names will be prefixed with "xmlhttp_".
		Function names will be prefixed with "xmlhttp_".
		
	Public functions:
	
		xmlhttp_CreateObject()
		xmlhttp_Send()
		xmlhttp_ReturnXML()
		xmlhttp_ReturnStatus()
		xmlhttp_ReturnStatusText()
		xmlhttp_ReturnXMLResponseText()
		
*/	

//debugging control
var xmlhttp_showerror = false; //set to true to see hidden error messages.
var xmlhttp_debug = false; //set to true to see debug messages

//status variables
//calling pages should use designated functions to access these values.
var xmlhttp_status = 0; //http status NOTE: 0 will be returned for non-http errors
var xmlhttp_statustext = "";
var xmlhttp_xmlresponsetext = ""; //will only be set if an error occurs when trying to return an xml dom object

// Function: xmlhttp_ReturnStatus()
// Purpose: Return the status code for the most recent request
// Return value: HTTP status code for most recent request (numeric value)
// Parameters: none
//
function xmlhttp_ReturnStatus()
{
	return(xmlhttp_status);
}

// Function: xmlhttp_ReturnStatusText()
// Purpose: Return the status message for the most recent request
// Return value: HTTP status message for most recent request (numeric value)
// Parameters: none
//
function xmlhttp_ReturnStatusText()
{
	return(xmlhttp_statustext);
}

// Function: xmlhttp_ReturnStatusText()
// Purpose: Return the document text for the most recent failed XML request.
// Return value: Raw text of document returned from an XML request which failed
//				Not valid if request succeeded.
// Parameters: none
//
function xmlhttp_ReturnXMLResponseText()
{
	return(xmlhttp_xmlresponsetext);
}

// Function: xmlhttp_CreateObject()
// Purpose: To create the appropriate XMLHttpRequest object for the current browser
// Return value: An instance of the Browser's XMLHttpRequest object.
//					Returns null if unable to create object.
// Parameters: none
//
// Status codes:
//		If object creation fails xmlhttp_status will be set to 0
//			and xmlhttp_statustext will be set to the error message.
//		If object creation succeeds then status variables are not affected.
//
function xmlhttp_CreateObject()
{
	var httpobj = null;
	
	try
	{
		// branch for native XMLHttpRequest object (mozilla browsers/IE7+)
		if( window.XMLHttpRequest )
		    httpobj =  new XMLHttpRequest();
 
		// branch for IE/Windows ActiveX version
		else if( window.ActiveXObject )
		    httpobj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		xmlhttp_status = 0;
		xmlhttp_statustext = "error creating XMLHttpRequest object: " + e;
		if( xmlhttp_showerror ) 
			alert( xmlhttp_statustext );
	}
	
	if( xmlhttp_debug )
		if( httpobj != null )
			alert( "XMLHttpRequest object created" );
		else
			alert( "error creating XMLHttpRequest object.");
			
	return( httpobj );
	
}

// Function: xmlhttp_Send()
// Purpose: Send a GET or POST request and return the resulting document as text.
// Return value: The requested document as as character string.
// Parameters:
//		strURL:
//			Character string
//			The URL for the requested document.
//
//		bPOST:
//			Boolean value
//			true if the request is to use the POST method
//			false if the request is to use the GET method
//
//		strData:
//			Character string
//			Data to be sent as part of the request. 
//			Must be formatted as name-value pairs separated by "&".
//			Data must be url-encoded.
//
// Status codes:
//		0	= error creating request object or sending request.
//				xmlhttp_statustext should contain error message
//
//		200 = Successful request. 
//
//		HTTP error code = Request was sent but rejected by server or
//						otherwise not able to be completed. 
//						xmlhttp_statustext will contain HTTP error text
//						Function will return the full error message page.
//
function xmlhttp_Send( strURL, bPOST, strData )
{

	var xmlhttp
	var strTmp;
	var response = "";
	
	xmlhttp_status = 0;
	
	//was a url passed
	strTmp = new String(strURL);
	if( strTmp.length == 0 )
	{
		xmlhttp_status = 0;
		xmlhttp_statustext = "no url supplied";
		return( "" );
	}
	
	xmlhttp = xmlhttp_CreateObject();
	if( xmlhttp != null )
	{
	
		try
		{
			if( bPOST )
			{
				xmlhttp.open( "POST", strURL, false );
				xmlhttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
			}
			else
				xmlhttp.open( "GET", strURL, false );

			xmlhttp.send( strData ); 
			
			xmlhttp_status = xmlhttp.status;
			xmlhttp_statustext = xmlhttp.statusText;
			response = xmlhttp.responseText;

		}
		catch(e)
		{
			xmlhttp_status = 0
			xmlhttp_statustext = "error in xmlhttp_Send (sending request): " + e;
		}
	
	}
		
	xmlhttp = null;
	
	return( response );
	
}

// Function: xmlhttp_ReturnXML()
// Purpose: Send a GET or POST request and return an XMLDomDocument object (for xml responses).
// Return value: An XMLDomDocument object (for xml responses).
//				A null value will be returned if the request fails.
// Parameters:
//		strURL:
//			Character string
//			The URL for the requested document.
//
//		bPOST:
//			Boolean value
//			true if the request is to use the POST method
//			false if the request is to use the GET method
//
//		strData:
//			Character string
//			Data to be sent as part of the request. 
//			Must be formatted as name-value pairs separated by "&".
//			Data must be url-encoded.
//
// Status codes:
//		0	= error creating request object or sending request.
//				xmlhttp_statustext should contain error message
//
//		200 = Successful request. 
//
//		HTTP error code = Request was sent but rejected by server or
//						otherwise not able to be completed. 
//						xmlhttp_statustext will contain HTTP error text
//						xmlhttp_xmlresponsetext will contain the full error message page.
//
function xmlhttp_ReturnXML( strURL, bPOST, strData )
{

	var xmlhttp, strTmp;
	var xmldoc = null;
		
	xmlhttp_status = 0;
	xmlhttp_xmlresponsetext = "";
	
	//was a url passed
	strTmp = new String(strURL);
	if( strTmp.length == 0 )
	{
		xmlhttp_status = 0;
		xmlhttp_statustext = "no url supplied";
		return( null );
	}
	
	xmlhttp = xmlhttp_CreateObject();
	if( xmlhttp != null )
	{
	
		try
		{
			if( bPOST )
			{
				xmlhttp.open( "POST", strURL, false );
				xmlhttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
			}
			else
				xmlhttp.open( "GET", strURL, false );

			xmlhttp.send( strData ); 
			
			xmlhttp_status = xmlhttp.status;
			xmlhttp_statustext = xmlhttp.statusText;
			if( xmlhttp_status == 200 )
				xmldoc = xmlhttp.responseXML;
			else
				xmlhttp_xmlresponsetext = xmlhttp.responseText;

		}
		catch(e)
		{
			xmlhttp_status = 0
			xmlhttp_statustext = "error in xmlhttp_Send (sending request): " + e;
		}
	
	}
		
	return( xmldoc );
	
}