//  If an IBAN is typed into the upper box, the input is checked a character at a time as each characters is typed in.
//  When the user asks for the complete IBAN to be checked, all blocks are checked again in case any blocks have 
// been changed since they were originally checked.
// If an IBAN is typed or copied into the lower box, the string is checked when the User clicks the Check box.
// When an IBAN is entered into the upper box, the lower box is cleared, and vice versa.


var countryCode_nrb
var gbLength_nrb

function clear1_nrb() { // clear upper boxes and results
	for (var i=1; i<=7; i++) { document.getElementById("nr"+i+"_nrb").value="" }
	document.getElementById("resultDivNRB").style.backgroundImage = ""
}
function clear2_nrb() { // clear lower box and results
	document.getElementById("NRBAll").value=""
	document.getElementById("resultDivNRB").style.backgroundImage = ""
}
function clearFocus1_nrb() { // clear upper boxes and set focus
	clear1_nrb()
	document.getElementById("nr0_nrb").focus()
}
function clearFocus2_nrb() { // clear lower box and set focus
	clear2_nrb()
	document.getElementById("NRBAll").focus()
}
function checkBlock0_nrb(x) {  // Check all chars in Block 0 are alphabetic
	clear2_nrb()	// clear lower box
	if (!alph(x))  {alert("Dwa pierwsze znaki muszą być literami A - Z");
	                document.getElementById("nr0_nrb").select(); return false}	
	if(x.length==2) {document.getElementById("nr0_nrb").value=x.toUpperCase();
	                 countryCode_nrb=document.getElementById("nr0_nrb").value;
	                 document.getElementById("nr1_nrb").focus()}  // move to next block 
	return true  
}
function checkBlock1_nrb(x) {  // Check that all chars in Block 1 are numeric
	clear2_nrb()	// clear lower box
	if (!num(x)) {alert("Dwa znaki w bloku drugim muszą być cyframi 0 - 9")
	              document.getElementById("nr1_nrb").select(); return false}		
	if(x.length==2) {document.getElementById("nr2_nrb").focus()}  // move to next block 
	return true
}
function checkBlockN_nrb(n,x) {  // Check that all chars in Blocks 2+ are alphanumeric
	clear2_nrb()	// clear lower box
	if (!alphnum(x)) {alert("Znaki w bloku nr " +(n+1) +" muszą być literami A - Z lub cyframi 0-9");
		           document.getElementById("nr"+n+"_nrb").select(); return false}	
	if(x.length==4) {document.getElementById("nr"+n+"_nrb").value = x.toUpperCase()
				if(n==2 && countryCode_nrb=="GB") { if (!alph(x))
					                                 { alert("Jeśli kodem kraju jest GB, znaki w bloku nr 3 muszą być literami")
													   document.getElementById("nr2_nrb").select()
													   return false} }
				if (n < 7) {document.getElementById("nr"+(n+1)+"_nrb").focus()}} // move to next block 
	return true
}
function check_nrb() { // check whichever box has non-empty field
	if(document.getElementById("nr1_nrb").value !="") {checkUpperBox_nrb(); return}
	if(document.getElementById("NRBAll").value !="") {checkLowerBox_nrb(); return}
	alert("Musisz wprowadzić numer NRB")
}
function checkUpperBox_nrb() { // check all blocks in upper box.  If OK, check digit sum
                           // all blocks must have right number of characters.  lastBlock can be < 4 char
	var someError = false
	var sCheck =""
	var lastBlock = 0
	var st = new Array()
	for (var i=0; i<=7; ++i ) { st[i] = document.getElementById("nr"+i+"_nrb").value;
                                                  if (st[i].length > 0) {lastBlock = i} }
// Check each block for numbers of characters and validity
	if (st[0].length<2) {alert("Blok nr 1 musi zawierać dwie litery"); document.getElementById("nr0_nrb").select(); return}
	if(!checkBlock0_nrb(st[0])) {document.getElementById("nr0_nrb").select(); return}
	if (st[1].length< 2) {alert("Blok nr 2 musi zawierać dwie cyfry"); document.getElementById("nr1_nrb").select(); return}
	if(!checkBlock1_nrb(st[1])) {document.getElementById("nr1_nrb").select(); return}
	for (i=2; i<=lastBlock; ++i) { if(st[i].length <4 && i<lastBlock) { alert("Blok nr " +(i+1) +" musi zawierać cztery znaki");
	   		           document.getElementById("nr"+i+"_nrb").select(); return }
			           if(!checkBlockN_nrb(i,st[i])) {document.getElementById("nr"+n+"_nrb").select(); return} }
// check last block
	st[lastBlock]=st[lastBlock].toUpperCase()
	document.getElementById("nr"+lastBlock+"_nrb").value=st[lastBlock]
	if(!checkBlockN_nrb(lastBlock,st[lastBlock])) {document.getElementById("nr"+lastBlock+"_nrb").select(); return}

//  create code to check in format: blocks 2, 3, 4, ..  + block 0 + block 1
	for (i=2; i<=lastBlock; ++i) {sCheck += document.getElementById("nr"+i+"_nrb").value}
							sCheck += document.getElementById("nr0_nrb").value + document.getElementById("nr1_nrb").value

// check total length if countryCode=GB
	if(countryCode_nrb=="GB") { gbLength_nrb = sCheck.length
							if(gbLength_nrb!=22) {alert("Jeśli kodem kraju jest GB, numer NRB musi składać się z 22 znaków. Ten numer NRB ma " + gbLength_nrb + " znaków")
							                  return} 
	}
	if(sCheck.length!=28) {
		someError=true; errMsg ="\nKod musi składać się z 26 znaków"			
	}	
	if(!someError) {checkDigitSum_nrb(sCheck)}
	else {alert("Format numeru NRB jest nieprawidłowy\n" + errMsg)}	 

	
} 

