
function apri_finestra(stringa_url,proprieta)
{
	//alert("ciao");
	window.open(stringa_url,"mappa",proprieta);
	//document.mappa_grafica.submit();
}



function isNotEmpty(fldobj)
{

  if(fldobj.value=="" || fldobj.value==null)
  {  alert ("Il campo " + fldobj.name + " e' obbligatorio");
	 fldobj.value="";
	 fldobj.focus();
	 return false;
  }
  return true;
}

function isInteger(fldobj)
{ var str=fldobj.value+"";

  if(str=="") return false;
  for(var i=0; i<str.length; i++)
  {
	if(str.charAt(i) < "0" || str.charAt(i) >"9")
  	{
		alert ("Il campo non e' un numero intero");
		fldobj.value="";
		fldobj.focus();
		return false;
	}
  }

  return true;
}

function isNumber(fldobj)
{

  var str=fldobj.value+"";
  
  if(str=="") return false;
  for(var i=0; i<str.length; i++)
  {
	if(str.charAt(i) < "0" || str.charAt(i) >"9")
  	{
		alert ("Il valore non e' un numero");
		fldobj.value="";
		fldobj.focus();
		return false;
	}
  }
  if(isNumber.arguments.length==3)
  {
     var num=parseInt(str);
     var min=isNumber.arguments[1];
     var max=isNumber.arguments[2];

     if(num < min || num > max)
     {
		alert ("Il valore amesso e da " +min+ " a " +max);
		fldobj.value="";
		fldobj.focus();
		return false;
     }
  }

  return true;
}

//La funzione seguente serve per gli importi in LIRE o EURO:
// 1) se euro==N sono in regime di lire
// 1) se euro!=N sono in regime di euro
function isImporto(fldobj,euro)
{
  var str=fldobj.value+"";

  if(euro == "N")
  {
	  if(str=="") return false;
	  for(var i=0; i<str.length; i++)
	  {
	        if(i == 0 && str.charAt(i) == "-")
		{
			continue;
		}
		if(str.charAt(i) < "0" || str.charAt(i) >"9")
		{
			alert ("Il valore non e' un importo corretto in Lire");
			fldobj.value="";
			fldobj.focus();
			return false;
		}
	  }
	  if(isImporto.arguments.length==4)
	  {
	     var num=parseInt(str);
	     var min=isImporto.arguments[2];
	     var max=isImporto.arguments[3];

	     if(num < min || num > max)
	     {
			alert ("L'importo deve essere compreso tra L. " +min+ " e L. " +max);
			fldobj.value="";
			fldobj.focus();
			return false;
	     }
	  }
  }
  else
  {
	 var num1=Number(str);
	 if(str=="") return false;
	 for(var j=0; j<str.length; j++)
	 {
	        if(j == 0 && str.charAt(j) == "-")
			continue;
	        if(str.charAt(j) < "0" || str.charAt(j) >"9" )
		{
			if(str.substr(j,1) != ".")
			{
				alert ("Il campo non e' un importo in Euro corretto");
				fldobj.value="";
				fldobj.focus();
				return false;
			}
		}
	  }
	  if(isImporto.arguments.length==4)
	  {
	     var min1=isImporto.arguments[2];
	     var max1=isImporto.arguments[3];

	     if(num1 < min1 || num1 >= max1)
	     {
			alert ("L'importo deve essere compreso tra E. " +min+ " e E. " +max);
			fldobj.value="";
			fldobj.focus();
			return false;
	     }
	  }
  }

  return true;
}

function isRealNumber(fldobj)
{
  var str=fldobj.value+"";

  if(str=="") return false;
  for(var i=0; i<str.length; i++)
  {
	if((str.charAt(i) < "0" || str.charAt(i) >"9") && str.charAt(i) !=".")
  	{
		alert ("Il valore non e' un numero");
		fldobj.value="";
		fldobj.focus();
		return false;
	}
  }
  if(isRealNumber.arguments.length==3)
  {
     var num=parseFloat(str);
     var min=isRealNumber.arguments[1];
     var max=isRealNumber.arguments[2];

     if(num < min || num > max)
     {
		alert ("Il valore amesso e da " +min+ " a " +max);
		fldobj.value="";
		fldobj.focus();
		return false;
     }
  }

  return true;
}

