// Script Utility.js

// Procedura per la visualizazione della data corrente
function VisualizzaData()
{
  var print1 = "";
  var print2 = "";
  var print3 = "";
  var today = new Date();
  var date = today.getDate();
  var weekday = today.getDay();
  var month = today.getMonth();
  var year = today.getFullYear();

  if(weekday == 1)      print1='Lunedì';
  else if(weekday == 2) print1='Martedì';
  else if(weekday == 3) print1='Mercoledì';
  else if(weekday == 4) print1='Giovedì';
  else if(weekday == 5) print1='Venerdì';
  else if(weekday == 6) print1='Sabato';

  if(month == 0)        print2='Gennaio';
  else if(month == 1)   print2='Febbraio';
  else if(month == 2)   print2='Marzo';
  else if(month == 3)   print2='Aprile';
  else if(month == 4)   print2='Maggio';
  else if(month == 5)   print2='Giugno';
  else if(month == 6)   print2='Luglio';
  else if(month == 7)   print2='Agosto';
  else if(month == 8)   print2='Settembre';
  else if(month == 9)   print2='Ottobre';
  else if(month == 10)  print2='Novembre';
  else if(month == 11)  print2='Dicembre';

  document.write(print1,', ', date, ' ', print2, ' ', year, ' ');
}

// Procedura per l'apertura di una finestra
function ApriFinestra(path, title, width, height, settings)
{
  var win = null; // Finestra
  var win_left = (screen.width - width) / 2;        // Margine sinistro della finestra
  var win_top  = (screen.height - height) / 2 - 50; // Margine superiore della finestra
  
  // Impostazioni
  if(settings != "")
    settings += ",";
  settings += 'width=' + width + ',';
  settings += 'height=' + height + ',';
  settings += 'top=' + win_top + ',';
  settings += 'left=' + win_left;
  
  // Apertura della finestra e impostazione del fuoco
  win = window.open(path, title, settings);
  if(win.window.focus)
    win.window.focus();
}

// Funzione per l'acquisizione di un oggetto della pagina conoscendo il suo id
function AcquisisciOggetto(id)
{ 
  var obj = null; 

  if(document.getElementById)
    obj = document.getElementById(id);
  else if (document.layers) 
    obj = document.layers[id]; 
  else if (document.all) 
    obj = document.all[id]; 

  return obj; 
} 

// Procedura per l'aggiunta dei valori di un prodotto nel carrello
function AggiungiProdotto(formName, txtIdHName, txtQtaHName, prdId, txtQtaName)
{
  var form    = AcquisisciOggetto(formName);
  var txtIdH  = AcquisisciOggetto(txtIdHName);
  var txtQtaH = AcquisisciOggetto(txtQtaHName);
  var txtQta  = AcquisisciOggetto(txtQtaName);
  
  var n = new Number(txtQta.value);
  
  txtQta.style.textAlign = "right";
  if(prdId > 0 && !isNaN(n) && n > 0)
  {
    txtIdH.value  = prdId;
    txtQtaH.value = txtQta.value;  
    form.submit();
  }
  else
  {
    txtQta.style.backgroundColor = "#ff0000";
  }
}

// Procedura per la modifica dei valori di un prodotto del carrello
function ModificaProdotto(formName, txtIdHName, txtQtaHName, prdId, txtQtaName)
{
  var form    = AcquisisciOggetto(formName);
  var txtIdH  = AcquisisciOggetto(txtIdHName);
  var txtQtaH = AcquisisciOggetto(txtQtaHName);
  var txtQta  = AcquisisciOggetto(txtQtaName);
  
  var n = new Number(txtQta.value);
  if(prdId > 0 && !isNaN(n) && n >= 0)
  {
    txtIdH.value  = prdId;
    txtQtaH.value = txtQta.value;  
    form.submit();
  }
  else
  {
    txtQta.style.backgroundColor = "#ff0000";
  }
}

// Procedura per la cancellazione di un prodotto dal carrello
function CancellaProdotto(formName, txtIdHName, txtQtaHName, prdId, txtQtaName)
{
  var form    = AcquisisciOggetto(formName);
  var txtIdH  = AcquisisciOggetto(txtIdHName);
  var txtQtaH = AcquisisciOggetto(txtQtaHName);
  var txtQta  = AcquisisciOggetto(txtQtaName);
  txtQta.value = 0;
  
  if(prdId > 0)
  {
    txtIdH.value  = prdId;
    txtQtaH.value = 0;  
    form.submit();
  }
}

// Procedura per tornare all'inizio del carrello
function TornaInizioCarrello()
{
  div = AcquisisciOggetto('divCarrello'); 
  div.scrollTop = 0;
  window.scrollTo(0,0);
}
