﻿//处理ajax方法

//显示浮动层
//args[0]要显示浮动层的id，为空则显示在body
//args[1]写入浮动层的内容，为空则显示默认
function ShowLoading(divId, cont) {
    var w = $("body").width();
    var h = $("body").height();
    var l = 0;
    var t = 0;
    if (divId) {
        w = $("#" + divId).width();
        h = $("#" + divId).height();
        l = $("#" + divId).offset().left;
        t = $("#" + divId).offset().top;
    }
    var content = "loading";
    if (cont) {
        content = cont;
    }

    $("body").append("<div id='ajaxloading' class='ajaxloading' style='width:" + w + "px;height:" + h + "px; top:" + t + "px;left:" + l + "px;'>" + content + "</div>");
}

//隐藏浮动层
function HideLoading() {
    $("#ajaxloading").remove();
}

function DisableSubmiter() { }
function EnableSubmiter() { }

function ShowError() {
    HideLoading();
}

//post发送ajax
//args[0]发送到的url
//args[1]要提交数据的Id
//args[2]显示浮动层的所在id，即需要禁止点击的控件id
function SendPost(purl, fromId, divId) {
        if (divId) {
            ShowLoading(divId)
        }
        else {
            ShowLoading(); DisableSubmiter();
        }

    $.ajax({
        type: "POST",
        url: purl,
        data: $('#' + fromId).serialize(),
        timeout: 20000,
        error: function(xmlHttpRequest, error) { ShowError(); },
        success: ShowResult
    });
}

//GET发送ajax
//args[0]发送到的url
//args[1]显示浮动层的所在id，即需要禁止点击的控件id
//args[2]是否显示浮动层
function SendGet(purl,divId) {
    if (divId) { ShowLoading(divId) }
    else { ShowLoading(); DisableSubmiter(); }
    $.ajax({
        type: "GET",
        url: purl,
        timeout: 20000,
        error: function(xmlHttpRequest, error) { ShowError(); },
        success: ShowResult
    });
}

//GET发送ajax,不显示浮动层
//args[0]发送到的url
function SendGetNoLoading(purl) {
    $.ajax({
        type: "GET",
        url: purl,
        timeout: 20000,
        error: function (xmlHttpRequest, error) { ShowError(); },
        success: ShowResult
    });
}

//GET发送ajax,返回单个数值并直接替换内容
//args[0]ajax发送的url
//args[1]要替换内容的控件id
function SendGetUseBlockUINoLoading(purl, divID) {
    $.ajax({
        type: "GET",
        url: purl,
        timeout: 20000,
        success: function (data, textStatus) {
            $('#' + divID).html(data);
        },
        error: function (xmlHttpRequest, error) { ShowError(); }
    });
}

//POST发送ajax,返回单个数值并直接替换内容
//args[0]ajax发送的url
//args[1]传递参数所在form的id
//args[2]要替换内容的控件id
function SendPostUseBlockUINoLoading(purl, fromID,divID) {
    $.ajax({
        type: "POST",
        url: purl,
        data: $('#' + fromID).serialize(),
        timeout: 20000,
        success: function (data, textStatus) {
            $('#' + divID).html(data);
        },
        error: function (xmlHttpRequest, error) { ShowError(); }
    });
}

//GET发送ajax，直接替换内容
//args[0]ajax发送的url
//args[1]要替换内容的控件id
function SendGetUseBlockUI(purl,divID ) {
    ShowLoading(divID);
    $.ajax({
        type: "GET",
        url: purl,
        timeout: 20000,
        beforeSend: function(XMLHttpRequest) {
            //$('#' + divID).block();
        },
        success: function(data, textStatus) {
            $('#' + divID).html(data);
        },
        complete: function (XMLHttpRequest, textStatus) {
            HideLoading();
            //$('#' + divID).unblock();
        },
        error: function(xmlHttpRequest, error) { ShowError(); }
    });
}

//POST发送ajax，直接替换内容
//args[0]ajax发送的url
//args[1]传递参数所在form的id
//args[2]要替换内容的控件id
function SendPostUseBlockUI(purl, formID, divID) {
    ShowLoading(divID);
    $.ajax({
        type: "POST",
        url: purl,
        data: $('#' + formID).serialize(),
        timeout: 20000,
        beforeSend: function (XMLHttpRequest) {
            //$('#' + divID).block();
        },
        success: function (data, textStatus) {
            $('#' + divID).html(data);
        },
        complete: function (XMLHttpRequest, textStatus) {
            HideLoading();
            //$('#' + divID).unblock();
        },
        error: function (xmlHttpRequest, error) { ShowError(); }
    });
}

//处理ajax返回数据
//要求返回的数据格式：{state:"OK|Err|Msg|Fuc",value:"value",parm:"Parm"}
//eg:{state:"Fuc",value:"方法名称",parm:"需要传入的参数(可为空)"}
function ShowResult(t) {
    HideLoading(); EnableSubmiter();
    var json = eval("(" + t + ")");
    switch (json.state) {
        case "OK":
            {
                window.location = json.value;
                break;
            }
        case "Err":
            {
                window.location = json.value;
                break;
            }
        case "Msg":
            {
                alert(json.value);
                break;
            }
        case "Fuc":
            {
                if (json.parm) {
                    eval(json.value + "('" + json.parm + "')");
                } else {
                    eval(json.value + "()");
                }
                break;
            }
        default:
            {
                window.location = json.value;
                break;
            }
    }
}

//同步发送ajax返回结果




function SendGetReturn(purl) {
    ShowLoading();
    $.ajax({
        type: "GET",
        url: purl,
        timeout: 20000,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //$('#' + divID).block();
        },
        success: function (data, textStatus) {
            return data;
        },
        complete: function (XMLHttpRequest, textStatus) {
            HideLoading();
            //$('#' + divID).unblock();
        },
        error: function (xmlHttpRequest, error) { ShowError(); }
    });
}

