var MINORDER = 45;

function BuyProduct(productdata)
{
  imgre = /_ID_/g
  newentry = trolley_entry.replace(imgre, productdata[0]);
  imgre = /_IMAGE_/
  newentry = newentry.replace(imgre, productdata[1]);
  imgre = /_NAME_/g
  if (productdata[2].length > 15) productdata[2] = productdata[2].substring(0, 12) + "...";
  newentry = newentry.replace(imgre, productdata[2]);
  imgre = /_TIP_/g
  newentry = newentry.replace(imgre, productdata[2]);
  $("trolley").innerHTML += newentry;
}

function RemoveProduct(productid)
{
  new Rico.Effect.FadeTo($("trolley_entry_" + productid), .1, 500, 10 );
}

function ShowTrolleyDetail(productdata)
{
}

function checknumeric(key)
{
  if ((key > 47 && key < 58) || (key > 96 && key < 105) || key == 8  || key == 28) return(true);
  return(false);
}

var CTrolley = Class.create();

CTrolley.prototype = 
{
   initialize: function() 
   {
      this.entries = new Array();
      this.trolley_divid = "trolley";
      this.entry_divid = "trolley_entry_";
      this.entry_css = "trolley_entry";
      this.index = 0;
   },
   
   FormatEntry : function(lines, TrolleyEntry)
   {
      imgre = /_AMOUNT_/g
      lines = lines.replace(imgre, TrolleyEntry.amount);
      imgre = /_PRODUCTID_/g
      lines = lines.replace(imgre, TrolleyEntry.productid);
      imgre = /_ID_/g
      lines = lines.replace(imgre, TrolleyEntry.id);
      imgre = /_IMAGE_/
      lines = lines.replace(imgre, TrolleyEntry.image);
      imgre = /_NAME_/g
      
      TrolleyEntry.name = TrolleyEntry.name.replace("[and]", "&");
      
      if (TrolleyEntry.name.length > 15) 
        productname = TrolleyEntry.name.substring(0, 12) + "...";
      else
        productname = TrolleyEntry.name;
      
      lines = lines.replace(imgre, productname);

      imgre = /_TIP_/
      lines = lines.replace(imgre, TrolleyEntry.amount + "x " + TrolleyEntry.name);
      
      return(lines);
   },
   
   AddEntry: function (TrolleyEntry, update)
   {
    if (ExistingEntry = this.GetEntryFromProductID(TrolleyEntry.productid))
    {
      if (isNaN(TrolleyEntry.amount) || TrolleyEntry.amount == '') TrolleyEntry.amount = 1;
      ExistingEntry.amount = parseInt(ExistingEntry.amount) + parseInt(TrolleyEntry.amount);
      TrolleyEntry = ExistingEntry;
    } else
      {
        if (isNaN(TrolleyEntry.amount) || TrolleyEntry.amount == '') TrolleyEntry.amount = 1;
        this.entries[TrolleyEntry.productid] = TrolleyEntry;
        TrolleyEntry.id = TrolleyEntry.productid;
        TrolleyEntry.element = document.createElement("div");
        TrolleyEntry.element.id = this.entry_divid + TrolleyEntry.index;
        TrolleyEntry.element.className = this.entry_css;
        $(this.trolley_divid).appendChild(TrolleyEntry.element);
      }
     
      newentry = this.FormatEntry(trolley_entry, TrolleyEntry);
      TrolleyEntry.element.innerHTML = newentry;
      this.AddEntryWindow();
      
      if (update != undefined) this.SaveTrolley();
      
      return(TrolleyEntry);
  },
  
  GetEntryFromProductID : function(productid)
  {
      for (i = 1; i < this.entries.length; i++)
        if (this.entries[i] != undefined)
          if (this.entries[i].productid == productid)
            return(this.entries[i]);
  },
  
  DeleteEntry : function(id, update)
  {
    $(this.trolley_divid).removeChild(this.entries[id].element);
    delete(this.entries[id]);
    if (update != undefined) this.SaveTrolley();
  },
  
//  i:0;a:3:{s:9:"productid";i:0;s:5:"image";s:5:"0.jpg";s:6:"amount";i:0;}
  
  Serialize : function(array)
  {
    str = "";
    for (i = 1; i < array.length; i++)
    {
      if (array[i] != undefined)
      {
        for (p in array[i])
        {
          if (p != "element" && p != "initialize" && p != "extend")
          {
            str += [p] + ":";
            if (typeof(array[i][p]) == "number")
              str += array[i][p] + ";";
            else
              str += array[i][p] + ";";
          }
        }
        str += "}";
      }
    }
    
    str = str.replace("&", "[and]");
    
    return(str);
  },
  
  AddEntryWindowCleanup : function()
  {
    addentrydiv = $("trolley_addentry");
    new Rico.Effect.FadeTo($("trolley_addentry"), 0, 1, 1);
    addentrydiv.style.top = "1px";
    addentrydiv.style.left = "1px";
    addentrydiv.style.width = "1px";
    addentrydiv.style.height = "1px";
    addentrydiv.innerHTML = "";
    activeentry = null;
  },
  
  AddEntryWindowInit : function()
  {
   $("amount_field").focus();
  },

  AddEntryWindow: function(TrolleyEntry, update)
  {
    y = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
    addentrydiv = $("trolley_addentry");
    activeentry = TrolleyEntry;
    //$("trolley_addentry").style.visibility = "visible";
    if (TrolleyEntry == undefined && update == undefined)
    {
      new Rico.Effect.FadeTo(addentrydiv, 0.1, 200, 10, { complete: this.AddEntryWindowCleanup });
    } else
      {
        addentrydiv.style.top = (y + 200) + "px";
        addentrydiv.innerHTML = this.FormatEntry(trolley_entry_body, TrolleyEntry);
        new Rico.Effect.FadeTo(addentrydiv, 1, 200, 10, { complete: this.AddEntryWindowInit });
        addentrydiv.style.left = ((screen.width - 300) / 2) + "px";
        addentrydiv.style.width = "300px";
        addentrydiv.style.height = "200px";
      }
  },
  
  SaveTrolley : function()
  {
    //document.writeln(this.Serialize(this.entries));
    var pars = "command=_SaveTrolley&trolley=" + this.Serialize(this.entries);
    var myAjax = new Ajax.Request(".", {method: 'get', parameters: pars, onComplete: this.showResponse});
  },
  
  showResponse : function(originalRequest)
  {
    document.title = originalRequest.responseText;
  }
}

var CTrolleyEntry = Class.create();

CTrolleyEntry.prototype = 
{
   initialize: function(productid, name, image, amount)
   {
      this.productid = productid;
      this.amount = amount == undefined ? 1 : amount;
      this.name = name;
      this.image = image;
   }
}

var Trolley = new CTrolley();
var activeentry = null;

function onBoot()
{
  //Trolley.AddEntry(new CTrolleyEntry(1, 'Driewieler', '1.jpg', 22));
}

function SavePayDesk()
{
  document.forms["paydesk"].command.value = "savepaydesk";
  document.forms["paydesk"].submit();
}

function FinishPayDesk()
{
  totalstr = document.getElementById('total').innerHTML.substring(2);
  totalstr = totalstr.replace(",", ".");
  
  if (parseFloat(totalstr) >= MINORDER)
  {
    document.forms["paydesk"].command.value = "finishorder";
    document.forms["paydesk"].submit();
  }
  else alert($('min_total_not_made').innerHTML);
}

function CheckForm(form)
{
  
}
