﻿/*
url:提交地址
datastr:提交的数据，可以以"act=upload&id=211"或{act:"upload",id:211}两种形式
posttype:提交类型，POST,GET,安全起见尽量使用POST
datatype:返回数据类型,"html"、"xml",可查看jq中文参考文档中更多数据类型
fn:提交完成后执行
msgdiv:提交时显示状态的DIV　ID
msgmattime:提示显示时间 消息显示后多少毫秒后关闭消息
msgsending:提交时显示加载状态的文字提示,可选，可以为函数（无参函数），也可为字符串，字符串时需要msgdiv才输出显示
msgcomplete:提交时显示完成状态的文字提示,可选，可以为函数（无参函数），也可为字符串，字符串时需要msgdiv才输出显示
msgerror:提交时显示加载错误状态的文字提示,可选，可以为函数（无参函数），也可为字符串，字符串时需要msgdiv才输出显示
*/
function AjaxLoad(posturl,datastr,posttype,datatype,fn,msgdiv,msgmattime,msgsending,msgcomplete,msgerror)
{
    var mdiv=null;
    if(msgdiv!="")mdiv=jq("#"+msgdiv);
    //posturl += "&tim=" + Math.random(); //弄一个随机值，使得每次的URL不一样，以防止AJAX从缓存里读取而不实时更新数据
    jq.ajax({
        url: posturl,
        type: posttype,
        data:datastr,
        dataType: datatype,//"html"、"xml"
        timeout: 20000,//20秒超时       
        success: fn,        
        beforeSend: function(XMLHttpRequest)
        {
            if(typeof msgsending =="function")msgsending();
            else
            {
                if(mdiv==null)return;
                mdiv.fadeIn(msgmattime);
                if(msgsending != "")
                mdiv.html((msgsending == null ? "Loading…":msgsending));
            }
        },        
        complete: function(XMLHttpRequest, textStatus)
        {
            if(typeof msgcomplete =="function")msgcomplete();
            else
            {
                if(mdiv==null)return;
                if(msgcomplete != "")
                    mdiv.html((msgcomplete == null ? "Loading complete." : msgcomplete)).fadeOut(msgmattime);
            }
        },        
        error: function()
        {
            if(typeof msgerror =="function")msgerror();
            else
            {
                if(mdiv==null)return;
                if(msgerror != "")
                    mdiv.html((msgerror == null ? "Loading erro！Please try again later." : msgerror)).fadeOut(5000);
            }
        }
    });
}
