 var index = 0;
 var otherIndex=0;
 var moneyIndex = 0;
 var medicalIndex=0;
$(function(){
	
	
});

function dispatchClaimPage(){
	 $("#claimReceiptForm").attr("action","dispatcherClmInfoByMyTask.do");
	 $("#claimReceiptForm").submit();
}

function dispatcherEventPage(eventNo){
	if(eventNo!=""){
		$("#eventNo").val(eventNo);
	}
	$("#claimReceiptForm").attr("action","dispatcherEventPage.do");
	$("#claimReceiptForm").submit();
	
}



function saveReceiptPage(){
	if (null == $("#invoiceNo").val() || undefined == $("#invoiceNo").val() || '' == $("#invoiceNo").val()) {
		alert("发票编号不能为空！");
		return;
	}
	if($.trim($("#spanCurrentcyName").html())!="人民币元"&&$("#exchangeRate").val()==""){
		alert("请填写汇率。");
		return;
	}else{
		$("#exchangeRate").val("1");
	}
    var billAmtSum = 0;
    var totalAmt =  0;
    if (null != $("#billAmtSum").val() && undefined != $("#billAmtSum").val() && '' != $("#billAmtSum").val()) {
    	billAmtSum = parseFloat($("#billAmtSum").val());
    }
    if (null != $("#totalAmt").val() && undefined != $("#totalAmt").val() && '' != $("#totalAmt").val()) {
    	totalAmt = parseFloat($("#totalAmt").val());
    }
    
	if(billAmtSum!=totalAmt){
		alert("收据总金额和账单金额之和不一致！");
		return;
	}
	var trList = $("#billDetailTable").find("tr");
	var rcptDrugsTr = $("#rcptDrugsTbl").find("tr");
	var rcptOtherCostInfoTr = $("#rcptOtherCostInfoTbl").find("tr");
	
	/*var sumRcptDrugs = 0.00;
	for(var i=2;i<rcptDrugsTr.length;i++){
		var tr = $(rcptDrugsTr[i]);
		var actualPayment = tr.find("INPUT[id='actualPayment']");
		var actualPaymentfloat = parseFloat(actualPayment.val());
		if(actualPayment.val()!=""){
			sumRcptDrugs = sumRcptDrugs + actualPaymentfloat;
		}
	}
	for(var i=2;i<rcptOtherCostInfoTr.length;i++){
		var tr = $(rcptOtherCostInfoTr[i]);
		var otheractualPayment = tr.find("INPUT[id='otheractualPayment']");
		var otheractualPaymentfloat = parseFloat(otheractualPayment.val());
		if(otheractualPayment.val()!=""){
			sumRcptDrugs = sumRcptDrugs + otheractualPaymentfloat;
		}
	}
	if(parseFloat($("#billAmtSum").val())!=sumRcptDrugs){
		alert("账单金额和明细不一致！");
		return;
	}*/
	var jsonStr= "";
	var billInfo = "";
	var billDetails = "";
	var drugsDetails = "";
	var rcptOtherCostInfo = "";
	var receipttype = $("#divreceipttype_code").val(); 
	if(receipttype!=undefined&&receipttype!="")
	$("#receipttype").val(receipttype);
	var receiptNo = $("#receiptNo").val();
	var currencySymbol = $("#currencySymbol").val();
	var exchangeRate = $("#exchangeRate").val();
	var totalAmt = $("#totalAmt").val();
	var invoiceNo = $("#invoiceNo").val();
	billInfo+='{"receiptType":"' + $("#receipttype").val() + '","receiptNo":"' + receiptNo
	+ '","currencySymbol":"' + currencySymbol  +  '","exchangeRate":"' + exchangeRate
	+'","totalAmt":"' + totalAmt+'","invoiceNo":"'+invoiceNo+'"}';
	for(var i=1;i<trList.length-1;i++){
		var tr = $(trList[i]);
		var trId=tr.attr("id");
		if(trId!="sumMoney"){
			var billAmt = tr.find("INPUT[id='billAmt']");
			var ownAmt = tr.find("INPUT[id='ownAmt']");
			var divAmt = tr.find("INPUT[id='divAmt']");
			var medIns = tr.find("INPUT[id='medIns']");
			var deductionsAmt = tr.find("INPUT[id='deductionsAmt']");
			if(billAmt.val()==""&&ownAmt.val()==""&&divAmt.val()==""&&medIns.val()==""&&deductionsAmt.val()==""){
				continue;
			}
			billDetails+='{"billItemCode":"' + trId + '","billAmt":"' + billAmt.val()+ '","ownAmt":"' + ownAmt.val()+
			'","divAmt":"' + divAmt.val()+'","medIns":"' + medIns.val()+'","deductionsAmt":"' + deductionsAmt.val()+'"},';
		}
		
	}
	billDetails = "["+billDetails.substring(0,billDetails.length-1)+"]";
	
	for(var i=2;i<rcptDrugsTr.length;i++){
		var tr = $(rcptDrugsTr[i]);
		var trId=tr.attr("id");
		if(trId=="addnewtr"){
			trId="";
		}
		var drugsCode = tr.find("INPUT[id='drugsCode']");
		var drugsName = tr.find("INPUT[id='drugsName']");
		var specifications = tr.find("INPUT[id='specifications']");
		var dosageForm = tr.find("INPUT[id='dosageForm']");
		var units = tr.find("INPUT[id='units']");
		var unitPrice = tr.find("INPUT[id='unitPrice']");
		var quantity = tr.find("INPUT[id='quantity']");
		var actualPayment = tr.find("INPUT[id='actualPayment']");
		var medicalInsType = tr.find("INPUT[id='medicalInsType']");
		var ownRate = tr.find("INPUT[id='ownRate']");
		var ownCostAmt = tr.find("INPUT[id='ownCostAmt']");
		var rejectSpan = tr.find("SPAN");
		var isReject = "";
		var excludeReason = tr.find("INPUT[id='excludeReason']");
		if(rejectSpan.attr("class")=="check_no"){
			isReject="N";
		}else{
			isReject="Y";
		}
		var costRate = 0.00;
		if(ownRate.val()!=""&&ownRate.val()!=undefined){
			costRate = ownRate.val();
		}
		if(drugsCode.val()==""&&drugsName.val()==""&&specifications.val()==""&&dosageForm.val()==""&&units.val()==""
			&&unitPrice.val()==""&&quantity.val()==""&&actualPayment.val()==""&&medicalInsType.val()==""&&ownRate.val()==""
			&&ownCostAmt.val()==""&&excludeReason.val()==""){
			continue;
		}
		drugsDetails+='{"drugsCode":"' + drugsCode.val() + '","id":"' + trId +'","drugsName":"' + drugsName.val()+ '","specifications":"' + specifications.val()+
		'","dosageForm":"' + dosageForm.val()+'","units":"' + units.val()+'","unitPrice":"' + unitPrice.val()+
		'","quantity":"' + quantity.val()+'","actualPayment":"' + actualPayment.val()+'","medicalInsType":"' + medicalInsType.val()+
		'","ownRate":"' + costRate+'","ownCostAmt":"' + ownCostAmt.val()+'","isReject":"' + isReject+
		'","excludeReason":"' + excludeReason.val()+'","billItemCode":"' + "000009"+'"},';
	}
	drugsDetails = "["+drugsDetails.substring(0,drugsDetails.length-1)+"]";
	
	
	
	
	
	for(var i=2;i<rcptOtherCostInfoTr.length;i++){
		var tr = $(rcptOtherCostInfoTr[i]);
		var trId=tr.attr("id");
		if(trId=="addnewtr"){
			trId="";
		}
		var otherproductName = tr.find("INPUT[id='otherproductName']");
		var otherexpenseName = tr.find("INPUT[id='otherexpenseName']");
		var othermedicalInsType = tr.find("INPUT[id='othermedicalInsType']");
		var otherdrugsCode = tr.find("INPUT[id='otherdrugsCode']");
		var otheractualPayment = tr.find("INPUT[id='otheractualPayment']");
		var otherownCostRate = tr.find("INPUT[id='otherownCostRate']");
		var otherownCostSAmt = tr.find("INPUT[id='otherownCostSAmt']");
		var otherunitPrice = tr.find("INPUT[id='otherunitPrice']");
		var otherSpan = tr.find("SPAN");
		var isReject = "";
		var excludeReason = tr.find("INPUT[id='excludeReason']");
		if(otherSpan.attr("class")=="check_no"){
			isReject="N";
		}else{
			isReject="Y";
		}
		var costRate = 0.00;
		if(otherownCostRate.val()!=""&&otherownCostRate.val()!=undefined){
			costRate = otherownCostRate.val();
		}
		if(otherproductName.val()==""&&otherexpenseName.val()==""&&othermedicalInsType.val()==""&&otherdrugsCode.val()==""&&otheractualPayment.val()==""
			&&otherownCostRate.val()==""&&otherownCostSAmt.val()==""&&otherunitPrice.val()==""&&excludeReason.val()==""){
			continue;
		}
		rcptOtherCostInfo+='{"id":"' + trId + '","productName":"' + otherproductName.val() +'","expenseName":"' + otherexpenseName.val()+ '","medicalInsType":"' + othermedicalInsType.val()+
		'","drugsCode":"' + otherdrugsCode.val()+'","actualPayment":"' + otheractualPayment.val()+'","ownCostRate":"' + costRate+
		'","ownCostSAmt":"' + otherownCostSAmt.val()+'","billItemCode":"' + "000009"+'","unitPrice":"' + otherunitPrice.val()+
		'","isReject":"' + isReject+'","excludeReason":"' + excludeReason.val()+'"},';
	}
	rcptOtherCostInfo = "["+rcptOtherCostInfo.substring(0,rcptOtherCostInfo.length-1)+"]";
	
	jsonStr = '{"rcptBill":' + billInfo +',"rcptBillDetail":' + billDetails+',"rcptDrugsList":' + drugsDetails+
	',"rcptOtherCostInfo":' + rcptOtherCostInfo+',"claimNo":"' + $("#claimNo").val()+'","claimEventNo":"' + $("#claimEventNo").val()+'"}';
	//判断发票号是否重复
	$.ajax({
		type:"POST",
		url:"checkInvoiceRepeat.do",
		data:"invoiceNo="+invoiceNo+"&&rcptNo="+receiptNo,
		error :function(){
			alert("数据录入收据信息保存失败");
		},
		success :function(data){
			if(data=="02"){
				if(confirm("所录收据信息重复，确认是否提交信息？")){
					$.ajax({
						type:"POST",
						contentType:"application/json; charset=UTF-8",
						url:"saveReceiptInfo.do",
						data:jsonStr,
						error :function(){
							alert("数据录入收据信息保存失败");
						},
						success :function(data){
							if(data=="00"){
								alert("数据录入收据信息保存成功。");
								dispatcherEventPage($("#claimEventNo").val());
							}else{
								alert("数据录入收据信息保存失败。");
							}
						}
					});
				}
			}else{
				$.ajax({
					type:"POST",
					contentType:"application/json; charset=UTF-8",
					url:"saveReceiptInfo.do",
					data:jsonStr,
					error :function(){
						alert("数据录入收据信息保存失败");
					},
					success :function(data){
						if(data=="00"){
							alert("数据录入收据信息保存成功。");
							dispatcherEventPage($("#claimEventNo").val());
						}else {
							alert("数据录入收据信息保存失败。");
						}
					}
				});
			}
		}
	});
	
}

