﻿	//*清除空格
	function Trim(s_tmp)
	{
		
		if (s_tmp == null || s_tmp.length == 0)
			return "";
			
		//剔除左边空格
		while (s_tmp.substring(0,1) == " ")
			s_tmp = s_tmp.substring(1);
		
		//剔除右边空格
		while (s_tmp.substring(s_tmp.length -1)== " ")
			s_tmp = s_tmp.substring(0,s_tmp.length - 1);
			
		return s_tmp;
	}

	//*得到字符长度(中文处理为两个字符)
	function strLength(str) 
	{
		var tmp,len;
		len = str.length;
		for (var i= 0;i < str.length;i++) 
		{ 
			tmp = str.charAt(i);
			tmp=escape(tmp);
			
			if (tmp.charAt(1) == 'u') 
				len ++;
		}
		return len;
	}

	//判断一个字符串是否是一个数字
	function isNumber(sval)
	{
		var ch;
		var digit = 0;
		
		var val = Trim(sval);

		if (val.length == 0 ) return true;
		//只有一个“＋”、“-”号的字符串，返回错误。
		ch = val.charAt(0);
		if ((ch == '+' || ch == '-') && val.length ==1)
		{
			return false;
		}
		for (var j = 0;j< val.length;j++)
		{	
			ch = val.charAt(j);
			if(j == 0 && (ch == '+' || ch == '-')) continue;	//正负号只能在字符串开始位置
			else if (ch == '.'&& j > 0 && digit ==0)	//该字符串中，只允许一个小数点，且小数点不能在字符串开始位置
			{
				digit = 1;
				continue;
			}
			else if (ch >= '0' && ch <= '9') continue;
			else
			{
				return false;
			} 
		}
		return true;
	}

	//年、月、日 的分隔符只能是 “-”或“.”。
	//年月必须是四位长度。
	function isShortDate(str) 
	{ 
		if (str == null || str == "")
			return true;
			
		var index=0 ,num1=0,num2=0,num3 = 0,tmp = "";
		var arr = new Array(3);
		
		for (var i = 0;i< str.length ;i++)
		{
			if (str.charAt(i) >= '0' && str.charAt(i) <= '9')
				continue;
			else if (str.charAt(i) == '-')
				num1++;
			else if (str.charAt(i) == '.')
				num2 ++;
			else
				num3 ++;
		}
		
		if (num3 > 0 || num1 + num2 != 2 || (num1== 1 && num2 == 1))
		{
//			alert("日期：" + str + " 格式错误!");	
			return false;
		}
			
		arr[0] = "";	arr[1] = "";	arr[2] = "";	
		for (var i = 0;i< str.length ;i++)
		{
			if (str.charAt(i) >= '0' && str.charAt(i) <= '9')
				tmp += str.charAt(i);
			else
			{
				arr[index] = tmp;
				index ++;
				tmp = "";
			}
		}
		arr[index] = tmp;
		
		if (arr[0].length != 4 )
		{
//			alert("日期：" + str + " 格式错误!");	
			return false;
		}
		
		if (isNaN(Date.parse(arr[1]+"/"+arr[2]+"/"+arr[0]))) 
		{
//			alert("日期：" + str + " 格式错误!");	
			return false; 
		}
			
		var dd = parseInt(arr[2],10); 
			// beware !! the computer handles monthes from 0 to 11. 
		var mm = parseInt(arr[1],10)-1; 
		var yy = parseInt(arr[0],10); 
		var date = new Date(yy,mm,dd); 
		//let's now compare the user's entry with what the computer understand of it 
		if (dd!=date.getDate() || mm!=date.getMonth() || yy!=date.getFullYear()) 
		{
//			alert("日期：" + str + " 格式错误!");	
			return false; 
		}
		return true; 
	}	
	
	function isDateTime(str) 
	{ 
		if (str == null || str == "")
			return true;
			
		var index=0 ,num1=0,num2=0,num3 = 0,tmp = "";
		var num4 = 0;
		var arr = new Array(6);
		
		for (var i = 0;i< str.length ;i++)
		{			
			if (str.charAt(i) >= '0' && str.charAt(i) <= '9')
				continue;
			if(str.charAt(i) == ' ')
				continue;
			else if (str.charAt(i) == '-')
				num1++;
			else if (str.charAt(i) == '.')
				num2 ++;
			else if (str.charAt(i) == ':')
				num4 ++;
			else 
				num3 ++;
		}
		
		
		
		if (num3 > 0 || num1 + num2 != 2 || (num1== 1 && num2 == 1) || num4 < 1)
		{
//			alert("日期：" + str + " 格式错误!");	
			return false;
		}
			
		arr[0] = "";	arr[1] = "";	arr[2] = "";	
		arr[3] = "";    arr[4] = "";	arr[5] = "";
		for (var i = 0;i< str.length ;i++)
		{
						
			if (str.charAt(i) >= '0' && str.charAt(i) <= '9')
				tmp += str.charAt(i);
			else
			{
				arr[index] = tmp;
				index ++;
				tmp = "";
			}
		}
		arr[index] = tmp;
		
		//alert(arr[0]+"   "+arr[1]+"   "+arr[2]+"   "+arr[3]+"   "+arr[4]+"   "+arr[5])
		
		if (arr[0].length != 4 )
		{
//			alert("日期：" + str + " 格式错误!");	
			return false;
		}
		
		if(arr[3] == "")
		{
			return false;
		}
		
		if(arr[4] == "")
		{
			return false;
		}
		
		if(arr[5] == "")
		{
			if(num4 == 2)	return false;
			arr[5] = "0";
		}
		
		
	
		if (isNaN(Date.parse(arr[1]+"/"+arr[2]+"/"+arr[0]+" "+arr[3]+":"+arr[4]+":"+arr[5]))) 
		{
//			alert("日期：" + str + " 格式错误!");	
			
			return false; 
		}		
		
			
		var dd = parseInt(arr[2],10); 
			// beware !! the computer handles monthes from 0 to 11. 
		var mm = parseInt(arr[1],10)-1; 
		var yy = parseInt(arr[0],10); 
		
		var hours = parseInt(arr[3],10);
		
		var minutes = parseInt(arr[4],10);
		
		var seconds = parseInt(arr[5],10);
		
		var date = new Date(yy,mm,dd,hours,minutes,seconds); 
		
		//let's now compare the user's entry with what the computer understand of it  
		if (dd!=date.getDate() || mm!=date.getMonth() || yy!=date.getFullYear() || hours!=date.getHours() || minutes!=date.getMinutes() || seconds!=date.getSeconds()) 
		{
//			alert("日期：" + str + " 格式错误!");	
			return false; 
		}
		return true; 
	}	

