function validate_luhn(ccnum) {
    var digit;
    var l; //length of input string
    var mul;
	var sum;
    var tproduct;

    sum = 0;
    mul = 1;
    l = ccnum.length;
 
//    if (ccnum.length > 19) {
	if(l > 19 || l < 15){
        return (false);
    }

    for (i = 0; i < l; i++) {
        digit = ccnum.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 checkout_Validator(theForm)
{
  if (theForm.billingaddress1.value == "")
  {
    alert("Please enter your Address.");
    theForm.billingaddress1.focus();
    return (false);
  }

  if (theForm.billingaddresscity.value == "")
  {
    alert("Please enter your City.");
    theForm.billingaddresscity.focus();
    return (false);
  }

  if (theForm.billingaddressstate.value == "")
  {
    alert("Please select your State.");
    theForm.billingaddressstate.focus();
    return (false);
  }

  if (theForm.billingaddresszip.value == "")
  {
    alert("Please enter your Zip.");
    theForm.billingaddresszip.focus();
    return (false);
  }

  if (theForm.billingphone.value == "")
  {
    alert("Please enter your Phone.");
    theForm.billingphone.focus();
    return (false);
  }

  if (theForm.billingemail.value == "")
  {
    alert("Please enter your Email Address.");
    theForm.billingemail.focus();
    return (false);
  }

  if ((theForm.billingemail.value.indexOf('@',0) == -1) || (theForm.billingemail.value.indexOf('.',0) == -1))
  {
    alert("Email Address is incorrectly formatted. Please enter again.");
    theForm.billingemail.focus();
    return (false);
  }

  if (theForm.paymentnamefull.value == "")
  {
    alert("Please enter the Name on your Credit Card.");
    theForm.paymentnamefull.focus();
    return (false);
  }

  if (theForm.paymentmethod.value == "")
  {
    alert("Please select your Credit Card Type.");
    theForm.paymentmethod.focus();
    return (false);
  }

  if (theForm.paymentnumber.value == "")
  {
    alert("Please enter your Credit Card Number.");
    theForm.paymentnumber.focus();
    return (false);
  }

  if (!validate_luhn(theForm.paymentnumber.value)){
    alert("Please enter a valid Credit Card Number.");
    theForm.paymentnumber.focus();
    return (false);
  }

//  if (theForm.paymentexpdatemonth.selectedIndex.value == "")
//  {
//    alert("Please select your Expiration Date Month.");
//    theForm.paymentexpdatemonth.focus();
//   return (false);
//  }

//  if (theForm.paymentexpdateyear.value == "")
//  {
//    alert("Please select your Expiration Date Year.");
//    theForm.paymentexpdateyear.focus();
//   return (false);
//  }

//  if (theForm.shippingmethod.value == "")
//  {
//    alert("Please select your Shipping Method.");
//    theForm.shippingmethod.focus();
//    return (false);
//  }

  return (true);
}

