﻿
/*检测用户是否选中了要删除的项*/
function Check() {
    var ids = document.getElementsByTagName("input");

    for (i = 0; i < ids.length; i++) {
        if (ids[i].type == "checkbox" && ids[i].checked == true) {
            return confirm("您确定要删除吗？");
        }
    }

    alert("您还没有选中您要删除的项！请先选中再删除！");
    return false;

}
/*把当前页面上面所有CheckBox都选中  实现全选*/
function ChangeCheck(obj) {
    var f = document.forms[0].elements;
    for (var i = 0; i < f.length; i++) {
        if (f[i].type.toLowerCase() == "checkbox") {
            f[i].checked = obj.checked;
        }
    }
}

function OpenWin(url,w,h,t,hand) {
    ymPrompt.win({ message: url, width: w, height: h, title: t, handler: hand, iframe: true })
}
/*判断是不是在效的Email地址*/
function isEmail(str) {
    var result = str.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
    if (result == null) return false;
    return true;
}


/*判断上传的文件是不是图片文件*/
String.prototype.IsPicture = function (obj) {
    var strFilter = ".jpeg|.gif|.jpg|.png|.bmp|.pic|"
    if (this.indexOf(".") > -1) {
        var p = this.lastIndexOf(".");
        var strPostfix = this.substring(p, this.length) + '|';
        strPostfix = strPostfix.toLowerCase();
        if (strFilter.indexOf(strPostfix) > -1) {
            return true;
        }
    }
    obj.outerHTML += '';
    return false;
}

//去左右空格; 
function Trim(s) {
    return s.replace(/(^\s*)|(\s*$)/g, "");
}
//判断输入的是否为整数
function isint(str) {
    var result = str.match("^[1-9]\\d*|0$");
    if (result == null) return false;
    return true;

}
//比较选择的两时间  开始时间应该小于结束时间
function Compare(fromtime, totime) {
    var ftime = fromtime.split(" ");
    var start0 = ftime[0].split("-");
    var ttime = totime.split(" ");
    var end0 = ttime[0].split("-");
    if (start0[0] * 1 > end0[0] * 1) {
        alert('起始时间不能大于结束时间！'); return false;
    }
    if (start0[0] * 1 == end0[0] * 1 && (start0[1] * 1 > end0[1] * 1)) {
        alert('起始时间不能大于结束时间！'); return false;
    }
    if (start0[0] * 1 == end0[0] * 1 && (start0[1] * 1 == end0[1] * 1) && (start0[2] * 1 > end0[2] * 1)) {
        alert('起始时间不能大于结束时间！'); return false;
    }

}


function checkAllDelete(obj, toolName) {
    var f = document.forms[0].elements;
    var str = "";
    for (var i = 0; i < f.length; i++) {
        if (f[i].type.toLowerCase() == "checkbox") {
            if (f[i].id.indexOf("chkItemCheck") != "-1") {
                if (f[i].checked) {
                    str = "have";
                }
            }
        }
    }
    if (str == "") {
        alert("请选择您要" + toolName + "的项");
        return false;
    }
    else {
        if (confirm("您确认批量" + toolName + "吗?")) {
            return true;
        }
        else {
            return false;
        }
    }
}
var highlightcolor = '#ebf1f8';
var clickcolor = '#22BE31';
function changeto(evt) {
    evt = evt ? evt : (window.event ? window.event : null);
    source = evt.srcElement ? evt.srcElement : evt.target;
    if (source.tagName == "TR" || source.tagName == "TABLE")
        return;
    while (source.tagName != "TD")
        source = source.parentNode;
    source = source.parentNode;
    cs = source.children;
    if (typeof (cs) == 'undefined') {
        return;
    }
    //alert(cs.length);
    if (cs[1].style.backgroundColor != highlightcolor && source.id != "nc" && cs[1].style.backgroundColor != clickcolor)
        for (i = 0; i < cs.length; i++) {
            cs[i].style.backgroundColor = highlightcolor;
        }
}

function changeback(evt) {
    evt = evt ? evt : (window.event ? window.event : null);
    var from = evt.fromElement ? evt.fromElement : evt.currentTarget;
    var to = evt.toElement ? evt.toElement : evt.currentTarget;
    if (source.id == "nc")
        return
    if (typeof (cs) == 'undefined' || typeof (cs[1]) == 'undefined')
        return;

    if (to != source && cs[1].style.backgroundColor != clickcolor)
    //source.style.backgroundColor=originalcolor

        for (i = 0; i < cs.length; i++) {
            cs[i].style.backgroundColor = "";
        }
}

// 是否为空，非空返回真，不非为空返回假
function isBlank(str) {
    var blankFlag = true;
    if (str.length == 0) return true;
    for (var i = 0; i < str.length; i++) {
        if ((str.charAt(i) != "") && (str.charAt(i) != " ")) {
            blankFlag = false;
            break;
        }
    }
    return blankFlag;
}

