document.domain="tiexue.net";
var xmlHttp = false;
try
{
	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
	try
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e2)
	{
		xmlHttp = false;
	}
}

if(!xmlHttp && typeof XMLHttpRequest != 'undefined')
{
	xmlHttp = new XMLHttpRequest();
}

//////////////// Method ////////////////
var callServerCallback = false;
var maskDiv = null;
var maskDivInfo = null;
var messageDivInfo = null;
var messageBox = null;
function CallServer(corefile,callback,nameParams,valueParams)
{
	//alert("Enter");
	if(maskDiv == null)
	{
		InitMaskDiv();
	}
	maskDiv.Show();
	// Build the URL to connect to
	var query = "";
	for(var i=0;i<nameParams.length;i++)
	{
		if(nameParams[i] != "" && nameParams[i] != "undefined" && valueParams[i] != "" && valueParams[i] != "undefined")
		{
			query += nameParams[i] + "=" + escape(valueParams[i])+"&";
		}
	}
	var url = "/XmlHttp/"+corefile;
	//alert(url+"|||"+query);

	// Open a connection to the server
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = ServerCallback;
	callServerCallback = callback;
	
	// Send the request
	xmlHttp.send(query);
	//alert("enter send");
}

function ServerCallback()
{
	//alert(xmlHttp.readyState);
	//messageBox.Show(xmlHttp.readyState);
	//return;
	if(xmlHttp.readyState == 4)
	{
		maskDiv.Hidden();
		if(xmlHttp.status == 200)
		{
			//alert("Server is done!");
			var response = xmlHttp.responseText;
			//alert(response);
			var responseRows = response.split("\r\n");
			if(responseRows.length==3)
			{
				if(responseRows[0]=="0")
				{
					messageBox.Show(responseRows[2]);
				}
				callServerCallback(responseRows[0],responseRows[1],responseRows[2]);
			}
			else
			{
				messageBox.Show("服务器响应错误,请重试或联系客服 http://help.tiexue.net/");
			}
		}
		else if(xmlHttp.status == 404)
		{
			messageBox.Show("你请求的页面不存在");
			callServerCallback(0,"","你请求的页面不存在");
		}
		else
		{
			var response = xmlHttp.responseText;
			alert(response);
			messageBox.Show("错误: 状态码是 " + xmlHttp.status);
			callServerCallback(0,"","错误: 状态码是 " + xmlHttp.status);
		}
	}
}

function InitMaskDiv()
{
	
	maskDiv = document.getElementById("formbackground");
	maskDivInfo  = document.getElementById("maskDivInfo");
	messageDivInfo = document.getElementById("messageDivInfo");
	messageBox = document.getElementById("messageBox");
	maskDiv.Show = function()
	{
		var height;
		if (document.body.scrollHeight < document.body.clientHeight)
		{
			height = document.body.clientHeight;
		}
		else
		{
			height = document.body.scrollHeight;
		}
		this.style.cssText = this.style.cssText + ";HEIGHT:" + height + "px;" + ";width:" + document.body.scrollWidth + "px;";
		//alert(this.style.cssText);
		this.style.display = "block";
		//alert(window.screenTop);
		maskDivInfo.style.top = document.body.clientHeight/2.6 + document.body.scrollTop;
		maskDivInfo.style.left = document.body.clientWidth/2.6;
		maskDivInfo.style.display = "block";
	};
	
	maskDiv.Hidden = function()
	{
		this.style.display = "none";
		maskDivInfo.style.display = "none";
	};
	
	messageBox.Show = function(message)
	{
		maskDiv.style.display = "block";
		this.innerHTML = message;
		messageDivInfo.style.top = document.body.clientHeight/2.6 + document.body.scrollTop;
		messageDivInfo.style.left = document.body.clientWidth/2.6;
		messageDivInfo.style.display = "block";
	};
	
	messageBox.Hidden = function()
	{
		messageDivInfo.style.display = "none";
		maskDiv.style.display = "none";
	}
}

function StopBubble()
{
	event.cancelBubble = true;
	event.returnValue = false;
	return false;
}

//////////// Test Method ///////////
function testCallback(response)
{
	alert("OK:"+response);
}

function InitXmlHttpRequest()
{
	var testNameParams = new Array;
	var testValueParams = new Array;
	testNameParams[0] = "city";
	testValueParams[0] = "222beijing";
	testNameParams[1] = "state";
	testValueParams[1] = "Error OK";
	CallServer("test.aspx",testCallback,testNameParams,testValueParams);
}

