// Controlli per codice IBAN
function checkIBAN(frm,Fname) 
{ 
	var someError = false
	var errMsg =""
	var st = new Array()
	var frmPass='['+frm+']'
	if (frmPass.indexOf('[[object') == 0)
	{
		FCtr=frm.elements[Fname];
	}
	else
	{
		FCtr=document.forms[frm].elements[Fname];
	}
	var x = FCtr.value
	
	x = x.toUpperCase() 
  	FCtr.value = x	
	x=noBlanks(x)
	st[0] = x.substring(0,2)
	countryCode=st[0]
	st[1] = x.substring(2,4)
	st[2] = x.substring(4,8)
	st[3] = x.substring(8,x.length)
	sCheck=st[2] + st[3] + st[0] +st[1]

	if(!alph(st[0])) 
  {
    someError=true; 
    errMsg = "\nI primi due caratteri devono essere alfabetici"
  }
	if(!num(st[1]))
  {
    someError=true; 
    errMsg +="\nI caratteri tre e quattro devono essere numeri"
  }
  if(!alphnum(sCheck)) 
  {
    someError=true; 
    errMsg +="\nSono consentiti solo numeri dallo 0 al 9 e lettere dalla A alla Z"
  }
	if(countryCode=="GB")
  { 
    if(!alph(st[2])) 
    {
      someError=true; 
      errMsg +="\nSe il codice del paese è GB, i caratteri 5-8 devono essere alfabetici"
    }
    gbLength = sCheck.length
		if(gbLength!=22) 
    {
      someError=true; 
        errMsg +="\nSe il codice del paese è GB, l\'IBAN deve avere 22 caratteri"
    }
	}
	if(!someError)
  {
    if (!checkDigitSum(sCheck)) 
    {
      FCtr.focus();
      return true
    }
  }
	else
  {
    alert("Codice IBAN non corretto\n" + errMsg)
    FCtr.focus();
    return true
  }
}  

// Rimuovi tutti gli spazi dal testo passato nella variabile X
function noBlanks(x) 
{ 
	var newX=""
	for (var i=0; i<x.length;++i)
  {
    if(x.charAt(i)!=" ") newX=newX+x.charAt(i) 
  }
	return newX
}

// Controllo che i caratteri siano compresi nell'intervallo A-Z
function alph(x)
{  
	var xNew=x.toUpperCase()
	for (var i=0; i<xNew.length; ++i )
  { 
    var a = xNew.charCodeAt(i)
		if (a<65 || a>90) return false 
  }
	return true
}

// Controllo che i caratteri siano compresi nell'intervallo 0 - 9
function num(x) 
{   
	for (var i=0; i<x.length; ++i ) 
  { 
    var a = x.charCodeAt(i)  
		if ( a<48 || a>57 ) 
    {
      return false
    }
  }
	return true
}

// Controllo che i caratteri siano alfanumerici compresi negli intervalli A - Z o 0 - 9
function alphnum(x) 
{
	for (var i=0; i<x.length; ++i )
  { 
    var c = x.charAt(i)  
	  if (!alph(c) && !num(c)) 
    { 
      return false
    } 
  }
	return true
}

// Sostituisce a ogni lettera l'equivalente numerico per creare la nuova strinca newS 
function checkDigitSum(s) 
{
	var n	
	newS = ""
	for (var i = 0; i<s.length; ++i ) 
  {
		var a = s.charCodeAt(i)	
		if (a>=65 & a<=90) 
    {
      n = a - 55
    }
		else
    {
      n = s.charAt(i)
    }
		newS = newS + n 
  }
	// check digit sum by long division, starting with first two digits
	var newM = parseInt(newS.substring(0,2),10)
	var r = newM % 97
	for (i = 2; i <newS.length; ++i) 
  {
		newM = 10 * r + parseInt(newS.substring(i, i+1),10)
		r = newM % 97
  }
	if (r==1)
  {
    //alert("OK!")
    return true
  }
	else
  {
    alert("Codice IBAN non corretto")
    return false
  }		
}

