/**
 * 使用方法与后台的StringFormat方法一样，StringFormat("abc{0}def","123");输出结果为"abc123def"。
 * @returns {*}
 * @constructor
 */
function StringFormat() {
    if (arguments.length == 0)
        return null;
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}

/**
 * 将字符串数组转化为字符串
 * @param arr
 * @returns {null}
 */
function arrToString(arr) {
    if (arr == null || arr.length == 0) {
        return null;
    }
    var result = null;
    for (var i = 0; i < arr.length; i++) {
        if (result == null) {
            result = arr[i];
        } else {
            result += "," + arr[i];
        }
    }
    return result;
}

/**
 * 获取返回的html中的js，并执行
 * @param html
 */
function executeScript(html) {
    //对整段HTML片段按<\/script>拆分
    var htmlBlock = html.split("<\/script>");
    for (var i in htmlBlock) {
        var reg = htmlBlock[i].match(/<script .*?src=\"(.+?)\"/);
        if (reg != null) {
            try {
                $.getScript(reg[1]);
            }
            catch (e) {
            }

        }
    }
}

/**
 * 判断内容是否是中文
 * @param val
 * @returns {boolean}
 */
function isChinese(val) {
    var reg = new RegExp("[\\u4E00-\\u9FA5]+", "g");
    if (reg.test(val)) {
        return true;
    }
    return false;
}


/*检测次数 --》自动添加*/
var needToAddJccs = "";
/*阿拉伯数字  转  汉字   */
var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
var chnUnitChar = ["", "十", "百", "千"];

// 节内转换算法
function SectionToChinese(section) {
    var strIns = '', chnStr = '';
    var unitPos = 0;
    var zero = true;
    while (section > 0) {
        var v = section % 10;
        if (v === 0) {
            if (!zero) {
                zero = true;
                chnStr = chnNumChar[v] + chnStr;
            }
        } else {
            zero = false;
            strIns = chnNumChar[v];
            strIns += chnUnitChar[unitPos];
            chnStr = strIns + chnStr;
        }
        unitPos++;
        section = Math.floor(section / 10);
    }
    return chnStr;
}

// 转换算法主函数
function numToChineseNum(num) {
    var unitPos = 0;
    var strIns = '', chnStr = '';
    var needZero = false;

    if (num === 0) {
        return chnNumChar[0];
    }
    while (num > 0) {
        var section = num % 10000;
        if (needZero) {
            chnStr = chnNumChar[0] + chnStr;
        }
        strIns = SectionToChinese(section);
        strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
        chnStr = strIns + chnStr;
        needZero = (section < 1000) && (section > 0);
        num = Math.floor(num / 10000);
        unitPos++;
    }
    if (chnStr == "一十一" || chnStr == "一十二" || chnStr == "一十三" || chnStr == "一十四" || chnStr == "一十五" || chnStr == "一十六" || chnStr == "一十七" || chnStr == "一十八" || chnStr == "一十九") {
        chnStr = chnStr.substring(1, 3);
    }
    if (chnStr == "一十") {
        chnStr = "十";
    }
    // if(chnStr!=null && chnStr!=''){
    //     chnStr="第"+chnStr+"次";
    // }
    return chnStr;
}

/**
 *  获取唯一值
 * @returns {string}
 */
function getUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}