function checkNotNull(theField, fieldName) {

    if (isBlank(theField.value)) {
        alert(fieldName + "不可为空！");
        theField.focus();
        return false;
    }

    return true;
}

//颜色
function GetColor(img_val, input_val) {
    var PaletteLeft, PaletteTop
    var obj = document.getElementById("colorPalette");
    ColorImg = img_val;
    ColorValue = document.getElementById(input_val);
    if (obj) {
        PaletteLeft = getOffsetLeft(ColorImg)
        PaletteTop = (getOffsetTop(ColorImg) + ColorImg.offsetHeight)
        if (PaletteLeft + 150 > parseInt(document.body.clientWidth)) PaletteLeft = parseInt(event.clientX) - 260;
        if (PaletteTop > parseInt(document.body.clientHeight)) PaletteTop = parseInt(document.body.clientHeight) - 165;
        obj.style.left = PaletteLeft + "px";
        obj.style.top = PaletteTop + "px";
        if (obj.style.visibility == "hidden") {
            obj.style.visibility = "visible";
        } else {
            obj.style.visibility = "hidden";
        }
    }
}
function setColor(color) {
    if (ColorImg.id == 'FontColorShow' && color == "#") color = '#000000';
    if (ColorImg.id == 'FontBgColorShow' && color == "#") color = '#FFFFFF';
    if (ColorValue) { ColorValue.value = color.substr(1); }
    if (ColorImg && color.length > 1) {
        ColorImg.src = '../Images/selectcolor.gif';
        ColorImg.style.backgroundColor = color;
    } else if (color == '#') { ColorImg.src = '../Images/nocolor.gif'; }
    document.getElementById("colorPalette").style.visibility = "hidden";
}
function getOffsetTop(elm) {
    var mOffsetTop = elm.offsetTop;
    var mOffsetParent = elm.offsetParent;
    while (mOffsetParent) {
        mOffsetTop += mOffsetParent.offsetTop;
        mOffsetParent = mOffsetParent.offsetParent;
    }
    return mOffsetTop;
}
function getOffsetLeft(elm) {
    var mOffsetLeft = elm.offsetLeft;
    var mOffsetParent = elm.offsetParent;
    while (mOffsetParent) {
        mOffsetLeft += mOffsetParent.offsetLeft;
        mOffsetParent = mOffsetParent.offsetParent;
    }
    return mOffsetLeft;
}

// 是否为数字
function checkNumber(theField, fieldName) {
    //var pattern = /^([0-9]|(-[0-9]))[0-9]*((\.[0-9]+)|([0-9]*))$/;
    if (theField.value == "") return true;
    if (isNaN(theField.value)) {
        alert(fieldName + "必须为合法数字");
        theField.focus();
        theField.select();
        return false;
    }

    return true;
}

// 是否为指定范围数字
function checkNumberRange(theField, fieldName, min, max) {
    if (theField.value == "") return true;
    if (!checkNumber(theField, fieldName)) return false;

    if ((min != "") && (theField.value < min)) {
        alert(fieldName + "不可小于" + min + "！");
        theField.focus();
        theField.select();
        return false;
    }

    if ((max != "") && (theField.value > max)) {
        alert(fieldName + "不可超过" + max + "！");
        theField.focus();
        theField.select();
        return false;
    }

    return true;
}

// 是否为整数
function checkInteger(theField, fieldName) {
    var pattern = /^(\d|(-\d))\d*$/;

    if (theField.value == "") return true;
    if (!pattern.test(theField.value)) {
        alert(fieldName + "必须为整数！");
        theField.focus();
        theField.select();
        return false;
    }

    return true;
}

// 是否为指定范围内整数
function checkIntegerRange(theField, fieldName, min, max) {
    if (theField.value == "") return true;
    if (!checkInteger(theField, fieldName)) return false;

    if ((min != "") && (theField.value < min)) {
        alert(fieldName + "不可小于" + min + "！");
        theField.focus();
        theField.select();
        return false;
    }

    if ((max != "") && (theField.value > max)) {
        alert(fieldName + "不可超过" + max + "！");
        theField.focus();
        theField.select();
        return false;
    }

    return true;
}

// 是否为正数
function checkPositiveNumber(theField, fieldName) {
    if (theField.value == "") return true;
    if (theField.value.charAt(0) == '-') {
        alert(fieldName + "必须为正数！");
        theField.focus();
        return false;
    }

    return true;
}

// 限制字串最大长度
function checkLength(theField, fieldName, maxLength) {
    if (theField.value == "") return true;
    if (theField.value.length > maxLength) {
        alert(fieldName + "的字数最多为" + maxLength + "字！");
        theField.select();
        theField.focus();
        return false;
    }

    return true;
}

