

/* Define Form Constants */

var	gst = 0.1;
// var shipping = 100.00;
var total = 0.0;

/* Form Pointers */

var ptr_cartTable;
var ptr_form;

/* Declare/initialise variables */


var products = new Array();

// Name --> Cost

// Light Vehicle Mechanical
products["Lt Vehicle 2pk"] = 15.00;
products["Lt Vehicle 4pk"] = 27.00;

// Light Vehicle LED Caps
products["Lt Vehicle LED caps - 2pk"] = 30.00;
products["Lt Vehicle LED caps - 4pk"] = 60.00;

// Heavy Vehicle LED Caps
products["Hv Vehicle LED caps (with anti-theft) - 2pk"] = 33.00;
products["Hv Vehicle LED caps (with anti-theft) - 4pk"] = 65.00;
products["Hv Vehicle LED caps (without anti-theft) - 4pk"] = 65.0;

// Tyredog
products["Tyredog 2 Wheels"] = 299.0;
products["Tyredog 4 Wheels"] = 399.0;
products["Tyredog 6-10 Wheels"] = 795.0;

// Other
products["Locknut Spanner"] = 3.0;



var cart = new Array();

// Name (product + PSI) --> qty,subtotal,rowptr
// Constants (offsets into cart-->[] arrays)

var QTY = 0;
var SUBTOTAL = 1;
var ROWPTR = 2;
var TYPE = 3

// This function is called when the page has been loaded.

function init() {
	ptr_cartTable = document.getElementById("shopping-cart-table");
	ptr_form = document.getElementById("shopping-form");
	updatetotals();
}


// Wrapper functions added Nov 2009 to handle changed format for heavy vehicle

function addHeavy(){
	var selectBox = document.getElementById("truck_pack");
	add(selectBox[selectBox.selectedIndex].value);
}

function subHeavy(){
	var selectBox = document.getElementById("truck_pack");
	sub(selectBox[selectBox.selectedIndex].value);	
}

function addHeavyAT(){
	var selectBox = document.getElementById("truck_packAT");
	add(selectBox[selectBox.selectedIndex].value);
}

function subHeavyAT(){
	var selectBox = document.getElementById("truck_packAT");
	sub(selectBox[selectBox.selectedIndex].value);	
}

function addTyreDog(){
	var selectBox = document.getElementById("tyredog_models");
	add(selectBox[selectBox.selectedIndex].value);
}

function subTyreDog(){
	var selectBox = document.getElementById("tyredog_models");
	sub(selectBox[selectBox.selectedIndex].value);	
}


// An associative array is not a *real* array, so implement a 
// custom function to get the length.

function cartlen(){
	var count = 0;

	for(i in cart){ count++; }
	return(count);
}

function sub(pName) {

	var productName = pName; //nb: we use both of these!

	// A bit of a hack, but it's a small application

	if(pName == "Lt Vehicle 2pk") {
		productName = "Lt Vehicle 2pk " + ptr_form.car2_psi[ptr_form.car2_psi.selectedIndex].value + "PSI";
	}

	if(pName == "Lt Vehicle 4pk") {
		productName = "Lt Vehicle 4pk " + ptr_form.car4_psi[ptr_form.car4_psi.selectedIndex].value + "PSI";
	}
	if(pName == "Hv Vehicle 1pk") {
		productName = "Hv Vehicle 1pk " + ptr_form.truck_psi[ptr_form.truck_psi.selectedIndex].value + "PSI";
	}

	// check for accidental click (trying to remove products that don't exist)

	if(cart[productName] == undefined || cart[productName][QTY] == 0){
			return;
	}

	cart[productName][QTY]--;

	if(cart[productName][QTY] != 0){

		// update the row details

		var subtotal = products[pName] * cart[productName][QTY];
		cart[productName][SUBTOTAL] = subtotal;
		updaterow(cart[productName][ROWPTR],cart[productName][QTY],subtotal);
	} else {
		// remove the row entirely
		deleterow(cart[productName][ROWPTR]);
		cart[productName] = undefined;
		delete cart[productName];
	}
	// Calculate and display the total
	total -= products[pName];
	updatetotals();
}


