var timerID = null;
var timerRunning = false;
function stopclock (){
        if(timerRunning)
                clearTimeout(timerID);
        timerRunning = false;
}

function showtime () {

        var now = new Date();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds()
        //var timeValue = "" + ((hours >12) ? hours -12 :hours)
        var timeValue = "" + hours;
        timeValue += ((minutes < 10) ? ":0" : ":") + minutes
        timeValue += ((seconds < 10) ? ":0" : ":") + seconds
        //timeValue += (hours >= 12) ? " p.m." : " a.m."

		timeValue = "<font face=\"Verdana\" color=\"#990000\">" + getGiorno() + "<BR/>" + timeValue + "</font>";

        document.all.item("TDClock").innerHTML = timeValue;
        // window.status = timeValue;
        timerID = setTimeout("showtime()",1000);
        timerRunning = true;
}

function startclock () {
        // Make sure the clock is stopped
        stopclock();
        showtime();
}
function getGiorno()
{
	var now = new Date();
    var gg=now.getDate();
	var mm=now.getMonth();
	var yy=now.getYear();
	var ng=now.getDay();
	var gSett=["Dom.","Lun.","Mar.","Mer.","Gio.","Ven.","Sab."]
	//return gSett[ng] + "&nbsp;" + getDataOggi();    
	return "&nbsp;" + getDataOggi();    
}

function getDataOggi()
{
	var now = new Date();
    var gg=now.getDate();
	var mm=now.getMonth();
	var yy=now.getYear();
	var ng=now.getDay();
	if (gg<10)
	{
		gg = "0"+gg;
	}
	mm = mm + 1
	if (mm<10)
	{
		mm = "0"+mm;
	}
	return gg +"/" + mm +"/" + yy;     
}

function openW(tit , w, h, dest) {
	//versione pagina centrata
	var param;
	var x = screen.width/2 - w/2;
	var y = screen.height/2 - h/2 -20;
	param = "width="+w+",height="+h+",top="+y+",left="+x+",scrollbars=no,toolbar=no,location=no,directories=no,menubar=no,status=yes,resizable=no,fullscreen=no";
	var defpage = window.open (dest, tit, param);
}
function openApp(tit , w, h, dest) {
	//versione pagina centrata
	var param;
	var x = screen.width/2 - w/2;
	var y = screen.height/2 - h/2 -20;
	param = "width="+w+",height="+h+",top="+y+",left="+x+",scrollbars=yes,toolbar=no,location=no,directories=no,menubar=no,status=yes,resizable=yes,fullscreen=no";
	var defpage = window.open (dest, tit, param);
}

//Funzioni per la ricerca del testo nella tabella degli intermediari
function ricerca()
{
	var bTrovato=false;
	var strRicerca = document.all.item('txtRicerca').value;
	if (strRicerca!=""){
		var posIni = parseFloat (document.all.item('txtPos').value);
		var tab=document.all.item("tabLista");
		var numRighe = tab.rows.length;
		var valCell = ""
		var numColl=0;
		var numPrimaRiga=1;
		for(var i = numPrimaRiga; i < numRighe ;i++)
		{
			tab.rows[i].cells[0].style.color = "black";
			tab.rows[i].cells[1].style.color = "black";
			tab.rows[i].cells[0].className = "tdNoSelezionata"
			tab.rows[i].cells[1].className = "tdNoSelezionata"
			tab.rows[i].cells[2].className = "tdNoSelezionata"
		}
		for(var i = posIni; i < numRighe ;i++)
		{
			strRicerca=strRicerca.toUpperCase();
			//Ricerco nella prima colonna
			numColl = 0;
			valCell = tab.rows[i].cells[numColl].innerHTML.toUpperCase();
			if (valCell.indexOf(strRicerca,0)!=-1)
			{
				//Descrizione trovata
				document.all.item('idLink').href="#"+tab.rows[i].id;
				document.all.item('idLink').click();
				document.all.item('txtPos').value = i+1;
				checkValueButton();
				bTrovato=true;
				break;
			}
			//Ricerco nella seconda colonna
			numColl = 1;
			valCell = tab.rows[i].cells[numColl].innerHTML.toUpperCase();
			if (valCell.indexOf(strRicerca,0)!=-1)
			{
				//Descrizione trovata
				document.all.item('idLink').href="#"+tab.rows[i].id;
				document.all.item('idLink').click();
				document.all.item('txtPos').value = i+1;
				checkValueButton();
				bTrovato=true;
				break;
			}
		}	
		
		if (!bTrovato)
		{
			alert('Ricerca terminata su tutto il documento.')
			resetRicerca();
		}else
		{
			tab.rows[i].cells[numColl].style.color = "red";
			tab.rows[i].cells[0].className="tdSelezionata"
			tab.rows[i].cells[1].className="tdSelezionata"
			tab.rows[i].cells[2].className="tdSelezionata"
		}
	}
}
function checkValueButton()
{
	var btn = document.all.item('btnricerca');
	var pos = parseFloat (document.all.item('txtPos').value);
	if (pos == 0)
	{
		btn.innerHTML = "Trova";
	}
	else
	{
		btn.innerHTML = "Trova Successivo";
	}
}
function resetRicerca()
{
	document.all.item('txtPos').value=1;
	document.all.item('btnricerca').innerHTML="Trova";
	document.all.item('idLink').href="#tdTop";
	document.all.item('idLink').click();
	document.all.item('txtRicerca').focus();
}

