BBDONATIONS = {};
BBDONATIONS.config = {
		active: false,
		data: [{
			name: "Friends of Boston Ballet - Charitable Contribution",
			key: "friends",
			range: "$35 - $1,999",
			paymentMethod: 149,
			levels: [
				{ name: "Donor ($1 - $34)", minAmount: 1, maxAmount: 34 },
				{ name: "Member ($35 - $99)", minAmount: 35, maxAmount: 99 },
				{ name: "Friends ($100 - $249)", minAmount: 100, maxAmount: 249 },
				{ name: "Supporter ($250 - $499)", minAmount: 250, maxAmount: 499 },
				{ name: "Corps de Ballet ($500 - $549)", minAmount: 500, maxAmount: 549 },
				{ name: "Corps de Ballet Family ($550 - $749)", minAmount: 550, maxAmount: 749 },
				{ name: "Soloist ($750 - $799)", minAmount: 750, maxAmount: 799 },
				{ name: "Soloist Family ($800 - $1,249)", minAmount: 800, maxAmount: 1249 },
				{ name: "Principal ($1,250 - $1,349)", minAmount: 1250, maxAmount: 1349 },
				{ name: "Principal Family ($1,350 - $1,999)", minAmount: 1350, maxAmount: 1999 }
			]}, {
			name: "The Benefactors Program - Charitable Contribution",
			key: "benefactors",
			range: "$2,000 - $25,000+",
			paymentMethod: 150,
			levels: [
				{ name: "Benefactors' Circle ($2,000 - $4,999)", minAmount: 2000, maxAmount: 4999 },
				{ name: "Choreographers' Circle ($5,000 - $9,999)", minAmount: 5000, maxAmount: 9999 },
				{ name: "Conductor's Circle ($10,000 - $24,999)", minAmount: 10000, maxAmount: 24999 },
				{ name: "Artistic Director's Circle ($25,000+)", minAmount: 25000, maxAmount: 0 }
			]}, {
			name: "Young Partners - Charitable Contribution",
			key: "young-partners",
			range: "$250 - $25,000+",
			paymentMethod: 151,
			levels: [
				{ name: "Supporter ($250 - $499)", minAmount: 250, maxAmount: 499 },
				{ name: "Corps de Ballet ($500 - $549)", minAmount: 500, maxAmount: 549 },
				{ name: "Corps de Ballet Family ($550 - $749)", minAmount: 550, maxAmount: 749 },
				{ name: "Soloist ($750 - $799)", minAmount: 750, maxAmount: 799 },
				{ name: "Soloist Family ($800 - $1,249)", minAmount: 800, maxAmount: 1249 },
				{ name: "Principal ($1,250 - $1,349)", minAmount: 1250, maxAmount: 1349 },
				{ name: "Principal Family ($1,350 - $1,999)", minAmount: 1350, maxAmount: 1999 },
				{ name: "Benefactors' Circle ($2,000 - $4,999)", minAmount: 2000, maxAmount: 4999 },
				{ name: "Choreographers' Circle ($5,000 - $9,999)", minAmount: 5000, maxAmount: 9999 },
				{ name: "Conductor's Circle ($10,000 - $24,999)", minAmount: 10000, maxAmount: 24999 },
				{ name: "Artistic Director's Circle ($25,000+)", minAmount: 25000, maxAmount: 0 }
			]}, {
				name: "Associate - Non-Member",
				key: "associate",
				range: "$1.00 - $34.99",
				paymentMethod: 150,
				levels: [
					{ name: "Associate Non-Member ($1.00 - $34.00)", minAmount: 1, maxAmount: 3499 }
				]},
			]
};

BBDONATIONS.init = function() {
	if (BBDONATIONS.config.active) {
		BBDONATIONS.doFundSelection();
		$j('#donate-now input[name="fund"]').click(BBDONATIONS.doFundSelection);
		$j('#donate-now input.inactive').click(BBDONATIONS.setActive)
		$j('#donate-now input.inactive').blur(BBDONATIONS.setActive);
		$j('#add-to-cart-button').unbind("click");
		$j('#add-to-cart-button').click(BBDONATIONS.Validation.validate);
	}
};

BBDONATIONS.setActive = function() {
	elem = $j(this);
	LOG("setActive!");
	if(elem.hasClass('inactive')) {
		elem.removeClass('inactive');
		elem.data('value', elem.val());
		elem.val('');
	} else {
		if (elem.val().length === 0) {
			elem.addClass('inactive');
			elem.val(elem.data('value'));
		}
	}
};

BBDONATIONS.doFundSelection = function() {
	var fund = BBDONATIONS.getSelectedFund();
	$j('#donation-range').html(fund.range);
};

