function trim(str){
  return str.replace(/^\s+/,'').replace(/\s+$/,'');
}//trim

function printErrori(err) {
  var msg = "Si sono verificati i seguenti errori:";
  var isCheck = err.length == 0;

  if (!isCheck)
    alert(msg + err);

  return isCheck;
}// printErrori

function trimField(obj){
  obj.value = trim(obj.value);
}//trimField

function winOpenStandard(indirizzo) {
  winOpen("Attendere...", indirizzo, "", "width=500,height=400'");
}

function winOpen(testo, url, target, options) {
  wOp = window.open("", target, options);
  wOp.document.write("<html>" +
                     "<head>" +
                       "<title>Attendere</title>" +
                       "<link name=\"tagCss\" id=\"tagCss\" rel=\"stylesheet\" href=\"css/style.css\">" +
                     "</head>" +
                     "<body>" +
                       "<table width='100%' height='100%'>" +
                         "<tr valign='middle' height='100%'>" +
                           "<td align='center' width='100%'>" +
                             "<b>" +
                               testo +
                             "</b>" +
                           "</td>" +
                         "</tr>" +
                       "</table>" +
                     "</body>" +
                     "</html>");
  wOp.location.href = url;

  return wOp;
}// winOpen

function addBR(strIn, nCh) {
  var strOut = "";
  var i, count;

  if (strIn.length <= nCh)
    return strIn;

  count = 1;
  for (i = 0; i < strIn.length; i++, count++) {
    strOut += strIn.substring(i, i + 1);

    if (strIn.substring(i, i + 1) == " ")
      count = 0;

    if (count == nCh) {
      strOut += "<br>";
      count = 0;
    }// if
  }// for

  return strOut;
}// addBR

function calcolo() {
  imp = form1.importo.value;
  re = /[^0-9^\.]/g;
  p = imp.search(re);
  if((p != -1) || (imp.length == 0))
        alert('Importo non valido (es: € 5.34)');  else
      form1.submit();
}

function openWindowImg(id) {
  openWindowImg(id, 365, 500);
}

function openWindowImg(id, width, height) {
  winOpen("Attendere...", "displayImg.php?id=" + id, "", "width=" + width + ",height=" + height + "");
}

function indietro() {
  history.back();
}

function checkForm(objForm) {
  var i;
  var errori = "";
  for (i = 0; i < objForm.elements.length; i++) {
    objElem = objForm.elements[i];

    if (objElem.mandatoryLabel && objElem.mandatoryLabel != "") {
      var cont = "";
      if (objElem.type == "text")
        cont = objElem.value;
      else if (objElem.type == "textarea")
        cont = objElem.innerText;
      else if (objElem.type == "hidden")
        cont = objElem.value;
      else if (objElem.type == "hidden")
        cont = objElem.value;
      else if (objElem.type == "password")
        cont = objElem.value;
      else if (objElem.type == "file")
        cont = objElem.value;

      if (cont == "")
        errori += "\n - Il campo " + objElem.mandatoryLabel + " e\' obbligatorio";
    }
  }

  return printErrori(errori);
}
