//This function rebuilds the main frame with the products. The products are always ordered first.
//function rebuild() {
//	var amount = MyOrder.NumberOfProducts;
//	var rowclass = "";
//	var dispstring = "";
//	if (amount == 0) {
//		dispstring += emptycartstring;
//	} else {
//		MyOrder.SmartSort();
//		dispstring += ShowTableHeader();
//		for (var i = 0; i < amount; i++) {
//			if (rowclass == "")
//				rowclass = " class=odd";
//			else
//				rowclass = "";
//			dispstring += ShowItem(MyOrder[i], rowclass);
//		}
//		dispstring += ShowTableFooter();
//	}
//	shoppinglist.write(dispstring);
//	return;
//}

function IsValidNumber(Number) {
	var numbers= "0123456789";
	var result = true;
	for (var i=0; i < Number.length; i++) {
		if (numbers.indexOf(Number.substring(i, i+1)) == -1)
			result = false;
	}
	return(result);
}

//This function changes the number of items ordered, and performs some checks.
function ChangeAmount(Id,Number) {
	if (!IsValidNumber(Number)) {
		alert("Please do not type any illegal characters at 'Number'.");
		rebuild();
		return;
	}
	//This removes trailing zero's.
	Number = parseInt(Number);
	//The amount entered was a number, let's find the corresponding product in MyOrder.
	var productindex = -1;
	for (var i=0; i < MyOrder.NumberOfProducts; i++) {
		if (MyOrder[i].Id == Id)
			productindex = i;
	}
	//The amount is changed if the number is found.
	if (productindex > -1) {
		//If the user entered '0', we ask for confirmation.
		if ((Number==0) && (confirm("Are you sure you want to remove all the  " + MyOrder[productindex].Description + " from your shopping cart?")))
			MyOrder.Remove(productindex);
		else if (Number!=0)
			MyOrder[productindex].Amount = Number;
		MyOrder.ResetCookie();
		rebuild();
	}
	return;
}

function Send() {
	if (confirm("Are you sure you want to send this to the shop?")) {
		var totalamount = MyOrder.NumberOfProducts;
		var order = "amount=" + totalamount;
		order += ",total=" + Math.round(MyOrder.TotalPrice() * 100);
		for (var i=0; i < totalamount; i++) {
			order += ",id" + MyOrder[i].Id + "=" + MyOrder[i].Amount;
		}
		alert("(From shopping cart.js, bottom lines)\n This would be send to the server: "+ order);
		main.window.location="confirm.php?data="+order;
	}
	return;
}

//This function returns a price which can be shown on te screen.
function ScreenPrice(number) {
	var result = String(Math.round(number*100));
	var nresult = "";
	if (result < 100)
		nresult = "0";
	nresult += result.substring(0, result.length-2) + decimalseperator + ((result.substring(result.length-2, result.length)) == 0 ? '00' : result.substring(result.length-2, result.length));
	return(nresult + " ");
}


//Use this to add 1 item. If the shoppingcart is visible, it's updated.
function AddToOrder(description, id, price,code) {
	var amount = 1;
	MyOrder.Add(description, id, price, amount,code);
	rebuild();
	return;
}

//Use this to add 1 item. If the shoppingcart is visible, it's updated.
function AddHashToOrder(hash) {
	var amount = 1;
	props = hash.split(";");
	MyOrder.Add(props[0], props[1], props[2], amount,props[3]);
	rebuild();
	return;
}

//Use this to erase the order after confirmation.
function EraseOrder(askconfirm) {
	if (!askconfirm && confirm("Do you really want to clear your shopping cart?")) {
		MyOrder = new OrderObject;
		//hier moet het cookie geleegd worden
		rebuild();
	}
	return;
}

//This function changes the key by which the order is sorted.
function Sortby(sortkey) {
	if (sortkey == MyOrder.SortKey) {
		MyOrder.ReverseSort = !MyOrder.ReverseSort;
	} else {
		MyOrder.ReverseSort = false;
	}
	MyOrder.SortKey = sortkey;
	rebuild();
	return;
}

//This function is used to sort the shoppingcart.
function MySort(item1, item2) {
	switch (MyOrder.SortKey) {
		case Cprice:
			return item1.MyPrice - item2.MyPrice;
		case Camount:
			return item1.Amount - item2.Amount;
		case Ctotalprice:
			return (item1.Amount * item1.MyPrice) - (item2.Amount * item2.MyPrice);
		default:
			return item1.Id - item2.Id;
	}
	return 0;
}

function isValidNumber(Number) { 
	var numbers= "0123456789";
	var result = true;
	for (var i=0; i < Number.length; i++) {
		if (numbers.indexOf(Number.substring(i, i+1)) == -1)
			result = false;
	}
	return(result);
}
