var required = { 'ctbForm':{ 'amount':'Contribution Amount','firstname':'First Name','lastname':'Last Name','phone':'Phone Number','email':'Email Address','address1':'Billing Address','city':'City','state':'State','zipcode':'Postal Code','cctype':'Credit Card Type','ccnum':'Credit Card Number','ccexp':'Credit Card Expiration Date' } };

$(function() {
  var $d = $('#amount a');
  $d.bind('click',function() {
    $d.removeClass('picked');
    $(this).addClass('picked');
    if ($(this).parent().attr('id') == 'otheramount') {
      $('#otheramountbox').find('input').val('').end().fadeIn(300,function() {
        if ($.browser.msie) { this.style.removeAttribute('filter'); }
        $('input',this).focus();
      });
    }
    else {
      $('#otheramountbox').hide().find('input').val($(this).html().replace(/\$/g,''));
    }
    return(false);
  });

  $('#cocontributor a').bind('click',function() {
    $('#cocontributor').fadeOut(300,function() {
      $('#cocontributorinfo').fadeIn('1000',function() {
        if ($.browser.msie) { this.style.removeAttribute('filter'); }
      });
    });
    return(false);
  });

  var $a = $('#cctype a');
  $a.bind('click',function() {
    $('#cctype input').val(this.title);
    $a.removeClass('picked');
    $(this).addClass('picked');
    $('#ccnum input').focus();
    return(false);
  });

  $('#ctbForm').bind('submit',function() {
    if (validate(this)) {
      tb_show('','about:blank?TB_iframe=true&width=650&height=350');
      this.target = $('#TB_iframeContent').attr('name');
      return(true);
    }
    else { return(false); }
  });

  var recurring = getquery('recurring');
  if (recurring) {
    $('#recurring select').attr('selectedIndex',recurring);
  }
  var amount = getquery('amount').replace(/(\.00|,)/g,'');
  if (amount) {
    var found = false;
    $d.each(function() {
      var dvalue = $(this).html().replace(/[$,]/g,'');
      if (amount == dvalue) {
        $(this).trigger('click');
        found = true;
      }
    });
    if (!found) {
      $('#otheramount a').trigger('click');
      $('#otheramountbox input').val(amount);
    }
  }    
});


function getquery(key) {
  var query = document.location.search;
  regExp = eval('/[?&]'+key+'=/g');
  keyloc = query.search(regExp);
  if (keyloc == -1) { return(''); }

  nextkey = query.indexOf('&',(keyloc+1));
  if (nextkey == -1) { nextkey = query.length }
  start = (query.substring(keyloc,nextkey).indexOf('=')+keyloc+1);

  return(unescape(query.substring(start,nextkey)).replace(/\+/g,' '));
}
