﻿/**
	gaAjax对象.
**/
gajson = {version: '1.0'};

//第二种ajax主要用于带选项卡的
gajson.gaAjax = function(url, param, method, reqType, obj,tabName)
{
	this.obj = gajson.tool.$(obj);
	this.url = url;
	this.method = method;
	this.tabName = tabName;
	this.parameter = param;//eval('('+param+')');
	var _paramStr = null;
	this.ajaxType = this.parameter.type.toLowerCase();//是
	var gajson_divnum = obj.substring(obj.lastIndexOf("_")+1);
	var gajson_divBase = obj.substring(0,obj.lastIndexOf("_")+1);
	if(this.ajaxType.indexOf("json") != -1){//是以json请求
		_paramStr = this.parameter.parameter;//将参数拼成一个串
		_paramStr += "&datalen="+gajson_divnum;
		temphrefurl = this.parameter.hrefURL;
		temphrefurl = gajson.tool.base64encode(gajson.tool.strUnicode2Ansi(temphrefurl));
		_paramStr += "&hrefURL="+temphrefurl;
//		tempfilter = this.parameter.filter;
//		tempfilter = gajson.tool.base64encode(gajson.tool.strUnicode2Ansi(tempfilter));
//		_paramStr += "&filter="+tempfilter;
		_paramStr += "&jsontype="+this.ajaxType;
	}else{//带样式的请求
		_paramStr = this.parameter.parameter;//将参数拼成一个串
		temphrefurl = this.parameter.hrefURL;
		temphrefurl = gajson.tool.base64encode(gajson.tool.strUnicode2Ansi(temphrefurl));
		_paramStr += "&hrefURL="+temphrefurl;
		tempfilter = this.parameter.filter;
		tempfilter = gajson.tool.base64encode(gajson.tool.strUnicode2Ansi(tempfilter));
		_paramStr += "&filter="+tempfilter
	}
	this.req = null;
	if(!isNaN(gajson_divnum)){
		var tempNum = Math.floor(parseInt(gajson_divnum)/2);
		for(var k = 0 ;k < tempNum; k ++){
			gajson.tool.$(gajson_divBase+k).innerHTML = "&nbsp;";
		}
		for(var k = tempNum+1 ;k < parseInt(gajson_divnum); k ++){
			gajson.tool.$(gajson_divBase+k).innerHTML = "&nbsp;";
		}
		gajson.tool.$(gajson_divBase+tempNum).innerHTML = " 数据正在加载，请稍侯......";
	}else{
		this.obj.innerHTML = " 数据正在加载，请稍侯......";
	}
	
	
	if (window.XMLHttpRequest)
	{
		this.req=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		this.req=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (this.req)
	{
		try
		{
			var loader=this;
			this.req.onreadystatechange=function()
			{
				gajson.gaAjax.onReadyState.call(loader);
			}
			var _method = reqType.trim();
			_method = _method.toLowerCase();
			if(_method == "post"){
//			    this.url = url + "?" + _paramStr;
//			    window.open(this.url);
				this.req.open("POST",url,true);
				this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.req.setRequestHeader("charset","GBK");
				this.req.send(_paramStr);
			}else{
				this.url = url + "?" + _paramStr;
//				window.open(this.url);
				this.req.open("GET",this.url,true);
				this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.req.setRequestHeader("charset","GBK");
				this.req.send(null);
			}
			///
			//this.req.open("post",this.url,true);
			//this.req.send(this.url);
		}
		catch (err)
		{
			alert(err);
		}
	}
}

gajson.gaAjax.onReadyState = function()
{
	if (this.req.readyState==4)
	{	
		var httpStatus=this.req.status;
		if (httpStatus==200 || httpStatus==0)
		{
			if (this.method)
			{
				this.method(this.req.responseText, this.obj,this.tabName);
			}
		}
	}
}

