function Judge(strZip) {
	//trim all space
	strZip = TrimAllSpace(strZip)
	
	if (strZip.length == 5 && AllDigit(strZip))
		return true;
	else if (strZip.length == 6 && OneCharOneDigit(strZip))
		return true;
	else if ((strZip.length == 9 || strZip.length == 10)  && AllDigit(strZip.substring(0, 5)))
		return true;
	else
		return false;
}

function AllDigit(strZip) {
	var i = 0;
	while (strZip.charAt(i) >= '0' && strZip.charAt(i) <= '9')
		i++;
		
	return (i == strZip.length);
}

function OneCharOneDigit(strZip) {
	
	var i = 0;
	strZip = strZip.toUpperCase();
	while (i < strZip.length)
		if (i % 2 == 0 && (strZip.charAt(i) >= 'A' && strZip.charAt(i) <= 'Z')) {
			i++;
			continue;
		}
		else if (i % 2 == 1 && (strZip.charAt(i) >= '0' && strZip.charAt(i) <= '9')) {
			i++;
			continue;
		}
		else
			return false;

		
	return (i == strZip.length);
}


function TrimAllSpace(str) {
	var i = str.indexOf(" ");

	while (i != -1) {
		if (i == 0)
			str = str.substring(1, str.length);
		else if (i == str.length - 1)
			str = str.substring(0, str.length - 1);
		else
			str = str.substring(0, i) + str.substring(i + 1, str.length);
	
		i = str.indexOf(" ");
	}
			
	return str;
}

function GetZip(strZips, strShippingAddressID) {
	return strZips.indexOf(strShippingAddressID) + strShippingAddressID.length + 1;
	
}

function ValidateDate(intMonth, intYear)
{
	if (intMonth > 12 || intMonth < 1)
		intErrorPos = 1;
	else if (intYear > 2099 || intYear < 2000)
		intErrorPos = 2;
	else
		intErrorPos = 0;
		
	
	if (intErrorPos > 0)
		return false;
	else
	{
		return true;
	}	
}

function ValidateMonth(intMonth)   //add by hs
{
	if (intMonth > 12 || intMonth < 1)
		return false;
	else
		return true;
}

function ValidateYear(intYear)    //add by hs
{
	if (intYear > 2099 || intYear < 2000)
		return false;
	else
		return true;
}


function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	
	return sString;
}

function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}

function IsValidEmail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(email)); 
}

function IsValidDate(strDate)
{
	if (strDate == "")	
		return true;

	var regDate = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
	return regDate.test(strDate);
}

function validateZIP(field, CountryID) 
{
	var valid = "0123456789-";
	var hyphencount = 0, entry, strlen;
	var allChar = "ABCDEFGHIJKLMNOPQRSTVUWXYZ";
	var allNum = "0123456789";
	
	switch (parseInt(CountryID, 10))
	{
		case 107:
			if (field.length!=5 && field.length!=10) 
			{
				alert("Please enter your 5 digit or 5 digit+4 zip code.");
				return false;
			}
	
			for (var i=0; i < field.length; i++) 
			{
				temp = "" + field.substring(i, i+1);
				if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") 
				{
					alert("Invalid characters in your zip code.  Please try again.");
					return false;
				}
				
				if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
				{
					alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
					return false;
				}
			}
			
			break;
			
		case 19:
			entry = field.replace(/\s/, ""); 
			strlen = entry.length; 
			entry = entry.toUpperCase();    

			if ((strlen != 6) || (allChar.indexOf(entry.charAt(0)) < 0) || (allNum.indexOf(entry.charAt(1)) < 0) ||
				(allChar.indexOf(entry.charAt(2)) < 0) || (allNum.indexOf(entry.charAt(3)) < 0) || 
				(allChar.indexOf(entry.charAt(4)) < 0) || (allNum.indexOf(entry.charAt(5)) < 0)) 
			{
				alert("Invalid zip code. Please try again.");
				return false;
			}

			break;
		
		case 65:
		
			break;	
		
	}
	
		
	return true;
}

function isCharsInBag (s, bag)
{  
var i;
   for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
}

function BeDate(strDate)
{
	if(IsValidDate(strDate))
	{
		return true;
	}
	else
	{
		alert("The Date is incorrect.");
		return false;
	}

}

function trim(varIn)
{
   var varOut = "" 
   var begin = 0
   
    if(!varIn)
     {
      // string is null, so nothing to do
     }
    else // string has at least one character
     {
      for(intI=0; intI < varIn.length; intI++)
       {
        if(varIn.charAt(intI) != " ")
         {
          //first non-space char found so return
          //string from this character forward
          //varOut = varIn.substring(intI)
          begin=intI
          break
         }
       }
	
      for(intI=varIn.length; intI >0 ; intI--)
       {
         if(varIn.charAt(intI-1) != " ")
          {
           varOut = varIn.substring(begin,intI)
           break
          }
       }
		
     }
     return varOut
}
