// Basket operations

function Basket_add (ID, count, additional, record)
{
	this.ID=ID;
	this.Count=count;
	this.AdditionalInfo = additional;
	this.Record = record;
}

function Basket_contains (ID)
{
	for (BASKET_i=0;BASKET_i<parent.Basket.length;BASKET_i++)
	{	
		if (parent.Basket[BASKET_i].ID == ID) return true;
	}

	return false;
}

function Basket_getID (ID)
{
	for (l=0;l<parent.Basket.length;l++)
	{	
		if (parent.Basket[l].ID == ID) return l;
	}

	return null;
}

function Basket_getRECORD (offset)
{
	if (offset!=null && offset<RECORD_count)
	{
		//ID=parent.Basket[offset].ID;
		return parent.Basket[offset].Record;;
		//offset=RECORD_getID (ID);

		//return RECORD(offset);
	}
}

function Basket_computePrice (offset, vat)
{
	if (offset!=null && offset<parent.Basket.length)
	{
		var Count=parent.Basket[offset].Count;
		var T_record=Basket_getRECORD(offset);
		return (vat ? T_record.PriceWithVAT : T_record.Price)*Count;
	}
}

function Basket_computeTotal ()
{
	var total=0;
	
	var showVAT = CONFIG_getValue ("cbVATTemplateShow") == "True";

	if (parent.Basket == undefined)
		parent.Basket = Array ();

	for (BASKET_i=0;BASKET_i<parent.Basket.length;BASKET_i++)
	{
		total+=Basket_computePrice (BASKET_i, showVAT);
	}

	return total;
}

function Basket_list (offset, currency, showMeasurement, showVAT, showAdditional)
{
	if (offset<parent.Basket.length)
	{
		var record=parent.Basket[offset];
		var T_record = Basket_getRECORD(offset);

		document.write ("<TR>");	

		document.write ("<TD> <a alt='Remove item' href='javascript:Basket_remove (" + offset + ")'><img src='./images/remove.gif' border=0></img></a> </TD>");
		document.write ("<TD class=RECORD_CodeTD>" + T_record.Code + "</TD>");
		
		var text = T_record.Name;
		if (showAdditional && record.AdditionalInfo.length > 0)
			text += "(" + record.AdditionalInfo + ")"
		
		document.write ("<TD class=RECORD_OtherTD>" + text + "</TD>");
		document.write ("<TD class=RECORD_OtherTD style='text-align: center'>" + record.Count + " " + (showMeasurement ? T_record.Measurement : "") + "</TD>");
		
		var lprice = showVAT ? T_record.PriceWithVAT : T_record.Price;
		
		document.write ("<TD class=RECORD_OtherTD align=right>" + format_Money (lprice) + ",- " + currency + "</TD>");
		document.write ("<TD class=RECORD_OtherTD align=right>" + format_Money (record.Count * lprice) + ",- " + currency + "</TD>");

		document.write ("<\TR>");
	}
}

function Basket_remove (offset)
{
	if (offset<parent.Basket.length)
	{
		if (parent.Basket.length > 1) parent.Basket[offset]=parent.Basket[parent.Basket.length-1];
		parent.Basket.length--;
		window.location.reload ();
		parent.top_bar.location.reload ();
	}
}

function Basket_empty ()
{
	parent.Basket.length=0;
	window.location.reload ();
	parent.top_bar.location.reload ();
}

function Basket_showContents ()
{
	document.write ("<center> <h2>");
	
	if (parent.Basket.length >0) document.write (TRANSLATION_getValue ("basket_contents"));
	else document.write (TRANSLATION_getValue ("basket_is_empty"));
	
	document.write ("</center> </h2>");

	if (parent.Basket.length >0)
	{
		document.write ("<table width=100%>");

		document.write ("<tr>");
		
		showVAT = CONFIG_getValue ("cbVATTemplateShow") == "True";
		var showAdditional = CONFIG_getValue ("cbAdditionalOrderShow") == "True";

		document.write ("<td width=16> </td>");
		document.write ("<th class=RECORD_TABLE_HEADER width=10%> " + TRANSLATION_getValue ("header_code") + " </th>");
		document.write ("<th class=RECORD_TABLE_HEADER> " + TRANSLATION_getValue ("header_name") + " </th>");
		document.write ("<th class=RECORD_TABLE_HEADER width=10% align=middle> " + TRANSLATION_getValue ("header_item_count") + " </th>");
		document.write ("<th class=RECORD_TABLE_HEADER width=15% align=right> " + TRANSLATION_getValue ("header_price_per_item") + " </th>");
		document.write ("<th class=RECORD_TABLE_HEADER width=20% align=right> " + TRANSLATION_getValue ( showVAT ? "header_price_total_with_vat" : "header_price_total") + " </th>");

		document.write ("</tr>");
		
		currency = CONFIG_getValue ("eUsedCurrency");
		showMeasurement = CONFIG_getValue ("cbShowMeasurement") == "True";

		for (BASKET_CONTENTS_i=0;BASKET_CONTENTS_i<parent.Basket.length;BASKET_CONTENTS_i++) 
			Basket_list (BASKET_CONTENTS_i, currency, showMeasurement, showVAT, showAdditional);

		document.write ("</table> <hr>");

		document.write ("<table width=100%>");	
		
		var total_price = Basket_computeTotal ();
		var vat = CONFIG_getValue ("eVATRate") / 100;
		
		if (showVAT)
		{
			var price_without_vat = total_price / (1+vat);
			document.write ("<tr> <td> <B> " + TRANSLATION_getValue ("price_total_without_VAT") + " : </td> <td align=right>" + format_Money (price_without_vat) + ",- " + CONFIG_getValue ("eUsedCurrency") + "</td> </tr>");
			document.write ("<tr> <td> <B> " + TRANSLATION_getValue ("price_VAT") + " : </td> <td align=right>" + format_Money (total_price - price_without_vat) + ",- " + CONFIG_getValue ("eUsedCurrency") + "</td> </tr>");
		}
		
		document.write ("<tr> <td> <B> " + TRANSLATION_getValue ("price_total" + (showVAT ? "_with_VAT" : "")) + " : </td> <td align=right>" + format_Money (total_price) + ",- " + CONFIG_getValue ("eUsedCurrency") + "</td> </tr>");

		document.write ("</table>");		
	}
}
	

