function check(){
    if (confirm("确认是否提交本次报销单，点击确定提交开始审批，点击取消关闭本窗口。"))
        return true;
    else return false;
}

function receiptYear(){
	 var form = $('#up'); 
	 $('#year').val($('#Y').val());
	 form.submit();  
}

function receiptDate(){
	var form = $('#up'); 
	//$('#year').val($('#date').val());
	form.submit();  
}

function reportCatePM(){
	var name=$('#reportCate').val();
	$("option[node-all='true']").show();
	if(name=='ALL'){
		$("tr[node-all='true']").show();
		$("option[node-all='true']").show();
	}else{
		$("tr[node-all='true']").hide();
		$("option[node-all='true']").hide();
		$("option[node-cate='"+name+"']").show();
		$("tr[node-boxCate='"+name+"']").show();
	}
}

function reportPM(){
	var name=$('#report').val();
	if(name=='ALL')
		$("tr[node-all='true']").show();
	else{
		$("tr[node-all='true']").hide();
		$("tr[node-box='"+name+"']").show();
	}
}

function receiptMoth(){
	 var form = $('#up'); 
	 $('#year').val($('#Y').val());
	 $('#month').val($('#M').val());
	 //alert($('#M').val());
	 form.submit();  
}

function receiptDay(){
	 var form = $('#up'); 
	 $('#year').val($('#Y').val());
	 $('#month').val($('#M').val());
	 $('#day').val($('#D').val());
	 form.submit();  
}

function receiptSop(){
	 var form = $('#up'); 
	 $('#year').val($('#Y').val());
	 $('#month').val($('#M').val());
	 $('#day').val($('#D').val());
	 $('#sop').val($('#S').val());
	 form.submit();  
}


function updateApprovalReceipt(status,rid){
	 var form = $('#verifyOpinion_'+rid); 
	 $('#status_'+rid).val(status);
	 $("input[name='amount']").val($('#amount').val());
	 
	 form.submit(); 
}

function setExeReceipt(tr,rid){
	 var form = $('#implement'+tr);
	 var amount=$('#amount'+tr).val();
	 
	 var accountBank=$('#accountBankSelect'+tr).val();
	 
	 console.log(amount);
	 if(amount=='')
		 return alert('请填写汇款金额');
	 $("#amountInp"+tr).val(amount);
	 $("#accountBank"+tr).val(accountBank);
	 form.submit(); 
}

function expensesDefray(status,rid){
	var form = $('#verifyOpinion_'+rid); 
	 $('#status_'+rid).val(status);
	form.submit();
}

function showOpinion(receiptOrder){
	//console.log(receiptOrder);
	$('#orderBody').html('读取中');
	var url="/ajaxGetReceiptOrder";
	
	$.ajax({
		url:url,
		type: "post",
		cache: false,
		dataType: "json",
		data: {
			receiptOrder:receiptOrder
		},
		global: true,
		success: function(data){
			if(data.status==1)
				$('#orderBody').html(data.receiptHtml);
			else
				$('#orderBody').html('没找到信息');
		},
		error:function(err){
		}
		});
}