// Controllo la data passata
function ctrDate(inDate) {
    if (inDate.indexOf('-')) {
        var inDay = inDate.substring(0,inDate.indexOf("-"));
            if (inDay.substring(0,1) == "0" && inDay.length > 1)
                inDay = inDay.substring(1,inDay.length);
            inDay = parseInt(inDay);
        var inMonth   = inDate.substring(inDate.indexOf("-") + 1, inDate.lastIndexOf("-"));
            if (inMonth.substring(0,1) == "0" && inMonth.length > 1)
                inMonth = inMonth.substring(1,inMonth.length);
            inMonth = parseInt(inMonth);
        var inYear  = parseInt(inDate.substring(inDate.lastIndexOf("-") + 1, inDate.length));

	if(!isFourDigitYear(inYear)) return false;
	if(inMonth == 0 || inMonth > 12) return false;
        numDays = getDaysInMonth(month,year);
	if(inDay > numDays || inDay == 0) return false;
	}

}

// verifico che l'anno sia di 4 caratteri
function isFourDigitYear(year) {
    if (year.length != 4) {
        return false;
    }
    else {
        return true;
    }
}

// Si fa' dare i giorni del mese
function getDaysInMonth(month,year)  {
    var days;
    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  days=31;
    else if (month==4 || month==6 || month==9 || month==11) days=30;
    else if (month==2)  {
        if (isLeapYear(year)) {
            days=29;
        }
        else {
            days=28;
        }
    }
    return (days);
}


// Verifica se l'anno e' bisestile
function isLeapYear (Year) {
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}
function isDate(campo) {
  var DId;	
  var DIm;	
  var DIy;	
  var data=campo.value;

  if(data.length != 6 && data.length != 8 && data.length != 10)
  {
	alert("I formati validi per le Date sono\nGGMMAA\n" +
	      "GGMMAAAA\nGG-MM-AAAA ");
	campo.value = "" ;
	campo.focus();
	return(false);	
  }
  if(data.length == 6)
  {	
  	DId=data.substring(0,2);	
  	DIm=data.substring(2,4);	
  	DIy="19" + data.substring(4,6);	
  }
  else
  {	
	if(data.length == 8)
	{	
  		DId=data.substring(0,2);	
  		DIm=data.substring(2,4);	
  		DIy=data.substring(4,8);	
  	}
  	else
  	{	
  		DId=data.substring(0,2);	
  		DIm=data.substring(3,5);	
  		DIy=data.substring(6,10);	
  	}
	
  }
  var isdata = 0;
  if (data == "" || data == "00-00-0000")
	return(true);

  for(var i=0; i<DId.length; i++)
  {
	if((DId.charAt(i) < "0" || DId.charAt(i) >"9"))
  	{
		isdata=1;
	}
  }

  for(var j=0; j<DIm.length; j++)
  {
	if((DIm.charAt(j) < "0" || DIm.charAt(j) >"9"))
  	{
		isdata=1;
	}
  }

  for(var r=0; r<DIy.length; r++)
  {
	if((DIy.charAt(r) < "0" || DIy.charAt(r) >"9"))
  	{
		isdata=1;
	}
  }

  if(parseFloat(DIm) < 1 || parseFloat(DIm) > 12)
  {	
	isdata=1;
  }
  if(parseFloat(DIy) < 1000 || parseFloat(DIy) > 2050)
  {	
	isdata=1;
  }
  if(parseFloat(DIm) == 1 || 
     parseFloat(DIm) == 3 ||
     parseFloat(DIm) == 5 ||
     parseFloat(DIm) == 7 ||
     parseFloat(DIm) == 8 ||
     parseFloat(DIm) == 10 ||
     parseFloat(DIm) == 12)
     { 
  	if(parseFloat(DId) < 1 || parseFloat(DId) > 31)
	{
	 isdata=1;
  	}
     }
  if(parseFloat(DIm) == 4 || 
     parseFloat(DIm) == 6 ||
     parseFloat(DIm) == 9 ||
     parseFloat(DIm) == 11)
     { 
  	if(parseFloat(DId) < 1 || parseFloat(DId) > 30)
	{
	 isdata=1;
  	}
     }
  if(parseFloat(DIm) == 2 ) 
     {
    	var iBis = parseFloat(DIy) % 4;
  	if( iBis == 0) //Bisestile
	{
  		if(parseFloat(DId) < 1 || parseFloat(DId) > 29)
		{
		 isdata=1;
  		}
	}
	else
	{
  		if(parseFloat(DId) < 1 || parseFloat(DId) > 28)
		{
		 isdata=1;
  		}
	}
     }
  if (isdata == 1) {
     alert("Data Errata!");
     campo.value="";	
     campo.focus();
     return(false);
  }
  campo.value = DId + "-" + DIm + "-" + DIy

  return(true);
}



