﻿/// <reference path="jquery1.4.2.js" />

/******************************************施工报建***************************************************************/
//取最大值
var getMax = function (a) {
    var vv = a[0];
    for (var i = 0; i < a.length; i++) {
        vv = Math.max(vv, a[i]);
    }
    return vv;
}
//最小值
var getMin = function (a) {
    var vv = a[0];
    for (var i = 0; i < a.length; i++) {
        vv = Math.max(vv, a[i]);
    }
    return vv;
}

//总和
var getSum = function (a) {
    var vv = 0;
    for (var i = 0; i < a.length; i++) {
        vv += a[i];
    }
    return vv;
}
//数组重复性判别
var unique = function (a) {
    var b = true;
    for (var m = 0; m < a.length; m++) {
        for (var n = 0; n < a.length; n++) {
            if (m != n && a[m] == a[n]) {
                b = false;
                break;
            }
            if (!b) break;
        }
    }
    return b;
}
//选择下啦列表框
var selectDll = function (obj, v) {
    var v0 = "＾" + v + "＾";
    var dlist = $(obj).find("option");
    for (var i = 0; i < dlist.length; i++) {
        var vv = $(dlist[i]).attr("value");
        if (vv && vv.length > 0) {
            vv = "＾" + vv + "＾";
            if (vv.indexOf(v0) == 0) {
                $(dlist[i]).get(0).selected = true;
            }
        }
    }
}
//验证安全员的业绩
var validateSafeYeji = function (oItem) {
    try {
        var oldprice = parseFloat($("input[id$=hfOldPrice]").val());
        var oldarea = parseFloat($("input[id$=hfOldArea]").val());
        var oldNum = parseFloat($("input[id$=hfOldNum]").val());
        var tprice = parseFloat($(".tbPrice").val());
        var tarea = parseFloat($(".tbBuildingArea").val());
        var p = oldprice;
        var s = oldarea;
        var n = oldNum;
        if (oItem.o == "1") {
            n = 0;
            p = tprice - oldprice;
            s = tarea - oldarea;
        }
        else {
            n = 1;
            p = tprice;
            s = tarea;
        }

        var r = oItem.na >= oItem.nc + n;
        var pn = oItem.nc + n;
        var msg = "";
        if (!r) {
            msg = String.format("选择“{2}”担任{3}后，该人员所担任工程总个数为：{0}个，超过了其允许的工程个数{1}个", oItem.nc + n, oItem.na, oItem.name, oItem.n);
            return { "r": r, "msg": msg };
        }
        r = pn <= 1 || ((pn > 0) && (oItem.ma >= oItem.mc + p));
        if (!r) {
 
            msg = String.format("选择“{2}”担任{3}后，该人员所担任工程总造价为：{0}万元，超过了其允许的工程造价{1}万元", oItem.mc + p, oItem.ma, oItem.name, oItem.n);
            return { "r": r, "msg": msg };
        }
        r = pn <= 1 || ((pn > 0) && (oItem.sa >= oItem.sc + s));
        if (!r) {
            msg = String.format("选择“{2}”担任{3}后，该人员所担任工程总面积为：{0}平米，超过了其允许的工程面积{1}平米", oItem.sc + s, oItem.sa, oItem.name, oItem.n);
            return { "r": r, "msg": msg };
        }

        return { "r": r, "msg": msg };
    }
    catch (e) {
        return { "r": false, "msg": "数据格式不正确" };
    }
}
var is_selsearch = true;
//添加一个数据列
var add = function (formater, dll, scope) {
    var table = $(scope).closest("table");
    var item = $(formater);
    var select = $('[id$=' + dll + ']').clone(true).removeAttr("id");
    select.attr("id", "_a" + new Date().getTime());
    item.find(".s-name").append(select.show());
    item.find(".del").click(function () {
        if (confirm("您确定删除吗?")) {
            $(this).closest("tr").remove();
        }
    });
    item.find(".s-txt").click(function () {
        $(this).closest("tr").find(".edit").click();
    });
    table.find("tr:last").after(item);
    if (is_selsearch == undefined || is_selsearch == true)
        select.selsearch({ scroll: $("#add_edit_body_ct"), direction: "top" });
}

