//the order object
function OrderObject () {
	this.NumberOfProducts = 0;	//The number of different products.
	this.SortKey = 0;			//Indicates what to sort by.
	this.ReverseSort = false;	//Indicates to reverse the sort or not.

	//This function sorts the order.
	this.SmartSort = function () {;
		var TmpArray = new Array();
		for (var i = 0; i < this.NumberOfProducts; i++)
			TmpArray[i] = this[i];
		//We use javascript's sort funciton to sort by description.
		if (this.SortKey.value==Cdescription)
			TmpArray.sort();
		else
			TmpArray.sort(MySort);
		if (this.ReverseSort)
			TmpArray.reverse();
		//Now override our values with the sorted one's.
		for (i in TmpArray)
			this[i] = TmpArray[i];
		return;
	}
	
	//reset the cookie
	this.ResetCookie = function() {
		var newcookie = new Array();
		for (var i = 0; i < this.NumberOfProducts; i++) {
			newcookie[i] = new Array();
			//newcookie[i][0] = this[i]['Description'];
			newcookie[i][0] = this[i]['Id'];
			//newcookie[i][2] = this[i]['MyPrice'];
			newcookie[i][1] = this[i]['Amount'];
		}
		writeCartCookie(newcookie);
	}

	//Use this to add items to the order.
	this.Add = function (description, id, price, amount,code) {
		var HasBeenOrdered = false;
		var totalamount = this.NumberOfProducts;
		for (var i = 0; i < totalamount; i++) {
			if (this[i].Id == id) {
				HasBeenOrdered = true;
				this[i].Amount += amount;
			}
		}
		if (!HasBeenOrdered) {
			this[totalamount] = new product(description, id, price, amount, code);
			this.NumberOfProducts++;
		}
		this.ResetCookie();
		return;
	}

	//This function returns the sum of the prices of all the products ordered in original currency.
	this.TotalPrice = function () {
		var total = 0;
		for (var i = 0; i < this.NumberOfProducts; i++)
			total += this[i].MySumPrice();
		return(total);
	}
	
	this.ScreenShippingCosts = function () {
		return ScreenPrice(shippingcosts);
	}
	
	this.ScreenGrandTotalPrice = function () {
		return ScreenPrice(this.TotalPrice() + shippingcosts);
	}

	//This function returns the grand total to be shown on the screen.
	this.ScreenTotalPrice = function () {
		return(ScreenPrice(this.TotalPrice() * currency[Ccurrency]["rate"]));
	}
  
	//Use this function to remove items from the order.
	this.Remove = function(number) {
		var NewOrder = new OrderObject();
		for (var i = 0; i < this.NumberOfProducts; i++) {
			if (number != i) {
				NewOrder[NewOrder.NumberOfProducts] = this[i];
				NewOrder.NumberOfProducts++;
			}
		}
		//The next thing is a little tricky, but it works for now.
		MyOrder = NewOrder;
		this.ResetCookie();
		return;
	}

	//This function returns the total number of items ordered. (it sums the amount of things ordered of each product)
	this.NumberOfItems = function () {
		var result = 0;
		for (var i = 0; i < this.NumberOfProducts; i++) {
			result += this[i].Amount;
		}
		return(result);
	}
}

OrderObject.prototype = Array();

//We here declare the order.
var MyOrder = new OrderObject();