_TOP=1;
_BOTTOM=2;

_ONEORMORE = 1;
_JUSTONE =2;
InvioOk=false;

function element_id(element_id) {
// This method returns the element corresponding to the id
	if (document.getElementById) {
		return document.getElementById(element_id);
	}
	else if (document.all) {
		return document.all[element_id];
	}
	else if (document.layers) {
	return document.layers[element_id];
	} else {
		return undefined;
	}
}
function mese_scorso(str) {
today=new Date()

giorno=str.substr(0,2)
mese=str.substr(3,2)
anno=str.substr(6,4)

var data_bolla=new Date(anno, mese-1, giorno) //Month is 0-11 in JavaScript
var mese_scorso=new Date(today.getFullYear(), today.getMonth()-1, today.getDate()) //Month is 0-11 in JavaScript

//Set 1 day in milliseconds
var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
if ((Math.floor((data_bolla.getTime()-mese_scorso.getTime())/(one_day))<31) && (Math.floor((data_bolla.getTime()-mese_scorso.getTime())/(one_day))>0))
	return true
else 
	return false

}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


//	FUNZIONA CHE CONTROLLA SE UN CAMPO E' UN NUMERO
function isNumeric(field)
{

	if(isNaN(field.value))
		field.value=field.value.substr(0,(field.value.length-1));

}


function select(a, changeTo)
{
  a.style.backgroundColor = changeTo;
}





function openWin(nome,width,height)
{

if(!width)
	width=540;

if(!height)
	height=360;

var winWidth = screen.availWidth;
var winHeight = screen.availHeight;
var popupWidth = width;
var popupHeight = height;

var xCoord = ((winWidth-popupWidth)/2);
var yCoord = ((winHeight-popupHeight)/2);

window.open(nome,'NuovaFinestra','toolbar=no, scrollbars=no, width='+popupWidth+',height='+popupHeight+',left='+xCoord+',top='+yCoord+'screenX='+xCoord+',screenY='+yCoord+'');

}





// funzione per formattare un campo come data
// Parametri : campo input


function MaskDataddmmyyyy (field) {
  var testo="";
  var selectedText = document.selection;
  if (selectedText.type == 'Text') 
  {
     var newRange = selectedText.createRange();
	 testo = newRange.text;
  } 
  if (testo.length>0 && testo==field.value)
	field.value='';
  switch (field.value.length) {
    case 0: 
      return String.fromCharCode(event.keyCode)>='0' && String.fromCharCode(event.keyCode)<'4';
      break;
    case 1:
	  //secondo numero maggiore di 0. NO a giorno 00, se però primo numero > 0 allora ok a 0 come secondo numero, es: 10, 20, 30gg
	  if (field.value.substring(0,1)>=1){
		var valOk=0;
	  }else{
		var valOk=1;
	  }
      if (/\d/.test(String.fromCharCode(event.keyCode)) && String.fromCharCode(event.keyCode)>=valOk)
	  field.value=field.value+String.fromCharCode(event.keyCode)+"/";
	  return false;
      break;
    case 2:
      event.keyCode = '/'.charCodeAt();
      return true;
      break;
    case 3:
      return String.fromCharCode(event.keyCode)>='0' && String.fromCharCode(event.keyCode)<'2';
	  break;
    case 4:
 	  //Se il primo numero del mese è 1 possibilità di 10-11-12 (mesi) altrimenti 01-02-03 ecc. NO a mese 00
	  if (field.value.substring(4,3)=='1'){
	     if (/\d/.test(String.fromCharCode(event.keyCode)) && String.fromCharCode(event.keyCode)<='2')
		 field.value=field.value+String.fromCharCode(event.keyCode)+"/";
    	 return false;
	 }else{
		 if (/\d/.test(String.fromCharCode(event.keyCode)) && String.fromCharCode(event.keyCode)>='1')
		 field.value=field.value+String.fromCharCode(event.keyCode)+"/";
    	 return false;
	 }
      break;
    case 5:
      event.keyCode = '/'.charCodeAt();
      return true;
      break;
    case 6:
		return String.fromCharCode(event.keyCode)>='1' && String.fromCharCode(event.keyCode)<'3';
		break;
    case 7:
    case 8:
    case 9:
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;
    default:
      return false;
      break;
  }
}



