<!--
var debug = false;
var defForm = 'form0';
var sep = ', ';

function help()
{
  var url = document.URL;
  var i = url.lastIndexOf("/");
  if (i!=-1) url = url.substring(i+1, url.length);
  popup(400, 200, "./help/"+url);
}

// SOTTOMETTE UNA FORM. USI:
// - go()            : sottomette 'defForm' usando l'action predefinita
// - go(action)      : sottomette 'defForm' usando 'action'
// - go(action, form): sottomette 'form' usando 'action'
// - go("", form)    : sottomette 'form' usando l'action di default
function go(action, formName)
{
  if ((typeof formName)=='undefined') formName = defForm;
  if ((typeof action)!='undefined' && action!="") eval("document." + formName + ".action = '" + action + ".asp';");
  eval("document." + formName + ".submit();");
}

// UTILITY

// SETTA IL FOCUS
// - formName: form in sui settare il focus [default: form0]
// - fieldName: campo a cui dare il focus [defult: primo cmpo di testo]
function setFocus(fieldName, formName)
{
  if ((typeof fieldName)=='undefined' || fieldName=="")
  {
    var i, j;
    if ((typeof formName)!='undefined')
    {
      els = document.forms[formName].elements;
      for (i=0; i<els.length; i++) if (isFocus(els[i])) { els[i].focus(); return; }
    }
    else for (j=0; j<document.forms.length; j++)
    {
      els = document.forms[j].elements;
      for (i=0; i<els.length; i++) if (isFocus(els[i])) { els[i].focus(); return; }
    }
  }
  else
  {
    if ((typeof formName)=='undefined') formName = defForm;
    var f = getField(fieldName, formName);
  	if (isFocus(f)) f.focus();
 }
}
// PROTOTIPO DA CHIAMARE NELL'onLoad
// - setta il focus al primo campo disponibile nella form di default
function loader()
{ setFocus(); }
// FUNZIONE DI CHECK SPECIFICA
// - se definita viene richiamata da details.asp prima della submit
function specificCheck()
{ return true; }
// RESETTA TUTTI I CAMPI DELLA FORM. (formName opzionale)
function clear(formName)
{
  if ((typeof formName)=='undefined') formName = defForm;
  var els = document.forms[formName].elements;
  var i;
  for (i=0; i<els.length; i++) set(els[i].name, "", formName);
}

// FUNZIONI DI CONROLLO (isXXX)

// TORNANO true SE IL CAMPO E' VUOTO. GLI DA IL FOCUS. USI:
// - isXXX(field)      : ritorna true se 'defForm.field' verifica XXX; non da messaggi; gli da il focus se possibile
// - isXXX(field, msg) : ritorna true se 'defForm.field' verifica XXX; da il messaggio 'msg'; gli da il focus se possibile
// - isXXX(field, msg, form): ritorna true se 'form.field' verifica XXX; da il messaggio 'msg'; gli da il focus se possibile
// - isXXX(field, "", form) : ritorna true se 'form.field' verifica XXX; non da messaggi; gli da il focus se possibile
// CONTROLLI ESISTENTI
// - isBlank: da true se il campo è vuoto
// - isNum: da true se il campo è numerico
// - isDate: da true se il campo è una data corretta nel formato gg-mm-aaaa
function isBlank(fieldName, failMsg, formName)
{
  if (get(fieldName, formName)!="") return false;
  return !abend(fieldName, failMsg, formName);
}
function isNum(fieldName, failMsg, formName)
{
  var str = get(fieldName, formName);
  if (str=="") return true;
  if (!isNaN(parseFloat(str))) return true;
  return abend(fieldName, failMsg, formName);
}
var gg = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
function isDate(fieldName, formName)
{
  var str = get(fieldName, formName);
	var pattern = /^(\d+)(\/|\-|\.)(\d+)(\/|\-|\.)(\d+)$/;
	var arr = str.match(pattern);
  if (arr==null || arr.length<6) return abend(fieldName, "La data avere il formato gg-mm-aaaa.", formName);
  var g = parseFloat(arr[1]);
  var m = parseFloat(arr[3]);
  var a = parseFloat(arr[5]);
  if (a<1 || m<1 || g<1 || m>12) return abend(fieldName, "Data errata.", formName);
  if (a<100) a += 2000;
  if (g>gg[m-1]) return abend(fieldName, "Data errata.", formName);
  if (m==2 && g>28 && (a%4!=0 || (a%100==0 && a%400!=0))) return abend(fieldName, "Data errata.", formName);
  str = ((g>9)?"":"0") + g + "-" + ((m>9)?"":"0") + m + "-" + a;
  set(fieldName, str, formName);
  return true;
}
// funzione utilizzata da tutti gli isXXX per settare il focus, dare l'alert e ritornare
function abend(fieldName, failMsg, formName)
{
  failMsg = trim(failMsg);
  if ((typeof failMsg)!='undefined' && failMsg!="") alert(failMsg);
  var field = getField(fieldName, formName)
  if (field.focus) field.focus();
  return false;
}

