﻿//数字(0-9)
function validInt(value) {
    //return /^\d*[0-9]$/gi.test(value);
    var rr = /^\d+$/;
    return rr.test(value);
}

//正浮点数
function validMoney(value) {
    //return /^([1-9]\d{0,11}|0)(\.[0-9]{0,5})?$/gi.test(value);
    var rr = /^[+-]?[0-9]{0,}\.[0-9]{1,4}$/;
    return rr.test(value);
}

var tool = {
    wording: "系统繁忙，请刷新再试",
    left: 0,
    top: 0,
    bgLeft: 0,
    bgTop: 0,
    setCenter: function($_dom, num) {
        var bodyWidth = tool.getDocumentWidth();
        var bodyHeight = tool.getDocumentHeight();
        var vWidth = tool.getViewportWidth();
        var vHeight = tool.getViewportHeight();
        var sLeft = tool.getHorizontalScroll();
        var sTop = tool.getVerticalScroll();
        var width1 = $_dom.width(); //背景		
        var height1 = $_dom.height(); //背景	
        var top1 = (sTop ? sTop : 0) + (vHeight - height1) / 2;
        var left1 = (sLeft ? sLeft : 0) + (vWidth - width1) / 2;
        if (num)
            left1 -= num;
        $_dom.css({ "top": top1 + "px", "left": left1 + "px" });
        $("#bg").css({ "height": bodyHeight + "px", "width": bodyWidth + "px", "display": "block" });
    },
    getWindowX: function() {
        if (window.screenLeft)
            return window.screenLeft;
        else if (window.screenX)
            return window.screenX;
    },
    getWindowY: function() {
        if (window.screenLeft)
            return window.screenTop;
        else if (window.screenX)
            return window.screenY;
    },
    getViewportWidth: function() {
        if (window.innerWidth)
            return window.innerWidth;
        else if (document.documentElement && document.documentElement.clientWidth)
            return document.documentElement.clientWidth;
        else if (document.body.clientWidth)
            return document.body.clientWidth;
    },
    getViewportHeight: function() {
        if (window.innerHeight)
            return window.innerHeight;
        else if (document.documentElement && document.documentElement.clientHeight)
            return document.documentElement.clientHeight;
        else if (document.body.clientHeight)
            return document.body.clientHeight;
    },
    getHorizontalScroll: function() {
        if (window.pageXOffset)
            return window.pageXOffset ? window.pageXOffset : 0;
        else if (document.documentElement && document.documentElement.scrollLeft)
            return document.documentElement.scrollLeft ? document.documentElement.scrollLeft : 0;
        else if (document.body.scrollLeft)
            return document.body.scrollLeft ? document.body.scrollLeft : 0;
    },
    getVerticalScroll: function() {
        if (window.pageYOffset)
            return window.pageYOffset ? window.pageYOffset : 0;
        else if (document.documentElement && document.documentElement.scrollTop)
            return document.documentElement.scrollTop ? document.documentElement.scrollTop : 0;
        else if (document.body.scrollTop)
            return document.body.scrollTop ? document.body.scrollTop : 0;
    },
    getDocumentWidth: function() {
        var d = document, b = d.body, e = d.documentElement;
        return Math.max(Math.max(b.scrollWidth, e.scrollWidth), Math.max(b.clientWidth, e.clientWidth))
    },
    getDocumentHeight: function() {
        var d = document, b = d.body, e = d.documentElement;
        return Math.max(Math.max(b.scrollHeight, e.scrollHeight), Math.max(b.clientHeight, e.clientHeight))
    },
    trim: function(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    checkQQ: function(qq) {
        qq = qq.replace(/(^\s*)|(\s*$)/g, "");
        var patrn = /^(\d){5,12}$/;
        if (patrn.exec(qq)) {
            qq = parseInt(qq, 10)
            if (qq > 10000 && qq < 9999999999)
                return true;
        }
        return false;
    }
};

Number.prototype.round = function() {
    var b = arguments.length > 0 ? arguments[0] : 0;
    var NUMBER = Math.round(this * Math.pow(10, b)) * Math.pow(10, -b);
    NUMBER = Math.round(NUMBER * 100000000000) / 100000000000;
    var ret = NUMBER.toString();
    if (b > 0) {
        if (ret.indexOf(".") <= -1) { ret = ret.space(b, "0", true); }
        else {
            if (ret.split(".")[1].length <= b) { ret = ret.space((b - ret.split(".")[1].length), "0"); }
            else { ret = ret.split(".")[0] + "." + ret.split(".")[1].substr(0, (b - 1)); }
        }
    }
    if (b <= 0) ret = ret.split(".")[0]
    return ret;
}

Date.prototype.format = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1,
        //月份
        "d+": this.getDate(),
        //日
        "h+": this.getHours() % 24 == 0 ? 24 : this.getHours() % 24,
        //小时
        "H+": this.getHours(),
        //小时
        "m+": this.getMinutes(),
        //分
        "s+": this.getSeconds(),
        //秒
        "q+": Math.floor((this.getMonth() + 3) / 3),
        //季度
        "S": this.getMilliseconds() //毫秒
    };
    var week = {
        "0": "\u65e5",
        "1": "\u4e00",
        "2": "\u4e8c",
        "3": "\u4e09",
        "4": "\u56db",
        "5": "\u4e94",
        "6": "\u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
    }
    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 AutoResizeImage(maxWidth, maxHeight, objImg) {
    var img = new Image();
    img.src = objImg.src;
    var hRatio;
    var wRatio;
    var Ratio = 1;
    var w = img.width;
    var h = img.height;
    wRatio = maxWidth / w;
    hRatio = maxHeight / h;
    if (maxWidth == 0 && maxHeight == 0) {
        Ratio = 1;
    } else if (maxWidth == 0) {//
        if (hRatio < 1) Ratio = hRatio;
    } else if (maxHeight == 0) {
        if (wRatio < 1) Ratio = wRatio;
    } else if (wRatio < 1 || hRatio < 1) {
        Ratio = (wRatio <= hRatio ? wRatio : hRatio);
    }
    if (Ratio < 1) {
        w = w * Ratio;
        h = h * Ratio;
    }
    objImg.height = h;
    objImg.width = w;
}