function formattaImp(objImp, numDec)
{
	
	var strImp = objImp.value.toString();
	var bOk = true;
	var lunImp = strImp.length;
	var strImpNew='';

	for (var xLun=0; xLun<lunImp; xLun++)
	{
		if (strImp.substring(xLun,xLun+1)!=".")
		{
			strImpNew = strImpNew + strImp.substring(xLun,xLun+1);
		}
	}
	strImp = strImpNew;
	//alert ('entro formatta : ' + objImp.name + " >>> " +  strImp);
	
	if (lunImp == '')
	{
		objImp.value='0,00';
		return;
	}

	if (strImp == '0')
	{
		objImp.value='0,00';
		return;
	}
	
	var posVirg = strImp.indexOf(',',0)
	var sInt = ''; 
	var sDec = ''; 
	if( posVirg == -1)
	{
		sDec = '00';
		sInt = strImp;
	}
	else
	{
		sInt = strImp.substring(0,posVirg);
		sDec = strImp.substring(posVirg+1, lunImp);
		//alert (sInt + ":" + sDec);
				
	}
	
	var lunInt = sInt.length;
	//alert (lunInt);
	var cont = 0;
	var sNewInt = '';
	for (var xInt=lunInt; xInt>0; xInt--)
	{
		cont++;
		sNewInt = strImp.substring(xInt-1,xInt) + sNewInt;
		if (cont==3)
		{
			cont=0;	
			sNewInt = "." + sNewInt;
		}
	}
	if (sNewInt.substring(0,1)=='.')
	{
		lunInt  = sNewInt.length
		sNewInt = sNewInt.substring(1,lunInt);
	}
	if (numDec == 2)
	{	
		objImp.value = sNewInt + "," + sDec;
	}
	else
	{
		objImp.value = sNewInt ;
	}
		
	return;
}
function convertE2L(objImpE, objImpL)
{
	var strImpE = objImpE.value.toString();
	var strImpENew='';
	var lunImp = strImpE.length;

	for (var xLun=0; xLun<lunImp; xLun++)
	{
		if (strImpE.substring(xLun,xLun+1)!=".")
		{
			if (strImpE.substring(xLun,xLun+1)==",")
			{
				strImpENew = strImpENew + '.';
			}
			else
			{
				strImpENew = strImpENew + strImpE.substring(xLun,xLun+1);
			}
		}
	}
	strImpE = strImpENew;
	//alert ('a-convertE2L:' + strImpE);

	var strImpL;
	strImpL = strImpE * 1936.27;
	
	strImpL = Math.round(strImpL,2);
	objImpL.value = strImpL;
	//alert ('b-convertE2L:' + objImpL.value)
	formattaImp(objImpL, 0);
	formattaImp(objImpE, 2);
	
}

function convertL2E(objImpL, objImpE)
{
	var strImpL = objImpL.value.toString();
	var strImpLNew='';
	var lunImp = strImpL.length;

	for (var xLun=0; xLun<lunImp; xLun++)
	{
		if (strImpL.substring(xLun,xLun+1)!=".")
		{
			if (strImpL.substring(xLun,xLun+1)==",")
			{
				strImpLNew = strImpLNew + '.';
			}
			else
			{
				strImpLNew = strImpLNew + strImpL.substring(xLun,xLun+1);
			}
		}
	}
	strImpL = strImpLNew;
	//alert ('a-convertL2E:' + strImpL);

	var strImpE;
	strImpE = strImpL / 1936.27;
	strImpE = strImpE * 100
	strImpE = Math.round(strImpE);
	strImpE = strImpE / 100
	
	strImpE = strImpE + "";
	var lunImp = strImpE.length;
	var strImpNew='';

	for (var xLun=0; xLun<lunImp; xLun++)
	{
		if (strImpE.substring(xLun,xLun+1)==".")
		{
			strImpNew = strImpNew + ",";
		}
		else
		{
			strImpNew = strImpNew + strImpE.substring(xLun,xLun+1);
		}
	}
	strImpE = strImpNew;





	

	objImpE.value = strImpE;
	//alert ('b-convertL2E:' + objImpE.value)
	formattaImp(objImpL, 0);
	formattaImp(objImpE, 2);
	
}

