//opt1 小数     opt2   负数
//当opt2为1时检查num是否是负数
//当opt1为1时检查num是否是小数
//返回1是正确的，0是错误的
function chknbr(num,opt1,opt2)
{
    var i=num.length;
    var staus;
//staus用于记录.的个数
    status=0;
    if ((opt2!=1) && (num.charAt(0)=='-'))
    {
        //alert("You have enter a invalid number.");
        return 0;
    
    }
//当最后一位为.时出错
    if (num.charAt(i-1)=='.')
    {
        //alert("You have enter a invalid number.");
        return 0;
    }

    for (j=0;j<i;j++)
    {
        if (num.charAt(j)=='.')
        {
            status++;
        }
        if (status>1) 
        {
        //alert("You have enter a invalid number.");
        return 0;        
        }
        if (num.charAt(j)<'0' || num.charAt(j)>'9' )
        {
            if (((opt1==0) || (num.charAt(j)!='.')) && (j!=0)) 
            {
                //alert("You have enter a invalid number.");
                return 0;
            }
        }
    }
    return 1;
}

//函数名：chkdate
//功能介绍：检查是否为日期
//参数说明：datestr要检查的对象
//返回值：0：不是日期  1：是日期
function chkdate(datestr)
{
	
    var lthdatestr
    if (datestr.value!="")
        lthdatestr= datestr.value.length ;
    else
        lthdatestr=0;
        
    var tmpy="";
    var tmpm="";
    var tmpd="";
    //var datestr;
    var status;
    status=0;

    
    for (i=0;i<lthdatestr;i++)
    {    if (datestr.value.charAt(i)== '-')
        {
            status++;
        }
        if (status>2)
        {
            //alert("Invalid format of date!");
            datestr.focus();
            return 0;
        }
        if ((status==0) && (datestr.value.charAt(i)!='-'))
        {
            tmpy=tmpy+datestr.value.charAt(i)
        }
        if ((status==1) && (datestr.value.charAt(i)!='-'))
        {
            tmpm=tmpm+datestr.value.charAt(i)
        }
        if ((status==2) && (datestr.value.charAt(i)!='-'))
        {
            tmpd=tmpd+datestr.value.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!");
        datestr.focus();
        return 0;
    }
    if (!((1<=month) && (12>=month) && (31>=day) && (1<=day) && (2050>=year) && (1850<=year)))
    {
        //alert ("Invalid month or day!");
        datestr.focus();
        return 0;
    }
    if (!((year % 4)==0) && (month==2) && (day==29))
    {
        //alert ("This is not a leap year!");
        datestr.focus();
        return 0;
    }
    if ((month<=7) && ((month % 2)==0) && (day>=31))
    {
        //alert ("This month is a small month!");
        datestr.focus();
        return 0;
    
    }
    if ((month>=8) && ((month % 2)==1) && (day>=31))
    {
        //alert ("This month is a small month!");
        datestr.focus();
        return 0;
    }
    if ((month==2) && (day==30))
    {
        //alert("The Febryary never has this day!");
        datestr.focus();
        return 0;
    }
    
    return 1;
}
//函数名：chksafe
//功能介绍：检查是否含有"'",'\\',"/"
//参数说明：要检查的字符串
//返回值：0：是  1：不是
/*function chksafe(a)
{    
    //return 1;
    fibdn = new Array ("'" ,"\\", "、", ",", ";", "/");
    i=fibdn.length;
    j=a.length;
    for (ii=0;ii<i;ii++)
    {    for (jj=0;jj<j;jj++)
        {    temp1=a.charAt(jj);
            temp2=fibdn[ii];
            if (tem1;p1==temp2)
            {    return 0; }
        }
    }
    return 1;
    
}*/

//函数名：chkspc
//功能介绍：检查是否含有空格
//参数说明：要检查的字符串
//返回值：0：是  1：不是
function chkspc(a)
{
    var i=a.length;
    var j = 0;
    var k = 0;
    while (k<i)
    {
        if (a.charAt(k) != " ")
            j = j+1;
        k = k+1;
    }
    if (j==0)
    {
        return 0;
    }
    
    if (i!=j)
    { return 2; }
    else
    {
        return 1;
    }
}

//函数名:chknum(obj)
//功能介绍:检查是否是数字
//参数说明:obj要检查的对象
//返回值:0:不是 1:是
function chknum(obj) {
	if(isNaN(obj.value)) {
		obj.focus();
		return 0;
	}
	else {
		obj.focus();
		return 1;
	}
}

//函数名:chknull
//功能介绍：检查是否为空
//参数说明:obj:要检查的对像；msgflag:是否显示提示信息
//返回值:0:是空 1:不是空
function chknull(obj,msgflag) {
	if(obj.value=='') {
		if(msgflag==1) {
			obj.focus();
			return 0;
		}
		obj.focus();
		return 0;
	}
	else {
		return 1;	
	}
}

//函数名:chkemail
//功能介绍：检查是合法email
//参数说明:email对象
//返回值:0:不合法 1:合法
function chkemail(email) {
    var i=email.value.length;
    var j = 0;
    var k = 0;
    var m = 0;
    while (k<i)
    {
        if (email.value.charAt(k) == "@") {
        	j = j+1;
        	m = k;
        }
        k = k+1;
    }
    if (j==1)
    {
    	j=0;
    	k=m;
    	m=0;
	    while (k<i)
    	{
        	if (email.value.charAt(k) == ".") {
        		j = j+1;
    	    }
        	k = k+1;
    	}
    	if(j!=0) {
        	return 1;
        }
        else {
        	email.focus();
        	return 0;
        }
    }
    else {
    	email.focus();
    	return 0;
    }
}

//函数名:datecompare
//功能介绍：两日期大小
//参数说明:date
//返回值:0:date1>date2 1:date1<date2 2:date1=date2
function datecompare(date1,date2) {
		if(!chkdate(date1)) {
			alert('invalid date '+date1);
			return false;
		}
		if(!chkdate(date2)) {
			alert('invalid date '+date2);
			return false;
		}
		
	if(date1>date2)
		return 0;
	if(date1<date2)
		return 1;
	if(date1==date2)
		return 2;
	
}

//函数名:chklen
//功能介绍：测试对象数据长度
//参数说明:obj:被测试的对象，len:长度
//返回值:0:过长  1:不过长

function chklen(obj,len) {
	if(obj.value.length>len) {
		obj.focus();
		return 0;
	}
	return 1;
}

//函数名:chkpwdconfirm
//功能介绍：检验密码是否一致
//参数说明:pwd:密码  confirmpwd:确认密码
//返回值:0:不一致   1:一致
function chkpwdconfirm(pwd,confirmpwd) {
	if(pwd.value!=confirmpwd.value)	{
		pwd.focus();
		return 0;
	}
	return 1;
}

//函数名:chkphone
//功能介绍：检查是合法phone
//参数说明:phone对象
//返回值:0:不合法 1:合法
function chkphone(phone) {
    var i=phone.value.length;
    var j = 0;
    var k = 0;
    var m = 0;
    while (k<i)
    {
        if (phone.value.charAt(k) == "(") {
        	j = j+1;
        	m = k;
        }
        k = k+1;
    }
    if (j==1)
    {
    	j=0;
    	k=m;
    	m=0;
	    while (k<i)
    	{
        	if (phone.value.charAt(k) == ")") {
        		j = j+1;
    	    }
        	k = k+1;
    	}
    	if(j!=0) {
        	return 1;
        }
        else {
        	phone.focus();
        	return 0;
        }
    }
    else {
    	phone.focus();
    	return 0;
    }
}

//函数名:chknum2(obj)
//功能介绍:检查是否是数字
//参数说明:obj要检查的对象
//返回值:0:不是 1:是
function chknum2(obj) {
	s=obj.value.replace("(","")
	s=s.replace(")","")
	if(isNaN(s)) {
		obj.focus();
		return 0;
	}
	else {
		obj.focus();
		return 1;
	}
}

//函数名:chkurl
//功能介绍：检查是合法ie地址
//参数说明:url对象
//返回值:0:不合法 1:合法
function chkie(ie2) {
    var i=ie2.value.length;
    var j = 0;
    var k = 0;
    var m = 0;
    s=ie2.value.search("http://")
        if (s!=-1)
        {
        	j = j+1;
        }
    if (j==1)
    {
    	j=0;
    	k=m;
    	m=0;
	    while (k<i)
    	{
        	if (ie2.value.charAt(k) == ".") {
        		j = j+1;
    	    }
        	k = k+1;
    	}
    	if(j!=0) {
        	return 1;
        }
        else {
        	ie2.focus();
        	return 0;
        }
    }
    else {
    	ie2.focus();
    	return 0;
    }
}