/*
 * BGUTILS.JS - 
 */
 
 
/* function checkDate(el, bEmpty)      --  Controllo la data 
 * function getCountSep(sData,CharSep) --  Numero separatori nella stringa
 * function getValidDate(iDay, iMonth, iYear)  --  Restitusce True se la data e' valida
 * function today()  --  Restituisce il giorno
 * function checkTime(el, bEmpty)  --   controlla e formatta un campo ora
 * function getValidTime(iHour, iMinute, iSecond)  -- Controlla se il tempo e' valido
 * function now()  --  Restituisce ora()
 * function getInt(sData,tipoNumero)
 *					sData		elemento nel form
 *					tipoNumero	se 1 verifica i limiti numerici di un campo Integer
 *								se .....
 * function checkZero(sData)
 * function checkZero(sData)  --  Metto zero in fondo al numero
 * function getDec(sData)  --  restituisce la parte decimale di un valore
 * function removeZero(sStr, iSide)
 * function formatVal(sVal, bPoint)  --  Formatta un valore
 * formatta una stringa convertendo il separatore decimale in "." e togliendo i separatori
 * delle migliaia (serve per l'invio a server dei dati numerici)
 * function formatNumber(myString)
 * function checkDouble(el,bCountSep,bZero)
 * function checkInt(el,bCountSep,bZero)
 * function checkCAP(el)
 * function dateToYYYYMMDD(el)
 * function timeToHHMMSS(el)
 */
 

var sCharDecSep=",";
var sCharMilSep=".";

function visAlert()
{
	alert("Attenzione!!! - La modifica di questi dati potrebbe compromettere il corretto funzionamento dell'applicazione IMMOWEB.");
}

//converte una stringa in data
//se bEmpty=true la data può essere anche vuota, 
//altrimenti se vuota viene valorizzata con today();
	function checkDate(el, bEmpty)
	{
		var sData=new String();
		var iNumSep;
		var sDay= new String();
		var sMonth= new String();
		var sYear= new String();
		var iCont;
		var d = new Date();
			
		sData=el.value;
		
		iNumSep=getCountSep(sData,"/");

		switch (iNumSep)
		{
			case 0:
			//non ci sono separatori per la data
				if ((sData.length<4)||(sData.length>8))
				{
					if ((sData.length==0) && bEmpty)
					{						
						el.value="";
						return true;
					}
					else
					{
						alert("Data non Valida");
						el.value=today();
						return false;
					}
				}
				else
				{					
						sDay=sData.substr(0,2);
						sMonth=sData.substr(2,2);
						sYear=sData.substr(4);																
						if ( Number(sDay)=="NaN" || Number(sYear)=="NaN" || Number(sMonth)=="NaN" )
						{
							el.value=today();
							return false;
						}
				}
				break;
			case 1: //un solo separatore
				if ((sData.length<5)||(sData.length>9))
				{
					alert("Data non Valida");
					el.value=today();
					return false
				}
				else
				alert
				{
					sDay=sData.substring(0,sData.indexOf("/"));
					iCont=sData.length-sData.indexOf("/")-1; //icont=numero di caratteri dopo il separatore
					switch (iCont)
					{
						case 0:
						{
							sMonth="";
							sYear="";
							break;
						}

						case 1:;case 2:;
						{
							sMonth=sData.substr(sData.indexOf("/")+1,iCont);
							sYear="";
							break;
						}
						default:
						{
						sMonth=sData.substr(sData.indexOf("/")+1,2);
						sYear=sData.substr(sData.indexOf("/")+3);
						break;
						}
					}
				}
				break;
			case 2://due separatori
				if ((sData.length<6)||(sData.length>10))
				{
					alert("Data non Valida");
					el.value=today();
					return false
				}
				else
				{
					sDay=sData.substring(0,sData.indexOf("/"));
					sMonth=sData.substring(sData.indexOf("/")+1,sData.lastIndexOf("/"));
					sYear=sData.substr(sData.lastIndexOf("/")+1);
				}
				break;
			default:
			{
				alert("Data non Valida");
				el.value=today();
				return false
			}
		}
		//adesso ho i valori della data in sDay, sMonth, sYear
		if (sDay.length>2) sDay=sDay.substr(0,2)
		if (isNaN(parseInt(sDay,10))||(parseInt(sDay,10)==0)) {
			alert("Data non Valida");
			el.value=today();
			return false;
		}
		
		if (sMonth.length>2) sMonth=sMonth.substr(0,2)
		if (isNaN(parseInt(sMonth,10))||(parseInt(sMonth,10)==0)){
			alert("Data non Valida");
			el.value=today();
			return false;
		}

		if (sYear.length>4) sYear=sYear.substr(0,4)
		if (isNaN(parseInt(sYear,10))||(parseInt(sYear,10)==0)){
			alert("Data non Valida");
			el.value=today();
			return false;
		}else{
			if (parseInt(sYear,10)<100)
			//l'anno è in 2 cifre lo converto in 4
			{
				if (parseInt(sYear,10)<50) 
					sYear= parseInt(sYear,10)+2000;
				else
					sYear= parseInt(sYear,10)+1900;
			}
		}
		if (getValidDate(parseInt(sDay,10), parseInt(sMonth,10), parseInt(sYear),10))
		{
			el.value=sDay + "/" + sMonth + "/" + sYear
			return true;
		}
		else
		{
			alert("Data non Valida");
			el.value=today();
			return false;
		}
	}


	function setTitle(myTitle)
	{
		parent.document.title=myTitle;
	}
	
	/* Conta il numero dei separatori nella stringa */
	function getCountSep(sData,CharSep)
	{
		var i, c;
		var NumCharSep=0;
	
		for (i=0;i<sData.length;i++) 
		{
			c=sData.charAt(i);
			if (c==CharSep) NumCharSep++;
		}
		return NumCharSep;
	}



	/*funzione che restituisce true se la data è corretta*/
	function getValidDate(iDay, iMonth, iYear)
	{
		var bisest=((((iYear%4)==0)&&(((iYear%100)!=0)||((iYear%400)==0)))||(bisest=2000));
		if ((iDay>31)||(iMonth>12)) return false;
			else
			{
				switch(iMonth)
				{
					case 2:;
					if (bisest) 
					{
						if (iDay>29) return false;
					}
					else 
					{
						if (iDay>28) return false;
					}
					break;
					case 4:;case 6:;
					case 9:;case 11:;
						if (iDay>30) return false;
						break;
				}
				return true
			}
	}
	
	function today()
	{
		var d = new Date();
		var s;
		if (d.getDate()<10)
			s="0" + d.getDate();
		else
			s= d.getDate();
			
		if (d.getMonth()<9)
			s+="/" + "0" + (d.getMonth()+1);
		else
			s+="/" + (d.getMonth()+1);

		s+="/" + (d.getFullYear());

		return(s);	
	}