function isTime(campo) {
  var TimeH;	
  var TimeM;	
  var TimeS
  
  TimeH="";	
  TimeM="";	
  TimeS=""
  
  var ora=campo.value;

  if(ora.length != 4 && ora.length != 5 && ora.length != 6 && ora.length != 8)
  {
	alert("I formati validi per le ore sono\nHHMM\n" +
	      "HH:MM\nHHMMSS\nHH:MM:SS ");
	campo.value = "" ;
	campo.focus();
	return(false);	
  }
  if(ora.length == 4)
  {	
  	TimeH=ora.substring(0,2);	
  	TimeM=ora.substring(2,4);	
  }
  else 
  {	
	if(ora.length == 5)
	{	
  		TimeH=ora.substring(0,2);	
  		TimeM=ora.substring(3,5);	
  	}
  	else
  	{	
  		if(ora.length == 6)
		{	
  			TimeH=ora.substring(0,2);	
  			TimeM=ora.substring(2,4);	
  			TimeS=ora.substring(4,6);	
  		}
  		else
  		{
  		  	TimeH=ora.substring(0,2);	
  			TimeM=ora.substring(3,5);	
  			TimeS=ora.substring(6,8);	
  		}
  	}
  }
  
  var isora = 0;
  if (ora == "" || ora == "00:00")
	return(true);

  for(var i=0; i<TimeH.length; i++)
  {
	if((TimeH.charAt(i) < "0" || TimeH.charAt(i) >"9"))
  	{
		isora=1;
	}
  }

  for(var j=0; j<TimeM.length; j++)
  {
	if((TimeM.charAt(j) < "0" || TimeM.charAt(j) >"9"))
  	{
		isora=1;
	}
  }

  if(TimeS.value="")
  {
  	for(var r=0; r<TimeS.length; r++)
  	{
		if((TimeS.charAt(r) < "0" || TimeS.charAt(r) >"9"))
  		{
			isora=1;
		}
  	}
  }

  if(parseFloat(TimeH) < 0 || parseFloat(TimeH) > 24)
  {	
	isora=1;
  }
  if(parseFloat(TimeM) < 0 || parseFloat(TimeM) > 59)
  {	
	isora=1;
  }
  if(parseFloat(TimeS) < 0 || parseFloat(TimeS) > 59)
  {	
	isora=1;
  }
  
  if (isora == 1) {
     alert("Ora errata!");
     campo.value="";	
     campo.focus();
     return(false);
  }
  
  if(TimeS!="")
  {
  		campo.value = TimeH + ":" + TimeM + ":" + TimeS

  		return(true);
  }
  else
  {
  		campo.value = TimeH + ":" + TimeM

  		return(true);  	
  }
}

function call_form(url_form)
{
		xform.set('ff_action','');
		document.dbform.target='main';
		xform.set('ff_target','main');
		document.dbform.action=url_form;
		document.dbform.submit();
}
