function cntr(tipo,campo,scrivi)
{
	ritorna = true;
	if (tipo==1)
	{
		cmp = eval("fr." + campo)
		if (cmp.value.length < 1)
		{
			alert(scrivi);
			cmp.focus();
			ritorna = false;
		}
	}
	return(ritorna);
}

function cambia(v1,v2)
{
	dest = eval("fr.v" + v1);
	dest.disabled = !v2;
}

function anteprima(valore)
{
	ante = eval("fr." + valore + ".value");
	if (ante=="no")
	{
		return(0);
	}
		alert(ante);
		var newwin;
		newwin = window.open(ante);
}

function isDate(dateStr) {

  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is format OK?

  if (matchArray == null) {
    alert("Inserire una data in formato gg/mm/aaaa");
    return false;
  }

  // parse date into variables
  day  = matchArray[1];
  month = matchArray[3];
  year = matchArray[5];

  if (month < 1 || month > 12) { // check month range
    alert("Inserire una data in formato gg/mm/aaaa");
    return false;
  }

  if (day < 1 || day > 31) {
    alert("Inserire una data in formato gg/mm/aaaa");
    return false;
  }

  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    alert("Inserire una data valida")
    return false;
  }

  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
      alert("Inserire una data valida");
      return false;
    }
  }
  return true;  // date is valid
}


function isDateNM(dateStr) {

  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is format OK?

  if (matchArray == null) {
    return false;
  }

  // parse date into variables
  day  = matchArray[1];
  month = matchArray[3];
  year = matchArray[5];

  if (month < 1 || month > 12) { // check month range
    return false;
  }

  if (day < 1 || day > 31) {
    return false;
  }

  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    return false;
  }

  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
      return false;
    }
  }
  return true;  // date is valid
}

function isTime(camp){
	tempo = true
    tid = String(camp.value);
	if (tid.length < 1){return(false)}
	if  (isNaN(tid))
	{
		tempo = false
	} else
	{
		tt = parseInt(tid.substring(0,2));
		mm = parseInt(tid.substring(2,4));
	
		if(tt > 23||mm > 59)
		{tempo = false;}
    }   
    if (!tempo)
	{
		alert("Quello inserito non è un orario valido");
		camp.focus();
	}
	return(tempo)
}

function stringa_ctnr(v1,v2,v3,v4)
{
	if (!stringa_valida(v1,v2)){return false}
	valida = true;
	dest = eval("fr." + v1)
	if (v3 == 1) // testo
	{
		if (dest.value.length < 1 || (v4 > 0 && dest.value.length > v4))
		{
			if (dest.value.length < 1)
			{
			alert("Campo " + v2 + " vuoto");
			}else
			{
			alert("Campo " + v2 + " con lunghezza superiore a quella consentita ("  + v4 + ")");
			}
			dest.focus();
			valida = false
		}
	}
	if(v3==2) // numerico obbligatorio
	{
		if (isNaN(dest.value) || dest.value.length < 1)
		{
			alert("Campo " + v2 + " vuoto o non numerico");
			dest.focus();
			valida = false
		}
	}
	if(v3==3) // numerico non obbligatorio
	{
		if (isNaN(dest.value) || dest.value.length < 1)
		{
			alert("Campo " + v2 + " vuoto o non numerico");
			dest.focus();
			valida = false
		}
	}
	if(v3==4) // data
	{
		if (!isDate(dest.value))
		{
			dest.focus();
			valida = false
		}
	}
	if (v3 == 5) // testo non obbligatorio
	{
		if (v4 > 0 && dest.value.length > v4)
		{
			alert("Campo " + v2 + " con lunghezza superiore a quella consentita ("  + v4 + ")");
			dest.focus();
			valida = false
		}
	}
	if (v3 == 6)
	{
		if (dest.selectedIndex < 0)
		{
			alert("Nessuna selezione per " + v2);
			dest.focus();
			valida = false;
		}
	}
	if(v3==7) // data non obbligatoria
	{
		if (dest.value > 0)
		{
			if (!isDate(dest.value))
			{
			dest.focus();
			valida = false
			}
		}
	}
	
	return valida
}

function lista_cntr(lista,nome)
{
	quanti = lista.length - 1
	if (quanti < 0)
	{
		alert("Lista " + nome + " vuota");
		return "0";
	}
	tmp = "";
	for (i=0;i<=quanti;i++)
	{
		tmp = tmp + "^" + lista.options[i].value;
	}
	return tmp;
}

function stringa_valida(v1,v2)
{
	valida = true;
	dest = eval("fr." + v1)
	for (i=0;i<dest.value.length;i++)
	{
		if (dest.value.charCodeAt(i) == 34 || dest.value.charCodeAt(i) == 35 || dest.value.charCodeAt(i) == 39)
		{
			valida = false;
			break;
		}
	}
	if (valida==false)
	{
		alert("Caratteri non validi nel campo " + v2);
		dest.focus();
	}
	return valida
}

function avviso()
{
	alert("La sessione di lavoro scadrà tra 1 minuto. Salva i dati.");
}

function nu_mun(va1,va2)
{
	if (va1.value.length < 1){return 0}
	indice = -1;
	for (i=0; i < va2.options.length; i++)
	{
		if (va2[i].text == va1.value)
		{
			indice = i;
			break;
		}
	}
	if (indice > -1)
	{
		va2.selectedIndex = indice;
	}else
	{
		va2[va2.options.length] = new Option('');
		va2[va2.options.length-1].value = 0;
		va2[va2.options.length-1].text = va1.value;
		va2.selectedIndex = va2.options.length -1;
	}
}

function ag_itm(prov,dest)
{
	var quanti = dest.length - 1;
	var selez = prov.selectedIndex;
	var esiste = false;
	if (selez>-1)
	{
		for (var i=0; i<= quanti; i++) 
		{
			if (prov.options[selez].value == dest.options[i].value)
			{esiste = true}
		}

		if (!esiste)
		{
			dest[dest.options.length] = new Option('');
			dest[dest.options.length-1].value = prov.options[selez].value;
			dest[dest.options.length-1].text = prov.options[selez].text;
			if (selez < (prov.length -1))
			{
				prov.selectedIndex = selez + 1
			}
		}
	}
}



function rm_itm(dest)
{
	if (dest.selectedIndex >-1)
	{
		dest.remove(dest.selectedIndex)
	}
}

function nu_itm(nuovo,prov,dest)
{
	if (nuovo.value.length < 1)
	{
		alert("Campo nuova lingua vuoto");
		nuovo.focus();
		return(0)
	}
	quanti = prov.length - 1;
	for (var i=0; i<= quanti; i++) 
	{
		if (nuovo.value.toLowerCase() == prov.options[i].text.toLowerCase())
		{
			alert("Lingua esistente");
			nuovo.value = "";
			return (0);
		}
	}
	quanti = dest.length -1;
	var esiste = false;
	for (var i=0; i<= quanti; i++) 
	{
		if (nuovo.value.toLowerCase() == dest.options[i].text.toLowerCase())
		{esiste = true}
	}

	if (!esiste)
	{
		dest[dest.options.length] = new Option('');
		dest[dest.options.length-1].value = "no";
		dest[dest.options.length-1].text = nuovo.value;
	}
	nuovo.value="";
}

function nuovo_itm(lista,testo,id)
{
	if (testo.length < 1){return 0}
	lista[lista.options.length] = new Option('');
	lista[lista.options.length-1].value = id;
	lista[lista.options.length-1].text = testo;
	lista.selectedIndex = lista.options.length -1;
}
