//****************************************
//this javascript file is written for index.php
//everyone who using this file ,sholud check this js first
//
/**
**
 * ajax技术使用探讨
 *
 * @author      BreezeXu
 * @version     1.0
 * @lastupdate  2008-11-17
 *
 */
 /*
/*********************************************************************/
//var XmlHttp;
//var Obj;
/*
构造函数
*/
function Ajax(sUrl,sRecvType,sQueryString,oResultFunc,sError,sState,sDownloadEnd,sType) 
{
	//Add by breezeXu 2008-12-12
	//传递个类别过来,对应类别操作
	this.sType = sType; 
	
    //访问服务器地址
    this.sUrl = sUrl;    
    //this.object = null;
    //this.sMethod = null;
    //返回数据类型,0为xml，1为text
    this.sRecvType = sRecvType;
    // 错误字符串
    this.ErrorStr = null;
    // 错误事件驱动,当发生错误时触发
    this.OnError = null;
    // 状态事件驱动,当状态改变时触发
    //this.EventState = EventState;
    
    this.sRecvType = sRecvType;
    
    this.QueryString = sQueryString;
    
    this.oResultFunc = oResultFunc;
	//页面上显示错误的html标记id,标记不填或留空则采用默认标记，下同
	this.sError = sError;
	//页面上显示状态的html标记id
	this.sState = sState;
	//页面上显示完成回调的html标记id
	this.sDownloadEnd = sDownloadEnd;
    //建立xmlhttp对象
    this.XmlHttp = this.createXMLHttpRequest();    
    
    XmlHttp = this.XmlHttp;

    
    if (this.XmlHttp == null) {
        alert('XMLHTTPREQUEST对象创建不成功!');
    } else if (typeof this.XmlHttp == "object") {
        //alert('XMLHTTPREQUEST对象创建成功!');
    }
    
    Obj = this;
    //定义xmlhttp对象的状态句柄
    this.XmlHttp.onreadystatechange = this.handler;
    
};

