﻿$(document).ready(function() {

    if ($("body").html().indexOf(AlertMessage()) == -1) {
        $("body").append(AlertMessage());
    }

});

function alertMessage(obj, msg) {
    var alertDiv = $("#alertMsg");
    alertDiv.css("top", obj.offset().top + obj.height() + 5 + "px");
    alertDiv.css("left", obj.offset().left + "px");
    alertDiv.css("zIndex", 1000);
    alertDiv.html(msg);
    alertDiv.show();

    initAlertMsg();

    obj.bind("onblur", function() {
        alertDiv.css("display", "none");
        $("#alertMsgFrame").css("display", "none");
        $("#alertMsgFrame").remove();
    });

    obj.focus();
}
function iframeDecorateDiv(divId, iframId) {
    if ($.browser.msie && $.browser.version < 7) {

        if ($("#" + divId + " > iframe").length > 0) {
            return;
        }

        var div = $("#" + divId);
        var divIframe = $('<iframe/>');

        div.append(divIframe);
        if (iframId) {
            divIframe.attr("id", iframId);
        }
        divIframe.css("position", "absolute");
        divIframe.css("display", "none");
        divIframe.css("display", "block");
        divIframe.css("z-index", "-1");
        divIframe.css("top", "-2");
        divIframe.css("left", "-2");
        try {
            divIframe.css("width", div.width() + parseInt(div.css("padding")) * 2 + 4 + "px");
            divIframe.css("height", div.height() + parseInt(div.css("padding")) * 2 + 4 + "px");
        } catch (e) {
        }
        divIframe.css("filter", "mask(color=#a09261)");
    }
}
function initAlertMsg() {
    if ($.browser.msie) {
        iframeDecorateDiv("alertMsg", "alertMsgFrame");
    }

    document.body.onmousedown = function() {
        $("#alertMsg").css("display", "none");
        $("#alertMsgFrame").css("display", "none");
        $("#alertMsgFrame").remove();
    }
}
function showMoreInfo(target, popBoxId) {
    if ($('#' + popBoxId).html()) {
        $('#' + popBoxId).css('top', $(target).offset().top + $(target).height());
        $('#' + popBoxId).css('left', $(target).offset().left);
        $('#' + popBoxId).show();
    }
}

var AlertMessage = function() {
    var div = "<div id=\"alertMsg\" style=\"z-index: 99999; margin: 0px; padding: 10px; font-size: 12px; text-align: left; background: #FFFFE8; border: 1px solid #FFDF47; color: #000;  white-space: nowrap; position: absolute; display: none;\">" +
         " </div>";
    return div;
}

var getLength = function(str) {
    var len = 0;
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) >= 0x4e00 && str.charCodeAt(i) <= 0x9fa5) {
            len += 2;
        }
        else {
            len++;
        }
    }
    return len;
}


var StringUtils = {};

