﻿//函數名：chkdate
//功能介紹：檢查是否為日期
//參數說明：要檢查的字符串
//返回值：false：不是日期  true：是日期
function chkdate(datestr)
{
	var lthdatestr
	if (datestr != "")
		lthdatestr= datestr.length ;
	else
	lthdatestr=0;
	  
	var tmpy="";
	var tmpm="";
	var tmpd="";
	//var datestr;
	var status;
	status=0;
	if ( lthdatestr== 0)
	return false


	for (i=0;i<lthdatestr;i++)
	{ 
		if (datestr.charAt(i)== '-')
		{
		status++;
		}
		if (status>2)
		{
			//alert("Invalid format of date!");
			return false;
		}
		if ((status==0) && (datestr.charAt(i)!='-'))
		{
			tmpy=tmpy+datestr.charAt(i)
		}
		if ((status==1) && (datestr.charAt(i)!='-'))
		{
			tmpm=tmpm+datestr.charAt(i)
		}
		if ((status==2) && (datestr.charAt(i)!='-'))
		{
			tmpd=tmpd+datestr.charAt(i)
		}
	}
	year=new String (tmpy);
	month=new String (tmpm);
	day=new String (tmpd)
	//tempdate= new String (year+month+day);
	//alert(tempdate);
	if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
	{
		//alert("Invalid format of date!");
		return false;
	}
	if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
	{
		//alert ("Invalid month or day!");
		return false;
	}
	if (!((year % 4)==0) && (month==2) && (day==29))
	{
		//alert ("This is not a leap year!");
		return false;
	}
	if ((month<=7) && ((month % 2)==0) && (day>=31))
	{
		//alert ("This month is a small month!");
		return false;
	}
	if ((month>=8) && ((month % 2)==1) && (day>=31))
	{
		//alert ("This month is a small month!");
		return false;
	}
	if ((month==2) && (day==30))
	{
		//alert("The Febryary never has this day!");
		return false;
	}
	return true;
}

//浮點數、整數驗證參數以下兩個函數。
//參數bnum=1 驗證整數
//參數bnum=2 驗證浮點數的合法性
//參數bnum=3 驗證浮點數2的合法性
//參數foc為輸入框的NAME
//如<input name=mm value="val" onblur="notnumfloat(1,mm)"> 如果輸入數不是合法整數焦點返回本編輯框
//可以重載myblur()執行自己的驗證代碼
function checknumfloat(val,bnum){
	if(bnum==1){
		if((!isFinite(val))||(isNaN(parseInt(val)))||val.lastIndexOf(".")>-1){
		return false
		}
	} 
	if(bnum==2){
		if((!isFinite(val))||(isNaN(parseFloat(val)))){
			return false
		}
	}
	if(bnum==3){
		val = val.replace(',','');
		if((!isFinite(val))||(isNaN(parseFloat(val)))){
			return false
		}
	}
	return true
}

//驗證字符串（只能是0-9,a-z,A-Z組成）
function chkAccount(strin){
	return (new RegExp(/[^0-9a-zA-Z]/g.test(strin)));
}

//驗證EMAIL
function chkEmail(strin){
	return (new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(strin));
}

//驗證密碼強度
function chkpwd(obj){
	var t=obj.value;
	var id=getResult(t);
	
	//定义对应的消息提示
	var msg=new Array(4);
	msg[0]="密码过短。";
	msg[1]="密码强度差。";
	msg[2]="密码强度良好。";
	msg[3]="密码强度高。";
	
	var sty=new Array(4);
	sty[0]=-45;
	sty[1]=-30;
	sty[2]=-15;
	sty[3]=0;
	
	var col=new Array(4);
	col[0]="gray";
	col[1]="red";
	col[2]="#ff6600";
	col[3]="Green";
	
	//设置显示效果
	var bImg="http://www.mmhomehome.com/Images/pwdlevel.gif";//一张显示用的图片
	var sWidth=300;
	var sHeight=15;
	var Bobj=document.getElementById("chkResult");

	Bobj.style.fontSize="12px";
	Bobj.style.color=col[id];
	Bobj.style.width=sWidth + "px";
	Bobj.style.height=sHeight + "px";
	Bobj.style.lineHeight=sHeight + "px";
	Bobj.style.background="url(" + bImg + ") no-repeat left " + sty[id] + "px";
	Bobj.style.textIndent="20px";
	Bobj.innerHTML="检测提示：" + msg[id];
}

//定义检测函数,返回0/1/2/3分别代表无效/差/一般/强
function getResult(s){
	if(s.length < 4){
		return 0;
	}
	var ls = 0;
	if (s.match(/[a-z]/ig)){
		ls++;
	}
	if (s.match(/[0-9]/ig)){
		ls++;
	}
	if (s.match(/(.[^a-z0-9])/ig)){
		ls++;
	}
	if (s.length < 6 && ls > 0){
		ls--;
	}
	return ls
}