﻿function CheckAll(name, check) {

    var items = document.getElementsByTagName("input");

    for (var i = 0; i < items.length; i++) {
        if (items[i].type == "checkbox" && items[i].name.indexOf(name) >= 0) {
            if (!items[i].disabled)
                items[i].checked = check;
        }
    }
}

function showTabInfo(num, count) {
    for (i = 1; i <= count; i++) {
        if (i == num) {
            document.getElementById("infolnk" + i).className = "active";
            document.getElementById("info" + i).style.display = "block";
        }
        else {
            document.getElementById("infolnk" + i).className = "";
            document.getElementById("info" + i).style.display = "none";
        }
    }
}

function strLen(str) {
    if (str == null) return 0;

    var len = 0;

    for (i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255)
            len += 2;
        else len++;
    }

    return len;
}


function $1(id) {
    return document.getElementById(id);
}

function bindRbl(id, name, func) {
    var a = document.getElementById(id).getElementsByTagName("INPUT");
    for (i = 0; i < a.length; i++) {
        $("#" + id + "_" + i).bind(name, func);
    }
    return "";
}

function getRblValue(id) {

    var b = document.getElementById(id);
    var a = b.getElementsByTagName("INPUT");

    for (i = 0; i < a.length; i++) {
        if (a.item(i).checked) {
            return a.item(i).value;
        }
    }
    return "";
}

function testNullOrEmpty(value, func) {
    if (value == null || value.length <= 0) {
        func();
        return false;
    }
    return true;
}

function testEmail(value, func) {

    var pattern = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

    if (!pattern.test(value)) {
        func();
        return false;
    }
    return true;
}

function testStrLength(value, min, max, func) {
    if (value == null || value.length < min || value.length > max) {
        func();
        return false;
    }

    return true;
}

function testNumber(value, func) {

    if (!isNumber(value)) {
        func();
        return false;
    }

    return true;
}

function testDate(value, func) {

    if (!isDate(value)) {
        func();
        return false;
    }

    return true;
}

function isDate(str) {
    var the1st = str.indexOf('-');
    var the2nd = str.lastIndexOf('-');

    if (the1st == the2nd) {
        return (false);
    }
    else {
        var y = str.substring(0, the1st);
        var m = str.substring(the1st + 1, the2nd);
        var d = str.substring(the2nd + 1, str.length);
        var maxDays = 31;

        if (isNumber(m) == false || isNumber(d) == false || isNumber(y) == false) {
            return false;
        }
        else if (y.length < 4) { return (false); }
        else if ((m < 1) || (m > 12)) { return (false); }
        else if (m == 4 || m == 6 || m == 9 || m == 11) maxDays = 30;
        else if (m == 2) {
            if (y % 4 > 0) maxDays = 28;
            else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
            else maxDays = 29;
        }
        if ((m < 1) || (m > maxDays)) { return (false); }
        else { return (true); }
    }
}

function isNumber(num) {
    var i, j, strTemp;
    strTemp = "0123456789";
    if (num.length == 0) {
        return false;
    }
    for (i = 0; i < num.length; i++) {
        j = strTemp.indexOf(num.charAt(i));
        if (j == -1) {//说明有字符不是数字
            return false;
        }
    }

    return true; //说明是数字
}

function testPhone(value, fail) {
    if (!isPhone(value)) {
        fail();
        return false;
    }

    return true;
}

function isPhone(value) {


    var n = 0;
    for (i = 0; i < value.length; i++) {
        var ch = value.charAt(i);
        if (ch >= 0 && ch <= 9) {
            n++;
            continue;
        }

        //if (ch != '+' && ch != '-' && ch != ' ') return false;
    }

    if (n < 7) return false;

    return true;

    //var match = /^[+]?((\(\d{2,3}\))|(\d{2,3}\-))?(\(0?\d{2,3}\)|0?\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/.test(value);   
    //return match;
}


function testMobile(value, fail) {
    if (!isMobile(value)) {
        fail();
        return false;
    }

    return true;
}

function isMobile(value) {

    var n = 0;
    for (i = 0; i < value.length; i++) {
        var ch = value.charAt(i);
        if (ch >= 0 && ch <= 9) {
            n++;
            continue;
        }

        //if (ch != '+' && ch != '-' && ch != ' ') return false;
    }
    
    if (n < 7) return false;
    return true;
    
    if (n < 7 || n > 24) return false;


    var match = /^[+]?((\(\d{2,3}\))|(\d{3}\-))?[1-9]\d{10,}$/.test(value);

    if (!match) {
        match = /^([+]|00)?((\(\d{2,3}\))|(\d{2,3}\-))?(\(0?\d{2,3}\)|0?\d{2,3}-)?[1-9]\d{5,}$/.test(value);
    }

    return match;
}
