/*#############################################################################

FILE:	dcc.js

	Copyright (c) 2007 Navitaire Inc. All rights reserved.

	This source code is (i) proprietary to and a trade secret of Navitaire Inc.
	and (ii) protected by copyright law and international treaties.
	Unauthorized disclosure, reproduction, distribution or alteration of this
	source code, or any portion of it, may result in severe civil and criminal
	penalties and will be prosecuted to the maximum extent possible under
	the law.

	www.navitaire.com

DESCRIPTION:

	Contains javascript for the DCC functionality.

#############################################################################*/

var IsBrowserIE = false;

// ---------------------------------------------------------------------------------------------------------------------------------------------------
// Solution variables
// ---------------------------------------------------------------------------------------------------------------------------------------------------
var activeXType;
var HttpXMLObj;

/*##############################################
#
# Function: isSupportedBrowser()
#
# Description: 
#		Determines if a browser supports the necessary technology 
#		to implement an AJAX (Asynchronous Javascript and XML) 
#		solution.
#	           
#*/
function isSupportedBrowser()
{
	// IE creates an XMLHttpRequest object differently from other browsers, thus, we need to do a check 
	// to handle the different ways of creating the XMLHttpRequest object. 
	//

	// Check to see if the XMLHTTPRequest object is supported by the non-IE browser
	if (window.XMLHttpRequest != null)
	{
		return true;
	}

	// Check if the platform is Windows and the version of IE supports AJAX.
	if (navigator.platform.toLowerCase() == 'win32' || navigator.platform.toLowerCase() == 'win64')
	{
		if (window.ActiveXObject != null)
		{
			// Get the browser's user agent. 
			var UserAgent = navigator.userAgent.toLowerCase();

			// Determine if the browser is Internet Explorer 5+, if so browser is supported
			if (parseInt(UserAgent.substring(UserAgent.indexOf('msie ') + 5 )) >= 5)
			{
				IsBrowserIE = true;
				return true;
			}
		}
	}

	// If we reach this part, it means that the browser does not support AJAX.
	return false;
}


/*##############################################
#
# Function: isPlatformWindows()
#
# Description: 
#		Determines if the current operating
#		system being used is Windows.
#	           
#*/
function isPlatformWindows()
{
	if ((navigator.platform.toLowerCase() == 'win32') || (navigator.platform.toLowerCase() == 'win64'))
	{
		return true;
	}
	
	return false;
}


/*##############################################
#
# Function: getNewHttpXmlObject()
#
# Description: 
#		Gets a new MSXML XMLHTTP object or a XML Http Request object
#	           
#*/
function getNewHttpXmlObject()
{	
	// Get object based on OS and browser type 
	if (isPlatformWindows() && IsBrowserIE)
	{		
		if (activeXType == null)
		{
			// List of all possible versions of the XMLHTTP object
			var HttpXmlProgIdCandidates = [
				'Microsoft.XMLHTTP',
				'MSXML2.XMLHTTP',
				'MSXML2.XMLHTTP.5.0',
				'MSXML2.XMLHTTP.4.0',
				'MSXML2.XMLHTTP.3.0'];

			// Iterate through each version and use the first one that works.
			for (var i = 0; i < HttpXmlProgIdCandidates.length; i++)
			{
				try
				{
					// Instantiate object
					HttpXMLObj = new ActiveXObject(HttpXmlProgIdCandidates[i]);
					activeXType = HttpXmlProgIdCandidates[i];

					// Return with success
					return true
				}
				catch (objException) {}
			}
		}
		else
		{
			// Use previously selected versions of the XMLHTTP object
			HttpXMLObj  = new ActiveXObject(activeXType);

			return true;
		}
	}
	else
	{
		// All non Windows/IE combinations
		try
		{
			// Instantiate object
			HttpXMLObj  = new XMLHttpRequest();				
			return (HttpXMLObj  != null);
		}
		catch (objException) {}
	}

	// Return false if we get here as we failed
	return false;
}


/*##############################################
#
# Function: makeDCCProxyCall()
#
# Description: 
#		Invokes an AJAX request.
#
# Parameters:
#		strPostPayload 		- The parameters we would like to submit along with the URL.
#		verb 			- Method verb ("GET" or "POST") 
#						Note: IE5 requires that the method verb POST be in uppercase to work on Https
#		proxy_url: 		- Location of where the GET or POST will be made.
#		readyStateFunction	- This is the name of the function that you wish to be called once the 
#					response has been received.
#	           
#*/
function makeDCCProxyCall(strPostPayload, verb, proxy_url, readyStateFunction) 
{
	// Get Http Xml object, if this fails exit function
	if (! getNewHttpXmlObject()) 
	{ 
		return; 
	}
	
	// Make post call to proxy (This should work in the same way for all supported OS/Browser combinations)
	// IE5 requires that the method verb POST be in uppercase to work on Https
	HttpXMLObj.onreadystatechange = readyStateFunction;
	HttpXMLObj.open(verb, proxy_url, true);
	HttpXMLObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	HttpXMLObj.send(strPostPayload);	
}