// JavaScript Document


function validateFormOnSubmit(theForm) {
var reason = "";
  reason += validateEmpty(theForm.name, "Name");
  
  reason += validateEmail(theForm.email);
  
  //reason += validatePhone(theForm.card_number);
  //reason += validatePhone(theForm.ccv);
  
  
  if (reason != "") {
    alert("Some fields need your attention:\n" + reason);
    return false;
  }
  return true;
}
function validateEmpty(fld, name) {
    var error = "";
 
    if (fld.value.length == 0) {
        error = "- The "+name+" field cannot be empty\n"
    } else {}
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        error = "- The Email field cannot be empty\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        error = "- Enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        error = "- Enter a valid email address.\n";
    } else {}
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "- The telephone field cannot be empty\n";
    } else if (isNaN(parseInt(stripped))) {
        error = "- Enter a valid telephone number.\n";
    }else {}
    return error;
}




function validateFormOnSubmit2(theForm) {
var reason = "";

  
  reason += validatePhone2(theForm.card_number, "card number");
  reason += validatePhone2(theForm.ccv, "ccv");
  
  
  if (reason != "") {
    alert("Some fields need your attention:\n" + reason);
    return false;
  }
  return true;
}

function trim2(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validatePhone2(fld, name) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "- The "+ name +" field cannot be empty\n";
    } else if (isNaN(parseInt(stripped))) {
        error = "- Enter a valid "+ name +" number.\n";
    }else {}
    return error;
}

function acceptTerms() {
	var check = document.getElementById('check');
	var button = document.getElementById('button2');
	
	if(check.checked == true) {
		button.disabled = false;	
	}
	else {
		button.disabled = true;
	}
}











