﻿// JavaScript Document

//========================================
//功能：通用函数  
//========================================



//========================================
//功能：去掉字符中左右两端的空格
//参数说明：需要去掉空格的字符
//========================================
function trim(value){
    return value.replace(/(^\s*)|(\s*$)/g,"");
}

//========================================
//功能：去掉字符中左边的空格
//参数说明：
//======================================== 
function LTrim(value){
    return value.replace(/(^\s*)/g,"");
}

//======================================== 
//功能： 去掉字符中右边的空格
//参数说明：
//========================================
function RTrim(value){
    return value.replace(/(\s*$)/g,"");
}

//========================================
//功能： 限制用户在文本框中输入非字数符
//使用方法：在文本框中添加事件 onkeypress="return num()"
//参数说明：：
//========================================
function Num(){
    var berr=false;
    if (!(event.keyCode>=46 && event.keyCode<=57)) berr=true;
    return !berr;
}

//========================================
//功能：弹出新窗口
//使用方法：在文本框中添加事件 onclick="return Num()"
//参数说明：
//========================================
function openWindow(href, width, height){
    window.open(href,'','width='+width+',height='+height+',left='+(screen.width-width)/2+',top=100,toolbar=no, menubar=no,scrollbars=yes,resizable=no,location=no,status=no');
}

//========================================
//功能：文件后缀
//参数说明：文件名称
//========================================
function fileExt(file){
    return file=(file.indexOf('.')>-1)? file.substring(file.lastIndexOf('.')+1,file.length): "";
}

//========================================
//功能：会员登录名是否合法,会员登录名由3-20位的字母、数字和下划线组成
//参数说明：用户名
//========================================
function isUserName(value)
{
	var reg = /^(\w){3,20}$/;
	return reg.test(value);
}

//========================================
//功能：非军用车车牌号是否合法,第1个字符必须为汉字，第2个字符必须为字母，第三个字符为分隔符”-“，分隔符后面由5位数字或字母组成
//参数说明：用户名
//========================================
function isCarNO(value)
{
	var reg = /^[\u4E00-\u9FA5]?[a-zA-Z]-\w{5}$/;	
	return reg.test(value);
}

function isUrl(value)
{
	var reg = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/;
	return reg.test(value);
}

function isIP(value)
{
	var reg = /^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$/;
	return reg.test(value);
}

//========================================
//功能：文件名是否合法
//参数说明：文件名称
//========================================
function isFileName(fileName){
	var err='\/:*?"<>|';
	for(i=0;i<fileName.length;i++){
		if(err.indexOf(fileName.substring(i,i+1))>-1) return false;
	}
	return true;
}

//========================================
//功能：弹出确认取消按钮窗口
//参数说明：提示信息
//========================================
function markSure(value){
    if(confirm(value)){return true; }else{return false; };
}

//========================================
//功能：判断是否为正确的日期
//参数说明：日期，以"-"或"/"分开
//========================================
function   isDate(strDate){  
        var   strSeparator   =   "-";   //日期分隔符   
        var   strDateArray;   
        var   intYear;   
        var   intMonth;   
        var   intDay;   
        var   boolLeapYear;  
		if(strDate.indexOf('-',0) != -1) strSeparator = '-';
		else if(strDate.indexOf('/',0) != -1) strSeparator = '/';
        strDateArray   =   strDate.split(strSeparator); 
		if(strDateArray.length!=3)   return   false;   
        intYear   =   parseInt(strDateArray[0],10);   
        intMonth   =   parseInt(strDateArray[1],10);   
        intDay   =   parseInt(strDateArray[2],10); 
		if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay))   return   false;   
        if(intMonth>12||intMonth<1)   return   false;   
        if((intMonth==1||intMonth==3||intMonth==5||intMonth==7||intMonth==8||intMonth==10||intMonth==12)&&(intDay>31||intDay<1))   return   false;   
        if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30||intDay<1))   return   false;   
        if(intMonth==2){   


              if(intDay<1)   return   false;   
              boolLeapYear   =   false;   
              if((intYear%100)==0){   
                    if((intYear%400)==0)   boolLeapYear   =   true;   
              }   
              else{   
                    if((intYear%4)==0)   boolLeapYear   =   true;   
              }   
    
              if(boolLeapYear){   
                    if(intDay>29)   return   false;   
              }   
              else{   
                    if(intDay>28)   return   false;   
              }   
        }   
        return true;   
  }

//========================================
//功能：检测是否为英文字符,包括大小写及符号
//参数说明：
//========================================
function isEnglish(value) //
{
	if(value.length == 0) return false;
	for(i = 0; i < value.length; i++) {
		if(value.charCodeAt(i) > 128) return false;
	}
return true;
}

//========================================
//功能：检测是否为中文
//参数说明：：
//========================================
function isChinese(value) 
{
	if(value.length == 0)	return false;
	for(i = 0; i < value.length; i++) {
		if(value.charCodeAt(i) > 19968)	return true;
	}
	return false;
}

//========================================
//功能：检测是否为数值（包括小数和整数）
//参数说明：
//========================================
function isNumber(value) 
{
	return !(isNaN(value));
}

