//-----------------------------------------------------------------------
// Copyright (C) wwwRoot.cn, All rights reserved.
//-----------------------------------------------------------------------
// Root.Xml.js
// Ӭ²½ȫȳXml

Xml = {};
Xml.__$Scripts = document.getElementsByTagName('script');
Xml.__Path = Xml.__$Scripts[Xml.__$Scripts.length - 1].src;
Xml.__Path = Xml.__Path.substring(0,Xml.__Path.lastIndexOf('/') + 1)
Xml.Request = function(url, data, command, args)
{	
	if(/^http:\/\//i.test(url))
	{
		var domain = url.replace(/^http:\/\//,'');
		var index = domain.indexOf('/');
		if(index > -1) domain = domain.substring(0,index);
		var raw = window.location;
		raw = raw.toString().replace(/^http:\/\//,'');
		index = raw.indexOf('/');
		if(index > -1) raw = raw.substring(0,index);
		if(domain != raw)
		{
			url = Xml.__Path + 'Root.Xml.aspx?url=' + escape(url);			
		}
	}
	url += (url.indexOf('?') > 0)?'&':'?';
	url += Math.random();
	
	var xmlRequest;
	if(window.ActiveXObject)
	{
		var MSXML = new Array('MSXML2.XMLHTTP','Microsoft.XMLHTTP','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.5.0');
		for(var i=0;i<MSXML.length;i++)
		{
			try
			{		
				xmlRequest = new ActiveXObject(MSXML[i]);
				break;
			}
			catch(e)
			{
				xmlRequest = null;
			}	
		}
	}
	else if(window.XMLHttpRequest)
	{
		xmlRequest = new XMLHttpRequest();
		if(xmlRequest.overrideMimeType)
		{
			xmlRequest.overrideMimeType('text/xml');
		}
	}	
	if(xmlRequest != null)
	{
		xmlRequest.onreadystatechange = 		
			function()
			{
				if(xmlRequest.readyState == 4)
				{	
					if(xmlRequest.status == 0 || (xmlRequest.status >= 200 && xmlRequest.status < 300))
					{
						if(command != null)
						{				
							if(typeof(command) == 'function')
							{
								if(args != null && args instanceof Array)
								{
									command.apply(xmlRequest, args);
								}
								else
								{
									command.call(xmlRequest, args);
								}
							}							
							else if(typeof(command) == 'string')
							{
								if(document.commands[command])
								{								
									document.commands.executeQueue(command,args,xmlRequest);								
								}
								else
								{
									command = command.replace('[Xml]','xmlRequest.responseXML');
									command = command.replace('[Text]','xmlRequest.responseText');					
									eval(command);
								}
							}
							else if(typeof(command) == 'object')
							{
								if(command[args])
								{
									command[args](xmlRequest);
								}
							}											
						}
					}
					else
					{
						window.alert('ErrorCode:' + xmlRequest.status + '\r\n' + 'ErrorMessage:' + xmlRequest.statusText);
					}
				}
			};
		if(data == null)
		{			
			xmlRequest.open('GET',url,true);
			xmlRequest.send(null);
		}
		else
		{	
			xmlRequest.open('POST',url,true);
			xmlRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8"); //
			xmlRequest.send(data);
		}
	}
	else
	{
		window.alert('GOD SAY: YOUR BROWSER IS TOO OLD!');
	}
}
