// Javascript stuff for the APS Web Shop.
// This file is only used by the basket.php file.
// Does live recalculations of the Basket totals
// without having to reload the page constantly.

// Globals
var gBasketWind = '';
// This needs to be changed to reflect the final server path.
var gPathToBasketPHP = '/projects/alpine/shop/code/basket.php';
// The Size limit for when a package goes from 'Small' to 'Large'
// in the shipping options.
var gMaxSmallShippingSize = '30';		// for instance '30' would be for 20x30.

//-------------------------------------------------------------
// Called when the user changes the value of one of the Basket's
// form fields. Recalculates the Basket totals...
//-------------------------------------------------------------
function recalcBasket()
{	
	//alert('Changed Qty ~ Would recalculate the Baskets totals');
	
	var basketForm = 'Basket';
	var inputName = '';
	var inputValue = '';
	
	var lineCounter = -1;
	var inputNameLength;
	var inputNameQtyPart;
	var inputNameSizePricePart;
	var inputNameIndexPart;
	var sizePriceArray;
	var sizePart1;
	var sizePart2;
	var sizeArray;
	var lineTotalName;
	var lineTotalAmount;
	var qtyIntTest;
	
	var shippingSizeTemp = 'Small';
	var updateShippingFlag = false;
	var shippingLocation;
	
	var qty = 0;
	var itemSize = 0;
	var itemPrice = 0;
	var subTotal = 0;
	
	for (var elems=0; elems<document.forms[basketForm].length; elems++ )
	{
		// Make sure that we have a Form element with a name.
		if ( document.forms[basketForm].elements[elems].name == "" ) { continue; }
		
		//alert( document.forms[basketForm].elements[elems].name + ' value is : ' + document.forms[basketForm].elements[elems].value );
		inputName = document.forms[basketForm].elements[elems].name;
		inputValue = document.forms[basketForm].elements[elems].value;
		//alert( 'Input Name: '  + inputName);
		
		//-------
		// Check if this element is XQty -- if yes, start of new LineItem
		// Update previous LineTotal (if it exists)
		inputNameLength = inputName.length;
		inputNameQtyPart = inputName.substring( inputNameLength - 3, inputNameLength );
		inputNameIndexPart = inputName.substring( 0, inputNameLength - 3 );
		//alert( 'Index: '  + inputNameIndexPart + ' ~ Qty: ' + inputNameQtyPart );
		if ( inputNameQtyPart == 'qty' ) {
			lineCounter++;
			
			//alert( 'Index: '  + inputNameIndexPart + ' ~ Qty: ' + inputNameQtyPart );
			
			// Guard against inputting a negative number, decimal or non numeric.
			//alert( 'InputValue: ' + inputValue );
			qtyIntTest = parseInt(inputValue, 10);
			if ( inputValue < 0 || isNaN(qtyIntTest) ) {
				inputValue = 0;
				document.forms[basketForm].elements[inputName].value = inputValue;
			} else {
				inputValue = qtyIntTest;
				document.forms[basketForm].elements[inputName].value = inputValue;
			}			
			qty = inputValue;
		}
		
		//-------
		// Check if this is Size&Price menu.
		// Get the price from the current selection.
		inputNameSizePricePart = inputName.substring( inputNameLength - 12, inputNameLength );
		inputNameIndexPart = inputName.substring( 0, inputNameLength - 12 );
		//alert( 'Index: '  + inputNameIndexPart + ' ~ SizePrice: ' + inputNameSizePricePart );
		if ( inputNameSizePricePart == 'sandpdrpdown' ) {
			//alert( 'Index: '  + inputNameIndexPart + ' ~ SizePrice: ' + inputNameSizePricePart );
			// Need to pull out the price from the 20x30:25 
			sizePriceArray = inputValue.split(':');
			if ( sizePriceArray.length != 2 ) {
				alert( "Error: Couldn't calculate the LineTotal for line: " +  lineCounter + ". Bad SizePrice value: " +  inputValue );
				return;
			}
			itemSize = sizePriceArray[0];
			itemPrice = sizePriceArray[1];
			//alert( 'Line ' + lineCounter + ' Total is: ' + qty * itemPrice );
			
			// Updates this Line's Total.
			//alert( 'Single Item Price: ' + itemPrice );
			lineTotalName = lineCounter + 'lineTotal';
			lineTotalAmount = ( qty * itemPrice ) + '.00';		// Format to a number string.
			document.forms[basketForm].elements[lineTotalName].value = lineTotalAmount;
			
			// Add this line item to the Sub Total
			subTotal = subTotal + ( qty * itemPrice );
			
			// Need to see if the shipping display should be Small or Large.
			// The actual control of this happens at the end of this function.
			// shippingSizeTemp
			sizeArray = itemSize.split('x');
			if ( sizeArray.length != 2 ) {
				alert( "Error: Couldn't calculate the specific Sizes for line: " +  lineCounter + ". Bad sizeArray value: " +  itemSize + ' (should be in the form of 20x30)' );
				return;
			}
			sizePart1 = sizeArray[0];
			sizePart2 = sizeArray[1];
			//alert( 'Size Part 1 ' + sizePart1 + '. Size Part 2: ' + sizePart2);
			if ( sizePart1 > sizePart2 ) {
				if ( sizePart1 > gMaxSmallShippingSize ) { shippingSizeTemp = 'Large'; }
			} else {
				if ( sizePart2 > gMaxSmallShippingSize ) { shippingSizeTemp = 'Large'; }
			}
		}
		
		
		//-------
		// Check if this is the Shipping RB group
		// Get its selected value
		if ( inputNameSizePricePart == 'ship' ) {
			//alert( 'Found Shipping RB. Value: ' + inputValue );		// returns EuropeSmall and WorldSmall or EuropeLarge and WorldLarge
			switch (inputValue) {
			case "EuropeSmall" :
				if ( document.Basket.ship[0].checked ) {
					//alert( 'Europe is Selected.' );
					shippingLocation = 'Europe';
					if ( shippingSizeTemp == 'Large' ) { updateShippingFlag = true }
				}
				break;
			case "EuropeLarge" :
				if ( document.Basket.ship[0].checked ) {
					//alert( 'Europe is Selected.' );
					shippingLocation = 'Europe';
					if ( shippingSizeTemp == 'Small' ) { updateShippingFlag = true }
				}
				break;
			case "WorldSmall" :
				if ( document.Basket.ship[1].checked ) {
					//alert( 'World is Selected.' );
					shippingLocation = 'World';
					if ( shippingSizeTemp == 'Large' ) { updateShippingFlag = true }
				}
				break;
			case "WorldLarge" :
				if ( document.Basket.ship[1].checked ) {
					//alert( 'World is Selected.' );
					shippingLocation = 'World';
					if ( shippingSizeTemp == 'Small' ) { updateShippingFlag = true }
				}
				break;
			}
		}
	}
	
	// If needed, update the displayed Shipping area.
	// Doing this by writing all of the changed data in the Basket to a 
	// hidden form field, then reloading the basket (the only simple way
	// to update the display of the Shipping area). The Basket.php file
	// then reads the data out of the hidden form field and updates the 
	// Cart object.
	//alert( 'Change to Ship a : ' + shippingSizeTemp + ' package.' );
	if ( updateShippingFlag == true ) {
		//alert( 'Need to change displayed Shipping to: '  + shippingLocation + shippingSizeTemp );
		document.Basket.submit();
	}
	
	
	// Update Sub and Total fields. 
	var subTotalAmount = subTotal + '.00';		// Format to a number string.
	//alert( 'Sub Total: ' + subTotalAmount );
	document.forms[basketForm].elements['subTotalOut'].value = subTotalAmount;
	
	// Update the Checkout button (enabled/disabled).
	if ( subTotal <= 0 ) {
		document.forms['Checkout'].elements['checkout'].disabled = 'disabled';
	} else {
		document.forms['Checkout'].elements['checkout'].disabled = '';
	}
	
}


//-------------------------------------------------------------
// User deleted a Line Item.
//-------------------------------------------------------------
function deleteBtn( imgName )
{	
	var basketForm = 'Basket';
	
	var myAction = 'basket.php?action=delete&img=' + imgName;
	
	document.forms[basketForm].action = myAction;
	document.forms[basketForm].submit();
	
}
