



/**
 * ajax 加载数据
 * @param url 地址
 * @param data post数据
 * @param calBackFunction 回掉函数
 */
function requestData(url, data, calBackFunction) {
    var fdStart = url.indexOf("/");
    if (fdStart == 0)
        url = "http://" + window.location.host + url;
    else
        url = "http://" + window.location.host + "/" + url;
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "JSON",
        timeout: 30000,
        async: true,
        beforeSend: function () {
            //$("body").append(loadDialog);
        },
        success: function (json) {
            if (json != null && json.length == 2 && json[0] == "status=1") {
                LayerLoadClose();
                window.location.href = "/index.aspx";
                return;
            } else if (json != null && json.length == 2 && json[0] == "status=2") {
                LayerLoadClose();
                LayerMsg(json[1]);
                return;
            }
            calBackFunction(json);
        },
        error: function (xhr, textStatus, errorThrown) {
            console.log("进入error---" + url);
            console.log("状态码：" + xhr.status);
            console.log("状态:" + xhr.readyState) //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
            console.log("错误信息:" + xhr.statusText);
            console.log("返回响应信息：" + xhr.responseText);//这里是详细的信息
            console.log("请求状态：" + textStatus);
            console.log(errorThrown);
            console.log("请求失败");
            //LayerMsg('系统错误');
        }
    });
}


function requestCheckAuth(permitOid) {
    let url = "http://" + window.location.host + "/Tools/AuthPermit.ashx?CheckAuth=" + permitOid;
    let CheckAuth = false;
    $.ajax({
        type: "GET",
        url: url,
        dataType: "JSON",
        timeout: 30000,
        async: false,
        success: function (json) {
            if (json != null && json.length == 2 && json[0] == "status=0") {
                CheckAuth = true;
                return true;
            } else {
                return false;
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            return false;
        }
    });
    return CheckAuth;
}

/**
 * 日期字符串转换为日期类型
 * @param 日期时间（格式yyyy-MM-dd）
 */
function GetDateFromString(str) {
    var strs = str.split('-');
    var dt = new Date();
    dt.setFullYear(strs[0]);
    dt.setMonth(parseInt(strs[1]) - 1);
    dt.setDate(strs[2]);
    return dt;
}

/**
 * 比较两个日期时间（格式yyyy-MM-dd）
 * @param time1 时间1
 * @param time2 时间2
 * @returns time1大于time2返回true,否则返回false
 */
function compareTime(time1, time2) {
    console.log(time1, time2);
    var arr1 = time1.split("-");
    var arr2 = time2.split("-");
    var date1 = new Date(parseInt(arr1[0]), parseInt(arr1[1]) - 1, parseInt(arr1[2]), 0, 0, 0);
    var date2 = new Date(parseInt(arr2[0]), parseInt(arr2[1]) - 1, parseInt(arr2[2]), 0, 0, 0);
    if (date1.getTime() > date2.getTime()) {
        return true;
    } else {
        return false;
    }
}

// 校验电话格式
function checkPhone(str) {
    var re = /^0\d{2,3}-?\d{7,8}$/;
    if (re.test(str)) {
        return true;
    } else {
        return false;
    }
}

// 校验邮箱
function checkEmail(str) {
    //var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
    var re = /^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/;
    if (re.test(str)) {
        return true;
    } else {
        return false;
    }
}

// 校验邮编
function checkPostalCode(str) {
    var re = /^[1-9][0-9]{5}$/;
    if (re.test(str)) {
        return true;
    } else {
        return false;
    }
}
// 去除多余空格 可以更换第二个参数去替换别的
function Trim(str, is_global) {
    var result;
    result = str.replace(/(^\s+)|(\s+$)/g, "");
    if (is_global.toLowerCase() == "g") {
        result = result.replace(/\s/g, "");
    }
    return result;
}

function chooseCompany() {
    window.open("../Common/SelectCallBack.aspx?cat=728&field=ParentComoanyName", 'new', 'left=200,top=200,width=600,height=800', false);
    //window.open(url, "newwindow", "height=200,width=400", "toolbar =no", "menubar=no", "scrollbars=no", "resizable=no", "location=no", "status=no");
    //这些要写在一行
}
// 检查日期是否正确。正确返回true
function check(date) {
    return (new Date(date).getDate() == date.substring(date.length - 2));
}
// 保留两位小数
function toDecimal2(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return "";
    }
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}
function toDecimal4(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return "";
    }
    var f = Math.round(x * 10000) / 10000;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 4) {
        s += '0';
    }
    return s;
}

