//
// Std.js
//
// $id$
//

/*
 * Azzcheckbox
 * Se c'e', modifico il checkbox "tutti i valori"
 *
 * Argomenti:
 * fn  - nome della variabile. Il checkbox si chiamera'
 *       'all_'+fn
 * ft  - Tipo modifica: 0=deseleziona 1=seleziona
 */
function azzcheckbox( fn,ff )
{
	var v=document.getElementById( 'all_'+fn );
	if( v == null )
		return true;
	var ft = parseInt(ff,10);
	v.checked = ( ft == 0 ) ? false : true;
	return true;
}

/*
 * Chkdate
 * Controllo di validita' di una data
 * richiesta con la funzione 'askdate' / 'askdatetime'
 *
 * Argomenti:
 * fn  - Radice del nome del campo data
 * mnd - true se obbligatorio rispondere, 
 *       false se una non-risposta e' considerata valida.
 *
 * In ritorno:
 * true  = data valida o risposta non data ma mnd == false
 * false = data non valida
 */
function chkdate( fn,mnd )
{
	var wdd = document.getElementById( fn+'_dd' );
	var wmm = document.getElementById( fn+'_mm' );
	var wyy = document.getElementById( fn+'_yy' );

	var idd = wdd.selectedIndex;
	var imm = wmm.selectedIndex;
	var iyy = wyy.selectedIndex;

	var  dd = parseInt(wdd.options[idd].value,10);
	var  mm = parseInt(wmm.options[imm].value,10);
	var  yy = parseInt(wyy.options[iyy].value,10);

	return testdate(dd,mm,yy);
}

/*
 * Controlladata
 * Controlla la data, se sbagliata emette un messaggio
 * e azzera il campo in input
 * Argomenti:
 * w - Oggetto text da cui prelevare la stringa da valutare
 * fmt - formato da esaminare, combinazione di 'DD','MM' e 'YYYY'
 *       e, opzionalmente, 'HH','II' ed 'SS'
 * msg - Messaggio di errore internazionalizzato
 */
function controlladata( w,fmt,msg )
{
	if( validdate(w,fmt) == false )
	{
		alert(msg);
		w.value = '';
	}
	return true;
}

/*
 * Validdate
 * Valida una data estraendola da una text
 * che ha formato imposto
 *
 * Argomenti:
 * w - Referenza all'input con il contenuto da controllare
 * fmt - formato da esaminare, combinazione di 'DD','MM' e 'YYYY'
 *       e, opzionalmente, 'HH','II' ed 'SS'
 *
 * In ritorno:
 * true = data valida / false = data non valida
 */
function validdate( w,fmt )
{
	var txt = w.value;

	// Verifico se data vuota...
	if( txt.length == 0 )
		return true;

	// Verifico se mi hanno dato data+ora anche quando non voglio!
	if( txt.length > fmt.length )
	{
		var dtxt = txt.substring(0,fmt.length);
		txt = dtxt;
		w.value=txt;
	}

	// Verifico se data parziale...
	if( txt.length != fmt.length )
		return false;

	var idd = fmt.indexOf( 'DD'   );
	var imm = fmt.indexOf( 'MM'   );
	var iyy = fmt.indexOf( 'YYYY' );

	var ihh = fmt.indexOf( 'HH' );
	var iii = fmt.indexOf( 'II' );
	var iss = fmt.indexOf( 'SS' );

	var cdd = txt.substring( idd,idd+2 );
	var cmm = txt.substring( imm,imm+2 );
	var cyy = txt.substring( iyy,iyy+4 );

	var dd = parseInt(cdd,10);
	var mm = parseInt(cmm,10);
	var yy = parseInt(cyy,10);

	if( ihh >=0 && iii >=0 && iss >=0 )
	{
		var chh = txt.substring( ihh,ihh+2 );
		var cii = txt.substring( iii,iii+2 );
		var css = txt.substring( iss,iss+2 );

		var hh = parseInt(chh,10);
		var ii = parseInt(cii,10);
		var ss = parseInt(css,10);

		if( hh<0 || hh>23 ) return false;
		if( ii<0 || ii>59 ) return false;
		if( ss<0 || ss>59 ) return false;

	}
	return testdate( dd,mm,yy );
}
/*
 * isvaldd
 *
 * controlla la validita di una data
 * con formato fisso 'dd/mm/yyyy'
 * o parziale dello stesso
 *
 * in ritorno: true / false a seconda
 *
 */
