//var AjaxTo = Class.create();

var AjaxTo = Class.create({

    thisForm : null,
	thisOForm : null,
    actionMethod : '',
    formAction : '',
    callBackAction : '',
    params : '',
    pToken : '',
    asynchronous : true,
    evalScripts : true,
	checkValidate : false,
	pageValidate : null,
    gCallCount : 0,
	objType : 'prototype',

	initialize: function(form) {
        this.init(form);
    },

    init: function(form,objType) {
        //this.form  = $(formName);
		this.setMyType(objType);
		if(typeof form == "string"){
			this.addFormElement(form);
		}else{
			this.addFormElement(form.name);
		}
    },

	setMyType : function(objType){
		this.objType = objType;
	},

	ajaxRequest : function(){
		var form = this.thisForm;
		var method = form.method;
		var formName = form.name;
		if(method == "get"){
			this.callRequestValidate(formName,"ajaxGet");
		}else if(method == "post"){
			this.callRequestValidate(formName,"ajaxPost");
		}else{
			alert("call method error!!");
		}
	},

    ajaxGet : function(){
        /*
        var form = $(this.postFormName);
        form.request({method: 'get', parameters:'method=' + this.actionMethod + '&sAction=' + this.formAction + '&' + this.params, onSuccess:this.handlerFunc, onFailure:this.errFunc,asynchronous:this.asynchronous, evalScripts:this.evalScripts});
        */
        var form = this.thisForm;
		var actionForm = form.action;
		if(this.objType == 'prototype'){
			new Ajax.Request(actionForm, {method: 'get', parameters:Form.serialize(form), onSuccess:this.handlerFunc, onFailure:this.errFunc,asynchronous:this.asynchronous, evalScripts:this.evalScripts});
		}else if(this.objType == 'jQuery'){
			jQuery.ajaxSetup({async: this.asynchronous});
			var options = { 
				//target:        '#output1',   // target element(s) to be updated with server response 
				//beforeSubmit:  showRequest,  // pre-submit callback 
				success:       this.handlerFunc,  // post-submit callback 
				type : 'GET'
		 
				// other available options: 
				//url:       url         // override for form's 'action' attribute 
				//type:      type        // 'get' or 'post', override for form's 'method' attribute 
				//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
				//clearForm: true        // clear all form fields after successful submit 
				//resetForm: true        // reset the form after successful submit 
		 
				// $.ajax options can be used here too, for example: 
				//timeout:   3000 
			}; 
		 
			// bind form using 'ajaxForm' 
			this.thisOForm.ajaxForm(options); 
		}else{
			alert("call method error!!");
		}
    },

    ajaxPost : function(){
        /*
        var form = $(this.postFormName);
        form.request({method: 'post', parameters:Form.serialize(form), onSuccess:handlerFunc, onFailure:errFunc,asynchronous:asynchronous, evalScripts:evalScripts});
        */
        var form = this.thisForm;
		var actionForm = form.action;
		if(this.objType == 'prototype'){
			new Ajax.Request(actionForm, {method: 'post', parameters:Form.serialize(form), onSuccess:this.handlerFunc, onFailure:this.errFunc,asynchronous:this.asynchronous, evalScripts:this.evalScripts});
		}else if(this.objType == 'jQuery'){
			jQuery.ajaxSetup({async: this.asynchronous});
			var options = { 
				//target:        '#output1',   // target element(s) to be updated with server response 
				//beforeSubmit:  showRequest,  // pre-submit callback 
				success:       this.handlerFunc,  // post-submit callback 
				type : 'POST'
		 
				// other available options: 
				//url:       url         // override for form's 'action' attribute 
				//type:      type        // 'get' or 'post', override for form's 'method' attribute 
				//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
				//clearForm: true        // clear all form fields after successful submit 
				//resetForm: true        // reset the form after successful submit 
		 
				// $.ajax options can be used here too, for example: 
				//timeout:   3000 
			}; 
		 
			// bind form using 'ajaxForm' 
			this.thisOForm.ajaxForm(options); 
		}else{
			alert("call method error!!");
		}
    },

    handlerFunc : function(t){
        eval("var rt=" + t.responseText);
        var form = this.thisForm;
        if(rt.content == ""){
            addFormInputElement(form,"submitMod","","hidden");
            addFormInputElement(form,"sAction",this.callBackAction,"hidden");
            addFormInputElement(form,"pToken",this.pToken,"hidden");
            this.formSubmit();
        }else{
            alert(rt.content);
        }
    },

    errFunc : function(t){
        alert('Error ' + t.status + ' -- ' + t.statusText);
    },

	formSubmit : function(){
        var form = this.thisForm;
		form.submit();
	},

    addFormElement : function(elementName){
        //var elem = $(elementName);
		eval('var elem = document.' + elementName + ';');
        if(!elem){
            var elem = document.createElement("form");
            //Element.extend(obj);
            elem.id = elementName;
            elem.name = elementName;
            document.body.appendChild(elem);
        }
		this.thisForm = elem;
		if(this.objType == 'prototype'){
			this.thisOForm = $(elementName);
		}else if(this.objType == 'jQuery'){
			eval("this.thisOForm = jQuery('#" + elementName + "')");
		}else{
			this.thisOForm = null;
		}
    },

	addFormElemByAry : function(queryParams){
		for(i=0; i<queryParams.length;i = i + 2){
			var elName = queryParams[i];
			if(queryParams[i+1] == false){
				var elValue = '';
			}else{
				var elValue = queryParams[i+1];
			}
			this.addFormInputElement(elName,elValue,"hidden");
		}
	},

    addFormInputElement : function(elementName,value,elementType){
        var form = this.thisForm;
		eval('var elem = form.' + elementName + ';');
        if(!elem){
            var obj = document.createElement("input");
            obj.value = value;
            obj.name = elementName;
			obj.id = elementName;
            obj.type = elementType;
            form.appendChild(obj);
        }else{
            eval('form.' + elementName + '.value = "' + value + '";');
        }
    },

    removeFormInputElement : function(elementName){
        var form = this.thisForm;
		eval('var elem = form.' + elementName + ';');
        if(elem){
            elem.remove();
        }
    },

	disableBtn : function(disabled){
        var form = this.thisForm;
		for(i = 0; i < form.length; i++){
			if(form[i].type == "button"){
				form[i].disabled = disabled;
			}
		}
		/*
		form.each(function() {
		  if(this.type == "button"){
			this.disabled = disabled;
		  }
		});
		*/
	},

	requestForm : function(){
		//eval("var form = document." + formName + "");
        var form = this.thisForm;
		var formName = form.name;
		this.callRequestValidate(formName,"formSubmit");
	},

	callRequestValidate : function(formName,callBackMethod){
		this.disableBtn(true);
		var chkSet = this.checkValidate(formName);
		if(!chkSet){
			this.disableBtn(false);
			return;
		}    
		eval("this." + callBackMethod + "()");
	},
	
	setFormElement : function(key,val){
		var form = this.thisForm;
		eval("form." + key + "='" + val + "'");
	}
});



