// FORMS MANAGER
// VERSIONE 0.4.1
// by ANDREA CASSA, 1 Marzo 1999
// Compatibile con Javascript 1.1

codici=new Array();
codici[0]="NA";
codici[1]="NK";
codici[2]="ND";
codici[3]="NP";
codici[4]="CONT";

function car_perm(a,cars) {
var p=1;
a+='';
var a_length=a.length;
for (var ind=0;ind<a.length;ind++) {
                              match=cars.indexOf(a.charAt(ind) );
                              if (match=='-1')
                              {p=0;
                               break;} 
                              } 
return p
} 



function is_code(val,spec) {
var c=0;
if (spec=='std') {var start=0;var end=3}
if (spec=='all') {var start=0;var end=4}
if (spec=='ext') {var start=0;var end=5}
if (spec=='np') {var start=3;var end=4}
for (var ind=start;ind<end;ind++) {                         
                              if (val.toUpperCase()==codici[ind])
                              {c=1;
                               break;} 
                              } 
return c
} 



function is_goodcode(t2,val) {
var c=0;
t2*=1;
if (t2=='00') {return c}
if (val=='NA') {if (is_dispari(t2)) {c=1;return c}}
if (val=='NK') {if (is_dispari(parseInt((t2/2)+1)-1)) {c=2;return c}}
if (val=='ND') {if (is_dispari(parseInt((t2/4)+1)-1)) {c=3;return c}}
if (val=='NP') {if (is_dispari(parseInt((t2/8)+1)-1)) {c=4;return c}}
if (val=='CONT') {if (is_dispari(parseInt((t2/16)+1)-1)) {c=4;return c}
} 
return c;
} 



function is_dispari(n) {
var c=0;

var mezzo=n/2+'';
if ( (!controllo_interi(1,n)) && controllo_interi(1,mezzo)) {c=1}
return c
} 



function controllo_data(tipo,dd,mm,yyyy) {
var err=0;
if ( (tipo=='2' || tipo=='3') && yyyy=='CONT') {err=0;return err}

if (dd=='' || mm=='' || yyyy=='') {err=3;return err}
             var numeri=0;var n1=0;var n2=0;var n3=0;
             if (!car_perm(dd,"0123456789")) {numeri=1;n1=1;}
             if (!car_perm(mm,"0123456789")) {numeri=1;n2=1;}
             if (!car_perm(yyyy,"0123456789")) {numeri=1;n3=1;}
             

                 if (numeri=='0') {
                 var f=yyyy;
                 if (yyyy.length != 4) {err=1; return err}
                 if (mm<1 || mm>12) {err = 2; return err}
                 if (dd<1 || dd>31) {err = 4; return err}
                 if (yyyy<1800 || yyyy>2050) {err = 6; return err}
                 if ( (mm==4 || mm==6 || mm==9 || mm==11) && (dd==31) ) {err=7;return err}
                 if (mm==2) {
                 if (dd>29) {err=8; return err}
                 if (dd==29) {
                 var f4=parseInt(f%4);
                 var f400=parseInt(f%400);
                 var f100=parseInt(f%100);
                 if (!((f4==0 && f100!=0) || f400 == 0)) {err=9; return err}
                                    } 
                                } 
                             } 
                else {

                if (is_code(dd,"all")) {n1=2}
                if (is_code(mm,"all")) {n2=2}
                if (is_code(yyyy,"all")) {n3=2}
                if (tipo=='2' && yyyy=='CONT') {n3=3}
                if (n1=='1' || n2=='1' || n3=='1') {err=10; return err}

                if (tipo=='1') {err=50; return err}
                if (tipo=='3' && yyyy!='CONT') {err=17; return err}

                if ( (n1=='0') && ( dd<1 || dd>31) ) {err=5; return err}
                if ( (n2=='0') && ( mm<1 || mm>12) ) {err=2; return err}
                if ( (n3=='0') && ( yyyy<1800 || yyyy>2050) ) {err=6; return err}
               } 

return err;
} 



function controllo_interi(tipo,valore) {
var err=0;
var neg_valore=(!valore);
if (!valore) {
err=18;return err} 
var numeri=0;
if (!car_perm(valore,"0123456789")) {
numeri=1}
             if (!numeri) {
return err}

                if (is_code(valore,"all")) {numeri=2}
                if (numeri=='1') {err=19; return err}

                if (tipo=='1') {err=50; return err}

return err;
} 



