
//延时函数
function sleep(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
	now = new Date();
	if (now.getTime() > exitTime)
	return;
	}
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3)//, //季度 
        //"S+": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	if (/(S+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getMilliseconds() + "").substr(3 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
//调用： var time1 = new Date().Format("yyyy-MM-dd");var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");

function CreateSnByTime(SnLength,Prefix){
	var sn = new String("");
	var time = new Date().Format("yyyyMMddhhmmssSSS");
	var time_length = time.length;
	var PrefixLength = Prefix.length;
	if(PrefixLength == 0){
		if(SnLength > time_length){
			var diff = SnLength - time_length;
			for(var i=1; i<diff+1; i++){
				sn += String(i%10);
			}
			sn += time;
		}
		else if(SnLength < time_length){
			var diff = time_length - SnLength;
			sn = time.substring(diff);
		}
		else{
			sn = time;
		}
	}
	else{
		if(SnLength == (PrefixLength + time_length)){
			sn = Prefix + time;
		}
		else if(SnLength > (PrefixLength + time_length)){
			var diff = SnLength - PrefixLength - time_length;
			for(var i=1; i<diff+1; i++){
				sn += String(i%10);
			}
			sn = Prefix + sn + time;
		}
		else if((SnLength < (PrefixLength + time_length)) && (SnLength > PrefixLength)){
			var diff = PrefixLength + time_length - SnLength;
			sn = Prefix + time.substring(diff);
		}
		else if(SnLength < PrefixLength){
			var diff = PrefixLength - SnLength;
			sn = Prefix.substring(diff);
		}
	}
	sleep(1);
	return sn;
}

//wifi & bt & OTTSN
function CreateOTTSN(Prefix){
	var ottsn = new String("");
	//var time = new Date();
	var WBLength = 12;
	var PrefixLength = Prefix.length;
	var diff = WBLength - PrefixLength;
	ottsn = Prefix;
	for(var i=0; i<diff; i++){
		var x = parseInt(Math.random()*16,10);
		ottsn += x.toString(16);
	}
	if(PrefixLength<2){
		var szTemp = ottsn.substring(1,2);
		var nTemp = parseInt(szTemp, 16);
		if(nTemp%4 != 0){
			nTemp = (nTemp%4)*4;
			var newTemp = nTemp.toString(16);
			ottsn = ottsn.substring(0,1) + newTemp + ottsn.substring(2, ottsn.length);
		}
	}
	return ottsn;
}

function CreateWifi(Prefix){
	var wifi = new String("");
	//var time = new Date();
	var WBLength = 12;
	var PrefixLength = Prefix.length;
	var diff = WBLength - PrefixLength;
	wifi = Prefix;
	for(var i=0; i<diff; i++){
		var x = parseInt(Math.random()*16,10);
		wifi += x.toString(16);
	}
	if(PrefixLength<2){
		var szTemp = wifi.substring(1,2);
		var nTemp = parseInt(szTemp, 16);
		if(nTemp%4 != 0){
			nTemp = (nTemp%4)*4;
			var newTemp = nTemp.toString(16);
			wifi = wifi.substring(0,1) + newTemp + wifi.substring(2, wifi.length);
		}
	}
	return wifi;
}
function CreateBT(Prefix){
	var BT = new String("");
	//var time = new Date();
	var WBLength = 12;
	var PrefixLength = Prefix.length;
	var diff = WBLength - PrefixLength;
	BT = Prefix;
	for(var i=0; i<diff; i++){
		var x = parseInt(Math.random()*16,10);
		BT += x.toString(16);
	}
//	if(PrefixLength<2){
//		var szTemp = BT.substring(0,2);
//		var nTemp = parseInt(szTemp, 16);
//		if(nTemp%2 != 0){
//			nTemp -= 1;
//			var newTemp = nTemp.toString(16);
//			BT = BT.replace(szTemp,newTemp);
//		}
//	}
	return BT;
}
/** 
 * 根据IMEI的前14位，得到第15位的校验位 
 * IMEI校验码算法： 
 * (1).将偶数位数字分别乘以2，分别计算个位数和十位数之和 
 * (2).将奇数位数字相加，再加上上一步算得的值 
 * (3).如果得出的数个位是0则校验位为0，否则为10减去个位数 
 * 如：35 89 01 80 69 72 41 偶数位乘以2得到5*2=10 9*2=18 1*2=02 0*2=00 9*2=18 2*2=04 1*2=02,计算奇数位数字之和和偶数位个位十位之和， 
 * 得到 3+(1+0)+8+(1+8)+0+(0+2)+8+(0+0)+6+(1+8)+7+(0+4)+4+(0+2)=63  
 * 校验位 10-3 = 7 
 */
