﻿var XHR;

XHR = function(){
	return new XHR.init();
}

XHR.constractor = (function(){
	var funcs = [
		function(){return new ActiveXObject("Msxml2.XMLHTTP");},
		function(){return new ActiveXObject("Microsoft.XMLHTTP");},
		function(){return new XMLHttpRequest();}
	];
	
	var constractor = null;
	for(var i=0; i<funcs.length; i++){
		try{
			var req = funcs[i]();
			if(req != null) constractor = funcs[i];
		}
		catch(e){continue;}
	}
	if(constractor) return constractor ;
	else throw new Error("XMLHttpRequest not supported!");
})();


XHR.init = function(){
	this.xhrobj = XHR.constractor();
}
XHR.init.prototype = {
	async : true,
	cache : true,
	request : function(data){
		
		/* data = {
			url : [URL 必須]
			async : [boolean] default true
			cache : [boolean] default true
			method : [GET or POST 必須]
			request : [Object or String サーバーAPI側で処理するデータが必要な際]
			errfunc : [Function エラー時の処理 指定がなければ空の匿名関数を実行]
			loadfunc : [Function ロード中の処理 指定がなければ空の匿名関数を実行]
			compfunc : [Function 通信完了の処理]
			username : [String]
			password : [String]
		}*/
		
		try{

			var async = data.async||this.async;
			var cache = data.cache||this.cache;
	
			if(!data.username) this.xhrobj.open(data.method , data.url , async);
			else this.xhrobj.open(data.method , data.url , async , data.username , data.password);
			
			var _this = this.xhrobj;
			this.xhrobj.onreadystatechange = function(){
							
				if(_this.readyState != 4 && _this.readyState != 0){
					data.loadfunc?data.loadfunc(_this.readyState):function(){}();
				}
				else if(_this.readyState == 4){
					
					if(_this.status == 200){
						var headers = {};
						
						var hds = _this.getAllResponseHeaders();
						hds = hds.replace(/\r/g,"");
						hds = hds.split("\n");
						
						for(var i=0; i<hds.length; i++){
							var hd = hds[i].split(": ");
							headers[hd[0]] = hd[1];
						}
						
						var type = headers["Content-Type"];
						
						if((type == "text/plain") || (type == "text/html")) {
							data.compfunc(_this.responseText , _this.status , headers);
						}
						if((type == "application/x-javascript") || (type == "text/javascript")) {
							data.compfunc(_this.responseText , _this.status , headers);
						}
						if((type == "application/xml") || (type == "text/xml")) {
							data.compfunc(_this.responseXML , _this.status , headers);
						}
					}
					else {
						data.errfunc?data.errfunc("[" + _this.status + "]"):function(){}();
					}
				}
			}
			
			if(!cache) this.xhrobj.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT");
			
			if(data.request){
				var _reqestdata = null;
				if(typeof data.request == "string") _requestData = encodeURIComponent(data.request);
				else {
					var _arr = [];
					for(prop in data.request) _arr.push(encodeURIComponent(prop) + "=" + encodeURIComponent(data.request[prop]));
					_reqestdata = _arr.join("&");
				}
				this.xhrobj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				this.xhrobj.send(_reqestdata);
			}
			else this.xhrobj.send(null);
			
			return this;

		}catch(e){
			var errtxt = "error script is 'XHR.init'\n";
			for(var prop in e) errtxt += prop + " : " + e[prop] + "\n";
			throw new Error(errtxt);
		}
	},
	abort : function(){
		this.xhrobj.abort();
		return this ;
	}
}

