 $(function(){
	var focusId;
	$("input").focus(function(){
		//记住焦点位置
		focusId = this.id;
		//this.scrollIntoView(true);//回到顶部
		//window.setTimeout("myScrollIntoView()",100);  
	});	
	
	setTimeout(function() {
		genTotalWaybillNo();
	}, 1000);
	
	
	//当失去焦点按回车时,焦点回到之前焦点位置
	$(window).keydown(function(event){
	 	if((event.keyCode==13)) {
	 		$(".messager-body").window('close');
	 		if(document.activeElement.id == null || document.activeElement.id ==''){
	 			//回到上次焦点地方
		 		//$("#"+focusId).focus();
	 		}
	 	}  
	});	
	
    $('#totalWaybillNo').keyup(function (event) {
        if (event.keyCode != "13") {return false;}
        $("#packetNo").val(trimStr($("#packetNo").val()));
        //播放扫描声音
    	playSound('../raw/scan.wav');
        submitTotalWaybillNo();
    });
    
    $('#packetNo').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#packetNo").val(trimStr($("#packetNo").val()));
    	//播放扫描声音
    	playSound('../raw/scan.wav');
    	submitPacketNo();
    });
	 
});

var isSubmitIng = false;//是否正在提交

function submitTotalWaybillNo(){
	var totalWaybillNo = $('#totalWaybillNo').val();
   	if(totalWaybillNo == null || totalWaybillNo == ''){
   		return;
   	}
	$("#packetNo").focus();
}
 
//提交装箱
function submitPacketNo(){
	if(isSubmitIng){return;}
	isSubmitIng = true;

	var totalWaybillNo = $('#totalWaybillNo').val();
	var packetNo = $('#packetNo').val();
	
	var addOrSub =$('input:radio[name="addOrSub"]:checked').val();//1表示加,2表示减
	
	if(totalWaybillNo=='' || totalWaybillNo==null){
	   mui.toast('总运单号不能为空');  
	   isSubmitIng = false;
	   return;
	}
		
	if(packetNo=='' || packetNo==null){
	   mui.toast('托盘号不能为空');  
	   isSubmitIng = false;
	   return;
	}

 	//绑定
	if(addOrSub == 1){
		binding();
		return;
	}
	//解绑
	if(addOrSub == 2){
		unBinding();
		return;
	}
}

function binding(){
	var packetNo = $('#packetNo').val();
	var totalWaybillNo = $('#totalWaybillNo').val();
	var formData = {packetNo:packetNo,totalWaybillNo:totalWaybillNo};
	
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.BIND_TOTAL_WAYBILL;
   	appRequest.msg = JSON.stringify(formData);//msg是消息内容主体
   	
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	isSubmitIng = false;
        	
        	if(!appResponse.success){
        		playSound('../raw/error.wav');
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('packetNo');  }); 
    			return;
    		}
        	
        	//播放成功声音
        	playSound('../raw/ok.wav'); 
			        	
	      	nextPacketNo();
    	},
    	error: function () {
    		isSubmitIng = false;
      		mui.toast('网络断开或服务器发生异常');  
	    }
  	});	
}

function unBinding(){
	var packetNo = $('#packetNo').val();
	var totalWaybillNo = $('#totalWaybillNo').val();
	var formData = {packetNo:packetNo,totalWaybillNo:totalWaybillNo};
	
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.UN_BIND_TOTAL_WAYBILL;
   	appRequest.msg = JSON.stringify(formData);//msg是消息内容主体
   	
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	isSubmitIng = false;
        	
        	if(!appResponse.success){
        		playSound('../raw/error.wav');
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('packetNo');  }); 
    			return;
    		}
        	
        	//播放成功声音
        	playSound('../raw/ok.wav'); 
        	
	      	nextPacketNo();
    	},
    	error: function () {
    		isSubmitIng = false;
      		mui.toast('网络断开或服务器发生异常');  
	    }
  	});		
}  


function cleanAll(){
	isSubmitIng = false;
	$("#packetNo").val("");
	$("#totalWaybillNo").val("");
	$('#totalWaybillNo').focus();
}

function nextPacketNo(){
	$("#packetNo").val('');
	$("#packetNo").focus();
}

function finishTotalWaybillNo(){
	cleanAll();
	genTotalWaybillNo();
}

function genTotalWaybillNo(){
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.GEN_TOTALWAYBILL_NO;
   	appRequest.msg = '';//msg是消息内容主体
	   	
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	if(!appResponse.success){
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('totalWaybillNo');  }); 
    			return;
    		}
        	var totalWaybillNo = appResponse.msg;
        	$("#totalWaybillNo").val(totalWaybillNo);
        	$('#totalWaybillNo').focus();
    	},
    	error: function () {
      		mui.toast('网络断开或服务器发生异常');  
	    }
  	});
	
}