/******************************************************************
// Returns true if string s is empty or 
// whitespace characters only.
******************************************************************/
function isWhitespace (s)
{   
	var i;
	var whitespace = " \t\n\r";

    // Is s empty?
    if ((s == null) || (s.length == 0)) 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) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}
/******************************************************************
* Reformat a string
******************************************************************/
function reformat (s)

{   var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

/******************************************************************
* Return true if value is integer
******************************************************************/
function isInteger (s)
{   var i;
    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
/******************************************************************
* Removes all characters which appear in string bag from string s.
******************************************************************/
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;
}

/******************************************************************
*	Checks for at least 4 letters or numbers
******************************************************************/
function checkChars(theField) 
{
	var illegalChars = /\W/;
	var retval = false;
	
	if (illegalChars.test(theField.value)) {
		retval = false;
		alert(theField.name + " can not contain SPACES and must be a-z, 0-9 or _\n");
	} else if(theField.value.length < 4) {
		retval = false;
		alert(theField.name + " must be at least 4 characters long.\n");
	} else {
		retval = true;
	}
	return retval;
}

/******************************************************************
*	REQUIRED FIELD
******************************************************************/
function checkBlank(theField,fieldPrompt)
{
	var retval = false;
	
	if(theField.value == null || theField.value.length == 0) {
		retval = false;
		alert(fieldPrompt + " is required.");
	} else {
		retval = true;
	}
	return retval;
}

/******************************************************************
*	VALID STATE CODE
******************************************************************/
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY"
var USStateCodeDelimiter = "|";
function checkStateCode (theField)
{
	var retval = false;
    theField.value = theField.value.toUpperCase();

	if( (USStateCodes.indexOf(theField.value) != -1) && (theField.value.indexOf(USStateCodeDelimiter) == -1) ) {
		retval = true;
	} else {
		retval = false;
		alert ("You must enter a valid 2 letter state code");
	}
	return retval;
}
/******************************************************************
*	VALID PHONE NUMBER
*	@param theField to check
*	@param fieldName Name of the field to 
*		show user if incorrect
******************************************************************/
function checkPhone (theField, fieldName)
{
	var normalizedPhone = stripCharsInBag(theField.value, "()- ");
	var retval = false;
	
	if(isInteger(normalizedPhone) && normalizedPhone.length == 10) {
		theField.value = reformat (normalizedPhone, "(", 3, ") ", 3, "-", 4);
		retval = true;
		
	} else {
		alert ("Please check your " + fieldName + ". Valid values include: digits ()-");
		retval = false;
	}	
	return retval;
}
/******************************************************************
*	VALID EMAIL
*	@param theField to check
*	@param fieldName Name of the field to 
*		show user if incorrect
******************************************************************/
function checkEmail (theField)
{      
	var retval = false;
	var s = theField.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if (filter.test(s)) retval = true;

	if(!retval) alert ("Please check your email address");
	return retval;
}
/******************************************************************
*	VALID CREDIT CARD
*	@param radio value of cc type
*	@param theField to check
******************************************************************/
function checkCreditCard (radio, theField)
{   
	var cardType = getRadioButtonValue (radio)
    var normalizedCCN = stripCharsInBag(theField.value, " -")
	var retval = true;
    if (!isCardMatch(cardType, normalizedCCN)) { 
       retval = false;
    } else {  
		theField.value = normalizedCCN;
    }
	
	if(!retval) alert("Please check your credit card number");
	return retval;
	
}
// Get checked value from radio button.

function getRadioButtonValue (radio)
{   
	var retval = "";
	for (var i = 0; i < radio.length; i++) {  
	 	if (radio[i].checked) {  
	  		retval = radio[i].value;
		}
    } 
    return retval;
}
/*  ================================================================
    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;
	return doesMatch;

}  // END FUNCTION CardMatch()

/*  ================================================================
    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:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()
