// JavaScript Document

function checkpattern(sInput,pattern){
  var test = true;
  if (sInput.search(pattern)==-1) //if match failed
    test = false;
  
  return test;
}

function validate(){
  debugger;
  var amount = bmpay.AMOUNT.value;
  var name = bmpay.NAME.value;
  var cc_num = bmpay.CC_NUM.value;
  var cc_expires = bmpay.CC_EXPIRES.value;
  var cvv2 = bmpay.CVCCVV2.value;
  var addr1 = bmpay.ADDR1.value;
  var city = bmpay.CITY.value;
  var state = bmpay.STATE.value;
  var zipcode = bmpay.ZIPCODE.value;
  var phone = bmpay.PHONE.value;
  var email = bmpay.EMAIL.value;
  
  var patccnum = /^((4\d{3}-?\d{4}-?\d{4}-?\d{4})||(5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4})||(6011-?\d{4}-?\d{4}-?\d{4})||(3[4,7]\d{13})||(3[0,6,8]\d{12}))$/;
  var patexpdate = /^(0[1-9]|1[0-2])[0-9][0-9]$/;
  var patcvv2 = /^([0-9]{3,4})$/;
  var patzip = /^\d{5}/;
  var patphone = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/;
  var patemail = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  
  var test = true;
  
  if(amount<=0){
    alert('Please use a valid amount $0.00 .');
    bmpay.AMOUNT.focus();
    test = false;
  }
  
  if(name == null || name == ''){
    alert('Please enter your name as it appears on your card.');
    bmpay.NAME.focus();
    test = false;
  }
  
  if(!checkpattern(cc_num,patccnum)){
    alert('Please enter a valid credit card number.');
    bmpay.CC_NUM.focus();
    test = false;
  }
    
  if(!checkpattern(cc_expires,patexpdate)){
    alert('Please enter a valid expiration date in MMYY format.');
    bmpay.CC_EXPIRES.focus();
    test = false;
  }
  
  if(!checkpattern(cvv2,patcvv2)){
    alert('Please enter a valid CVV2 from the back of your credit card.');
    bmpay.CVCCVV2.focus();
    test = false;
  }
  
  if(addr1 == null||addr1==''){
    alert('Please enter a valid Address.');
    bmpay.ADDR1.focus();
    test = false;
  }
  
  if(city == null || city == ''){
    alert('Please enter a City.');
    bmpay.CITY.focus();
    test=false;
  }
  
  if(!checkpattern(zipcode,patzip)){
    alert('Please enter a valid 5 digit zip code.');
    bmpay.ZIPCODE.focus();
    test = false;
  }
  
  if(!checkpattern(phone,patphone)){
    alert('Please enter a valide phone number (602)555-1212.');
    bmpay.PHONE.focus();
    test = false;
  }
  if(!checkpattern(email,patemail)){
    alert('Please enter a valid email address.');
    bmpay.EMAIL.focus();
    test = false;
  }
  
  return test;
}