$(function() {

	$('select[node-implement]').change(function(){
		//$item=$(this).attr('value');
		
//		var action = $('#up').attr("action");
//		action=encodeURI(action+'/'+$item);
//		$('#up').attr("action",action);
		$('#up').submit();
		
		
		
		console.log();
	});
	
	$('button[node-qmk="tm"]').click(function(){
		var id=$(this).attr('data');
		$('#'+id).attr('value',$('#'+id).attr('data-tm'));

	});
	
	$('a[node-ReceiptBack="true"]').click(function(){
		var rid=$(this).attr("p-d");
		var opinion=$('#opinion_'+rid).val();
		
		var url="/approvalChe";
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				rid:rid,
				opinion:opinion
			},
			global: true,
			success: function(data){
				if(data.status==2){
					alert('参数错误');
				}else{
					window.location.href="/approvalExpenses/pendApproval";
				}
			},
			error:function(err){
			}
			});
	});
	
	$("a[id^=examine]").click(function(){
		var rid=$(this).attr('data');
		var url="/ajaxExamine";
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				rid:rid
			},
			global: true,
			success: function(data){
				if(data.status==0){
					alert('参数错误');
				}
				var count=data.receipt.length;
				if(count!=0){
					if(data.receipt.Rtype==1){
						window.location.href="/expensesEdi/"+data.receipt.rid; 
					}else if(data.receipt.Rtype==0){
						window.location.href="/receiptburEdi/"+data.receipt.rid; 
					}else if(data.receipt.Rtype==2){
						window.location.href="/remitApproval/"+data.receipt.rid; 
					}
				}else
					alert('没有需要审批的费用');
			},
			error:function(err){
			}
			});
	});
	
	
	$("a[id^=examineExe]").click(function(){
		var rid=$(this).attr('data');
		var url="/ajaxExamineExe";
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				rid:rid
			},
			global: true,
			success: function(data){
				
				if(data.status==0){
					alert('参数错误');
				}
				
				var count=data.receipt.length;
				
				if(count!=0){
					window.location.href="/implement/"+data.receipt.ridKey+".html"; 
					
				}else
					alert('没有需要审批的费用');
				
				
				console.log(data);
				
			},
			error:function(err){
			}
			});
		
		
	});
	
	
	
	
	$("input[id^='agency_']").change(function() {
		
		$i=0;
		$("input[id^='agency_']").each(function(i){
			$i+=parseFloat(this.value);
			
		 });
		$("#agencyResult").html("￥"+$i.toFixed(2));
		$("#agencyR").val($i.toFixed(2));
		
		var travelR=parseFloat($("#travelR").val());
		var trainR=parseFloat($("#trainR").val());
		var otherR=parseFloat($("#otherR").val());
		var RS=parseFloat($i)+parseFloat(travelR)+parseFloat(trainR)+parseFloat(otherR);
		
		$("#RS").html("￥"+RS.toFixed(2));
	});
	
	$("input[id^='travel_']").change(function() {
		
		$i=0;
		$("input[id^='travel_']").each(function(i){
			$i+=parseFloat(this.value);
			
		 });
		$("#travelResult").html("￥"+$i.toFixed(2));
		$("#travelR").val($i.toFixed(2));
		
		var agencyR=$("#agencyR").val();
		var trainR=parseFloat($("#trainR").val());
		var otherR=parseFloat($("#otherR").val());
		var RS=parseFloat($i)+parseFloat(agencyR)+trainR+otherR;
		
		$("#RS").html("￥"+RS.toFixed(2));
	});
	
	$("input[id^='train_']").change(function() {
		
		$i=0;
		$("input[id^='train_']").each(function(i){
			$i+=parseFloat(this.value);
			
		 });
		$("#trainResult").html("￥"+$i.toFixed(2));
		$("#trainR").val($i.toFixed(2));
		
		var agencyR=$("#agencyR").val();
		var otherR=parseFloat($("#otherR").val());
		var travelR=parseFloat($("#travelR").val());
		var RS=parseFloat($i)+parseFloat(agencyR)+travelR+otherR;
		
		$("#RS").html("￥"+RS.toFixed(2));
	});
	
	$("input[id^='other_']").change(function() {
		
		$i=0;
		$("input[id^='other_']").each(function(i){
			$i+=parseFloat(this.value);
			
		 });
		$("#otherResult").html("￥"+$i.toFixed(2));
		$("#otherR").val($i.toFixed(2));
		
		var agencyR=$("#agencyR").val();
		var travelR=parseFloat($("#travelR").val());
		var trainR=parseFloat($("#trainR").val());
		var RS=parseFloat($i)+parseFloat(agencyR)+parseFloat(travelR)+parseFloat(trainR);
		
		$("#RS").html("￥"+RS.toFixed(2));
	});
	
	$("a[id^='getStaffInfo']").click(function(){
		
		var username=$(this).attr('data');
		
		var url="/getStaffInfo";
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				username:username
			},
			global: true,
			success: function(data){
				
				if(data.success){
					alert('请重新登录');
				}
				
				$('#staffInfo').html(data.html);
				$('#staffInfo').show();
				$('#longleLog').hide();
				$('#clientInfo').hide();
				$('#changeCompany').hide();
			},
			error:function(err){
			}
			});
	});
	
	$("a[id^='getClientInfo']").click(function(){

		var client=$(this).attr('data');
		
		var url="/getClientInfo";
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				client:client
			},
			global: true,
			success: function(data){
				
				if(data.success){
					alert('请重新登录');
				}
				
				$('#clientInfo').html(data.html);
				$('#clientInfo').show();
				$('#staffInfo').hide();
				$('#longleLog').hide();
				$('#addClient').hide();
				$('#changeCompany').hide();
				autoFlashHeight();
			},
			error:function(err){
				autoFlashHeight();
			}
			});
		
	});
	
	$("a[id^='getCompanyInfo']").click(function(){
		alert('dd');
	});
	
	$("a[id^='getLongleLog_']").click(function() {
		
		var lid=$(this).attr('data');
		
		var url="/ajaxGetLongle";
		
		$.ajax({
			url:url,
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				lid:lid
			},
			global: true,
			success: function(data){
				
				if(data.success==false){
					alert('请重新登录');
				}
				
				$("#klid").val(data.klid);
				
				$("#longleTitle").html(data.longle.key_num);
				
				var lka='<a href="javascript:void(0)" onclick="getStaffInfo(\''+data.longle.responsible+'\')" >'+data.longle.responsible+'</a>';
				
				$("#longleResponsible").html(lka);
				
				var kk="";
				if(data.longle.alloted_time!="")
					kk="期限："+data.longle.alloted_time;
				
				$("#longleStatus").html(data.longle.statusT+"/"+data.longle.product+"/("+data.longle.version+")"+kk);
				
				$("#LongleLogHtml").html(data.longleLogHtml);
				
				if(data.longle.status>=3){
					$("#longleStatus_5").attr('disabled',false);
					$("#longleStatus_6").attr('disabled',false);
					$("#longleStatus_7").attr('disabled',false);
					$("#longleStatus_8").attr('disabled',false);
				}
				$('#staffInfo').hide();
				$("#longleLog").show();
				$('#clientInfo').hide();
				autoFlashHeight();
			},
			error:function(err){
//				easyDialog.open({
//					  container : { content : '请输入正确的参数'},
//					  autoClose : 1500,
//					  overlay : false
//				});
			}
			});
		
	});
	
	
	
	$("a[node-invoice='INFO']").click(function() {
		var postData = $(this).attr("data-isKey");
		
		$("div[loading-msg='true']").html("<img src='/global/img/loadIco.gif' />");
		$("#nc").html('illegal request');
		var url = "/ajaxGetReceiptDetails";
		$.ajax({
			url : url,
			type : "post",
			cache : false,
			dataType : "json",
			data : {
				serial : postData
				
			},
			global : true,
			success : function(data) {
				if (data.status == 1) {
					//$("div[loading-msg='true']").html("");
					
					$("#nc").html(data.html);
				} 
			},
			error : function(err) {
				$("div[loading-msg='true']").html("");
			}
		});
	});
	
	
	$("form[name='recepitAdd']").validator({
		timely: 3,
	    focusCleanup: true,
	    theme:"yellow_top",
	    rules: {
	    	remittanceBankType: function() {
	    		var rbt=$('select[node-rbt]').val();
	    		if(rbt==0)
	    			return false;
	    		return true;
	        }
	    },
	    fields: {
	        'verify': 'required; ',
	        'explanation':'required; ',
	        
	        'remittanceName':'required; ',
	        'bankName':'required; ',
	        'bankNumber':'required; ',
	        'coupletNumber':'required(remittanceBankType); '
	        
	    }
	});
	
	$("select[node-rbt]").change(function(){
		var rbt=$(this).val();
		if(rbt==0){
			$("tr[node-cn]").hide();
		}else if(rbt==1){
			$("tr[node-cn]").show();
		}
	});
	
	//培训班结算
	$("form[name='receiptTrainAdd']").validator({
		timely: 3,
	    focusCleanup: true,
	    theme:"yellow_top",
	    rules: {
	    	remittanceBankType: function() {
	    		var rbt=$('select[node-rbt]').val();
	    		if(rbt==0)
	    			return false;
	    		return true;
	        }
	    },
	    fields: {
	        'verify': 'required; ',
	        'explanation':'required; ',
	        
	        'remittanceName':'required; ',
	        'bankName':'required; ',
	        'bankNumber':'required; ',
	        'coupletNumber':'required(remittanceBankType); ',
	        
	        'trainName':'required; ',
	        'trainStartDate':'required; ',
	        'trainEndDate':'required; ',
	        'participants':'required; ',
	        'trainAddress':'required; ',
	    }
	});

	//培训班结算-添加支出
	$("form[name='receiptTrainItemAdd']").validator({
		timely: 3,
	    focusCleanup: true,
	    theme:"yellow_right",
	   
	    fields: {
	        'price': 'required; ',
	        'date':'required; ',
	        'inputer':'required; '
	    }
	});
	
	//培训班结算-添加讲师
	$("form[name='receiptTrainLecturerAdd']").validator({
		timely: 3,
	    focusCleanup: true,
	    theme:"yellow_right",
	   
	    fields: {
	    	'ltype':'required; ',
	    	'lecturerName':'required; ',
	        'price': 'required; ',
	        'schoolDay':'required; '
	    }
	});
	
	$("a[getReceiptLoan]").click(function() {
		$.ajax({
			url:"/ajaxGetReceiptTrainLoan",
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				
			},
			global: true,
			success: function(data){
				var html='';
				for(var i=0;i<data.receiptLoanList.length;i++){
					html +='<option value="'+data.receiptLoanList[i].ridKey+'">'+data.receiptLoanList[i].receiptOrder+' ￥'+data.receiptLoanList[i].sum+'</option>';
				}
				$("select[receiptLoanSelect]").html(html);
			},
			error:function(err){}
			});
	});
	
	$("a[getReceiptInvoice]").click(function() {
		$.ajax({
			url:"/ajaxGetInvoiceTrainByEnd",
			type: "post",
			cache: false,
			dataType: "json",
			data: {
				
			},
			global: true,
			success: function(data){
				var html='';
				for(var i=0;i<data.invoiceTrainingList.length;i++){
					html +='<option value="'+data.invoiceTrainingList[i].itidKey+'">'+data.invoiceTrainingList[i].trainDate+'&nbsp;'+data.invoiceTrainingList[i].trainName+'</option>';
				}
				$("select[receiptInvoieSelect]").html(html);
			},
			error:function(err){}
			});
	});
	
	//h5文件上传
	var numFilesUploaded=0;
	$("input[enclosurFile]").change(function() {
		
		var form_data = new FormData();
		var file_data = $(this).prop("files")[0];
		
		form_data.append("Filename", file_data.name);
		form_data.append("Filedata", file_data);
		
		$.ajax({
		    type: "POST",
		    url: "/swfupload",
		    dataType : "json",
		    processData: false,  // 注意：让jQuery不要处理数据
		    contentType: false,  // 注意：让jQuery不要设置contentType
		    data: form_data
		}).success(function(msg) {
			var status = document.getElementById("divStatus");
			numFilesUploaded+=1;
			status.innerHTML = file_data.name+"上传成功 <br/>"+numFilesUploaded+" 个文件已上传.";
			
			var en=$('#enclosurName').val();
			if (en!='')
				$('#enclosurName').val(en+'-'+msg.filename);
			else
				$('#enclosurName').val(msg.filename);
			
			
			//页面展示上传内容
			var tr = document.createElement("tr");
			tr.id=msg.id;
			var td1 = document.createElement("td");
			td1.innerHTML = '<a href="#" target="_blank">'+file_data.name+'</a>';
			var td2 = document.createElement("td");
			td2.innerHTML = toDecimal2(file_data.size/1024)+'kb';
			var td3 = document.createElement("td");
			td3.innerHTML = '<a href="javascript:void(0)" onclick="delEnclosurName(\''+msg.filename+'\',\''+msg.id+'\')" >删除</a>';
			tr.appendChild(td1);
			tr.appendChild(td2); 
			tr.appendChild(td3); 
			var kt=document.getElementById('KT');
			
			if(kt!=null)
			kt.appendChild(tr);
			
		}).fail(function(msg) {
		    //console.log(msg);
		});
	});	
})

