//-------------------------------------------------------------------------------------------------
// cartHandler.js
//
// Handles a shopping cart: adds items, deletes items, displays cart, sends email to purchaser
// and sales contact upon order submission.
//
// Scripts should be included in the HTML file just after the "body" tag in the following
// manner and order:
// 
//     	<script src="cookieHandler.js" language="JavaScript"></script>
//     	<script src="cartHandler.js" language="JavaScript"></script>
//     	<script src="imageInfo.js" language="JavaScript"></script>    	
//
//
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// afterLoad
//
// Call this function via the body tag in the HTML code:
// 	<body onload="afterLoad()">
//

function afterLoad()
{

//create cookies to pass values on to any script which might need them
setCookies();

}//end of afterLoad
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// setCookies
//
// Sets up cookies needed by various scripts.
//

function setCookies()
{

//create cookies to pass values on to any script which might need them

createCookie('StandardShippingCost', baseShippingCost, 0);
createCookie('SalesTaxRate', salesTaxRate, 0);
createCookie('DispShipCostColOnCart', dispShipCostColumnOnCart, 0);

}//end of setCookies
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// checkOutWithCheck
//
// This function is called when the user is ready to finalize the purchase by check or money order.
//

function checkOutWithCheck()
{

//load the checkout page
window.location="CheckOut.html";

}// end of checkOutWithCheck
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// checkOutWithCreditCard
//
// This function is called when the user is ready to finalize the purchase by credit card.
//

function checkOutWithCreditCard()
{

//load the checkout page
window.location="CheckOutPayPal.html";

}// end of checkOutWithCreditCard
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// handleNMResident
//
// This function sets cookie 'NMResident' to 'Yes' if the "Billing or Shipping to New Mexico 
// Address?" checkbox is checked or to 'No' if unchecked.
//
// Call this function from the onClick of the checkbox and the "Update Cart" button to make sure
// the cookie is set properly before calling the update script.
//

function handleNMResident()
{

if (document.getElementById('NMResident').checked)
	createCookie('NMResident', 'Yes', 0);
else
	createCookie('NMResident', 'No', 0);

}// end of handleNMResident
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// addItem
//
// Call this function before invoking the addItemToCartTKP.cgi script.  If a cart number has
// not already been assigned, that will be done first.
//

function addItem()
{

//assign a cart number and set a flag to force creation of a new cart if necessary
createCart();

}// end of addItem
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// createCart
//
// If the user does not already have a cookie named "Cart" with a value, the user is given such a
// cookie with a unique cart number.
//
// If a cart needs to be created, the cookie "CreateCart" will be set to "Yes" so the script
// will know to create a new cart.  If a cart has already been assigned, the cookie will be set
// to "No".
//

function createCart()
{

var cookieValue = readCookie('Cart');

if ((cookieValue == "") || (cookieValue == null)){

	var date = new Date();
	var cartNumber = date.getTime();
	createCookie('Cart', cartNumber, 0);
	createCookie('CreateCart','Yes', 0);
		
	}
else{
	//clear the create cart flag as one has already been assigned
	createCookie('CreateCart','No', 0);
		
	}

}// end of createCart
//-------------------------------------------------------------------------------------------------

