// Global variables
theProducts   = new Array ();
theDisplay    = new Array ();
theQty        = new Array ();
theLire       = new Array ();
thePrice      = new Array ();
theName       = new Array ();
theCatNo      = new Array ();
theLar        = new Array ();
theLun        = new Array ();
theCol        = new Array ();
theWeight     = new Array ();
deliveryCharges = new Array ("0","");
scontoCharges = new Array ("0","4","6","3","7","7","11","13","17","");
taxCharges = new Array ("20","0","");

totalItemsInArray = 0;

function GetTaxAndDelivery (form)
{

	form.taxRate.value=(taxCharges[document.forms[0].taxRate.selectedIndex]);
	form.scontoRate.value=(scontoCharges[document.forms[0].scontoRate.selectedIndex]);
	form.delivery.value=(deliveryCharges[document.forms[0].delivery.selectedIndex]);
}

function Monify(value)
{
  var str = "" + Math.round(value*100);
  var len = str.length;
  return (str=="0")?"0.00":(str.substring(0,len-2)+"."+str.substring(len-2,len));
}

function Lirefy(value)
{
  var str = "" + Math.round(value*100);
  var len = str.length;
  return (str=="0")?"0.00":(str.substring(len-2,0));
}



function InsertInArray (i, qty, lire, price, name, catNo, lar, lun, col, weight)
{
   if (qty > 0)
   {
      theQty[i]       = qty;
	  theLire[i]      = lire;
      thePrice[i]     = price/1.2;
      theName[i]      = name;
      theCatNo[i]     = catNo;
	  theLar[i]       = lar;
	  theLun[i]       = lun;
	  theCol[i]       = col;
      theWeight[i]    = weight;
      theProducts[i]  = "|info@quartulli.com,"+theQty[i]+","+theLire[i]+","+thePrice[i]+","+theName[i]+","+theCatNo[i]+","+theLar[i]+","+theLun[i]+","+theCol[i]+","+theWeight[i];
      theDisplay[i]   = "N°"+theQty[i]+" confezioni art. "+theCatNo[i]+" da "+theLar[i]+" x "+theLun[i]+" colore "+theCol[i]+"\r\n";
   }
   else
   {
      theCatNo[i]     = catNo;
      theProducts[i]  = "";
   }
}

function CheckTheProduct (qty, lire, price, name, catNo, lar, lun, col, weight)
{
   price = Monify (price);
   var i = 0;
   while (i < totalItemsInArray)
   {
      // Two items with the same or no catalog number are allowed, if they have different names
      if (theCatNo[i] == catNo && theName[i] == name)
      {
         InsertInArray (i, qty, lire, price, name, catNo, lar, lun, col, weight);
         return;
      }
      i++;
   }

   // If adding a new item, its qty must be more than 0
   if (qty > 0)
   {
      InsertInArray (i, qty, lire, price, name, catNo, lar, lun, col, weight);
      totalItemsInArray++;
   }
}

function PrepareProductsList (form)
{
   var products = "";
   var display  = "";
   var i = 0;
   var sub = 0;
   while (i < totalItemsInArray)
   {
      // CONSTRUCT THE PRODUCTS LIST
      if (theProducts[i])
      {
         products += theProducts[i];
         display  += theDisplay[i];
         sub += theQty[i]*theLar[i]*theLun[i]*thePrice[i];
	  }
      i++;
     }
   products += "|";
   form.orderproducts.value    = products;
   form.displayProducts.value = display;
   form.total.value    = Monify(sub);

   // Get the sconto charge. The value is converted from string to intger.
   // Get the delivery charge. The value is converted from string to intger.
   // Get the tax charge. The value is converted from string to intger
   // Calculate the sconto + tax + delivery charge; If tax rate is negative ignore it
   // Do not add the tax or delivery if the sub total is 0
   var delivery = parseFloat (deliveryCharges[form.delivery.selectedIndex]);
   if (delivery < 0) { delivery = 0; }
   var sconto = parseFloat (scontoCharges[form.scontoRate.selectedIndex]);
   if (sconto < 0) { sconto = 0; }
   var tax = parseFloat (taxCharges[form.taxRate.selectedIndex]);
   if (tax < 0) { tax = 0; }
   if (sub > 0) { form.subTotal.value    = Monify((sub - sub * sconto/100)+((sub - sub * sconto/100) * tax/100) + delivery); }
   if (sub > 0) { form.liretotale.value    = Lirefy(((sub - sub * sconto/100)+((sub - sub * sconto/100) * tax/100) + delivery)*1937.27 ); }
   else { form.subTotal.value    = "0.00"; } 

}

function CtrlObbl(Field, Label) {
	if (Field.value == "") {
		alert("Non è stato selezionato alcun articolo !");
		products = "";
	} 
	else {
		return true
	}
}

function ControllaCampo(Form) 
	{
	// Controlli se i campi obbligatori sono vuoti o meno
	if (!CtrlObbl(Form.orderproducts))		return false;
	return true;
}


function D_M_Table(form, form_element, lire, price, name, catNo, form_lar, form_lun, form_col, weight)
{
   var qty = Math.abs(form_element.value);
   var lar = (form_lar.value);
   var lun = (form_lun.value);
   var col = (form_col.value);
      CheckTheProduct (qty, lire, price, name, catNo, lar, lun, col, weight);
   PrepareProductsList (form);
//   form.submit();
}

function ClearAndReset (form)
{
   totalItemsInArray = 0;
}
