// imsa_paypal.js

function imsa_paypal_validate(formname) {
    // validate the IMSA PayPal button with given name
    var valid = true;
    amount = document.forms[formname].amount;

    valid = /^\s*\d+(\.\d\d)?\s*$/.test(amount.value);

    if (valid) {
	// clear any special styling we might have put on the input element
	amount.style.cssText = '';
    }
    else {
	amount.style.borderColor = '#FF0000';
	amount.style.borderWidth = 3;
	alert('The amount you entered (' + amount.value +
	      ') is not a number.  Please enter a numeric amount (such as 50 or 50.00) into the input field.');
	amount.focus();
    }
    return valid;
}

function imsa_paypal_update_total(formname, validating) {
    var valid = true;
    var total = 0.00;
    f = document.forms[formname];
    for (i = 1; i < 100; i++) {
	name = "amount_" + i;
	if (! (name in f)) {
	    break;
	}
	var itemvalid = true;
	samount = f[name].value;
	if (/^\s*$/.test(samount)) {
	    // amount value is blank; ignore it
	}
	else {
	    itemvalid = /^\s*\d+(\.\d\d)?\s*$/.test(samount);
	    if (! itemvalid) {
		if (validating) {
		    alert("The amount you entered (" + samount + ") is not a number.  Please enter a numeric amount such as 50 or 50.00");
		}
	    }
	    else {
		amount = parseFloat(samount);
		if (amount == 0) {
		    if (validating) {
			alert("Amount should not be zero");
		    }
		    itemvalid = false;
		}
		total += amount;

		if (validating) {
		    fundElement = f["item_name_" + i];
		    if (fundElement.value == "") {
			alert("You must choose a fund name for each amount");
			valid = false;
		    }
		}
	    }
	}
	if (itemvalid) {
	    f[name].style.cssText = '';
	}
	else {
	    f[name].style.borderColor = '#FF0000';
	    f[name].style.borderWidth = 3;
	    valid = false;
	}
    }
    totalElement = document.getElementById('total');
    totalElement.disabled = false;
    totalElement.value = total.toFixed(2);
    totalElement.disabled = true;
    return valid;
}

function imsa_paypal_donation_validate(formname) {
    var valid = imsa_paypal_update_total(formname, true);
    if (valid) {
	totalElement = document.getElementById('total');
	totalElement.disabled = false; // so its value gets POSTed
    }
    return valid;
}
// JavaScript Document
