//添加一行收据信息
function  addReceiptTblTr(){
	var tbl=$("#receiptTbl");
	var id=tbl.find("tr:last").attr("id");
	var receiptType='<option value="">请选择</option><option value="1" >住院</option><option value="2" >门急诊</option><option value="3" >药店</option>';
	var index=-1;
	if(id!=undefined&&id.indexOf("tr")>-1){//新增的行
		index=parseInt(id.replace("tr",""));
		index++;
	}else{
		index=1;
	}
	var trStr='<tr id="tr'+index+'" onclick="selTblTr(this,\'receiptTbl\')"><td><input type="text" name="invoiceNo" /></td>'
				+'<td><select name="receiptType">'+receiptType+'</select></td><td><input type="text" readonly="readonly" name="currencySymbol"  onclick="showCurrencyDiv(\'open1\',\'tr'+index+'\')" value="人民币"/><input type="hidden" name="hcurrencySymbol" /></td>'
				+'<td><input type="text" name="exchangeRate" /></td><td><input type="text" name="totalAmt" /></td><td><input type="text" name="invoiceDate" readonly="readonly" onclick="WdatePicker({maxDate:\'%y-%M-%d\'})" /></td>'
				+'<td><input type="text" name="overallPay" /></td><td><input type="text" name="attachPay" /></td><td><input type="text" name="ownAmt" /></td><td><input type="text" name="divAmt" /></td>'
				+'<td><input type="text" name="thirdPay" /></td><td></td></tr>';
	tbl.append(trStr);
}
//删除收据信息
function delReceiptTblTr(){
	 var tbl=$("#receiptTbl");
	 var index=-1;
	 index=tbl.find("input[name='deltrIndex']").val();
	 var claimNo=$("#hclaimNo").val();
	 var receiptNo=tbl.find("tr:eq("+index+") input[name='receiptNo']").val();
	 if(index>0){
		 tbl.find("input[name='deltrIndex']").val("-1");
		 if(confirm("确定要删除吗？")){
			 $.ajax({
					type:"POST",
					url:"/claimserver/epiboly/delReceiptInfo.do",
					data:{claimNo:claimNo,receiptNo:receiptNo},
					success:function(){
						tbl.find("tr:eq("+index+")").remove();
						alert("删除成功！");
					},
					error:function(){
						alert("删除失败！");
					}
			});
		 }
		 
	 }
}
//保存收据信息
function saveReceiptData(){
	str="";
	var receiptTbltr=$("#receiptTbl tr");
	var claimNo=$("#hclaimNo").val();//赔案号
	var eventNo=$("#heventNo").val();//事件号
	for(var i=1;i<receiptTbltr.length;i++){
		var tr=$(receiptTbltr[i]);
		var id=tr.attr("id");
		var invoiceNo=tr.find("input[name='invoiceNo']").val();//发票号
		var receiptType=tr.find("select[name='receiptType']").val();
		var currencySymbol=tr.find("input[name='hcurrencySymbol']").val();
		var exchangeRate=tr.find("input[name='exchangeRate']").val();
		var totalAmt=tr.find("input[name='totalAmt']").val();
		var invoiceDate=tr.find("input[name='invoiceDate']").val();
		var overallPay=tr.find("input[name='overallPay']").val();
		var attachPay=tr.find("input[name='attachPay']").val();
		var ownAmt=tr.find("input[name='ownAmt']").val();
		var divAmt=tr.find("input[name='divAmt']").val();
		var thirdPay=tr.find("input[name='thirdPay']").val();
		if(id.indexOf("tr")>-1){
			id="";
		}
		str+='{"id":"'+id+'","invoiceNo":"'+invoiceNo+'","receiptType":"'+receiptType+'","currencySymbol":"'+currencySymbol
				+'","exchangeRate":"'+exchangeRate+'","exchangeRate":"'+exchangeRate+'","totalAmt":"'+totalAmt+'","invoiceDate":"'+invoiceDate
				+'","overallPay":"'+overallPay+'","attachPay":"'+attachPay+'","ownAmt":"'+ownAmt+'","divAmt":"'+divAmt+'","thirdPay":"'+thirdPay+'"},';
		
	}
	if(str!=""){
		str=str.substring(0,str.length-1);
		var jsonStr='{"claimNo":"'+claimNo+'","eventNo":"'+eventNo+'","rcptBills":['+str+']}';
		$.ajax({
			type:"POST",
			url:"/claimserver/epiboly/saveReceiptData.do",
			contentType:"application/json; charset=UTF-8",
			data:jsonStr,
			success:function(data){
				alert('保存成功');
				location='/claimserver/epiboly/findReceiptInfo.do?claimNo='+claimNo+'&eventNo='+eventNo;
			},error:function(e){
				alert('保存失败');
			}
		});
	}
}
//查询币种
function moneyChoose(){
	$("#moneyTbl").html("");
	var countryName =$.trim($("#countryNameChoose").val());
	if(countryName==""){
		alert("请填写国家名称");
		return;
	}
    $.ajax({  
        type: "POST",  
        url: "/claimserver/epiboly/countrySearch.do",  
        data:{countryName:countryName},
        success: function(data){
        	var items = data.moneys; 
        	 $.each(items, function(i, n){
        		 $("#moneyTbl").append("<tr id="+items[i].currencySymbol+" onclick=choosEd(this)>" +
               			"<td>"+items[i].countryName+"</td>"+
               			"<td id='chooseCountryName' >"+items[i].currencyChName+"</td>"+
               			"</tr>");
        	 });
        }  
    }); 
}
//删除行
function delTblTr(id){
	var tbl=$("#"+id);
	var index=tbl.find("input[name='deltrIndex']").val();
	tbl.find("tr:eq("+index+")").remove();
}
function openReceiptWindow(url,claimNo,receiptNo){
	url=url+"?claimNo="+claimNo+"&receiptNo="+receiptNo+"&eventNo="+$("#heventNo").val();
	window.open(url,'newwindow','top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no');
}
function selTblTr(obj,id){
	var index=$(obj).index();
	$("#"+id).find("input[name='deltrIndex']").val(index);
}
function choosEd(flag){
	 
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
	index=$(flag).index();
}
function showCurrencyDiv(id,trid){
	showDiv(id);
	$("#hCurrencyTrId").val(trid);
}
function saveCurrentcyDiv(id) {
	var tr = $("#moneyTbl tr:eq("+index+")"); 
	var currencySymbol = tr.attr("id"); 
	var currencyTr=$("#"+$("#hCurrencyTrId").val());
	currencyTr.find("input[name='hcurrencySymbol']").val(currencySymbol);
	var spanCurrentcyName = tr.find("td[id='chooseCountryName']").html();
	currencyTr.find("input[name='currencySymbol']").val(spanCurrentcyName);
	$.closePopupLayer(id);
}