﻿//去除左右空格 
String.prototype.trim = function() {
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}

//去处左空格 
String.prototype.lTrim = function() {
    return this.replace(/(^[\s]*)/g, "");
}

//去处右空格 
String.prototype.rTrim = function() {
    return this.replace(/([\s]*$)/g, "");
}

/**
* 统计字符串字节数
*
* return	integer
*/
String.prototype.ByteCount = function() {
    txt = this.replace(/([\u0391-\uFFE5])/ig, '11');
    var count = txt.length;
    return count;
}
//判断是否是整数
function chkInteger(NUM) {
    var pattern = /^-?([1-9]\d*|0)$/;
    if (NUM.length == 0) {
        return false;
    } else {
        return pattern.test(NUM);
    }
}
//判断是否是浮点数
function chkNumeric(NUM){
    var pattern = /^-?([1-9]\d*|[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$/;
    if (NUM.length == 0) {
        return false;
    } else {
        return pattern.test(NUM);
    }
}
//判断是否是合法的email
function checkEmail(email){
	var pattern = /^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|com|gov|mil|org|edu|int)$/;
	if (email.length==0){
		return false;
	}else{
		return pattern.test(email);
	}
}
//判断手机是否合法
function checkMobile(mobile){
    var pattern = /^1[3,5,8]\d{9}$/;
    if (mobile.length==0){
		return false;
	}else{
		return pattern.test(mobile);
	}
}
/// <summary>
/// 验证输入框的值是否合法
/// </summary>
/// <param name="inputid">输入框的ID</param>
/// <param name="canempty">可否为空</param>
/// <param name="maxlength">最大长度</param>
/// <param name="minlength">最小长度，没有最小长度为0</param>
/// <returns>是否合法</returns>
function checkinputvalue(inputid, canempty, maxlength, minlength) {
    var inputValue = document.getElementById(inputid).value.trim();
    if (!canempty) {
        if (inputValue == "") {
            return false;
        }
    }
    var valueLength = inputValue.ByteCount();
    if (minlength > 0) {
        if (valueLength > 0 && valueLength < minlength) {
            return false;
        }
    }
    if (valueLength > maxlength) {
        return false;
    }
    else {
        return true;
    }
}
//弹出新窗口
function win_open(htmlurl, win_width, win_heihgt, win_left, win_top) {
    var newwin = window.open(htmlurl, "newWin", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=" + win_top + ",left=" + win_left + ",width=" + win_width + ",height=" + win_heihgt);
    if (newwin) { newwin.focus(); }
    return false;
}
//图片自动缩放函数
//ImgD 图片所在的img对象
//要显示的最大宽
//要显示的最大高
function ImgAuto(ImgD,proMaxWidth,proMaxHeight)
{
    var image=new Image();
    image.src=ImgD.src;
    image.onload = function(){
        if(image.width>0&&image.height>0)
        {
            if (image.width <= proMaxWidth && image.height <= proMaxHeight)
            {
                ImgD.width=image.width;
                ImgD.height=image.height;
            }
            else
            {
                if (image.width > image.height)
                {
                    proMaxHeight = image.height * proMaxHeight / image.width;
                }
                else
                {
                    proMaxWidth = image.width * proMaxWidth / image.height;
                }
                ImgD.width = proMaxWidth;
                ImgD.height = proMaxHeight;
            }
        }
    };
    image.onload();
}