/* 
 *  Copyright (c) Netease.com, Inc. - 2006
 *  Author: Robin Pan (htmlor [at] gmail.com)
 *
 *
 *  InstSubmit() [class]
 *
 *  触发提交事件后，把数据通过xmlhttp发送到远端（异步）
 *
 *  需调用 XmlHttpRequest 类 （xmlhttprequest.js）
 *
/*----------------------------------------------------------------*/


function InstSubmit(sURL, oParas, eTrigger, fCallback){
	this.url = sURL;
	this.paras = oParas;
	this.trigger = eTrigger;
	this.callback = fCallback;
	this.method = "post";
	
	var ME = this;
	var $xmlhttp = null;
	
	this.setParas = function(oParas){
		this.paras = oParas;
	};
	this.setPara = function(sName, oValue){
		this.paras[sName] = oValue;
	};
	this.setMethod = function(sType){
		this.method = sType;
	};
	
	this.execute = function(){
		if(this.paras == null){
			alert("no paras");
			return false;
		}
		var parastr = "";
		for(var key in this.paras){
			parastr += "&" + key + "=" + this.paras[key];
		}
		var urlstr, data;
		if(this.method == "post"){
			urlstr = this.url;
			data = parastr;
		}
		else{
			urlstr = this.url + "?" + parastr;
			data = null;
		}
		if($xmlhttp == null){
			$xmlhttp = new XmlHttpRequest();
		}
		return $xmlhttp.send(urlstr, this.method, data, this.response);
	};
	this.response = function(sFlag, oReq){
		switch(sFlag){
			case "ing":
				ME.setTriggerStyle(1);
				ME.resLoading(oReq);
				break;
			case "ex":
				ME.setTriggerStyle(0);
				ME.resException(oReq);
				break;
			case "ok":
				ME.setTriggerStyle(0);
				ME.resComplete(oReq);
				break;
		}
	};
	this.resLoading = function(oReq){
		this.onLoading(oReq);
	};
	this.resException = function(oReq){
		this.onFailed(oReq);
	};
	this.resComplete = function(oReq){
		if(oReq.responseText == "success"){
			this.onSuccess(oReq);
			if(this.callback != null){
				this.callback(this.paras, this.trigger);
			}
		}
		else{
			this.onFailed(oReq);
		}
	};
	this.onLoading = function(oReq){
	};
	this.onSuccess = function(oReq){
	};
	this.onFailed = function(oReq){
		//alert(oReq.responseText);
		alert("提交未成功。");
	};
	this.setTriggerStyle = function(iStatus){
		if(this.trigger == null){
			return;
		}
		this.trigger.disabled = (iStatus == 0) ? false : true;
	};
}