//判断施工单位技术人员是否重复
var validateSame = function () {
    var f = true;
    var persons = [];
    $("select.cc-person").each(function () {
        var v = $(this).val().split('＾')[0];
        if (v != "ef11d9e0-6a3c-4e45-902a-2a6c3416c8b6") {
            persons.push(v);
        }
    });
    $(".cc-person-box .s-id").each(function () {
        var v = $(this).text();
        if (v != "-1" && $.trim(v) != "") {
            persons.push(v);
        }
    });

    if (persons.length > 0 && !unique(persons)) {
        alert("施工单位存在一个技术人员担当两个角色职位!");
        f = false;
    }
    return f;
}

//验证需要技术人员人数
var validatePersonNum = function (personType, sr) {
    var f = true;
    var bt = parseInt($(".ddlBuildingCategoryId").val().split("＾")[1], 10);
    var price = parseFloat($(".tbPrice").val());
    var area = parseFloat($(".tbBuildingArea").val());
    var v = bt == 1 ? area : price;
    var items = $.grep(rules, function (item, i) {
        return item["bt"] == bt && item["st"] == personType && item["vs"] <= v && v <= item["ve"];
    });
    var sitem = items[0];
    var n = $(".ddlBuildingCategoryId").find("option:selected").text();
    if (sr.isValidate) {
        if (!(sr.count >= sitem["ps"] && sr.count <= sitem["pe"])) {
            alert(n + "工程," + sitem["des"]);
            f = false;
        }
    }
    else {
        f = false;
    }
    return f;
}
String.prototype.replaceAll = function (reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith);
    } else {
        return this.replace(reallyDo, replaceWith);
    }
}

var getSplistChar = function () {
    return "■";
};

var getRigthtChar = function () {
    return "▲";
}
/***************************************************************************************************/

function gv_import2(tab) {//导出全部
    if (!table_check_validator())
        return;
    var tab_id=tab;
    tab = $("#" + tab);
    var tab1 = tab.clone();
    tab1.attr("id", tab_id + "_1")
    tab1.find(".dn_").remove();
    tab1.find("tr").each(function () {
        var tr = $(this);
        if (tr.find("span.cbIsCheckField :checked").length > 0 || tr.find("th").length > 0) {

        } else {
            tr.remove();
        }
    });
 
    var i = 0;
    tab.find("th").each(function () {
        if ($(this).hasClass("gv_import2")) {
            tab1.find("tr").find(String.format("td:eq({0})", i)).remove();
            tab1.find("tr").find(String.format("th:eq({0})", i)).remove();
        }
        i++;
    });
    if (tab1.find("th,td").length > 20 * 15) {
        if (!confirm("导出数据较多时，对于部分性能较差的计算机有可能运行较慢，请耐心等待。是否继续该操作？"))
            return;
    }
    tab1.css({ visibility: "hidden" });
    
    $(document.body).append(tab1);
    saveAsExcel(tab1.attr("id"));
    tab1.remove();
};
function gv_import1(tab) {//导出已选择

    
    var tab_id = tab;
    tab = $("#" + tab);
    var tab1 = tab.clone();
    tab1.find(".dn_").remove();
    var i = 0;
    tab1.attr("id", tab_id + "_1")
    tab.find("th").each(function () {
        if ($(this).hasClass("gv_import2")) {
            tab1.find("tr").find(String.format("td:eq({0})", i)).remove();
            tab1.find("tr").find(String.format("th:eq({0})", i)).remove();
        }
        i++;
    });
    if (tab1.find("th,td").length > 20 * 15) {
        if (!confirm("导出数据较多时，对于部分性能较差的计算机有可能运行较慢，请耐心等待。是否继续该操作？"))
            return;
    }

    tab1.css({ visibility: "hidden" });

    
    $(document.body).append(tab1);
    saveAsExcel(tab1.attr("id"));
    tab1.remove();
};

 

