// JavaScript Document


function SetCookie(name, value) 
{
	/*
	if (testSessionCookie ())
	{
	*/
		var never = new Date();
		never.setTime(never.getTime() + 2010*24*60*60*1000);
		//var expString = "; expires=" + never.toGMTString();
		var expString = "; path=/";
		document.cookie = name + "=" + escape(value) + expString;
	/*
	}
	else
	{
		alert("Questo programma necessita dei Cookie per funzionare. Abilitare i Cookie!!");
	}
	*/
}





function GetCookie(name) 
{
	var result = null;
	var myCookie = " " + document.cookie + ";";
	var searchName = " " + name + "=";
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;
	if (startOfCookie != -1) 
	{
		startOfCookie += searchName.length; // skip past name of cookie
		endOfCookie = myCookie.indexOf(";", startOfCookie);
		result = unescape(myCookie.substring(startOfCookie, endOfCookie));
	}
	
	return result;
	
}

function array_to_single_data(array_in)
{
	var scratch = "";
	for(var i = 0; i < array_in.length; i++)
	{
		scratch = scratch+array_in[i]+ '§';
	}
	return scratch;
}

function single_data_to_array(single_data)
{
	var scratch = new Array();
    var pointer = 0;
	var lunghezza_stringa = single_data.length;
	
	fine_elemento = 0;
	
	//document.writeln(fine_elemento);
	//document.writeln(lunghezza_stringa);
	i=0;
	while (single_data.length > 0)
	{
		//document.writeln(i);
		fine_elemento = single_data.indexOf('§');
		//document.writeln(fine_elemento);
		//document.writeln(single_data.substr(pointer,fine_elemento));
		scratch[i] = single_data.substr(0,fine_elemento);
		//document.writeln(scratch[i]);
		pointer = fine_elemento+1;
		single_data = single_data.substr(pointer);
		//document.writeln(single_data.length);
		i++;
	}
	return scratch;
}



function BasketItem(Item,
NumberPrices, Prices, FromQuants, ToQuants,
Quantity, Textin, Attribut1, Attribut2, Attribut3,
productvatrate,weight,VariantDescription)
{
	this.Item = Item;
	this.NumberPrices = NumberPrices;
	this.Prices = Prices;
	this.FromQuants = FromQuants;
	this.ToQuants = ToQuants;

	this.Quantity=Quantity;
	this.Textin=Textin;
	this.Attribut1=Attribut1;
	this.Attribut2=Attribut2;
	this.Attribut3=Attribut3;
	this.productvatrate=productvatrate;
	this.weight=weight;
	this.VariantDescription=VariantDescription;
}

function get_basket_cookies()
{
	var scratch;
	var scratch_1;
	var scratch_array = new Array();
	
	theBasket = [new BasketItem("","","","","","","","","","","","","")];
	scratch = GetCookie("theBasket");
	if(scratch == null)
	{
		return false;
	}
	//document.writeln(scratch);
	
	scratch_1 = scratch;
	number_of_cookie_counter = 1;
	
	while (scratch_1.length == parseInt (max_dimensione_basket_cookie))
	{
		scratch_1 = GetCookie("theBasket"+number_of_cookie_counter);
		scratch = scratch + scratch_1;		
		number_of_cookie_counter = number_of_cookie_counter+1; 			
	}
	
	scratch_array = single_data_to_array(scratch);
	

	for (var i = 1; i < scratch_array.length; i++)
	{
		theBasket_new_item = [new BasketItem("","","","","","","","","","","","","")];
		theBasket=theBasket.concat([theBasket_new_item]);
		
		fine_elemento = scratch_array[i].indexOf('|');
		
		if (fine_elemento == 0)
		{
			theBasket[i].Item = "";	
		}
		else
		{
			theBasket[i].Item = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Item);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].NumberPrices = "";	
		}
		else
		{
			theBasket[i].NumberPrices = scratch_array[i].substr(0,fine_elemento);	
		}
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Prices = "";	
		}
		else
		{
			theBasket[i].Prices = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Prices);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].FromQuants = "";	
		}
		else
		{
			theBasket[i].FromQuants = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].FromQuants);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].ToQuants = "";	
		}
		else
		{
			theBasket[i].ToQuants = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].ToQuants);
		
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Quantity = "";	
		}
		else
		{
			theBasket[i].Quantity = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Quantity);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Textin = "";	
		}
		else
		{
			theBasket[i].Textin = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Textin);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Attribut1 = "";	
		}
		else
		{
			theBasket[i].Attribut1= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Attribut1);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Attribut2 = "";	
		}
		else
		{
			theBasket[i].Attribut2= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Attribut2);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Attribut3 = "";	
		}
		else
		{
			theBasket[i].Attribut3= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Attribut3);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].productvatrate = "";	
		}
		else
		{
			theBasket[i].productvatrate= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].productvatrate);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].weight = "";	
		}
		else
		{
			theBasket[i].weight= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].weight);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].VariantDescription = "";	
		}
		else
		{
			theBasket[i].VariantDescription= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].VariantDescription);
		
		
		//document.writeln(theBasket.length);
		/*
		if (scratch_array[i].length == fine_elemento+1)
		{
			theOrderFormular[i].formcontent = "";	
		}
		else
		{
			theOrderFormular[i].formcontent = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length);	
		}
		//document.writeln(theOrderFormular[i].formcontent);
		*/	
	}
	
}

