/*
---
description: Capital One Venture Card MicroSite Business Logic and Data
last_updated: Fri Feb 26 17:10:52 -0500 2010
...
*/

var Object_sumValues = function(object){
	if (!isNaN(object)) return Number(object);
	var total = 0;
	for (var property in object)
		total += object[property];
	return total;
};


// Calculators

function calculateAnnualPoints_forCard_fromSpend_withCategories( card_id, monthlySpend_value, whereISpendTheMost_byCategory ){
	monthlySpend_value = Number(monthlySpend_value) || 0;
	whereISpendTheMost_byCategory = whereISpendTheMost_byCategory || 'custom';
	var monthlySpend_byCategory = {};
	
	for (var i=0, category, multiplier; category = SPEND_CATEGORIES[i]; i++){
		multiplier = whereISpendTheMost_byCategory[category];
		multiplier = multiplier || (DEFAULTS_WHERE_I_SPEND_THE_MOST[whereISpendTheMost_byCategory]||{})[category];
		monthlySpend_byCategory[category] = monthlySpend_value * multiplier;
	}
	return calculatePoints_forCard_fromSpendCategories_andMonths( card_id, monthlySpend_byCategory, 12 );
};

function calculatePoints_forCard_fromSpendCategories_andMonths( card_id, monthlySpend_byCategory, months_count ){
	
	if (!(Cards[card_id] && Cards[card_id].miles_earned_per_dollar_spent)) return 0;
	
	if (!monthlySpend_byCategory) return 0;
	var monthlySpend_total = Object_sumValues(monthlySpend_byCategory);
	var total_annual_rewards = 0;
	
	months_count = months_count || 12;
	
	for (var i=0, category, multiplier; category = SPEND_CATEGORIES[i]; i++){
		multiplier = Cards[card_id].miles_earned_per_dollar_spent[category];
		total_annual_rewards += monthlySpend_byCategory[category] * multiplier;
	}
	
	return Math.round(total_annual_rewards * months_count) || 0;
};


// Categories to loop over
var SPEND_CATEGORIES = "travel gas entertainment shopping utilities other".split(' ');


// Defaults for the UI
var DEFAULTS_WHERE_I_SPEND_THE_MOST = {
	travel         : {
		travel         : 0.60,
		gas            : 0.08,
		entertainment  : 0.04,
		shopping       : 0.23,
		utilities      : 0.02,
		other          : 0.03
	},
	shopping       : {
		travel         : 0.18,
		gas            : 0.15,
		entertainment  : 0.10,
		shopping       : 0.45,
		utilities      : 0.04,	/*was 0.05, changed to total 100%*/
		other          : 0.08
	},
	entertainment  : {
		travel         : 0.07,
		gas            : 0.07,
		entertainment  : 0.60,
		shopping       : 0.21,
		utilities      : 0.02,
		other          : 0.03
	},
	custom         : {
		travel         : 0.1610,
		gas            : 0.1600,
		entertainment  : 0.0870,
		shopping       : 0.4780,
		utilities      : 0.0440,
		other          : 0.0700
	}
};


// Uses dollar values instead of percentages
var CUSTOM_WHERE_I_SPEND_THE_MOST = {
	travel        : 1250 * 0.1610,
	gas           : 1250 * 0.1600,
	entertainment : 1250 * 0.0870,
	shopping      : 1250 * 0.4780,
	utilities     : 1250 * 0.0440,
	other         : 1250 * 0.0700
};


// Specific Card Data
var Cards = {
	
	capital_one_venturesm_card: {
		name: "Capital One® Venture<sup>SM</sup> Card",
		miles_earned_per_dollar_spent: {
			travel        : 2,
			gas           : 2,
			entertainment : 2,
			shopping      : 2,
			utilities     : 2,
			other         : 2
		}
	},
	
	gold_delta_skymiles_american_express_card: {
		name: "Gold Delta SkyMiles® American Express® Card",
		miles_earned_per_dollar_spent: {
			travel        : 2,
			gas           : 1,
			entertainment : 1,
			shopping      : 1,
			utilities     : 1,
			other         : 1
		}
	},
	
	united_mileage_plus_visa_signature_card: {
		name: "United Mileage Plus® Visa Signature® Card",
		miles_earned_per_dollar_spent: {
			travel        : 2,
			gas           : 1,
			entertainment : 1,
			shopping      : 1,
			utilities     : 1,
			other         : 1
		}
	},
	
	citi_gold_aadvantage_world_mastercard: {
		name: "Citi® Gold AAdvantage® World MasterCard®",
		miles_earned_per_dollar_spent: {
			travel        : 1,
			gas           : 1,
			entertainment : 1,
			shopping      : 1,
			utilities     : 1,
			other         : 1
		}
	},
	
/*
	us_airways_dividend_miles_world_mastercard: {
		name: "US Airways® Dividend Miles World MasterCard®",
		miles_earned_per_dollar_spent: {
			travel        : 1,
			gas           : 1,
			entertainment : 1,
			shopping      : 1,
			utilities     : 1,
			other         : 1
		}
	},
*/
	
	american_express_preferred_rewards_gold_card: {
		name: "American Express® Preferred Rewards Gold Card",
		miles_earned_per_dollar_spent: {
			travel        : 2,
			gas           : 1,
			entertainment : 1,
			shopping      : 1,
			utilities     : 1,
			other         : 1
		}
	},
	
	chase_sapphire_preferred: {
		name: "Chase Sapphire® Preferred",
		note: "7% annual dividend (factored into miles earned per purchase dollar… 1.07)",
		miles_earned_per_dollar_spent: {
			travel        : 2 + 0.14,
			gas           : 1 + 0.07,
			entertainment : 2 + 0.14,
			shopping      : 1 + 0.07,
			utilities     : 1 + 0.07,
			other         : 1 + 0.07
		}
	}
	
};



