<% option explicit %>
<HTML bgcolor=#3875c9>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>Calcolo IBAN</TITLE>
<LINK href="Bonifici.css" rel=stylesheet>
<SCRIPT language=JavaScript>
        function entrata(){
                frmCalcoloIban.AbiAcc.focus();
        }
        
        function ctrlParam(){
                
                var CiIAcc, CinAcc, AbiAcc, CabAcc, CC1Acc, NazAcc;
                //----------------- Controlli Coordinate Bancarie
                
                //combinazioni di coordinate bancarie ammesse
                var varDatiBeneficiario='';
                for (i=0;i<frmCalcoloIban.length;i++){
                        var tempobj=frmCalcoloIban.elements[i];
                        if (tempobj.name=='CiIAcc' || tempobj.name=='CinAcc' || tempobj.name=='AbiAcc' ||
                                tempobj.name=='CabAcc' || tempobj.name=='CC1Acc'){
                                if (tempobj.name=='CinAcc') comFirst=tempobj;
                                if (tempobj.value!='')
                                        varDatiBeneficiario = varDatiBeneficiario + '1';
                                else
                                        varDatiBeneficiario = varDatiBeneficiario + '0';
                        }                
                }
        
                if (varDatiBeneficiario!='00111' && // 1 abi-cab-conto
                        varDatiBeneficiario!='01111' && // 1 cin-abi-cab-conto
                        varDatiBeneficiario!='11111' ){ // 4 cinIban-cin-abi-cab-conto
                        myalert('Coordinate non congruenti.');
                        comFirst.focus();
                        return false;
                }
                //Cin IBAN
                if (frmCalcoloIban.CiIAcc.value!=''){
                        if(frmCalcoloIban.CiIAcc.value.length != 2) {
                                myalert('Il cin del codice Iban deve essere di 2 caratteri.');
                                frmCalcoloIban.CiIAcc.focus();
                                return false;
                        }
                        
                        if(frmCalcoloIban.CiIAcc.value == '00') {
                                myalert('Il cin del codice Iban non può essere zero.');
                                frmCalcoloIban.CiIAcc.focus();
                                return false;
                        }
                }
                
                //Cin BBAN
                if (frmCalcoloIban.CinAcc.value!=''){
                        if(frmCalcoloIban.CinAcc.value.length != 1 || !ctrlCharSet(frmCalcoloIban.CinAcc.value, 'a')) {
                                myalert('Il cin delle coordinate bancarie deve essere un carattere alfabetico.');
                                frmCalcoloIban.CinAcc.focus();
                                return false;
                        }
                }
                
                //Abi
                if (frmCalcoloIban.AbiAcc.value!='') {
                        if (isNaN(frmCalcoloIban.AbiAcc.value)) {
                                myalert('Il codice Abi deve essere numerico.');
                                frmCalcoloIban.AbiAcc.focus();
                                return false;
                        }
                        if (frmCalcoloIban.AbiAcc.value!='1030'&& frmCalcoloIban.AbiAcc.value!='01030' &&
                                frmCalcoloIban.AbiAcc.value!='3400'&& frmCalcoloIban.AbiAcc.value!='03400' &&
                                frmCalcoloIban.AbiAcc.value!='5024'&& frmCalcoloIban.AbiAcc.value!='05024' &&
                                frmCalcoloIban.AbiAcc.value!='3140'&& frmCalcoloIban.AbiAcc.value!='03140' &&
                                frmCalcoloIban.AbiAcc.value!='3163'&& frmCalcoloIban.AbiAcc.value!='03163' &&
                                frmCalcoloIban.AbiAcc.value!='3210'&& frmCalcoloIban.AbiAcc.value!='03210' &&
                                frmCalcoloIban.AbiAcc.value!='3129'&& frmCalcoloIban.AbiAcc.value!='03129' ) {
                                myalert('Il codice Abi deve appartenere ad una banca del gruppo.');
                                frmCalcoloIban.AbiAcc.focus();
                                return false;
                        }
                }
                
                
                //Cab
                if (frmCalcoloIban.CabAcc.value!='') {
                        if (isNaN(frmCalcoloIban.CabAcc.value)) {
                                myalert('Il codice Cab deve essere numerico.');
                                frmCalcoloIban.CabAcc.focus();
                                return false;
                        }
                }
                
                //Conto coordinate bancarie
                if (frmCalcoloIban.CC1Acc.value!='') {
                        if (frmCalcoloIban.CC1Acc.value.length<2) {
                                myalert('Il conto del beneficiario deve essere lungo almeno 2 caratteri.');
                                frmCalcoloIban.CC1Acc.focus();
                                return false;
                        }
                        if (frmCalcoloIban.CC1Acc.value.length>12) {
                                myalert('Il conto del beneficiario non può essere lungo più di 12 caratteri.');
                                frmCalcoloIban.CC1Acc.focus();
                                return false;
                        }
                        //Il conto deve essere composto da caratteri alfabetici e numerici
                        if(!ctrlCharSet(frmCalcoloIban.CC1Acc.value,'h')) {
                                myalert('Conto errato. Caratteri non ammessi.');
                                frmCalcoloIban.CC1Acc.focus();
                                return false;
                        }                        
                }
                frmCalcoloIban.AbiAcc.value = normCod(frmCalcoloIban.AbiAcc.value,5)
                frmCalcoloIban.CabAcc.value = normCod(frmCalcoloIban.CabAcc.value,5)
                frmCalcoloIban.CC1Acc.value = normCod(frmCalcoloIban.CC1Acc.value,12)
                frmCalcoloIban.CC1Acc.value = frmCalcoloIban.CC1Acc.value.toUpperCase();
                frmCalcoloIban.CinAcc.value = frmCalcoloIban.CinAcc.value.toUpperCase();
                
                
                CiIAcc = frmCalcoloIban.CiIAcc.value
                CinAcc = frmCalcoloIban.CinAcc.value
                AbiAcc = frmCalcoloIban.AbiAcc.value
                CabAcc = frmCalcoloIban.CabAcc.value
                CC1Acc = frmCalcoloIban.CC1Acc.value
                NazAcc = frmCalcoloIban.NazAcc.value
                
                CinAcc = CalcolaCin(AbiAcc, CabAcc, CC1Acc);
                if (varDatiBeneficiario == '00111'){ // abi-cab-conto
                        frmCalcoloIban.CinAcc.value = CinAcc;
                        varDatiBeneficiario = '01111';
                }
                else{
                        if(frmCalcoloIban.CinAcc.value != CinAcc){
                                myalert('Cin BBAN Errato.');
                                frmCalcoloIban.CinAcc.focus();
                                return false;
                        }                        
                }
                CiIAcc = calcolaCheckIban(CinAcc + AbiAcc + CabAcc + CC1Acc);                        
                CiIAcc = normCod(CiIAcc,2);
                if (varDatiBeneficiario == '01111'){ // cin-abi-cab-conto
                        frmCalcoloIban.CiIAcc.value = CiIAcc;
                        varDatiBeneficiario = '11111';
                }
                else{
                        if(frmCalcoloIban.CiIAcc.value != CiIAcc){
                                myalert('Cin IBAN Errato.');
                                frmCalcoloIban.CiIAcc.focus();
                                return false;
                        }                        
                }
                
                frmCalcoloIban.IbanAcc.value = NazAcc + CiIAcc + CinAcc + AbiAcc + CabAcc + CC1Acc;
                
                myalert('OK');
                
        }
        
        function myalert(text){
                frmCalcoloIban.Message.style.backgroundColor = '#FFFFFF'
                frmCalcoloIban.Message.value = text;
                return true;
        }
        
        function resetalert(){
                frmCalcoloIban.Message.style.backgroundColor = '#D2E6F4'
                frmCalcoloIban.Message.value = '';
                return true;
        }
        
        function pulisci(){
                frmCalcoloIban.CiIAcc.value = '';
                frmCalcoloIban.CinAcc.value = '';
                frmCalcoloIban.AbiAcc.value = '';
                frmCalcoloIban.CabAcc.value = '';
                frmCalcoloIban.CC1Acc.value = '';
                frmCalcoloIban.IbanAcc.value = '';
                resetalert();
                        
                return true;
        }
        
        
        
        /*------------------------------------------------------*
         * Controllo di numericita di una stringa *
         *------------------------------------------------------*/
        function nonNumerico(n){
                var i;
                
                if(n=='') return false;
                for(i=0;i<n.length;i++){
                        if(isNaN(n.substr(i,1))) return true;
                }
                return false;
        }
                
        /*------------------------------------------------------*
         * Controllo del set di caratteri *
         *------------------------------------------------------*/
        
        function ctrlCharSet(c,tipo){
                
                var charSet,ind;
                        
                if(tipo=='a') // alfabetico
                        charSet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                if(tipo=='n') // numerico
                        charSet = '0123456789';
                if(tipo=='nv') // numerico con virgola
                        charSet = '0123456789,';
                if(tipo=='an') // alfabetico + numeri
                        charSet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
                if(tipo=='an+') // alfabetico + numeri e punteggiatura
                        charSet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,-_';
                if(tipo=='h') // controllo conforme a host
                        charSet = ' "£$%&\'()*+,-.0123456789;<>=?§_^çò`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
                if(tipo=='ib') // controllo conforme a host x info cliente bir
                        charSet = ' "£$%&\'()*+,-.0123456789;<>=?§_^çò`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/';
                if(tipo=='nf') // numeri di fattura
                        charSet = '0123456789-.';
                if(tipo=='cfe1') // codice fiscale eccezioni 1
                        charSet = 'LMNPQRSTUV0123456789';
                if(tipo=='cfe2') // codice fiscale eccezioni 2
                        charSet = 'LMNP0123';
                        
                for(ind=0;ind<c.length;ind++){
                 if( charSet.indexOf(c.charAt(ind))==-1) return false;
                }
                                
                return true;                        
        }
        /*------------------------------------------------------*
         * Sostituzione di caratteri *
         *------------------------------------------------------*/
        
        function substChar(c,o,n){
                
                var temp=c;
                                                
                while(temp.indexOf(o)!=-1){
                 temp = temp.replace(o,n);
                }
                                
                return temp;                        
        }
        /*------------------------------------------------------*
         * Normalizza lunghezza codici con zeri a sinistra *
         *------------------------------------------------------*/
        function normCod(c,n){
                var zz='000000000000000000000000000000';
                var cn= '';
                
                if(n<=c.length) return c;
                if(n>30) return c;
                
                cn = zz.substr(1,n-c.length)+c;
                return cn;
        }
        /*------------------------------------------------------*
         * Calcola il resto di n%m con m di lungh. arbitraria *
         *------------------------------------------------------*/
        function modulo(n,m){                // n e m stringhe e ritorno intero
                var ln,lm,ni,mi,it,nt,D,R;
                
                ln = n.length;
                lm = m.length;
                it = 0;
                
                if(!ctrlCharSet(n,'n') || !ctrlCharSet(m,'n')) return -1; // dati non numerici
                ni = parseInt(n,10);                                                // dividendo intero
                mi = parseInt(m,10);                                                // divisore intero
                if(ni < mi) return ni;                                                // divisore maggiore del dividendo
                if(ni == mi) return 0;                                                // divisore uguale al dividendo
                nt = n;                                                                                // dividendo da consumare (stringa)
                R = 0;                                                                                // resto
                
                while(nt.length>0){
                        D = R;                                                                        // riparto dal resto
                        D = D*10 + parseInt(nt.substr(0,1),10);        // isolo il dividendo parziale e lo
                        R = D % mi;                                                                // resto parziale
                        nt = nt.substring(1,nt.length);                        // tolgo D dal dividendo
                }
                
                return R;        
        }
                
        /*------------------------------------------------------*
         * Calcola check iban *
         *------------------------------------------------------*/
        function calcolaCheckIban(bban){        // iban di 27 char con lettere maiuscole
        
                var temp1='', temp2='', resto;
                                
                temp1 = bban + 'IT00';                        // metto i primi 4 char alla fine
                
                for(i=0; i<temp1.length; i++){                                                                        // Converto in soli numeri
                        if(ctrlCharSet(temp1.charAt(i),'n'))                                                // Carattere numerico:
                                temp2 = temp2 + temp1.charAt(i);                                                //         si riporta uguale
                        else                                                                                                                // Carattere alfabetico:
                                temp2 = temp2 + (temp1.charCodeAt(i) - 55).toString();        //          si converte come da algoritmo
                }
                resto = modulo(temp2,'97')                                                                                // Se il check mod 97 = 1 è ok
                        
                return modulo((98 - resto).toString(), '97').toString();                
        }
        
        /*------------------------------------------------------*
         * Controlla correttezza check iban *
         *------------------------------------------------------*/
        function ctrlCheckIban(iban){        // iban di 27 char con lettere maiuscole
                var temp1='', temp2='';
                
                temp1 = iban.substr(4,iban.legth) + iban.substr(0,4);                        // metto i primi 4 char alla fine
                
                for(i=0; i<temp1.length; i++){                                                                        // Converto in soli numeri
                        if(ctrlCharSet(temp1.charAt(i),'n'))                                                // Carattere numerico:
                                temp2 = temp2 + temp1.charAt(i);                                                //         si riporta uguale
                        else                                                                                                                // Carattere alfabetico:
                                temp2 = temp2 + (temp1.charCodeAt(i) - 55).toString();        //          si converte come da algoritmo
                }
                if(modulo(temp2,'97') == 1)                                                                                // Se il check mod 97 = 1 è ok
                        return true;
                else
                        return false;
        }
        /*------------------------------------------------------*
         * Calcolo CIN BBAN *
         *------------------------------------------------------*/
        function CalcolaCin(abi, cab, conto)
        {
                // costanti e variabili per calcolo pesi
                var numeri = '0123456789';
                var lettere = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ-. ';
                var listaPari = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28);
                var listaDispari = new Array(1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23,27,28,26);
                var c = new Array();
                var codice = '';
                var somma = 0;
                var k,i;
                // codice normalizzato
                codice = abi + cab + conto;
                // calcolo valori caratteri
         for (k=0; k<codice.length; k++){
                        i = numeri.indexOf(codice.charAt(k))
                        if (i<0)
                                i = lettere.indexOf(codice.charAt(k))
                                // se ci sono caratteri errati usciamo con un valore impossibile da trovare sul cin
                        if (i<0) return ('Error');
                        
                        if (modulo(k.toString(),2) == 0) {
                                // valore dispari
                                somma += listaDispari[i];
                        }
                        else{
                                // valore pari
                                somma += listaPari[i];
                        }
                }
                
         return lettere.substr(modulo(somma.toString(),'26'),1);
        }
                
        
