/*
Description:	Custom JavaScript cart for Johndrow Vineyards. Collects variables and sends them via a simple PayPal button to the PayPal API
Author:			Nick Sayre for Maxus Creative
Web:			www.Maxuscreative.com
email: 			nick@maxuscreative.com
*/

// =Outline
/*---------------------------------------------------------------------------------------------------------------
1. 	Declare variables
2.	Triggered Functions
	A.	update_clarity6
			-update stored qty of given product
	B.	assembleCart
			-Update the cart's listing (at the bottom of the page)
			-Update the variable 'cartContentsPaypal', which is ultimately sent to PayPal
	C.	check
			-Returns the name and amount of the product, if it has been set (to more than 0)
	D.	selectState
			-Save the value of the selected HTML state
	E. addToCart.
		-Update the vars 'amount' and 'shippingAmount' which are ultimately sent to PayPal


---------------------------------------------------------------------------------------------------------------*/


// =Variables
//---------------------------------------------------------------------------------------------------------------

// usShipping applies to wine products to determine shipping costs
var usShipping = [	
	// Name of State, 6-pack cost, 12-pack cost, shipping type, tax, abbreviation
	['Alabama', 58, 94, '3-day', .04, 'AL'],
	['Alaska', 87, 124, '2-day', 0, 'AK'],
	['Arizona', 33, 50, 'Ground', .056, 'AZ'],
	['Arkansas', 38, 56, 'Ground', .06, 'AR'],
	['California', 26, 36, 'Ground', .0725, 'CA'],
	['Colorado', 33, 50, 'Ground', .029, 'CO'],
	['Connecticut', 38, 56, 'Ground', .06, 'CT'],
	['Delaware', 58, 94, 'Ground', 0, 'DE'],
	['District of Columbia', 38, 56, 'Ground', .0575, 'DC'],
	['Florida', 38, 56, 'Ground', .06, 'FL'],
	['Georgia', 38, 56, 'Ground', .04, 'GA'],
	['Hawaii', 87, 124, '2-day', .04, 'HI'],
	['Idaho', 33, 50, 'Ground', .06, 'ID'],
	['Illinois', 38, 56, 'Ground', .06, 'IL'],
	['Indiana', 38, 56, 'Ground', .0625, 'IN'],
	['Iowa', 35, 53, 'Ground', .06, 'IA'],
	['Kansas', 55, 91, '3-day', .053, 'KS'],
	['Kentucky', 58, 94, '3-day', .06, 'KY'],
	['Louisiana', 38, 94, 'Ground', .04, 'LA'],
	['Maine', 58, 94, '3-day', .05, 'ME'],
	['Maryland', 58, 94, '3-day', .06, 'MD'],
	['Massachusetts', 70, 130, '3-day', .05, 'MA'],
	['Michigan', 38, 56, 'Ground', .06, 'MI'],
	['Minnesota', 35, 53, 'Ground', .065, 'MN'],
	['Mississippi', 58, 94, '3-day', .07, 'MS'],
	['Missouri', 35, 53, 'Ground', .04225, 'MO'],
	['Montana', 49, 81, '3-day', 0, 'MT'],
	['Nebraska', 35, 53, 'Ground', .055, 'NE'],
	['Nevada', 26, 36, 'Ground', .065, 'NV'],
	['New Jersey', 58, 94, '3-day', .07, 'NJ'],
	['New Mexico', 33, 50, 'Ground', .05, 'NM'],
	['New York', 38, 56, 'Ground', .04, 'NY'],
	['North Carolina', 38, 56, 'Ground', .0425, 'NC'],
	['North Dakota', 35, 53, 'Ground', .05, 'ND'],
	['Ohio', 38, 56, 'Ground', .055, 'OH'],
	['Oklahoma', 38, 56, 'Ground', .045, 'OK'],
	['Oregon', 26, 36, 'Ground', 0, 'OR'],
	['Pennsylvania', 70, 130, '3-day', .06, 'PA'],
	['Rhode Island', 58, 94, '3-day', .07, 'RI'],
	['South Carolina', 38, 56, 'Ground', .06, 'SC'],
	['South Dakota', 55, 91, '3-day', .04, 'SD'],
	['Tennessee', 58, 94, '3-day', .07, 'TN'],
	['Texas', 35, 53, 'Ground', .0625, 'TX'],
	['Utah', 65, 120, '3-day', .0465, 'UT'],
	['Vermont', 38, 56, 'Ground', .06, 'VT'],
	['Virginia', 58, 94, '3-day', .05, 'VA'],
	['Washington', 33, 50, 'Ground', .065, 'WA'],
	['West Virginia', 38, 56, 'Ground', .06, 'WV'],
	['Wisconsin', 38, 56, 'Ground', .05, 'WI'],
	['Wyoming', 33, 50, 'Ground', .04, 'WY']
];