//controlla e formatta un campo ora
	function checkTime(el, bEmpty)
	{
		var sTime=new String();
		var iNumSep;
		var sHour= new String();
		var sMinute= new String();
		var sSecond= new String();
		var iCont;
			
		sTime=el.value;
		iNumSep=getCountSep(sTime,":");

		switch (iNumSep)
		{
			case 0:
			//non ci sono separatori per l'ora
				if ((sTime.length<3)||(sTime.length>6))
				{
					if ((sTime.length==0) && bEmpty)
					{						
						el.value="";
						return true;
					}
					else
					{
						alert("Ora non Valida");
						el.value=now().substr(0,5);
						return false;
					}
				}
				else
				{
					sHour=sTime.substr(0,2);
					sMinute=sTime.substr(2,2);
					sSecond=sTime.substr(4);
				}
				break;
			case 1: //un solo separatore
				if ((sTime.length<4)||(sTime.length>7))
				{
					alert("Ora non Valida");
					el.value=now().substr(0,5);
					return false
				}
				else
				{
					sHour=sTime.substring(0,sTime.indexOf(":"));
					iCont=sTime.length-sTime.indexOf(":")-1; //icont=numero di caratteri dopo il separatore
					switch (iCont)
					{
						case 0:
						{
							sMinute="";
							sSecond="";
							break;
						}

						case 1:;case 2:;
						{
							sMinute=sTime.substr(sTime.indexOf(":")+1,iCont);
							sSecond="";
							break;
						}
						default:
						{
						sMinute=sTime.substr(sTime.indexOf(":")+1,2);
						sSecond=sTime.substr(sTime.indexOf(":")+3);
						break;
						}
					}
				}
				break;
			case 2://due separatori
				if ((sTime.length<5)||(sTime.length>8))
				{
					alert("Ora non Valida");
					el.value=now().substr(0,5);
					return false
				}
				else
				{
					sHour=sTime.substring(0,sTime.indexOf(":"));
					sMinute=sTime.substring(sTime.indexOf(":")+1,sTime.lastIndexOf(":"));
					sSecond=sTime.substr(sTime.lastIndexOf(":")+1);
				}
				break;
			default:
			{
				alert("Ora non Valida");
				el.value=now().substr(0,5);
				return false
			}
		}
		//adesso ho i valori del'ora in sHour, sMinute, sSecond
		if (sHour.length>2) sHour=sHour.substr(0,2)
		if (isNaN(parseInt(sHour,10))||(parseInt(sHour,10)==0)) sHour="0"
		if (sHour.length==1) sHour="0" + sHour

		if (sMinute.length>2) sMinute=sMinute.substr(0,2)
		if (isNaN(parseInt(sMinute,10))||(parseInt(sMinute,10)==0)) sMinute="0"
		if (sMinute.length==1) sMinute="0" + sMinute

		if (sSecond.length>2) sSecond=sSecond.substr(0,2)
		if (isNaN(parseInt(sSecond,10))||(parseInt(sSecond,10)==0)) sSecond="0";
		if (sSecond.length==1) sSecond="0" + sSecond

		if (getValidTime(parseInt(sHour,10), parseInt(sMinute,10), parseInt(sSecond),10))
		{
			el.value=sHour + ":" + sMinute //+ ":" + sSecond
			return true;
		}
		else
		{
			alert("Ora non Valida");
			el.value=now().substr(0,5);
			return false;
		}
	}

