(function($){
    var validate = function(event) {
        $("errors").innerHTML = "";
        var errors = [];

        $("amount").value = nowtools.normalizeCurrency($("amount").value);
        if( nowtools.invalid($("amount").value, true) )
            errors.push("Please enter a donation amount");

        if( nowtools.invalid($("amount").value, false, nowtools.numberPattern) )
            errors.push("Please enter a valid dollar amount");

        if( nowtools.tooCheap($("amount").value, 1.0) )
            errors.push("Donation amount must be at least $1.00");

        if( errors.length > 0 ) {
            nowtools.showErrors("errors", errors);
            nowtools.stop(event);
        }
        return errors.length == 0;
    };

    nowtools.event(window, "load", function() {
        $("donate").target = "_top";
        nowtools.event($("donate"), "submit", validate);
        nowtools.externalizeLinks();
    });
})(nowtools.$);