var selectedState = usShipping[0];  // Set this value initially in case user does not use pulldown menu (they live in Alabama)
var amount = 0;						// The sum of each product's (qty * price)
var shippingAmount = 0;
var tax = usShipping[0][4];			// Default to Alabama tax
var cartContents = 0;				// String of cart contents
var cartContentsPaypal = 0;			// String of cart contents for Paypal's 107 character limit
var shippingAmount = 0;				// qty of items * state's shipping cost for each type of item
var total = 0;						// Qty all items + tax + shipping;
var flatShipping = 2;				// Flat shipping fee applied to non-wine products



// Products
// Quantity, Price of each unit, Name of Product, short name
var clarity6 = [0, 70, 'Clarity 6-Pack', 'Cla6'];
var clarity12 = [0, 144, 'Clarity 12-Pack', 'Cla12'];
var cab6 = [0, 120, 'Cab 6-Pack', 'Cab6'];
var cab12 = [0, 240, 'Cab 12-Pack', 'Cab12'];
var reserve6 = [0, 240, 'Reserve 6-Pack', 'Res6'];
var reserve12 = [0, 480, 'Reserve 12-Pack', 'Res12'];
var forum6 = [0, 450, 'Forum 6-Pack', 'Frm6'];
var forum12 = [0, 900, 'Forum 12-Pack', 'Frm12'];
var redlovers = [0, 250, 'Red Lover\'s Pack', 'Rlov'];
var samplepack = [0, 130, 'Sample Pack', 'Smpl'];

var cooler = [0, 25, 'JV Cooler', 'Cooler'];
var beachChair = [0, 30, 'JV Beach Chair', 'Chair'];

var logoTee_xs = [0, 20, 'Logo Tee XS', 'LogoTee_XS'];
var logoTee_s = [0, 20, 'Logo Tee S', 'LogoTee_S'];
var logoTee_m = [0, 20, 'Logo Tee M', 'LogoTee_M'];
var logoTee_l = [0, 20, 'Logo Tee L', 'LogoTee_L'];
var logoTee_xl = [0, 20, 'Logo Tee XL', 'LogoTee_XL'];

var happy_xs = [0, 20, 'Happy Place #1 Tee XS', 'happy1_XS'];
var happy_s = [0, 20, 'Happy Place #1 Tee S', 'happy1_S'];
var happy_m = [0, 20, 'Happy Place #1 Tee M', 'happy1_M'];
var happy_l = [0, 20, 'Happy Place #1 Tee L', 'happy1_L'];
var happy_xl = [0, 20, 'Happy Place #1 Tee XL', 'happy1_XL'];

var happy2_xs = [0, 20, 'Happy Place #2 Tee XS', 'happy2_XS'];
var happy2_s = [0, 20, 'Happy Place #2 Tee S', 'happy2_S'];
var happy2_m = [0, 20, 'Happy Place #2 Tee M', 'happy2_M'];
var happy2_l = [0, 20, 'Happy Place #2 Tee L', 'happy2_L'];
var happy2_xl = [0, 20, 'Happy Place #2 Tee XL', 'happy2_XL'];



// 2. =Functions
//---------------------------------------------------------------------------------------------------------------


