/*
2006.11.27
增加ie和firefox创建xmlHttpRequest对象的兼容性
增加同步处理

2006.10.30 by mikeyao
此ajax类目前只支持IE。
功能：
最简单的http get和post方法的使用，返回文本

使用范例：
var ajax = new Ajax();
ajax.method = "POST";
ajax.url = "test.php";
ajax.body = "test=22222222222"; // 使用post方法
ajax.callback = function() {
	alert(ajax.returnText);	
}
ajax.callServer();
*/
function Ajax() {
	this.xmlHttp = null;
	this.method = "POST";            // get还是post方法
	this.callback = function(){};    // 回调函数，默认为空  
	this.asyn = true;                // 异步还是同步，默认为异步
	this.body = null;                // 参数列表，post方法时有参数，get方法时为空
	this.returnText = "";            // 返回文本responseText
	this.url = "";
	this.wait = function(){};
	
	try	{
		this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e1) {
		try	{
			this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e2) {}
	}
	if (!this.xmlHttp) 
		this.xmlHttp = new XMLHttpRequest();
}
Ajax.prototype.callServer = function() {
	if (this.asyn) {
		var that = this;
		this.xmlHttp.onreadystatechange = function() {
		    if (that.xmlHttp.readyState < 4)
			that.wait();
			if (that.xmlHttp.readyState == 4) {
				//if (that.xmlHttp.state == 200) {
					that.returnText = that.xmlHttp.responseText;
					that.callback();
				//}
			}
		}
		this.xmlHttp.open(this.method, this.url, this.asyn);
		// post方法必须的设置
		if (this.method == "POST") { 						
			this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.xmlHttp.setRequestHeader("Content-Length", this.body.length);
		}
		this.xmlHttp.send(this.body);
	} else {
		this.xmlHttp.open(this.method, this.url, this.asyn);
		if (this.method == "POST") { 						
			this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.xmlHttp.setRequestHeader("Content-Length", this.body.length);
		}
		this.xmlHttp.send(this.body);
		this.returnText = this.xmlHttp.responseText;
	}
}

function $id(id) {
	return document.getElementById(id);	
}
function $tag(obj, tag) {
	return obj.getElementsByTagName(tag);	
}
function $name(name) {
	return document.getElementsByName(name);	
}

function goTop() {
	window.scroll(0, 0);	
}

// cookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toUTCString();
	} else 
		var expires = "";
	
	document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}
function eraseCookie(name) {
    createCookie(name, "", -1);
}


