﻿
// 代替 xmlhttp.js

function AJAX()
{
	var base = this;
	this.bAsync = true;
	this.ver = "1.0.0";

	this.createXHR = function()
	{
		var xmlhttp;
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			if( xmlhttp ) return xmlhttp;
		}
		catch(e)
		{
		}
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			if( xmlhttp ) return xmlhttp;
		}
		catch(e)
		{
		}
		try
		{
			xmlhttp = new XMLHttpRequest();
			if( xmlhttp ) return xmlhttp;
		}
		catch(e)
		{
		}
		try
		{
			xmlhttp = window.createRequest();
			if( xmlhttp ) return xmlhttp;
		}
		catch(e)
		{
		}
		alert("Can't create XMLHttp object. Please download MSXML from www.microsoft.com.");
		return null;	
	}
	
	this.loading = function(){}

	this.abort = function ()
	{
		if(base.xmlhttp) xmlhttp.abort();
	}

	this.success = function(xmlhttp)
	{
		alert( "debug:\n\n" + base.xmlhttp.responseText );
	}
	
	this.failure = function()
	{
		var xmlhttp = base.xmlhttp;
		
		if ( xmlhttp.status == 404 )
		{
			alert( "File not found." );
		}
		else
		{
			alert( "xmlhttp.status = " +  xmlhttp.status );
		}
	}
		
	this.get = function( sUrl )
	{
		// XmlHttpRequest Object default Code UTF-8
		var xmlhttp = this.createXHR();
		base.xmlhttp = xmlhttp;
		xmlhttp.open( "GET", sUrl, base.bAsync );
		xmlhttp.setRequestHeader( "Content-Type", "text/xml" );
		//xmlhttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
		xmlhttp.onreadystatechange = function()
		{
			if( xmlhttp.readyState == 1 )	// The object has been created, but the send method has not been called.
			{
				base.loading();
				return;
			}
			if( xmlhttp.readyState != 4 ) return;
			if( xmlhttp.status == 200 )
			{
				base.success(xmlhttp);
			}
			else
			{
				base.failure();
			}
		}
		xmlhttp.send( null );	// must null for FireFox
	}
	
	this.post = function( sUrl, sPostData )
	{
		var xmlhttp = this.createXHR();
		base.xmlhttp = xmlhttp;
		xmlhttp.open( "POST", sUrl, base.bAsync );
		xmlhttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
		//xmlhttp.setRequestHeader( "Content-Length", sPostData.length );
		xmlhttp.onreadystatechange = function()
		{
			if( xmlhttp.readyState == 1 )	// The object has been created, but the send method has not been called.
			{
				base.loading();
				return;
			}
			if( xmlhttp.readyState != 4 ) return;
			if( xmlhttp.status == 200 )
			{
				base.success(xmlhttp);
			}
			else
			{
				base.failure();
			}
		}
		xmlhttp.send( sPostData );
	}
}