//restituisce true se l'ora è valida
	function getValidTime(iHour, iMinute, iSecond)
	{
		if ((iHour>23)||(iHour<0)) return false;
		if ((iMinute>59)||(iMinute<0)) return false;
		if ((iSecond>59)||(iSecond<0)) return false;
		return true
	}

//restituisce l'ora corrente
	function now()
	{
		var d = new Date();
		var s;
		
		if (d.getHours()<10)
			s="0" + d.getHours();
		else
			s= d.getHours();
			
		if (d.getMinutes()<9)
			s+=":" + "0" + (d.getMinutes());
		else
			s+=":" + (d.getMinutes());

		if (d.getSeconds()<9)
			s+=":" + "0" + (d.getSeconds());
		else
			s+=":" + (d.getSeconds());
		return(s);	
	}

function getInt(sData,tipoNumero)
{
var sInt="";
	switch (getCountSep(sData,sCharDecSep))
	{
		case 0:
		{
			sInt=sData;
			break;
		}
		case 1:
		{
			sInt=sData.substring(0,sData.indexOf(sCharDecSep,0));
			break;
		}
		default:
		{
			sInt="0";
		}	
	}

	sInt=removeZero (sInt,1)
	if (tipoNumero==1)
	{
		/*
		Quando in tipo numero passo 1 allora suppongo che sto richiedendo il intero
		e verifico i limiti numerici...
		*/
		if (Number(sInt)>32000)
		{
			sInt="0";
		}
	}
	return(sInt);
}