BBDONATIONS.getSelectedFund = function() {
	var selectedFund = $j('#donate-now input[name="fund"]:checked').val();
	var fund = BBDONATIONS.getFund(selectedFund);
	
	return fund;
};

BBDONATIONS.getFund = function(fundKey) {
	for(var i = 0; i < BBDONATIONS.config.data.length; i++) {
		var fund = BBDONATIONS.config.data[i];

		if(fund.key === fundKey) {
			return fund;
		}
	}	
};

BBDONATIONS.getAmount = function() {
	var donationAmount = $j('#donation-amount').val();
	return parseInt(donationAmount);
}

BBDONATIONS.getFundLevel = function(fund, amount) {
	for(var i = 0; i < fund.levels.length; i++) {
		var level = fund.levels[i];
		if (amount >= level.minAmount && amount <= level.maxAmount) {
			return level.name;
		}
	}
	
	return "";
}

BBDONATIONS.generateCartNotes = function() {
	var fund = BBDONATIONS.getSelectedFund();
	var notes = "";
	notes += "<strong>" + fund.name + "</strong><br />";
	notes += BBDONATIONS.getFundLevel(BBDONATIONS.getSelectedFund(), BBDONATIONS.getAmount());
	
	$j('#cartNotes').val(notes);
}

BBDONATIONS.setPaymentMethod = function() {
	var fund = BBDONATIONS.getSelectedFund();
	LOG("SETTING METHOD " + fund.paymentMethod);
	$j('#METHOD').val(fund.paymentMethod);
	$j('#FUNDNAME').val(fund.name);
}

/*
 * Validation
 */
 BBDONATIONS.Validation = {};
 
 BBDONATIONS.Validation.validate = function() {
 	$j('#donate-now .important').remove();
 	var errors = false;
 	
 	//Check that they picked a renewal option
 	var renewalButtons = $j('#donate-now input[name="renewal"]:checked');
 	if (renewalButtons.length != 1) {
 		$j('#donate-now input[name="renewal"]:first').parent().before('<p class="important">Please tell us whether you are a returning member or not.</p>');
 		errors = true;
 	}
 	
 	//Check that the donation about is present and a valid number
 	var donationAmount = $j('#donation-amount').val();
 	if (!donationAmount.isInt()) {
 		$j('#donation-amount').parent().before('<p class="important">Donation amount required. Please enter a number only. No special characters ($, \',\').</p>');
 		errors = true;
 	}
 	
 	//Check that the donation amount is within the rage we need
 	var currentFund = BBDONATIONS.getSelectedFund();
 	if(BBDONATIONS.getFundLevel(currentFund, donationAmount) == "") {
 		$j('#donation-amount').parent().before('<p class="important">The donation amount should be within the range of the fund you selected: '+ currentFund.range +'</p>');
 		errors = true;
 	}
 	
 	//If designation is other, make sure the name is there
 	var designationButton = $j('#donate-now input[name="designation"]:checked');
 	if (designationButton.val() === "other") {
 		var otherDesignation = $j('#other-designation');
 		if(otherDesignation.val().trim().length == 0 || otherDesignation.hasClass('inactive')) {
 			otherDesignation.parent().before('<p class="important">Please enter a designation for your gift.</p>');
 			errors = true;
 		}
 	}
 	
 	//if they want their name recognized, make sure they've entered a name.
 	var recognition = $j('#donate-now input[name="donor-recognition"]:checked');
 	if (recognition.val() === "name") {
 		var recognitionName = $j('#donor-name');
 		if(recognitionName.val().trim().length == 0 || recognitionName.hasClass('inactive')) {
 			recognitionName.parent().before('<p class="important">Please enter your name so that it may be recognized by others.</p>');
 			errors = true;
 		}
 	}
 	
 	//make sure they've entered a name if they want to dedicate it
 	var dedication = $j('#donate-now input[name="dedication"]:checked');
 	if(dedication.val() === "honor" || dedication.val() === "memory") {
 		if(dedication.val() === "honor") {
 			var honorName = $j('#honor-name');
 			if(honorName.val().trim().length == 0 || honorName.hasClass('inactive')) {
	 			honorName.parents('table:first').before('<p class="important">Please enter the name of the person you wish to honor.</p>');
	 			errors = true;
	 		}
 		}
 		
 		if(dedication.val() === "memory") {
 			var memoryName = $j('#honor-name');
 			if(memoryName.val().trim().length == 0 || memoryName.hasClass('inactive')) {
	 			memoryName.parents('table:first').before('<p class="important">Please enter the name of the person you would like remembered.</p>');
	 			errors = true;
	 		}
 		}
 	}
 	
 	if(!errors) {
 		//console.log("submitting...");
 		BBDONATIONS.generateCartNotes();
 		BBDONATIONS.setPaymentMethod();
 		$j('#donate-now-form').submit();
 	}
 };


