// -----------------------------------------------------------------------------
// rvform.js		(Copyright 2003 Rick Harvey)
//
// Checks mandatory objects in a form by passing in those objects to be checked.
//
// <form name="test" onSubmit="return rvCheckForm(this.fullname, this.email)">
//   Full name: <input type="text" name="fullname"><br>
//   Your email: <input type="text" name="email"><br>
//   <input type="submit" value="Submit">
// </form>
// -----------------------------------------------------------------------------

function rvCheckForm()
{
  if (! (document.layers || document.getElementById || document.all))
    return true
  for (var i=0; i<arguments.length; i++)
  {
    var name = arguments[i].name
    var value = arguments[i].value
    //alert(name+": "+value)
    switch(name)
    {
      case "email":
        if (! validateEmail(value))
          return error("Please enter a valid email address")
        break
      case "cardnumber":
        if (! validateCreditCard(value))
          return false
        break
      case "videoList":
        if (! validateString(value))
          return error("Your order is empty!\r\n\nPlease select a video from the list\r\nand click the \"Add Title\" button.")
        break
      default:
        if (! validateString(value))
          return error("Please enter a value for \""+arguments[i].name+"\"")
        break
    }
  }
  return true
}

function error(msg)
{
  alert(msg)
  return false
}

// -----------------------------------------------------------------------------
// Check for non empty string
// -----------------------------------------------------------------------------
function validateString(str)
{
//  var tmp = str.replace(/\s/g,'').length
  return str.replace(/\s/g,'').length
}

// -----------------------------------------------------------------------------
// Email check from javascriptkit.com
// -----------------------------------------------------------------------------
function validateEmail(email)
{
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  return (filter.test(email))
}

// -----------------------------------------------------------------------------
// Credit card checks
// -----------------------------------------------------------------------------
function validateCreditCard(str)
{
  // cleanup
  var ccString = str.replace(/\D/g,'')
  if (ccString.length < 13)
     return error("Please enter a valid credit card number (there aren't enough digits)")
  if (ccString.length > 16)
     return error("Please enter a valid credit card number (there are too many digits)")
  if (! ccLength(ccString))
     return error("Please enter a valid credit card number (wrong number of digits)")
  if (! ccLUHN(ccString))
     return error("Please enter a valid credit card number (one or more digits is mistyped)")
  return true
}
// LUHN from peterbailey.net
function ccLUHN(ccString)
{
  var odds = ""
  var evens = ""
  for (var i = ccString.length-2; i >= 0; i = i-2)
  {
    var digit = parseInt(ccString.charAt(i)) * 2
    odds += digit + ""
  }
  for (i = ccString.length-1; i >= 0; i = i-2)
    evens += ccString.charAt(i)
  var luhnStr = odds + evens
  var checkSum = 0
  for (i = 0; i < luhnStr.length; i++)
    checkSum += parseInt(luhnStr.charAt(i))	
  return (checkSum % 10 == 0)
}
// LENGTHS from http://www.analysisandsolutions.com/software/ccvs/ccvs-vb.htm
function ccLength(ccString)
{
  var block = parseInt(ccString.substr(0,4))   // derive card issuer
  var length = ccString.length
  // AMEX (e.g. 3400 000000 00009)
  if (block >= 3400 && block <= 3499 || block >= 3700 && block <= 3799)
    return (length == 15)
  // VISA (e.g. 4111 1111 1111 1111)
  if (block >= 4000 && block <= 4999)
    return (length == 16 || length == 13)
  // MASTER CARD (e.g. 5500 0000 0000 0004)
  if (block >= 5100 && block <= 5599)
    return (length == 16)
  // AUSTRALIAN BANK CARD
  if (block == 5610)
    return (length == 16)
  return false
}

