/* ALL functions declared here
function addOne(itm);
function removeOne(itm);
function zeroSubTotal();
function njTax(yesno);
function addEdition(ed);
function checkout();
function continueShopping();
function _setPrice (nbr);
function _computeTotal();
*/

function addtoCart() {
	//$('Deluxe').update('get IT Deluxe');
	//$('Standard').update('get IT Standard');
	//$('Math').update('get IT Math');
	//$('Verbal').update('get IT Verbal');
}

// Once you have added 3 products, you will not be able to add more products.
// Once you have removed the product, you will not be able to re-add products.
function addOne (itm) {

	var description = $('description_id').innerHTML; // Ex: description_id = get IT Deluxe Edition (1)
	var comment = $('comment_id').innerHTML; // Ex: comment_id = d(1,635,6.00),tax(44.45),total(685.45)
	var items = $('items_id').innerHTML; // Ex: items_id = d|1|
	var subtotal = $('subtotal_id').innerHTML; // Ex: subtotal_id = 635.00
	var shipAmt = $('ship_amt').innerHTML; // Ex: ship_amt = 6.00
	
	var edition = $('edition_' + itm).innerHTML; // Ex: edition_d = get IT Deluxe Edition
	var quantity = $('quantity_' + itm).innerHTML; // Ex: quantity_d = 1
	var cost = $('cost_' + itm).innerHTML; // Ex: cost_d = 635
	var shipCost = $('ship_' + itm).innerHTML; // Ex: ship_d = 6.00

	// Create regular expression prior to incrementing quantity.
	var descriptionRegEx = new RegExp (edition + ' \\(' + quantity + '\\)');
	var commentRegex = new RegExp (itm + '\\(' + quantity);
	var itemsRegex = new RegExp (itm + '\\|' + quantity);
	
	// subtotal could look like this 10,100.00, so remove the comma.
	subtotal = parseFloat (subtotal.sub (/,/g, ''));
	
	// Adding a product, so increase the quantity.
	++quantity;
 
	// Update description, Ex: description_id = get IT Deluxe Edition (2)
	description = description.sub (descriptionRegEx, edition + ' (' + quantity + ')');
	$('description_id').update(description);
	
	// Update comment, Ex: comment_id = d(2,635,6.00),tax(44.45),total(685.45)
	comment = comment.sub (commentRegex, itm + '(' + quantity);
	$('comment_id').update (comment);

	// Update items, Ex: items_id = d|2|
	items = items.sub (itemsRegex, itm + '|' + quantity);
	$('items_id').update(items);

	// Set Real Values
	var newPrice = _setPrice (quantity * cost);  // we are adding a product cost
	var newSubtotal = _setPrice (parseFloat(subtotal) + parseFloat(cost)); // we are adding a product cost
	var newShipAmt = _setPrice (parseFloat(shipAmt) + parseFloat(shipCost));

	$('quantity_' + itm).update (quantity);
	$('price_' + itm).update (newPrice);
	$('subtotal_id').update (newSubtotal);
	$('ship_amt').update (newShipAmt);

	if (quantity > 2) { // Cannot add more (limit 3)
		$('add_' + itm).hide();
	}

	// Is tax being charged?
	return _computeTotal();
}

function removeOne (itm) {

	var description = $('description_id').innerHTML; // Ex: description_id = get IT Deluxe Edition (2)
	var comment = $('comment_id').innerHTML; // Ex: comment_id = d(2,635,6.00),tax(88.90),total(1370.90)
	var items = $('items_id').innerHTML; // Ex: items_id = d|2|
	var subtotal = $('subtotal_id').innerHTML; // Ex: subtotal_id = 1270.00
	var shipAmt = $('ship_amt').innerHTML; // Ex: ship_amt = 12.00

	var edition = $('edition_' + itm).innerHTML; // Ex: edition_d = get IT Deluxe Edition
	var quantity = $('quantity_' + itm).innerHTML; // Ex: quantity_d = 1
	var cost = $('cost_' + itm).innerHTML; // Ex: cost_d = 635
	var shipCost = $('ship_' + itm).innerHTML; // Ex: ship_d = 6.00
	
	// Create regular expression prior to decrementing quantity.
	var descriptionRegEx = new RegExp (edition + ' \\(' + quantity + '\\)');
	var commentRegex = new RegExp (itm + '\\(' + quantity + ',' + cost + ',' + shipCost + '\\)');
	var itemsRegex = new RegExp (itm + '\\|' + quantity + '\\|');

	// subtotal could look like this 10,100.00, so remove the comma.
	subtotal = parseFloat (subtotal.sub (/,/g,''));
	
	// Subtracting a product, so dexrease the quantity
	--quantity;

	if (quantity < 1) {
		description = description.sub (descriptionRegEx, '');
		description = description.sub (/^ *, */, '');
		description = description.sub (/ *, *$/, '');
		comment = comment.sub (commentRegex, '');
		comment = comment.sub (/^,/, '');
		comment = comment.sub (/,,/, ',');
		items = items.sub (itemsRegex, '');
	} else {
		description = description.sub (descriptionRegEx, edition + ' (' + quantity + ')');
		comment = comment.sub (commentRegex, itm + '(' + quantity + ',' + cost + ',' + shipCost + ')');
		items = items.sub (itemsRegex, itm + '|' + quantity + '|');
	}

	$('description_id').update (description);
	$('comment_id').update (comment);
	$('items_id').update (items);

	if (quantity < 3) {
		$('add_'+itm).show();
	}
	
	// Set Real Values
	var newPrice = _setPrice (parseFloat(quantity) * parseFloat(cost));
	var newSubtotal = _setPrice (subtotal - parseFloat(cost));
	var newShipAmt = _setPrice (parseFloat(shipAmt) - parseFloat(shipCost));

	$('quantity_'+itm).update (quantity);
	$('price_'+itm).update (newPrice);
	$('subtotal_id').update (newSubtotal);
	$('ship_amt').update (newShipAmt);
	
	// return without rendering new page content.
	if (zeroSubTotal()) {
		return true;
	}

	if (quantity < 1) {
		$('line_'+itm).hide();
	}

	// Is tax being charged?
	return _computeTotal();
}

