﻿// JScript File

var AiXml = function(){}

AiXml.prototype.GetHttpRequest = function()
{
	if ( window.XMLHttpRequest )		// Gecko
		return new XMLHttpRequest() ;
	else if ( window.ActiveXObject )	// IE
		return new ActiveXObject("MsXml2.XmlHttp") ;
}

AiXml.prototype.LoadXml = function( url, asyncFunc )
{
    var oAiXml = this ;

    var bAsync = ( typeof(asyncFunc) == 'function' ) ;
    if(window.ActiveXObject){ 	// IE
	    oAiXml.DOMDocument = new ActiveXObject("MsXml2.DOMDocument");
	}
	else{
	    oAiXml.DOMDocument = document.implementation.createDocument("","", null);
	}
    if(bAsync){
		    oAiXml.DOMDocument.onreadystatechange = function(){
		     try{		
			    if ( oAiXml.DOMDocument.readyState == 4 ){
				    asyncFunc( oAiXml ) ;
        		}
		     }
		     catch(er){
			    window.alert( er.message);
		     }
		    }
    }
    //alert(url);
    oAiXml.DOMDocument.load(url);
}

AiXml.prototype.LoadUrl = function( url, asyncFunc )
{
	var oAiXml = this ;

    var bAsync = ( typeof(asyncFunc) == 'function' ) ;
    
	var oXmlHttp = this.GetHttpRequest() ;
		
	oXmlHttp.open( "GET", url, bAsync ) ;	
	
	if ( bAsync )
	{	
		oXmlHttp.onreadystatechange = function() 
		{
			if ( oXmlHttp.readyState == 4 )
			{
			    try{
				oAiXml.DOMDocument = oXmlHttp.responseXML;
				if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ){
				    if(oAiXml.DOMDocument.parseError && oAiXml.DOMDocument.parseError != 0){
				        alert(oAiXml.DOMDocument.parseError.resone);
				    }
					asyncFunc( oAiXml ) ;
			    }
				else
					alert( 'Request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
				}
				catch(er){
				    alert( er.message);
				}
			}
		}
	}
	
	oXmlHttp.send( null ) ;
	
	if ( ! bAsync )
	{
		if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ){
			oAiXml.DOMDocument = oXmlHttp.responseXML ;
		    if(oAiXml.DOMDocument.parseError && oAiXml.DOMDocument.parseError != 0){
		        alert(oAiXml.DOMDocument.parseError.reason);
		    }
		}
		else
		{
			alert( 'Request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
		}
	}
}

AiXml.prototype.LoadUrlText = function( url, asyncFunc )
{

	var oAiXml = this ;

    var bAsync = ( typeof(asyncFunc) == 'function' ) ;
    
	var oXmlHttp = this.GetHttpRequest() ;
		
	oXmlHttp.open( "GET", url, bAsync ) ;	
	
	if ( bAsync )
	{	
		oXmlHttp.onreadystatechange = function() 
		{
			if ( oXmlHttp.readyState == 4 )
			{
			    try{
				oAiXml.DOMDocument = oXmlHttp.responseText ;
				if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ){
				    if(oAiXml.DOMDocument.parseError && oAiXml.DOMDocument.parseError != 0){
				        alert(oAiXml.DOMDocument.parseError.resone);
				    }
					asyncFunc( oAiXml ) ;
			    }
				else
					alert( 'Request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
				}
				catch(er){
				    alert('LoadUrlText :'+ er.message);
				}
			}
		}
	}
	
	oXmlHttp.send( null ) ;
	
	if ( ! bAsync )
	{
		if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ){
			oAiXml.DOMDocument = oXmlHttp.responseXML ;
		    if(oAiXml.DOMDocument.parseError && oAiXml.DOMDocument.parseError != 0){
		        alert(oAiXml.DOMDocument.parseError.reason);
		    }
		}
		else
		{
			alert( 'Request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
		}
	}
}

AiXml.prototype.SelectNodes = function( xpath )
{
	if ( document.all )		// IE
		return this.DOMDocument.selectNodes( xpath ) ;
	else					// Gecko
	{
		var aNodeArray = new Array();

		var xPathResult = this.DOMDocument.evaluate( xpath, this.DOMDocument, 
				this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
		if ( xPathResult ) 
		{
			var oNode = xPathResult.iterateNext() ;
 			while( oNode )
 			{
 				aNodeArray[aNodeArray.length] = oNode ;
 				oNode = xPathResult.iterateNext();
 			}
		} 
		return aNodeArray ;
	}
}

AiXml.prototype.SelectSingleNode = function( xpath ) 
{
	if ( document.all )		// IE
		return this.DOMDocument.selectSingleNode( xpath ) ;
	else					// Gecko
	{
		var xPathResult = this.DOMDocument.evaluate( xpath, this.DOMDocument,
				this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), 9, null);

		if ( xPathResult && xPathResult.singleNodeValue )
			return xPathResult.singleNodeValue ;
		else	
			return null ;
	}
}

AiXml.prototype.SendPost = function(xdata, url){
	var oAiXml = this ;
	var bAsync = ( typeof(asyncFunc) == 'function' ) ;
	var oXmlHttp = this.GetHttpRequest() ;
	oXmlHttp.open( "POST", url, bAsync ) ;
	
	if ( bAsync )
	{	
		oXmlHttp.onreadystatechange = function() 
		{
			if ( oXmlHttp.readyState == 4 )
			{
				oAiXml.DOMDocument = oXmlHttp.responseXML ;
				if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 )
					asyncFunc( oAiXml ) ;
				else
					alert( 'Request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
			}
		}
	}
	
	oXmlHttp.send( xdata ) ;
	if ( ! bAsync )
	{
		if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 )
			this.DOMDocument = oXmlHttp.responseXML ;
		else
		{
			alert( 'Request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
		}
	}
}