var validd='';
function isvalidd( str )
{
	var s = str;

	if( s.length == 0 )
		return false;	// per me la data vuote non e' valida!

	if( s.indexOf('/') <0 )	// DDMMYY o DDMMYYYY ?
	{
		if( s.length != 8 && s.length!=6 )
			return false;	// NO!
		var cd=s.substr(0,2);
		var cm=s.substr(2,2);
		var cy=s.substr(4);
		s = cd + '/' + cm + '/' + cy;
	}

	var ts = s.split('/');
	if( ts.length!=3 )
		return false;	// data non formata da 3 componenti

	var dd = parseInt(ts[0],10);
	var mm = parseInt(ts[1],10);
	var yy = parseInt(ts[2],10);
	if( yy<100 ) yy+=2000;

	if( testdate(dd,mm,yy) == false )
		return false;

	var cdd = ( dd<10 ) ? '0'+dd : dd;
	var cmm = ( mm<10 ) ? '0'+mm : mm;
	validd = cdd+'/'+cmm+'/'+yy;

	return true;
}
/*
 * Testdate
 * Effettivo controllo di validita' di una data
 *
 * Argomenti:
 * dd - Giorno
 * mm - Mese
 * yy - Anno
 *
 * In ritorno:
 * true = data valida / false = data non valida
 */
function testdate( dd,mm,yy )
{
	if( dd == 0 && mm == 0 && yy == 0 )
	{
		if( mnd )
			return false;
		return true;
	}

	if( dd == 0 || mm == 0 || yy == 0 )
		return false;

	switch( mm )
	{
		case 4:
		case 6:
		case 9:
		case 11:
		if( dd > 30 )
			return false;
		break;

		case 2:
		var a = yy % 4;
		if( a != 0 && dd > 28 )
			return false;
		else if( a == 0 && dd >29 )
			return false;
		break;
	}
	return true;
}
/*
 * ChkDateSequnce
 *
 * Controllo una sequnza di due date per
 * verificare se sono consecutive.
 *
 * Argomenti:
 * fn1  - Nome campo prima data
 * fn2  - Nome campo seconda data
 *
 * In ritorno:
 * true  = le date sono consecutive
 * false = le date NON sono consecutive
 *
 * Comportamento:
 * Una data di valore zero e' considerata minima se fn1
 * o infinito se fn2
 */
function chkDateSequence( fn1,fn2 )
{
	var wdd1 = document.getElementById( fn1+'_dd' );
	var wmm1 = document.getElementById( fn1+'_mm' );
	var wyy1 = document.getElementById( fn1+'_yy' );

	var idd1 = wdd1.selectedIndex;
	var imm1 = wmm1.selectedIndex;
	var iyy1 = wyy1.selectedIndex;

	var  dd1 = parseInt(wdd1.options[idd1].value,10);
	var  mm1 = parseInt(wdd1.options[imm1].value,10);
	var  yy1 = parseInt(wdd1.options[iyy1].value,10);

	var wdd2 = document.getElementById( fn2+'_dd' );
	var wmm2 = document.getElementById( fn2+'_mm' );
	var wyy2 = document.getElementById( fn2+'_yy' );

	var idd2 = wdd2.selectedIndex;
	var imm2 = wmm2.selectedIndex;
	var iyy2 = wyy2.selectedIndex;

	var  dd2 = parseInt(wdd2.options[idd2].value,10);
	var  mm2 = parseInt(wdd2.options[imm2].value,10);
	var  yy2 = parseInt(wdd2.options[iyy2].value,10);

	if( (dd1==0 && mm1==0 && yy1==0) &&
	    (dd2==0 && mm2==0 && yy2==0))
	{
		azzcheckbox( fn1,1 );
		return true;
	}

	azzcheckbox( fn1,0 );
	
	if( (dd1==0 && mm1==0 && yy1==0) ||
	    (dd2==0 && mm2==0 && yy2==0))
		return true;

	if( yy1 > yy2 )
		return false;

	var nn1 = dd1;
	var nn2 = dd2;

	var i = 0;
	var n = 0;
	var b1= 0;
	var b2= 0;

	for( i=1;i<=12;i++ )
	{
		b1=b2=0;

		switch(i)
		{
			case 4:
			case 6:
			case 9:
			case 11:
			n = 30;
			b =  0;
			break;

			case 2:
			n = 28;
			var a = yy1 % 4;
			if( a == 0 ) b1=1;
			a = yy2 % 4;
			if( a == 0 ) b2=1;
			break;

			default:
			n=31;
			break;
		}
		if( mm1 > i )
			nn1+=(n+b1);;
		if( mm2 > i )
			nn2+=(n+b2);;
	}

	if( yy2 > yy1 )
		nn2+=365;

	return ( nn2 >= nn1 ) ? true : false;
}
/*
 * Datexsequnce
 *
 * Controllo una sequnza di due date per
 * verificare se sono consecutive.
 *
 * Argomenti:
 * fn1  - Nome campo prima data
 * fn2  - Nome campo seconda data
 *
 * In ritorno:
 * true  = le date sono consecutive
 * false = le date NON sono consecutive
 *
 * Comportamento:
 * Una data di valore zero e' considerata minima se fn1
 * o infinito se fn2
 */