function MaskDatahhmm (field) {
  var testo="";
  var selectedText = document.selection;
  if (selectedText.type == 'Text') 
  {
     var newRange = selectedText.createRange();
	 testo = newRange.text;
  } 
  if (testo.length>0 && testo==field.value)
	field.value='';
  switch (field.value.length) {
    case 0: 
      return String.fromCharCode(event.keyCode)>='0' && String.fromCharCode(event.keyCode)<'3';
      break;
    case 1:
      if (/\d/.test(String.fromCharCode(event.keyCode)))
	field.value=field.value+String.fromCharCode(event.keyCode)+".";
      return false;
      break;
    case 2:
      event.keyCode = '.'.charCodeAt();
      return true;
      break;
    case 3:
    case 4:
      return String.fromCharCode(event.keyCode)>='0' && String.fromCharCode(event.keyCode)<'7';
      break;
    default:
      return false;
      break;
  }
}


// controlla se sono selezionate delle voci in un elenco a discesa
// Parametri : elenco : elenco da controllare, how : indica il tipo di controllo (_ONEORMORE : 1 o più, _JUSTONE : 1 e solo 1)

function checkSelected(elenco, how) 
  {
  if (how==_JUSTONE)
    {
	tot=0;
	for(t=0;t<elenco.length;t++)
	  if (elenco.options[t].selected) tot++;
	if (tot==0)
	  alert('Selezionare un valore');
	else
	  if (tot>1)
		alert('Selezionare un solo valore');
	return tot==1;
	}
  else
    if (how==_ONEORMORE)
	  {
	  tot=0;
	  for(t=0;t<elenco.length;t++)
  	    if (elenco.options[t].selected) tot++;
  	  if (tot==0)
  	    alert('Selezionare almeno un valore');
	  return tot>=1;
	  }
  }
  
  
_MOVE = 1;
_COPY =2;
InvioOk=false;

// Sposta le voci selezionate da un elenco ad un altro
// Parametri : elencosorg : elenco sorgente, elencodest : elenco destinazione, how : (se _MOVE sposta la selezione, con _COPY la copia)

function moveSelection(elencosorg, elencodest, how) 
  {
//  var option0 = new Option("Red", "color_red")   var option1 = new Option("Blue", "color_blue")   var option2 = new Option("Yellow", "color_yellow")   var option3 = new Option("Green", "color_green")
//   for (var i=0; i < 4; i++) {      eval("inForm.selectTest.options[i]=option" + i)
	for(t=0;t<elencodest.length;t++)
		elencodest.options[t].selected = false;
	for(t=0;t<elencosorg.length;t++)
		if (elencosorg.options[t].selected) 
			elencodest.options[elencodest.length]=new Option(elencosorg.options[t].text,elencosorg.options[t].value,false,true);
	if (how==_MOVE)
		for(t=elencosorg.length-1;t>=0;t--)
			if (elencosorg.options[t].selected)
				elencosorg.options[t]=null;
	// history.go(0);
    return true;
  }
  
  
// Seleziona tutte le voci di un elenco
// Parametri : elenco : elenco da selezionare

function selectAll(elenco)
{
	for(t=0;t<elenco.length;t++) elenco.options[t].selected = true;
	return true;
}
  
// Elimina le voci selezionate da un elenco 
// Parametri : elenco : elenco da cui eliminare le voci
function deleteSelection(elenco) 
  {
	for(t=elenco.length-1;t>=0;t--)
		if (elenco.options[t].selected)
			elenco.options[t]=null;
    return true;
  }