//========================================
//功能：检测E-mail是否合法
//参数说明：
//========================================
function isMail(value) 
{
	var reg = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.(com|cn|mobi|asia|net|org|info|me|com.cn|net.cn|org.cn|gov.cn|hk|tv|biz|cc|name)$/;
	return reg.test(value);
}

//========================================
//功能：检查手机是否合法
//合法格式：(1)移动电话号码为11或12位，如果为12位,那么第一位为0  (2)11位移动电话号码的第一位和第二位为'13' (3)12位移动电话号码的第二位和第三位为'13' 部分隔开 
//参数说明：
//========================================
function isMobile(value)//验证手机
   {
          var reg=/^0{0,1}1[3,5,8]{1}\d{9}$/;   //130--139。至少7位
          return reg.test(value);
   }
  

//========================================
//功能：检查座机是否合法
//合法格式：(1)电话号码由数字、'('、')'和'-'构成 (2)电话号码为3到8位(3)如果电话号码中包含有区号，那么区号为三位或四位 (4)区号用'('、')'或'-'和其他部分隔开 
//参数说明：
//========================================
function isPhone(value)//
  {
	  var reg = /(^[0-9]{3,4}\-[0-9]{3,8}\-[0-9]{1,4}$)|(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)/
      return reg.test(value);
  }
  
function isPostCode(value)
    {
	  var reg = /^[0-9]{6}$/
      return reg.test(value);
    }

//========================================
//功能：四舍五入 How为小数位数：
//参数说明：
	//Dight  : 要进行四舍五入的数
	//How : 要舍入的位数
//========================================
function forDight(Dight,How){ 
    var result = parseInt(Math.round (Dight*Math.pow(10,How)),10)/Math.pow(10,How);
    return result.toString();
} 

//========================================
//功能：产生随机数(minnum,maxnum之间)
//参数说明：minNum最小值，maxNum最大值
//========================================
function rndNumber(minNum,maxNum){
	var result = parseInt(minNum) + parseInt((maxNum-minNum) * Math.random());
	return result.toString();
}

//========================================
//功能：加载图片限制 resizeImg(this,width,height))
//参数说明：img对像，高，宽
//应用说明：<img scr="" onload=制 resizeImg(this,10,10)""/> //========================================
function resizeImg(ImgD,iwidth,iheight) {
     var image=new Image();
     image.src=ImgD.src;
     if(image.width>0 && image.height>0){
        if(image.width/image.height>= iwidth/iheight){
           if(image.width>iwidth){
               ImgD.width=iwidth;
               ImgD.height=(image.height*iwidth)/image.width;
           }else{
                  ImgD.width=image.width;
                  ImgD.height=image.height;
                }
               //ImgD.alt=image.width+"×"+image.height;
        }
        else{
                if(image.height>iheight){
                       ImgD.height=iheight;
                       ImgD.width=(image.width*iheight)/image.height;
                }else{
                        ImgD.width=image.width;
                        ImgD.height=image.height;
                     }
                //ImgD.alt=image.width+"×"+image.height;
            }
　　　　　ImgD.style.cursor= "pointer"; //改变鼠标指针
　　　　　//ImgD.onclick = function() { window.open(this.src);} //点击打开大图片
　　　　if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判断浏览器，如果是IE
　　　　　　//ImgD.title = "请使用鼠标滚轮缩放图片，点击图片可在新窗口打开";
　　　　　　ImgD.onmousewheel = function img_zoom() //滚轮缩放
　　　　　 {
　　　　　　　　var zoom = parseInt(this.style.zoom, 10) || 100;
　　　　　　　　　　zoom += event.wheelDelta / 12;
　　　　　　　　if (zoom> 0)　this.style.zoom = zoom + "%";
　　　　　　　　　　return false;
　　　　　 }
　　　  } else { //如果不是IE
　　　　　　　   //ImgD.title = "点击图片可在新窗口打开";
　　　　　　 }
    }
} 


//========================================
//功能：格式化数字为多少位小数,并加千分位
//参数说明：num格式化数字 ,nAfterDot小数位
//应用说明：
//========================================
function formatNumber(num,nAfterDot)   //小数位数
{
	var result=0;
	var dec="";
	num = forDight(num,nAfterDot).toString();
	if (isNaN(num)){
		result=0;
	}else{
		if (num.length<4){
			result=num;
		}else{	
			pos=num.indexOf(".",1);
			if (pos>0){
				dec=num.substr(pos);   //小数部分的字符串，包括小数点
				res=num.substr(0,pos);
			}else{
				res=num;
			}
			var tempResult="";
			for(i=res.length;i>0;i-=3){     //将整数部分分位显示
				if(i-3>0){
				tempResult=","+res.substr(i-3,3)+tempResult;	
				}else{
					tempResult=res.substr(0,i)+tempResult;	
				}	
			}
			result=tempResult+dec;
		}
	}
	return result;
} 

function changeCode(oElement)
{
	var s_time = (new Date()).getTime();
	oElement.src =  webSiteUrl + '/ValidateCode.aspx?tmp=' + s_time;
}