//打开添加编辑iframe
function manage_open(url) {
    url = url + GetUrlParm(url);
    var iframe = $("#iframe_manage");
    var split_ct = $("div.split_ct");
    var split_below = split_ct.find("div.split_below");
    if (split_below && split_below.hasClass("dn_")) {
        var up = split_ct.find("div.split_line .up img");
        if (up) {
            up.click();
        }
    }

    iframe.attr("src", url);

}
//打开添加编辑iframe
function manage_open2(url) {
    url = url + GetUrlParm(url);
    var iframe = $("#iframe_manage");
    var split_ct = $("div.split_ct");
    var split_below = split_ct.find("div.split_below");
    if (split_below && split_below.hasClass("dn_")) {
        var up = split_ct.find("div.split_line .up img");
        if (up) {
            up.click();
            up.click();
        }
    }
    iframe.attr("src", url);
}
//取消管理面，一般在管理面点击取消事件引发
function manage_cancel() {
    if (window.parent) {
        var split_ct = $(window.parent.document).find("div.split_ct:eq(0)");
        var split_below = split_ct.find("div.split_below:eq(0)");
        if (split_below && !split_below.hasClass("dn_")) {
            var down = split_ct.find("div.split_line:eq(0) .down img");
            if (down) {
                down.click();
            }
        }
    }
    window.location = "/blank.aspx";
}
function manage_cancel2() {

    if (window.parent) {

        var split_ct = $(window.parent.document).find("div.split_ct:eq(0)");
        var split_below = split_ct.find("div.split_below:eq(0)");
        if (split_below && !split_below.hasClass("dn_")) {
            var down = split_ct.find("div.split_line:eq(0) .down img");
            if (down) {
                down.click();
                down.click();
            }
        }
    }
    window.location = "/blank.aspx";
}
function loading_open() {

    var loading = $("#loading");
    if (loading.length == 0) {
        loading = loading_create();
    }
    loading.trigger("open");
    //在指定时间调用关闭,防止网页出错了不调用关闭 
    setTimeout("loading_close()", 20 * 1000);
}
function loading_close() {
    var loading = $("#loading");
    loading.trigger("close");
}
function loading_create() {
    var divHtml = "<div class='drag dn_ loading' mark='' id='loading'><table><tr><td><span class='msg'>请稍候,网页正在加载中</span><span class='flag'>．．．</span></td></tr><tr><td><img src='/images/24.gif' class='loadimg' /></td></tr></table></div>";
    var div = $(divHtml);
    $("body").append(div);
    drag_setup(div[0]);
    return div;
}


var setup_auto_hw_inited = false;
function setup_auto_hw() {//自动宽高

    var isIE6_7 = ($.browser.version == "6.0" || $.browser.version == "7.0");
    var offset = isIE6_7 ? 16 : 16;
    $(".add_edit_body_ct").height($(window).height());
    $(".add_edit").width($(window).width() - offset);
    if (!setup_auto_hw_inited) {
        $(window).resize(function () {
            setup_auto_hw();

        });
        setup_auto_hw_inited = true;
    }
}

function table_check_validator() {

    if ($("table.ct_list_body :checked:not(.cbSel)").length == 0) {
        alert("请选择相应记录"); return false;
    }
    return true;
}
var setup_min_width_inited = false
var setup_min_width_ismin = true
var setup_min_width_open = true;
function setup_min_width() {
    if (setup_min_width_open) {
        var ismin = $(window).width() < 1000;
        if (ismin) {
            $("body").width(1000);
            $("body").css({ overflow: "auto" });
            $("html").css({ overflow: "auto" });
        }
        //    html { overflow: -moz-scrollbars-horizontal; overflow: hidden; }
        if (!setup_min_width_inited) {
            setup_min_width_inited = true;
            $(window).resize(function () {
                setup_min_width();
            });
        }
        setup_min_width_ismin = ismin;
        return ismin;
    }

}



