﻿/****************************************************/
/*                  Ajax Model                      */
/*               Author:Yoyo.Pan                    */
/****************************************************/
function HashTable(){
    this.Items=[];
    this.AddItem=function(_name,_value){
	    for(var i=0;i<this.Items.length;i++){
		    if(this.Items[i].Name==_name) {
		        this.Items[i].Value=_value;
		        return i;
		    }
	    }
	    this.Items.push({Name:_name,Value:_value});
	    return this.Items.length-1;
    };
    this.GetItem=function(_name){
        for(var i=0;i<this.Items.length;i++){
		    if(this.Items[i].Name==_name) {
		        return this.Items[i];
		    }
	    }
	    return null;
    };
    this.RemoveItem=function(name){
	    if(this.Count==0) return;
	    for(var i=0;i<this.Items.length;i++){
		    if(this.Items[i].Name==name) {this.Items.splice(i,1);break;}
	    }
    };
    this.Count=function(){return this.Items.length;};
    this.Clear=function(){
        for(var i=0;i<this.Items.length;i++) this.Items.pop();
    };
    this.toUrlString=function(){       
        var str=new Array();        
        for(var i=0;i<this.Items.length;i++){
            str[i]= this.Items[i].Name + "=" + this.Items[i].Value;
        }
        return str.join("&");
    };
}
var Ajax={
	Objects:[],
	Create:function(){
		var o;var V=["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];	
		if(window.XMLHttpRequest) o=new XMLHttpRequest();
		else if (window.ActiveXObject) {
			for(var i=0;i<V.length;i++) {
				try{
					o=new ActiveXObject(V[i]);
					break;
				}
				catch(e){}
			}
		}
		if(!o) alert("Create XMLHttpRequest Failed!");	
		else this.Objects.push(o);
	},
	Send:function(Url,Paras,onComplete,sMethod,sEncoding){
		var Request=null;
		for(var i=0; i<this.Objects.length; i++){
			if(this.Objects[i].readyState==4){
				this.Objects[i].abort();
				Request=this.Objects[i];
				break;
			}
		}
		if(!Request) {
			this.Create();
			Request=this.Objects[this.Objects.length-1];
		}
		if(!sMethod) sMethod=this.Options.Method;
		if(Paras && typeof(Paras)=="object") Paras=Paras.toUrlString();
		if(sMethod.toLowerCase()=="get"&&Paras) Url+="?"+Paras;
		Request.open(sMethod,Url,true);			
		with(this.Options){			
			Header.AddItem("content-type",ContentType+"; charset="+(sEncoding?sEncoding:Encoding));
			for(var i=0;i<Header.Items.length;i++) Request.setRequestHeader(Header.Items[i].Name,Header.Items[i].Value);
		}		
		var async=typeof(onComplete)=='function';		
		if(async){
			Request.onreadystatechange=function(){
				if(Request.readyState==4) {
				    Ajax.Events.onLoaded();
					if(Request.status==200) onComplete(new Response(Request));
					else Ajax.Events.onError(Request.status);					
				}
			};
		}		
		this.Events.onLoading();
		Request.send(Paras);		
		if(!async) return new Response(Request);
	},
	Options:{
		Method:'GET',		
		ContentType:'application/x-www-form-urlencoded',
		Encoding:'UTF-8',
		Header:new HashTable()
	},	
	Events:{
	    msgLoading:"Loading ...",
		onError:function(status){alert("Request failed with status:"+status);},
		onComplete:function(){},
		onLoading:function(){
			var s=G$("vLoading");
			if(!s) {
				s=C$("DIV");
				s.id="vLoading";				
				s.innerHTML=this.msgLoading;
				document.body.appendChild(s);
			}
			s.style.display="";
		},
		onLoaded:function(){
			G$("vLoading").style.display="none";
		}
	},
	Test:function(o){
	    s="";
	    for(var i in o) s+=i+":"+o[i]+"\n";
	    alert(s);
    },
    Parameters:new HashTable()
};

var Response=function(request){
        this.Request=request;
	    this.text=request.responseText;
	    this.xml=request.responseXML;	
};
function C$(t){return document.createElement(t);}
function G$(o){return document.getElementById(o);}
