/**************************************************************************
   Funzione che restituisce il valore del campo radio button selezionato
 **************************************************************************/
 function getRadioButtonValue (radio)
  {
    for (var i = 0; i < radio.length; i++)
     { 
       if (radio[i].checked) { break }
     }
    return radio[i].value
  }
/**************************************************************
   Funzione per controllo campi obbligatori
   restituisce true se la stringa contiene solo spazi bianchi
 **************************************************************/
function isEmpty(objField)
{
   for(var i=0; i<objField.value.length; i++)
   {
      var char = objField.value.charAt(i);
      if(char!=' ') return false;
   }
   return true;
}

/**************************************************************
   Funzione per verificare l'esattezza del campo EMail
   restituisce true se la stringa rientra nel formato mail
 **************************************************************/  
 function isEmail(email) 
  {
    var pos1=0;
    var pos2=0;
    var pos3=0;

    pos1=email.indexOf("@",0);	    // looking for @
    pos2=email.indexOf(".",pos1);   // looking for . past @
    pos3=email.indexOf("@",pos1+1); // looking for other @

    if (pos1>0 && (pos2>(pos1+1)) && pos3==-1)
     {
	return true;
     }
    else
     {
	return false;
     }
  }
  
function PostForm(strAction)
{
	document.forms[0].action=strAction;
	document.forms[0].submit();
}

function CheckAll(form) 
 {
	var elem;	
	for (i=0;i<form.elements.length ;i++ ) 
	{
	   elem = form.elements[i];	   
	   if (elem.type=='checkbox')
	   {
	     elem.checked = true;
	   }	   
	}
 }
 
 function InvertCheck(form) 
 {
	var elem;	
	for (i=0;i<form.elements.length ;i++ ) 
	{
	   elem = form.elements[i];	   
	   if (elem.type=='checkbox')
	   {
	     elem.checked = !elem.checked;
	   }	   
	}
 }
 
 // MilkieX: replace v1.11 r03sep2000 1r07mar2000
 // string=replace(otxt:string,stxt:string,rtxt:string[,op:integer])
 // Replaces any occurrence of stxt in otxt with rtxt, optionally starting from position op.
 // Author: Elf Qrin <valcap@thepentagon.com> http://www.ElfQrin.com
 function replace(otxt,stxt,rtxt,op) {
 otxt=""+otxt;
 if (op==null || op=="") {op=0;}
 var ok=false;
 while (ok==false) {
 ok=true;
 var ol=otxt.length;
 var pos=otxt.indexOf(stxt,op);
 if (pos>0) {
 var a1=otxt.substring(0,pos);
 var a2=rtxt;
 var a3="";
 if (pos+stxt.length<=otxt.length) {
 var a3=otxt.substring(pos+stxt.length,otxt.length);
 }
 otxt=a1+a2+a3;
 op=pos+rtxt.length+0;
 ok=false;
 }
 }
 otxt=otxt.substring(1,otxt.length);
 return otxt;
 }



 function isSelectAlmostOneCheckbox(form) 
 {
	var elem;	
	var total = 0;
	for (i=0;i<form.elements.length ;i++ ) 
	{
	   elem = form.elements[i];	   
	   if (elem.type=='checkbox')
	   {
	     if (elem.checked == true) 
	     {	total ++;  }	     	    
	   }	   
	}
	if(total==0)
	{
		//alert("Non è stato selezionato nessun CheckBox");
		return false;
	}
	else
	{
		//alert("Hai selezionato " + total + " CheckBox.");
		return true;
	}
 }


function showCal(objTxt)
{
	var objCal=document.all("spCal");
	
	if (objCal != null) 
	{
		closeCal();
	}else{
		sUrl='../html/pgCalendar.html?' + objTxt.form.name + '.' + objTxt.name
		//win1=window.open(sUrl,'','width=220,height=180')
		//alert(objTxt.offsetTop);
		x=objTxt.offsetTop+objTxt.offsetHeight;
		y=objTxt.offsetLeft;
		CalFrame =document.createElement("<iframe id='spCal' src='"+ sUrl +"' STYLE='border:1px;position:absolute;top:"+ x +";left:"+ y +";width:178;height:170;z-index=100' MARGINHEIGHT='0' MARGINWIDTH='0' NORESIZE FRAMEBORDER='0' width:156 SCROLLING='NO'></iframe>");
		document.body.appendChild(CalFrame);
	}
}

function closeCal()
{
	var objCal=document.all("spCal");
	
	if(objCal != null)
	{
		objCal.outerHTML="";
	}
}


