var IBAN_isIE = (window.navigator.userAgent.indexOf("IE") >= 0);


var IBAN_IT_BBAM_NumSet	  = '0123456789';
var IBAN_IT_BBAM_AsciiSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var IBAN_IT_BBAM_OddMap	  = [
	 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
];

function IBAN_Get_IT_BBAM_Check(s) {
	var sum = 0;

	var i;
	for(i = 0; i < s.length; ++i) {
		var c = s.substr(i, 1);
		var v = IBAN_IT_BBAM_NumSet.indexOf(c);
		if(v < 0)
			v = IBAN_IT_BBAM_AsciiSet.indexOf(c);

		if(i%2 == 0)
			v = IBAN_IT_BBAM_OddMap[v];

		sum += v;
	}

	return(IBAN_IT_BBAM_AsciiSet.substr(sum%26, 1));
}


var IBAN_DigitSet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function IBAN_ZeroBigInt() {
	var a = new Array();
	a[0] = 0;
	return(a);
}

function IBAN_BigIntMulBy10Or100AddByX(a, x) {
	var mp = (x > 10 ? 100 : 10);
	var i;

	for(i = 0; i < a.length; ++i) {
		x += mp*a[i];
		a[i] = x&0xffff;

		x >>= 16;
	}

	if(x != 0)
		a[i] = x;
}

function IBAN_StringToBigInt(s) {
	var a = IBAN_ZeroBigInt();

	var i;
	for(i = 0; i < s.length; ++i) {
		var c = IBAN_DigitSet.indexOf(s.substr(i, 1));
		IBAN_BigIntMulBy10Or100AddByX(a, c);
	}

	return(a);
}

function IBAN_BigIntModBy97(a) {
	var m = 0;

	var i;
	for(i = a.length - 1; i >= 0; --i)
		m = ((m<<16) + a[i]) % 97;

	return(m);
}


function IBAN_LeftPad(s, n) {
	while(s.length < n)
		s = '0' + s;

	return(s);
}


function IBAN_IE_PutText(e, s)		{ e.innerText = s; }
function IBAN_NonIE_PutText(e, s)	{ e.innerHTML = s; }

var IBAN_PutText = (IBAN_isIE ? IBAN_IE_PutText : IBAN_NonIE_PutText);


function IBAN_GetForm()
{ return(document.forms['frmIBAN']); }

function IBAN_GetFormElement(nm)
{ return(IBAN_GetForm().elements[nm]); }

function IBAN_GetField(nm) {
	var e = IBAN_GetFormElement(nm);
	return(e.value.replace(/^[ \t]*/, '').replace(/[ \t]*$/, ''));
}


var IBAN_idRefresh = null;

function IBAN_OnRefresh() {
	IBAN_Refresh();
	IBAN_ClearRefresh();
}

function IBAN_PostRefresh() {
	if(IBAN_idRefresh == null)
		IBAN_idRefresh = window.setTimeout(IBAN_OnRefresh, 10);
}

function IBAN_ClearRefresh() {
	if(IBAN_idRefresh != null) {
		window.clearTimeout(IBAN_idRefresh);
		IBAN_idRefresh = null;
	}
}


function IBAN_Clear() {
	var e;

	if(IBAN_GetField('idNation') == 'IT') {
		e = IBAN_GetForm().elements['idBBAN'];
		e.value = '';
	}

	e = document.getElementById('idIBAN');
	e.innerHTML = '&nbsp;';
}

function IBAN_GetNation() {
	var idxNation = IBAN_GetForm().elements['idNation'].selectedIndex;
	if(idxNation < 0)
		return(null);;

	return(IBAN_GetForm().elements['idNation'].options[idxNation].value);
}

function IBAN_Refresh() {
	var nation = IBAN_GetNation();
	if(nation == null)
		IBAN_Clear();

	var e;
	var bban = '';
	if(nation == 'IT') {
		e = document.getElementById('msgABI');
		var abi = IBAN_GetField('idABI');
		if(
			abi == null || abi.length == 0	||
			abi.search(/[^0-9]/) >= 0	||
			abi.length > 5
		)
			e.style.fontWeight = 'bold';
		else {
			e.style.fontWeight = 'normal';
			abi = IBAN_LeftPad(abi, 5);
			bban += abi;
		}

		e = document.getElementById('msgCAB');
		var cab = IBAN_GetField('idCAB');
		if(
			cab == null || cab.length == 0	||
			cab.search(/[^0-9]/) >= 0	||
			cab.length > 5
		)
			e.style.fontWeight = 'bold';
		else {
			e.style.fontWeight = 'normal';
			cab = IBAN_LeftPad(cab, 5);
			bban += cab;
		}

		e = document.getElementById('msgCC');
		var cc = IBAN_GetField('idCC');
		cc = cc.toUpperCase();
		if(
			cc == null || cc.length == 0	||
			cc.search(/[^0-9A-Z]/) >= 0	||
			cc.length > 12
		)
			e.style.fontWeight = 'bold';
		else {
			e.style.fontWeight = 'normal';
			cc = IBAN_LeftPad(cc, 12);
			bban += cc;
		}

		if(bban.length > 0)
			bban = IBAN_Get_IT_BBAM_Check(bban) + bban;
	} else {
		bban = IBAN_GetField('idBBAN');
		bban = bban.toUpperCase();
	}

	e = IBAN_GetForm().elements['idBBAN'];
	e.value = bban;
	IBAN_OnBBANChange();
}


