function getXMLHTTP() 
{
	var _ms_XMLHttpRequest_ActiveX = null; 
	var AJAX;
	if( window.XMLHttpRequest )
	{
		// Not IE
		AJAX = new XMLHttpRequest();
	}
	else if( window.ActiveXObject )
	{
		// Hello IE!
		// Instantiate the latest MS ActiveX Objects
		if( _ms_XMLHttpRequest_ActiveX )
		{
			AJAX = new ActiveXObject( _ms_XMLHttpRequest_ActiveX );
		}
		else
		{
			// loops through the various versions of XMLHTTP to ensure we're using the latest
			var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
			for( var i = 0; i < versions.length ; i++ )
			{
				try
				{
					// try to create the object
					// if it doesn't work, we'll try again
					// if it does work, we'll save a reference to the proper one to speed up future instantiations
					AJAX = new ActiveXObject( versions[i] );
					//alert(versions[i]);
					if( self.AJAX )
					{
						_ms_XMLHttpRequest_ActiveX = versions[i];
						break;
					}
				}
				catch( objException )
				{
					// trap; try next one
				}
			}
		}
	}
	return(AJAX);
}

function getDOMDoc()
{
	var progIDs = [ 'Msxml2.DOMDocument.6.0', 'Msxml2.DOMDocument.3.0', 'Microsoft.XMLDOM'];       
	for( var i = 0; i < progIDs.length; i++ )
	{
		try
		{
			var xmlDOM = new ActiveXObject( progIDs[i] );
			return xmlDOM;
		}
		catch( ex )
		{
			;
		}
	}        
	return null;      
}		

