var digits = "0123456789";
var reInteger = /^\d+$/
var digitsInUSPhoneNumber = 10;
var phoneNumberDelimiters = "()- ";
var validUSPhoneChars = digits + phoneNumberDelimiters; 

function isInteger (s)
{    
    return reInteger.test(s)
}
 
function isEmailAddr(email)

	{

	  var result = false

	  var theStr = new String(email)

	  var index = theStr.indexOf("@");

	  if (index > 0)

	  {

		var pindex = theStr.indexOf(".",index);

		if ((pindex > index+1) && (theStr.length > pindex+1))

		result = true;

	  }

	  return result;

	}

function isUSPhoneNumber (s)
{   
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not 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) returnString += c;
    }

    return returnString;
}

function minLength(address)
{
		if (address.length < 4)
   		{
   		   return (false);
   		}

		return (true);
}

function isZip(s) 

{



     // Check for correct zip code

     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);



     if (!reZip.test(s)) {

          //alert("Zip Code Is Not Valid");

          return false;

     }



return true;

}

function checkUSPhone (fieldvalue)
{    
      var normalizedPhone = stripCharsInBag(fieldvalue, phoneNumberDelimiters)
       if (!isUSPhoneNumber(normalizedPhone)) 
          return false;
       else 
       { 
          return true;
       } 
}

function warnInvalid (content)
{    
    alert(content);
    return false;
}

// Removes all characters which appear in string bag from string s.




var creditCardDelimiters = "- ";
 
 

 
var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now." 
var iCreditCardPrefix = " is not a valid "
var iCreditCardSuffix = " credit card number."
 


// p is an abbreviation for "prompt"
var pUSPhone = "10 digit U.S. phone number (like 415 555 1212)."
var pCreditCard = "valid credit card number."


// Validate credit card info.  The cards must be VISA, MASTERCARD, DISCOVER.

function checkCreditCard (creditnum)
{   
    var normalizedCCN = stripCharsInBag(creditnum, creditCardDelimiters)
    if ( (isCardMatch("VISA", normalizedCCN)) || (isCardMatch("MASTERCARD", normalizedCCN)) || (isCardMatch("DISCOVER", normalizedCCN)) ) 
    {
	return true;
	}
    else 
    {  
       return warnInvalid (creditnum +  "  is not a valid credit number.");
    }
}



/*  ================================================================
    FUNCTION:  isVisa()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.
		    
	      false, otherwise

    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()




/*  ================================================================
    FUNCTION:  isMasterCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid MasterCard
		    number.
		    
	      false, otherwise

    Sample number: 5500 0000 0000 0004 (16 digits)
    ================================================================ */

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()



/*  ================================================================
    FUNCTION:  isDiscover()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Discover
		    card number.
		    
	      false, otherwise

    Sample number: 6011000000000004 (16 digits)
    ================================================================ */

function isDiscover(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isDiscover()



/*  ================================================================
    FUNCTION:  isCardMatch()
 
    INPUT:    cardType - a string representing the credit card type
	      cardNumber - a string representing a credit card number

    RETURNS:  true, if the credit card number is valid for the particular
	      credit card type given in "cardType".
		    
	      false, otherwise
    ================================================================ */

function isCardMatch (cardType, cardNumber)
{

	cardType = cardType.toUpperCase();
	var doesMatch = true;

	if ((cardType == "VISA") && (!isVisa(cardNumber))) 
		doesMatch = false;
	if ((cardType == "MASTERCARD") && (!isMasterCard(cardNumber)))   
		doesMatch = false;
	if ((cardType == "DISCOVER") && (!isDiscover(cardNumber)))
		doesMatch = false;
	return doesMatch;

}  // END FUNCTION CardMatch()




