/*
 
s: string value
bag: char array

isEmpty(s) : null
isWhitespace (s) : " \t\n\r"
isEmail (s)
isCharsInBagEx (s, bag) : return a bad char


*/

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 isSingleQuotes(s)
 {
 	var badChar = "'"; 
 	errorChar = isCharsInBagEx( s, badChar)
	    	if (errorChar != "" )
		{
		return false;
		} 	
	
return true;
}
function isEmail (s)
{
   	// is s Empty?
    if (isEmpty(s))
	{
		alert("This blank could not be empty");	
		return false;
	}
  return isEmailaddress(s)
}

function isEmailaddress (s)
	//is s contain whitespace
{    if (isWhitespace(s))
	{
		alert("Blank space is not allowed");	
		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("Digits of email should be less than 30");
		return false;
	}
	if (len!=0)
         {
	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");
		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");
			return false;
		}
	}}

	if ( !isCharsInBag( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@"))
	{
		alert("Email address contains ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@ only\n" + "please re-enter your email" );
		return false;
	}
	//is s contain invalid characters
	/*
	var badChar = "><,[]{}?/+=|\\'\":;!#$%^&()`"; 
	if ( isCharsInBag( s, badChar))
	{
		alert("Please do not enter " + badChar + "\n" );
		alert("Please try again" );
		return false;
	}
	*/
	return true;
}

function isfloat(s,string) 
{
var len = s.length;
  if(!isCharsInBag (s, "0123456789."))
  {
    alert(string+"Please enter a valid decimal");
    return false;
  }
  pos1 = s.indexOf(".");
  pos3 = s.lastIndexOf(".");
//check '.' is not first or last character
	if (pos1 == 0)  
	{
		alert(string+"Please enter a valid decimal");
		return false;
	}
	else
	{
		//check @. or .@
		if ( pos1 != pos3 )  //find two @
		  {
			alert(string+"Please enter a valid decimal");
			return false;
		}
	}

  return true;   
}

function CheckSeach()
{
  if (!isfloat(form1.length.value,""))
   {  return false;    }
  if (!isfloat(form1.high.value,""))
   {   return false;}
  if (!isfloat(form1.diameter.value,""))
   {  return false;}
return true;
}

function isNumber(s,string) 
{
var len = s.length;
  if(!isCharsInBag (s, "0123456789"))
  {
    alert(string+"只能输入数字!");
    return false;
  }
   return true;   
}
function CheckNumber()
{
  if (!isNumber(form1.length.value,"长"))
   {  return false;    }
  if (!isNumber(form1.high.value,"高"))
   {   return false;}
  if (!isNumber(form1.diameter.value,"宽/直径"))
   {  return false;}
return true;
}
function Editgoods()
{ if (isEmpty(form1.Goodsname.value))
	{
		alert("名称不能为空，请输入!");	
		return false;
	}
  if (isEmpty(form1.goodsnumb.value))
	{
		alert("商品号码不能为空，请输入!");	
		return false;
	}

  if (isEmpty(form1.imagepath.value))
	{
		alert("小图片不能为空，请输入!");	
		return false;
	}
  if (isEmpty(form1.Largerimagepath.value))
	{
		alert("大图片不能为空，请输入!");	
		return false;
	}
  if (!CheckNumber())
  {	return false;}
  if (!isNumber(form1.arrangement.value,"次序"))
   {  return false;    }

return true;
}

 function isKeyword(s)
 {
 	var a, errorChar;
	var badChar = "><,[]{}?/+=|\\'\":;~!@#$%^&()`"; 
	// is s Empty?
   	if ( isEmpty(s) )
	{
		a = confirm("If you enter nothing in this blank, you will get too many mataches.\n\nAre you sure you want continue?");	
		if (a == false)
			return false;
	}
	else
	{
		//is s contain invalid characters
		//Validate the user name
		errorChar = isCharsInBagEx( s, badChar)
	    	if (errorChar != "" )
		{
			alert("Please do not enter " + errorChar + " in the keywords, \n\nplease try again" );
			return false;
		} 	
	}
	return true;
 }
function Editpage()
{ 	if (isEmpty(form1.urlname.value))
	{
		alert("URL不能为空，请输入!");	
		return false;
	}
	if (!isSingleQuotes(form1.urlname.value))
	{
	      alert("URL请不要输入单引号\n\n请重新输入!" );
	      return false;	
        }
		
  if (isEmpty(form1.title.value))
	{
		alert("标题不能为空，请输入!");	
		return false;
	}
	if (!isSingleQuotes(form1.title.value))
	{
	      alert("标题请不要输入单引号\n\n请重新输入!" );
	      return false;	
        }

  if (isEmpty(form1.contents.value))
	{
		alert("内容不能为空，请输入!");	
		return false;
	}
   	if (!isSingleQuotes(form1.contents.value))
	{
	      alert("内容请不要输入单引号\n\n请重新输入!" );
	      return false;	
        }
  return true;
}


function Editdesigns()
{ 	if (isEmpty(form1.designsname.value))
	{
		alert("名称不能为空，请输入!");	
		return false;
	}
	if (!isSingleQuotes(form1.designsname.value))
	{
	      alert("名称请不要输入单引号\n\n请重新输入!" );
	      return false;	
        }
		
//  if (isEmpty(form1.imagepath.value))
//	{
//		alert("图片路径不能为空，请输入!");	
//		return false;
//	}
	if (!isSingleQuotes(form1.imagepath.value))
	{
	      alert("图片路径请不要输入单引号\n\n请重新输入!" );
	      return false;	
        }
  if (!isNumber(form1.arrangement.value,"次序"))
   {  return false;    }

   return true;
}
function postdone()
{
if (isEmpty(form2.author.value))
	{
		alert("Please enter your name");	
		return false;
	}
//	if (!isSingleQuotes(form2.author.value))
//	{
	      //alert("Please do not enter single quotes in your name, \n\nplease try again");
//	      return false;	
//        }
 
if (!isEmailaddress(form2.Email.value))
{return false;
}
if (isEmpty(form2.title.value))
	{
		alert("Please enter your title");	
		return false;
	}
//	if (!isSingleQuotes(form2.title.value))
//	{
	 //     alert("Please do not enter single quotes in the title, \n\nplease try again" );
	 //     return false;	
    //    }
if (isEmpty(form2.content.value))
	{
		alert("Please enter your content");	
		return false;
	}
//	if (!isSingleQuotes(form2.content.value))
//	{
//	      alert("Please do not enter single quotes in the content, \n\nplease try again" );
//	      return false;	
//        }

return true;
}

function  submitEmail(){
 if (!isEmail(emailsubmit.emailaddress.value))
         {return false;
        }
      windowOpener('emailsubmit.asp');
      
}

function FeedBack()
{ if (isEmpty(form1.FirstName.value))
	{
		alert("Please enter your first name");	
		return false;
	}
  if (isEmpty(form1.LastName.value))
	{
		alert("Please enter your last name");	
		return false;
	}

  if (isEmpty(form1.City.value))
	{
		alert("Please enter city");	
		return false;
	}
  if (isEmpty(form1.EmailAddress.value))
	{
		alert("Please enter your Email address");	
		return false;
	}
  
return true;
}