// 2A. Update the stored quantity of indicated item in the cart
function update_clarity6(value) {								// Value comes from the HTML value=""
	if (isNaN(value)){											// If this is not a number then
		document.getElementById('clarity6_qty').value = 0;		// Set the HTML value to 0
	} else {													// Otherwise
		clarity6[0] = value;									// Set the product's quantity in the store
		assembleCart();											// Run the function to update store variables (totals)
	}
}
function update_clarity12(value) {
if (isNaN(value)){
		document.getElementById('clarity12_qty').value = 0;
	} else {
		clarity12[0] = value;
		assembleCart();
	}
}
function update_cab6(value) {
	if (isNaN(value)){
		document.getElementById('cab6_qty').value = 0;
	} else {
		cab6[0] = value;
		assembleCart();
	}
}
function update_cab12(value) {
	if (isNaN(value)){
		document.getElementById('cab12_qty').value = 0;
	} else {
		cab12[0] = value;
		assembleCart();
	}
}
function update_reserve6(value) {
if (isNaN(value)){
		document.getElementById('reserve6_qty').value = 0;
	} else {	
		reserve6[0] = value;
		assembleCart();
	}
}
function update_reserve12(value) {
if (isNaN(value)){
		document.getElementById('reserve12_qty').value = 0;
	} else {	
		reserve12[0] = value;
		assembleCart();
	}
}

function update_forum6(value) {
	if (isNaN(value)){
		document.getElementById('forum6_qty').value = 0;
	} else {
		forum6[0] = value;
		assembleCart();
	}
}
function update_forum12(value) {
	if (isNaN(value)){
		document.getElementById('forum12_qty').value = 0;
	} else {
		forum12[0] = value;
		assembleCart();
	}
}
function update_redlovers(value) {
	if (isNaN(value)){
		document.getElementById('redlovers_qty').value = 0;
	} else {
		redlovers[0] = value;
		assembleCart();
	}
}
function update_samplepack(value) {
	if (isNaN(value)){
		document.getElementById('samplepack_qty').value = 0;
	} else {
		samplepack[0] = value;
		assembleCart();
	}
}
function update_cooler(value) {
	if (isNaN(value)){
		document.getElementById('cooler_qty').value = 0;
	} else {
		cooler[0] = value;
		assembleCart();
	}
}
function update_beachChair(value) {
	if (isNaN(value)){
		document.getElementById('beachChair_qty').value = 0;
	} else {
		beachChair[0] = value;
		assembleCart();
	}
}

//logoTee
function update_logoTee_xs(value) {
	if (isNaN(value)){
		document.getElementById('logoTee_xs_qty').value = 0;
	} else {
		logoTee_xs[0] = value;
		assembleCart();
	}
}
function update_logoTee_s(value) {
	if (isNaN(value)){
		document.getElementById('logoTee_s_qty').value = 0;
	} else {
		logoTee_s[0] = value;
		assembleCart();
	}
}
function update_logoTee_m(value) {
	if (isNaN(value)){
		document.getElementById('logoTee_m_qty').value = 0;
	} else {
		logoTee_m[0] = value;
		assembleCart();
	}
}
function update_logoTee_l(value) {
	if (isNaN(value)){
		document.getElementById('logoTee_l_qty').value = 0;
	} else {
		logoTee_l[0] = value;
		assembleCart();
	}
}
function update_logoTee_xl(value) {
	if (isNaN(value)){
		document.getElementById('logoTee_xl_qty').value = 0;
	} else {
		logoTee_xl[0] = value;
		assembleCart();
	}
}

//happy tee
function update_happy_xs(value) {
	if (isNaN(value)){
		document.getElementById('happy_xs_qty').value = 0;
	} else {
		happy_xs[0] = value;
		assembleCart();
	}
}
function update_happy_s(value) {
	if (isNaN(value)){
		document.getElementById('happy_s_qty').value = 0;
	} else {
		happy_s[0] = value;
		assembleCart();
	}
}
function update_happy_m(value) {
	if (isNaN(value)){
		document.getElementById('happy_m_qty').value = 0;
	} else {
		happy_m[0] = value;
		assembleCart();
	}
}
function update_happy_l(value) {
	if (isNaN(value)){
		document.getElementById('happy_l_qty').value = 0;
	} else {
		happy_l[0] = value;
		assembleCart();
	}
}
function update_happy_xl(value) {
	if (isNaN(value)){
		document.getElementById('happy_xl_qty').value = 0;
	} else {
		happy_xl[0] = value;
		assembleCart();
	}
}

