/*
mino Class Request

coded by : hanulis (hanulis@docircle.com)
date : 2006. 12. 8

about : AJAX Request function
usege : (object) mino.Request(url,params,method,callback[,param1,param2,...])

		param1, param2, ... 등등은 Request후 callback function에 넘겨줄 인자들을 받는다./
		callback function에서는 req(xml original), json, params(array)를 넘겨받는다.

*/
mino_request = {}

mino_request = function() {
	this.params=new Array();
	this.responseFunc=new Array();
	this.method="POST";
	this.url="";
	this.transVars=new Array();
	this.trycount=1;
	this.timeout=300000;
	this.async=1;
	//this.params = (params == null) ? null : params;
}

mino_request.prototype = {
	addParam:function(name,val) {
		this.params[name]=val;
	},
	
	makeParam:function() {
		var str="";
		for(var i in this.params) {
			str+=i+"="+this.params[i]+"&";
		}
		
		return str;
	},
	
	addResponse:function(fnc) {
		this.responseFunc.push(fnc);
	},
	
	addVar:function(v) {
		this.transVars.push(v);
	},
	
	getXMLHttpRequest: function() {

		var XMLHttpFactories = [
			function () {return new XMLHttpRequest()},
			function () {return new ActiveXObject("Msxml2.XMLHTTP")},
			function () {return new ActiveXObject("Msxml3.XMLHTTP")},
			function () {return new ActiveXObject("Microsoft.XMLHTTP")}
		];

		var xmlhttp = false;
		for (var i=0;i<XMLHttpFactories.length;i++) {
			try {
				xmlhttp = XMLHttpFactories[i]();
			}
			catch (e) {
				continue;
			}
			break;
		}
		
		return xmlhttp;
	},

	send: function() {
		this.req = this.getXMLHttpRequest();

		if(!this.req) alert("failed to request xmlHTTP.");

		var httpMethod = this.method ? this.method : 'GET';
		if (httpMethod != 'GET' && httpMethod != 'POST') httpMethod = 'POST';
		var httpParams = this.makeParam();
		var httpUrl = this.url;
		if (httpMethod == 'GET' && httpParams != null) httpUrl = httpUrl + "?" + httpParams;
		
		//sync or async
		if(this.async==1) this.req.open(httpMethod, httpUrl, true);
		else {
			this.req.open(httpMethod, httpUrl, false);
		}

		this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		var request = this;

		this.requestTimer = setTimeout(function() {if(request.trycount==5) {request.req.abort();} else {request.resend();}}, this.timeout);

		if(this.async==1) {
			this.req.onreadystatechange = function() {
				request.onStateChange.call(request);
			}
		}

		this.req.send(httpMethod == 'POST' ? httpParams : null);

		if(this.async!=1) {
			clearTimeout(this.requestTimer);
			if(this.responseFunc) this.callback(this.req,this.responseFunc,this.transVars);
		}
	},

	resend: function() {
		this.trycount++;
		this.send();
	},

	onStateChange: function() {
	
		if(this.req.readyState == 4 && this.req.status==404) {
			var al=new mino.alert();
			al.message="404 not found. ('"+this.url+"')";
			al.open();
			
			return;
		} else if(this.responseFunc) this.callback(this.req,this.responseFunc,this.transVars);
		else {
			if(this.req.readyState == 4) clearTimeout(this.requestTimer);
		}
		return;
	},

	callback: function (req,notifyto,transvars){
		if(req.readyState == 4){
			clearTimeout(this.requestTimer);
			 if(req.status == 200){

				var t=req.responseText;

				if(t.match(/Parse error/)) {
					var al=new mino.alert();
					al.message="PHP error\n\n"+t;
					al.open();
					return;
				}

				var t=req.responseXML;
				try
				{
					var err=t.selectSingleNode("//ERRORMESSAGE").firstChild.nodeValue;
					if(err != "success") {
						var al=new mino.alert();
						al.message=err;
						al.open();						
						return;
					}
				}
				catch (e){}


//				if(t.getElementsByTagName("ERRORMESSAGE").item(0).nodeValue) {
//					alert_layer("DB Qeury ERROR\n\n"+t.getElementsByTagName("ERRORMESSAGE").item(0).nodeValue);
//					return;
//				}

				if(notifyto) {
					for(var i in notifyto) {
						if(transvars) {
							notifyto[i](req,transvars);
						} else {
							notifyto[i](req);
						}
					}
				}
			} else {
				if(req.status=="0" || req.status=="12029" || req.status=="12030" || req.status=="12152") {
					try{
						this.trycount=1;
						this.req.abort();
						this.resend();
 					} catch (e) {
						return false;
					}
					//alert_layer("Process failed temporarily due to poor network connection.  Please try again...");
				} else {
					var al=new mino.alert();
					al.message='HTTP ERROR:'+req.status+'<br><br>callback Failed.';
					al.open();
				}
			}
		}
	} 
}