function datexsequence( fn1,fn2 )
{
	var w1 = document.getElementById( fn1 );
	var v1 = w1.value;
	var  dd1 = 0;
	var  mm1 = 0;
	var  yy1 = 0;
	if( v1.length == 10 )
	{
		dd1 = parseInt(v1.substr(0,2),10);
		mm1 = parseInt(v1.substr(3,2),10);
		yy1 = parseInt(v1.substr(6,4),10);
	}

	var w2 = document.getElementById( fn2 );
	var v2 = w2.value;
	var  dd2 = 0;
	var  mm2 = 0;
	var  yy2 = 0;
	if( v2.length == 10 )
	{
		dd2 = parseInt(v2.substr(0,2),10);
		mm2 = parseInt(v2.substr(3,2),10);
		yy2 = parseInt(v2.substr(6,4),10);
	}

	if( (dd1==0 && mm1==0 && yy1==0) &&
	    (dd2==0 && mm2==0 && yy2==0))
	{
		return true;
	}

	if( (dd1==0 && mm1==0 && yy1==0) ||
	    (dd2==0 && mm2==0 && yy2==0))
		return true;

	if( yy1 > yy2 )
		return false;

	var nn1 = dd1;
	var nn2 = dd2;

	var i = 0;
	var n = 0;
	var b1= 0;
	var b2= 0;

	for( i=1;i<=12;i++ )
	{
		b1=b2=0;

		switch(i)
		{
			case 4:
			case 6:
			case 9:
			case 11:
			n = 30;
			b =  0;
			break;

			case 2:
			n = 28;
			var a = yy1 % 4;
			if( a == 0 ) b1=1;
			a = yy2 % 4;
			if( a == 0 ) b2=1;
			break;

			default:
			n=31;
			break;
		}
		if( mm1 > i )
			nn1+=(n+b1);;
		if( mm2 > i )
			nn2+=(n+b2);;
	}

	if( yy2 > yy1 )
		nn2+=365;

	return ( nn2 >= nn1 ) ? true : false;
}
/*
 * Chknum
 * Controllo validita' numerico floating point
 *
 * Argomenti:
 * fn  - Nome oggetto contenente la stringa del numero
 * nd  - Numero massimo di decimali ammessi
 *
 * Ritorno:
 * true  = numero valido
 * false = numero non valido
 *
 */
function chknum( fn,nd )
{
	var v   = document.getElementById(fn);
	var str = v.value;
	var q   = str.length;

	if( q == 0 ) 
		return true;

	var i   = 0;
	var cc  = '';
	var sgn = false;
	var dec = false;
	var ndc = 0;

	for( i=0;i<q;i++ )
	{
		cc = str.charAt(i);
		switch ( cc )
		{
			case ' ':
			break;

			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			sgn = false;
			if( dec )
				ndc++;
			break;

			case '-':
			if( sgn )
				return false;
			sgn = true;
			break;

			case '.':
			if( dec )
				return false;
			dec = true;
			break;

			default:
			return false;
		}
	}

	if( nd < ndc )
		return false;
	q = nd - ndc;
	if( ndc == 0 )
		str += '.';
	for( i=0;i<q;i++ )
	{
		str += '0';
	}
	return true;
}
/*
 * ChkNumSequence
 * Controllo sequenza di numeri
 *
 * Argomenti:
 * fn1 = Nome oggetto primo numero
 * fn2 = Nome oggetto secondo numero
 *
 * Ritorno:
 * true  = Il numero in fn1 e' minore o uguale al numero in fn2
 * false = Il numero in fn2 e' maggiore al numero in fn2
 *
 */
function chkNumSequence( fn1,fn2 )
{
	var w1 = document.getElementById( fn1 );
	var w2 = document.getElementById( fn2 );
	var c1 = w1.value;
	var c2 = w2.value;

	if( c1.length == 0 && c2.length == 0 )
	{
		azzcheckbox( fn1,1 );
		return true;
	}

	azzcheckbox( fn1,0 );

	if( c1.length == 0 || c2.length == 0 )
		return true;

	var v1 = parseFloat( c1 );
	var v2 = parseFloat( c2 );

	return( v2 >= v1 ) ? true : false;

}
/*
 * Chkint
 * Controllo validita' di digitazione di un intero
 *
 * Argomenti:
 * fn  - Nome oggetto contenente la stringa del numero
 *
 * Ritorno:
 * true  = numero valido
 * false = numero non valido
 *
 */