_TOP=1;
_BOTTOM=2;
// Aggiunge una voce ad un elenco
// Parametri : elenco : elenco a cui aggiungere la voce, Unique : se true impedisce l'inserimento di valori duplicati, Testo : testo dell'opzione, Valore: Value per l'opzione, DefaultSelected : true se la voce è selezionata di default, Selected : true se la voce è selezionata
//             ResetSelection : reimposta la selezione ogni volta, Position : _TOP oppure _BOTTOM inserisce in cima o in fondo
function addToSelection(elenco,Unique,Testo,Valore,DefaultSelected, Selected, ResetSelection, Position) 
{
  Trovato=false;
  if (Unique)
  {
  	for (t=0;t<elenco.length;t++) 
  	{ 
      var elencoValue=elenco.options[t].value;
      var elencoText=elenco.options[t].text;
      if ((Testo.toUpperCase()==elencoText.toUpperCase()) || (Valore.toUpperCase()==elencoValue.toUpperCase())) Trovato=true;
  	}
  }
  if (Trovato)
  {
    alert('Tentativo di inserire un valore duplicato');
    return false;
  }
  else
  {
    if (ResetSelection) for (t=0;t<elenco.length;t++) elenco.options[t].selected=false;
    if (Position==_TOP) 
    {
    	var option = new Option();
    	elenco.options[elenco.length]=option;
		var number=elenco.length;
    	for (t=number-1;t>0;t--)
    	{
    		var opzionexhg=new Option (elenco.options[t-1].text,elenco.options[t-1].value,elenco.options[t-1].defaultSelected,elenco.options[t-1].selected);
      		elenco.options[t]=opzionexhg;
    	}
    }
  	var option = new Option(Testo,Valore,DefaultSelected,Selected);
  	elenco.options[((Position==_TOP)?0:elenco.length)]=option;
    return true;
  }
}



// Legge gli elenchi di modelli compresi in modelli-modulo.js
// Parametri : Tipo : Tipo di modello da leggere, Elenco : campo Select HTML dove inserire i valori
_HTMLMODULE = 1;
_CHIAVE = 0;
_VALORE = 1;
function GetModelli(Tipo,Elenco)
{
	if (Tipo==_HTMLMODULE)
	{
		while (Elenco.length>0) Elenco.options[Elenco.length-1]=null;
		Elenco.options[Elenco.length]=new Option("-- Seleziona un Modello --",-1,false,true);
		for(t=0;t<_ModuliHtml.length;t++) 
		  Elenco.options[Elenco.length]=new Option(_ModuliHtml[t][_CHIAVE],t,false,false);
	}
}

// Legge un valore di modello da modelli-modulo.js
// Parametri : Tipo : Tipo di modello da leggere, Numero : numero del modello
function GetModello(Tipo,Numero)
{
	if (Tipo==_HTMLMODULE)
	{
		return _ModuliHtml[Numero][_VALORE];
	}
}


// modifica i permessi per tutte le voci selezionate di un elenco a discesa
// Parametri : elenco : elenco da modificare, Lettura/Scrittura: True / False / Vuoto = legge valore corrente e lo nega,

function ChangePermission(elenco, Lettura, Scrittura) 
{
	for(t=0;t<elenco.length;t++)
		if (elenco.options[t].selected) 
	  	{
	  	Valore=elenco.options[t].value;
	  	OldLettura=Valore.charAt(Valore.indexOf('[')+1);
	  	OldScrittura=Valore.charAt(Valore.lastIndexOf('[')+1);
	  	if ((Lettura!="T" && Lettura!="F") || (Scrittura!="T" && Scrittura!="F")) 
	  	{
	  		Lettura=OldLettura;
	  		Scrittura=OldScrittura;
	  		if (Lettura=="F" && Scrittura=="F") 
	  			Lettura="T";
	  		else 
	  			if (Lettura=="T" && Scrittura=="F")
	  				Scrittura="T";
	  			else
	  				if (Lettura=="T" && Scrittura=="T")
	  					Scrittura=Lettura="F";
	  	}
	  	Valore=Valore.substring(0,Valore.indexOf('['));
	  	Valore=Valore+'['+Lettura+']'+'['+Scrittura+']';
	  	Testo=elenco.options[t].text;
	  	Testo=Testo.substring(0,Testo.lastIndexOf(']')-6)+'['+Lettura+']'+' ['+Scrittura+']'+Testo.substring(Testo.lastIndexOf(']')+2,1000);
	  	elenco.options[t].value=Valore;
	  	elenco.options[t].text=Testo;
	  	}	

}



// Elimina le voci selezionate da un elenco 
// Parametri : elenco : elenco da cui eliminare le voci
function deleteSelection(elenco) 
  {
	for(t=elenco.length-1;t>=0;t--)
		if (elenco.options[t].selected)
			elenco.options[t]=null;
    return true;
  }