function update_basket()
{
	var scratch;
	var scratch_array = new Array();

	for (i = 1 ; i < theBasket.length; i++)
	{
		scratch_array[i] = theBasket[i].Item + "|" 
				+ theBasket[i].NumberPrices + "|"
				+ theBasket[i].Prices + "|"
				+ theBasket[i].FromQuants + "|"
				+ theBasket[i].ToQuants + "|"
				+ theBasket[i].Quantity + "|"
				+ theBasket[i].Textin + "|"
				+ theBasket[i].Attribut1 + "|"
				+ theBasket[i].Attribut2 + "|"
				+ theBasket[i].Attribut3 + "|"
				+ theBasket[i].productvatrate + "|"
				+ theBasket[i].weight + "|"
				+ theBasket[i].VariantDescription + "|";
	}
		
	scratch = array_to_single_data(scratch_array);
	
	document.getElementById("new_item_hidden").value = scratch;
  	//alert(scratch);	
	document.update_basket_form.submit();		
}

function fill_basket(value)	
{	
	
	var scratch_array = new Array();
	theBasket = [new BasketItem("","","","","","","","","","","","","")];

	scratch_array = single_data_to_array(value);
	

	for (var i = 1; i < scratch_array.length; i++)
	{
		theBasket_new_item = [new BasketItem("","","","","","","","","","","","","")];
		theBasket=theBasket.concat([theBasket_new_item]);
		
		fine_elemento = scratch_array[i].indexOf('|');
		
		if (fine_elemento == 0)
		{
			theBasket[i].Item = "";	
		}
		else
		{
			theBasket[i].Item = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Item);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].NumberPrices = "";	
		}
		else
		{
			theBasket[i].NumberPrices = scratch_array[i].substr(0,fine_elemento);	
		}
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Prices = "";	
		}
		else
		{
			theBasket[i].Prices = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Prices);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].FromQuants = "";	
		}
		else
		{
			theBasket[i].FromQuants = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].FromQuants);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].ToQuants = "";	
		}
		else
		{
			theBasket[i].ToQuants = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].ToQuants);
		
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Quantity = "";	
		}
		else
		{
			theBasket[i].Quantity = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Quantity);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Textin = "";	
		}
		else
		{
			theBasket[i].Textin = scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Textin);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Attribut1 = "";	
		}
		else
		{
			theBasket[i].Attribut1= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Attribut1);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Attribut2 = "";	
		}
		else
		{
			theBasket[i].Attribut2= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Attribut2);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].Attribut3 = "";	
		}
		else
		{
			theBasket[i].Attribut3= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].Attribut3);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].productvatrate = "";	
		}
		else
		{
			theBasket[i].productvatrate= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].productvatrate);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].weight = "";	
		}
		else
		{
			theBasket[i].weight= scratch_array[i].substr(0,fine_elemento);	
		}
		//document.writeln(theBasket[i].weight);
		
		scratch_array[i] = scratch_array[i].substr(fine_elemento+1,scratch_array[i].length-1);
		fine_elemento = scratch_array[i].indexOf('|');
		if (fine_elemento == 0)
		{
			theBasket[i].VariantDescription = "";	
		}
		else
		{
			theBasket[i].VariantDescription= scratch_array[i].substr(0,fine_elemento);	
		}
	
	}
}



