
// Xml Request(My AJAX) by small

function XRequest(){

	this._currentRequestObj=null;
	this._mode="object";
	this._callback=null;
	this._callbackArg=null;
	this.OnObjectStringCreated=null;
	
	this.GetXmlReqObj=function(){
		try{
			if(window.XMLHttpRequest){
				return new XMLHttpRequest(); }
		}catch(e){}
		var obj;
		var t=["Microsoft.XMLHTTP","MSXML2.XmlHttp"];
		for(var i=0;i<t.length;i++){
			try{
				obj=new ActiveXObject(t[i]);
				if(obj){ return obj;}
			}catch(e){	}
		}
		return false;
	}
	
	this.Reset=function(){
		if(this._currentRequestObj){
			try{
				this._currentRequestObj.abort();
			}catch(e){}
			this._currentRequestObj=null;
		}
	}
	
	this.SetCallBack=function(handler, arg){
		if(typeof(handler)=="function"){
			this._callback=handler;
			this._callbackArg=arg;
		}
	}
	
	//
	this.GetRemoteObject=function(url, handler, handlerArg){
		this.SendRequest(url, handler, handlerArg, "object");
	}
	
	this.GetRemoteString=function(url, handler){
		this.SendRequest(url, handler, handlerArg, "string");
	}
	
	this.SendRequest=function(url, handler, handlerArg, mode){
		this.Reset();
		this._currentRequestObj=this.GetXmlReqObj();
		if(this._currentRequestObj){
			this._mode=mode;
			var request=this;
			this._currentRequestObj.onreadystatechange=function(){ 
				request.OnRequestStateChanged.call(request);
			}
			this._currentRequestObj.open("GET", url, true);
			this.SetCallBack(handler, handlerArg);
			try{ 
				this._currentRequestObj.send(null); 
			}catch(e){ }
		}
	}
	
	this.FromXmlNodeToObjectString=function(node){
		var str="{";
		if(node){
			var attributescount=0;
			var subobjectcount=0;
			var needlastquot=false;
			if(node.attributes){
				for(var i=0;i<node.attributes.length;i++){
					if(attributescount>0){
						str+=", ";
					}
					str+=new String(node.attributes[i].name).toLowerCase();
					str+=":";
					str+="\""+node.attributes[i].value+"\"";
					attributescount++;
					needlastquot=true;
				}
			}
			var nodetext="";
			if(node.childNodes.length>0){
				if(node.childNodes.length==1 && (node.childNodes[0].nodeType==3 /* NODE_TEXT */ || node.childNodes[0].nodeType==4 /* NODE_CDATA_SECTION */ )){
					nodetext=node.childNodes[0].nodeValue;
				}else{
					if(attributescount>0){
						str+=", ";
					}
					str+="Items:[ ";					
					for(var i=0;i<node.childNodes.length;i++){
						if(node.childNodes[i].nodeType==3 /* NODE_TEXT */ || node.childNodes[0].nodeType==4 /* NODE_CDATA_SECTION */){
							nodetext=node.childNodes[i].nodeValue;
						}else if(node.childNodes[i].nodeType==1){
							if(subobjectcount>0){
								str+=", ";
							}
							str+=this.FromXmlNodeToObjectString(node.childNodes[i], true);
							subobjectcount++;
						}
					}
					str+=" ]";
					needlastquot=true;
				}
			}
			if(nodetext!=""){
				if(subobjectcount>0 || attributescount>0){
					str+=", ";
				}
				str+="Text:\""+nodetext+"\"";
				needlastquot=true;
			}else{
				if(subobjectcount>0 || attributescount>0){
					str+=", ";
				}
				str+="Text:\"\"";
				needlastquot=true;
			}
			if(needlastquot){
				str+=", ";
			}
			str+="NodeName:\""+node.nodeName+"\"";
		}
		str+="}";
		return str;
	}
	
	this.OnRequestStateChanged=function(){
		var obj=this._currentRequestObj;
		if(obj.readyState==4){
			if(this._mode=="object"){
				var x=obj.responseXML;
				//alert(this.GetXmlReqObj);
				if(!this._callback){ return; }
				if(x.documentElement){
					var objecttext=this.FromXmlNodeToObjectString(x.documentElement);
					eval("var content="+objecttext+";");
					//if(content){alert(content);}
					if(this.OnObjectStringCreated){
						if(typeof(this.OnObjectStringCreated)=="function"){
							this.OnObjectStringCreated(objecttext);
						}
					}
					if(this._callback && content){
						this._callback(content, this._callbackArg);
					}
					//this._callback(eval(objecttext));
					//eval("this._callback("+objecttext+");");
				}else{
					eval("this._callback({Error:'Invalid XML Response.'});");
					if(this._callback){
						this._callback({Error:'Invalid XML Response.'}, this._callbackArg);
					}
				}				
			}else if(this._mode=="string"){
				if(this._callback){
					this._callback(obj.responseText, this._callbackArg);
				}
			}
		}
	}

	
}

