﻿/*
设置当前的标签
使用方法：菜单的标签标题嵌套在<span>里面，span的id要用span#, 其中#是从0开始的数字
*/
//图片按比例缩放

var flag = false;
function DrawImage(ImgD, iwidth, iheight) {
    var image = new Image();
    //var iwidth = 60; //定义允许图片宽度，当宽度大于这个值时等比例缩小
    //var iheight = 60; //定义允许图片高度，当宽度大于这个值时等比例缩小
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        flag = true;
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.width = iwidth;
                ImgD.height = (image.height * iwidth) / image.width;
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
            //ImgD.alt = image.width + "×" + image.height;
        }
        else {
            if (image.height > iheight) {
                ImgD.height = iheight;
                ImgD.width = (image.width * iheight) / image.height;
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
            //ImgD.alt = image.width + "×" + image.height;
        }
    }
}
//调用：<img src="图片" onload="javascript:DrawImage(this)">
//-->



//显示当前时间
function CurrentTime(div) {

    var d = new Date();
    var vYear = d.getFullYear();
    var vMon = d.getMonth() + 1;
    var vDay = d.getDate();
    var vHour = d.getHours();
    var vMin = d.getMinutes();
    var vSec = d.getSeconds();

    var v = document.getElementById(div);

    if (v == null) {
        alert(div + ' is null');
        return;
    }
    var remainTime = vYear + "年" + vMon + "月" + vDay + "日" + vHour + "时" + vMin + "分" + vSec + "秒";
    v.innerHTML = remainTime;
    //v.setAttribute('innerHTML', remainTime);
}

//显示剩余时间，从网上抄下来的，做了很多改动，使之更加通用
//其中divRemainTime是一个div或者span
//调用方式
//<script>setInterval(RemainTime(year, month, date, hour, minute, second, document.getElementByID('divRemainTime')), 1000);
//<span id='Span1'></span>
function RemainTime(vYear, vMonth, vDate, vHour, vMinute, vSecond, div) {
    var divRemainTime = document.getElementById(div);
    if (divRemainTime == null) {
        alert(div + ' is null');
        return;
    }
    //设置过期日期
    var expireDate = new Date(vYear, vMonth - 1, vDate, vHour, vMinute, vSecond);
    //设置当前日期
    var thisTime = new Date();
    //总计剩余时间(毫秒)
    var remainTime = expireDate - thisTime;
    if (remainTime > 0) {
        var remainDay, remainHour, remainMinute, remainSecond;
        remainDay = Math.floor(remainTime / 1000 / 3600 / 24);
        remainTime = remainTime - (remainDay * 1000 * 3600 * 24);
        remainHour = Math.floor(remainTime / 1000 / 3600);
        remainTime = remainTime - (remainHour * 1000 * 3600);
        remainMinute = Math.floor(remainTime / 1000 / 60);
        remainTime = remainTime - (remainMinute * 1000 * 60);
        remainSecond = Math.floor(remainTime / 1000);
        remainTime = remainDay + "天" + remainHour + "小时" + remainMinute + "分" + remainSecond + "秒";
        divRemainTime.innerHTML = remainTime;
    }
    else {
        divRemainTime.innerHTML = '竞拍结束';
    }
}

// 检测CheckBox是否有选择，只可以单选
function CheckSingle() {
    var hasSelected = 0;
    var chkList = document.getElementsByName("chkSelect");

    for (i = 0; i < chkList.length; i++) {
        if (chkList[i].checked == true) {
            hasSelected += 1;
        }
    }

    if (hasSelected == 0) {
        alert("请选择！");
        return false;
    }
    else if (hasSelected == 1) {
        return true;
    }
    else {
        alert("只能选择一项！");
        return false;
    }
}

// 检测CheckBox是否有选择，可以多选择
function CheckMulti() {
    var hasSelected = 0;
    var chkList = document.getElementsByName("chkSelect");

    for (i = 0; i < chkList.length; i++) {
        if (chkList[i].checked == true) {
            hasSelected += 1;
        }
    }

    if (hasSelected == 0) {
        alert("请选择！");
        return false;
    }
    else {
        return true;
    }
}

// 检测修改时是否有选择，只可以单选
function CheckModify(url) {
    var selectedIndex = 0;
    var hasSelected = 0;
    var chkList = document.getElementsByName("chkSelect");

    for (i = 0; i < chkList.length; i++) {
        if (chkList[i].checked == true) {
            selectedIndex = i;
            hasSelected += 1;
        }
    }

    if (hasSelected == 0) {
        alert("请选择！");
    }
    else if (hasSelected == 1) {
        window.open(url + "?ID=" + chkList[selectedIndex].value, "_blank");
    }
    else {
        alert("只能选择一项！");
    }
}

// 检测删除时CheckBox是否有选择，可以多选择
function CheckDelete() {
    var hasSelected = 0;
    var chkList = document.getElementsByName("chkSelect");

    for (i = 0; i < chkList.length; i++) {
        if (chkList[i].checked == true) {
            hasSelected += 1;
        }
    }

    if (hasSelected == 0) {
        alert("请选择！");
        return false;
    }
    else {
        var bExec = window.confirm("你确认要删除所选项吗？");
        return bExec;
    }
}

function IsInputNum(Input) {
    if (trim(Input.value) != "") {
        if (!isNumberInt(Input.value) || Input.value <= 0 || Input.value.toString().indexOf(".")>0 ) {
            alert("价格必须是大于0的整数！");
            Input.value = "";
            Input.focus();
            return false;
        }

    }

}


// 是否是整数
function isNumberInt(inputString) {
    return (!isNaN(parseInt(inputString))) ? true : false;
}

// 是否是数字
function isNumber(inputString) {
    return !isNaN(inputString);
}

// 是否是日期
function isDate(inputString) {
    var reDate = /\d{4}-(\d{2}|\d{1})-(\d{2}|\d{1})/;
    return reDate.test(inputString);
}

// 是否是邮件地址
function isMail(inputString) {
    var mail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    return mail.test(inputString);
}

// 截去两边空格
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

// 截去左边空格
function LTrim(stringToTrim) {
    return stringToTrim.replace(/(^\s*)/g, "");
}

// 截去右边空格
function RTrim(stringToTrim) {
    return stringToTrim.replace(/(\s*$)/g, "");
}
//判断是否是数字或字母
function isNumberOrLetter(s) {
    var regu = "^[0-9a-zA-Z_]+$";
    var re = new RegExp(regu);
    if (re.test(s)) {
        return true;
    }
    else {
        return false;
    }
}

function CheckTimeFormat(TextInput) {
    re = /[1-9][0-9]{3}-(0?[1-9]|1[0|1|2])-(0?[1-9]|[1|2][0-9]|3[0|1])\s(0?[1-9]|1[0-9]|2[0-3]):(0?[0-9]|[1|2|3|4|5][0-9]):(0?[0-9]|[1|2|3|4|5][0-9])$/;
    if (trim(TextInput.value) != '') {
        if (!re.test(TextInput.value)) {
            alert('日期应按照 (年-月-日 时:分:秒) 格式填写\n yyyy-MM-DD hh:mm:ss 比如: 2099-01-08 13:30:00 ');

            TextInput.select();
            TextInput.focus();
            return false;
        }
    }
}
    
