function validateFormOnSubmit(theForm) {

var reason = "";
	
	reason += validateName(theForm.name);
	reason += validateCardtype(theForm.cardtype);
	reason += validateNumber(theForm.number);
	reason += validateDate(theForm.date);
	reason += validatePhone(theForm.phone);
	
      
  	if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
}

  	alert("Your order has been submitted.  We will contact you promptly.  Thank you!");
  	return true;
}




function validateName(fld) {
var error = "";

	if (fld.value == "") {
	error = "Please enter your name.\n";
}
	return error;
}




function validateCardtype(fld) {
	var error = "";

	if (fld.selectedIndex == 0) {
	error = "Please indicate a credit card type.\n";
}
	return error;
}




function validateNumber(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   	if (fld.value == "") {
	error = "You didn't enter a credit card number.\n";
    
} 	else if (isNaN(stripped)) {
	error = "The credit card number contains illegal characters.\n";
	
}	else if (stripped.length < 15) {
	error = "Your credit card number is of an inproper length. \n";
	
}	else if (stripped.length > 16) {
	error = "Your credit card number is of an inproper length. \n";
	
}
    return error;
}






function validateDate(fld) {
var error = "";

	if (fld.value == "") {
	error = "You did not enter an expiration date for your credit card.\n";
}
	return error;
}





function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   	if (fld.value == "") {
	error = "You didn't enter a phone number.\n";
    
} 	else if (isNaN(stripped)) {
	error = "The phone number contains illegal characters.\n";
    
} 	else if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
}
    return error;
}

// JavaScript Document
