﻿//堃

var AJAX = new Object();

AJAX.Sender=function(url,onLoad,xmlDom, isAsynchronously){
	this.xmlDom=xmlDom;
	this.xmlReq = null;
	this.onLoad=onLoad;
	if(isAsynchronously != null){
	    this.isAsynchronously = isAsynchronously;
	}else{
	    this.isAsynchronously = false;
	}
	this.loadXMLDoc(url);
	
}
AJAX.Sender.prototype={
	loadXMLDoc:function(url){
		
		if(window.XMLHttpRequest){
			this.xmlReq = new XMLHttpRequest();
			
		} else if(window.ActiveXObject){ 
		
			this.xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
			//alert("xmlReq:"+this.xmlReq.readyState);
		} else {
			alert("XMLHttpRequest can't initial");
			return;
		}
		if(this.xmlReq){
			try{
				var loader = this;
				
				//use "call" method to transport XMLHTTPRequest
				this.xmlReq.onreadystatechange=function(){
					loader.onReadyState.call(loader);
				}
			
				//this.xmlReq.onreadystatechange=this.onReadyState;//can't transport XMLHTTPRequest
				this.xmlReq.open('POST', url, this.isAsynchronously); //open(METHOD, URL, isAsynchronously)
				this.xmlReq.send (this.xmlDom);
				
			}catch(err){
				//var n= prompt("integer","");
				//var f=factorial(n);
				alert("ajax.js error");
				alert(err);
			}
			
		}
	},
	onReadyState:function(){
	
		var req = this.xmlReq;
		
		if(req.readyState==4){
//alert("req.status:"+req.status+" "+ typeof this.xmlReq);
			var httpStatus=req.status;
			if(httpStatus ==200||httpStatus==0){
			    
				this.onLoad.call(this,req);
				//alert(req.responseText);
				
				/*
				if(!document.getElementById("mes")){
					alert("no tag'mes', please set a tag for responseText of AJAX");
				}else{
					yy =document.getElementById("mes");
					yy.innerHTML += req.responseText;
				}
				*/
				
			}
		}
	}

}


AJAX.SenderParameter=function(url,onLoad,parameters, isAsynchronously){
	this.parameters=parameters;
	this.xmlReq = null;
	this.onLoad=onLoad;
	if(isAsynchronously != null){
	    this.isAsynchronously = isAsynchronously;
	}else{
	    this.isAsynchronously = false;
	}
	this.loadXMLDoc(url);
	
}
AJAX.SenderParameter.prototype={
	loadXMLDoc:function(url){
		
		if(window.XMLHttpRequest){
			this.xmlReq = new XMLHttpRequest();
			
		} else if(window.ActiveXObject){ 
		
			this.xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
			//alert("xmlReq:"+this.xmlReq.readyState);
		} else {
			alert("XMLHttpRequest can't initial");
			return;
		}
		if(this.xmlReq){
			try{
				var loader = this;
				
				//use "call" method to transport XMLHTTPRequest
				this.xmlReq.onreadystatechange=function(){
					loader.onReadyState.call(loader);
				}
			
				//this.xmlReq.onreadystatechange=this.onReadyState;//can't transport XMLHTTPRequest
				this.xmlReq.open('POST', url, this.isAsynchronously); //open(METHOD, URL, isAsynchronously)

				var parameterString="";
				var i=0;
				for(var key in this.parameters){
				    parameterString += (i > 0 ? "&" : "")
                        + key + "="
                        + encodeURI(this.parameters[key]);
                    i++;
                }
				
				this.xmlReq.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                this.xmlReq.setRequestHeader("Content-length",parameterString.length);
                this.xmlReq.setRequestHeader("Connection", "close"); 
                
				this.xmlReq.send(parameterString);
				
			}catch(err){
				//var n= prompt("integer","");
				//var f=factorial(n);
				alert("ajax.js error"+err);
				
			}
			
		}
	},
	onReadyState:function(){
	
		var req = this.xmlReq;
		
		if(req.readyState==4){
//alert("req.status:"+req.status+" "+ typeof this.xmlReq);
			var httpStatus=req.status;
			if(httpStatus ==200||httpStatus==0){
			    
				this.onLoad.call(this,req);
				//alert(req.responseText);
				
				/*
				if(!document.getElementById("mes")){
					alert("no tag'mes', please set a tag for responseText of AJAX");
				}else{
					yy =document.getElementById("mes");
					yy.innerHTML += req.responseText;
				}
				*/
				
			}
		}
	}

}