// se passo un numero vuoto allora ci metto lo zero
function checkZero(sData)
{
var sInt="";
sInt=sData;
	if (sData=="")
	{
		sInt="0";
	}	
	return(sInt);
}
//restituisce la parte decimale di un valore
function getDec(sData)
{
var sDec="";
	switch (getCountSep(sData,sCharDecSep))
	{
		case 1:
		{
			sDec=sData.substr(sData.indexOf(sCharDecSep,0)+1);
			break;
		}
		default:
		{
			sDec="0";
		}	
	}
	sDec=removeZero (sDec,2)
	return(sDec);
}
//funzione che toglie gli zeri (iniziali o finali) e i caratteri da una stringa
function removeZero(sStr, iSide)
{
	var sRet="";
	var i,sChar;
	var bNotRemove=false;
	if (iSide==1)//toglie gli zeri alla parte intera
		for(i=0; i<sStr.length; i++)
		{
			sChar=sStr.charAt(i);
			if (sChar=="0") 
			{
				if (bNotRemove) sRet+=sChar;
			}
			else
				if(!isNaN(sChar))
				{
					sRet+=sChar;
					bNotRemove=true;
				}
		}
	else //toglie gli zeri alla parte decimale
		for(i=sStr.length-1; i>-1; i--)
		{
			sChar=sStr.charAt(i);
			if (sChar=="0") 
			{
				if (bNotRemove) sRet=sChar+sRet;
			}
			else
				if(!isNaN(sChar))
				{
					sRet=sChar+sRet;
					bNotRemove=true;
				}
		}
	return(sRet);
}
//formatta una stringa lasciando solo i numeri e se bPoint==true separando con '.'
function formatVal(sVal, bPoint)
{
	var sRet="";
	var iNumPoint=0;
	for(i=sVal.length-1; i>-1; i--)
	{
		if (!isNaN(sVal.charAt(i)))
		{
			sRet= sVal.charAt(i) + sRet;
			if ((((sRet.length-iNumPoint)%3)==0)&&(i>0)&&(bPoint==true)) 
			{
				sRet=sCharMilSep + sRet;
				iNumPoint++;
			}
		}
	}
	return (sRet);
}
//formatta una stringa convertendo il separatore decimale in "." e togliendo i separatori
//delle migliaia (serve per l'invio a server dei dati numerici)
function formatNumber(myString)
{
	var s=myString;
	if (s=="") s="0";
	do
	{
		s=s.replace(sCharMilSep,"");
	}
	while (s.indexOf(sCharMilSep)>-1);
	s=s.replace(sCharDecSep,",");
	return(s);
}


	//converte in numero con la virgola, 
	//se bcountsep=true inserisce il separatore di migliaia
	//se bZero=true restituisce zero se il campo non ha valore
	function checkDouble(el,bCountSep,bZero)
	{
		var sDec="";
		var sInt="";
		sInt=getInt(el.value);
		sDec=getDec(el.value);
		if (sInt!="") sInt=formatVal(sInt, bCountSep);
		if (sDec!="") sDec=formatVal(sDec,false);
		if ((sInt=="")&& bZero) sInt="0";
		
		if (sDec=="")
			el.value=sInt
		else
			el.value=sInt + sCharDecSep + sDec
	}

	//converte in numero intero, 
	//se bcountsep=true inserisce il separatore di migliaia
	//se bZero=true restituisce zero se il campo non ha valore

	function checkInt(el,bCountSep,bZero)
	{
		var sInt="";
		sInt=getInt(el.value);
		if (sInt!="") {
		sInt=formatVal(sInt, bCountSep);
		}
		else
		{
		//alert("Il campo e' numerico. Inserire solo cifre [/0-9/]");
		}
		if ((sInt=="") && bZero) sInt="0";
		el.value=sInt;
		
		
	}

	function checkCAP(el)
	{
		var sCap="";
		var i,l;
		sCap=getInt(el.value);
		if (sCap!="") sCap=formatVal(sCap,false);
		if (sCap=="") 
			sCap="00000"
		else
		{
			if (sCap.length>5)
				sCap=sCap.substr(0,5);
			else
			{
				l=sCap.length;
				for (i=1;i<=5-l;i++)
				{
					sCap="0"+ sCap;
				}
			}
		}
		el.value=sCap;
	}
	
	function dateToYYYYMMDD(el)
	{
		var sDateInv="";
		if (el.value=="")
		{
			sDateInv=""
		}
		else
		{
			sDateInv=el.value.substr(6,4)+el.value.substr(3,2)+el.value.substr(0,2);
		}
		return(sDateInv);
	}

	function dateToDDMMYYYY(el)
	{
		var sDate="";
		if (el.value!="")
		{
			sDate=el.value.substr(6,2) + "/" + el.value.substr(4,2) + "/" + el.value.substr(0,4);
		}
		return(sDate);
	}

	function timeToHHMMSS(el)
	{
		var sTimeInv="";
		if (el.value!="")
		{
			sTimeInv=el.value.substr(0,2) + ":" + el.value.substr(2,2) + ":" + el.value.substr(4,2);
		}
		return(sTimeInv);
	}
	function formatHour(el)
	{
		var sTimeInv="";
		sTimeInv=el.value.substr(0,2) + el.value.substr(3,2) + el.value.substr(6,2);
		return(sTimeInv);
	}

function MM_jumpMenu(targ,selObj,restore)
{ //v3.0
	document.dati.hdnData.value=selObj.options[selObj.selectedIndex].value;
	document.dati.hdnData.value=dateToYYYYMMDD(document.dati.hdnData);
	document.dati.hdnAction.value="1";
	document.dati.action="home.asp";
	document.dati.target="";
	document.dati.submit();
}

function gotoPage(xC,xId)
{
	document.dati.target="";
	switch (xC)
		{
			case 4:
				// avanti
				document.dati.hdnPage.value=xId;
				document.dati.hdnAction.value="4";
				document.dati.action="home.asp";
				//document.dati.method="get";
				document.dati.target="";
				document.dati.submit();
				break;
			case 2:

				break;
			case 3:
				// dietro
				document.dati.hdnPage.value=xId;
				document.dati.hdnAction.value="3";
				document.dati.action="home.asp";
				//document.dati.method="post";
				document.dati.target="";
				document.dati.submit();
				break;
			case 1:
				var xwin;
				var ALT = 70;
				var LAR = 10;
				/*
				document.dati.hdnId.value=xId;
				document.dati.hdnTipo.value=document.dati.hdnTipo.value;
				document.dati.target="_blank";
				document.dati.action="singolo.asp";
				document.dati.submit();
				*/
				xwin=window.open('singolo.asp?hdnTipo=' + document.dati.hdnTipo.value + "&hdnId=" + xId, 'SING', 'toolbar=no,location=yes,resizable=yes,status=no,menubar=no,scrollbars=yes,top=0,left=0,width='+(screen.availWidth -LAR)+',height='+(screen.availHeight-ALT));
				//finpagina.moveTo((screen.availWidth-LAR)/2,(screen.availHeight-ALT)/2);
				//,width=400,height=346,screenX=435,screenY=50,left=435,top=50');
				xwin.focus();


				break;
			default:
				break;
		}
}

