
$.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(decodeURI(r[2].replace(/\+/g, "%20"))); return "";
};


$.setQueryForm = function () {
    $("form.form-query").find("input,select").each(function () {
        var v = unescape(decodeURI($.getUrlParam($(this).attr("name"))));
        if (v != '')
            $(this).val(v);
    });
};



function startWith(str, s) {
    return str.indexOf(s) == 0;
}

function waitTime(event) {
    //$(this).prop("disabled", true);
    if ($(this).hasClass("disabled")) return;
    $(this).addClass("disabled");
    var snd = event.data.snd;
    var str = event.data.str;
    if (event.data.fn)
        event.data.fn();
    var $obj = $(this);
    var oldStr = $obj.html();
    $obj.html(snd + str);
    var uptime = function () {
        snd = snd - 1;
        if (snd <= 0) {
            clearInterval(t);
            //$obj.prop("disabled", false);
            $obj.removeClass("disabled");
            $obj.html(oldStr);
        } else {
            $obj.html(snd + str);
        }
    }
    var t = setInterval(uptime, 1 * 1000);
}



function getInt(val) {
    if (/^[+-]?\d+$/.test(val))
        return parseInt(val);
    else
        return 0;
}

function getDouble(val) {
    if (/^[+-]?\d+(\.\d+)?$/.test(val))
        return parseFloat(val);
    else
        return 0;
}

function toFixed2(num) {
    return parseFloat(+num.toFixed(2));
}

function getHHmmss(snds) {
    if (snds <= 0)
        return "00:00:00";
    var h = snds / (60 * 60);
    h = parseInt(h);
    var m = (snds - (h * 60 * 60)) / 60;
    m = parseInt(m);
    var s = snds - (h * 60 * 60) - (m * 60);
    s = parseInt(s);
    var mm = m > 10 ? "" + m : "0" + m;
    var ss = s > 10 ? "" + s : "0" + s;
    return h + ":" + mm + ":" + ss;
}

function getTimeFormat(snd, fmt) {
    if (snd <= 0)
        return "00:00:00";
    if (!fmt)
        fmt = 'hh:mm:ss';
    var date = new Date(snd * 1000);
    var o = {
        "M+": date.getUTCMonth() + 1, //月份 
        "d+": date.getUTCDate(), //日 
        "h+": date.getUTCHours(), //小时 
        "m+": date.getUTCMinutes(), //分 
        "s+": date.getUTCSeconds(), //秒 
        "q+": Math.floor((date.getUTCMonth() + 3) / 3), //季度 
        "S": date.getUTCMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getUTCFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function setFormVal(name, val, form) {
    var e;
    if (form)
        e = form.find('[name="' + name + '"]');
    else
        e = $('[name="' + name + '"]');
    if (e.length > 1) {
        e.each(function () {
            if ($(this).val() == val)
                $(this).prop('checked', true);
        });
    } else if (e.prop('type') == 'radio' || e.prop('type') == 'checkbox') {
        if ($(this).val() == val)
            $(this).prop('checked', true);
    } else
        return e.val(val);
}
function getFormVal(name, form) {
    var e;
    if (form)
        e = form.find('[name="' + name + '"]');
    else
        e = $('[name="' + name + '"]');
    if (e.length > 1) {
        var val = '';
        e.each(function () {
            if ($(this).prop('checked')) val = $(this).val();
        });
        return val;
    } else if (e.prop('type') == 'radio' || e.prop('type') == 'checkbox') {
        if (e.prop('checked'))
            return e.val();
        else
            return '';
    } else
        return e.val();
}

function getFormDataVal(name, dataName, form) {
    var e;
    if (form) {
        e = form.find('[name="' + name + '"]');
    }
    else
        e = $('[name="' + name + '"]');
    if (e.length > 1) {
        var val = '';
        e.each(function () {
            if ($(this).prop('checked')) val = $(this).data(dataName);
        });
        return val;
    } else if (e.prop('type') == 'radio' || e.prop('type') == 'checkbox') {
        if (e.prop('checked'))
            return e.data(dataName);
        else
            return '';
    } else
        return e.data(dataName);
}


function getFormInt(name, form) {
    return getInt(getFormVal(name, form));
}
function getFormDouble(name, form) {
    return getDouble(getFormVal(name, form));
}

var isMobile = function (mobile) {
    var pattern = /^1\d{10}$/;
    return pattern.test(mobile);
}
var isQQ = function (qq) {
    var pattern = /^[1-9][0-9]{4,9}$/;
    return pattern.test(qq);
}

function selectAll(obj) {
    $("input[name=chkId]").each(function () {
        if ($(obj).prop("checked"))
            $(this).prop("checked", true);
        else
            $(this).prop("checked", false);
    });
    $("input[name=chkAll]").each(function () {
        if ($(obj).prop("checked"))
            $(this).prop("checked", true);
        else
            $(this).prop("checked", false);
    });
}

function selectValue(selector, name) {
    $(selector).on('click', function () {
        $('[name=' + name + ']').val($(this).html().replace(/&lt;/ig, '<').replace(/&gt;/ig, '>')).trigger('input');
    });
}



function getTimeFormat(snd, fmt) {
    if (snd <= 0)
        return "00:00:00";
    if (!fmt)
        fmt = 'hh:mm:ss';
    var date = new Date(snd * 1000);
    var o = {
        "M+": date.getUTCMonth() + 1, //月份 
        "d+": date.getUTCDate(), //日 
        "h+": date.getUTCHours(), //小时 
        "m+": date.getUTCMinutes(), //分 
        "s+": date.getUTCSeconds(), //秒 
        "q+": Math.floor((date.getUTCMonth() + 3) / 3), //季度 
        "S": date.getUTCMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getUTCFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


function SetRemainTime(SysSecond) {
    if (SysSecond > 0) {
        SysSecond = SysSecond - 1;
        var second = Math.floor(SysSecond % 60);             // 计算秒     
        var minite = Math.floor((SysSecond / 60) % 60);      //计算分 
        var hour = Math.floor((SysSecond / 3600) % 24);      //计算小时 
        var day = Math.floor((SysSecond / 3600) / 24);        //计算天
        if (day > 0) {
            return day + "天" + hour + "小时" + minite + "分" + second + "秒";
        }
        else {
            return hour + "小时" + minite + "分" + second + "秒";
        }
    } else {//剩余时间小于或等于0的时候，就停止间隔函数
        return "00:00:00";
        //这里可以添加倒计时时间为0后需要执行的事件 
    }
} 

function toStringFormat(str, len, dem) {
    var s = "";
    var reg = new RegExp(dem, 'g');
    str = str.replace(reg, "");
    str = str.split(dem).join("");
    for (var i = 1; i <= str.length; i++) {
        s += str.charAt(i - 1);
        if (i % len == 0)
            s += dem;
    }
    return s.trim();
}

String.prototype.render = function (obj) {
    var str = this, reg;

    Object.keys(obj).forEach(function (v) {
        reg = new RegExp('\\!\\{' + v + '\\}', 'g');
        str = str.replace(reg, obj[v]);
    });

    return str;
};


function isNumber(val) {

    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }

}

function isIntNum(val) {
    var regPos = / ^\d+$/; // 非负整数
    var regNeg = /^\-[1-9][0-9]*$/; // 负整数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}


function goto(url) {
    window.location.href = url;
}


$(function () {
    $.setQueryForm();

})





