/*
s: string value
bag: char array

isEmpty(s) : null
isWhitespace (s) : " \t\n\r"
isUserName(s) 
isName(s)
isEmail (s)
isTelNumber(s)
isCardNumber(s) 
isAddress(s)
isCharsInBag (s, bag)
isKeyword(s)
isItemNum(s)
isPassword(s)
isInt(s, item)
isIntEx(s, item, len， bCompare)

isCharsInBagEx (s, bag) : return a bad char

isUserNameOld(s) 
*/

function isEmpty(s)
{  
	return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{  
  var whitespace = " \t\n\r";
  var i;
  // 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.
   for (i = 0; i < s.length; i++)
   {   
       // Check that current character isn't whitespace.
       var c = s.charAt(i);
       if (whitespace.indexOf(c) >= 0) 
	   {
		  return true;
	   }
   }

   // All characters are whitespace.
   return false;
}

function isCharsInBagEx (s, bag)
{  
  var i,c;
  // Search through string's characters one by one.
  // If character is in bag, append to returnString.
  for (i = 0; i < s.length; i++)
  {   
        c = s.charAt(i);
	if (bag.indexOf(c) > -1) 
        return c;
  }
  return "";
}

function isCharsInBag (s, bag)
{  
  var i;
  // Search through string's characters one by one.
  // If character is in bag, append to returnString.

  for (i = 0; i < s.length; i++)
  {   
      // Check that current character isn't whitespace.
      var c = s.charAt(i);
      if (bag.indexOf(c) == -1) return false;
  }
  return true;
}

function isRegisterUserName(s)
{
 	var errorChar;
	var badChar = "><,[]{}?/+=|\\'\":;~!@#$%^&()`"; 
 	if (isEmpty(s))
 	{
 		alert("Please enter a username!");
 		return false;
 	}
 	//is s contain whitespace
   	if ( isWhitespace(s) )
	{
		alert("输入的用户名中不能包含空格符，请重新输入！");	
		return false;
	}
	//is s contain invalid characters
	//Validate the user name
	errorChar = isCharsInBagEx( s, badChar)
    	if (errorChar != "" )
	{
		alert("您输入的用户名" + s+"是无效的用户名,\n\n请不要在用户名中输入字符" + errorChar + "!\n\n请重新输入合法的用户名！" );
		return false;
	} 	
	
	return true;
}

function isEmail (s)
{
   	// is s Empty?
    if (isEmpty(s))
	{
		alert("Please enter a valid email address!");	
		return false;
	}
	//is s contain whitespace
    if (isWhitespace(s))
	{
		alert("Please enter a valid email address!");	
		return false;
	}

   // there must be >= 1 character before @, so we
   // start looking at character position 1
   // (i.e. second character)
   var i = 1;
   var len = s.length;

	if (len > 30)
	{
		alert("email地址长度不能超过30位!");
		return false;
	}
	
	pos1 = s.indexOf("@");
	pos2 = s.indexOf(".");
	pos3 = s.lastIndexOf("@");
	pos4 = s.lastIndexOf(".");
	//check '@' and '.' is not first or last character
	if ((pos1 <= 0)||(pos1 == len)||(pos2 <= 0)||(pos2 == len))  
	{
		alert("Please enter a valid email address!");
		return false;
	}
	else
	{
		//check @. or .@
		if( (pos1 == pos2 - 1) || (pos1 == pos2 + 1) 
		  || ( pos1 != pos3 )  //find two @
		  || ( pos4 < pos3 ) ) //. should behind the '@'  		
		{
			alert("Please enter a valid email address!");
			return false;
		}
	}

	if ( !isCharsInBag( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@"))
	{
		alert("email地址中只能包含字符ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@\n" + "请重新输入" );
		return false;
	}
	//is s contain invalid characters
	/*
	var badChar = "><,[]{}?/+=|\\'\":;!#$%^&()`"; 
	if ( isCharsInBag( s, badChar))
	{
		alert("请不要在email地址中输入字符 " + badChar + "\n" );
		alert("请重新输入" );
		return false;
	}
	*/
	return true;
}
function isTelNumber(s, item)
{

	//is s contain invalid characters
	var goodChar = "0123456789-*()"; 
   //Validate the user name
   if (isEmpty(s))
   {    alert("电话号码不能为空！");
		return false;
   }

   if ( !isCharsInBag( s, goodChar))
	{  
	    alert(item+"号码里只能有0123456789-*()！");
		return false;
	}
	//check user length 
	if ((s.length>12)||(s.length<6))
	{
		alert(item+"号码不能超过12位也不能少于6位！");
		return false;
	}
	return true;
}
function isBPNumber(s)
{
	//is s contain invalid characters
	var goodChar = "0123456789-*()"; 
   //Validate the user name
   if (!isEmpty(s)){
		if ( !isCharsInBag( s, goodChar)){  
			alert("寻呼机号码里只能有0123456789-*()！");
			return false;
		}
		//check user length 
		if ((s.length>15)||(s.length<10)){
			alert("寻呼机号码不能超过15位也不能少于10位！");
			return false;
		}
   }
   return true;
}

function isMPNumber(s)
{
	//is s contain invalid characters
	var goodChar = "0123456789-*()"; 
   //Validate the user name
   if (!isEmpty(s)){
		if ( !isCharsInBag( s, goodChar)){  
			alert("移动电话号码里只能有0123456789-*()！");
			return false;
		}
		//check user length 
		if (s.length!=11){
			alert("移动电话号码位数不对！");
			return false;
		}
   }
   return true;
}

function isAddress(s)
{
   	// is s Empty?
   	if ( isEmpty(s) )
	{
		alert("联系地址不能为空，请重新输入！");	
		return false;
	}
	//is s contain invalid characters
	var badChar = "><,[]{}?/+=|\\'\":;~!@$%^&`"; 
	var len = s.length;
	var errorChar

   	errorChar = isCharsInBagEx( s, badChar)
   	if (errorChar != "" )
	{
	    alert("联系地址中不能含有字符 "+badChar + " !");
		return false;
	}
	//check user length 
	if ((len>100)||(len<5))
	{
		alert("联系地址不能超过50个汉字也不能少于五个汉字！");
		return false;
	}
	return true;
}

function isInt(s, item)
{
  if (isEmpty(s))
  {
	alert(item + "不能为空，请输入！");
	return false;
  }
      	
  var validChar = "0123456789"; 
  if (!isCharsInBag(s, validChar))
  {
	alert("您输入的" + item + s +"是无效的" + item + "，\n\n请输入合法的" + item + "！");
	return false;
  }

  return true;
}


function isCardNumber(s,type) 
{
//身份证号码验证
  if (s == "")
  {
     alert("证件号码不能为空！请填写！");
	return false;
  }
  
  if (!isInt(s,"证件号码"))
  {  
     return false;
  }
   
  if (type==1||type==2||type==5)
  {  
     if ((type==1&&s.length!=15)||(type==2&&s.length!=18)||(type==5&&s.length!=10))
     {
       alert("请检查一下您输入的身份证号码位数是否正确！");
	   return false;
     }
  }
  else 
 { 
     if ((s.length<10)||(s.length>20))
     {
       alert("请检查一下您输入的证件号码位数是否正确！");
	   return false;
     }
 }

  return true;
}

 function isKeyword(s)
 {
 	var a, errorChar;
	var badChar = "><,[]{}?/+=|\\'\":;~!@#$%^&()`"; 
	// is s Empty?
   	if ( isEmpty(s) )
	{
		a = confirm("如果您不输入任何查询关键字，您可能会得到太多的查询结果。\n\n您确定要进行查询吗？");	
		if (a == false)
			return false;
	}
	else
	{
		//is s contain invalid characters
		//Validate the user name
		errorChar = isCharsInBagEx( s, badChar)
	    	if (errorChar != "" )
		{
			alert("请不要在查询关键字中输入字符" + errorChar + "\n\n请重新输入！" );
			return false;
		} 	
	}
	return true;
 }
 
 function isItemNum(s)
 {
 	if (isEmpty(s))
	{
		alert("请输入物品编号进行查询！");
 		return false;
	}
	var validChar = "0123456789"; 
	if (!isCharsInBag(s, validChar))
	{
		alert("您输入的物品编号" + s +"是无效的物品编号，\n\n请输入合法的物品编号！");
 		return false;
	}
	return true;		
 }
 
 function isUserName(s)
 {
 	var errorChar;
	var badChar = "><,[]{}?/+=|\\'\":;~!#$%()`"; 
 	if (isEmpty(s))
 	{
 		alert("请输入用户名！");
 		return false;
 	}

    if ((s.length<2)||(s.length>18))
    {
	   	alert("用户名长度为2-18位！");
 		return false;
    }
 	//is s contain whitespace
   	if ( isWhitespace(s) )
	{
		alert("输入的用户名中不能包含空格符，请重新输入！");	
		return false;
	}
	//is s contain invalid characters
	//Validate the user name
	errorChar = isCharsInBagEx( s, badChar)
    	if (errorChar != "" )
	{
		alert("您输入的用户名" + s+"是无效的用户名,\n\n请不要在用户名中输入字符" + errorChar + "!\n\n请重新输入合法的用户名！" );
		return false;
	} 	
	
	return true;
 }
 
 function isName(s)
 {
 	var errorChar;
	var badChar = "><,[]{}?/+=|\\'\":;~!#$%()`"; 
 	if (isEmpty(s))
 	{
 		alert("请输入姓名！");
 		return false;
 	}

    if ((s.length<=1)||(s.length>21))
    {
	   	alert("姓名长度要大于1位小于20位！");
 		return false;
    }
 	//is s contain whitespace
   	if ( isWhitespace(s) )
	{
		alert("输入的姓名中不能包含空格符，请重新输入！");	
		return false;
	}
	//is s contain invalid characters
	//Validate the user name
	errorChar = isCharsInBagEx( s, badChar)
    	if (errorChar != "" )
	{
		alert("您输入的姓名" + s+"是无效的姓名,\n\n请不要在姓名中输入字符" + errorChar + "!\n\n请重新输入合法的姓名！" );
		return false;
	} 	
	
	return true;
 }

function isSex(s)
{
   if (s=='选')
   {
      alert("性别信息不能为空，请你选择");
      return false;
   }
   return true;
}

function isCity(s)
{
   if (s=='选择')
   {
      alert("城市信息不能为空，请你选择");
      return false;
   }
   return true;
}

function isPassword(s)
{
  if (isEmpty(s))
  {
	alert("Please enter a password!");
	return false;
  }
//is s contain whitespace
  if ( isWhitespace(s) )
  {
	alert("password contain whitespace!");	
	return false;
  }

  if ((s.length>16)||(s.length<4))
  {
 	alert("Password lenght must <16 and >3！");
	return false;
  }
  return true;
}

function isInt2(s, item)
{
  var validChar = "0123456789"; 
  if (!isCharsInBag(s, validChar))
  {
	alert("您输入的" + item + s +"是无效的" + item + "，\n\n请输入合法的" + item + "！");
	return false;
  }

  return true;
}

function isIntEx(s, item, len, bCompare)
{
  if (isEmpty(s))
  {
	alert(item + "不能为空，请输入！");
	return false;
  }
      	
  var validChar = "0123456789"; 
  if (!isCharsInBag(s, validChar))
  {
	alert("您输入的" + item + s +"是无效的" + item + "，\n\n请输入合法的" + item + "！");
	return false;
  }
  
  if (bCompare == "=") 
  {
    if (s.length != len)
    {
	alert("您输入的" + item + s +"是无效的" + item + "，\n\n必须等于" + len + "位！");
	return false;
    }
  }
  else if (bCompare == "<")
  {
    if (s.length >= len) 
    {
	alert("您输入的" + item + s +"是无效的" + item + "，\n\n必须小于" + len + "位！");
	return false;
    }
  }
  
  return true;
}

function isValidString(s, des)
{
 	var errorChar;
	var badChar = "><,[]{}?/+=|\\'\":;~!@#$%^&()`"; 
 	if (isEmpty(s))
 	{
 		alert("请输入"+ des +"！");
 		return false;
 	}
	//is s contain invalid characters
	//Validate the user name
	errorChar = isCharsInBagEx( s, badChar)
    	if (errorChar != "" )
	{
		alert("您输入的" + des +"是无效的"+des +",\n\n请不要在"+des+"中输入字符" + errorChar + "!\n\n请重新输入合法的"+des+"！" );
		return false;
	} 	
	
	return true;
 }
 
function isPrice(s, item)
{
  if (isEmpty(s))
  {
	alert(item + "不能为空，请输入！");
	return false;
  }
      	
  var validChar = "0123456789."; 
  if (!isCharsInBag(s, validChar))
  {
	alert("您输入的" + item + s +"是无效的" + item + "，\n\n请输入合法的" + item + "！");
	return false;
  }
  if (s.indexOf(".") == -1)
  	return true;
  	
  if (s.indexOf(".") != s.lastIndexOf("."))
  {
	alert("您输入的" + item + s +"是无效的" + item + "，\n\n请输入合法的" + item + "！");
	return false;
  }
  var opart = s.substr(s.indexOf(".")+1)
  if (parseInt(opart) != 0)
  {
	alert(item+"的最小单位是1元！");
	return false;
  }
  return true;
}

function checkContent(strInput,length,Item){
    if (isEmpty(strInput))
 	{
 		alert("您输入的"+Item+"为空！");
 		return false;
 	}
 	
    if (strInput.length > length) {
        alert("您输入的"+Item+"超过了"+length+"位");
      return false;
  }

   //ind2=isCharsInBagEx(strInput, "/");
   ind3=isCharsInBagEx(strInput, "<");
   ind4=isCharsInBagEx(strInput, ">");
   ind5=isCharsInBagEx(strInput, "'");
   if (ind3 || ind4 || ind5) {
      alert("请勿使用 <, >, '等特殊字符 ");
      return false;
  }
  return true;
} 

function NewWindow(url,windowName,width,height)
{ 
	var windowFeature="width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1";
	var windowHandle = window.open(url, windowName, windowFeature);
	windowHandle.focus();
}

function AddToCart(quantity,pieceCode,action){
    var strHref;
	if (!isInt(quantity,"数量")) 
	   return false;
    if ((pieceCode.indexOf("00")==0)&&(quantity>1)) {
	   alert("本商品库存仅此一件，售完即无，请输入数量为1。");
	   return false;
       }
	alert("您已经把商品号为"+pieceCode+"的商品添加进购物车了");
 	strHref="../sale/mycart.asp?quantity="+quantity+"&piececode="+pieceCode+"&act="+action;
	NewWindow(strHref,'cart',600,300);
}

 function isRightInput(s)
 {
 	var a, errorChar;
	var badChar = "><,[]{}?/+=|\\'\":;~!@$%^&()`"; 
	// is s Empty?
		//is s contain invalid characters
		//Validate the user name
		errorChar = isCharsInBagEx( s, badChar)
	    	if (errorChar != "" )
		{
			alert("请不要输入字符" + errorChar + "\n\n请重新输入！" );
			return false;
		} 	

	return true;
 }
