﻿Json.Ajax={
    Submit : function(id,param){
        $("body").before("<div class='loading'>Loading......</div>");
        var p = this.GetParam(id,param);
        $.ajax({
          type: "POST",
          url: window.location.href,
          data: p,
          success: this.CallBack,
          error: this.Exception
        })
    },
    GetParam : function(id,param){
        var p = "";
        var item = $("#Json_TypeName").val().split("^");

        for(var i=0;i<item.length;i++){
            var tName = item[i];
            p +="&"+tName+"="+encodeURIComponent($("#"+tName).val());
        }
        p += "&Json_TypeName=" + encodeURIComponent($("#Json_TypeName").val());
        p += "&__EVENTTARGET="+encodeURIComponent(id);
        p += "&__EVENTARGUMENT="+encodeURIComponent(param);
        p += "&__VIEWSTATE="+encodeURIComponent($("#__VIEWSTATE").val());
        p += "&__EVENTVALIDATION="+encodeURIComponent($("#__EVENTVALIDATION").val());
        p += "&__SUBMIT_TYPE=AJAX";
        
        if(p!=""){
            p = p.substring(1, p.length);
        }
        return p;
    },
    Exception : function(http,msg,ex){
        $('div.loading').fadeOut('fast');//hide all indicator 
        //ex: client-side?
        if(!ex){//Server exception ?
            if(http.status==700)//Login page id
                ex=Json.Util.getMessage('MSG_TIMEOUT');
            else
                ex=http.statusText+' [Code:'+http.status+']';
        }
        alert(ex);
    },
    CallBack:function(res){
        if(res && res.length>0){
            var db = eval("("+res+")");
            Json.Dao.db = db;
            Json.Config = Json.Dao.searchByList("Json_Config_Info");
            if(Json.Config.OpType =="SaveAll" || Json.Config.OpType =="Search"){
                getJson().LoadAll();
            }
            if(Json.Config.Message && Json.Config.Message!=""){
                alert(Json.Config.Message);
            }
        }
        
        $("div.loading").fadeOut('fast');
    } 
}