function controllo_reali(tipo,val) {
var err=0;
if (!val) {err=13;return err}
if (!is_code(val,"all") ) {
var v=val.indexOf('.');
if (v=='-1') {if (controllo_interi(1,val)) {err=12;return err} } 
else {
var enne=val.substring(0,v);
var emme=val.substring(1+v,val.length);
if ( controllo_interi(1,enne) || controllo_interi(1,emme) ) {err=12;return err} 
} 
                           } 
else {if (tipo=='1') {err=12;return err} } 

return err;
} 



function controllo_ora(tipo,mm,hh) {
var err=0;
if (mm=='' || hh=='') {err=3;return err}
             var numeri=0;var n1=0;var n2=0;
             if (!car_perm(mm,"0123456789") ) {numeri=1;n1=1}
             if (!car_perm(hh,"0123456789") ) {numeri=1;n2=1}
                 if (numeri=='0') {
                 if (mm<0 || mm>59) {err = 16; return err}
                 if (hh<0 || hh>23) {err = 15; return err}
                             } 
                else {

                if (is_code(mm,"all")) {n1=2}
                if (is_code(hh,"all")) {n2=2}
                if (n1=='1' || n2=='1') {err=30; return err}

                if (tipo=='1') {err=50; return err}

                if ( (n1=='0') && ( mm<0 || mm>59) ) {err=16; return err}
                if ( (n2=='0') && ( hh<0 || hh>23) ) {err=15; return err}
               } 

return err;
} 



function controllo_iniziali(valore) {
var err=0;
var vall=valore.length;
if (vall!='3' && vall!='2') {err=14;return err;}
if (vall=='3') {var car_permessi="-QWERTYUIOPASDFGHJKLZXCVBNM";}
if (vall=='2') {var car_permessi="QWERTYUIOPASDFGHJKLZXCVBNM";}
if (!car_perm(valore,car_permessi)) {
err=14;return err}
if (vall=='3'){var a=valore.indexOf('-');
              var b=valore.lastIndexOf('-');
              if ( (a!=b) || (a=='0') || (b=='2') ) {err=14;return err} 
return err;}
return err;
} 



function contr(campi_obbligatori,alerts,numform) {
var c1=campi_obbligatori;

if (alerts=='undefined') {alerts='on'}
numform+='';
if (numform=='undefined') {numform=0}
numform*=1;
if (numform) {
f=self.document.forms[numform];
el=self.document.forms[numform].elements;
}  else {
f=self.document.forms[0];
el=self.document.forms[0].elements;}

campi= new Array();
var bit=0;
var stringa=c1;
for (var j=0;bit==0;j++)
{var lstr=stringa.length;
var virgpos=stringa.indexOf(',');
if (virgpos!='-1') {
campi[j]=stringa.substring(0,virgpos);
var nuovastringa=stringa.substring(1+virgpos,lstr);
stringa=nuovastringa;
} 
else {bit=1;
campi[j]=stringa;} 

}




var tot=(campi.length)/3;
var err=0;

var eln=el.length;


for (var z=0;z<tot;z++) {


var pos=3*z;
var tipo=campi[pos];
var t1=tipo.charAt(0);
var nome=campi[pos+1];
var descrizione=campi[pos+2];

if (tipo=='x') {tipo='x15'}
else if (tipo=='x1' || tipo=='x0') {tipo='x00'}
else if (tipo=='n') {tipo='n15'}
else if (tipo=='n1' || tipo=='n0') {tipo='n00'}
else if (tipo=='s' || tipo=='s1' || tipo=='s0') {tipo='si'}
else if (tipo=='r' || tipo=='r1' || tipo=='r0') {tipo='r'}
else if (tipo=='d') {tipo='d15'}
else if (tipo=='d0' || tipo=='d1') {tipo='d00'}
else if (tipo=='d2') {tipo='d31'}
else if (tipo=='d3') {tipo='d16'}
else if (tipo=='h') {tipo='h15'}
else if (tipo=='h1' || tipo=='h0') {tipo='h00'}
else if (tipo=='xr'){tipo='gx00'}
else if (tipo=='nr'){tipo='gn00'}



if (t1=='x'){
err=0;
var val=el[nome].value;
val=val.toUpperCase();
var t2=tipo.substring(tipo.length-2,tipo.length);


if (is_code(val,'all')) {if (is_goodcode(t2,val)) {
continue;
} else {
err=23;
} 
} else {
err=controllo_reali(1,val);
} 

if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome].focus();return err}
continue;
} 