function getXMLDocFromText( strText )
{	
	var xmlDoc;
	// code for IE
	if( window.ActiveXObject )
	{
		xmlDoc = getDOMDoc(); 
		xmlDoc.async=false;
		xmlDoc.loadXML(strText);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if( document.implementation && document.implementation.createDocument )
	{
		var parser=new DOMParser();
		var xmlDoc=parser.parseFromString(strText,"text/xml");
	}
	return(xmlDoc);		
}

 //////////////// Pooling
function AJAXRq(){}; 
var _AJAXRq = AJAXRq.prototype;
_AJAXRq.engaged = false;
_AJAXRq.xmlResponse = ""; 
_AJAXRq.callback = null;
_AJAXRq.xmlhttp = null;
_AJAXRq.timerid = null;
_AJAXRq.args = null;
_AJAXRq.bText = false;
_AJAXRq.queue = null;

function AJAXPool( nPoolSize )
{
	// Setup a pool of ajax objects
	this.arrRqPool = Array();
	this.nPoolSize = nPoolSize;
	this.timeout = 5000;
	this.bText = false;
	this.bAlert = false;
	this.bRedir = false;
	this.queue = new Array();
	for( var i = 0; i < this.nPoolSize; i++ )
	{		
		this.arrRqPool[i] = new AJAXRq();
		this.arrRqPool[i].xmlhttp = getXMLHTTP();
	}
}

var _AJAXPool = AJAXPool.prototype;
_AJAXPool.Request = function( strRqUrl, request )
{	
	for( var i = 0; i < this.nPoolSize; i++ )
	{
		if( this.arrRqPool[ i ].engaged != true )
		{				
			this.arrRqPool[ i ].engaged = true;				
			this.arrRqPool[ i ].bText = request.bText ? true : false;
			this.arrRqPool[ i ].bAlert = request.bAlert ? true : false;
			
			if( request.bRedir ) document.location.href = strRqUrl;
			
			this.arrRqPool[ i ].callback = request.callback;
			this.arrRqPool[ i ].args = request.args;
			var t = this;
			this.arrRqPool[ i ].xmlhttp.onreadystatechange = function(){t.doStateChange(i)};
			var rnd = Math.random();
			this.arrRqPool[ i ].xmlhttp.open( request.type ? request.type : "GET", strRqUrl + ( strRqUrl.indexOf( "?" ) != -1 ? "&" : "?" )  + "rnd=" + rnd  );
			this.arrRqPool[ i ].xmlhttp.setRequestHeader( 'Content-Type','application/x-www-form-urlencoded' );

			// init timer for timeout
			this.arrRqPool[ i ].timerid = setTimeout
			(
				function()
				{
					if( t.arrRqPool[ i ].engaged )
					{
						t.arrRqPool[ i ].engaged = false;
						t.arrRqPool[ i ].callback( getXMLDocFromText( "<ajaxresponse><success timeout='true'>False</success></ajaxresponse>" ), t.arrRqPool[ i ].args );
					}
				}
				, request.timeout ? request.timeout : this.timeout 
			);
			
		//				alert( "req= ... " + request.postdata );
			this.arrRqPool[ i ].xmlhttp.send( request.postdata ? request.postdata : null );
			
			// remove from queue
			if( request.bQueued ) this.deQueue( request.nQueuePosition );
			
			return( i );
		} 
	}

	// no objects available immediately so queue
	if( !request.bQueued ) this.queue.push( { strRqUrl: strRqUrl, request: request } );
	
	return( null );
}

_AJAXPool.deQueue = function( i )
{
	this.queue[ i ] = null;
}

_AJAXPool.doQueue = function()
{
	for( var i = 0; i < this.queue.length; i++ )
	{
		var request = this.queue[ i ];
		if( request )
		{
			request.request.bQueued = true;
			request.request.nQueuePosition = i;
			this.Request( request.strRqUrl, request.request );
		}
	}
}

_AJAXPool.free = function( i )
{
	// Abort frees up the object	  
	this.arrRqPool[ i ].xmlhttp.abort();
	this.arrRqPool[ i ].engaged = false;		
	// Process the queue
	this.doQueue();
}

_AJAXPool.doStateChange = function( i )
{	
	if( this.arrRqPool[ i ].xmlhttp && this.arrRqPool[i].xmlhttp.readyState == 4 )
	{					
		// cancel timeout timer
		clearTimeout( this.arrRqPool[ i ].timerid );
			
		// get response
		var xmlText = this.arrRqPool[i].xmlhttp.responseText;				
		if( this.arrRqPool[ i ].bAlert ) alert( xmlText );		
		
		// check for cookie header and if set apply to browser to pass on login cookie
		var cookie = this.arrRqPool[i].xmlhttp.getResponseHeader( "Set-Cookie" );		
		/*
		if( cookie ) 
		{ 					
			var myCookie = cookie + ( cookie.toString().indexOf( "domain=" ) == -1 ? ";domain=" + g_strCookieDomain : "" );						
			document.cookie = myCookie;			
		}	
		*/				
		// return xml doc as first argument of callback
		// Check if global or request-based raw xml request		
		if( this.bText || this.arrRqPool[i].bText )
		{
			this.arrRqPool[ i ].callback( xmlText, this.arrRqPool[ i ].args );		
		}
		else
		{
			this.arrRqPool[ i ].callback( getXMLDocFromText( xmlText ), this.arrRqPool[ i ].args );		
		}			
		
		this.free( i );
	}
}

var g_AJAXPool = new AJAXPool( 10 );
var g_XML = new Object();


function urlEncode( strUrl )
{
	strUrl = escape( strUrl );
	strUrl = strUrl.replace( /\+/g, "%2B" );	
	return( strUrl );	
}

/*
function doFormFields( form, strType, strFieldName, bPlainText )
{	
	var formNodes = form.getElementsByTagName( strType || "input");
	for( var iNode = 0; iNode < formNodes.length; iNode++ )
	{		
		var formNode = formNodes[ iNode ];		
		if( formNode && formNode.name.toLowerCase() == strFieldName.toLowerCase() && !formNode.disabled )
		{					
			var strValue;
		  
			switch( formNode.type ? formNode.type.toLowerCase() : formNode.tagName.toLowerCase() )
			{
				case "text":
						strValue = formNode.value;
					break;
				case "textarea":					
						strValue = formNode.value;						
					break;
				case "hidden":
						strValue = formNode.value;
					break;
				case "radio", "checkbox":
					if( formNode.checked )
					{						
						strValue = formNode.value;
					}
					else
					{
						return "";
					}
					break;
			}					
			if( bPlainText ) 
				return ( strValue );
			else 
				return urlEncode( strValue.replace(/ /g, "+" ) );		
		}
	}
}

function getFormFieldValueOnly( form, strFieldName, bPlainText )
{
	form = document.getElementById( form );
  
	var ret = doFormFields( form, "input", strFieldName, bPlainText );
	if( ret ) return ret;
	
	ret = doFormFields( form, "textarea", strFieldName, bPlainText );
	if( ret ) return ret;
	
	var formNodes = form.getElementsByTagName("select");
	for( var iNode = 0; iNode < formNodes.length; iNode++ )
	{
		var formNode = formNodes[ iNode ];		
		if( formNode && !formNode.disabled && formNode.options && formNode.options.length )
		{
			return urlEncode( formNode.options[ formNode.selectedIndex ].value.replace(/ /g, "+" ) );
		}
	}
	return( "" );
}
*/

function getFormPostData( form, includeDisabled )
{
	// Iterate through fields in form, building up post data pairs
	form = document.getElementById( form );

	var strPostData = "";
	var formNodes = form.getElementsByTagName("input");
	for( var iNode = 0; iNode < formNodes.length; iNode++ )
	{
		var formNode = formNodes[ iNode ];
		if( formNode )
		{
			if( !includeDisabled && formNode.disabled )
				continue;

			switch( formNode.type.toLowerCase() )
			{
				case "password":
				case "text":
					strPostData += ( iNode == 0 ? "" : "&" ) + formNode.name + "=" + urlEncode( formNode.value );
					break;
				case "hidden":
					strPostData += ( iNode == 0 ? "" : "&" ) + formNode.name + "=" + urlEncode( formNode.value );
					break;
				case "checked":
				case "radio":
					if( formNode.checked ) strPostData += ( iNode == 0 ? "" : "&" ) + urlEncode( formNode.name ) + "=" + urlEncode( formNode.value );
					break;
			}															
		}
	}

	var inputUBound = iNode;
	formNodes = form.getElementsByTagName("select");
	for( iNode = 0; iNode < formNodes.length; iNode++ )
	{
		var formNode = formNodes[ iNode ];
		if( formNode && !formNode.disabled && formNode.options && formNode.options.length )
		{
			strPostData += ( ( inputUBound == 0 && iNode == 0 ) ? "" : "&" ) + formNode.name + "=" + urlEncode( formNode.options[ formNode.selectedIndex ].value );
		}
	}
	var selectUBound = iNode;
	formNodes = form.getElementsByTagName("textarea");
	for( iNode = 0; iNode < formNodes.length; iNode++ )
	{		
		var formNode = formNodes[ iNode ];
		strPostData += ( inputUBound == 0 && selectUBound == 0 && iNode == 0 ? "" : "&" ) + formNode.name + "=" + urlEncode( formNode.value );
	}
	return( strPostData );
}