function checkLowerBox_nrb() { // all-in-one check on lower text box.
                           // Any blank spaces are removed before the check is done 
                           // nError indicates which error message or combination of messages to display
	var someError = false
	var errMsg =""
	var st = new Array()
	var x = document.getElementById("NRBAll").value
	var nr0_nrb = document.getElementById("nr0_nrb").value;
	x=nr0_nrb.toUpperCase() + x.toUpperCase()
	
	x=noBlanks(x)
	st[0] = x.substring(0,2)
	countryCode_nrb=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 = "\nDwa pierwsze znaki muszą być literami A - Z"}
	if(!num(st[1])) {someError=true; errMsg +="\nWszystkie znaki muszą być cyframi"}
	if(!alphnum(sCheck)) {someError=true; errMsg +="\nDozwolone są tylko litery A-Z i cyfry 0-9"}
	if(countryCode_nrb=="GB"){ if(!alph(st[2])) {someError=true; errMsg +="\nJeśli kodem kraju jest GB, znaki 5-8 muszą być literami"}
						   gbLength_nrb = sCheck.length
						   if(gbLength_nrb!=22) {someError=true; errMsg +="\nJeśli kodem kraju jest GB, numer NRB musi składać się z 22 znaków"}
						   }
	gbLength = sCheck.length	
	if(gbLength!=28) {
		someError=true; errMsg +="\nKod musi składać się z 26 znaków"			
	}	
	if(!someError) {checkDigitSum_nrb(sCheck)}
	else {alert("Format numeru NRB jest nieprawidłowy\n" + errMsg)}	 
}  

function checkDigitSum_nrb(s) {// replace each letter by numerical equivalent to create new string newS
	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
	}
	
	control_sum = s.substring(s.length - 2);
	
	if(control_sum == 99 || control_sum =='01' || control_sum == '00'){
		document.getElementById("resultDivNRB").style.backgroundImage = "url(/systems/kir/_gfx_/webdesign/nieok.gif)"
	}
	else if (r==1) {
		document.getElementById("resultDivNRB").style.backgroundImage = "url(/systems/kir/_gfx_/webdesign/ok.gif)"
	}
	else {
		document.getElementById("resultDivNRB").style.backgroundImage = "url(/systems/kir/_gfx_/webdesign/nieok.gif)"
	}
}
function noBlanks(x) { // remove blanks from text in lower box before checking
	var newX=""
	for (var i=0; i<x.length;++i) {if(x.charAt(i)!=" ") newX=newX+x.charAt(i) }
	return newX
}
function alph(x) { // Check if each character in string x is A-Z 
	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
}
function num(x) {  // Check if each character in string x is 0 - 9 
	for (var i=0; i<x.length; ++i ) { var a = x.charCodeAt(i)  
		             if ( a<48 || a>57 ) {return false} }
	return true
}
function alphnum(x) { // check if each character in string x is A-Z or 0-9
	for (var i=0; i<x.length; ++i ) { var c = x.charAt(i)  
	             if (!alph(c) && !num(c)) { return false} }
	return true
}

function copyNrb() {
	var number ='';
	for (var i=1; i<=7; i++) { 
		number = number+""+document.getElementById("nr"+i+"_nrb").value; 
	}
	document.getElementById('NRBAll').value = number;
}
