var sErrorMessage = "You must fill in these fields before you continue.\r\n"; var bErrorOccurred = false; // set by AddErrorMsg function var digits = "0123456789"; var whitespace = " \t\n\r"; var bSubmitted = false; function ErrorMessage() { var sTmpErrorMessage = sErrorMessage; sErrorMessage = "You must fill in these fields before you continue.\r\n"; return sTmpErrorMessage; } function AddErrorMsg(sMsg) { sErrorMessage = sErrorMessage.concat(sMsg, "\r\n"); bErrorOccurred = true; } function isEmpty(field) { return (field.length == 0 || field == null); } function bFieldPresent(name) { return (!isEmpty(name)); } function isEmail(s) { var sArray1, sArray2; var bPassed = false; sArray1 = s.split("@") if (sArray1.length == 2) { if (sArray1[0].length > 0) { if (sArray1[1].length > 2) { sArray2 = sArray1[1].split(".") if (sArray2.length > 1) { if (sArray2[0].length > 0) { if (sArray2[1].length > 0) { bPassed = true; } } } } } } return bPassed; } function bCreditCardDateOK(sMonth, sYear) { var right_now = new Date(); var nEnteredExpDate = 0; // YYYYMM format var nCurrentDate = 0; // YYYYMM format var bErrorOccurred = false; if (isWhitespace(sMonth) || isWhitespace(sYear)) { bErrorOccurred = true; } else { nCurrentDate = right_now.getFullYear() * 100 + right_now.getMonth() + 1; nEnteredExpDate = parseInt(sYear, 10) * 100 + parseInt(sMonth, 10); if (nCurrentDate > nEnteredExpDate) { return false; } else { return true; } } } function bCreditCardOK(sCardNumber) { var sJustNumeric; var bOK = false; var nFirstDigit; var nFirstTwoDigits; var nLenToCheck; if (!isCreditCard(sCardNumber)) { return false; } if (bFieldPresent(sCardNumber)) { sJustNumeric = sCullChars(sCardNumber, digits); sCardNumber = sJustNumeric; nFirstDigit = parseInt(sJustNumeric.charAt(0), 10); nFirstTwoDigits = parseInt(sJustNumeric.substr(0, 2), 10); if (3 <= nFirstDigit && nFirstDigit <= 6) { if (nFirstTwoDigits == 37) { nLenToCheck = 15; } else { nLenToCheck = 16; } if (sJustNumeric.length == nLenToCheck) { bOK = true; } } } return bOK; } function isCreditCard(st) { // Encoding only works on cards with less than 19 digits if (st.length > 19) return (true); var i; var sum = 0; var mul = 1; var len = st.length; var digit; for (i = 0; i < len; i++) { digit = st.substring(len - i - 1, len - i); tproduct = parseInt(digit, 10) * mul; if (tproduct >= 10) sum += (tproduct % 10) + 1; else sum += tproduct; if (mul == 1) mul++; else mul--; } return ((sum % 10) == 0) } function bZipCodeOK(sZipCode) { // Good for US only var sJustNumeric; var bOK = false; var aArray; var aLengths = new Array(5, 4); var i; var nTotalLength = 0; var nArrayLen; if (bFieldPresent(sZipCode)) { aArray = sZipCode.split("-"); nArrayLen = aArray.length; if (nArrayLen > 2) { nArrayLen = 2; } for (i = 0; i < nArrayLen; i++) { aArray[i] = sCullChars(aArray[i], digits); nTotalLength += aLengths[i]; } nTotalLength += nArrayLen - 1; sJustNumeric = aArray[0]; if (nArrayLen > 1) { if (aArray[0].length > 0 && aArray[1].length > 0) { sJustNumeric += "-" + aArray[1]; } else { sJustNumeric += aArray[1]; } } sZipCode = sJustNumeric; if (sJustNumeric.length == nTotalLength) { var bOK = true; } } return bOK; } function sCullChars(sText, sCharsToCull) { var i; var sResult = ""; for (i = 0; i < sText.length; i++) { if (sCharsToCull.indexOf(sText.charAt(i)) != -1) { sResult += sText.charAt(i); } } return sResult; } // check for valid numeric strings function IsNumeric(strString) { var strValidChars = "0123456789"; var strChar; var blnResult = true; // test strString consists of valid characters listed above for (i = 0; i < strString.length && blnResult == true; i++) { strChar = strString.charAt(i); if (strValidChars.indexOf(strChar) == -1) { blnResult = false; break; } } return blnResult; } function isWhitespace(s) { var i; var c; 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. c = s.charAt(i); if (whitespace.indexOf(c) == -1) return false; } // All characters are whitespace. return true; } /* ================================================================ 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: isAmericanExpress() INPUT: cc - a string representing a credit card number RETURNS: true, if the credit card number is a valid American Express number. false, otherwise Sample number: 340000000000009 (15 digits) ================================================================ */ function isAmericanExpress(cc) { firstdig = cc.substring(0,1); seconddig = cc.substring(1,2); if ((cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7))) return isCreditCard(cc); return false; } // END FUNCTION isAmericanExpress() /* ================================================================ FUNCTION: isDinersClub() INPUT: cc - a string representing a credit card number RETURNS: true, if the credit card number is a valid Diner's Club number. false, otherwise Sample number: 30000000000004 (14 digits) ================================================================ */ function isDinersClub(cc) { firstdig = cc.substring(0,1); seconddig = cc.substring(1,2); if ((cc.length == 14) && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8))) return isCreditCard(cc); return false; } /* ================================================================ FUNCTION: isCarteBlanche() INPUT: cc - a string representing a credit card number RETURNS: true, if the credit card number is a valid Carte Blanche number. false, otherwise ================================================================ */ function isCarteBlanche(cc) { return isDinersClub(cc); } /* ================================================================ 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: isEnRoute() INPUT: cc - a string representing a credit card number RETURNS: true, if the credit card number is a valid enRoute card number. false, otherwise Sample number: 201400000000009 (15 digits) ================================================================ */ function isEnRoute(cc) { first4digs = cc.substring(0,4); if ((cc.length == 15) && ((first4digs == "2014") || (first4digs == "2149"))) return isCreditCard(cc); return false; } /* ================================================================ FUNCTION: isJCB() INPUT: cc - a string representing a credit card number RETURNS: true, if the credit card number is a valid JCB card number. false, otherwise ================================================================ */ function isJCB(cc) { first4digs = cc.substring(0,4); if ((cc.length == 16) && ((first4digs == "3088") || (first4digs == "3096") || (first4digs == "3112") || (first4digs == "3158") || (first4digs == "3337") || (first4digs == "3528"))) return isCreditCard(cc); return false; } // END FUNCTION isJCB() /* ================================================================ FUNCTION: isAnyCard() INPUT: cc - a string representing a credit card number RETURNS: true, if the credit card number is any valid credit card number for any of the accepted card types. false, otherwise ================================================================ */ function isAnyCard(cc) { if (!isCreditCard(cc)) return false; if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) && !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) { return false; } return true; } // END FUNCTION isAnyCard() /* ================================================================ 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 == "AMERICANEXPRESS") || (cardType == "AMEX") ) && (!isAmericanExpress(cardNumber))) doesMatch = false; if ((cardType == "DISCOVER") && (!isDiscover(cardNumber))) doesMatch = false; if ((cardType == "JCB") && (!isJCB(cardNumber))) doesMatch = false; if ((cardType == "DINERS") && (!isDinersClub(cardNumber))) doesMatch = false; if ((cardType == "CARTEBLANCHE") && (!isCarteBlanche(cardNumber))) doesMatch = false; if ((cardType == "ENROUTE") && (!isEnRoute(cardNumber))) doesMatch = false; return doesMatch; } // END FUNCTION CardMatch() /* ================================================================ The below stub functions are retained for backward compatibility with the original LivePayment code so that it should be possible in principle to swap in this new module as a replacement for the older module without breaking existing code. (There are no guarantees, of course, but it should work.) When writing new code, do not use these stub functions; use the functions defined above. ================================================================ */ function IsCC (st) { return isCreditCard(st); } function IsVisa (cc) { return isVisa(cc); } function IsVISA (cc) { return isVisa(cc); } function IsMasterCard (cc) { return isMasterCard(cc); } function IsMastercard (cc) { return isMasterCard(cc); } function IsMC (cc) { return isMasterCard(cc); } function IsAmericanExpress (cc) { return isAmericanExpress(cc); } function IsAmEx (cc) { return isAmericanExpress(cc); } function IsDinersClub (cc) { return isDinersClub(cc); } function IsDC (cc) { return isDinersClub(cc); } function IsDiners (cc) { return isDinersClub(cc); } function IsCarteBlanche (cc) { return isCarteBlanche(cc); } function IsCB (cc) { return isCarteBlanche(cc); } function IsDiscover (cc) { return isDiscover(cc); } function IsEnRoute (cc) { return isEnRoute(cc); } function IsenRoute (cc) { return isEnRoute(cc); } function IsJCB (cc) { return isJCB(cc); } function IsAnyCard(cc) { return isAnyCard(cc); } function IsCardMatch (cardType, cardNumber) { return isCardMatch (cardType, cardNumber); }