function HYTsubstr(str, len) {
    if (!str || !len) { return ''; }       //预期计数：中文2字节，英文1字节     
    var a = 0;       //循环计数     
    var i = 0;       //临时字串     
    var temp = '';
    for (i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255) {             
            //按照预期计数增加2             
            a += 2;
        } else {
            a++;
        }         
        //如果增加计数后长度大于限定长度，就直接返回临时字符串
        if (a > len) {
            return temp;
        }           
        //将当前内容加到临时字符串
        temp += str.charAt(i);
    }     
    //如果全部是单字节字符，就直接返回源字符串     
    return str;
}

this.imagePreview = function() {
    xOffset = 10;
    yOffset = 30;
    $("a.preview").hover(function(e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='" + this.href + "' alt='Image preview' />" + c + "</p>");
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function() {
	    this.title = this.t;
	    $("#preview").remove();
	});
    $("a.preview").mousemove(function(e) {
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });
};


function VisitCardDisplay(cc, cn, cp, ct, cf, cph, cq, cm, ce, ca, cs) {
    var piccard = "<div class=\"" + cs + "\"><p id=\"" + cs + "_txt\">" + cc + "</p>";
    piccard += "<p id=\"" + cs + "_txt2\"><span>" + cn + "</span>  " + cp + "</p>";
    if (cph != "")
        piccard += "<p>手机：" + cph + "</p>";
    if (ct != "" & cf != "")
        piccard += "<p>座机：" + ct + " 传真：" + cf + "</p>";
    else if (ct == "" & cf != "")
        piccard += "<p>传真：" + cf + "</p>";
    else if (ct != "" & cf == "")
        piccard += "<p>座机：" + ct + "</p>";
    if (ce != "")
        piccard += "<p>Email：" + ce + "</p>";
    if (cq != "" & cm != "")
        piccard += "<p>QQ：" + cq + "  MSN：" + cm + "</p>";
    else if (cq == "" & cm != "")
        piccard += "<p>MSN：" + cm + "</p>";
    else if (cq != "" & cm == "")
        piccard += "<p>QQ：" + cq + "</p>";
    piccard += "<p>公司地址：" + ca + "</p></div>";
    return piccard;
}
