/* This file is part of a javascript shoppingcart, written by my,
A.J. van der Vegt (A.J.vanderVegt@ITS.TUDelft.nl)
I've released this program under the GPL license.
A copy of the GPL license can be found in the file /COPYING.
Please feel free to contact me about this program. */

//Constants for readability:
var Cdescription = parent.Cdescription;
var Cid = parent.Cid;
var Cprice = parent.Cprice;
var Camount = parent.Camount;

//Constants for currencies:
//I'd advice to supply all prices in the currency with value '0', and use all convertion rates from this currency.
//In this example, all prices are supplied and stored in euro's. Change 'Ccurrency' to view the prices in a different currency.
//(You'll probably want to update the exchange rate!)
var Ceuro = 0;
var Cdollar = 1;
var Cguilder = 2;
var convertcurrency = new Array(1, 1.00, 1);
var currencysign = new Array("&euro;", "$", "fl.");
var currencydescription = new Array("Euro", "US dollar", "Guilder");
var Ccurrency = Cdollar;
var decimalseperator = "."

//We first define some objects: a 'product' object, and a 'OrderObject' object.
function product (Description, Id, Price, Amount) {
  this.Description = String(Description);
  this.Id = Number(Id);
  this.Amount = Number(Amount);

//Prices are stored in one currency. functions starting with 'My..' return the price in the currencyin which they are stored.
//Other functions return prices in the currency set by 'Ccurrency'.
//Functions starting with 'Sum' return the price times the amount of products ordered.

  this.MyPrice = Number(Price);
  this.Price = function () {
     return(this.MyPrice * convertcurrency[Ccurrency]);
  }
  this.ScreenPrice = ScreenPrice(this.Price()); 

  this.MySumPrice = function () {
    return(this.MyPrice * this.Amount);
  }
  this.SumPrice = function () {
    return(this.MySumPrice() * convertcurrency[Ccurrency]);
  }
  this.ScreenSumPrice = function () {
    return(ScreenPrice(this.SumPrice()));
  }
}

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. I use my own function 'cause the array.sort doesn't work for some reason.

  this.Sort = 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==Cdescription)
      TmpArray.sort();
    else
      TmpArray.sort(MySort);

    if (this.ReverseSort)
      TmpArray.reverse();

//Now override our values with the sorted one's.
    for (var i = 0; i < this.NumberOfProducts; i++)
      this[i] = TmpArray[i];
    
    return;
  }

//Use this to add items to the order.
  this.Add = function (description, id, price, amount) {
    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);
      this.NumberOfProducts++;
    }
    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 function returns the grand total to be shown on the screen.
  this.ScreenTotalPrice = function () {
    return(ScreenPrice(this.TotalPrice() * convertcurrency[Ccurrency]));
  }
  
//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;
    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. You might want to pre-fill it from a cookie e.g.
var MyOrder = new OrderObject();

//---------------------------------------------------------- End of objects declaration ---------------------------------------------

//To add some default products:
//MyOrder.Add(description, id, price, amount); 
//e.g.:
//MyOrder.Add("Beer", 4563, .4, 10); 

//This function returns a price which can be shown on te screen.
function ScreenPrice(number) {
  var result = String(Math.round(number*100));
  result = result.substring(0, result.length-2) + decimalseperator + result.substring(result.length-2, result.length);
  return(currencysign[Ccurrency] + " " + result);
}


//Use this to add 1 item. If the shoppingcart is visible, it's updated.
function AddToOrder(description, id, price) {
  MyOrder.Add(description, id, price, 1);
  UpdateMainform();
  if (parent.SCOpened)
    parent.ShoppingCartScreen.rebuild();
  return;
}

//Use this to erase the order after confirmation.
function EraseOrder(askconfirm) {
  if (!askconfirm && confirm("Do you really want to clear your shoppingcart?")) {
    MyOrder = new OrderObject;
    if (parent.SCOpened)
      parent.ShoppingCartScreen.rebuild();
    UpdateMainform();
  }
  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.SortKey = sortkey;
    MyOrder.ReverseSort = false;
  }
  return;
}

//This function is used to sort the shoppingcart.
function MySort(item1, item2) {
  switch (MyOrder.SortKey) {
  case parent.Cprice:
    return item1.MyPrice - item2.MyPrice;
  case parent.Camount:
    return item1.Amount - item2.Amount;
  case parent.Ctotalprice:
    return (item1.Amount * item1.MyPrice) - (item2.Amount * item2.MyPrice);
  default:
    return item1.Id - item2.Id;
  }
  return 0;
}

//This function prints a line on the main screen indicating something has been ordered (and how much it costs).
function UpdateMainform() {
  var number = MyOrder.NumberOfItems();
  var myShoppingCart=" <A href='javascript:parent.parent.ShowShoppingCart()'";
  myShoppingCart = myShoppingCart + " onMouseOver=\"window.status='Click to show your shoppingcart';return true;\"";
  myShoppingCart = myShoppingCart + " onMouseOut=\"window.status=''\"";
  myShoppingCart = myShoppingCart + ">here</A> ";
  parent.sccontent.document.write("<html>");
  parent.sccontent.document.write("  <head>");
  parent.sccontent.document.write("    <link rel=\"stylesheet\" href=\"css/shop.css\">");
  parent.sccontent.document.write("  </head><body>");
  if (number == 0) {
    parent.sccontent.document.write("Click" + myShoppingCart + "to see your shoppingcart");
  } else if (number == 1) {
    parent.sccontent.document.write("Click" + myShoppingCart + "to see the product in your shoppingcart");
  } else 
    parent.sccontent.document.write("You've ordered " + number + " products so far, which cost "+ MyOrder.ScreenTotalPrice() + ". Click" + myShoppingCart + "to see them.");
  parent.sccontent.document.write("</body></html>");
  parent.sccontent.document.close();
  return;
}

//  (c) 2001, A.J. van der Vegt (A.J.vanderVegt@ITS.TUDelft.nl)
