 function isEmpty(s) { 
 return ((s == null) || (s.length == 0));
 }

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s) {
  var whitespace = " \t\n\r";
  // Is s empty?
  if (isEmpty(s)) return true;
  // Search through string's characters one by one
  // until we find a non-whitespace character.
  // When we do, return false; if we don't, return true.
  var i;		
  for (i = 0; i < s.length; i++)
  {   
   // Check that current character isn't whitespace.
   var c = s.charAt(i);
   if (whitespace.indexOf(c) != -1) 
      continue;
   else
      return false;
    }
 // All characters are whitespace.
 return true;
}
//判断某一个是不是EMAIL
function isEmail(s) {
// there must be >= 1 character before @, so we
// start looking at character position 1
// (i.e. second character)
  var i = 1;
  var sLength = s.length;
 // look for @
  while ((i < sLength) && (s.charAt(i) != "@"))
  { 
   i++;
  }

  if ((i >= sLength) || (s.charAt(i) != "@")) return false;
  else i += 2;
// look for .
  while ((i < sLength) && (s.charAt(i) != "."))
  { 
    i++;
  }
// there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
  else return true;
}
//判断一串是不是有效EMAIL
function isEmailList(mailList){
	var i;
	var num;//串中是否有有效邮箱
	num=0;
	while(mailList.length>0){
		i=mailList.indexOf(',');
		if(i>=0)
			tmpEmail=mailList.substr(0,i);
		else
			tmpEmail=mailList;
		if(!isWhitespace(tmpEmail))
			if(!isEmail(tmpEmail)){
				alert("有无效EMAIL："+tmpEmail);
				return false;
		}
			else
				num=num+1;
		mailList=mailList.substr(i+1);
		if(i<0)
			break;
		//alert(mailList);
	}
	if(num>0)
		return true;
	else
		return false;
}
function warnEmpty (theField, s)
{ 	alert(s);
    theField.focus();
   return false;
}

function warnInvalid (theField, s)
{	alert(s);
    theField.focus();
    theField.select();
    return false;
}

function checkString (theField,s)
{   // Make sure the field exists before completing the test
  if (theField == null) return true;
  if (isWhitespace(theField.value))
  return warnEmpty (theField, s);
		else return true;
}

function checkValueChar (theField,s)
	{  
		if (!isValueChar(theField.value))
		   return warnEmpty(theField, s);
		else return true;
	}

function isValueChar (s) {
    var badWords="'<`\/";
//	var badWords ="\t\n\r><,[]{}?/=^+|\\'\":;~!@#$%^&()`";

		// 是否有无效效字符
		var i;		
		
		for (i = 0; i < s.length; i++)
		{   
			var c = s.charAt(i);
			if (badWords.indexOf(c) > -1)
				return false;				
			else
				continue;
		}
	// All characters are value.
		   return true;
}

function checkEmail (theField,s)
{
 if (!isEmail(theField.value))
   return warnInvalid (theField, s);
 else return true;
}

function isNumberForDxh(s)
{
 var digits = "0123456789.";
 var i = 0;
 var sLength = s.length;
 while ((i < sLength))
 { 
   var c = s.charAt(i);
   if (digits.indexOf(c) == -1) return false; 
      i++;
 }
 return true;
}

function checkSelect(theSelect,s)
{
  if (theSelect.options[theSelect.selectedIndex].value != "")	return true;
  else
  {
    theSelect.focus();
    warnEmpty(theSelect,s);
    return false;
  }
} 

function checkNumber (theField,s)
{
  if (!isNumberForDxh(theField.value))
   {    return warnEmpty (theField, s);
   }
  if (isWhitespace(theField.value))
    return warnEmpty (theField, s);
  else return true;	
}

function checkzip (theField,s)
{
  var ss=theField.value;
  var digits = "0123456789";
  var i = 0;
  var sLength = ss.length;

		if(sLength<6)
		    return warnInvalid (theField, s);
		    
		while ((i < sLength))
		{ 
			var c = ss.charAt(i);
			if (digits.indexOf(c) == -1) 
					return warnInvalid (theField, s);

			i++;		
		}
		
		return true;
	}

	function checkPhone(theField,s)
	{
		var ss=theField.value;
		var digits = "0123456789-";
		var i = 0;
		var sLength = ss.length;

		while ((i < sLength))
		{ 
			var c = ss.charAt(i);
			if (digits.indexOf(c) == -1) 
				return warnInvalid (theField, s);

			i++;
		}

		c = "--";
		if (ss.indexOf(c) != -1) 
			return warnInvalid (theField, s);

		return true;
	}

	function checkPassWord(theField)
	{
		if (theField.value.length < 4)
		{
			alert("密码长度小于4");
			theField.focus();	
			return false;
		}
		else return true;		
	}
	
	function checkFigure (theField,s)
	{
		var vv,ww,xx;
		vv=theField.value;
		var iPos=vv.indexOf(".");
		if(iPos==0) vv="0"+vv;
		if(iPos==(vv.length - 1))  vv=vv+"0";

  		xx=parseFloat(vv);
  		ww=xx.toString(10);
  		var i=vv.length - ww.length;
  		var j=0;
  		if(i>0)	{
  			iPos=ww.indexOf(".");
  			if(iPos==-1){ ww=ww+".";  		 
  			              i=i-1;}
  			for(j=0;j<i;j++)
  				ww=ww+"0";
  		}

  		if(ww!=vv) 
  		    return warnInvalid (theField, s);
  		 
  		return true;  		
	}

function parseYMD(theYear,theMonth,theDay) {
  theYear=parseNum(theYear)
  theMonth=parseNum(theMonth)
  theDay=parseNum(theDay)
  if ((theYear < 1900) || (theYear > 3000)){
    return 1
  }
  if (theMonth < 1 || theMonth > 12){
    return 2
  }
  if ((theMonth==1 || theMonth==3 || theMonth==5 || theMonth==7 || theMonth==8 || theMonth==10 || theMonth==12) &&
      (theDay <1 || theDay > 31)
     ){
    return 3
  }
  if ((theMonth==4 || theMonth==6 || theMonth==9 || theMonth==11) &&
      (theDay <1 || theDay > 30)
     ){
    return 3
  }
  if (theYear%400==0 || (theYear%4==0 && theYear%100!=0)){  //闰年
    if (theMonth==2 && (theDay <1 || theDay > 29) )
      return 3
  }
  else  //平年
    if (theMonth==2 && (theDay <1 || theDay > 28) )
      return 3
  return 0
}

function parseNum(theNum){
  if (theNum.substring(0,1)==0)
    theNum=theNum.substring(1)
  return theNum
}

function isInvalidDate(theDate,separator){
  default_style=1
  if (theDate.length>10 || theDate.length<8)
    return true
  idx1=theDate.indexOf(separator)
  if (idx1==-1)
    return true
  idx2=theDate.indexOf(separator,idx1+1)
  if (idx2==-1)
    return true
  if (isInvalidDate.arguments.length>2)
  	default_style=isInvalidDate.arguments[2]
  if (default_style<1 || default_style>9){
  	alert("传入参数有误！请检查。")
	return true
  }
  if (default_style==1){
  theYear=theDate.substring(0,idx1)
  theMonth=theDate.substring(idx1+1,idx2)
  theDay=theDate.substring(idx2+1)
  }
  if (default_style==2){
  theMonth=theDate.substring(0,idx1)
  theDay=theDate.substring(idx1+1,idx2)
  theYear=theDate.substring(idx2+1)
  }
  if (theDay.length>2)
    return true
  if (parseYMD(theYear,theMonth,theDay)>0)
    return true
  else
    return false
}