function addTr(){
	var tr = "<tr id=\"addnewtr\" onclick=\"choosEd(this)\">"+
	"<td><input readonly=\"readonly\" id=\"drugsCode\" name=\"drugsCode\" type=\"text\" class=\"input i_s_60\" onclick=\"showDiv('open2')\"/></td>"+
    "<td><input readonly=\"readonly\" id=\"drugsName\" name=\"drugsName\" type=\"text\" class=\"input i_s_60\" onclick=\"showDiv('open2')\"/></td>"+
    "<td><input id=\"specifications\" name=\"specifications\" type=\"text\" class=\"input i_s_60\"  /></td>"+
    "<td><input readonly=\"readonly\" id=\"dosageForm\" name=\"dosageForm\" type=\"text\" class=\"input i_s_60\" /></td>"+
    "<td><input id=\"units\" name=\"units\" type=\"text\" class=\"input i_s_60\"  /></td>"+
    "<td><input id=\"unitPrice\" onblur=\"checkMoney(this);\" name=\"unitPrice\" type=\"text\" class=\"input i_s_60\"  /></td>"+
    "<td><input id=\"quantity\"  onblur=\"checkMoney(this);\" name=\"quantity\" type=\"text\" class=\"input i_s_60\" /></td>"+
    "<td><input id=\"actualPayment\"  onblur=\"checkMoney(this);\" name=\"actualPayment\" type=\"text\" class=\"input i_s_60\" /></td>"+
    "<td><input id=\"medicalInsType\" readonly=\"readonly\" name=\"medicalInsType\" type=\"text\" class=\"input i_s_60\" /></td>"+
    "<td><input id=\"ownRate\" onblur=\"checkRate(this);\" name=\"ownRate\" type=\"text\" class=\"input i_s_60\" />%</td>"+
    "<td><input id=\"ownCostAmt\" onblur=\"checkMoney(this);\" name=\"ownCostAmt\" type=\"text\" class=\"input i_s_60\" /></td>"+
    "<td><span  class=\"check_no\"  onclick=\"cvtCheck(this)\"></span></td>"+
    "<td colspan=\"2\">"+
      "<input type=\"text\" class=\" input i_s_100\" id=\"excludeReason\" name=\"excludeReason\"  />"+
    "</td>"+
"</tr>";
	
	$("#rcptDrugsTbl").append(tr);
}


