var NptAjaxLib = new function __NptAjaxLib()
{
	this.NxRequest = new Array();
	this.nRequestCount = 0;

	this.GetAjaxObject = function ( nIndex ) 
	{
		if( window.ActiveXObject )
		{
			try 
			{
				this.NxRequest[ nIndex ] = new ActiveXObject( 'Msxml12.XMLHTTP' );
			} 
			catch( e ) 
			{
				try 
				{
					this.NxRequest[ nIndex ]= new ActiveXObject( 'Microsoft.XMLHTTP' );
				} 
				catch (e2) 
				{
					this.NxRequest[ nIndex ] = new XMLHttpRequest();	// IE 7.0
				}
			}
		} 
		else 
		{
			this.NxRequest[ nIndex ] = new XMLHttpRequest();
		}
	}
	
	this.AddRequest = function( objAjaxRequest )
	{
		var nIndex = this.nRequestCount;
		this.nRequestCount++;
		this.GetAjaxObject( nIndex );

		if( this.NxRequest[ nIndex ] )
		{
			this.NxRequest[ nIndex ].open( 'Post', objAjaxRequest.strURL + '?' + objAjaxRequest.GetQueryString(), true );
			this.NxRequest[ nIndex ].setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=ks_c_5601-1987' );
			this.NxRequest[ nIndex ].onreadystatechange = function() { objAjaxRequest.HandleReponse( nIndex ); } ;
			this.NxRequest[ nIndex ].send( objAjaxRequest.GetPostData() );
		}
	}
	
	
	this.AddAmlRequest = function( objAjaxRequest )
	{
		var nIndex = this.nRequestCount;
		this.nRequestCount++;
		this.GetAjaxObject( nIndex );

		if( this.NxRequest[ nIndex ] )
		{
			this.NxRequest[ nIndex ].open( 'Post', objAjaxRequest.strURL + '?' + objAjaxRequest.GetQueryString(), true );
			this.NxRequest[ nIndex ].setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=ks_c_5601-1987' );
			this.NxRequest[ nIndex ].onreadystatechange = function() { objAjaxRequest.HandleAmlReponse( nIndex ); } ;
			this.NxRequest[ nIndex ].send( objAjaxRequest.GetPostData() );
		}
	}
	
	this.RemoveRequest = function( nIndex )
	{
		this.NxRequest[ nIndex ] = null;
	}
}

function __AjaxRequest( url )
{
	this.strURL = url;
	this.arrQueryString = new Array();
	this.arrPostData = new Array();
	this.Handler = null;
	
	this.HandleReponse = function( nIndex )
	{
		var xmlRequest =NptAjaxLib.NxRequest[ nIndex ];
		if( xmlRequest && xmlRequest.readyState == 4 && xmlRequest.status == 200 ) 
		{
			NptAjaxLib.RemoveRequest( nIndex );

			var responseText = xmlRequest.responseText;
			this.Handler( responseText );
		}
	}
	
	this.HandleAmlReponse = function( nIndex )
	{
		var xmlRequest =NptAjaxLib.NxRequest[ nIndex ];

		if( xmlRequest && xmlRequest.readyState == 4 && xmlRequest.status == 200 ) 
		{
			NptAjaxLib.RemoveRequest( nIndex );
                        try 
			{
			   var responseXML = xmlRequest.responseText;
			   var resultObject = NxamlParser.ParseXmlText( responseXML );
                        } 
			catch (e)
			{
				alert( '[XMLDoc Parse Error] ' + e.description );
			}
			this.Handler( responseXML, resultObject  );
		}
	}
	
	this.GetQueryString = function()
	{
		var strQueryString = '';
		if( this.strURL.indexOf( '?' ) != -1 )
			strQueryString = this.strURL.substr( this.strURL.indexOf( '?' ) );
			
		for( var i = 0 ; i < this.arrQueryString.length ; i++ )
		{
			if( strQueryString != '' )
				strQueryString += '&';
			
			strQueryString += this.arrQueryString[ i ].key + '=' +  this.arrQueryString[ i ].value;
		}
		
		return strQueryString;	
	}
	
	this.GetPostData = function()
	{
		var strPostData = '';

		for( var i = 0 ; i < this.arrPostData.length ; i++ )
		{
			if( strPostData != '' )
				strPostData += '&';
			
			strPostData += this.arrPostData[ i ].key + '=' +  this.arrPostData[ i ].value;
		}
		
		return strPostData;	
	}
	
	this.AddQueryString = function( key, value )
	{
		this.arrQueryString[ this.arrQueryString.length ] = new __QueryString( key, value );
	}
	
	this.AddPostData = function( key, value )
	{
		this.arrPostData[ this.arrPostData.length ] = new __QueryString( key, value );
	}
	
	this.AddHandler = function( handler )
	{
		this.Handler = handler;
	}
	
	this.Execute = function()
	{
		NptAjaxLib.AddRequest( this );
	}
	
	this.ExecuteAml = function()
	{
		NptAjaxLib.AddAmlRequest( this );
	}
}

function __QueryString( key, value )
{
	this.key = key;
	this.value = value;
}
