/************************3*********************************************************************
 PreviFast.js
 ALA Gestione Guasti - Libreria di funzioni javascript
 Ultima modifica: 13.02.2006
- 31.10.2006:	jDateDiff() nuova
- 15.11.2006:	correzione di jDateDiff(): radix=10
- 20.12.2007:	correzione a removeAllOptionsBut()
*********************************************************************************************/


var TYPE_STRING = 3;	//cella valorizzata (non contiene un controllo html)
var ERR_DATA = "Data non valida";
var C_ANAG = "AngContraenti";
var C_DATIN = "DatiNascitaContr";
var P_DATI = "DatiAngPra";
var SUCCESSIVA = 0;
var PRECEDENTE = 1;
var CAMPONUMERICO = "Campo numerico";


var tables = new Array(C_ANAG, C_DATIN);
var g_arrtipi = new Array("NUM","INT","IMP","CF","DATA");
var g_arrvalori= new Array("MINVAL","MAXVAL","LUNGO");


// ritorna tutti gli elementi di una tabella
function getTableElements(pTableId)
{
	var mycell;
	var i,k,j;
	var mytable = document.getElementById(pTableId);

	var lista = new Array();
	
	if (mytable == null)
		return lista;
	

	j=0;

	for (i=0; i<mytable.rows.length; i++)
	{
		for (k=0; k<mytable.rows[i].cells.length; k++)
		{
			mycell = mytable.rows[i].cells[k];
			if (mycell.firstChild != null)
			{
				lista[j++] = mycell.childNodes[0];
			    	if (mycell.childNodes[1] != null)
					lista[j++] = mycell.childNodes[1];
				
			}
			
		}		
	
	}
	return lista;
}

// verifica la correttezza formale dei valori delle celle di una tabella
// ritorna:
// - true, se tutte le celle sono valorizzate correttamente
// - false, altrimenti, e in pCell ho la reference alla prima cella errata
function checkTable(pTableId)
{
	var lista = getTableElements(pTableId);
	
	for (i=0; i<lista.length; i++)
	{
		
			if (lista[i].type == "text" || lista[i].type == "select-one" || lista[i].type == "radio")
				if (!checkControl(lista[i]))
					return lista[i];
	}

	return null;
}

// verifica la correttezza del valore di un controllo html
function checkControl(obj)
{
	var i,v;

	if (obj.type == "radio")
	{
		v = document.getElementsByName(obj.name);
		for (i=0; i<v.length; i++)
		{
			if (v[i].checked)
				break;
		}

		if (i == v.length)
			return false;

	}
	else
	{
		if (obj.className == "Richiesto")
		{
	    		if (obj.value == "")
				return false;
		}
	}

	return true;

}


/************************************************************************************************************************
	Valida la data 'GG', 'MM', 'AAAA'
*************************************************************************************************************************/
function isDate(pg, pm, pa)
{

	var ig, im, ia;

	if (pa.length != 4 || pm.length != 2 || pg.length != 2)
		return false;

	try
	{

		ig = parseInt(pg, 10);
		if (isNaN(ig))
			return false;


		im = parseInt(pm, 10) - 1;
		if (isNaN(im))
			return false;

		ia = parseInt(pa, 10);
		if (isNaN(ia))
			return false;

	}
	catch (ex)
	{
		return false;
	}

	var myDate = new Date(ia, im, ig);
	return ((ig == myDate.getDate()) && (im == myDate.getMonth()) && (ia == myDate.getFullYear()));
}

/* verifica e-mail */
function isEMail(pmail)
{
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	return email_reg_exp.test(pmail);
}

/* controllo formale del cod. fiscale */
function isCf(p_valore)
{
  	var i;	
	var mask = "AAAAAANNANNANNNA";

	for (i=0; i<16; i++)
	{	
		switch (mask.charAt(i))
		{
			case 'A':
				if (!isAlfa(p_valore.charAt(i)))
					return false;
					break;
					
			case 'N':
				if (!isInteger(p_valore.charAt(i)))
					return false;
					break;
		}
	}
	return true;
}

/*******************************************************************
 ritorna il valore del radio button selezionato in un gruppo
********************************************************************/
function getRadioGVal(idObjs)
{
	var i;
	var v = document.getElementsByName(idObjs);
	for (i=0; i<v.length; i++)
		if (v[i].checked)
			return (v[i].value);

	return "";
}



//Show PopUp Window for Help
//standardizzato per NS e IE
function popup(str) 
{
 window.open(str,'misure','scrollbars=yes,resizable=yes,width=460,height=380,status=no,location=no,toolbar=no');
}

function popupWin(pFile, pW, pH)
{
	window.open(pFile,'misure','top=150,left=150,screenY=150,screenX=150,scrollbars=yes,resizable=yes,width=' + pW + ',height=' + pH + ',status=no,location=no,toolbar=no');
}

