// JavaScript Document
var Product_Item = function(arg)
{
	this.serial = arg.serial;
	this.group  = arg.group;
	this.cost   = arg.cost;
} 

Product_Item.prototype.qty = 0;

var Product_List = function()
{
  this.Items = new Array();
	this.onRefresh = function () {};
	
	this.AddItem = function(arg)	{	  this.Items.push(new Product_Item(arg));	}
	
	this.SaveQty = function()
	{
	  for (var i=0;i<this.Items.length;i++)
	  {
		 	var obj = $("qty_"+this.Items[i].serial);
		  if (obj) this.Items[i].qty = parseInt(obj.value);
	  }
	}
	
	this.onCalcOne = function(item)	{ var field = $("value_"+obj.serial);	   if (field) field.innerHTML = format(obj.qty*obj.cost,2,',')+"&nbsp;&euro;";}
		
	this.CalcOne = function(serial)
	{
		this.SaveQty();
		 
		for(var i=0;i<this.Items.length;i++) 
		{
			obj = this.Items[i];
		  if (obj.serial == serial)
		  {
	      this.AddToDB(obj);
				this.onCalcOne(obj);
	  	}
    }
		this.onRefresh();
	}
	
	this.CalcAll = function()
	{
		this.SaveQty();
		 
		for(var i=0;i<this.Items.length;i++) 	{	obj = this.Items[i];	this.onCalcOne(obj); }
		this.onRefresh();
	}
	
	this.AddToDB = function (obj)
	{
    xhr = GetHttpRequest();
		if (xhr)
		{
	  	xhr.open("GET","http://"+globals.url+"/ajax/modbasket.php?sessid="+globals.sessid+"&qty="+obj.qty+"&serial="+obj.serial,false);
	  	xhr.send(null);
		}
	}
}

function Add(field,list,serial)
{
	if (isNaN(field.value)) field.value = 0;
	field.value = parseInt(field.value)+1;
	
	list.CalcOne(serial);
}

function Substract(field,list,serial)
{
	if (isNaN(field.value)) field.value = 0;
  field.value = parseInt(field.value)-1;
	if (field.value < 0) field.value = 0;
	
	list.CalcOne(serial);
}




function DisplayBasket()
{
	  result = null;
	  xhr = GetHttpRequest();
		if (xhr)
		{
	    xhr.open("GET","http://"+globals.url+"/ajax/getbasket.php?sessid="+globals.sessid+"",false);
		  xhr.send(null);
		  if (xhr.readyState == 4) 
			{
			  var field = $("basket"); 
			  field.innerHTML=xhr.responseText;
			}
		}
	return result;	
}

function SaveMode(mode)
{
	result = null;
	xhr = GetHttpRequest();
	if (xhr)
	{
    xhr.open("GET","http://"+globals.url+"/ajax/savemode.php?sessid="+globals.sessid+"&mode="+mode,false);
	  xhr.send(null);
  }
	 return result;	
}

function ShowHide(id)
{
  if ($(id).style.display == "none")
	{
    new Effect.BlindDown($(id));
		SaveMode('');
	}	
	else	
	{
    new Effect.BlindUp($(id));
		SaveMode('none');
	}	
	return true;	
}

var Products=new Product_List();
Products.onRefresh = DisplayBasket;

