| 58 | | if (this.isValidCheckDigit(this.text) == false) { |
|---|
| 59 | | //the user didn't input an identifier with a valid check digit |
|---|
| 60 | | this.hideHeaderRow(); |
|---|
| 61 | | var img = this.getProblemImage(); |
|---|
| 62 | | var tmp = " <img src='" + img.src + "' title='" + img.title + "' /> " + this.invalidCheckDigitText + this.text; |
|---|
| 63 | | patients.push(tmp); |
|---|
| 64 | | patients.push(this.noPatientsFoundText); |
|---|
| 65 | | patients.push(this.searchOnPatientNameText); |
|---|
| 66 | | } |
|---|
| 67 | | else { |
|---|
| 68 | | //the user did input a valid identifier, but we don't have it |
|---|
| 69 | | patients.push(this.noPatientsFoundText); |
|---|
| 70 | | patients.push(this.searchOnPatientNameText); |
|---|
| 71 | | if (this.showAddPatientLink) |
|---|
| 72 | | patients.push(this.addPatientLink); |
|---|
| 73 | | } |
|---|
| | 58 | patients.push(this.noPatientsFoundText); |
|---|
| | 59 | patients.push(this.searchOnPatientNameText); |
|---|
| | 60 | if (this.showAddPatientLink) |
|---|
| | 61 | patients.push(this.addPatientLink); |
|---|
| 207 | | |
|---|
| 208 | | isValidCheckDigit: function(value) { |
|---|
| 209 | | if (value == null) return false; |
|---|
| 210 | | |
|---|
| 211 | | if (value.length < 3 || value.indexOf('-') != value.length - 2) |
|---|
| 212 | | return false; |
|---|
| 213 | | |
|---|
| 214 | | var checkDigit = value.charAt(value.length - 1).valueOf(); |
|---|
| 215 | | |
|---|
| 216 | | var valueWithoutCheckDigit = value.substr(0, value.length - 2); |
|---|
| 217 | | |
|---|
| 218 | | return (checkDigit == this.getCheckDigit(valueWithoutCheckDigit)); |
|---|
| 219 | | }, |
|---|
| 220 | | |
|---|
| 221 | | getCheckDigit: function(value) { |
|---|
| 222 | | |
|---|
| 223 | | // allowable characters within identifier |
|---|
| 224 | | var validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVYWXZ_"; |
|---|
| 225 | | |
|---|
| 226 | | if (this.stripCharsInBag(value, validChars) != "") { |
|---|
| 227 | | //Invalid character in string |
|---|
| 228 | | } |
|---|
| 229 | | |
|---|
| 230 | | // remove whitespace |
|---|
| 231 | | value = value.replace([ ], ""); |
|---|
| 232 | | // convert to uppercase |
|---|
| 233 | | value = value.toUpperCase(); |
|---|
| 234 | | |
|---|
| 235 | | // running total |
|---|
| 236 | | var sum = 0; |
|---|
| 237 | | |
|---|
| 238 | | //alert("sum: " + sum + " ch: " + ch + " digit: " + digit + " weight: " + weight); |
|---|
| 239 | | |
|---|
| 240 | | // loop through digits from right to left |
|---|
| 241 | | for (var i = 0; i < value.length; i++) { |
|---|
| 242 | | //set ch to "current" character to be processed |
|---|
| 243 | | var ch = value.charCodeAt(value.length - i - 1); |
|---|
| 244 | | var digit = ch - 48; |
|---|
| 245 | | var weight; |
|---|
| 246 | | if (i % 2 == 0) { |
|---|
| 247 | | // for alternating digits starting with the rightmost, we use our formula |
|---|
| 248 | | // this is the same as multiplying x 2 and adding digits together for values |
|---|
| 249 | | // 0 to 9. Using the following formula allows us to gracefully calculate a |
|---|
| 250 | | // weight for non-numeric "digits" as well (from their ASCII value - 48). |
|---|
| 251 | | weight = (2 * digit) - Math.floor(digit / 5) * 9; |
|---|
| 252 | | } |
|---|
| 253 | | else { |
|---|
| 254 | | // even-positioned digits just contribute their ascii value minus 48 |
|---|
| 255 | | weight = digit; |
|---|
| 256 | | } |
|---|
| 257 | | // keep a running total of weights |
|---|
| 258 | | sum = sum + weight; |
|---|
| 259 | | } |
|---|
| 260 | | // avoid sum less than 10 (if characters below "0" allowed, this could happen) |
|---|
| 261 | | sum = Math.abs(sum) + 10; |
|---|
| 262 | | // check digit is amount needed to reach next number divisible by ten |
|---|
| 263 | | return (10 - (sum % 10)) % 10; |
|---|
| 264 | | |
|---|
| 265 | | }, |
|---|
| 266 | | |
|---|
| | 182 | |
|---|