// RESTITUISCE IL VALORE DI UN CAMPO DI INPUT. USI:
// - get(field)      : ritorna il valore di 'defForm.field'
// - get(field, form): ritorna il valore di 'form.field'
// ESITO IN FUNZIONE DEL TIPO DI CAMPO:
// - radio   : ritorna il value dell'unico checkato con nome 'field'
// - checkbox: ritorna i value di tutti i checkati con nome 'field' (separati da SEP(##))
// - select  : ritorna il value dell'item selezionato nella combo 'field'
// - text, password, textarea: ritorna il value di 'field'
function get(fieldName, formName)
{
  var field = getField(fieldName, formName);
  var val = "";
  var i = "";
  if (typeof field.type=='undefined' && isCheck(field[0]))
  {
    for (i=0; i<field.length; i++) if (field[i].checked==true)
    { if (val.length>0) val += sep; val += field[i].value; }
  }
  else if (isCheck(field))
  {
    if (field.checked) val = field.value;
  }
  else if (isSelect(field))
  {
    val = field.options[field.selectedIndex].value;
  }
  else if (isText(field))
  {
    val = field.value;
  }
  return trim(val);
}

// IMPOSTA IL VALORE DI UN CAMPO DI INPUT. USI:
// - set(field, val)      : setta a 'val' il campo 'defForm.field'
// - set(field, val, form): setta a 'val' il campo 'form.field'
// ESITO IN FUNZIONE DEL TIPO DI CAMPO:
// - radio   : checka il radio con value 'val' tra quelli con nome 'field'
// - checkbox: checka i checkbox con valore 'val' (separati da SEP(##)) tra quelli con nome 'field'
// - select  : seleziona l'item con value 'val' nell combo 'field'
// - text, password, textarea: imposta 'val' in 'field'
function set(fieldName, val, formName)
{
  var field = getField(fieldName, formName);
  var i;
  if (typeof field.type=='undefined' && isCheck(field[0]))
  {
    for (i=0; i<field.length; i++) field[i].checked = false;
    while ((i=val.indexOf(sep))>0)
    { select(field, val.substring(0, i), true, 'checked'); val = val.substring(i+sep.length); }
    select(field, val, true, 'checked');
  }
  else if (isCheck(field))
  {
    field.checked = (field.value==val);
  }
  else if (isSelect(field))
  {
    select(field.options, val, true, 'selected');
  }
  else if (isText(field))
  {
    field.value = val;
  }
}

// VERIFICA CHE UN CHECK SIA CHECKATO. USI:
// - isChecked(field, val)      : ritorna lo stato del check con value 'val' tra quelli con nome 'defForm.field'
// - isChecked(field, val, form): ritorna lo stato del check con value 'val' tra quelli con nome 'defForm.field'
function isChecked(fieldName, val, formName)
{
  var v = get(fieldName, formName);
  return v.substring(0, val.length)==val || v.indexOf(sep+val)>=0;
}
// CHECKA UN CHECKBOX. USI:
// - check(field, val, checked)      : checka (true/false in input) il check con value 'val' tra quelli con nome 'defForm.field'
// - check(field, val, checked, form): checka (true/false in input) il check con value 'val' tra quelli con nome 'form.field'
function check(fieldName, val, checked, formName)
{
  var field = getField(fieldName, formName);
  select(field, val, checked==true||checked=='on'||checked=='true'||checked=='1', 'checked');
}


// VARIE

// ritorna il campo indentificato da nome e form
function getField(fieldName, formName)
{
  if ((typeof formName)=='undefined') formName = defForm;
  var field = eval("document." + formName + "." + fieldName + ";");
  return field;
}
// toglie gli spazi in testa e in coda
function trim(str) 
{
  var i = 0;
  while (i<str.length && str.substring(i, i+1)==" ") i++;
  if (i==str.length) return "";
  var ii = str.length - 1; 
  while (str.substring(ii, ii+1)==" ") ii--;
  return str.substring(i, ii+1);
}

// PRIVATE

function select(field, val, check, sel)
{
  var i;
  if (isNaN(field.length))
  {
    if (field.value==val) eval("field." + sel + " = " + check);
  }
  else for (i=0; i<field.length; i++)
  {
    if (field[i].value==val) eval("field["+i+"]." + sel + " = " + check);
  }
}

function isText(field)   { return field.type=='text'||field.type=='textarea'||field.type=='password'||field.type=='hidden'||field.type=='file'; }
function isCheck(field)  { return field.type=='checkbox'||field.type=='radio'; }
function isSelect(field) { return field.type.substring(0,3)=='sel'; }
function isFocus(field)  { return field.type=='text'||field.type=='textarea'||field.type=='password'; }

//-->

function checkEnter(formName)
{
	if (event.keyCode==13)
		go(formName);
}

