function validateCheckoutFormOnSubmit() {
  var reason = "";
  
  // First, handle alternate address fields
  if (document.forms[0].address_on_file.value == 1) {
    if (document.forms[0].shipTo[1].checked == true) {
	  reason += validateName(document.forms[0].ship_to_name); // Name - required
	  reason += validateAddress1(document.forms[0].address1); // Address 1 - required
	  reason += validateAddress2(document.forms[0].address2); // Address 2 - optional
	  reason += validateCity(document.forms[0].city); // City - required
	  reason += validateRegion(document.forms[0].region); // State/Region - required
	  reason += validateZipcode(document.forms[0].postalCode); // zip code - required
	  reason += validateCountry(document.forms[0].country); // Country - required
	  reason += validateShippingRegion(document.forms[0].shippingRegion); // ShippingRegion - required
	}
  } else {
	  reason += validateName(document.forms[0].ship_to_name); // Name - required
	  reason += validateAddress1(document.forms[0].address1); // Address 1 - required
	  reason += validateAddress2(document.forms[0].address2); // Address 2 - optional
	  reason += validateCity(document.forms[0].city); // City - required
	  reason += validateRegion(document.forms[0].region); // State/Region - required
	  reason += validateZipcode(document.forms[0].postalCode); // zip code - required
	  reason += validateCountry(document.forms[0].country); // Country - required
	  reason += validateShippingRegion(document.forms[0].shippingRegion); // ShippingRegion - required
  }
  
/*  
  // Next, deal with CC fields
  if (document.forms[0].noCCStored.value == 1) {
	//alert("CC is not stored.\n");
    if (document.forms[0].payment_method[0].checked == true) {
	  //alert("verify new card.\n");
	  reason += validateCardHolderName(document.forms[0].cardHolder); // Card Holder name - required
	  reason += validateCardNumber(document.forms[0].cardNumber); // Card Number - required
	  reason += validateCardExpDate(document.forms[0].expDate); // Exp Date - required
	} else { // no action - using PayPal
	}
  } else {
	//alert("CC is on file.\n");
    if (document.forms[0].payment_method[0].checked == true) {
	  //alert("use stored card.\n");	  
	} else if (document.forms[0].payment_method[1].checked == true) {
	  //alert("verify new card.\n");
	  reason += validateCardHolderName(document.forms[0].cardHolder); // Card Holder name - required
	  reason += validateCardNumber(document.forms[0].cardNumber); // Card Number - required
	  reason += validateCardExpDate(document.forms[0].expDate); // Exp Date - required
	} else { // no action - using PayPal
	}
  }
*/
  
  // Finally, either return error message or allow processing
  if (reason != "") { 
	alert("Some fields need correction:\n" + reason);
	return false;
  } else {
	return true;
  }
}

// Functions
function validateCardHolderName(fld) {
	//alert('Card Holder');
    var error = "";
    var illegalChars = /\W_/; // allow letters and numbers, no underscores
    var validChars = /^[ a-zA-Z0-9.-]+$/;
	
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter the name of the Card Holder.\n";
        //alert('... and again 1');
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "the name of the Card Holder contains characters our form does not allow.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "the name of the Card Holder contains characters our form does not allow2.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCardNumber(fld) {
	//alert('CardNumber');
    var error = "";
    var illegalChars = /\W_/; // allow letters and numbers, no underscores
    var validChars = /^[0-9-]+$/;	
	
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Card Number.\n";
        //alert('... and again 1');
    } else if (fld.value.length < 16) {
        fld.style.background = 'Yellow'; 
        error = "The Card Number must be exactly 16 characters.\n";
        //alert('... and again 2');
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Card Number contains characters our form does not allow.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "Card Number contains characters our form does not allow2.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCardExpDate(fld) {
	//alert('Exp Date');
    var error = "";
    var illegalChars = /\W_/; // allow letters and numbers, no underscores
    var validChars = /^[0-9\/]+$/;	
	
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter an Expiration Date.\n";
        //alert('... and again 1');
    } else if ((fld.value.length < 5)||(fld.value.length > 5)) {
        fld.style.background = 'Yellow'; 
        error = "The Expiration Date must be exactly 5 characters (eg.03/11).\n";
        //alert('... and again 2');
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Expiration Date contains characters our form does not allow.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "Expiration Date contains characters our form does not allow2.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateName(fld) {
	//alert('Name');
    var error = "";
    var illegalChars = /\W_/; // allow letters and numbers, no underscores
    var validChars = /^[ a-zA-Z0-9.-]+$/;
	
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter the name of who to ship to.\n";
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "the name of who to ship to contains illegal characters.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "the name of who to ship to contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateAddress1(fld) {
	//alert('Address1');
    var error = "";
    var illegalChars = /\W_/; // allow letters and numbers, no underscores
	var validChars = /^[ a-zA-Z0-9.-\/\#]+$/;
	
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a street address.\n";
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Street address contains illegal characters.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "Street address contains illegal characters .\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateAddress2(fld) {
	//alert('Address2');
    var error = "";
	var validChars = /^[#]?[ a-zA-Z0-9.-\/]+$/;
	
    if (fld.value.length == 0) { //don't need it
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Address (line 2) contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCity(fld) {
	//alert('City');
    var error = "";
    var illegalChars = /\W_/; // allow letters and numbers, no underscores
	var validChars = /^[ a-zA-Z0-9.-]+$/;
	
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a city.\n";
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "City contains illegal characters.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "City contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateRegion(fld) {
    var error = "";
	//alert('Region');
    if (fld.value == 1) {
        fld.style.background = 'Yellow'; 
        error = "Please select a State/Region.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateZipcode(fld) {
    var error = "";
	var validChars = /^[0-9-]+$/;	
	//alert('zip');
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter your zip code.\n";
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Your zip code contains illegal characters.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 10)) {
        fld.style.background = 'Yellow'; 
        error = "The zip code is the wrong length.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCountry(fld) {
	//alert('Country');
    var error = "";
    var illegalChars = /\W_/; // allow letters and numbers, no underscores
    var validChars = /^[ a-zA-Z.]+$/;
	
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a country.\n";
    } else if (!validChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Country contains illegal characters.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "Country contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateShippingRegion(fld) {
    var error = "";
	//alert('ShippingRegion');
    if (fld.value == 1) {
        fld.style.background = 'Yellow'; 
        error = "Please select a Shipping Region.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}