function GetImei15(Digit){
	var DigitLength = 14;
	var Sum = new Number(0);
	for(var i=0; i<DigitLength; i++){
		var x1 = new Number(Digit.substring(i, i+1));
		i++;
		var temp = new Number(Digit.substring(i, i+1)) * 2;
		var x2 = temp<10? temp: temp%10 + parseInt(temp/10);
		Sum += x1+x2;
	}
	Sum %= 10;
	Sum = Sum==0? 0: 10 - Sum;
	
	return Sum;
}

/*
* MEID校验码算法：
* (1).将偶数位数字分别乘以2，分别计算个位数和十位数之和，注意是16进制数
* (2).将奇数位数字相加，再加上上一步算得的值
* (3).如果得出的数个位是0则校验位为0，否则为10(这里的10是16进制)减去个位数
* 如：AF 01 23 45 0A BC DE 偶数位乘以2得到F*2=1E 1*2=02 3*2=06 5*2=0A A*2=14 C*2=18 E*2=1C,
* 计算奇数位数字之和和偶数位个位十位之和，得到 A+(1+E)+0+2+2+6+4+A+0+(1+4)+B+(1+8)+D+(1+C)=100 
* => 校验位, 如果整除，那么 = 0，否则 16 - Sum % 16 
*/
function Get15thOfMEID(meid){
	var sum = 0;
	for(var i=0; i<14;){
		var  odd = parseInt(meid.substring(i, i+1), 16);
		i++;
		var even = parseInt(meid.substring(i, i+1), 16) * 2;
		sum += (odd + (((even&0xF0) >> 4) + (even&0x0F)));
		i++;
	}
	if ((sum % 16) == 0) {
		return '0';
	} else {
		var chksum = 16 - (sum % 16);
		return chksum.toString(16).toUpperCase();
	}
}

function CreateImei(Prefix){
	var imei = new String("");
	var Digit = new String("");
	
	var DigitLength = 14;
	var PrefixLength = Prefix.length;
	var diff = DigitLength - PrefixLength;
	Digit = Prefix;
	for(var i=0; i<diff; i++){
		Digit += parseInt(Math.random()*10,10);
	}
	
	imei = Digit + GetImei15(Digit).toString();
	
	return imei;
}

function CreateMEIDbyPrefix(prefix){
	var meid = new String("");
	var left = 14 - prefix.length;
	meid = prefix;
	for(var i=0; i<left; i++){
		meid += parseInt(Math.random()*10,10);
	}
	
	return (meid + Get15thOfMEID(meid).toString()).toUpperCase();
}

$(function() {
	var bClickBtn = true;
	for(var i=0; i<Settings.Barcodes.length; i++){
		if(Settings.Barcodes[i].Enable){
			var barcode = new String("");
			if(Settings.Barcodes[i].GenerateType == "ManualInput"){
				//barcode = "";
				bClickBtn &= false;
			}
			else if(Settings.Barcodes[i].GenerateType == "Section")
			{
				if(-1 != Settings.Barcodes[i].Name.search(/IMEI/g)){
					barcode = Settings.Barcodes[i].InitBarCode + GetImei15(Settings.Barcodes[i].InitBarCode).toString();    //默认Imei生成14位，调用之后，生成15位
				}
				else if(-1 != Settings.Barcodes[i].Name.search(/MEID/g)){
					barcode = (Settings.Barcodes[i].InitBarCode + Get15thOfMEID(Settings.Barcodes[i].InitBarCode)).toUpperCase(); 
				}
				else{
					barcode = Settings.Barcodes[i].InitBarCode;
				}
				
			}
			else{
				if(-1 != Settings.Barcodes[i].Name.search(/SN/g)){
					barcode = CreateSnByTime(Settings.Barcodes[i].MaxLength, Settings.Barcodes[i].Prefix);
				}
				else if(-1 != Settings.Barcodes[i].Name.search(/IMEI/g)){
					barcode = CreateImei(Settings.Barcodes[i].Prefix);
				}
				else if(-1 != Settings.Barcodes[i].Name.search(/OTTSN/g)){
					barcode = CreateOTTSN(Settings.Barcodes[i].Prefix);
					barcode = barcode.toUpperCase();
				}
				else if(-1 != Settings.Barcodes[i].Name.search(/WIFI/g)){
					barcode = CreateWifi(Settings.Barcodes[i].Prefix);
					barcode = barcode.toUpperCase();
				}
				else if(-1 != Settings.Barcodes[i].Name.search(/BT/g)){
					barcode = CreateBT(Settings.Barcodes[i].Prefix);
					barcode = barcode.toUpperCase();
				}
				else if(-1 != Settings.Barcodes[i].Name.search(/MEID/g)){
					barcode = CreateMEIDbyPrefix(Settings.Barcodes[i].Prefix);
				}
				else if(-1 != Settings.Barcodes[i].Name.search(/CustFixed/g)){
					barcode = Settings.Barcodes[i].InitBarCode;
				}				
			}
						
			$("#"+ Settings.Barcodes[i].Name).val(barcode);
		}
	}
	if(bClickBtn){
		Submit();
	}
});