﻿/**
 * Designed to support the KZME Periodic Donation (Subscription) Form
 *
 * @param   type      info
 */
//////////////  data tables  //////////////
/// no data here...

//////////MEAT/////////
/// no init required...

/////////potatoes//////////
function kzme_donate_repeat () {
	//// display or hide payment limit dropdown and total donation
	// refs
	var c_rpt = document.getElementById('KZME_Donate_repeat').checked; // repeats checkbox
	var d_lim = document.getElementById('KZME_Donate_showCount'); // recur limit dropdown
	var d_ttl = document.getElementById('KZME_Donate_showTotal'); // total donation text
	
	
	// is this a recurring donation (subscription) or one time donation
	if (c_rpt) {
		// recurring payment
		
		// unhide payment limit dropdown
		d_lim.style.display = 'inline';
		
		// toggle form parts
		kzme_don_form_enable ('amount', false);// one time amount
		kzme_don_form_enable ('a3', true);// subs reg amount
		kzme_don_form_enable ('p3', true);// subs reg timing period
		kzme_don_form_enable ('t3', true);// subs reg timing span
		kzme_don_form_enable ('src', true);// payment recurs
		
		// do recurring count subr
		kzme_donate_count ();
		
		// toggle form values
		kzme_don_form_set_value ('cmd', '_xclick-subscriptions');// paypal trans type
		kzme_don_form_set_value ('item_name', 'Recurring Donation');
		kzme_don_form_set_value ('bn', 'MetroEast_Subscribe_WPS_US');// paypal button code
		
	} else {
		// one time donation
		
		// hide payment limit dropdown & total
		d_lim.style.display = 'none';
		d_ttl.style.display = 'none';
		
		// toggle form parts
		kzme_don_form_enable ('amount', true);// one time amount
		kzme_don_form_enable ('a3', false);// subs reg amount
		kzme_don_form_enable ('p3', false);// subs reg timing period
		kzme_don_form_enable ('t3', false);// subs reg timing span
		kzme_don_form_enable ('src', false);// payment recurs
		kzme_don_form_enable ('srt', false);// total payments
		
		// toggle form values
		kzme_don_form_set_value ('cmd', '_donations');// paypal trans type
		kzme_don_form_set_value ('item_name', 'Donation');
		kzme_don_form_set_value ('bn', 'MetroEast_Donate_WPS_US');// paypal button code
	}
	
} //// END kzme_donate_repeat


function kzme_donate_count () {
	//// adjusts form to reflect donation count change
	// refs
	var c_cnt = document.getElementById('KZME_Donate_count'); // count dropdown
	var d_ttl = document.getElementById('KZME_Donate_showTotal'); // total donation text
	
	
	// how many payments...
	var the_count = c_cnt.value;
	
	
	// when no limit is selected, update form accordingly
	if (the_count == 0) {
		// no limit
		kzme_don_form_enable ('srt', false);// total payments
		
		// hide total
		d_ttl.style.display = 'none';
		
	} else {
		// imit
		kzme_don_form_enable ('srt', true);// total payments
		kzme_don_form_set_value ('srt', the_count);// total payments
		
		// calc/show total
		d_ttl.style.display = 'inline';
		kzme_donate_calc();
	}
	
} //// END kzme_donate_count


function kzme_donate_calc () {
	//// calculate and display the total donation for user convenience.
	// refs
	var c_rpt = document.getElementById('KZME_Donate_repeat').checked; // repeats checkbox
	var c_amt = document.getElementById('KZME_Donate_amount'); // amount
	var c_cnt = document.getElementById('KZME_Donate_count'); // count dropdown
	var c_ttl = document.getElementById('KZME_Donate_total'); // ttl
	
	
	// get the count
	the_count = c_cnt.value;
	
	// get donation amount (round wth subr)
	var the_amount = kzme_fix_amount(c_amt.value);
	c_amt.value = the_amount;
	
	
	// is this recurring?
	if (c_rpt) {
		// recurs
		kzme_don_form_set_value ('a3', the_amount);// amount
		
		// limited payment count?
		if (the_count != 0) {
			// do simple math (round with subr)
			var the_total = kzme_fix_amount(the_count * the_amount);
			
			// update display
			c_ttl.value = the_total;
		}
	} else {
		// single
		kzme_don_form_set_value ('amount', the_amount);// amount
	}
	
} //// END kzme_donate_calc


function kzme_fix_amount (amount) {
	//// round to 2 dec, lose negative values...  100.00 def when empty...
	var i = parseFloat(amount);
	if (isNaN(i)) {
		// make it 100.00 if empty
		i = 100.00;
	}
	
	// var minus = '';
	// if(i < 0) { minus = '-'; }
	
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	
	// handle 00
	if (s.indexOf('.') < 0) {
		s += '.00';
	}
	
	// handle 0
	if (s.indexOf('.') == (s.length - 2)) {
		s += '0';
	}
	
	// s = minus + s;
	return s;

} //// END kzme_fix_amount


function kzme_don_form_set_value (the_field, the_value) {
	var the_forms = document.getElementById('KZME_Donation_Form').getElementsByTagName('input');
	for(x = 0; x < the_forms.length; x++) {
		if (the_forms[x].name == the_field) {
			the_forms[x].value = the_value;
		}
	}
}


function kzme_don_form_enable (the_field, the_state) {
	var the_forms = document.getElementById('KZME_Donation_Form').getElementsByTagName('input');
	for(x = 0; x < the_forms.length; x++) {
		if (the_forms[x].name == the_field) {
			if (the_state) {
				the_forms[x].disabled=false;
			} else {
				the_forms[x].disabled=true;
			}
		}
	}
}