function chkintx( fn )
{
	var v   = document.getElementById(fn);
	var str = v.value;
	var q   = str.length;

	if( q == 0 )
		return false;

	return chkint( fn );

}
function chkint( fn )
{
	var v   = document.getElementById(fn);
	var str = v.value;
	var q   = str.length;

	if( q == 0 )
		return true;

	var i   = 0;
	var cc  = '';
	var sgn = false;
	var ndc = 0;

	for( i=0;i<q;i++ )
	{
		cc = str.charAt(i);
		switch ( cc )
		{
			case ' ':
			break;

			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			sgn = false;
			break;

			case '-':
			if( sgn )
				return false;
			sgn = true;
			break;

			default:
			return false;
		}
	}
	return true;
}
/*
 * ChkIntSequence
 * Controllo sequenza di numeri interi
 *
 * Argomenti:
 * fn1 = Nome oggetto primo numero
 * fn2 = Nome oggetto secondo numero
 *
 * Ritorno:
 * true  = Il numero in fn1 e' minore o uguale al numero in fn2
 * false = Il numero in fn2 e' maggiore al numero in fn2
 *
 */
function chkIntSequence( fn1,fn2 )
{
	var w1 = document.getElementById( fn1 );
	var w2 = document.getElementById( fn2 );
	var c1 = w1.value;
	var c2 = w2.value;

	if( c1.length == 0 && c2.length == 0 )
	{
		azzcheckbox( fn1,1 );
		return true;
	}

	azzcheckbox( fn1,0 );

	if( c1.length == 0 || c2.length == 0 )
		return true;

	var v1 = parseInt( c1,10 );
	var v2 = parseInt( c2,10 );

	return( v2 >= v1 ) ? true : false;

}
/*
 * Checklist
 * Controllo che una lista non sia vuota
 *
 * Argomenti:
 * fn - Nome dall control che contiene la lista
 *
 * In ritorno:
 * true  = La lista contiene almeno un valore selezionato
 * false = La lista e' vuota
 */
function checklist( fn )
{
	var w = document.getElementById( fn );
	if( w.selectedIndex < 0 )
		return false;
	return true;
}
/*
 * AcquireFocus
 * Mette il fuoco sul primo campo utile della form
 *
 */
