﻿String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function Ryo_Ajax(){
    this.div=null;
    this.aobj=null;
	this.callBack=null;
	this.Method="post";
    try{
        var xhr = new XMLHttpRequest();
    }catch (e){
        var a = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'MICROSOFT.XMLHTTP.1.0','MICROSOFT.XMLHTTP.1', 'MICROSOFT.XMLHTTP'];
        for (var i = 0; i < a.length; i++){
            try{
                var xhr = new ActiveXObject(a[i]);
                break;
            }catch (e){}
        }
    }
    if (xhr){this.aobj=xhr;}else{this.aobj=null;}
}

Ryo_Ajax.prototype.showObjCellInfo=function(strMsg){
    try{
        if(this.div!=null && this.div!="undefined"){
            this.div.style.display=strMsg.trim()==""?"none":"block";
			try{this.div.tagName.toLowerCase().trim()=="input"?this.div.value=strMsg:this.div.innerHTML=strMsg;}catch(e){}
        }
    }catch(err){}
}

Ryo_Ajax.prototype.Send=function(){
	var url=arguments[0] || "";
	var data=arguments[1] || "";
	var odav=arguments[3] || document.getElementById("AjaxMessage");
	var callBack=arguments[2] || null;
    try{
		this.div=odav;	
		this.callBack=callBack;
        var objC=this;
        this.aobj.open("POST", url, true);
        this.aobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    this.aobj.onreadystatechange = function(){objC.ProcessChange(objC);};
		this.showObjCellInfo("Loading...");
        this.aobj.send(data);
    }catch(err){
        this.showObjCellInfo("Errors:"+err.message);
    }
}

Ryo_Ajax.prototype.ProcessChange=function(obj){
	var RtnValue;
	try{
		if(obj.aobj.readyState == 4){
			switch(obj.aobj.status){
				case 200:
					obj.showObjCellInfo("");
					try{
						RtnValue=unescape(obj.aobj.responseText);
						obj.DefaultCallBack(RtnValue,obj);
					}catch(e){
						obj.showObjCellInfo("Eval Error:"+e.message);	
					}
					break;
				case 404:
					obj.showObjCellInfo("target page not found!");break;
				case 500:
					obj.showObjCellInfo("php script have some error!");break;
				default:
					obj.showObjCellInfo("Receiving data problems! Please ensure that your network connection debris; Otherwise, timely and administrator, thank you!");break;					
			}
			
		}else if(obj.aobj.readyState == 0){
			obj.showObjCellInfo("Data sent failure!");
		}else if(obj.aobj.readyState == 1){
			obj.showObjCellInfo("Please wait, data loading!");
		}else if(obj.aobj.readyState == 2){
			obj.showObjCellInfo("Please wait, data loading!..");
		}else if(obj.aobj.readyState == 3){
			obj.showObjCellInfo("Please wait, data loading!....");
		}
	}catch(e){
		obj.showObjCellInfo("Errors:"+e.message);
	}
}

Ryo_Ajax.prototype.DefaultCallBack=function(RtnStr,rObj){
	if(rObj.callBack==null){
		alert(RtnStr);
	}else{
		rObj.callBack(RtnStr,rObj);
	}
}