function delEnclosurByKey(id){
	
	var url = "/ajaxRemEnclousur";
	$.ajax({
		url : url,
		type : "post",
		cache : false,
		dataType : "json",
		data : {
			key : id
		},
		global : true,
		success : function(data) {
			
			if (data.status == 1) {
				$('#'+id).remove(); 
			} 
		},
		error : function(err) {
		}
	});
}

function toDecimal2(x) {  
    var f = parseFloat(x);  
    if (isNaN(f)) {  
        return false;  
    }  
    var f = Math.round(x*100)/100;  
    var s = f.toString();  
    var rs = s.indexOf('.');  
    if (rs < 0) {  
        rs = s.length;  
        s += '.';  
    }  
    while (s.length <= rs + 2) {  
        s += '0';  
    }  
    return s;  
}

function delEnclosurName(name,id){
	var en=$('#enclosurName').val();
	var newName=en.replace(name,'');
	$('#enclosurName').val(newName);
	 $('#'+id).remove();     
	
}

function selectToolTipCl(name){
	var e = is_ie ? event : selectToolTipCl.caller.arguments[0];
	obj = is_ie ? e.srcElement : e.target;
	var arr = document.getElementsByName(name);
	var k = arr.length;
	for ( var i = 0; i < k; i++) {
		arr[i].checked = obj.checked;
	}
	$('#addToolTag').hide();
}

function selectTagCl(name){
	var e = is_ie ? event : selectTagCl.caller.arguments[0];
	obj = is_ie ? e.srcElement : e.target;
	var arr = document.getElementsByName(name);
	var k = arr.length;
	for ( var i = 0; i < k; i++) {
		arr[i].checked = obj.checked;
	}
}

function checkall(name) {
	var e = is_ie ? event : checkall.caller.arguments[0];
	obj = is_ie ? e.srcElement : e.target;
	var arr = document.getElementsByName(name);
	var k = arr.length;
	for ( var i = 0; i < k; i++) {
		arr[i].checked = obj.checked;
	}
}



if (is_ie)
	document.documentElement.addBehavior("#default#userdata");

var userAgent = navigator.userAgent.toLowerCase();
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
