/* 
*  NacreData L.L.C.
*  PO Box 646 Chapel Hill, NC 27514
*  (919) 442-8899
*  info@nacredata.com 
*/

jQuery(document).ready(function(){

  jQuery("#other_amount").bind("change", function(event) { 
    var dollarAmount = jQuery("#other_amount").val();
    var dollarNum    = dollarAmount.replace(/[^\d.]/g, '');
    var otherString  = "Price::Qty::Code::Description::Flags|"+dollarNum+"::1::donOther::$"+dollarNum+" Donation::";
    jQuery("#Amount_other").val(otherString);
    if( dollarNum > 0 ) { 
      jQuery("#Amount_other").attr("checked",true);
    }
    else {
      jQuery("#Amount_30").attr("checked",true);
    }
  });
  
  jQuery("#donationform").bind("submit", function(event) {
    var product_string = jQuery("#Amount_30").val();     
    var dollarAmount   = jQuery("#other_amount").val();
    var dollarNum      = dollarAmount.replace(/[^\d.]/g, '');
    if( jQuery("#recurring").is(":checked") ) {
      if( !product_string.match( /\{RB/ ) ) {
        jQuery("#Amount_30").val( product_string + "{RB amount=30 startmonth=+0 frequency=" + jQuery("#recurring_time").val() + " duration=0 email=2}" );
        product_string = jQuery("#Amount_50").val();
        jQuery("#Amount_50").val( product_string + "{RB amount=50 startmonth=+0 frequency=" + jQuery("#recurring_time").val() + " duration=0 email=2}" );
        product_string = jQuery("#Amount_100").val();
        jQuery("#Amount_100").val( product_string + "{RB amount=100 startmonth=+0 frequency=" + jQuery("#recurring_time").val() + " duration=0 email=2}" );
        product_string = jQuery("#Amount_250").val();
        jQuery("#Amount_250").val( product_string + "{RB amount=250 startmonth=+0 frequency=" + jQuery("#recurring_time").val() + " duration=0 email=2}" );
        product_string = jQuery("#Amount_500").val();
        jQuery("#Amount_500").val( product_string + "{RB amount=500 startmonth=+0 frequency=" + jQuery("#recurring_time").val() + " duration=0 email=2}" );
        product_string = jQuery("#Amount_1000").val();
        jQuery("#Amount_1000").val( product_string + "{RB amount=1000 startmonth=+0 frequency=" + jQuery("#recurring_time").val() + " duration=0 email=2}" );
        product_string = jQuery("#Amount_other").val();
        jQuery("#Amount_other").val( product_string + "{RB amount="+dollarNum+" startmonth=+0 frequency=" + jQuery("#recurring_time").val() + " duration=0 email=2}" );
      }      
    }
    else {
      if( product_string.match( /\{RB/ ) ) {
        var non_recurring = product_string.match( /(.*){RB/ );
        jQuery("#Amount_30").val( non_recurring[1] );
        product_string = jQuery("#Amount_50").val();
        non_recurring = product_string.match( /(.*){RB/ );
        jQuery("#Amount_50").val( non_recurring[1] );
        product_string = jQuery("#Amount_100").val();
        non_recurring = product_string.match( /(.*){RB/ );
        jQuery("#Amount_100").val( non_recurring[1] );
        product_string = jQuery("#Amount_250").val();
        non_recurring = product_string.match( /(.*){RB/ );
        jQuery("#Amount_100").val( non_recurring[1] );
        product_string = jQuery("#Amount_500").val();
        non_recurring = product_string.match( /(.*){RB/ );
        jQuery("#Amount_500").val( non_recurring[1] );
        product_string = jQuery("#Amount_1000").val();
        non_recurring = product_string.match( /(.*){RB/ );
        jQuery("#Amount_1000").val( non_recurring[1] );
        product_string = jQuery("#Amount_other").val();
        non_recurring = product_string.match( /(.*){RB/ );
        jQuery("#Amount_other").val( non_recurring[1] );
      }  
    }      


    var gift_name      = jQuery("#gift_name").val();
    var product_string = jQuery("#Amount_30").val();
    if( gift_name.match( /\w/ ) && ( jQuery("#gifttype_gift").is(":checked") || jQuery("#gifttype_memory").is(":checked") ) ) {

      /* We have a gift name, so make sure we put the gift info into the description fields */
      if( !product_string.match( /gift/ ) ) {
        var gift_tag;
        if( jQuery("#gifttype_gift").is(":checked") ) {
          gift_tag = " gift for " + gift_name;
        }
        else {
          gift_tag = " in memory of " + gift_name;
        }
        
        var parts = product_string.match( /^(.*)(::[^:]*)$/ );
        jQuery("#Amount_30").val( parts[1] + gift_tag + parts[2] );
       
        product_string = jQuery("#Amount_50").val();
        parts          = product_string.match( /^(.*)(::[^:]*)$/ );
        jQuery("#Amount_50").val( parts[1] + gift_tag + parts[2] );
        
        product_string = jQuery("#Amount_100").val();
        parts          = product_string.match( /^(.*)(::[^:]*)$/ );
        jQuery("#Amount_100").val( parts[1] + gift_tag + parts[2] );
        
        product_string = jQuery("#Amount_250").val();
        parts          = product_string.match( /^(.*)(::[^:]*)$/ );
        jQuery("#Amount_250").val( parts[1] + gift_tag + parts[2] );
        
        product_string = jQuery("#Amount_500").val();
        parts          = product_string.match( /^(.*)(::[^:]*)$/ );
        jQuery("#Amount_500").val( parts[1] + gift_tag + parts[2] );
        
        product_string = jQuery("#Amount_1000").val();
        parts          = product_string.match( /^(.*)(::[^:]*)$/ );
        jQuery("#Amount_1000").val( parts[1] + gift_tag + parts[2] );
        
        product_string = jQuery("#Amount_other").val();
        parts          = product_string.match( /^(.*)(::[^:]*)$/ );
        jQuery("#Amount_other").val( parts[1] + gift_tag + parts[2] );        
      }
    }
  });  
  
});