function checkImp(objImp)
{
	var strChar = "0123456789,.-"
	var strImp = objImp.value.toString();
	var lunImp = strImp.length;
	var bOk = true;
	if (lunImp == '')
	{
		objImp.value='0,00';
		return;
	}
	for (var i=0; i<lunImp;i++)
	{
		if (strChar.indexOf(strImp.charAt(i),0)==-1)
		{
			//carattere non ammesso
			alert ("Attenzione: valore immesso non accettabile");
			bOk = false;
			break;
		}
	}
	
	//Controllo che non ci siano due o pių virgole 
	var ret = checkNumCarSpec(strImp, ",", 1)
	if (!ret)
	{
		alert ("Attenzione: valore immesso non accettabile");
		bOk = false;
	}
		
	//Controllo che non ci siano due o pių puntini 
	var ret = checkNumCarSpec(strImp, ".", 1)
	if (!ret)
	{
		alert ("Attenzione: valore immesso non accettabile");
		bOk = false;
	}
	
	//Controllo che non ci siano due o pių meno
	var ret = checkNumCarSpec(strImp, "-", 1)
	if (!ret)
	{
		alert ("Attenzione: valore immesso non accettabile");
		bOk = false;
	}
	
	//Controllo che non ci siano contemporaneamente . e ,
	if (strImp.indexOf(".", 0) != -1 && strImp.indexOf(",", 0) != -1 )
	{
		//alert ("Attenzione: valore immesso non accettabile");
		//bOk = false;
	}
	
	if (!bOk)
	{
		objImp.focus();
	}
	else 
	{
		evidImp(objImp,"N")
	}
	return;
}

function checkData(objImp)
{
	var strChar = "0123456789/"
	var strImp = objImp.value.toString();
	var lunImp = strImp.length;
	var bOk = true;
	var cntSl = 0;
	var pos = 0;
	
	if (lunImp == '')
	{
		//objImp.value= getDataOggi();
		evidGen(objImp,"N")
		return;
	}
	for (var i=0; i<lunImp;i++)
	{
		if (strChar.indexOf(strImp.charAt(i),0)==-1)
		{
			//carattere non ammesso
			alert ("Attenzione: valore immesso non accettabile");
			bOk = false;
			break;
		}
	}

	if (bOk)
	{
	//Controllo che ci siano due / 
	for (var i=0; i<lunImp;i++)
	{
		pos = strImp.indexOf("/", pos+1);
		if (pos!=-1)
		{
			cntSl = cntSl + 1;
		}
		else
		{break;}
	}
	if (cntSl != 2)
	{
		alert ("Attenzione: data formalmente non valida.");
		bOk = false;
	}
	}
	if (bOk)
	{
	//Controllo che tra il primo e il secondo / ci sia almeno un numero
	var pos1 = strImp.indexOf("/", 0);
	var pos2 = strImp.indexOf("/", pos1+1);
	if (pos2 - pos1 < 2)
	{
		alert ("Attenzione: data formalmente non valida.");
		bOk = false;
	}
	}
	if (bOk)
	{
	//Lunghezza minima = 10
	if (lunImp < 10)
	{
		alert ("Attenzione: data formalmente non valida.");
		bOk = false;
	}
	}
	
	if (bOk)
	{
	var numMesi = strImp.substring(3,5);
	//Controllo mesi < 13
	if (numMesi > 12 )
	{
		alert ("Attenzione: data formalmente non valida.");
		bOk = false;
	}
	}
	
	if (bOk)
	{
	var numGiorni = strImp.substring(0,2);
	//Controllo giorni < 32
	if (numGiorni > 31) 
	{
		alert ("Attenzione: data formalmente non valida.");
		bOk = false;
	}
	if (numMesi==2 && numGiorni > 29) 
	{
		alert ("Attenzione: data formalmente non valida.");
		bOk = false;
	}
	
	}
	
	if (!bOk)
	{
		objImp.focus();
	}
	else 
	{
		evidGen(objImp,"N")
	}
	return;
}