document.write("<script src='../Scripts/layer/layer.js' type='text/javascript'></script>");
// 加载弹层
function LayerLoad() {
    layer.msg("加载中", {
        icon: 16
        , shade: 0.6
        , time: 0
    });
}
// 关闭加载弹层
function LayerLoadClose() {
    layer.closeAll('dialog');
}
// 消息框
function LayerMsg(msg) {
    var index = layer.msg(msg, {
        time: 4000
    });
    layer.style(index, {
        width: 'auto',
        height: 'auto'
    });
}



// 弹层一个按钮
function LayerAlert(msg, btn, btnFunc) {
    if (btn == undefined || btn == null)
        btn = "确定";
    layer.confirm(msg, {
        btn: [btn]
    },
        function (index) {
            if (btnFunc != null) {
                btnFunc();
            }
            layer.close(index);
        });
}

// 弹层一个按钮
function LayerAlertEx(msg, t, btn, btnFunc) {
    layer.confirm(msg, {
        title: t,
        btn: [btn]
    },
        function (index) {
            btnFunc();
            layer.close(index);
        });
}
// 弹层两个按钮
function LayerConfirm(msg, btn1, btn2, btn1Func, btn2Func) {
    layer.confirm(msg, {
        btn: [btn1, btn2]
    },
        function (index) {
            btn1Func();
            layer.close(index);
        },
        function (index) {
            if ($.isFunction(btn2Func))
                btn2Func();
            layer.close(index);
        });
}
// 弹层两个按钮，一个关闭
function LayerConfirmOk(msg, btn1, btn2, btn1Func) {
    layer.confirm(msg, {
        btn: [btn1, btn2]
    },
        function (index) {
            btn1Func();
            layer.close(index);
        },
        function (index) {
            layer.close(index);
        });
}


// 发送邮件 -- eamil代表接收人的地址
function Email(email) {
    location.href = "mailto:" + email + "";
}
// 返回个带0的整数
function returnNumber(param) {
    if (param < 10) {
        return "0" + param
    }
    return param;
}
function DateDiff(sDate1, sDate2) {    //sDate1和sDate2是2002-12-18格式  
    var aDate, oDate1, oDate2, iDays
    aDate = sDate1.split("-")
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])    //转换为12-18-2002格式  
    aDate = sDate2.split("-")
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)    //把相差的毫秒数转换为天数  
    return iDays
}
function MonthDiff(date1, date2) {
    // 拆分年月日
    date1 = date1.split('-');
    // 得到月数
    totalMonth1 = parseInt(date1[0]) * 12 + parseInt(date1[1]);
    // 拆分年月日
    date2 = date2.split('-');
    // 得到月数
    totalMonth2 = parseInt(date2[0]) * 12 + parseInt(date2[1]);
    var m = Math.abs(totalMonth1 - totalMonth2);
    if (parseInt(date2[2]) >= parseInt(date1[2])) {
        m = m + 1;
    }
    return m;
}

$.fn.populateForm = function (data) {
    return this.each(function () {
        var formElem, name;
        if (data == null) { this.reset(); return; }
        for (var i = 0; i < this.length; i++) {
            formElem = this.elements[i];
            //checkbox的name可能是name[]数组形式
            name = (formElem.type == "checkbox") ? formElem.name.replace(/(.+)\[\]$/, "$1") : formElem.name;
            if (data[name] == undefined) continue;
            switch (formElem.type) {
                case "checkbox":
                    if (data[name] == "") {
                        formElem.checked = false;
                    } else {
                        //数组查找元素
                        if (data[name] == formElem.value) {
                            formElem.checked = true;
                        } else {
                            formElem.checked = false;
                        }
                    }
                    break;
                case "radio":
                    if (data[name] == "") {
                        formElem.checked = false;
                    } else if (formElem.value == data[name]) {
                        formElem.checked = true;
                    }
                    break;
                case "button": break;
                case "select": formElem.value = data[name].value; break;
                default: formElem.value = data[name];
            }
        }
    });
};


