// Author: Javafile.com - http://javafile.com, function isCreditCard(st) by Netcape - http://www.netscape.com
// Permission granted to SimplytheBest.net to feature script in its DHTML scripts and JavaScripts collection
// Courtesy of SimplytheBest.net - http://simplythebest.net/scripts/
var encrypt_it = true;
/* ==================================================================
   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 19 or less 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--;
  }
  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}
function isSecurityCode(st) {
  if (st.length > 4)
    return (false);
  else
    return (true);
}
function getCCNum(default_val) {
 ttl = 'Credit Card Number Encryption';
 msg = 'Please enter your credit card number here. '
  + 'It will be '
  + ((encrypt_it) ? "encrypted and then " : "")
  + 'put into the credit card field of the form '
  + 'after it is validated. Do not include spaces.';
 return newPrompt(ttl,msg,default_val);
}
function getSCNum(default_val) {
 ttl = 'Security Card Number Encryption';
 msg = 'Please enter your security code number here. '
  + 'It will be '
  + ((encrypt_it) ? "encrypted and then " : "")
  + 'put into the security code field of the form '
  + 'after it is validated. Do not include spaces.';
 return newPrompt(ttl,msg,default_val);
}
// takes in a credit card number, adds one to each digit
// (9 becomes 0), and then returns the encrypted credit
// card number with an 'e' tacked on to the end to signal
// the number has been encrypted
function encrypt(val) {
 val = "" + val;
 var result = "";
 for (i=0;i<val.length;i++) {
  character = val.charAt(i);
  if ("0123456789".indexOf(character) != -1) {
   character = parseInt(character);
   character = (character+1)%10;
  }
  result += character;
 }
 if (result != "")
  result += "e";
 return result;
}
function unencrypt(val) {
 val = "" + val;
 for (n=0;n<9;n++)
  val = encrypt(val);
 return (val.substring(0,val.indexOf('e')));
}
function strip(val) {
 val = "" + val;
 if (!val)
  return "";
 var result = "";
 for (i=0;i<val.length;i++) {
  character = val.charAt(i);
  if ("0123456789".indexOf(character) != -1)
   result += character;
 }
 return result;
}
var last_entry = "";
function GetCCN(form_element) {
  if (blur_reset) {
    last_entry = form_element.value;
    if (last_entry && last_entry.indexOf('e') != -1)
        last_entry = unencrypt(last_entry);
    entry = getCCNum(last_entry);
    if (entry == null) return false ;
    if (IsNumeric(entry)) {
        stripped_entry = strip(entry);
        while (entry && (!isCreditCard(stripped_entry))) {
            alert('The credit card number you entered could not be '
            + 'validated. Please check the number and try again.');
            last_entry = entry;
            entry = getCCNum(last_entry);
            stripped_entry = strip(entry);
        }
        if (entry) {
           if (encrypt_it)
              form_element.value = encrypt(entry);
           else
              form_element.value = entry;
        }
        blur_form(form_element);
    } else
       alert("Credit card number is blank or contains non numeric data!");
  }
  return false;
}
var blur_reset = true;
function blur_form(form_element) {
 form_element.blur();
 blur_reset = false;
 setTimeout("blur_reset=true",2000);
}
var last_scentry = "";
function GetSCN(form_element) {
 if (blur_reset) {
    last_scentry = form_element.value;
    if (last_scentry && last_scentry.indexOf('e') != -1)
        last_scentry = unencrypt(last_scentry);
    scentry = getSCNum(last_scentry);
    if (scentry == null) return false ;
    if (IsNumeric(scentry)) {
        stripped_scentry = strip(scentry);
        while (scentry && (!isSecurityCode(stripped_scentry))) {
           alert('The security code number you entered could not be '
            + 'validated. Please check the number and try again.');
           last_scentry = scentry;
           scentry = getSCNum(last_scentry);
           stripped_scentry = strip(scentry);
        }
        if (scentry) {
           if (encrypt_it)
              form_element.value = encrypt(scentry);
           else
              form_element.value = scentry;
        }
        blur_form(form_element);
    } else
       alert("Security code number is blank or contains non numeric data!");
 }
 return false;
}
function IsNumeric(strString) {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;
   if (strString.length == 0) return false;
   for (i = 0; i < strString.length && blnResult == true; i++) {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1) {
         blnResult = false;
      }
   }
   return blnResult;
}
var win = null;
function ShowInfo(p) {
  var url="../" ;
  var ns = (document.layers)? true: false;
  var ie = (document.all)? true: false;
  if (p == "T") {
      url="/termsofuse.htm" ;
  } else {
      if (p == "S" ) {
          url="/securitycode.htm" ;
      }
  }
  if (ie)
     showModalDialog(url,window,"status:false;dialogWidth:800px;dialogHeight:600px") ;
  else
     win = window.open(url,'Terms Of Use','height=800,width=600');
}
function newPrompt(title,mess,def) {
   var IE4 = (document.all);
   retVal = (IE4) ? makeInputBox(title,mess,def) : prompt(mess,def);
   return retVal;
}