function evidImp(objImp,bFlag)
{
	if (bFlag=="S")
	{
		objImp.className="txtImpEvid";
		objImp.select();		
	}
	else
	{
		objImp.className="txtImp";
	}
}
function evidPsw(objImp,bFlag)
{
	
	if (bFlag=="S")
	{
		objImp.className="txtPswEvid";
		objImp.select();		
	}
	else
	{
		objImp.className="txtPsw";
	}
}
function evidGen(objImp,bFlag)
{
	if (bFlag=="S")
	{
		objImp.className="txtGenEvid";
		objImp.select();		
	}
	else
	{
		objImp.className="txtGen";
	}
}
function evidData(objImp,bFlag)
{
	if (bFlag=="S")
	{
		objImp.className="txtDataEvid";
		objImp.select();		
	}
	else
	{
		objImp.className="txtData";
	}
}
function checkPsw(objPsw)
{
	var retCheck = checkCarSpec(objPsw.value);
	if (!retCheck)
	{
		alert ("Attenzione: č stato inserito un carattere non accettabile.\nRidigitare la password.")
		objPsw.focus();
	}
}

function checkCarSpec(strInp)
{
	var strChar = " !#$&()*+-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}";
	var lunInp = strInp.length;
	var bOk = true;
	if (lunInp == '')
	{
		return true;
	}
	for (var i=0; i<lunInp;i++)
	{
		if (strChar.indexOf(strInp.charAt(i),0)==-1)
		{
			//carattere non ammesso
			bOk = false;
			break;
		}
	}
	if (!bOk)	{
		return false;
	}
	else 
	{
		return true;
	}
}

function checkNumCarSpec(strImp, strChar, numPoss)
{
	var lunImp = strImp.length;
	var cntSl = 0;
	var pos = 0;
	for (var i=0; i<lunImp;i++)
	{
		pos = strImp.indexOf(strChar, pos);
		if (pos!=-1)
		{
			cntSl = cntSl + 1;
			pos = pos + 1;
		}else
		{break;}
	}
	if (cntSl > numPoss)
	{
		return false;
	}
	else
	{
		return true;
	}
}
function isNumeric(valore)
{
	var strChar = "0123456789"
	var strImp = valore.toString();
	var lunImp = strImp.length;
	var bOk = true;
	var cntSl = 0;
	var pos = 0;
	
	for (var i=0; i<lunImp;i++)
	{
		if (strChar.indexOf(strImp.charAt(i),0)==-1)
		{
			//carattere non ammesso
			alert ("Attenzione: valore immesso non accettabile");
			bOk = false;
			break;
		}
	}

	return bOk;
}
function setTextDataOggi(idObj)
{
	var obj = document.all.item(idObj);
	obj.value = getDataOggi();
}
function convertiData2aaaammgg(dtInp)
{
	var strDtOut = dtInp.toString().substring(6,10)
	strDtOut = strDtOut + dtInp.toString().substring(3,5)
	strDtOut = strDtOut + dtInp.toString().substring(0,2)
	return strDtOut;
}
function stampaPag()
{
	window.print();
}

function chVisSelect(stato) {
//Chiamata per rendere visibile/invisibile tutte le select presenti nella pagina
//Per evitare che la select venga invisibilizzata aggiungere il parametro noUnvis="true"
//Valori possibili di stato: 'hidden', 'visible'
	//alert (document.forms.length +":"+ stato);
	for (var i =0; i < document.forms.length; i++) {
		for (var j =0; j < document.forms[i].length; j++) {
			if ((document.forms[i][j].type=="select-one")&&(document.forms[i][j].noUnvis!="true"))
				document.forms[i][j].style.visibility = stato;
		}
	}
}


function ordinaLista(numCol, numTdTit)
{
	var sTypeOrder = document.all.item('txtTypeOrder').value;
	document.all.item('txtValColOrder').value = numCol
	if (document.all.item('txtTypeOrder').value == 'asc')
	{document.all.item('txtTypeOrder').value = 'desc'}
	else
	{document.all.item('txtTypeOrder').value = 'asc'}
	
	document.all.item('txtStrOrder').value = numCol + ' ' + document.all.item('txtTypeOrder').value
	document.all.item('txtNumColTit').value = numTdTit;
	document.all.item('frmOrdina').submit();
}

function checkEMail(objInp)
{
	var strChar = "@"
	var strInp = objInp.value.toString();
	var lunInp = strInp.length;
	var cntSl = 0;
	var pos = 0;
	
	if (lunInp == '')
	{
		//objImp.value= getDataOggi();
		evidGen(objInp,"N")
		return;
	}
	if (strInp.indexOf('@')==-1)
		{
			//carattere non ammesso
			alert ("Indirizzo e-mail non valido");
			objInp.focus();

		}
	return;
}

function evidBtn(obj, cFunzio)
{
	if (cFunzio == 'S')
		{obj.className='btnGenEvid'}
	else
		{obj.className='btnGen'}
}

function apriLog(url,windowName){
	var win=open(url,windowName,'menubar=no,scrollbars=yes,toolbar=yes,resizable=yes,width=600,height=400');
	}
function Log(url,windowName){
	var win=open(url,windowName,'menubar=no,scrollbars=yes,toolbar=no,resizable=yes,width=400,height=400');
	}