//happy2 tee
function update_happy2_xs(value) {
	if (isNaN(value)){
		document.getElementById('happy2_xs_qty').value = 0;
	} else {
		happy2_xs[0] = value;
		assembleCart();
	}
}
function update_happy2_s(value) {
	if (isNaN(value)){
		document.getElementById('happy2_s_qty').value = 0;
	} else {
		happy2_s[0] = value;
		assembleCart();
	}
}
function update_happy2_m(value) {
	if (isNaN(value)){
		document.getElementById('happy2_m_qty').value = 0;
	} else {
		happy2_m[0] = value;
		assembleCart();
	}
}
function update_happy2_l(value) {
	if (isNaN(value)){
		document.getElementById('happy2_l_qty').value = 0;
	} else {
		happy2_l[0] = value;
		assembleCart();
	}
}
function update_happy2_xl(value) {
	if (isNaN(value)){
		document.getElementById('happy2_xl_qty').value = 0;
	} else {
		happy2_xl[0] = value;
		assembleCart();
	}
}


// 2B. assembleCart
// Updates the variable 'cartContents', which is displayed at the bottom of the page
// Updates the variable 'cartContentsPaypal', which is sent to PayPal as the item description. This variable has a 107 character limit on PayPal's end
function assembleCart() {
	//names and quantities of products to be purchased concatenated
	cartContents =																
	check(reserve6[0], reserve6[2]) +
	check(reserve12[0], reserve12[2]) +
	check(cab6[0], cab6[2]) +
	check(cab12[0], cab12[2]) +
	check(clarity6[0], clarity6[2]) +
	check(clarity12[0], clarity12[2]) +
	check(forum6[0], forum6[2]) +
	check(forum12[0], forum12[2]) +
	check(redlovers[0], redlovers[2]) +
	check(samplepack[0], samplepack[2]) +
	check(cooler[0], cooler[2]) +
	check(beachChair[0], beachChair[2]) +	
	check(logoTee_xs[0], logoTee_xs[2]) +
	check(logoTee_s[0], logoTee_s[2]) +
	check(logoTee_m[0], logoTee_m[2]) +
	check(logoTee_l[0], logoTee_l[2]) +
	check(logoTee_xl[0], logoTee_xl[2]) +
	check(happy_xs[0], happy_xs[2]) +
	check(happy_s[0], happy_s[2]) +
	check(happy_m[0], happy_m[2]) +
	check(happy_l[0], happy_l[2]) +
	check(happy_xl[0], happy_xl[2]) +
	check(happy2_xs[0], happy2_xs[2]) +
	check(happy2_s[0], happy2_s[2]) +
	check(happy2_m[0], happy2_m[2]) +
	check(happy2_l[0], happy2_l[2]) +
	check(happy2_xl[0], happy2_xl[2]) +
	
	selectedState[3] + ' to ' + selectedState[0];	//Concatenate the shipping type and state
	
	//names and quantities of products to be purchased concatenated for Paypal
	cartContentsPaypal =
	check(reserve6[0], reserve6[3]) +
	check(reserve12[0], reserve12[3]) +
	check(cab6[0], cab6[3]) +
	check(cab12[0], cab12[3]) +
	check(clarity6[0], clarity6[3]) +
	check(clarity12[0], clarity12[3]) +
	check(forum6[0], forum6[3]) +
	check(forum12[0], forum12[3]) +
	check(redlovers[0], redlovers[3]) +
	check(samplepack[0], samplepack[3]) +
	check(cooler[0], cooler[3]) +
	check(beachChair[0], beachChair[3]) +
	check(logoTee_xs[0], logoTee_xs[3]) +
	check(logoTee_s[0], logoTee_s[3]) +
	check(logoTee_m[0], logoTee_m[3]) +
	check(logoTee_l[0], logoTee_l[3]) +
	check(logoTee_xl[0], logoTee_xl[3]) +
	check(happy_xs[0], happy_xs[3]) +
	check(happy_s[0], happy_s[3]) +
	check(happy_m[0], happy_m[3]) +
	check(happy_l[0], happy_l[3]) +
	check(happy_xl[0], happy_xl[3]) +
	check(happy2_xs[0], happy2_xs[3]) +
	check(happy2_s[0], happy2_s[3]) +
	check(happy2_m[0], happy2_m[3]) +
	check(happy2_l[0], happy2_l[3]) +
	check(happy2_xl[0], happy2_xl[3]) +
	selectedState[3] + ' to ' + selectedState[5];	//Concatenate the shipping type and state abbreviation
	
	document.getElementById('item_name').value = cartContentsPaypal;	//Store the value in the button's HTML
}