function add(pName) {
	
	
	var productName = pName; //nb: we use both of these!

	// A bit of a hack, but it's a small application
	
	// If it's not one of these products, just leave the productName as the pName
	
	if(pName == "Lt Vehicle 2pk") {
		
		// Assemble name for cart display with product + PSI
		
		productName = "Lt Vehicle 2pk " + ptr_form.car2_psi[ptr_form.car2_psi.selectedIndex].value + "PSI";
	}

	if(pName == "Lt Vehicle 4pk") {
		productName = "Lt Vehicle 4pk " + ptr_form.car4_psi[ptr_form.car4_psi.selectedIndex].value + "PSI";
	}
	if(pName == "Hv Vehicle 1pk") {
		productName = "Hv Vehicle 1pk " + ptr_form.truck_psi[ptr_form.truck_psi.selectedIndex].value + "PSI";
	}


	// Check if the product already exists in the cart
	if(cart[productName] == undefined) {
		cart[productName] = [1,null,products[pName],pName]; // Add product to cart
		cart[productName][ROWPTR] = addrow(productName,1,products[pName]);	
		cart[productName][SUBTOTAL] = products[pName];
	} 
	else {
		cart[productName][QTY]++; // increment counter
		var subtotal = products[pName] * cart[productName][QTY];
		cart[productName][SUBTOTAL] = subtotal;
		updaterow(cart[productName][ROWPTR],cart[productName][QTY],subtotal);
	}

	// Update total

	total += products[pName];
	updatetotals();

	return;
}


function addrow(name,qty,cost){

	var tbody = ptr_cartTable.getElementsByTagName("tbody")[0];

	var row = document.createElement("TR");
	var cell1 = document.createElement("TD");
	var cell2 = document.createElement("TD");
	var cell3 = document.createElement("TD");

	cell1.innerHTML = qty;
	cell2.innerHTML = name;
	cell3.innerHTML = cost;

	row.appendChild(cell1);
	row.appendChild(cell2);
	row.appendChild(cell3);

	tbody.appendChild(row);

	return (row); // return reference to the row to save
}

function updaterow(rowptr,qty,subtotal){

	rowptr.cells[0].innerHTML = qty;
	rowptr.cells[2].innerHTML = formatcurr(subtotal);
	return;
}

function deleterow(rowptr){

	var tbody = ptr_cartTable.getElementsByTagName("tbody")[0];
	tbody.removeChild(rowptr)

}

function updatetotals(){

	// Recalculate

	var aggregate_total = total;

	//aggregate_total += shipping;
	//aggregate_total += aggregate_total * gst;

	// Update display

	var totalcell = document.getElementById("cart_total");
	totalcell.innerHTML = formatcurr(aggregate_total);

	//var gstcell = document.getElementById("cart_gst");
	//gstcell.innerHTML = formatcurr(aggregate_total);

	return;
}

function resetcart(){

	for(var i in cart){
		deleterow(cart[i][ROWPTR]);
		cart[i] = undefined;
	}
	total = 0;
	updatetotals();
	return;
}

function formatcurr(thenum) {

	thenum = thenum.toFixed(2)
	
	thenum = thenum.toString();
	var fpoint = thenum.indexOf('.');
	var whole,frac;

	// Check for decimal point (fractional part)

	frac = -1;

	if(fpoint != -1){
		whole = thenum.substr(0, fpoint);
		frac = thenum.substr(fpoint+1);
	} else {
		whole = thenum;
	}


	// Format whole number with comma

	var thousands = "";

	if(whole.length > 3){

		numthous = parseInt(whole.length / 3);
		remainder = whole.length % 3;
		
		// If you don't check for zero, you get "," appended to the front sometimes!

		if(remainder != 0){
			thousands = whole.substr(0,remainder) + ", ";
		}

		for(var i = remainder; i < whole.length; i+=3) {
			thousands = thousands + whole.substr(i,3) + ", ";
		}


		// slice off last ","
		thousands = thousands.substr(0,thousands.length - 2);
	} else {
		thousands = whole;
	}
	
	frac = frac == -1 ? "" : frac.substring(0,2);		//we'll truncate it.

	thenum = frac != "" ? thousands + "." + frac : thousands;

	return(thenum);
}

function submitform() {
 
	var hiddenfield = document.getElementById("order");
	var temp = "";


	if(cartlen() < 1){
		alert("No products selected");
		return false;
	}

	for(var i in cart) {
		temp += cart[i][TYPE] + "," + i + "," + cart[i][QTY] + "," + cart[i][SUBTOTAL] + "|";
	}

	hiddenfield.value = temp;
	document.shopping_form.submit();
}

function popupwin(pageloc){
 
    myRef = window.open(pageloc,'mywin','left=20,top=20,width=500,height=700,toolbar=0,resizable=1,scrollbars=1');
	 myRef.focus();
}