function IBAN_OnBBANChange() {
	var bban = IBAN_GetField('idBBAN');
	if(bban.length > 0) {
		var nation = IBAN_GetNation();
		var a = IBAN_StringToBigInt(bban + nation + '00');
		var m = (98 - IBAN_BigIntModBy97(a)) % 97;

		var s =
			nation				+
			Math.floor(m / 10).toString()	+
			(m % 10).toString()		+
			bban
		;

		var iban = '';
		for(i = 0; i < s.length; ++i) {
			iban += s.substr(i, 1);
			if(((i + 1)%4) == 0)
				iban += ' ';
		}

		e = document.getElementById('idIBAN');
		IBAN_PutText(e, iban);
	} else
		IBAN_Clear();
}


function IBAN_OnNationChange() {
	var e;

	IBAN_Clear();

	var nation = IBAN_GetNation();
	if(nation == 'IT') {
		IBAN_GetFormElement('idABI').disabled = false;
		IBAN_GetFormElement('idCAB').disabled = false;
		IBAN_GetFormElement('idCC').disabled = false;

		IBAN_GetFormElement('idBBAN').disabled = true;
	} else {
		e = IBAN_GetFormElement('idABI');
		e.value = '';
		e.disabled = true;

		e = IBAN_GetFormElement('idCAB');
		e.value = '';
		e.disabled = true;

		e = IBAN_GetFormElement('idCC');
		e.value = '';
		e.disabled = true;

		IBAN_GetFormElement('idBBAN').disabled = false;
	}

	var bban_len = IBAN_BBANLengthByNation[nation];
	if(bban_len == null)
		bban_len = 30;	/* Maximum */

	e = IBAN_GetFormElement('idBBAN');
	e.size	  = bban_len;
	e.maxLength = bban_len;

	IBAN_PostRefresh();
}


var IBAN_GetKeyCode = (
	IBAN_isIE
?
	function(e)	{ return(e.keyCode); }
:
	function(e)	{ return(e.which); }
);

var IBAN_SetKeyCode = (
	IBAN_isIE
?
	function(e, c)	{ e.keyCode = c; }
:
	function(e, c)	{ /* no way in non-IE */ }
);

function IBAN_OnNumKeyPress(e, o) {
	var keyCode = IBAN_GetKeyCode(e);
	if(
		keyCode != 0 &&
		"0123456789\b\t\r\n".indexOf(String.fromCharCode(keyCode)) < 0
	)
		return(false);

	IBAN_PostRefresh();
	return(true);
}

function IBAN_OnNumLetKeyPress(e, o) {
	var keyCode = IBAN_GetKeyCode(e);
	if(
		keyCode != 0 &&
		"0123456789\b\t\r\n".indexOf(String.fromCharCode(keyCode)) < 0
	) {
		var c = String.fromCharCode(keyCode).toUpperCase();
		if(IBAN_IT_BBAM_AsciiSet.indexOf(c) < 0)
			return(false);

		IBAN_SetKeyCode(e, c.charCodeAt(0));
	}

	IBAN_PostRefresh();
	return(true);
}

var IBAN_BBANLengthByNation = new Array();
IBAN_BBANLengthByNation['AD'] = 24;
IBAN_BBANLengthByNation['AT'] = 20;
IBAN_BBANLengthByNation['BE'] = 16;
IBAN_BBANLengthByNation['CY'] = 28;
IBAN_BBANLengthByNation['CZ'] = 24;
IBAN_BBANLengthByNation['DK'] = 18;
IBAN_BBANLengthByNation['EE'] = 20;
IBAN_BBANLengthByNation['FI'] = 18;
IBAN_BBANLengthByNation['FR'] = 27;
IBAN_BBANLengthByNation['DE'] = 22;
IBAN_BBANLengthByNation['GI'] = 23;
IBAN_BBANLengthByNation['GR'] = 27;
IBAN_BBANLengthByNation['HU'] = 28;
IBAN_BBANLengthByNation['IS'] = 26;
IBAN_BBANLengthByNation['IE'] = 22;
IBAN_BBANLengthByNation['IT'] = 27;
IBAN_BBANLengthByNation['LV'] = 21;
IBAN_BBANLengthByNation['LT'] = 20;
IBAN_BBANLengthByNation['LU'] = 20;
IBAN_BBANLengthByNation['NL'] = 18;
IBAN_BBANLengthByNation['NO'] = 15;
IBAN_BBANLengthByNation['PL'] = 28;
IBAN_BBANLengthByNation['PT'] = 25;
IBAN_BBANLengthByNation['SK'] = 24;
IBAN_BBANLengthByNation['SI'] = 19;
IBAN_BBANLengthByNation['SP'] = 24;
IBAN_BBANLengthByNation['SE'] = 24;
IBAN_BBANLengthByNation['CH'] = 21;
IBAN_BBANLengthByNation['GB'] = 22;

window.onload = IBAN_Refresh;
