//
// This is used to show the recurring information (frequency, whether it's New or Update, and the
// charge day of 1st or 15th) if "monthly" is chosen. Otherwise the DIV with the recurring details
// is hidden and is not required.
//
function showRecurringDetails(show_recurring_fields) {
  if (show_recurring_fields) {
    $j('#recurring_details').show();
  }
  else {
    $j('#recurring_details').hide();
  }
}

//
// When user selects the type of giving method (Credit Card, eCheck, Mail in Check) then this
// function will show or hide the appropriate <div>'s that have the corresponding fields that
// need to be filled out. IE, if you select "Credit Card" then the <div> with credit card
// fields is displayed and the <div> for eCheck fields is hidden.
//
function selectGivingMethod() {
  if ($j('#don_giving_donation_type').val() == 'CC') {
    $j('#donation_type_credit_card_details').show();
    $j('#donation_type_echeck_details').hide();
    $j('#donation_type_mail_in_check_details').hide();
  }
  else if ($j('#don_giving_donation_type').val() == 'ECHECK') {
    $j('#donation_type_credit_card_details').hide();
    $j('#donation_type_echeck_details').show();
    $j('#donation_type_mail_in_check_details').hide();
  }
  else {
    //mail in a check
    $j('#donation_type_credit_card_details').hide();
    $j('#donation_type_echeck_details').hide();
    $j('#donation_type_mail_in_check_details').show();
  }
}

//
// Formats a number into a currently (with 2 decimal places).
//
function CurrencyFormatted(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	var s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}







//
// This is the javascript validation for the Testimony form (submit_your_testimony.php). If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function validate_donate_form() {
  //Form validation
  var bError = false;
  if ($j('#don_first_name').val() == '') { addError('don_first_name'); bError = true; } else { removeErrorClass('don_first_name'); }
  if ($j('#don_last_name').val() == '') { addError('don_last_name'); bError = true; } else { removeErrorClass('don_last_name'); }
  if ($j('#don_address_1').val() == '') { addError('don_address_1'); bError = true; } else { removeErrorClass('don_address_1'); }
  if ($j('#don_city').val() == '') { addError('don_city'); bError = true; } else { removeErrorClass('don_city'); }
  if ($j('#don_state').val() == '') { addError('don_state'); bError = true; } else { removeErrorClass('don_state'); }
  if ($j('#don_zip').val() == '') { addError('don_zip'); bError = true; } else { removeErrorClass('don_zip'); }
  if ($j('#don_country').val() == '') { addError('don_country'); bError = true; } else { removeErrorClass('don_country'); }
  if ($j('#don_phone').val() == '') { addError('don_phone'); bError = true; } else { removeErrorClass('don_phone'); }

  var sEmail = $j('#don_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('don_email'); bError = true; } else { removeErrorClass('don_email'); }

  if ($j('#don_amount').val() == '') { addError('don_amount'); bError = true; } else { removeErrorClass('don_amount'); }
  var don_amount = $j('#don_amount').val();
  if (don_amount <= 0) { addError('don_amount'); bError = true; } else { removeErrorClass('don_amount'); }

  //Format the amount with 2 decimal places
  don_amount = CurrencyFormatted(don_amount);
  $j('#don_amount').val(don_amount);

  if ($j('#don_giving_donation_type').val() == 'CC') {
    if ($j('#don_type_of_card').val() == '--') { addError('don_type_of_card'); bError = true; } else { removeErrorClass('don_type_of_card'); }
    if ($j('#don_cc_name').val() == '') { addError('don_cc_name'); bError = true; } else { removeErrorClass('don_cc_name'); }
    if ($j('#don_cc_number').val() == '') { addError('don_cc_number'); bError = true; } else { removeErrorClass('don_cc_number'); }
	if ($j('#don_cv2_number').val() == '') { addError('don_cv2_number'); bError = true; } else { removeErrorClass('don_cv2_number'); }
    if ($j('#don_exp_month').val() == '--') { addError('don_exp_month'); bError = true; } else { removeErrorClass('don_exp_month'); }
    if ($j('#don_exp_year').val() == '--') { addError('don_exp_year'); bError = true; } else { removeErrorClass('don_exp_year'); }
  }
  else if ($j('#don_giving_donation_type').val() == 'ECHECK') {
    if ($j('#don_account_number').val() == '') { addError('don_account_number'); bError = true; } else { removeErrorClass('don_account_number'); }
    if ($j('#don_routing_number').val() == '') { addError('don_routing_number'); bError = true; } else { removeErrorClass('don_routing_number'); }
    if ($j('#don_bank_name').val() == '') { addError('don_bank_name'); bError = true; } else { removeErrorClass('don_bank_name'); }
    if ($j('#don_bank_address_1').val() == '') { addError('don_bank_address_1'); bError = true; } else { removeErrorClass('don_bank_address_1'); }
    if ($j('#don_bank_city').val() == '') { addError('don_bank_city'); bError = true; } else { removeErrorClass('don_bank_city'); }
    if ($j('#don_bank_state').val() == '') { addError('don_bank_state'); bError = true; } else { removeErrorClass('don_bank_state'); }
    if ($j('#don_bank_zip').val() == '') { addError('don_bank_zip'); bError = true; } else { removeErrorClass('don_bank_zip'); }
    if ($j('#don_bank_country').val() == '') { addError('don_bank_country'); bError = true; } else { removeErrorClass('don_bank_country'); }
  }

  //if ($j('#don_security_code').val() == '') { addError('don_security_code'); bError = true; } else { removeErrorClass('don_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

function removeErrorClass(control_id) {
  $j('#'+control_id).parent().parent().removeClass('error');
  return true;
}

function addError(control_id) {
  $j('#'+control_id).select().focus();
  $j('#'+control_id).parent().parent().addClass('error');
  return true;
}