function addOtherTr(){
	var tr = "<tr id=\"addnewtr\" onclick=\"choosOtherEd(this)\">"+
            "<td><input id=\"otherproductName\" name=\"otherproductName\" type=\"text\" class=\"input i_s_60\"  /></td>"+
            "<td><input id=\"otherexpenseName\" name=\"otherexpenseName\" type=\"text\" class=\"input i_s_60\" /></td>"+
            "<td><input id=\"othermedicalInsType\" name=\"othermedicalInsType\" type=\"text\" class=\"input i_s_60\" /></td>"+
            "<td><input id=\"otherdrugsCode\" name=\"otherdrugsCode\" type=\"text\" class=\"input i_s_60\"  /></td>"+
            "<td><input id=\"otheractualPayment\" onblur=\"checkMoney(this);\" name=\"otheractualPayment\" type=\"text\" class=\"input i_s_60\" /></td>"+
            "<td><input id=\"otherownCostRate\" onblur=\"checkRate(this);\" name=\"otherownCostRate\" type=\"text\" class=\"input i_s_60\"  />%</td>"+
            "<td><input id=\"otherownCostSAmt\" onblur=\"checkMoney(this);\" name=\"otherownCostSAmt\" type=\"text\" class=\"input i_s_60\" /></td>"+
            "<td><input id=\"otherunitPrice\"  onblur=\"checkMoney(this);\" name=\"otherunitPrice\" type=\"text\" class=\"input i_s_60\" /></td>"+
            "<td><span class=\"check_no\" onclick=\"cvtCheck(this)\"></span></td>"+
            "<td colspan=\"2\"><input type=\"text\" class=\" input i_s_100\" id=\"excludeReason\" name=\"excludeReason\" /></td>"+
          "</tr>";
	
	$("#rcptOtherCostInfoTbl").append(tr);
}
function cvtCheck(check){
	if($(check).attr("class")=="check_no"){
		$(check).attr("class","check_yes");
	}else{
		$(check).attr("class","check_no");
	}
}

