$package("js.net");

js.net.HttpConnection = function (httpType){
	this.httpType    = js.net.HttpConnection.prototype.XMLHTTP;
	this.method      = js.net.HttpConnection.prototype.POST;
	this.url         = "about:blank";
	this.async       = true;
	if(httpType)
		this.httpType = httpType;
}
js.net.HttpConnection.prototype.XMLHTTP     = "js.net.XMLHttpConnection";
js.net.HttpConnection.prototype.IFRAME      = "js.net.IFrameConnection";
js.net.HttpConnection.prototype.WINDOW_OPEN = "js.net.WindowOpenConnection";
js.net.HttpConnection.prototype.HttpType    = js.net.HttpConnection.prototype.XMLHTTP;
js.net.HttpConnection.prototype.POST        = "POST";
js.net.HttpConnection.prototype.GET         = "GET";
js.net.HttpConnection.prototype.method      = js.net.HttpConnection.prototype.POST;
js.net.HttpConnection.prototype.url         = "about:blank";
js.net.HttpConnection.prototype.async       = true;
js.net.HttpConnection.prototype.instance    = null;
js.net.HttpConnection.prototype.onload      = null;
js.net.HttpConnection.prototype.onerror     = null;

js.net.HttpConnection.prototype.open = function (url){
	if(url)
		this.url = url;
	if(!this.instance){
		this.instance = new js.net.XMLHttpConnection();//js.net.HttpFactory.prototype.getInstance(this.httpType);
		this.instance.owner = this;
	}
	if(this.instance){
		return this.instance.open(this.method, this.url, this.async);
	}
}

js.net.HttpConnection.prototype.send = function (text){
	if(this.instance){
		this.instance.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		return this.instance.send(text);
	}
}
/**
 * Construct HttpFactory to get httprequest by httpType
 * @class
 * @constructor
 */
js.net.HttpFactory = function (){
}
/**
 * get httprequest by httpType
 * @param {String} httpType: XMLHttpConnection|IFrameConnection|WindowOpenConnection
 * @return {IHttpRequest}
 */
js.net.HttpFactory.prototype.getInstance = function(httpType){
	if(!httpType)
		httpType = js.net.HttpConnection.prototype.XMLHTTP;
	return js.lang.ClassLoader.prototype.load(httpType);
}

/**
 * Construct IHttpRequest Interface
 * @class
 * @constructor
 */
js.net.IHttpRequest = function (){
};
js.net.IHttpRequest.prototype = new js.lang.BaseObject();
js.net.IHttpRequest.prototype.open    = function(url, method, async){};
js.net.IHttpRequest.prototype.send    = function(str){};
js.net.IHttpRequest.prototype.onload  = function(){};
js.net.IHttpRequest.prototype.onerror = function(){};

/**
 * Construct XMLHttpConnection.inherits from IHttpRequest.
 * @class XMLHttp is the XMLHttpRequest class.  
 *
 * @see IHttpRequest IHttpRequest is the base interface for this
 */
js.net.XMLHttpConnection = function () 
{
	try { 
		this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	} 
	catch(e) { 
		try 
		{ 
			this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(ex) { 
			this.xmlHttp = null;	
		} 
	} 
	if ( !this.xmlHttp && typeof XMLHttpRequest != "undefined" ) 
	{ 
		this.xmlHttp = new XMLHttpRequest(); 
	} 

} 
js.net.XMLHttpConnection.prototype = new js.net.IHttpRequest();
/**
 * open a url to prepare post request.
 * @param {String} url
 * @param {String} method
 * @param {Boolean} async
 * @return {Boolean}
 */
js.net.XMLHttpConnection.prototype.open = function(method, url, async){
	this.__method = method;
	this.xmlHttp.open(method, url, async);
};
/**
 * SetRequestHeader
 * @param {String} key
 * @param {String} value
 */
js.net.XMLHttpConnection.prototype.setRequestHeader = function(key, value){
	this.xmlHttp.setRequestHeader(key, value);
}
/**
 * post request in string as xml,jason...
 * @param {String} str
 * @return {Boolean}
 */
js.net.XMLHttpConnection.prototype.send = function(str){
	var __this = this;
	if(browser.isIE){
		this.xmlHttp.onreadystatechange = function(){
			var http = __this.xmlHttp;
			var result = false;
			if(http.readyState==4){
				if(http.status==200 || http.status==0){
					result = true;
					if(__this.onload) __this.onload(http);
				}
				if(!result && __this.onerror) __this.onerror(http);
			}
		}
	}
	else{
		this.xmlHttp.onload  = function(){if(__this.onload)__this.onload(this);};
		this.xmlHttp.onerror = function(){if(__this.onerror)__this.onerror(this);};
	}
	if(this.__method=="GET")
		str = null;
	this.xmlHttp.send(str);
}
js.net.XMLHttpConnection.prototype.onload  =  function(xmlhttp){if(this.owner)if(this.owner.onload)this.owner.onload(xmlhttp);};
js.net.XMLHttpConnection.prototype.onerror =  function(xmlhttp){if(this.owner)if(this.owner.onerror)this.owner.onerror(xmlhttp);};
js.net.XMLHttpConnection.prototype.xmlHttp = null;