//Extract editing char from string
function Strip (str)
{
 var str1 ="";
 
 for (i=0; i < str.value.length; i++)
	if (str.value.substr(i,1) != ".")
		str1 = str1 + str.value.substr(i,1);
 
  str.value = str1;
}

//-------------------------------------------------------------------------------------------------------------
//Reverse string
function StrReverse (str)
{
 var str1 ="";

 for (i=str.length - 1; i >= 0; i= i-1)
	str1 = str1 + str.substr(i,1);

 return str1;	
}

//Check numeric field
function isNumeric(p_val)
{
	var nv=0, nt=0, np=0;
	for (var i=0; i<p_val.length; i++)
  	{
   		var ch = p_val.substring(i, i+1);
   		if ((ch < '0' || ch > '9') && ch != ',' && ch != '-' && ch != '.')
	 		return false;

  	}

	

	return true;
}

//Edit field with thousand separator
function FormatNumeric (Numb)
{
  
	var Result;
	var pos;  
	var Dec="";	
	var Res;
	var TempResult="";
	var LoopCount; 
	var i;  
	var sNum;
 
	if (isNumeric(Numb) == true)
		if (Numb.value.length < 4)
 	   		return Numb;

//max
	sNum = Numb.value;

	i = 0;
	while (i < sNum.length-1)
	{
		if (sNum.charAt(i) == '0' && sNum.charAt(i+1) != ',')
			i++;
		else
			break;
	}

	sNum = sNum.substring(i);


  //Determino l'eventuale numero di decimali  
  pos = sNum.search (",");

  if (pos > 0 )
    Dec = sNum.substr(pos, sNum.length - pos + 1);

  if (pos == -1)
	 Res = StrReverse(sNum);
  else	
	 Res = StrReverse(sNum.substr (0, pos));
  

  LoopCount = 0;

  while (LoopCount < Res.length)
   {	 
   TempResult = TempResult +  Res.substr (LoopCount, 3 ); 
 
   LoopCount = LoopCount + 3;
   
   if (LoopCount <= Res.length - 1)
    TempResult = TempResult + "." ;
   }       

   sNum = StrReverse(TempResult) + Dec;
   
	Numb.value = sNum; 
}


//Strip blank before and after string
function jTrim(s)
{
 var sr;
 var i;
 var j;
 var l = s.length;
 for (i=0;i<l;i++)
	if (s.charAt(i) != " ") break;
 if (i==l) return "";
 for (j=l-1;j>i;j--)
	if (s.charAt(j) != " ") break;
 return s.substring(i,j+1);
}


//Strip blank from string
function stripBlanks(s)
{
 var i;
 var c;
 var szRes="";
 
 for (i=0; i<s.length; i++)
    {
     c=s.charAt(i);
     if (c != " ") szRes+=c;
    }
 return szRes;
}


//Check numeric field into range
function checkNum1(p, min, max, campo)
{
  str = p.value;

if (str == "")
	return true;

  if (campo == "mese" || campo == "giorno")
	if (str.length < 2)
     {	  
	  alert ("Numero nel formato a due cifre");
	  p.focus();
	  return false;
	}

  for (var i = 0; i < str.length; i++) 
  {
   var ch = str.substring(i, i + 1);
   if (ch < "0" || ch > "9") 
	{
	 alert (CAMPONUMERICO);
	 p.focus();
	 return false;
	}
  }
  
  var val = parseInt(str, 10);
  if ((val < min) || (val > max)) 
  {
   alert ("Numero compreso tra "+min+" e "+max);
   p.focus();
   return false;
  }
  
  return true;
}


//Check Day and Month of date
function checkDayMonth(str, min, max, campo)
{
  
  var val = parseInt(str, 10);
  if ((val < min) || (val > max)) 
  {
   alert ("Numero compreso tra "+min+" e "+max);
   return false;
  }
  
  return true;
}

function checkDate(vDate) 
{
	if (vDate.length != 8)
		return false;
	
	ggmesi = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	anno = vDate.substr (4,4);
	mese = vDate.substr (2,2);
	giorno = vDate.substr (0,2);

	if (parseInt(mese,10) < 1 || parseInt(mese,10) > 12 ||
	    parseInt(giorno,10) < 1 || parseInt(giorno,10) > 31 ||
	    parseInt(anno,10) < 1500 || parseInt(giorno,10) > 2500) return false;
	
	numgg = ggmesi[parseInt(mese,10)-1];
	if ((mese.length)<2) 
		return false;

    if ((parseInt(mese,10)==2)&&(((parseInt(anno,10))%4 == 0)&&((parseInt(anno,10))%100 != 0)||((parseInt(anno,10))%400 == 0))) {numgg=numgg+1;}

	if (!checkDayMonth(giorno,1,numgg,'Giorno')) 
		return false;
	
	if ((giorno.length)<2) 
       giorno="0"+giorno;
  
	return true;
}