// 限制字串长度,注意参数顺序
function checkLength2(theField, fieldName, maxLength, minLength) {
    if (theField.value == "") return true;
    if (theField.value.length > maxLength) {
        alert(fieldName + "的字数最多为" + maxLength + "字！");
        theField.focus();
        return false;
    }

    if ((minLength != "") && (theField.value.length < minLength)) {
        alert(fieldName + "的字数最少为" + minLength + "字！");
        theField.focus();
        return false;
    }

    return true;
}

// 所输入字符串是否均为合法字符
// charBag中为包含所有合法字符的字符串
function checkStrLegal(theField, fieldName, charBag) {
    if (theField.value == "") return true;
    for (var i = 0; i < theField.value.length; i++) {
        var c = theField.value.charAt(i);
        if (charBag.indexOf(c) == -1) {
            alert(fieldName + "含有非法字符(" + c + ")！");
            theField.focus();
            return false;
        }
    }

    return true;
}

// 所输入字符串是否均为合法字符
// charBag中为包含非法字符的字符串
function checkStrLegal2(theField, fieldName, charBag) {
    if (theField.value == "") return true;
    for (var i = 0; i < theField.value.length; i++) {
        var c = theField.value.charAt(i);
        if (charBag.indexOf(c) > -1) {
            alert(fieldName + "含有非法字符(" + c + ")！");
            theField.focus();
            return false;
        }
    }

    return true;
}

// 电子邮件验证
function checkEmail(theField) {
    var pattern = /^.+@.+\..+$/;

    if (theField.value == "") return true;
    if (!pattern.test(theField.value)) {
        alert("请输入合法的电子邮件地址");
        theField.focus();
        theField.select();
        return false;
    }

    return true;
}




/**********************************************************************************/
//年月日的选择
function getValue() {
    var year = document.getElementById("YYYY").options[document.getElementById("YYYY").selectedIndex].value;
    var month = document.getElementById("MM").options[document.getElementById("MM").selectedIndex].value;
    var day = document.getElementById("DD").options[document.getElementById("DD").selectedIndex].value;
    document.getElementById("hfValue").value = year + "-" + month + "-" + day;
}

function SetDate() {
    strYYYY = document.form1.YYYY.outerHTML;

    strMM = document.form1.MM.outerHTML;
    strDD = document.form1.DD.outerHTML;
    MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    //先给年下拉框赋内容
    var y = new Date().getFullYear();
    var str = strYYYY.substring(0, strYYYY.length - 9);

    for (var i = (y - 80); i < (y + 2); i++) //以今年为准，前30年，后30年
    {
        str += "<option value='" + i + "'> " + i + "" + "</option>\r\n";
    }
    document.form1.YYYY.outerHTML = str + "</select>";
    //赋月份的下拉框
    var str = strMM.substring(0, strMM.length - 9);
    for (var i = 1; i < 13; i++) {
        str += "<option value='" + i + "'> " + i + "" + "</option>\r\n";
    }
    document.form1.MM.outerHTML = str + "</select>";
    document.form1.YYYY.value = y;
    document.form1.MM.value = new Date().getMonth() + 1;
    var n = MonHead[new Date().getMonth()];
    if (new Date().getMonth() == 1 && IsPinYear(YYYYvalue)) n++;
    writeDay(n); //赋日期下拉框
    document.form1.DD.value = new Date().getDate();
}
function YYYYMM(str) //年发生变化时日期发生变化(主要是判断闰平年)
{
    var MMvalue = document.form1.MM.options[document.form1.MM.selectedIndex].value;
    if (MMvalue == "") {
        DD.outerHTML = strDD;
        return;
    }
    var n = MonHead[MMvalue - 1];
    if (MMvalue == 2 && IsPinYear(str)) n++;
    writeDay(n)
}
function MMDD(str) //月发生变化时日期联动
{
    var YYYYvalue = document.form1.YYYY.options[document.form1.YYYY.selectedIndex].value;
    if (str == "") {
        DD.outerHTML = strDD;
        return;
    }
    var n = MonHead[str - 1];
    if (str == 2 && IsPinYear(YYYYvalue)) n++;
    writeDay(n)
}
function writeDay(n) //据条件写日期的下拉框
{
    var s = strDD.substring(0, strDD.length - 9);
    for (var i = 1; i < (n + 1); i++) s += "<option value='" + i + "'> " + i + "" +

"</option>\r\n";
    document.form1.DD.outerHTML = s + "</select>";

}
function IsPinYear(year) //判断是否闰平年
{
    return (0 == year % 4 && (year % 100 != 0 || year % 400 == 0))
}
/**********************************************************************************/


//去左右空格; 
function Trim(s) {
    return s.replace(/(^\s*)|(\s*$)/g, "");
}

//取得当前时间   
function getNowTime() {
    var now = new Date();
    var year = now.getYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    var nowdate = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    return nowdate;
}