StringUtils.isEmail = function(value) {
    var trimValue = $.trim(value);
    if (trimValue == "")
        return true;
    var reg = /^\b(^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b$/;
    return reg.test(trimValue);
}
var Trim = function(str) {
    if (str == null) {
        return "";
    }
    return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');
}
StringUtils.isBlank = function(value) {
    var trimValue = $.trim(value);
    return trimValue == "";
};

StringUtils.containsChinese = function(value) {
    var reg = /[\u4E00-\u9FA5]/;
    return reg.test(value);
}

StringUtils.containsChineseCorner = function(value) {
    var reg = /[\uff00-\uffff]/;
    return reg.test(value);
}

StringUtils.isEnglishStr = function(value) {
    var trimValue = $.trim(value);
    if (trimValue == "") {
        return true;
    }
    var pattern = /^[A-Za-z\s]+$/;
    return pattern.test(trimValue);
}

StringUtils.isEnglishName = function(value) {
    var trimValue = $.trim(value);
    return StringUtils.isEnglishStr(trimValue) && trimValue.indexOf(' ') > 0;
}

StringUtils.isIntegerStr = function(value) {
    var reg = /^\d+$/;
    return reg.test(value);
}

StringUtils.isPhone = function(value) {
    var reg = /^(0\d{2,3}-)?(0\d{2,3}-)?(\d{7,8})([-]\d{2,})?$/;
    return reg.test(value);
}
StringUtils.IsMobile = function(value) {
    var reg = /^[1][3,5,8][0-9]{9}$/;
    return reg.test(value);
}
StringUtils.isPostCard = function(value) {
    var reg = /^[1-9]\d{5}$/;
    return reg.test(value);
}
StringUtils.isQQ = function(value) {
    var reg = /^[1-9][0-9]{4,}$/;
    return reg.test(value);
}
StringUtils.isNumberStr = function(value) {
    var reg = /^\d+([\.]\d+)?$/;
    return reg.test(value);
}

StringUtils.isMoneyStr = function(value) {
    var reg = /^[0-9]+([.]{1}[0-9]{1,2})?$/;
    return reg.test(value);
}
var isIdCardNo = function(num) {
    var retMeg = '';
    var len = num.length, re;
    if (len == 15) {
        if (!isData(num)) { retMeg = "身份证号码输入的不是数字！\n"; return retMeg; }
    }
    else if (len == 18) {
        if (num.substring(17, 18) != "X" && !isData(num.substring(17, 18)) && num.substring(17, 18) != "0") {
            retMeg = "18位身份证中最后一位请输入数字或大写X\n"; return retMeg;
        }
        else if (!isData(num.substring(0, 17))) {
            retMeg = "18位身份证中前17位请输入数字\n"; return retMeg;
        }
    }
    if (len == 15)
        re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
    else if (len == 18)
        re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$/);
    else { retMeg = "身份证号码输入的数字位数不对！\n"; return retMeg; }


    var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏",
        33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南",
        50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾",
        81: "香港", 82: "澳门", 91: "国外"
    };
    if (aCity[parseInt(num.substr(0, 2))] == null) { retMeg = "身份证地区非法！\n"; return retMeg; }

    var a = num.match(re), F;
    var today = new Date();
    if (a != null) {
        var D = null;
        if (len == 15) {
            D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        }
        else {
            D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        }
        var StartDate = new Date("1900/01/01");
        if (D < StartDate)
        { retMeg = "输入的身份证号 " + a[0] + " 里出生日期不对！\n"; return retMeg; }
        if (B) {
            if (a[3] == today.getFullYear()) {
                if (a[4] < (today.getMonth() + 1) && a[5] < today.getDate()) {
                    F = true;
                }
                else {
                    F = false;
                }
            }
            else if (a[3] < today.getFullYear()) {
                F = true;
            }
            else {
                F = false;
            }

            if (!F) {
                retMeg = "输入的身份证号 " + a[0] + " 里出生日期不对！\n";
                return retMeg;
            }
        }
        else {
            retMeg = "输入的身份证号 " + a[0] + " 里出生日期不对！\n";
            return retMeg;
        }
    }
    return retMeg;
}
var isData = function(s) {
    if (isNaN(parseInt(s)) == true) {
        return false;
    }
    else {
        var patrn = /(^[0-9]*[1-9][0-9]*$)/;
        return patrn.exec(s);
    }
}
StringUtils.isDate = function(value) {
    var reg = /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/;
    var result = value.match(reg);
    if (result != null) {
        var year = result[1];
        var month = result[2] - 1; //0-11
        var day = result[3] - 0;
        var date = new Date(year, month, day);

        return year == date.getFullYear()
                && month == date.getMonth()
                && day == date.getDate();
    }
    return false;
}

var DateUtils = {};
DateUtils.dateFormat = function(date) {
    date = DateUtils.convertStringToDate(date);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return "" + year + "-" + (month < 10 ? "0" + month : "" + month) + "-" + (day < 10 ? "0" + day : "" + day);
};

DateUtils.getNextDate = function(date) {
    return new Date(DateUtils.convertStringToDate(date).getTime() + (1000 * 60 * 60 * 24));
};