//Format date for DB
function ConvDateToDB(vDate) 
{
	return vDate.substr (6,4) + vDate.substr (3,2) + vDate.substr (0,2);
}


//Format date for field
function ConvDateFromDB(vDate) 
{

	return vDate.substr (6,2) + "/" + vDate.substr (4,2) + "/" + vDate.substr (0,4);
}




//Sostituzione delle vocali accentate con gli escape HTML
function TestoHTML(exString)
{
 var valore="";
 var nString="";
 for (i=0; i<exString.length; i++)
    {
     valore=exString.charAt(i);
     switch(valore)
       {
        case "è":
			nString+="&egrave;";
			break;
        case "à":
			nString+="&agrave;";
			break;
        case "ì":
			nString+="&igrave;";
			break;
        case "ò":
		    nString+="&ograve;";
            break;
        case "ù":
			nString+="&ugrave;";
            break;
        case "È":
            nString+="&Egrave;";
            break;
        case "À":
            nString+="&Agrave;";
            break;
        case "Ì":
            nString+="&Igrave;";
            break;
        case "Ò":
            nString+="&Ograve;";
            break;
        case "Ù":
            nString+="&Ugrave;";
            break;
        case "é":
            nString+="&eacute;";
            break;
        case "á":
            nString+="&aacute;";
            break;
        case "í":
            nString+="&iacute;";
            break;
        case "ó":
            nString+="&oacute;";
            break;
        case "ú":
            nString+="&uacute;";
            break;
        case "Á":
            nString+="&Aacute;";
            break;
        case "É":
            nString+="&Eacute;";
            break;
        case "Í":
            nString+="&Iacute;";
            break;
        case "Ó":
            nString+="&Oacute;";
            break;
        case "Ú":
            nString+="&Uacute;";
            break;
        default: 
            nString+=valore;
       } 
    }
 return(nString);	
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isLetter (c)
{
 return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}

function isDigit (c)
{
 return ((c >= "0") && (c <= "9"));
}


function isAlphabetic (str)
{
 var i;
 if (isEmpty(str)) return false;
 for (i=0; i<str.length; i++)
    if (!isLetter(str.charAt(i))) return false;
 return true;
}

function isAlphanumeric (str)
{
 var i;
 var c;
 if (isEmpty(str)) return false;
 for (i=0; i<str.length; i++)
    {
     c=str.charAt(i);
     if (!isLetter(c) && !isDigit(c)) return false;
    }
 return true;
}

function checkTarga (szTarga)
{
 var targhe = new Array("lldddll","lldddddd","lllddddd","lldddddl","lldldddd");
 var i,j;
 var str;
 var bOK;

 str=stripBlanks(szTarga).toUpperCase();
 for (i=0; i<targhe.length; i++)
    if (str.length == targhe[i].length)
      {
		for (j=0; j<str.length; j++)
		   {
		    switch (targhe[i].charAt(j))
		       {
		        case "l":
		            bOK = isLetter(str.charAt(j));
		            break;
		        case "d":
		            bOK = isDigit(str.charAt(j));
		            break;
		        default:
		            bOK = true;
		            break;
		       }
		    if (!bOK) break;
		   }
		if (bOK) return str;
      }
 return "";
}


function isInteger1(pVal)
{
	var ch;

 	for (var i=0; i<pVal.length; i++) 
  	{
   		ch = pVal.charAt(i);
   		if ((ch < '0' || ch > '9'))
   		{

	 		return false;
   		}
  	}

	return true;
}

function addDaysToDate(pData, pGG)
{
	var obj;
	var temp;
	var gg = pData.substr(6,2);
	var mm = pData.substr(4,2);
	var yy = pData.substr(0,4);

	if (!checkDate(gg+mm+yy))
		return "";


	temp = yy + "/" + mm + "/" + gg;

	obj = new Date(temp);
    	return(new Date(obj.getTime() + pGG * 24 * 60 * 60 * 1000));
}

function formatDate1(pData)
{
	var anno, mese, giorno;
	var obj;

	obj = new Date(pData);

	anno = obj.getFullYear();

	mese = obj.getMonth() + 1;
	if (parseInt(mese,10) < 10)
		mese = "0" + mese;

	giorno = obj.getDate();
	if (parseInt(giorno,10) < 10)
		giorno = "0" + giorno;

	return (""+anno + mese + giorno);

}

function formatDate(objData,psep)
{
	var anno, mese, giorno;
	

	anno = objData.getFullYear();

	mese = objData.getMonth() + 1;
	if (parseInt(mese,10) < 10)
		mese = "0" + mese;

	giorno = objData.getDate();
	if (parseInt(giorno,10) < 10)
		giorno = "0" + giorno;

	return (giorno + psep + mese + psep + anno);

}


//rimuove tutte gli oggetti option da una select tranne quelli col value indicato
function removeAllOptionsBut(pId, pValues)
{
	if (jTrim(pValues) == "")
		return false;
			
	var obj = getObj(pId);
	if (obj == null)
		return false;
	
	var nOptions = obj.options.length;
	var myval;
	i = nOptions - 1;
	while (i >= 0)
	{
		myval = jTrim(obj.options[i].value);
		if (pValues.indexOf(";" + myval + ";") == -1 )
			removeOption(pId, i);
		
		i--;
	}

	removeOptionByValue(pId, "");
	
	return true;
}

//rimuove tutte gli oggetti option da oggetto select
function removeAllOptions(pId)
{
			
	var obj = getObj(pId);
	if (obj == null)
		return false;
	
	var nOptions = obj.options.length;
	i = nOptions - 1;
	while (i >= 0)
	{		
		removeOption(pId, i);
		i--;
	}

	return true;
}


/********************************************************************
ritorna:
	- la reference al controllo html dato l'id
	- null, se non lo trova
*********************************************************************/
function getObj(pId)
{
	return window.document.getElementById(pId);
}

/********************************************************************
 trim stringa
*********************************************************************/
function trim(s)
{
	var sr,i,j;
 	var l = s.length;
 	for (i=0;i<l;i++)
		if (s.charAt(i) != " ") break;

 	if (i==l)
 		return "";

 	for (j=l-1;j>i;j--)
		if (s.charAt(j) != " ") break;

 	return s.substring(i,j+1);
}


function getValue(pId)
{
	var obj = window.document.getElementById(pId);
	if (obj != null)
		return obj.value;
		
	return "";	

}



function setValue(pId, pVal)
{
	var obj = window.document.getElementById(pId);
	if (obj != null)
		obj.value = pVal;
}


function copyValue(pTarget, pSource)
{
	var objT = getObj(pTarget);
	if (objT == null)
		return false;

	var objS = getObj(pSource);
	if (objS == null)
		return false;

	objT.value = objS.value;

	return true;
}


function setFocus(pId)
{
	var obj = getObj(pId);
	if (obj != null)
		if (obj.type != "hidden" && !obj.disabled)
			obj.focus();
}


function setDisabled(pId, pDisabled)
{
	if (pDisabled != true && pDisabled != false)
		return false;

	var obj = getObj(pId);
	if (obj != null)
		obj.disabled = pDisabled;
}


function setClass(pId, pClass)
{
	var obj = getObj(pId);
	if (obj != null)
		obj.className = pClass;
}


function isInteger(p_mystr)
{

	var i=0;
	var mymask = "0123456789-";

	for (i=0;i < p_mystr.length;i++)
	{
		if (mymask.indexOf(p_mystr.charAt(i)) == -1)
			return false;
	}

	return true;

}

function isAlfa(p_mystr)
{

	var i=0;
	var mymask = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	for (i=0; i<p_mystr.length; i++)
		if (mymask.indexOf(p_mystr.charAt(i).toUpperCase()) == -1)
			return false

	return true;

}



function setDisplay(pId, pFlag)
{
	var obj = getObj(pId);
	if (obj == null)
		return;

	if (pFlag)
		obj.style.display = 'block';
	else
		obj.style.display = 'none';

}



function getOrder(mytable)
{
	for (i=0; i<tables.length; i++)
		if (tables[i] == mytable)
			return i;
	return -1;
}

function getNextPage(mytable)
{
	var i = getOrder(mytable);
	if (i >= tables.length-1)
		return null;
	else
		return tables[i+1];
}

function getPrevPage(mytable)
{
	var i = getOrder(mytable);
	if (i <= 0)
		return null;
	else
		return tables[i-1];
}

function showTable(mytable)
{
	var i, obj;
	for (i=0; i<tables.length; i++)
	{
		obj = getObj(tables[i]);
		if (tables[i] == mytable)
			obj.style.display = "inline";
		else
			obj.style.display = "none";
	}
}
	
function showMessage(obj)
{
	var temp = "";
	if (obj == null)
		return;
	
	if (obj.title != "undefined")
		temp = obj.title;
			
	alert("Campo '" + temp + "' Obbligatorio");
		
	if (!obj.disabled && obj.type != "radio")
		obj.focus();
}


function  CampoNumerico()
{
	var valore = window.event.keyCode;
	if(valore < 48 || valore > 57)
	   window.event.keyCode = 0;
		
}


// ritorna l'id del primo controllo obbligatorio non valorizzato
// sFields = "id1;id2;....;idn"
function checkRequired(sFields)
{
	var i;
	var obj;
	var objs = sFields.split(";");

	for (i=0; i<objs.length; i++)
	{
		obj = getObj(objs[i]);
		if (obj != null)
		{
			if (obj.style.display != 'none' && trim(getValue(objs[i])) == "")
				return objs[i];
		}
	}
	return "";

}


/****************************************************************************************
	Visualizza una section <div>
*****************************************************************************************/
function showSection(idsezione)
{
	var obj = getObj(idsezione);
	if (obj != null)
		obj.style.display = "inline";
	
	return true;
}

/****************************************************************************************
	Nasconde una section <div>
*****************************************************************************************/
function hideSection(idsezione)
{
	var obj = getObj(idsezione);
	if (obj != null)
		obj.style.display = "none";

	return true;
}

/****************************************************************************************
	Visualizza una section <div>
*****************************************************************************************/
function showObject(id)
{
	var obj = getObj(id);
	if (obj != null)
		obj.style.visibility = "visible";
	
	return true;
}

/****************************************************************************************
	Nasconde una section <div>
	mantiene l'ingombro
*****************************************************************************************/
function hideObject(id)
{
	var obj = getObj(id);
	if (obj != null)
		obj.style.visibility = "hidden";

	return true;
}


/****************************************************************************************
	Visualizza una section <div>
*****************************************************************************************/
function isHidden(id)
{
	var obj = getObj(id);
	if (obj != null)
		return (obj.style.visibility == "hidden" || obj.style.display == "none");
}

/****************************************************************************************
	Seleziona nell'oggetto select con id <p_id> l'option col value <p_value>
*****************************************************************************************/
function selectOption(p_id, p_value)
{
	var i;
	var objsel = getObj(p_id);
	if (objsel == null)
		return false;

	for (i=0; i<objsel.options.length;i++)
	{
		if (objsel.options.item(i).value == p_value)
		{
			objsel.selectedIndex = i;
			return true;
		}
	}
	return false;
}

function checkRadio(p_id, p_value)
{
	var i;
	var obj = getObj(p_id);
	if (obj == null)
		return false;


	coll = document.getElementsByName(p_id);

	for (i=0; i<coll.length; i++)
	{
		if (coll[i].value == p_value)
		{
			coll[i].checked = true;
			return true;
		}
	}
	return false;
}


/* carica nella combo box l'elenco delle stampanti configurate */
function loadPrinters()
{

	var WshNetwork = new ActiveXObject("WScript.Network");
	var oPrinters = WshNetwork.EnumPrinterConnections();
	var cboOtherCurrency = document.getElementById("cboPrinters");
	
	
	if(oPrinters.Count() == 0)
	{
		alert('Errore: caricamento stampanti');
		return false;
	}

	for(i=0; i<oPrinters.Count(); i+=2)
	{		
		oOption = document.createElement("OPTION");
		cboOtherCurrency.options.add(oOption, cboOtherCurrency.options.length);
		oOption.innerText = oPrinters.Item(i+1);
		oOption.value = oPrinters.Item(i+1);
	}
	return true;
}


function getSelectedOptionText(pId)
{
			
	var obj = getObj(pId);
	if (obj == null)
		return false;
	
	for (i=0; i<obj.options.length; i++)
		if (obj.options[i].selected)		
			return obj.options[i].text;
	
	return "";
}

//aggiunge una option ad una select
function addOption(pSel, pText, pVal)
{
	var obj = getObj(pSel);
	if (obj == null)
		return false;

	var opt = new Option(pText, pVal);
	obj.options[obj.options.length] = opt;

	return true;
}


function setOptionText(pId, pValue, pText)
{
	var obj = getObj(pId);
	if (obj == null)
		return false;

	for (var i=0; i<obj.options.length; i++)
	{	
		if (jTrim(obj.options[i].value) == pValue)
		{			
			obj.options[i].text = pText;
			break;
		}
	}

	return true;
}

//rimuove una option da una select
function removeOption(pId, pIndex)
{

	var obj = getObj(pId);
	if (obj == null)
		return false;

	obj.remove(pIndex);
	
	return true;

}


function disableText(pId)
{
	var coll = document.getElementsByTagName("text");
}


function removeOptionByValue(pId, pValue)
{
	var obj;
	var nOptions;
	
	obj = getObj(pId);
	if (obj == null)
		return false;
		
	nOptions = obj.options.length;
	i=nOptions-1;
	while (i>=0)
	{
		if (jTrim(obj.options[i].value) == pValue)
		{
			removeOption(pId, i);
			break;
		}
		
		i--;

	}

	return true;
}


//inserisce una option in una select
function insertOption(pObj, pText, pVal, pIndex)
{	
	
	var obj = getObj(pObj);
	if (obj == null)
		return false;

	if (pIndex < 0)
		return false;

	var nOptions = obj.options.length;
	if (pIndex > nOptions)
		return false;

	var opt = new Option("", "");
	obj.options[nOptions] = opt;

	for (var i=nOptions; i>pIndex; i--)
	{
		obj.options[i].text = obj.options[i-1].text;
		obj.options[i].value = obj.options[i-1].value;
	}
	obj.options[pIndex].text = pText;
	obj.options[pIndex].value = pVal;
}

function isBisestile(anno)
{
	if (anno%4 == 0 && anno%100 != 0 || anno%400 == 0)
		return true;
	else
		return false;

}

/*****************************************************************************
Gestione del menu
******************************************************************************/
function fClose()
{
	window.parent.close();
}


/*****************************************************************************
	Pagina di ricerca
******************************************************************************/
function fr_onchange1(obj)
{
	
	if (obj.value == "P")
	{
		setDisplay("TR_C_PIVA",false);
		setDisplay("TR_C_RAGSOC",false);
		setDisplay("TRS_C_PIVA",false);
		setDisplay("TRS_C_RAGSOC",false);

		setDisplay("TR_C_COGNOME",true);
		setDisplay("TR_C_NOME",true);
		setDisplay("TR_C_CF",true);
		setDisplay("TRS_C_COGNOME",true);
		setDisplay("TRS_C_NOME",true);
		setDisplay("TRS_C_CF",true);			
	}		
	else
	{
		setDisplay("TR_C_PIVA",true);
		setDisplay("TR_C_RAGSOC",true);
		setDisplay("TRS_C_PIVA",true);
		setDisplay("TRS_C_RAGSOC",true);

		setDisplay("TR_C_COGNOME",false);
		setDisplay("TR_C_NOME",false);
		setDisplay("TR_C_CF",false);
		setDisplay("TRS_C_COGNOME",false);
		setDisplay("TRS_C_NOME",false);
		setDisplay("TRS_C_CF",false);
	}

}

/******************************************************************************
	Setta alla select p_targetid l'indice selezionato nella select p_sourceid
******************************************************************************/
function setSelectMySelection(p_sourceid, p_targetid)
{	
	var obj1 = getObj(p_sourceid);
	if (obj1 == null)
		return false;
		
	var obj2 = getObj(p_targetid);

	if (obj2 != null)
		obj2.selectedIndex = obj1.selectedIndex;
}


/*****************************************************************************
Posiziona il cursore sul primo campo editabile della form
*****************************************************************************/
function setFocusFirst()
{
	var myForm = document.forms[0];
	if (myForm == null)
		return false;
	
	for (var i=0; i<myForm.length; i++)
	{
		if (!myForm[i].disabled)
		{
			switch(myForm[i].type)
			{
				case "text":
				case "select-one":
				{
					myForm[i].focus();
					return true;
				}
			}
		}
		else
		{
			if (myForm[i].type == "text" && myForm[i].value == "")
				return true;	
		}
	}
}

/************************************************************************
	Verifica il contenuto di un campo
	p_type:	'N': numerico intero
			'F': numerico reale
			'A':alfabetico
			'F': alfanumerico
			'X': qualsiasi
	ritorna:
	0:		ok
	-1:		campo obbligatorio non valorizzato
	-2:		campo di tipo diverso
	-3:		superata la lunghezza max
*************************************************************************/
function checkField(p_myid, p_required, p_type, p_maxlen)
{
	var obj = getObj(p_myid);
	if (obj == null)
		return 0;

	var myval = getValue(p_myid);

//obbligatorio ?
	if (p_required == "S" && myval == "")
		return -1;

// tipo : da finire
	switch(p_type)
	{
		case 'N':
		{
			if (!isInteger(myval))
				return -2;
		}
	}

// lunghezza massima
	if (myval.length > p_maxlen)
		return -3;

	return 0;
}

function getStrLeft(p_str, p_sep)
{
	var pos = p_str.indexOf(p_sep);
	
	if (pos > -1)
		return p_str.substr(0, pos);
	else
		return p_str;
}

function getStrRight(p_str, p_sep)
{
	var pos = p_str.indexOf(p_sep);
	
	if (pos > -1)
		return p_str.substr(pos+1);
	else
		return p_str;
}

/********************************************************************************************
 Ritorna il valore di una data nel formato AAAAMMGG
*********************************************************************************************/
function getDateValue(p_Id)
{
	var coll = document.getElementsByName(p_Id);
	if (coll.length == 0)
		return "";
	
	return coll[2].value + coll[1].value + coll[0].value;
}


function setDateValue(p_Id, p_value)
{
	var coll = document.getElementsByName(p_Id);
	if (coll.length == 0)
		return "";

	if (p_value.length != 8)
		return false;

	var mygg = p_value.substr(6,2);
	var mymm = p_value.substr(4,2);
	var myaa = p_value.substr(0,4);
	
	if (!checkDate(mygg+mymm+myaa))
		return "";

	coll[0].value = mygg;
	coll[1].value = mymm;
	coll[2].value = myaa;
}


//mod060406
var	gTouristPass = ';TPIA;TPIB;TPIG;TPIL;';

function setHeader(p_prod)
{
	if (gTouristPass.indexOf(';' + p_prod + ';') < 0)
	{
		window.parent.frames["LogoTop"].hideLinks('HNVE');
		window.parent.frames["LogoTop"].hideLinks('HMNU');
	}
}

function setUser(pUser)
{
	window.parent.frames["LogoTop"].setUser(pUser);

}

function initHeader()
{
	window.parent.frames["LogoTop"].hideLinks('HNVE');
	window.parent.frames["LogoTop"].hideLinks('HMNU');
}



// decodifica delle azioni associate alle info
function showInfo(p_info)
{
	var vinfo = p_info.split(";");
	var pos;
	var resources;
	
	for (var i=0; i<vinfo.length; i++)
	{
		pos = vinfo[i].indexOf(':');
		if (pos <= 0)
			return;

		info = vinfo[i].substr(0,pos);			// tipo di info
		resources = vinfo[i].substr(pos+1);		// file da utilizzare

		vitems = resources.split(",");

		for (j=0; j<vitems.length; j++)
			infoAction(info, vitems[j]);

	}
	
}

// esecuzione dell'azione associata alle info
function infoAction(p_info, p_resource)
{
	switch(p_info)
	{
		case "gn":
			window.parent.frames["LogoTop"].setLink('HNVE', 'target', "info/" + p_resource);
			window.parent.frames["LogoTop"].showLinks('HNVE');
			break;
		case "mu":
			window.parent.frames["LogoTop"].setLink('HMNU', 'target', "info/" + p_resource);
			window.parent.frames["LogoTop"].showLinks('HMNU');
			break;
		case "":
			window.parent.frames["LogoTop"].hideLinks('HNVE');
			window.parent.frames["LogoTop"].hideLinks('HMNU');
			break;
	}	
}

function openModalWindow(pBrowser,spage)
{
	if (pBrowser == 'IE')
		var ret = window.showModalDialog(spage,"conferma","dialogWidth:350px;dialogHeight:150px;status:no;help:no");
	
	return ret;
}

// ritorna il codice del tipo di browser
function getBrowser()
{
	var myret = "";
	switch(navigator.appName.toUpperCase())
	{
		case "NETSCAPE":
			myret = "NS";
			break;

		case "MICROSOFT INTERNET EXPLORER":
			myret = "IE";
			break;
		
	}
	return myret;
}

/************************************************************************
 Ritorna la differenza tra due date in giorni
*************************************************************************/
function jDateDiff(szDateStart,szDateEnd)
{
	var myYear, myMonth, myDay;
	var dateStart, dateEnd;

// data inizio
	myYear = parseInt(szDateStart.substr(0,4),10);
	myMonth = parseInt(szDateStart.substr(4,2),10)-1;
	myDay = parseInt(szDateStart.substr(6,2),10);

	dateStart = new Date(myYear, myMonth, myDay);

// data fine
	myYear = parseInt(szDateEnd.substr(0,4),10);
	myMonth = parseInt(szDateEnd.substr(4,2),10)-1;
	myDay = parseInt(szDateEnd.substr(6,2),10);

	dateEnd = new Date(myYear, myMonth, myDay);

//differenza in giorni
	return((dateEnd.getTime()-dateStart.getTime())/(1000*60*60*24));
}


/************************************************************************
 Sposta il focus sul successivo campo della data
 html:	<input .... onkeyup="f_onkeyupDate(this)" ...>
*************************************************************************/
function f_onkeyupDate(obj)
{
	var myval = obj.value;
	if (myval.length < obj.maxLength)
		return;

	var coll = document.getElementsByName(obj.id);
	var mylen = coll.length;

	for(var i=0;i<mylen;i++)
	{
		if(coll[i] == obj)
		{
			if (i+1<mylen)
				coll[i+1].focus();
			return;
		}
	}
}

function f_onkeyupDateNamed(obj, idnext)
{
	var myval = obj.value;
	if (myval.length == obj.maxLength)
		setFocus(idnext);

}

function nop()
{
	var i=0;
	i++;
}

function obj_setFocus(p_obj)
{
	if (p_obj != null)
		if (p_obj.type != "hidden" && !p_obj.disabled)
			p_obj.focus();
}


function radio_uncheck(p_id)
{
	var coll = document.getElementsByName(p_id);
	if (coll.length == 0)
		return;
	for (var i=0; i<coll.length; i++)
		coll[i].checked = false;
}

/************************************************************************************
Apre una popup con dimensioni fisse o in funzione dello schermo
*************************************************************************************/
function f_winopen(p_url, p_des, p_left, p_top, p_w, p_h, p_scrollbars)
{
	var mywidth, myheight;
	var mysb = "no";
	if (p_scrollbars)
		mysb = "yes";

	mywidth = parseFloat(p_w);
	if (mywidth <= 1)
		mywidth = parseInt(screen.width * mywidth, 10);

	myheight = parseFloat(p_h, 10);
	if (myheight <= 1)
		myheight = parseInt(myheight * myheight, 10);
	
	window.open(p_url, p_des, "height=" + myheight + ", width=" + mywidth + ", left=" + p_left + ", top=" + p_top + ", statusbar=no, toolbar=no, location=no, menubar=no, scrollbars=" + mysb + ", resizable=no, fullscreen=no");
}

function setCursor(p_style)
{
	switch(p_style)
	{
		case 0:
		{
			document.body.style.cursor = "default";
			break;
		}

		case 1:
		{
			document.body.style.cursor = "hourglass";
			break;
		}
	}
}

// ...
function dataTypeMatch(p_value, p_dataType)
{
	var myret = false;
	switch(p_dataType)
	{
		case "A":
			myret = isAlphabetic(p_value);
			break;
		
		case "N":
			myret = isNumeric(p_value);
			break;

		case "I":
			myret = isInteger(p_value);
			break;
		
		case "V":
			myret = isCurrency(p_value);
			break;

		case "C":
			myret = isCf(p_value);
			break;

		case "X":
			myret = true;
			break;
		
		default:
			myret = false;
			break;
			
	}
	return myret;
}

// da fare...
function isCurrency(p_value)
{
	return true;
}

/************************************************************************************************************************
	Valida la data 'GG', 'MM', 'AAAA'
*************************************************************************************************************************/
function isDate2(pm, pa)
{
	var im, ia, ig=10;

	if (pa.length != 4 || pm.length != 2)
		return false;

	try
	{
		im = parseInt(pm, 10) - 1;
		if (isNaN(im))
			return false;

		ia = parseInt(pa, 10);
		if (isNaN(ia))
			return false;
	}
	catch (ex)
	{
		return false;
	}

	var myDate = new Date(ia, im, ig);
	return ((ig == myDate.getDate()) && (im == myDate.getMonth()) && (ia == myDate.getFullYear()));
}


function setDataScadenza(p_idData1,p_idData2)
{
	var coll = document.getElementsByName(p_idData1); //data di scadenza 575
	var coll1 = document.getElementsByName(p_idData2); //data di effetto 577

	if (coll.length == 0 || coll1.length == 0)
		return false;

	var anno1 = parseInt(coll1[2].value,10);
	
	if (isBisestile(anno1) && coll1[0].value == "29" && coll1[1].value == "02")
		coll[0].value = "28";
	else
		coll[0].value = coll1[0].value;
	coll[2].value = anno1 + 1;	
	coll[1].value = coll1[1].value;

	
	coll[0].disabled = true;
	coll[1].disabled = true;
	coll[2].disabled = true;
	return true;
}


function isBisestile(anno)
{
	if (anno%4 == 0 && anno%100 != 0 || anno%400 == 0)
		return true;
	else
		return false;

}

//-------------------------------------------------------------------------------------------------------------
//Check numeric field into range
function checkNum(p, min, max, campo)
{
 	str = p.value;

	if (str == "")
		return true;

  	if (campo == "mese" || campo == "giorno")
  	{
		if (str.length < 2)
     	{	  
	  		alert ("Numero nel formato a due cifre");
	  		p.focus();
	  	return false;
		}
	}
	
  	for (var i=0; i<str.length; i++) 
  	{
   		var ch = str.substring(i, i + 1);
   		if (ch < "0" || ch > "9") 
		{
	 		alert (CAMPONUMERICO);
	 		p.focus();
	 		return false;
		}
  	}
  
  	var val = parseInt(str, 10);
  	if (val < min || val > max) 
  	{
   		alert ("Numero compreso tra "+min+" e "+max);
   		p.focus();
   		return false;
  	}

 	return true;
}

//Accetta solamente i valori da 0 a 9 (es. onkeypress)
function chkInput(p_type)
{
	var mystr;
	switch(p_type)
	{
		case 'I':
			mystr = "0123456789";
			break;
	}
	var num = String.fromCharCode(event.keyCode)
	if (mystr.indexOf(num) != -1)
		return true;
    event.returnValue = false;
}

//mod030922
function replace1(sSource, sCharIn, sCharOut)
{
	var sOut = "";

	for(var i=0; i<sSource.length; i++)
	{

		if (sSource.charAt(i) == sCharIn)
			sOut = sOut + sCharOut;
		else
			sOut = sOut + sSource.charAt(i);

	}
	
	return sOut;
}

function setTblBord(pname)
{

	window.parent.frames["Header"].setTblBord(pname);

}

// va al pezzo di data indicato
function f_nextDateToc(p_obj, p_len, p_inext)
{
	if (p_obj.value.length < p_len || p_inext < 0)
		return false;

	var coll = document.getElementsByName(p_obj.name);
	if (p_inext <= coll.length-1)
	{
		coll[p_inext].focus();
		return true;
	}
	return false;
}

function f_onkeyupD(p_obj, p_len, p_inext)
{
	var valore = window.event.keyCode;
	if(valore < 48 || valore > 57)
		return false;
	return f_nextDateToc(p_obj, p_len, p_inext);
}