function choosEd(flag){ 
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
	index=$(flag).index();
}
function choosOtherEd(flag){
	 
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
	otherIndex=$(flag).index();
}

function delTr(){
	if(index!=0){
		$("#rcptDrugsTbl tr:eq("+index+")").remove();
	}
	index=0;
}

function delOtherTr(){ 
	if(otherIndex!=0){
		$("#rcptOtherCostInfoTbl tr:eq("+otherIndex+")").remove();
	}
	otherIndex=0;
}

function moneyChoose(){
	$("#moneyTbl").html("");
	var countryName =$.trim($("#countryNameChoose").val());
	if(countryName==""){
		alert("请填写国家名称");
		return;
	}
    $.ajax({  
        type: "POST",  
        url: "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 medicalChoose(){
	$("#medicalTbl").html("");
	var medicalName =$.trim($("#medicalName").val());
	if(medicalName==""){
		alert("请填写药品名称");
		return;
	}
    $.ajax({  
        type: "POST",  
        url: "medicalSearch.do",  
        data:{medicalName:medicalName},
        success: function(data){
        	var items = data.medicalList; 
        	 $.each(items, function(i, n){
        		 $("#medicalTbl").append("<tr id="+items[i].hmGjypbm+" onclick=\"choosMedicalEd(this)\">" +
               			"<td id='hmGjypbm'>"+items[i].hmGjypbm+"</td>"+
               			"<td id='hmMedChName'>"+items[i].hmMedChName+"</td>"+
               			"<td id='hmType' >"+items[i].hmType+"</td>");
               			if(items[i].hmMedType=="null"){
               				$("#medicalTbl").append("<td id='hmMedType'></td>");
               			}else{
               				$("#medicalTbl").append("<td id='hmMedType'>"+items[i].hmMedType+"</td>");
               			}
            
               			$("#medicalTbl").append("</tr>");
        	 });
        }  
    }); 
}

function choosEd(flag){
	 
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
	index=$(flag).index();
}
function choosMedicalEd(flag){
	 
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
	medicalIndex=$(flag).index();
}

function saveDiv(id) {
	var tr = $("#moneyTbl tr:eq("+moneyIndex+")"); 
	var currencySymbol = tr.attr("id"); 
	$("#currencySymbol").val(currencySymbol);
	var spanCurrentcyName = tr.find("td[id='chooseCountryName']").html();
	$("#spanCurrentcyName").html(spanCurrentcyName);
	$.closePopupLayer(id);
}
function saveMedicalDiv(id) {
	var tr = $("#medicalTbl tr:eq("+medicalIndex+")"); 
	var drugsCode = tr.attr("id"); 
	var hmMedType = tr.find("td[id='hmMedType']").html();
	var drugsName = tr.find("td[id='hmMedChName']").html();
	var hmType = tr.find("td[id='hmType']").html();
	var drugstr = $("#rcptDrugsTbl tr:eq("+index+")");
	drugstr.find("INPUT[id='drugsCode']").val(drugsCode);
	drugstr.find("INPUT[id='drugsName']").val(drugsName);
	drugstr.find("INPUT[id='medicalInsType']").val(hmType);
	drugstr.find("INPUT[id='dosageForm']").val(hmMedType);
	
	$.closePopupLayer(id);
}

function sumMoney(moneyInput){
checkMoney(moneyInput);
	
	var tr = $("#billDetailTable").find("tr");
	var billMoney = 0.00;
	var ownMoney = 0.00;
	var classOwnMoney = 0.00;
	var ybjfMoney = 0.00;
	var dsfzfMoney = 0.00;
	var kftzMoney = 0.00;
	var treatmentTimeSum = 0;
	for(var i=1;i<tr.length-1;i++){
		var billAmt = $(tr[i]).find("INPUT[id='billAmt']").val();
		var ownAmt = $(tr[i]).find("INPUT[id='ownAmt']").val();
		var divAmt = $(tr[i]).find("INPUT[id='divAmt']").val();
		var medIns = $(tr[i]).find("INPUT[id='medIns']").val();
		var thirdPay = $(tr[i]).find("INPUT[id='thirdPay']").val();
		var deductionsAmt = $(tr[i]).find("INPUT[id='deductionsAmt']").val();
		var treatmentTime = $(tr[i]).find("INPUT[id='treatmentTime']").val();
		if(billAmt!=""){
			billMoney = billMoney+parseFloat(billAmt);
		}
		if(ownAmt!=""){
			ownMoney = ownMoney+parseFloat(ownAmt);
		}
		if(divAmt!=""){
			classOwnMoney = classOwnMoney+parseFloat(divAmt);
		}
		if(medIns!=""){
			ybjfMoney = ybjfMoney+parseFloat(medIns);
		}
		if(thirdPay!=""){
			dsfzfMoney = dsfzfMoney+parseFloat(thirdPay);
		}
		if(deductionsAmt!=""){
			kftzMoney = kftzMoney+parseFloat(deductionsAmt);
		}
		if(treatmentTime!=""){
			treatmentTimeSum = treatmentTimeSum + parseInt(treatmentTime);
		}
		$("#billAmtSum").val(parseFloat(billMoney).toFixed(2));
		$("#ownAmtSum").val(parseFloat(ownMoney).toFixed(2));
		$("#divAmtSum").val(parseFloat(classOwnMoney).toFixed(2));
		$("#medInsSum").val(parseFloat(ybjfMoney).toFixed(2));
		$("#thirdPaySum").val(parseFloat(dsfzfMoney).toFixed(2));
		$("#deductionsAmtSum").val(parseFloat(kftzMoney).toFixed(2));
		$("#treatmentTimeSum").val(parseInt(treatmentTimeSum));
	}
	var overallPay=$("#overallPay").val();
	var attachPay=$("#attachPay").val();
	var medInsSums=0.00;	
	if($.trim(overallPay)!=""){
		medInsSums=medInsSums+parseFloat(overallPay);
	}
	if($.trim(attachPay)!=""){
		medInsSums=medInsSums+parseFloat(attachPay);
	}
	if($("#isBpo").val()=="Y"){
		$("#medInsSum").val(medInsSums);
	}
}
function checkMoney(moneyInput){
	if($(moneyInput).val()!=""){
//		var mny = /(^\d+$)|(^\d+.\d+$)/g;
		var mny = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
		if(!mny.test($(moneyInput).val())){
			$(moneyInput).val("");
		}
	}
}
//校验自负比例
function checkRate(rate){
	if($(rate).val()!=""){
//		var mny = /(^\d+$)|(^\d+.\d+$)/g;
		var mny = /^([1-9][\d]{0,7}|0)(\.[\d]{1,4})?$/;
		if(!mny.test($(rate).val())){
			$(rate).val("");
		}
	}
	if($(rate).val()!=""&&$(rate).val()!=undefined){
		var num = parseInt($(rate).val());
		if(num>100||num<0){
			$(rate).val("");
		}
	}
	
}