var windowObj = {
    company: 'company',
    contact: 'contact',
    saleOrder: 'saleOrder',
    configurationItem: 'configurationItem',
    subscription: 'subscription',
    quote: 'quote',
    quoteItem: 'quoteItem',
    contract: 'contract',
    contractService: 'contractService',
    contractCost: "contractCost",
    contractCharge: 'contractCharge',
    contractNotRule: 'contractNotRule',
    notes: 'notes',
    todos: 'todos',
    attachment: 'attachment',
    holiday: 'holiday',
    holidaySet: 'holidaySet',
    invoice: 'invoice',
    inventoryLocation: 'inventoryLocation',
    inventoryOrder: 'inventoryOrder',
    inventoryItem: 'inventoryItem',
    inventoryItemSerailNum: 'inventoryItemSerailNum',
    inventoryOrderItem: 'inventoryOrderItem',
    inventoryOrderPaymentPlan: 'inventoryOrderPaymentPlan',
    project: 'project',
    projectReport: 'projectReport',
    projectTeam: 'projectTeam',
    projectCalendar: 'projectCalendar',
    projectAttach: 'projectAttach',
    projectUdf: 'projectUdf',
    quoteEmailTmpl: 'quoteEmailTmpl',
    expense: "expense",
    expenseReport: "expenseReport",
    workEntry: "workEntry",
    task: "task",
    ticket: "ticket",
    service: "service",
    serviceBundle: "serviceBundle",
    workflow: "workflow",
    timeoffPolicy: "timeoffPolicy",
    masterTicket: 'masterTicket',
    serviceCall: "serviceCall",
    timeoffRequest: 'timeoffRequest',
    kbArticle: "kbArticle",
    select: "select",
    appointment: "appointment",
    resource: "resource",
    resourcePolicy: "resourcePolicy",
    contactGroup: "contactGroup",
    opportunity: "opportunity",
    contactActionTemp: "contactActionTemp",
    formTemp: "formTemp",
    general: "general",
    generalCate: "generalCate",
    resourceWorkGroup: "resourceWorkGroup",
    resourceApproval: "resourceApproval",
    queue: "queue",
    logo: "logo",
    board: "board",
    CheckListLibrary: "CheckListLibrary",
    shareDashboard: "shareDashboard",
    costCode: "costCode",
    Region: "Region",
    sla: "sla",
    slaItem: "slaItem",
    saleQuota: "saleQuota",
    dataImport: "dataImport",
    addToOtherResWorkList: "addToOtherResWorkList",
    InvoiceApply: "InvoiceApply",
    InvoiceApplyView: "InvoiceApplyView",
    InvoiceApplyActualInvoic: "InvoiceApplyActualInvoic",
    NoticeTempl: "NoticeTempl",
    ServiceSerialized: "ServiceSerialized",
}
var windowType = {
    blank: '_blank',
    add: 'add',
    edit: 'edit',
    view: 'view',
    manage: 'manage',
}

//正则获取url中的参数
function URL_Request(strName) {
    debugger;
    var strHref = document.location.toString();
    var intPos = strHref.indexOf("?");
    var strRight = strHref.substr(intPos + 1); //==========获取到右边的参数部分
    var arrTmp = strRight.split("&"); //=============以&分割成数组

    for (var i = 0; i < arrTmp.length; i++) //===========循环数组
    {
        var dIntPos = arrTmp[i].indexOf("=");
        var paraName = arrTmp[i].substr(0, dIntPos);
        var paraData = arrTmp[i].substr(dIntPos + 1);
        if (paraName.toUpperCase() == strName.toUpperCase()) {
            return paraData;
        }
    }
    return "";
}



var permitAuhtOid = {
    工时1173: 1173,
    工时1172: 1172,
    工时1107: 1107,
    工时1144: 1144,
    休假1148: 1148,
    客户24: 24,
    待办219: 219,
    待办223: 223,
    待办225: 225,
    备注212: 212,
    备注213: 213,
    备注214: 214,
    商机125: 125,
    UDF832: 832,
    知识库1734: 1734,
    知识库1737: 1737,
    知识库1738: 1738,
    知识库1739: 1739,
    配送1026: 1026,
    配送1031: 1031,
    配送1027: 1027,
    配送1028: 1028,
    仪表板1923:1923,
    仪表板1933: 1933,
    仪表板1934: 1934,
    仪表板1935: 1935,
    仪表板1936: 1936,
    仪表板1937: 1937,
    仪表板1938: 1938,
    配置项导入3621: 3621,
    附件2873: 2873,
    附件2876: 2876,
    附件2879: 2879,
    采购订单1015: 1015,
    项目827: 827,
    工单成本和费用1615: 1615,
    工单成本和费用1616: 1616,
    工单成本和费用809: 809,
    工单成本和费用810: 810,
    销售订单187: 187,
    合同产品646: 646,
    项目产品和费用808: 808,
    合同产品645: 645,
    费用报表1136: 1136,
    合同产品651: 651,
    费用报表1137:1137
}
