﻿//替換字符串函數
function ReplaceAll(str, sptr, sptr1) {
    while (str.indexOf(sptr) >= 0) {
        str = str.replace(sptr, sptr1);
    }
    return str;
}

//秒轉小時分鐘格式
//格式：xxmxxh
function computeTime(second) {
    var h, m, s;
    if (second >= 3600) {
        //大于1小时
        h = parseInt(second / 3600);
        m = second - (h * 3600);
        s = second - (h * 3600) - (m * 60);
        if (m == 0 && s == 0) return h + "h";
        if (s == 0) return h + "h" + m + "m";
        return h + "h" + m + "m" + s + "s";
    }
    else {
        //小于1小时
        m = parseInt(second / 60);
        s = second - (m * 60);
        if (m == 0 && s == 0) return "0";
        if (s == 0) return m + "m";
        return m + "m" + s + "s";
    }
}

//秒轉小時分鐘格式
//格式：HH:mm:ss
function formatSecondToTime(second) {
    if (second < 60) {
        var h = 00;
        var m = 00;
        var s = second;
        return (h.toString().length == 1 ? "0" + h : h) + ":" + (m.toString().length == 1 ? "0" + m : m) + ":" + (s.toString().length == 1 ? "0" + s : s);
    }
    else {
        if (second < 3600) {
            var h = 00;
            var m = parseInt(second / 60);
            var s = parseInt(second - parseInt(m * 60));
            return (h.toString().length == 1 ? "0" + h : h) + ":" + (m.toString().length == 1 ? "0" + m : m) + ":" + (s.toString().length == 1 ? "0" + s : s);
        }
        else {
            var h = parseInt(second / 3600);
            var m = parseInt((second - parseInt(h * 3600)) / 60);
            var s = parseInt(second - parseInt(h * 3600) - parseInt(m * 60));
            return (h.toString().length == 1 ? "0" + h : h) + ":" + (m.toString().length == 1 ? "0" + m : m) + ":" + (s.toString().length == 1 ? "0" + s : s);
        }
    }
}
//===========================================
//转换成日期
//===========================================
String.prototype.toDate = function() {
    try {
        return new Date(this.replace(/-/g, "\/"));
    }
    catch (e) {
        return null;
    }
}
//===========================================
//Date(1251561600000+0800)格式转换成日期
//===========================================
function JsontoDate(v) {
    var rx = /\/Date\((-?[0-9]+)(\+[0-9]+)?\)\//g;
    if (rx.test(v)) {
        var milli = Number(v.replace(rx, '$1'));
        if (!isNaN(milli))
            v = new Date(Number(v.replace(rx, '$1')));
        else
            throw new Error("Date format not recognised.");
    }
    return v;
}
//===========================================
//格式化日期
//===========================================
Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1, //month   
        "d+": this.getDate(),    //day   
        "h+": this.getHours(),   //hour   
        "m+": this.getMinutes(), //minute   
        "s+": this.getSeconds(), //second   
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter   
        "S": this.getMilliseconds() //millisecond   
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
//===========================================
//格式化貨幣
//===========================================
function formatCurrency(s) {
    if (/[^0-9\.]/.test(s)) return "invalid value";
    s = s.toString().replace(/^(\d*)$/, "$1.");
    s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
    s = s.replace(".", ",");
    var re = /(\d)(\d{3},)/;
    while (re.test(s))
        s = s.replace(re, "$1,$2");
    s = s.replace(/,(\d\d)$/, ".$1");
    return s.replace(/^\./, "0.");
}


//===========================================
//限制只能輸入數字及小數 eg:onpropertychange="OnlyNum(this)"
//===========================================
function OnlyNum(obj) {
    if (obj.value != '' && !obj.value.match(/^[0-9\.]+$/)) {
        obj.value = obj.value.replace(/[^0-9\.]/g, '')
    }
}

//===========================================
//只能輸入數字(含負數)及小數
//===========================================
function negativeOrNum(obj) {
    if (obj.value != '' && !obj.value.match(/^[0-9\.\-]+$/)) {
        obj.value = obj.value.replace(/[^0-9\.\-]/g, '')
    }
}

function OnlyEn(obj) {
    if (obj.value != '' && obj.value.match(/^[A-Za-z]+$/)) {
        obj.value = obj.value.toUpperCase();
    }
    else {
        alert("請輸入字母");
        obj.value = "";
        obj.focus();
    }
}

//截取字符串
function cutString(str, size, repStr) {
    if (str.length > parseInt(size))
        return (str.substring(0, size) + repStr);
    else
        return str;
}
