var ocodeUtil = {
isvalidentpcode: function(code) {
code = code.toUpperCase();
var ws = [ 3, 7, 9, 10, 5, 8, 4, 2 ];
var str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var reg = /^([0-9A-Z]){8}-[0-9|X]$/;
if (!reg.test(code)) {
return false;
}
var sum = 0;
for ( var i = 0; i < 8; i++) {
sum += str.indexOf(code.charAt(i)) * ws[i];
}
var c9 = 11 - (sum % 11);
if(c9 == 10){
c9 = 'X';
}else if(c9 == 11){
c9='0';
}
if(c9 != code.charAt(9)){
return false;
}else{
return true;
}
},
checkBusinessCode : function(orgCode) {
orgCode = orgCode.toUpperCase();
if (orgCode.length != 18) {
return false;
}
// 字符集数组
var fin_array = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'T', 'U', 'W', 'X', 'Y' ];
// 加权数组
var i_array = [ 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
30, 28 ];
// 结果数组
var val_array = [];
// 相乘数组
var ch_array = [];
// Sigma
var sigma = 0;
// 部分IE浏览器不支持数组中的indexOf函数, 故加上此代码;
if (!Array.indexOf) {
Array.prototype.indexOf = function(obj) {
for ( var i = 0; i < this.length; i++) {
if (this[i] == obj) {
return i;
}
}
return -1;
};
}
// 转换成字符集的值
for ( var i = 0; i < 17; i++) {
val_array[i] = fin_array.indexOf(orgCode.charAt(i));
}
// 校验是否包含字符集之外的字符
for ( var i = 0; i < 17; i++) {
if (val_array[i] == -1) {
return false;
// return "包含非法字符";
}
}
// 生成加权结果
for ( var i = 0; i < 17; i++) {
ch_array[i] = val_array[i] * i_array[i];
}
// 求出Sigma
for ( var i = 0; i < 17; i++) {
sigma = sigma + ch_array[i];
}
// 求出校验位
var vali = fin_array[31 - sigma % 31];
if((31 - sigma % 31) == 31){
vali = '0';
}
// 查看校验码是否正确
if (orgCode.charAt(17) == vali) {
return true;
// return "正确";
} else {
return false;
// return "校验码错误: 应该为: " + vali;
}
},
isValidBusCode : function(busCode) {
var ret = false;
if (busCode.length == 15) {
var s = [];
var p = [];
var a = [];
var m = 10;
p[0] = m;
for ( var i = 0; i < busCode.length; i++) {
a[i] = parseInt(busCode.substring(i, i + 1), m);
s[i] = (p[i] % (m + 1)) + a[i];
if (0 == s[i] % m) {
p[i + 1] = 10 * 2;
} else {
p[i + 1] = (s[i] % m) * 2;
}
}
if (1 == (s[14] % m)) {
ret = true;
} else {
ret = false;
}
} else {
ret = false;
}
return ret;
}
};