// 2C. check. Returns the name and amount of the product, if it has been set (to more than 0)
function check(qty, name) {
	if (qty > 0) {									// Has the quantity of the product been set (more than 0?)
		return('(' + qty + ') ' + name +", ");		// If so, return the amount and product name
	} else {
		return("");									// Otherwise, return nothing
	}
}

// 2D. selectState. Get the value from the HTML, match it up with the array in JS and save the result
function selectState(selected) {			// selected is the ID# from the HTML
	selectedState = usShipping[selected];	// save the value of usShipping with is stored at the 'selected's value					
	assembleCart();							// Update the cart
}

// addToCart. Runs when the submit button gains focus, or when an imput field loses focus
// Updates the var 'amount'. The sum of each product's (qty * price)
// Updates the var 'shippingAmount'.
function addToCart() {
    var amount =
	(+clarity6[0] * +clarity6[1])
	+(clarity12[0] * +clarity12[1])
	+(cab6[0] * +cab6[1]) + +(cab12[0] * +cab12[1])
	+(reserve6[0] * +reserve6[1]) + +(reserve12[0] * +reserve12[1])
	+(forum6[0] * +forum6[1]) + +(forum12[0] * +forum12[1])
	+(redlovers[0] * +redlovers[1])
	+(samplepack[0] * +samplepack[1])
	+(cooler[0] * +cooler[1])
	+(beachChair[0] * +beachChair[1])
	+(logoTee_xs[0] * +logoTee_xs[1])
	+(logoTee_s[0] * +logoTee_s[1])
	+(logoTee_m[0] * +logoTee_m[1])
	+(logoTee_l[0] * +logoTee_l[1])
	+(logoTee_xl[0] * +logoTee_xl[1])
	
	+(happy_xs[0] * +happy_xs[1])
	+(happy_s[0] * +happy_s[1])
	+(happy_m[0] * +happy_m[1])
	+(happy_l[0] * +happy_l[1])
	+(happy_xl[0] * +happy_xl[1])
	
	+(happy2_xs[0] * +happy2_xs[1])
	+(happy2_s[0] * +happy2_s[1])
	+(happy2_m[0] * +happy2_m[1])
	+(happy2_l[0] * +happy2_l[1])
	+(happy2_xl[0] * +happy2_xl[1]);
	
	
	var tax = selectedState[4];						// Store the tax of the US state that is saved in the variable 'selectedState'
	total = Math.round(amount *(1 + +tax));			// total = rounded amount * tax of state
	
	shippingAmount =
	(+clarity6[0] + +cab6[0] + +reserve6[0] + +forum6[0] + +redlovers[0] + +samplepack[0]) * +selectedState[1]								// (amount of 6 packs) * selected US states's 6pack cost
	+ (+clarity12[0] + +cab12[0] + +reserve12[0] + +forum12[0]) * +selectedState[2]															// add in (amount of 12 packs) * selected US states's 12pack cost
	+ (+cooler[0] + +beachChair[0]
	+ +logoTee_xs[0] + +logoTee_s[0] + +logoTee_m[0] + +logoTee_l[0] + +logoTee_xl[0]
	+ +happy_xs[0] + +happy_s[0] + +happy_m[0] + +happy_l[0] + +happy_xl[0]
	+ +happy2_xs[0] + +happy2_s[0] + +happy2_m[0] + +happy2_l[0] + +happy2_xl[0]
	) * +flatShipping;		// add in (amount of (non-wine merch) * the flat shipping rate.

	document.getElementById('total_amount').value = total;				//Send amount to Paypal btn
	document.getElementById('total_shipping').value = shippingAmount;	//Send shippingAmount to Paypal btn
	userFeedback();
}

function userFeedback() {
	var cart = document.getElementById('cart');
	cart.innerHTML = cartContents;
	var price = document.getElementById('price');
	price.innerHTML = '$' + (total + +shippingAmount);		
}
