function AjaxSubmit () {

	this.INSERT =  "insert";
	this.UPDATE =  "update";
	this.DELETE =  "delete";
	this.QUERY =  "query";
	
	this.SUCCESS =  "success";
	this.ERROR =  "error";
	this.JSON = "json";
	this.STRING = "string";
	this.DEFINEJSON = "defineJson";
	
	this.object =  null;
	this.asynchronous =  null;
	this.action =  null;
		
	this.params =  null;
	
	this.resultType="json";
	this.status =  null;
	this.data =  null;
	
	this.response =  null;
	
	this.init = function(object,action,asynchronous){
		this.object = object;
		this.action = action;
		this.asynchronous = asynchronous;
		
		this.params = null;
		this.status = null;
		this.data = null;
		this.response = null;
	};
	this.formParams = function(form){
		var json = "{";
		//var controls = document.all[form].getElementsByTagName("input");
		var controls = $(form).getElementsByTagName("input");
		var radios = new Array();
		var checkboxs = new Array();
		for(var i=0;i<controls.length;i++){
			if(controls[i].type == "text" || controls[i].type == "hidden" || controls[i].type == "password")
				json += "\""+controls[i].name+"\":\""+this.dealString(controls[i].value)+"\",";
			else
			if(controls[i].type == "radio"){
				var haveFlag = false;
				for(var j=0;j<radios.length;j++)
					if(radios[j] == controls[i].name){
						haveFlag = true;
						break;
					}
				if(!haveFlag)
					radios.push(controls[i].name);
			}
			else
			if(controls[i].type == "checkbox"){
				var haveFlag = false;
				for(var j=0;j<checkboxs.length;j++)
					if(checkboxs[j] == controls[i].name){
						haveFlag = true;
						break;
					}
				if(!haveFlag)
					checkboxs.push(controls[i].name);
			}
		}
		
		for(var i=0;i<radios.length;i++){
			json += "\""+radios[i]+"\":\""+this.dealString(this.getRadio(radios[i]))+"\",";
		}
		for(var i=0;i<checkboxs.length;i++){
			json += "\""+checkboxs[i]+"\":\""+this.dealString(this.getCheckbox(checkboxs[i]))+"\",";
		}
		
		var controls = $(form).getElementsByTagName("textarea");
		for(var i=0;i<controls.length;i++){
			json += "\""+controls[i].name+"\":\""+this.dealString(controls[i].value)+"\",";
		}
		
		var controls = $(form).getElementsByTagName("select");
		for(var i=0;i<controls.length;i++){
			json += "\""+controls[i].name+"\":\""+this.dealString(controls[i].value)+"\",";
		}
		json = json.substr(0,json.length-1);
		json += "}";
		
		this.params = DXJSON.parse(json,null);
		
	};
	this.dealString = function(str){
		var str = str.replace(/\"/g,"\\\"");
		str = str.replace(/\n/g,"\\n");
		return str;
	};
	this.getCheckbox = function(checkboxname) {
		var checkboxboxs = document.getElementsByName(checkboxname);
		var CheckboxValue = "";
		if (checkboxboxs != null) {
			
			if (checkboxboxs.length == null) {
				if (checkboxboxs.checked) {
					return checkboxboxs.value;
				}
			}
			for (i = 0; i < checkboxboxs.length; i++) {
				if (checkboxboxs[i].type == "checkbox" && checkboxboxs[i].checked) {
					if (CheckboxValue == "") {
						CheckboxValue += checkboxboxs[i].value;
					} else {
						CheckboxValue += "," + checkboxboxs[i].value;
					}
				}
			}
			//return checkboxboxs.value
		}
		return CheckboxValue;
	};
	this.getRadio = function(radioname){
		var radios = document.getElementsByName(radioname);
	 	for (var i=0; i < radios.length; i++)
	        if (radios[i].checked)
	            return radios[i].value;
	 };
	this.submit = function(){
		
		var json = "";
		if(this.params!=null){
			json += DXJSON.stringify(this.params);
		}
		
		//alert("params="+json);
		//alert("params="+encodeURIComponent(json));
		//alert(this.asynchronous);
		
		//alert(this.object+"!"+this.action+".action");
		var myAjax = new Ajax.Request(
		   this.object+"!"+this.action+".action",{
		     method: 'post',
		     parameters: "params="+encodeURIComponent(json)+"&_DXSUBMITTYPE_=Ajax",
		     onComplete: this.doResponse(this),
		     asynchronous: this.asynchronous
		});
		
	};
	this.doResponse = function(ajaxSubmit){
		return function(httpRequest){
			//alert(ajaxSubmit.object);
			//alert( ajaxSubmit.resultType+"\n"+httpRequest.responseText);
			if(ajaxSubmit.resultType =="json"){
				var json = DXJSON.parse(httpRequest.responseText,null);
				ajaxSubmit.status = json.status;
				//alert(json.status);
				
				if(json.data == 'undefined')
					ajaxSubmit.data = null;
				else
					ajaxSubmit.data = json.data;
			}
			else
			if(ajaxSubmit.resultType == "string"){
				ajaxSubmit.data = httpRequest.responseText.toString();
			}
			else
			if(ajaxSubmit.resultType == "defineJson"){
				var txt = httpRequest.responseText;
				txt = txt.replace(/\n/g,"");
				txt = txt.replace(/\r/g,"");
				var json = DXJSON.parse(txt,null);
				ajaxSubmit.data = json;
			}
			//alert(this.data.content);
			//alert(ajaxSubmit.object+"!"+ajaxSubmit.action+".action");
			if(ajaxSubmit.response!=null)
				ajaxSubmit.response();
				
		}
	}
	this.test = function(){
		alert(this.object);
	}
}