DateUtils.getAfterDate = function(date, day) {
    return new Date(DateUtils.convertStringToDate(date).getTime() + (1000 * 60 * 60 * 24 * day));
};

DateUtils.toDate = function(date) {
    return DateUtils.convertStringToDate(date);
};

DateUtils.convertStringToDate = function(date) {
    if (typeof (date) == 'string') {
        date = new Date(date.replace(/-/g, '/'));
    }
    return date;
}

DateUtils.getWeek = function(date) {
    date = DateUtils.convertStringToDate(date);

    return date.getDay();
}

DateUtils.dateBefore = function(d1, d2) {
    return Date.parse(DateUtils.convertStringToDate(d1)) - Date.parse(DateUtils.convertStringToDate(d2)) < 0;
}

DateUtils.dateEqual = function(d1, d2) {
    return Date.parse(DateUtils.convertStringToDate(d1)) - Date.parse(DateUtils.convertStringToDate(d2)) == 0;
}

DateUtils.dateBeforeOrEqual = function(d1, d2) {
    return DateUtils.dateBefore(d1, d2) || DateUtils.dateEqual(d1, d2);
}


DateUtils.dateAfter = function(d1, d2) {
    return Date.parse(this.convertStringToDate(d1)) - Date.parse(this.convertStringToDate(d2)) > 0;
}

DateUtils.dateAfterOrEqual = function(d1, d2) {
    return this.dateAfter(d1, d2) || this.dateEqual(d1, d2);
}

DateUtils.parseYMD = function(s) {
    var reg = new RegExp("[^0-9-]", "")
    if (s.search(reg) >= 0) {
        return null;
    }

    var ss = s.split("-");
    if (ss.length != 3) {
        return null;
    }
    if (isNaN(ss[0]) || isNaN(ss[1]) || isNaN(ss[2])) {
        return null;
    }
    if (ss[0].length != 4 || ss[1].length > 2 || ss[2].length > 2 || ss[1].length == 0 || ss[2].length == 0) {
        return null;
    }

    if (ss[1] > 12 || ss[2] > 31) {
        return null;
    }
    var date = new Date(parseFloat(ss[0]), parseFloat(ss[1]) - 1, parseFloat(ss[2]));
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);
    return date;
}

DateUtils.days = function(start, end) {
    var times = DateUtils.convertStringToDate(start).getTime() - DateUtils.convertStringToDate(end).getTime();
    var days = times / (1000 * 60 * 60 * 24);
    if (days < 0) {
        return -1 * days;
    } else {
        return days;
    }
}

var BizRatingCode = {};
BizRatingCode["EQUILANT_RATING_2"] = -2;
BizRatingCode["RATING_2"] = 2;
BizRatingCode["EQUILANT_RATING_3"] = -3;
BizRatingCode["RATING_3"] = 3;
BizRatingCode["EQUILANT_RATING_4"] = -4;
BizRatingCode["RATING_4"] = 4;
BizRatingCode["EQUILANT_RATING_5"] = -5;
BizRatingCode["RATING_5"] = 5;

function getBizRatingCode(code) {
    return BizRatingCode[code];
}


function CheckCreditCard(s) {
    if (s == "4580603482981582" || s == "4029976401161809") {
        return true;
    }

    var v = "0123456789";
    var w = "";
    for (var i = 0; i < s.length; i++) {
        x = s.charAt(i);
        if (v.indexOf(x, 0) != -1)
            w += x;
    }
    var j = w.length / 2;
    if (j < 6.5 || j > 8 || j == 7) return false;
    var k = Math.floor(j);
    var m = Math.ceil(j) - k;
    var c = 0;
    for (var i = 0; i < k; i++) {
        a = w.charAt(i * 2 + m) * 2;
        c += a > 9 ? Math.floor(a / 10 + a % 10) : a;
    }
    for (var i = 0; i < k + m; i++) c += w.charAt(i * 2 + 1 - m) * 1;
    return (c % 10 == 0);
}

