/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Validator extensions for IBAN (International Bank Account Number) for italian bank accounts (BBAN of 23 characters).<br/>
* Definition is <a href="http://it.wikipedia.org/wiki/International_Bank_Account_Number">here</a>
* @for Validator
* @namespace jsbeans
* @static
* */
jsbeans.Validator.iban_it = {
/**
* Default messages for Validator.iban_it extension
* @property iban_it.messages
* @type JSON
* @static
* */
messages: {
en: "is not a valid italian International Bank Account Number (IBAN)",
it: "non e' un IBAN valido"
},
/**
* Checks for a valid italian IBAN in both format:<br/>
* IT 12 L 12345 12345 123456789012<br/>
* and<br/>
* IT12L1234512345123456789012
* @method assertIban_it
* @param input {DOM} the input to validate
* @param [controlDigitsCheck] {boolean} if true checks if control digits used in current iban are the same as the aspected ones (European CIN)
* @return {boolean} true if <code class="param">input</code>'s value is a valid italian IBAN
* @static
* */
assertIban_it: function(/*DOM*/obj) {
var val = (obj.value || "").replace(/^\s+|\s+$/g, "");
// IT 12 L 12345 12345 123456789012 | IT12L1234512345123456789012
var res = (val == "" || /^((IT|it)[ ][0-9]{2}[ ][a-zA-Z][ ][0-9]{5}[ ][0-9]{5}[ ][0-9a-zA-z]{12})|((IT|it)[0-9]{2}[a-zA-Z][0-9]{5}[0-9]{5}[0-9a-zA-z]{12})$/.test(val));
if (res && ((arguments[1] && arguments[1] == 'true') || false)) {
// skip spaces and check
return jsbeans.Validator.iban_it._check(val.split(" ").join(""));
}
return res;
},
/**
* Checks if iban is a valid one using its control digits.<br/>
* Function based on code found here: http://www.gpiattoni.altervista.org/CalcoloIBAN.html
* @method _check
* @param iban {String} the iban to check
* @return {boolean} true if control digits of the passed iban are correct.
* @private
* @static
*/
_check: function(iban) {
// toUpperCase otherwise it hangs in infinite loop
iban = iban.toUpperCase();
var pBBAN = iban.substr(4);
var checkDigits = iban.substr(2, 2);
var r0 = function (s, l){
var c = "" + s;
while (c.length < l) {
c = "0" + c;
}
return(c);
}
var vIBAN = pBBAN + "IT00";
var w = "";
for (var i = 0; i < 27; i++){
w += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(vIBAN.charAt(i));
}
while (w.length > 2) {
w = parseInt(w.substr(0,7), 10) % 97 + w.substr(7);
}
var checkDigitsCalculated = r0(98 - parseInt(w, 10) % 97, 2);
//var ibanFinal = ("IT" + checkDigitsCalculated + pBBAN);
return checkDigits == checkDigitsCalculated;
}
};