function zeroSubTotal() {
	var subtotal=$('subtotal_id').innerHTML;
	var newSubtotal=parseFloat(subtotal.sub(/,/g,''));
	if (newSubtotal==0) {
		return true;
	}
	return false;
}

function njTax(yesno) {
	if (yesno == 1) { // "Ship to NJ" selected
		if ($('njtax').hasClassName('livelink')) { // "Do not shipproduct to NJ" currently selected
			// Charge tax.
			$('njtax').removeClassName('livelink');
			$('njtax').addClassName('deadlink');
			$('notax').removeClassName('deadlink');
			$('notax').addClassName('livelink');
			$('njtax_img').show();
			$('notax_img').hide();
			return _computeTotal();
		}
	} else { // "Do not shipproduct to NJ" selected
		if ($('notax').hasClassName('livelink')) { // "Ship to NJ" currently selected
			// Do not charge tax.
			$('notax').removeClassName('livelink');
			$('notax').addClassName('deadlink');
			$('njtax').removeClassName('deadlink');
			$('njtax').addClassName('livelink');
			$('notax_img').show();
			$('njtax_img').hide();
			return _computeTotal();
		}
	}
	return false;
}

function addEdition(ed) {
	if (ed=='Deluxe') {
		document.edition_form.edition_type.value='d';
		document.edition_form.submit();
	} else if (ed=='Standard') {
		document.edition_form.edition_type.value='s';
		document.edition_form.submit();
	} else if (ed=='Math') {
		document.edition_form.edition_type.value='m';
		document.edition_form.submit();
	} else if (ed=='Verbal') {
		document.edition_form.edition_type.value='v';
		document.edition_form.submit();
	}
	return false;
}

function checkout() {
	var total = $('total_id').innerHTML;
	var tax = $('tax_amt').innerHTML;
	var ship = $('ship_amt').innerHTML;
	var desc = $('description_id').innerHTML;
	var comment = $('comment_id').innerHTML;
	/*
	var ans = confirm (comment + ' ' + desc);
	if (!ans) return 0;
	*/

	document.checkoutform.AMOUNT.value=total;
	if (ship == '0.00') {
		document.checkoutform.SHIPAMOUNT.value = "";
	} else {
		document.checkoutform.SHIPAMOUNT.value = ship;
	}
	if (tax == '0.00') {
		document.checkoutform.TAX.value="";
	} else {
		document.checkoutform.TAX.value=tax;
	}
	document.checkoutform.DESCRIPTION.value=desc;
	document.checkoutform.COMMENT1.value=comment;

	document.checkoutform.submit();
}

function continueShopping() {
	var cartItems=$('items_id').innerHTML;
	document.continueform.CART_ITEMS.value=cartItems;
	document.continueform.submit();
}

// Helper function
function _setPrice (nbr) {
	nbr = new String ((Math.round (nbr*100))/100);
	var zeros;
	var pnt = nbr.lastIndexOf (".");
	if (pnt == -1) {
		nbr = nbr + ".";
		zeros = 2;
	} else {
		zeros = 2-nbr.length+pnt+1;
	}
	for (; zeros > 0; --zeros) nbr = nbr + "0";
	return nbr;
}

// Helper function
function _computeTotal() {

	var tax = $('tax_amt').innerHTML;
	var total = $('total_id').innerHTML;
	var chargeTax = $('njtax').hasClassName ('deadlink'); // "Ship to NJ" currently selected
	var subtotal = $('subtotal_id').innerHTML;
	var shipTotal = $('ship_amt').innerHTML;

	var commentRegex = new RegExp ('tax\\(' + tax + '\\),total\\(' + total);

	subtotal = parseFloat (subtotal.sub (/,/g, ''));
	shipTotal = parseFloat (shipTotal.sub (/,/g, ''));

	var newTotal;
	var newTax;
	if (chargeTax) {
		newTax = _setPrice (subtotal * 0.07);
		newTotal = _setPrice (subtotal * 1.07 + shipTotal);
	} else {
		newTax = _setPrice (subtotal * 0);
		newTotal = _setPrice (subtotal + shipTotal);
	}

	$('tax_amt').update (newTax);
	$('total_id').update (newTotal);

	var comment = $('comment_id').innerHTML;
	comment = comment.sub (commentRegex, 'tax('+newTax+'),total('+newTotal);
	$('comment_id').update (comment);
	
	if (total < 4) { // to be safe, nothing should be less than 4
		return true;
	}
	return false;
}