// Aggiunge una voce ad un elenco
// Parametri : elenco : elenco a cui aggiungere la voce, Unique : se true impedisce l'inserimento di valori duplicati, Testo : testo dell'opzione, Valore: Value per l'opzione, DefaultSelected : true se la voce è selezionata di default, Selected : true se la voce è selezionata
//             ResetSelection : reimposta la selezione ogni volta, Position : _TOP oppure _BOTTOM inserisce in cima o in fondo
function addToSelection(elenco,Unique,Testo,Valore,DefaultSelected, Selected, ResetSelection, Position) 
{
  Trovato=false;
  if (Unique)
  {
  	for (t=0;t<elenco.length;t++) 
  	{ 
      var elencoValue=elenco.options[t].value;
      var elencoText=elenco.options[t].text;
      if ((Testo.toUpperCase()==elencoText.toUpperCase()) || (Valore.toUpperCase()==elencoValue.toUpperCase())) Trovato=true;
  	}
  }
  if (Trovato)
  {
    alert('Tentativo di inserire un valore duplicato');
    return false;
  }
  else
  {
    if (ResetSelection) for (t=0;t<elenco.length;t++) elenco.options[t].selected=false;
    if (Position==_TOP) 
    {
    	var option = new Option();
    	elenco.options[elenco.length]=option;
		var number=elenco.length;
    	for (t=number-1;t>0;t--)
    	{
    		var opzionexhg=new Option (elenco.options[t-1].text,elenco.options[t-1].value,elenco.options[t-1].defaultSelected,elenco.options[t-1].selected);
      		elenco.options[t]=opzionexhg;
    	}
    }
  	var option = new Option(Testo,Valore,DefaultSelected,Selected);
  	elenco.options[((Position==_TOP)?0:elenco.length)]=option;
    return true;
  }

}



// FUNZIONE PER INSERIRE DEL TESTO HTML ALL'INTERNO DI UN TAG ID (TD, O BODY ...)
function insertHtmltxt(field, Txt)
{

	var IE = navigator.appName.indexOf("Microsoft") != -1;

	if(IE)
	{
		if(document.all[field]){
		  document.all[field].innerText="";
		  document.all[field].insertAdjacentHTML("beforeEnd",Txt);
		}
	}
	else
	{

	  var range = "";
	  var docFrag = "";
	  document.getElementById(field).innerText="";
	  document.getElementById(field).innerHTML="";
	  range = document.createRange();
	  range.setStartBefore(document.getElementById(field));
	  docFrag = range.createContextualFragment(Txt);
	  document.getElementById(field).appendChild(docFrag);

	}





}


function ValidateForm(nameForm)
{

  err='';

  // se non è definito usa questo per andare avanti senza errori
  if(undefined===window.fieldRequest)
  {
    fieldRequest = new Array();  
  }


  // Imposta forma di riferimento
  //form = document[nameForm];
  form = nameForm;


  for(i=0; i<fieldRequest.length; i++)
  {

	valueField = form[fieldRequest[i][0]].value;

	switch(fieldRequest[i][2])
	{
	
		case 'txt': 
			if(valueField=='')
				err += "\n"+(i+1)+'- '+fieldRequest[i][1];
			break;

		case 'data': 
			if(valueField.length < 10)
				err += "\n"+(i+1)+'- '+fieldRequest[i][1];
			break;

		case 'email': 
			if(valueField!='')
			{
				p=valueField.indexOf('@');
				if(p<3 || p==(valueField.length-1))
					err += "\n"+(i+1)+'- '+fieldRequest[i][1];
			}
			else
			{
				err += "\n"+(i+1)+'- '+fieldRequest[i][1];
			}

			break;
			
			case 'privacy': 
			valueField = form[fieldRequest[i][0]][0].checked;
			if(!valueField)
				err += "\n"+(i+1)+'- '+fieldRequest[i][1];
			break;
	
	}

  }


  if(err)
  {
	alert('ATTENZIONE! Si sono verificati i seguenti errori:'+err);
	return false;
  }
  else
	return true

}
