﻿// JScript File

function CheckABN(source, arguments)
{

    var abn,abnSansSpace,i,digits,total,results;    
    var weights = new Array(10,1,3,5,7,9,11,13,15,17,19);
    
    abn = arguments.Value;
    
     // Get the number, parse out any non digits, should have 11 left
     abnSansSpace = abn.replace(/\D/g, "");
     if(abnSansSpace.length != 11) 
     {
         arguments.IsValid = false;
         return;   // invalid abn
     }
     
      // Convert to array of digits
      digits = new Array(11);
        for(i=0; i<11; i++)
                digits[i] = Number(abnSansSpace.charAt(i));
     
     //deduct the first number by 1
     digits[0] = digits[0] - 1;
     
     //results array
     results = new Array(11);
     
     //multiply the array elements with each other and store the results
     for(i=0;i<11;i++)
     {
        results[i]=digits[i] * weights[i];
     }
     
     //sum the results      
     total = 0;
     for(i=0; i<11; i++)     total += results[i];
     
     //divide the results by 89 and make sure there is no reminder      
     if( total % 89 == 0 )   {
         arguments.IsValid = true;
         return;    // valid abn
       }
       else  {
         arguments.IsValid = false;
         return;   // invalid abn
     }
     
}