/*
创建XMLHttp对象
*/
Ajax.prototype.createXMLHttpRequest = function() {
    if (window.XMLHttpRequest){     
        return  new XMLHttpRequest();
    } else {      
        var MSXML = ["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; 
        for(var n = 0; n < MSXML.length; n ++) {
            try {
              return new ActiveXObject(MSXML[n]); return; 
            } catch(e) {
                this.ErrorStr = "XMLHttpRequest对象创建不成功!.";
            } 
        }
    }
    this.ErrorStr = "你的浏览器不支持XMLHttpRequest对象.";    
    if (typeof this.EventError == "function") {
         this.EventError(this.ErrorStr); 
    }    
    return null;
};

/*
发送服务器请求
*/
Ajax.prototype.send = function() {
    sUrl = this.sUrl;
    sMethod = this.sMethod;
    var sUrl = sUrl + "?timeStamp=" + new Date().getTime();//采取当前时间取不同url，防止页面缓存
	
    var queryString = this.QueryString;
    if (sMethod.toLocaleUpperCase() == 'POST') {
        //POST方式
        this.XmlHttp.open("POST",sUrl,true);
        this.XmlHttp.setRequestHeader("content-length",queryString.length);
        //this.XmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded","charset=UTF-8");
        this.XmlHttp.send(queryString);
    } else if (sMethod.toLocaleUpperCase() == 'GET') {
	    //GET方式
        queryString = sUrl + "&" + this.QueryString;
        this.XmlHttp.open("GET",queryString,true);
    this.XmlHttp.send(null);        
    }
};

/*
sRecvType 接受数据类型:0为xml 1为text
responseBody  将回应信息正文以unsigned byte数组形式返回的未经解码的二进制数据.只读 
responseText 将响应信息作为字符串返回.只读
*/

Ajax.prototype.handler = function () {
	
    if(Obj.EventState) 
	{
      Obj.EventState(XmlHttp.readyState);
	  
    }
  if (XmlHttp.readyState == 4) 
  {
      if (XmlHttp.status == 200) 
	  {          
          if(Obj.oResultFunc)
		  {
              this.RetData = Obj.UTFTOGB(XmlHttp.responseBody);
              //alert(this.RetData);//返回ajax请求的内容
			  //此处可以添加详细的处理函数，使用ajax返回结果，做相应操作
              //Obj.oResultFunc(this.RetData); 
          } 

/*
sRecvType 接受数据类型:1为text 0为xml 
*/
    
          Result = (Obj.sRecvType) ? XmlHttp.responseText :    Result = XmlHttp.responseXML;
          Obj.EventDownloadEnd(Result);          
          Obj.status = XmlHttp.status;
	 }
	 else 
	 {
		
          Obj.ErrorStr = "您所请求的页面有异常。";
          if (typeof Obj.EventError == "function") {
              Obj.EventError(Obj.ErrorStr); 
      }
    }
  }
};


// UTF 转入 GB (by:Rimifon) 
Ajax.prototype.UTFTOGB = function(strBody) { 
    var Rec=new ActiveXObject("ADODB.RecordSet"); 
    Rec.Fields.Append("DDD",201,1); 
    Rec.Open(); 
    Rec.AddNew(); 
    Rec(0).AppendChunk(strBody); 
    Rec.Update(); 
    var HTML=Rec(0).Value; 
    Rec.Close(); 
    delete Rec; 
    return(HTML); 
};


// 错误回调事件函数 
Ajax.prototype.EventError = function(strValue){ 
    if(this.sError != null && this.sError != "" && document.getElementById(this.sError) != "undefined"){
    document.getElementById(this.sError).innerHTML = strValue; 
	}
};

// 状态回调事件函数 
Ajax.prototype.EventState = function(strValue) { 
    var strState = new Array(); 
    strState[0] = "<img src='images/loading.gif'>未初始化..."; 
    strState[1] = "<img src='images/loading.gif'>开始读取数据..."; 
    strState[2] = "<img src='images/loading.gif'>读取数据...";
    strState[3] = "<img src='images/loading.gif'>读取数据中..."; 
    strState[4] = "";//"读取完成...";
    statusText = (strValue < 4 || strValue != 0) ? strState[strValue] : strState[0];
	if(this.sState != null && this.sState != "" && document.getElementById(this.sState) != "undefined"){
    document.getElementById(this.sState).innerHTML = " <font color=red>" + statusText + "</font> "; 
	}
};

// 完成回调事件函数 
Ajax.prototype.EventDownloadEnd = function (strValue) 
{
	//alert(strValue);
    if(this.sDownloadEnd != null && this.sDownloadEnd != "" && document.getElementById(this.sDownloadEnd) != "undefined")
	{
		document.getElementById(this.sDownloadEnd).innerHTML = strValue; 
		
	}
	else //在这里处理结果
	{
		//alert(this.sType);
		switch(this.sType)
		{
			//---------------------------------------------------------省市连动-------------------------------------------
			case "setcity": 
				var labels=strValue.split("|");
				document.getElementById("stu_city").options.length=0;
				if(document.all)
				{
					
					document.getElementById("stu_city").add(document.createElement("OPTION"));
					document.getElementById("stu_city").options[0].text="所有城市";
					document.getElementById("stu_city").options[0].value="";
					for(var ii=0;ii<labels.length;ii++)
					{
						var nn = labels[ii].split("*");
						var jj = ii+1;
						document.getElementById("stu_city").add(document.createElement("OPTION"));
						document.getElementById("stu_city").options[jj].text=nn[1];
						document.getElementById("stu_city").options[jj].value=nn[0];
					}
				}
				else
				{
								var option = document.createElement("option");
								option.text = "所有城市";
								option.value = "";
								document.getElementById("stu_city").appendChild(option);
								for(var ii=0;ii<labels.length;ii++)
								{
									var nn = labels[ii].split("*");
									var jj = ii+1;
									var option = document.createElement("option");
									option.text = nn[1];
									option.value = nn[0];
									document.getElementById("stu_city").appendChild(option);
									
								}
								
				}
				document.getElementById("stu_city").selectedIndex = 0; 
			 break;
			 //----------------------------------------------------后台大类选择小类----------------------------------
			case "changetype": 
					
					var labels=strValue.split("|");
					document.myform1.smallclass.options.length=0;
						if(labels.length >0)
						{
							if(document.all)
							{
								document.myform1.smallclass.add(document.createElement("option"));
								document.myform1.smallclass.options[0].text="请选择类别";
								document.myform1.smallclass.options[0].value="";
								for(var ii=0;ii<labels.length;ii++)
								{
									var iii = ii+1;
									var uu = labels[ii].split("*");
									document.myform1.smallclass.add(document.createElement("OPTION"));
									document.myform1.smallclass.options[iii].text=uu[1];
									document.myform1.smallclass.options[iii].value=uu[0];
								}
							}
							else
							{
								var option = document.createElement("option");
								option.text = "请选择类别";
								option.value = "";
								document.myform1.smallclass.appendChild(option);
								for(var ii=0;ii<labels.length;ii++)
								{
									var iii = ii+1;
									var uu = labels[ii].split("*");
									var option = document.createElement("option");
									option.text = uu[1];
									option.value = uu[0];
									document.myform1.smallclass.appendChild(option);
								}
							}
							
						}
					document.myform1.smallclass.selectedIndex = 0;
			break;

			 //----------------------------------------------------后台大类选择小类----------------------------------
			case "changetype1": 
					
					var labels=strValue.split("|");
					document.myform1.smallclass1.options.length=0;
						if(labels.length >0)
						{
							if(document.all)
							{
								document.myform1.smallclass1.add(document.createElement("option"));
								document.myform1.smallclass1.options[0].text="请选择类别";
								document.myform1.smallclass1.options[0].value="";
								for(var ii=0;ii<labels.length;ii++)
								{
									var iii = ii+1;
									var uu = labels[ii].split("*");
									document.myform1.smallclass1.add(document.createElement("OPTION"));
									document.myform1.smallclass1.options[iii].text=uu[1];
									document.myform1.smallclass1.options[iii].value=uu[0];
								}
							}
							else
							{
								var option = document.createElement("option");
								option.text = "请选择类别";
								option.value = "";
								document.myform1.smallclass1.appendChild(option);
								for(var ii=0;ii<labels.length;ii++)
								{
									var iii = ii+1;
									var uu = labels[ii].split("*");
									var option = document.createElement("option");
									option.text = uu[1];
									option.value = uu[0];
									document.myform1.smallclass1.appendChild(option);
								}
							}
							
						}
					document.myform1.smallclass1.selectedIndex = 0;
			break;

			//----------------------------------------------------删除对应表的对应单个记录--------------------------------
			case "delinfo": 
					var info = strValue;
					info = info.split("^");
					var p = "tr"+trim(info[0]);
					if(trim(info[1]) == "ok")
					{
						document.getElementById(p).style.display='none';
						
					}
					else if(trim(info[1]) == "sorry")
					{
						alert("对不起,您不能删除它");
					}
					
		   break;
		   //----------------------------------------------------企业删除单个记录（并没有真删除）--------------------------------
			case "delinfo_comp": 
					var info = strValue;
					info = info.split("^");
					var p = "tr"+trim(info[0]);
					if(trim(info[1]) == "ok")
					{
						document.getElementById(p).style.display='none';
						
					}
					else if(trim(info[1]) == "sorry")
					{
						alert("对不起,您不能删除它");
					}
					
		   break;

		  //----------------------------------------------------更改审核与未审核-----------------------------------------------------
		   case "changestate":
				var info = strValue;
					if(trim(info)=="breezeqx")
					{
						alert("对不起，您所在的用户组没有权限");return false;
					}
					info = info.split("^");
					var p = "img"+trim(info[0]);
					
					if(trim(info[1]) == "yes")
					{
						document.getElementById(p).src="images/yes.gif";
						document.getElementById(p).alt = "已审核";
					}
					else if(trim(info[1]) == "no")
					{
						document.getElementById(p).src="images/no.gif";
						document.getElementById(p).alt = "未审核";
					}
					else if(trim(info[1]) == "sorry")
					{
						alert("操作失败,可能是您没有权限");
					}
		   break;
		   //----------------------------------------------------更改审核与未审核----改为文字显示-----------------------------------------------------
		   case "changestate_zi":
				var info = strValue;
					
					info = info.split("^");
					var p = "imgzi"+trim(info[0]);
					
					if(trim(info[1]) == "yes")
					{
						document.getElementById(p).innerHTML="<font color='blue'>已公开</font>";
					}
					else if(trim(info[1]) == "no")
					{
						document.getElementById(p).innerHTML="<font color='red'>未公开</font>";
					}
					else if(trim(info[1]) == "sorry")
					{
						alert("操作失败,可能是您没有权限");
					}
		   break;
		   //---------------------------------------------------------会员组会员级别连动-------------------------------------------
			case "setgrade": 
				var labels=strValue.split("|");
					document.getElementById("gradeid").options.length=0;
					
				for(var ii=0;ii<labels.length;ii++)
				{
					var nn = labels[ii].split("*");
					document.getElementById("gradeid").add(document.createElement("OPTION"));
					document.getElementById("gradeid").options[ii].text=nn[1];
					document.getElementById("gradeid").options[ii].value=nn[0];
				}
				document.getElementById("gradeid").selectedIndex = 0; 
			 break;
			 //---------------------------------------------------------标签内容检查SQL语句是否正确-------------------------------------------
			case "checksql":
				
					if(strValue.length==37)
					{
						document.getElementById("hintspan").innerHTML="非法SQL语句，只能执行select语句";
					}
					else if(strValue.length==31)
					{
						document.getElementById("hintspan").innerHTML="SQL语句语法有误";
					}
					else
					{
						document.getElementById("hintspan").innerHTML="正确,返回的结果集有<font color='red'>"+strValue+"</font>条数据";
					}
				break;
		}
	}
};    
/*
function EventDownloadEnd(strValue) 
{
    if(this.sDownloadEnd != null && this.sDownloadEnd != "" && document.getElementById(this.sDownloadEnd) != "undefined"){
    document.getElementById(this.sDownloadEnd).innerHTML = strValue; 
	}
};
*/
/*********************************************************************/

/*
//使用示例
function docallback(strValue) 
{
    document.getElementById("DownloadEnd").innerHTML = strValue;
	alert(strValue); 
};   
function sbutton() {
var aj = new Ajax("ajax.php",1,"act=ddd&bb=xxx",'EventDownloadEnd1',"Error","State","DownloadEnd");
aj.sMethod = "POST";
//重新定义返回操作函数
//aj.EventDownloadEnd = docallback;
aj.send();
}
</script>
<input type='button' name='submit' value='test ajax' onClick="javascript:sbutton()">
<div id="DownloadEnd">ssssssssssssssssss</div>
<div id="State"></div>
<div id="Error"></div>  
//ajax.php代码
<?php
Header("Content-type:text/html;charset=gb2312");//加入头，防止乱码
sleep(1);
echo "ajax测试成功";
?>
//*/