</SCRIPT>
</HEAD>
<BODY onLoad="entrata();" leftmargin=0 topmargin=0 rightmargin=0>
<TABLE background="" border=0 cellPadding=0 cellSpacing=0 width="597" align="center">
<TR>
        <TD align=center bgcolor=#edf9ff class=testa><strong>CALCOLO IBAN DA COORDINATE</strong></TD>
</TR>
</TABLE>
<TABLE border=0 cellPadding=2 cellSpacing=0 width="597" align="center">
<FORM name=frmCalcoloIban OnSubmit="return false;">
        <TR>
                <TD align=left class=costantebody width="40%"><FONT face="MS Sans Serif" size=1><STRONG>Coordinate bancarie:</STRONG></FONT> </TD>
                <TD class=costantebody width="60%"> </TD>
        </TR>
        <TR>
                <TD colspan=2 class=costantebody2>
                         <br>
                </TD>
        </TR>
        <TR>
                <TD align=right class=costantebody2 width="100%" colspan=2>
                        <TABLE border=0 cellPadding=2 cellSpacing=0 width="100%">
                                <TR>
                                        <TD class=costantebody2 width="6%" align=right>                                                
                                                
                                        </TD>
                                        <TD class=costantebody2 width="77%">
                                                <INPUT id=NazAcc name=NazAcc class=inputtext2 style="WIDTH: 19px" value="IT" maxlength="2" disabled>
                                                Cin Iban <INPUT id=CiIAcc name=CiIAcc class=inputtext2 style="WIDTH: 21px" value="" maxlength="2">
                                                Cin <INPUT id=CinAcc name=CinAcc class=inputtext2 style="WIDTH: 14px" value="" maxlength="1">
                                                Abi <INPUT id=AbiAcc name=AbiAcc class=inputtext2 style="WIDTH: 42px" value="" maxlength="5">
                                                Cab <INPUT id=CabAcc name=CabAcc class=inputtext2 style="WIDTH: 42px" value="" maxlength="5">
                                                Conto <INPUT id=CC1Acc name=CC1Acc class=inputtext2 style="WIDTH: 100px" value="" maxlength="12">
                                        </TD>
                                        <TD class=costantebody2 width="17%" align=right>                                                
                                        </TD>
                                </TR>
                                <TR>
                                        <TD colspan=3 class=costantebody2>
                                                 <br>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD class=costantebody2 align=right>                                                
                                                Iban
                                        </TD>
                                        <TD class=costantebody2>
                                                <INPUT id=IbanAcc name=IbanAcc class=inputtext7 style="WIDTH: 230px" value="" maxlength="27">
                                        </TD>
                                        <TD class=costantebody2 align=right>                                                
                                                
                                        </TD>
                                </TR>
                        </TABLE>
                </TD>
        </TR>
        <TR>
                <TD colspan=2 class=costantebody2>
                         <br> <br>
                         <br> <br>
                </TD>
        </TR>
        <TR>
                <TD align=left class=costantebody2 width="20%" colspan=2>
                        <INPUT class=inputtext5 id=Message name=Message readOnly style="WIDTH: 594px" value="">
                </TD>
        </TR>        
        
        <TR>
        <TD align=right class=titolo width="100%" colspan=2>
                        <TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
                                <TR>
                                        <TD class=titolo width="22%" align=left>
                                                <INPUT class=button name=btnAvanti type=submit
                                                                STYLE ="width: 90; cursor:hand;"
                                                                value="PULISCI"
                                                                onclick="pulisci();">
                                        </TD>
                                        <TD class=titolo width="22%" align=middle>
                                        </TD>
                                        <TD class=titolo width="34%" align=middle>
                                        </TD>
                                        <TD class=titolo width="22%" align=right>
                                                <INPUT class=button name=btnAvanti type=submit
                                                                STYLE ="width: 90; cursor:hand;"
                                                                value="CALCOLA"
                                                                onclick="ctrlParam();">
                                        </TD>
                                </TR>
                        </TABLE>
</TD>
</TR>
</FORM>
</TABLE>
</BODY>
</HTML>