function acquireFocus()
{
	var f0=document.forms.length;
	if( f0 == 0 )
		return true;
	f0=document.forms[0].length;
	if( f0 == 0 )
		return true;
	var i=0;
	var e0;
	var t0;
	for( i=0;i<f0;i++ )
	{
		e0=document.forms[0].elements[i];
		if( e0 == null )
			continue;
		if( e0.type == null )
			continue;
		t0=e0.type;
		if( t0.indexOf("text")==0 )
		{
			if(e0.disabled)
				continue;
			if(e0.readOnly)
				continue;
			e0.select();
			e0.focus();
			return true;
		}
		if(	(t0.indexOf("select")==0 ) ||
			(t0.indexOf("check")==0 ) ||
			(t0.indexOf("textarea")==0 ) ||
			(t0.indexOf("radio")==0 ))
		{
			if(e0.disabled)
				continue;
			e0.focus();
			return true;
		}
	}
	return true;
}
function fcin(w)
{
	if( w.readOnly ) return go2next(w);
	w.style.backgroundColor ='#FEFF82'; //#1069EF
	w.style.color='Black';	//'White';
	return true;
}
function fcout(w)
{
	if( w.readOnly ) return true;
	w.style.backgroundColor ='White';
	w.style.color='Black';
	return true;
}
function go2next( w )
{
	var q=document.forms[0].elements.length;
	var i=0;
	var e=null;
	for( i=0;i<q;i++ )
	{
		e=document.forms[0].elements[i];
		if( e.id.length != w.id.length )
			continue;
		if( e.name.indexOf(w.name)!=0 )
			continue;
		if( i<(q-1))
			document.forms[0].elements[i+1].focus();
		return true;
	}
	return true;
}
function butt_fcin(w)
{
	w.style.backgroundColor ='#FEFF82'; //#1069EF
	w.style.color='Black';	//'White';
	return true;
}
function butt_fcout(w)
{
	w.style.backgroundColor ='White';
	w.style.color='Black';
	return true;
}
function cbfcin(w)
{
	w.style.borderColor ='Red';
	return true;
}
function cbfcout(w)
{
	w.style.borderColor ='Black';
	return true;
}
function nonumfcin( w,nd,ds )
{
	if( w.readOnly ) return go2next(w);
	if(w.value.length>0)
		numfcin(w,nd,ds);
	else
	{
		w.style.backgroundColor ='#FEFF82'; //#1069EF
		w.style.color='Black';	//'White';
	}
	return true;
}
function numfcin( w,nd,ds )
{
	if( w.readOnly ) return go2next(w);
	w.style.backgroundColor ='#FEFF82'; //#1069EF
	w.style.color='Black';	//'White';
	if( nd>0 && ds.indexOf(",")==0  )
	{
		var str = w.value;
		var i=0;
		var q=str.length;
		var cc='';
		var st='';
		for( i=0;i<q;i++ )
		{
			cc = str.charAt(i);
			switch ( cc )
			{
				case ',':
				st=st.concat('.');
				break;
				default:
				st=st.concat(cc);
				break;
			}
		}
		w.value=st;
	}
	w.select();
}
function nonumfcout( w,nd,ds )
{
	if( w.readOnly ) return true;
	if(w.value.length>0)
		numfcout(w,nd,ds);
	else
	{
		w.style.backgroundColor ='White';
		w.style.color='Black';
	}
	return true;
}
function numfcout( w,nd,ds )
{
	if( w.readOnly ) return true;
	w.style.backgroundColor ='White';
	w.style.color='Black';

	if( nd>0 && ds.indexOf(",")==0  )
	{
		var str = w.value;
		var i=0;
		var q=str.length;
		var cc='';
		var st='';
		var found = false;
		for( i=0;i<q;i++ )
		{
			cc = str.charAt(i);
			switch ( cc )
			{
				case '.':
				st=st.concat(',');
				found = true;
				break;
				default:
				st=st.concat(cc);
				break;
			}
		}
		if( !found )
		{
			st=st.concat(',');
			for( i=0;i<nd;i++ )
				st=st.concat('0');
		}
		w.value=st;
	}
}
/** Getselectvalue
  * Ottengo il valore correntemente selezionato di una select
  * @param id string Identificatore dell'oggetto
  * @return il valore corrente
  */
function getselectvalue( id )
{
	var w=document.getElementById(id);
	var idx=w.selectedIndex;
	return w.options[idx].value;
}
/** Gettextvalue
  * Ottengo il valore corrente di una richiesta in linea (text/textarea)
  * @param id string Identificatore dell'oggetto
  * @return il valore corrente
  */
function gettextvalue( id )
{
	var w=document.getElementById(id);
	return w.value;
}
/** Getaskvalue
  * Ottengo il valore corrente di una richiesta in linea (text/textarea)
  * @param id string Identificatore dell'oggetto
  * @param def string Valore di default se vuoto
  * @return il valore corrente
  */
function getaskvalue( id,def )
{
	var w=document.getElementById(id);
	if( w == null )
		w=document.getElementByName(id);
	var v=w.value;
	if( v.lenght == 0 )
		return def;
	return v;
}
/** Button_pressed
  * Inserisco l'immagine corrispondente ad un ImageButton premuto
  * @param w    - Referenza all'oggetto bottone
  * @param iurl - URL dell'immagine da inserire
  */
function button_pressed( w,iurl )
{
	w.src=iurl;
	return true;
}
/** Button_released
  * Inserisco l'immagine corrispondente ad un ImageButton rilasciato
  * @param w    - Referenza all'oggetto bottone
  * @param iurl - URL dell'immagine da inserire
  */
function button_released( w,iurl )
{
	w.src=iurl;
	return true;
}
/** Js_trim
  * Tolgo blanks da destra e sinistra
  * @param str - Stringa da elaborare
  */
function js_trim( str )
{
	return str.replace(/^\s*([\S\s]*)\b\s*$/, '$1');
}
/** Select_clear
  * azzero una select
  * @param w - la select da azzerare
  */
function select_clear( w )
{
	if( w.options.length == 0 )
		return true;
	var q = w.options.length;
	var i = 0;
	for( i=0;i<q;i++ )
	{
		w.remove(0);
	}
	return true;
}
/** Select_add
  * Aggiungo un valore in fondo a una select
  * @param w la select da modificare
  * @param key il valore della scelta
  * @param text l'etichetta della scelta
  * @param selected true se selezionata
  */
function select_add( w,key,text,selected )
{
        try
        {
                w.add(new Option(text,key,selected,selected),null);
        } catch(e)
        {
                w.add(new Option(text,key,selected,selected));
        } 
	return true;
}