function popup(nomefile, larghezza, altezza, x, y,param,id) 
 {
    win_popup = window.open(nomefile+"?username="+param+"&id="+id,"popup","toolbar= 0,location= 0,directories= 0,status= 1,menubar= 0,scrollbars= 1,resizable= 1,copyhistory= 0,width=" + larghezza + ",height=" + altezza);
    x = parseInt(x);
    y = parseInt(y);
    win_popup.moveTo(x, y); 
    win_popup.focus();    
 }

function popupGenerica(nomefile, larghezza, altezza, x, y,param,id) 
 {
    win_popup = window.open(nomefile,"popup","toolbar= 0,location= 0,directories= 0,status= 1,menubar= 0,scrollbars= 1,resizable= 1,copyhistory= 0,width=" + larghezza + ",height=" + altezza);
    x = parseInt(x);
    y = parseInt(y);
    win_popup.moveTo(x, y); 
    win_popup.focus();    
 }
 
 function LoadCombi_Data(objCombo,strType,strDefValue)
 {
 //input:	objCombo=oggetto combo (<select>);
 //		strType='g' per caricare i giorni,
 //		strTYpe='m' per caricare il combo dei mesi;
 //		strType='a' per caricare il combo dei anni;
 //		strDefValue=valore che si desidera sia selezionato all'interno del combo;
 //		se omesso verra selezionato il giorno o il mese o l'anno della data di sistema.
 //output:	nessuno.
 	
 	var intIni;
 	var intFin;
 	var i;
 	var delta;
 	var today=new Date()
 	var currentYear;
 	
 	//Controllo il parametro strType
 	if (strType=="g" )
 	{
 		//Inizializzo il valore iniziale e finale del ciclo che mi andrà a popolare l'objCombo.
 		intIni=1;
 		intFin=31;
 		delta=0;
 		if (strDefValue==null) strDefValue=today.getDate();	
 	}
 	else if (strType=="m")
 	{
 		//Inizializzo il valore iniziale e finale del ciclo che mi andrà a popolare l'objCombo.
 		intIni=1;
 		intFin=12;
 		delta=0;
 		if (strDefValue==null) strDefValue=today.getMonth()+1;			
 	}
 	else if (strType=="a")
 	{
 		//Inizializzo il valore iniziale e finale del ciclo che mi andrà a popolare l'objCombo.
 		intIni=1;
 		intFin=10;
 		currentYear=today.getFullYear();
 		delta=currentYear-3;
 		if (strDefValue==null) strDefValue=currentYear;			
 	}
 	//Dimensiono il mio array
 	opt=new Array()
 	//Inizializzo la prima voce dell'elenco
 	objCombo.options[0]=new Option ("","");
 	
 	//Ciclo per popolare l'objCombo.
 	for (i=intIni;i<=intFin;i++)
 	{ 		
 		var appo = "";
 		if(parseInt(delta+i)<10) appo="0"+(delta+i);
 		else appo=""+(delta+i);
 		
 		opt[i]=new Option((appo),(appo));
 		objCombo.options[i]=opt[i];
 		
 		if (strDefValue== delta + i)
 		{
 			objCombo.options[i].selected=true;
 		}		 		
 	}	
}

function IsCorrectDate(objComboGG,objComboMM,objComboAA)
{	
//input:	objComboGG,objComboMM,objComboAA oggetti combo da passare come parametri.
//output	boolean;restituisce TRUE se la data è una data valida,FALSE se non lo è;

	switch (objComboMM.value)
	{
		case '2':
			if ((Bisestile(objComboAA.value)==true)  && (objComboGG.value>29) )
			{ return false; }
			else if ((Bisestile(objComboAA.value)==false) && (objComboGG.value>28) )
			{ return false; }
			else
			{ return true; }
		break
		case '4':
			if (objComboGG.value>30)
			{ return false; }
			else
			{ return true; }
		break
		case '6':
			if (objComboGG.value>30)
			{ return false; }
			else
			{ return true; }
		break
		case '9':
			if (objComboGG.value>30)
			{ return false; }
			else
			{ return true; }
		break
		case '11':	
			if (objComboGG.value>30)
			{ return false; }
			else
			{ return true; }
		break
		default: return true;
	}	
}

function Bisestile(iAnno)
{
//input:	iAnno ;anno che deve essere valutato;
//output:	boolean:true se booleano;

	if ((iAnno%4==0) && (iAnno%100) || iAnno%400==0) 
	{ return true; }
	else
	{ return false; }	
}
