﻿$(document).ready(function()
{
    $(".donationAmount").blur(function()
    {
        var amounts = $(".donationAmount");
      
        var amount = 0;
        amounts.each(
            function(){
                $(this).val($(this).val().replace(/[\,_$]/g,""));
                $(this).val($(this).val().replace(/\.$/,""));
            
                if ( trim($(this).val()) != "" && !isNaN($(this).val()) )
                {
                    amount = amount + parseFloat($(this).val());  
                    $(this).val("$" + parseFloat($(this).val()).toFixed(2));
                }
            }
        );
            
        $(".donationAmountTotal").text("$" + parseFloat(amount).toFixed(2));
        
        if ( amount < 5 || amount > 9999.99 )
        {
            //alert("not valid");
            $(".donationAmountError").text("** The total donation amount must be between $5.00 and $9,999.99.");
            $(".donationAmountError").attr({
                style: "display:inline;color:red;" 
            });
        }
        else 
        {
            //alert("valid");
            $(".donationAmountError").text("");
            $(".donationAmountError").attr({
                style: "display:none"
            });
        }
        
        if ( event.keyCode == 9 )
        {
            var nf = amounts[$(this).attr("tabindex") + 1];
            if ( nf != null )
            {
                nf.focus();   
            }
        }
    });
    
});/*end document.ready function*/


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

