var AjaxRequest = Class.create({
	initialize: function(actionID){
		this._SOURCE='ajax';
		this.method='post';
		this.asyn=true;
		this.actionID=actionID;
		this.url=_SELF;
		this.pars='';
		this.result='';
		this.ajaxObj='';
	},
	
	setMethod: function(method){
		this.method=method=='get'?'get':'post';
	},
	
	setUrl: function(url){
		this.url=url;
	},
	
	setPars: function(pars){
		this.pars="actionID=" + encodeURIComponent(this.actionID) + "&_SOURCE=" + encodeURIComponent(this._SOURCE);
		if (!Object.isUndefined(pars)){
			this.pars+="&" + pars;
		}
	},
	
	setAsyn: function(asyn){
		this.asyn=asyn?true:false;
	},
	
	onCreate:function(){ 
	},
	
	onFailure: function(){
		alert('未能正常執行，請與管理員聯絡');
	},
	
	onSuccess:function(result){
		this.result=result;
		setLog('ajax-' + this.actionID,'debug');
		logConsoleResponse(this.result);    
		var exception=this.result.responseXML.getElementsByTagName("exception");
		if (exception.length==0){
			this.onSuccessNoExp();
		} else {
			this.onSuccessExp();
		}
	},
	
	onSuccessExp:function(){
	},
	
	onSuccessNoExp:function(){
		var responses=this.result.responseXML.getElementsByTagName("responses")[0];
		var reqResult=responses.getElementsByTagName("result")[0][_TEXT];
		if (reqResult){
			this.onSuccessResultTrue();
		} else {
			this.onSuccessResultFalse();
		}
	},
	
	onSuccessResultTrue:function(){

	},
	
	onSuccessResultFalse:function(){
	},
	
	onComplete:function(){
	},
	
	setAjaxReqObj: function(){
		setLog('method=' + this.method + ', asyn=' + this.asyn ,'ajax');
		setLog(this.url+'?'+this.pars,'ajax');
		this.ajaxObj = new Ajax.Request(
			this.url,{
				method: this.method, 
				postBody: this.pars, 
				asynchronous: this.asyn,
				onCreate:this.onCreate,
				onFailure:this.onFailure,
				onSuccess:this.onSuccess.bind(this),
				onComplete:this.onComplete.bind(this)
			}
		);
	}
}); 

//--------------------------------------------------------------------------------------------------------------

var AjaxForm = Class.create(AjaxRequest, {
	initialize: function($super,FormExt){
		$super();
		this.FormExt=FormExt;
		this.frmObj=FormExt.obj;
		this.actionID=this.FormExt.actionID;
	},
/*	
	setValidObj: function(validObj){
		if (validObj!=null){
			this.validObj=validObj;
		}
	},
	
	setMultiFCK: function(multiFCK){
		if (multiFCK!=null){
			this.multiFCK=multiFCK;
		}
	},
*/	
	
	onCreate:function($super){ 
		$super();
		/*
		//useless in slimbeauty
		toggleLoadingCorner();
		*/
	},
	
	onSuccessNoExp:function($super){
		var responses=this.result.responseXML.getElementsByTagName("responses")[0];
		this.respMsg=responses.getElementsByTagName("msg")[0][_TEXT];
		$super();
		if (this.respMsg!=''){
			this.FormExt.respMsgObj.setContent(this.respMsg);
			this.FormExt.respMsgObj.show();
		}
	},
	
	onSuccessResultTrue:function($super){
		$super();
	},
		
	onComplete:function($super){
		$super();
		/*
		//useless in slimbeauty
		toggleLoadingCorner();
		*/
	}
});

var AjaxForm_ADD = Class.create(AjaxForm, {
	onSuccessResultTrue:function($super){
		$super();
		this.FormExt.reset();
	}
});	

var AjaxForm_EDIT = Class.create(AjaxForm, {
});	

var AjaxForm_DEL = Class.create(AjaxForm, {
	onComplete:function($super){
		$super();
		this.FormExt.historyBack();
	}
});	

//Reset the form's values, you may continues to edit the form
var AjaxForm_RESET = Class.create(AjaxForm, {
	onSuccessResultTrue:function($super){
		$super();
		this.FormExt.setFrmData(this.result);
		
		//alert('old load file item');
		//if (this.FormExt.withFileUpload()){
			//this.FormExt.fileUploadLoadItem();
		//}
	}
});	

//Reset the form's values, and not allow to edit anymore
var AjaxForm_DETAIL = Class.create(AjaxForm_RESET, {
	onComplete:function($super){
		$super();
		this.FormExt.disable();
	}
});	


//--------------------------------------------------------------------------------------------------------------


var AjaxContent = Class.create(AjaxRequest, {

});

//---------------------------------------------------------------------------------------------------------------

var AjaxValidation = Class.create(AjaxRequest, {
	initialize: function($super,actionID){
		$super();
		if (Object.isUndefined(actionID)){
			this.actionID='check';
		} else {
			this.actionID=actionID;
		}
		this.msg='';
		this.ValidResult='';
		this.asyn=false;
	},
	onSuccessResultTrue:function($super){
		$super();
		this.ValidResult=true;
		setLog(this.ValidResult);
	},
	onSuccessResultFalse:function($super){
		$super();
		var responses=this.result.responseXML.getElementsByTagName("responses")[0];
		this.msg=responses.getElementsByTagName("msg")[0][_TEXT];
		//this.msg=getElementTextNS("","msg",this.result.responseXML.getElementsByTagName("responses")[0],0);
		this.ValidResult=false;
		setLog(this.ValidResult);
	},
	getMsg:function(){
		return this.msg;
	},
	getValidResult:function(){
		return this.ValidResult;
	}
});	