//{
//  "HelpCode": 0,
//  "DisplayPage": null,
//  "Mode": 0,
//  "Icon": 4,
//  "Link": 8,
//  "TransferPage": "/login.aspx",
//  "EndResponse": true,
//  "Affixs": null,
//  "Title": null,
//  "Description": "帐号或密码错误",
//  "Original": null
//}
function PageWarn(obj) {
    var e = {
        "HelpCode": 0,
        "DisplayPage": null,
        "Mode": 1,
        "Icon": 4,
        "Link": 3, //Back=0x01,Close = 0x02, Exit = 0x03,Reflash = 0x04,ReflashList = 0x05,Login = 0x06,Default = 0x07,Transfer = 0x08,
        "TransferPage": null,
        "EndResponse": true,
        "Affixs": null,
        "Title": null,
        "Description": "新增新闻成功",
        "Original": null
    };
    if (obj.Link == 1) {//Back=0x01
        history.go(-1)
        //alert(obj.Description);
    } else if (obj.Link == 5) {//ReflashList = 0x05
        ReflashList();
        //alert(obj.Description);
    } else if (obj.Link == 4) {//Reflash = 0x04
        var url = window.location.toString();
        var url = url + GetUrlParm(url);
        window.location = url;
        //alert(obj.Description);
    } else if (obj.Link == 6) {//Login = 0x06
        if (obj.TransferPage.indexOf("?") > 0) {

            window.top.location = obj.TransferPage + "&from=" + UrlEncode(window.location.toString());
        }
        else {
            window.top.location = obj.TransferPage + "?from=" + UrlEncode(window.location.toString());
        }
    } else if (obj.Link == 8) {//Transfer = 0x08
        //window.top.location = obj.TransferPage;
        window.location = obj.TransferPage;
        //alert(obj.Description);
    } else if (obj.Link == 10) {//Top = 10
        window.top.location = obj.TransferPage;
        //alert(obj.Description);
    } 

    //http://localhost:2622/login.aspx?account=test&password=888888
    alert(obj.Description);
    if (obj.Link == 2) {//Close = 10
        self.close();
        window.close();
        //alert(obj.Description);
    }
}
function ReflashList() {
    var url = window.parent.window.location.toString();
    var url = url + GetUrlParm(url);
    var __btReflashList = $(window.parent.document).find(".__btReflashList");
    if (__btReflashList.length > 0)
        __btReflashList.click();
    else
        window.parent.window.location = url;
}


/* 获取时间擢*/
function GetUrlParm(url) {
    var t = new Date().getTime();
    var urlparm = "?";
    if (url.indexOf('?') >= 0) {
        urlparm = "&";
    }
    urlparm = urlparm + "__t=" + t;
    return urlparm;
}

function UrlEncode(str) {
    return encodeURIComponent(str);
    var i, c, ret = "", strSpecial = "!\"#$%&'()*+,/:;<=>?@[\]^`{|}~%";
    for (i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) >= 0x4e00) {
            c = qswhU2GB[str.charCodeAt(i) - 0x4e00];
            ret += "%" + c.slice(0, 2) + "%" + c.slice(-2);
        } else {
            c = str.charAt(i);
            if (c == " ")
                ret += "+";
            else if (strSpecial.indexOf(c) != -1)
                ret += "%" + str.charCodeAt(i).toString(16);
            else
                ret += c;
        }
    }
    return ret;
}
function UrlDecode(str) {
    return decodeURIComponent(str);
    var i, c, d, t, p, ret = "";
    function findPos(str) {
        for (var j = 0; j < qswhU2GB.length; j++) {
            if (qswhU2GB[j] == str) { return j; }
        }
        return -1;
    }
    for (i = 0; i < str.length; ) {
        c = str.charAt(i); i++;
        if (c != "%") {
            if (c == "+") {
                ret += " ";
            } else {
                ret += c;
            }
        } else {
            t = str.substring(i, i + 2); i += 2;
            if (("0x" + t) > 0xA0) {
                d = str.substring(i + 1, i + 3); i += 3;
                p = findPos(t + d);
                if (p != -1) {
                    ret += String.fromCharCode(p + 0x4e00);
                }
            } else {
                ret += String.fromCharCode("0x" + t);
            }
        }
    }
    return ret;
}

function go_back() {
    window.history.go(-1);
}
