//-------------------- AddListOptions -----------------------------------------
function AddListOptions(strControl, strOptions)
{
	var arrOptions = strOptions.split("|");
	var i;
	for(i=0 ; i< arrOptions.length; i++)
	{ 
		AddListOption(strControl, arrOptions[i].split("#")[0], arrOptions[i].split("#")[1]); 
	}
}
//-------------------- CounterNext --------------------------------------------
function CounterNext(numColumn)
{
	// Ritorna il numero di righe della tabella
	// Attiva il campo numero della riga precedente
	// Prende il valore del campo
	// Disattiva il cammpo numero della riga precedente
	var numNumero = 0;
	var nRows = document.all.oTable.rows.length;
	if ( nRows > 2 )
	{
		var objTemp = document.all.oTable.rows(nRows-2).cells(numColumn).getElementsByTagName("INPUT")[0];
		var bDisabled = objTemp.disabled;
		if ( bDisabled )
			document.all.oTable.rows(nRows-2).cells(numColumn).getElementsByTagName("INPUT")[0].disabled = false;
		numNumero = parseInt(document.all.oTable.rows(nRows-2).cells(numColumn).getElementsByTagName("INPUT")[0].value,10);
		if ( bDisabled )
			document.all.oTable.rows(nRows-2).cells(numColumn).getElementsByTagName("INPUT")[0].disabled = true;
	}
	return numNumero;
}
//------------------------ GetSelectedOptions ---------------------------------
function GetSelectedOptions(strControl)
{
	var strOptions = "";
	var arrOptions = document.getElementById(strControl).options; 

	for ( var i = 0; i < arrOptions.length; i++ )
	{
		if ( arrOptions[i].selected == true )
		{ 
			if ( strOptions != "" )
				strOptions += "|";
			strOptions += "" + arrOptions[i].text + "#" + arrOptions[i].value + "";	
		}
	} 
	return strOptions;
}
//-------------------- SelectListOption ---------------------------------------
function SelectOption(strControl, strValue)
{
	var arrOptions = document.all(strControl).options; 
	for ( var i = 0; i < arrOptions.length; i++ )
	{
		if ( arrOptions(i).value == strValue )
		{
			arrOptions(i).selected = true;
			break;
		}
	}
}
//-------------------- F2_keypressed------------------------------------------
function F2_keypressed(numQueryId, numId, strElementClean, strElementInp, 
					   strElementOut, strElementFoc, strTabella)
{
	// Skip code if control is disabled or read only
	if (window.event.srcElement.disabled == true ||
        window.event.srcElement.readOnly == true )
		return;
	
	// Skip code if user press TAB key
	if ( window.event.keyCode == 9 )	// TAB
		return;
	
	var i, j, strElemento, strValue;
	var strStringa = strId = "";
	var bSelected = false;
	var arrElementClean = strElementClean.split (",");
	var arrElementInp = strElementInp.split (",");
	var arrElementOut = strElementOut.split (",");
	if (numId != "") strId =  "_" + numId
	
	// pulisco tutti i campi
	if(isPrintableKeyCode(window.event.keyCode)||
	   window.event.keyCode == 0x2E	           ||   // delete key
	   window.event.keyCode == 0x08)				// backspace key   
	{
		if (strElementClean != "" )
		{
			for (i = 0; i <= arrElementClean.length - 1; i++)
				if (document.all(arrElementClean[i].split("*")[0] + strId).name.substr(0,3) == "cmb")
					document.all(arrElementClean[i].split("*")[0] + strId).options.value = 0
				else 
				{
					strValue = "";
					if ( arrElementClean[i].split("*").length > 1 )
						strValue = arrElementClean[i].split("*")[1];
					document.all(arrElementClean[i].split("*")[0] + strId).value = strValue;
				}
		}
	}
	if ( window.event.keyCode == 113 )	// F2
	{// Se è stata specificata una tabella,è il primo parametro
		if (strTabella != "") 
			strStringa = strTabella + "|"
		for (i = 0; i <= arrElementInp.length - 1 ; i++)
		{
			if (arrElementInp[i].charAt(0) == "$")
			{
				strElemento = arrElementInp[i].substring(1,200);
			}
			else
			{		
				elimchar(document.all(arrElementInp[i] + strId));
				strElemento = document.all(arrElementInp[i] + strId).value;
			}
			if (strElemento == "" || strElemento == " " || strElemento == "0")
				strStringa += "%"+ "|"
			else strStringa +=strElemento + "|";
		}		
		strStringa = escape(strStringa);
		var retVal = window.showModalDialog("Container.asp?" + 
			"strLocation=RicercaRisultati.asp" +
			"&strForm=frmRicercaRisultati" +
			"&strHeader=Header.asp" +
			"&numQueryId=" + numQueryId +
			"&strParam=" + strStringa + 
			"&numPage=0&nMultipla=0&nModale=1" +
			"&strTitolo=Ricerca" +
			"&strSottoTitolo=Risultati");
		
		// compilo i campi da restituire, scateno l'evento onblur 
		// e metto il focus sul campo prescelto
		if ( retVal )
		{
			var aParam = retVal.split ("|")
			for (i = 0; i <= arrElementOut.length - 1; i++)
			{
				if (arrElementOut[i]!= "")
				{
					if (document.all(arrElementOut[i] + strId).name.substr(0,3) == "cmb")
						document.all(arrElementOut[i] + strId).options.value = aParam[i];
					else document.all(arrElementOut[i] + strId).value = aParam[i];
				}
				try 
				{
					document.all(arrElementOut[i] + strId).onblur();
				}
				catch (excp)
				{
					;
				}
			}
			if ( strElementFoc != "" )
				document.all(strElementFoc + strId).focus();
			bSelected = true;
		}
	}
	return bSelected;
}
//----------------------------------< F7_keypressed >--------------------------
function F7_keypressed(oElement)
{
	if ( window.event.keyCode == 118 )	// F7
	{
		RimuoviApici(oElement);

		var oText = new Object();
		oText.String = oElement.value;

		var retVal =
			window.showModalDialog("TextArea.asp", oText);

		if ( typeof(retVal) != "undefined" )
			oElement.value = retVal;
	}
}
//-------------------- FormatDate ---------------------------------------------
function FormatDate(sDateOriginal, sFtmOriginal, sSepOriginal, sFmtTarget, sSepTarget)
{
	
	if (sDateOriginal != "")
	{
		var aDateOriginal = sDateOriginal.split(sSepOriginal);
		var aDateTarget = sDateOriginal.split(sSepOriginal);
		var i, nFmtTargetPartPos;
		var sFmtTargetPart, sDatePart;
		
		for (i = 0; i < 3; i++)
		{
			sFmtTargetPart = sFmtTarget.charAt(i);
			nFmtTargetPartPos = sFtmOriginal.indexOf(sFmtTargetPart);
			nFmtTargetPartPos = nFmtTargetPartPos;
			sDatePart = aDateOriginal[nFmtTargetPartPos];
			if (sDatePart.length < 2 )
				sDatePart = "0" + sDatePart;
			aDateTarget[i] = sDatePart;
		}
		return aDateTarget.join(sSepTarget);
	}
}
//------------------------- FormatMoneyAlt ------------------------------------
// Da numero a stringa
function FormatMoneyAlt( numValue, numDecimal )
{		
	// Arrotonda solo se decimali specificati
	if ( numDecimal > 0 )
		numValue = RoundDouble( numValue, numDecimal );

	// Converte il numero in stringa
	var strValueLoc = "" + numValue + "";
	
	// Sostituisce il punto con la virgola 
	var re = /[.]/gi;
	strValueLoc = strValueLoc.replace(re, ",");

	var aValue = strValueLoc.split(",");
	strValueLoc = Separatori(aValue[0]);
	if ( aValue.length == 2 )
	{
		strValueLoc += ",";
		if ( aValue[1].length > 0 )
			strValueLoc += aValue[1];
		else
			strValueLoc += "0";
	}
	return strValueLoc;
}
//------------------------- GetMoneyAlt ---------------------------------------
// Da stringa a numero
function GetMoneyAlt( strValueLoc )
{
	// Rimuove eventuali separatori di migliaia
	var re = /[.]/gi;
	strValueLoc = strValueLoc.replace(re, "");

	// Sotituisce la virgola con il punto
	re = /[,]/gi;
	strValueLoc = strValueLoc.replace(re, ".");

	return parseFloat(strValueLoc);
}
//------------------------- GetMoneyDef ---------------------------------------
// Da stringa a numero
function GetMoneyDef( strValueLoc )
{
	// Rimuove eventuali separatori di migliaia
	var re = /[.]/gi;
	strValueLoc = strValueLoc.replace(re, "");

	return parseInt(strValueLoc,10);
}
//------------------- CONTROLLI SU STRINGHE -------------------------------------
function isPrintableKeyCode(chr)
{/*controlla quali sono i caratteri impostabili dall'utente
   in un campo */ 
	return !(chr <= 0x2F || chr == 0x90 || (chr >= 0x70 && chr <= 0x7F) );
}
//------------------- RemoveOptions -------------------------------------------
function RemoveOptions(strControl)
{
	/* pulisco il combo figlio */ 
    var coll1 = document.all(strControl).options; 
    while (coll1.length > 0)
		coll1.remove(0); 
}
//-------------------- ARROTONDA A n DECIMALI ------------------------
function RoundDouble( numValue, numDecimal )
{
	var numPower = Math.pow( 10, numDecimal );
	
	if( numValue >= 0 )
		return Math.floor( numValue * numPower + 0.5 ) / numPower;
	else
		return Math.ceil( numValue * numPower - 0.5 ) / numPower;
}
//------------------- CONTROLLI SU NUMERI ------------------------------------
function Separatori(numero)
{//mette i separatori ai dati numerici
   	numero = "" + numero
   	if (numero.indexOf(".") != -1)
		return numero
	else
	{
		var i, j;
		var strNumber = "";
		for (i=numero.length - 1, j=1; i>=0; i--, j++)
		{
		    if(j==4) 
		    {
				strNumber = "." + strNumber;
				j = 1;
			}
		    strNumber = numero.charAt(i) + strNumber;
		}
		return  strNumber
	}
}
//----------------------- TogliSeparatori -------------------------------------
function TogliSeparatori(strNumero)
{	// Rimuove separatori di migliaia
	var re = /[.]/gi;
	return  strNumero.replace(re, "");
}
//---------------------- Trim -------------------------------------------------
function Trim(strVal)
{
	var strReturn;
	var iStart, iEnd;
	iEnd = strVal.length - 1
	for(iStart = 0; iStart <= iEnd && strVal.charAt(iStart) == " "; iStart++) ;
	for(; iStart < iEnd && strVal.charAt(iEnd) == " "; iEnd--) ;
	return (iStart <= iEnd) ? strVal.substring(iStart, iEnd+1) : "";
}
//--------------------- TrimFiled ----------------------------------------------
function TrimField(fld)
{
	fld.value = Trim(fld.value);
}
//----------------------------------< RimuoviApici >---------------------------
function RimuoviApici (oElement)
{

	var strValue = oElement.value;
	if ( strValue != "" )
	{
    	// Sostituisce l'eventuale apice singolo(')  con  l'ALT 96 (`)
		var re = /[']/gi;
		strValue = strValue.replace(re, "`");
	    // Sostituisce l'eventuale apice doppio(")  con  l'ALT 96 (`)
		var re = /["]/gi;
		strValue = strValue.replace(re, "`");
	}	
	oElement.value = strValue
}
//----------------------------------< RimuoviLettereAccentate >----------------
function RimuoviLettereAccentate(oElement)
{
	var strValue = oElement.value;
	if ( strValue != "" )
	{
    	// Sostituisce l'eventuale à (a accentata) con a + ALT 96 (`)
		var re = /[à]/gi;
		strValue = strValue.replace(re, "a`");

    	// Sostituisce l'eventuale è (e accentata) con e + ALT 96 (`)
		var re = /[è]/gi;
		strValue = strValue.replace(re, "e`");

    	// Sostituisce l'eventuale é (e accentata) con e + ALT 96 (`)
		var re = /[é]/gi;
		strValue = strValue.replace(re, "e`");

    	// Sostituisce l'eventuale ì (i accentata) con i + ALT 96 (`)
		var re = /[ì]/gi;
		strValue = strValue.replace(re, "i`");

    	// Sostituisce l'eventuale ò (o accentata) con o + ALT 96 (`)
		var re = /[ò]/gi;
		strValue = strValue.replace(re, "o`");

    	// Sostituisce l'eventuale ù (u accentata) con u + ALT 96 (`)
		var re = /[ù]/gi;
		strValue = strValue.replace(re, "u`");
	}
	oElement.value = strValue;
}
//----------------------------------< CellsBackgroundSet >---------------------
function CellsBackgroundSet(strTable, numCellWithColor, strCellsToSet)
{
	var i, j, k;
	var strColor;
	var arrRows, arrCells;
	var arrCellsToSet = strCellsToSet.split(",");

	arrRows = document.all(strTable).rows;
	for ( i = 1; i <= arrRows.length - 1; i++)
	{
		arrCells = arrRows(i).cells;
		strColor = arrCells(numCellWithColor).childNodes(0).value;
		if ( strCellsToSet == "" )	// All cells
		{
			for ( j = 0; j <= arrCells.length - 1; j++ )
			{
				arrCells(j).style.background = strColor;
			}
		}
		else						// Specific cells
		{
			for ( j = 0; j <= arrCellsToSet.length - 1; j++ )
			{			
				k = parseInt(arrCellsToSet[j],10);
				arrCells(k).style.background = strColor;				
			}
		}
	}
}
//-------------------- DateLesserThan -----------------------------------------
function DateLesserThan(strDate_1, strDate_2)
{
	var oDate_1 = new Date(strDate_1.split("/")[2],
						   strDate_1.split("/")[1],
						   strDate_1.split("/")[0])
	var oDate_2 = new Date(strDate_2.split("/")[2],
						   strDate_2.split("/")[1],
						   strDate_2.split("/")[0])
	return oDate_1.valueOf() < oDate_2.valueOf();
}