function buyItem(variant, newQuantity , textin, Attribut1, Attribut2, Attribut3, productvatrate,weight)
{

//document.writeln(variant.ProductShortName);

//alert ("Il prodotto selezionato non è disponibile. Non è possibile aggiungere il prodotto al carrello");
newItem = variant.ProductShortName;//unescape(newItem);

var index=-1;


if (! Attribut1) {Attribut1=""};
if (! Attribut2) {Attribut2=""};
if (! Attribut3) {Attribut3=""};
if (! textin)    {textin=""};

arrayPrices = new Array();
arrayFromQuants = new Array();
arrayToQuants = new Array();
NumberPrices = variant.numberPrices;
myVariantDescr = unescape(variant.VariantDescr);
giacenza = variant.giacenza;

if (giacenza < 1)
{
	alert (newItem+" non disponibile\nNon è possibile aggiungerlo al carrello");
	return;
}

if (giacenza < newQuantity)
{
	alert ("La quantità che vuoi aggiungere al carrello è maggiore della disponibilità. Puoi aggiungere al massimo "+ giacenza+ " pezzi del prodotto selezionato");
	return;
}

for(var ipr = 0 ; ipr<variant.numberPrices; ipr = ipr+1)
{
	arrayPrices[ipr] = variant.scPr[ipr+1].price;
	arrayFromQuants[ipr] = variant.scPr[ipr+1].fromQuant;
	arrayToQuants[ipr] = variant.scPr[ipr+1].toQuant;
}

Prices = arrayPrices.join(":");
FromQuants = arrayFromQuants.join(":");
ToQuants = arrayToQuants.join(":");



if (!( (parseFloat(newQuantity) == parseInt(newQuantity) )&&( parseInt(newQuantity) > 0) ) || isNaN(newQuantity))
alert(Loc_Integer);
else
{
	if(Attribut1.substr(Attribut1.length-1,1)=='?')
	alert(Loc_Please+ Attribut1.substr(0,Attribut1.length-1)+Loc_Select);
	else
	{
		if(Attribut2.substr(Attribut2.length-1,1)=='?')
		alert(Loc_Please+ Attribut2.substr(0,Attribut2.length-1)+Loc_Select);
		else
		{
			if(Attribut3.substr(Attribut3.length-1,1)=='?')
			alert(Loc_Please+ Attribut3.substr(0,Attribut3.length-1)+Loc_Select);
			else
			{
				if(confirm(newQuantity+' x '+newItem+' : Aggiungere al Carrello ?'))
				{
				
					
					for (var i=1; i<= theBasket.length-1; i++)
					{
						if ((theBasket[i].Item==newItem)&&(theBasket[i].Attribut1==Attribut1)&&(theBasket[i].Attribut2==Attribut2)&&(theBasket[i].Attribut3==Attribut3)&&(theBasket[i].Textin==textin))
						{
							if (window.confirm(Loc_Add))
							if((parseInt(theBasket[i].Quantity)+parseInt(newQuantity)) > 9999) 
							{
								alert("Die maximale Bestellmenge ist 9999!");
								theBasket[i].Quantity = 9999;
							}
							else
							theBasket[i].Quantity=parseInt(theBasket[i].Quantity)+parseInt(newQuantity);
						else

					if (parseInt(newQuantity) > 9999)
					{
						alert("Die maximale Bestellmenge ist 9999!");
						newQuantity = '9999';
					}
					else
					theBasket[i].Quantity=newQuantity;
					theBasket[i].Textin=textin;
					index=i;
					}
				}

if (index<0)                                         
{
	
	if (parseInt(newQuantity) > 9999)
	{
		alert("Die maximale Bestellmenge ist 9999!");
		newQuantity = '9999';
	}

Attribut1 = "DISP.";	
var neuItem=new BasketItem(newItem,NumberPrices,Prices,FromQuants,ToQuants,newQuantity,textin, Attribut1, Attribut2, Attribut3, productvatrate, weight, myVariantDescr);
theBasket=theBasket.concat([neuItem]);
//document.writeln(newItem,NumberPrices,Prices,FromQuants,ToQuants,newQuantity,textin, Attribut1, Attribut2, Attribut3, productvatrate, weight, myVariantDescr);
//alert(newQuantity+' x '+newItem+' : Aggiunto al Carrello')
}
}
//Hauptframe.location.reload();
//basketframe.location = "permbasket.htm";

}
}
}
}

update_basket();


}