if (t1=='g'){
err=0;
var nome_e_range=nome;
var grat_pos=nome_e_range.indexOf('#');
var nome=nome_e_range.substring(0,grat_pos); 
var ranges=nome_e_range.substring(1+grat_pos,nome_e_range.length);
var grat_pos=ranges.indexOf('#');
var minimo=ranges.substring(0,grat_pos); 
var massimo=ranges.substring(1+grat_pos,ranges.length); 

var val=el[nome].value;
val=val.toUpperCase();
var t2=tipo.substring(tipo.length-2,tipo.length);
var t3=tipo.substring(tipo.length-3,tipo.length-2);


if (is_code(val,'all')) {if (is_goodcode(t2,val)) {
continue;
} else {
err=23;messaggi_errore(tipo,err,descrizione,alerts);el[nome].focus();return err
} 
} else {
if (t3=='x') {err=controllo_reali(1,val);
} else {
err=controllo_interi(1,val)} 
if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome].focus();return err}
err=check_range(val,minimo,massimo);
if (err) {range_errors(tipo,err,descrizione,minimo,massimo,alerts);el[nome].focus();return err}
} 

continue;
} 


if (t1=='n'){

err=0;
var val=el[nome].value;
val=val.toUpperCase();
var t2=tipo.substring(tipo.length-2,tipo.length);


if (is_code(val,'all')) {if (is_goodcode(t2,val)) {
continue;
} else {
err=23;
} 
} else {
err=controllo_interi(1,val);
} 

if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome].focus();return err}
continue;
} 


if (tipo=='si' || tipo=='sv' || tipo=='st'){
err=11;
if (tipo=='si') {if (el[nome].selectedIndex>0){err=0;}}
else if (tipo=='sv') {if (selectvalue(nome,numform)!='') {err=0;}}
else if (tipo=='st') {if (selecttext(nome,numform)!='') {err=0;}}
if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome].focus();return err}
continue;
} 


if (tipo=='r'){
err=11;
var num_bot=el[nome].length;
if (num_bot==null) {
check=el[nome].checked;
if (check) {err=0} 
if (err) {messaggi_errore(tipo,err,descrizione,alerts);return err}
continue;
} 

for (i=0;i<num_bot;i++) {
if (el[nome][i].checked=='1'){err=0;break}}
if (err) {messaggi_errore(tipo,err,descrizione,alerts);return err}
continue;
} 


if (tipo=='t'){
err=20;
if (el[nome].value!=''){err=0}
if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome].focus();return err}
continue;
} 

if (tipo=='b'){
err=0;
if (el[nome].value!=''){err=35}
if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome].focus();return err}
continue;
} 




if (t1=='d') {

var dd=el[nome+'D'].value; 
dd=dd.toUpperCase();
var mm=el[nome+'M'].value; 
mm=mm.toUpperCase();
var yyyy=el[nome+'Y'].value; 
yyyy=yyyy.toUpperCase();





var t2=tipo.substring(tipo.length-2,tipo.length);


if (is_code(dd,'all')) {if (!is_goodcode(t2,dd)) {
err=23;messaggi_errore(tipo,err,descrizione,alerts);el[nome+'D'].focus();return err
} 
}  


if (is_code(mm,'all')) {if (!is_goodcode(t2,mm)) {
err=23;messaggi_errore(tipo,err,descrizione,alerts);el[nome+'M'].focus();return err
} 
} 


if (is_code(yyyy,'ext')) {

if (!is_goodcode(t2,yyyy)) {

err=23;messaggi_errore(tipo,err,descrizione,alerts);el[nome+'Y'].focus();return err
} 
else {

}
}  

var tipo_data=2;



err=controllo_data(tipo_data,dd,mm,yyyy);
if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome+'D'].focus();return err}
continue;
} 

if (t1=='h') {

var m=el[nome+'M'].value; // valore del minuto digitato da utente
m=m.toUpperCase();
var h=el[nome+'H'].value; // valore dell'ora digitato da utente
h=h.toUpperCase();



var t2=tipo.substring(tipo.length-2,tipo.length);


if (is_code(m,'all')) {if (!is_goodcode(t2,m)) {
err=23;messaggi_errore(tipo,err,descrizione,alerts);el[nome+'M'].focus();return err
} 
} 


if (is_code(h,'all')) {if (!is_goodcode(t2,h)) {
err=23;messaggi_errore(tipo,err,descrizione,alerts);el[nome+'H'].focus();return err
} 
} 


var tipo_ora=0;
err=controllo_ora(tipo_ora,m,h)
if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome+'H'].focus();return err}
continue;
} 

if (tipo=='i'){
err=0;var num_campi=0;var valore='';
valore=el[nome].value;
valore=valore.toUpperCase();

err=controllo_iniziali(valore);
if (err) {messaggi_errore(tipo,err,descrizione,alerts);el[nome].focus();return err}
continue;
} 


alert('Attenzione si è verificato un errore. Contattare per favore a.cassa@cineca.it');
err=100;return err;



} 

return err;

} 








