//定义各个按揭年段和按揭类型的年利率
shangyeRateTable = new Array(6);//商业贷款
gjjRateTable = new Array(3);//公积金贷款
shangyeRateTable[0] = 5.58;//1年
shangyeRateTable[1] = 5.67;//2年、3年
shangyeRateTable[2] = 5.94;//4年、5年
shangyeRateTable[3] = 6.12;//6-30年
gjjRateTable[0] = 3.51;//1-5年
gjjRateTable[1] = 4.05;//6-30年

//注意调用函数时，字符型参数要加''，我忘记了，花了我很长时间debug，惨
function setRate(type,thisel,el){//设置默认年利率
	var el=document.getElementById(el);
	var thisel=document.getElementById(thisel);
	sel = thisel.options[thisel.selectedIndex].value;
	if (type=="shangye"){
		if (sel==1) rate=shangyeRateTable[0];
		if (sel>=2 && sel<=3) rate=shangyeRateTable[1];
		if (sel>=4 && sel<=5) rate=shangyeRateTable[2];
		if (sel>=6 && sel<=30) rate=shangyeRateTable[3];
	}
	if (type=="gjj"){
		if (sel>=1 && sel<=5) rate=gjjRateTable[0];
		if (sel>=6 && sel<=30) rate=gjjRateTable[1];
	}
	el.value=rate;
}

function collapsible(title,el) {
	if (document.getElementById){
		var el=document.getElementById(el).style;
		var title=document.getElementById(title);
		if (el.display == "block")
			{el.display = "none";	title.innerHTML="显示详细月供表";hide('resultBenjinDetailTitle');}
		else	
			{el.display = "block";	title.innerHTML="隐藏详细月供表";show('resultBenjinDetailTitle');}
	} 
}

function hide(el) {
	if (document.getElementById){
		var el=document.getElementById(el);
		el.style.display = "none";	
	} 
}


function show(el) {
	if (document.getElementById){
		var el=document.getElementById(el);
		el.style.display = "block";	
	} 
}

function calcBx(formid)
{// 等额本息，不显示月供款明细
	var formid=document.getElementById(formid);
	//alert("hell");
	amount=parseFloat(formid.amount.value);
	year=parseInt(formid.year.value);
	nlx=parseFloat(formid.rate.value)/100;
	qsh=12*year;
	ylx=nlx/12;
	yj=Math.round((ylx/(1-(1/(Math.pow(1+ylx,qsh)))))*amount*100,5)/100;
	hkzy=yj*qsh;
	//show brief result
	hide("resultBenjin");
	show("resultBenxi");
	formid.reYgkBx.value=yj;//每月供款额
	formid.reGkBx.value=Math.round(hkzy,5);//累计供款额
	formid.reLxBx.value=Math.round((hkzy-amount),5);//累计利息总额
	
}

function calcBj(formid)
{// 等额本金，显示月供款明细
	var formid=document.getElementById(formid);
	amount=parseFloat(formid.amount.value);
	year=parseInt(formid.year.value);
	nlx=parseFloat(formid.rate.value)/100;
	qsh=12*year;
	ylx=nlx/12;
	yj=Math.round((amount/qsh)*100,5)/100;
	//show brief result
	hide("resultBenxi");
	show("resultBenjin");
	formid.reGkBj.value=Math.round((amount*ylx*(qsh+1)/2)+amount,5);//累计供款额
	formid.reLxBj.value=Math.round((amount*ylx*(qsh+1)/2),5);//累计利息总额

	outdetail="";
	outdetail+=('<table cellspacing="0" rules="all" border="1"style="width:100%;border-collapse:collapse;">');
	//outdetail+=('<tr style="background-color:#58E1F1;"><td>还款期数</td><td>每期还款额</td><td>本期应还本金</td><td>本期应还利息</td><td>剩余本金</td></tr>');
	sybj=amount;
	yhbj=0;
	yhlx=0;
	for (i = 1; i <= qsh; i++)
	{
		if (i%2==0)
			rowTag='<tr style="background-color:#DAFDF9;">';
		else
			rowTag='<tr style="background-color:#fff;">';
		bjlx=Math.round(sybj*ylx*100,5)/100;
		yhlx=Math.round((yhlx+bjlx)*100,5)/100;
		yhbj=Math.round((yhbj+yj)*100,5)/100;
		sybj=Math.round((amount-yhbj)*100,5)/100;
		outdetail += rowTag+'<td width=20%>'+i+'</td>';
		outdetail += '<td width=20%>'+Math.round((yj+bjlx)*100,5)/100+'</td>';
		outdetail += '<td width=20%>'+yj+'</td>';
		outdetail += '<td width=20%>'+bjlx+'</td>';
		outdetail += '<td width=20%>'+sybj+'</td></tr>';
	}
	outdetail+=('<tr><td>合计</td><td>'+(yhbj+yhlx)+'</td><td>'+yhbj+'</td><td>'+yhlx+'</td><td>/</td></tr></table>');
	document.getElementById("resultBenjinDetail").innerHTML=outdetail;
}


function changeBD(value, bd1, bd2, bd3)
{
	if (value == 1)
	{	
		document.getElementById(bd1).style.display = 'block'; //show
		document.getElementById(bd2).style.display = 'none'; //
		document.getElementById(bd3).style.display = 'none'; //
	}
	else
	{
		document.getElementById(bd1).style.display = 'none'; //
		document.getElementById(bd2).style.display = 'block'; //show
		document.getElementById(bd3).style.display = 'none'; //
	}
}

function zhuhe(type)
{//商业公积金组合贷款
//	if(!CheckElem($('year'), "\u8d37\u6b3e\u5e74\u9650")) return false;
//	if(!IsIntYear($('year'), '', "\u8d37\u6b3e\u5e74\u9650")) return false;
//	if(!CheckElem($('com_amount'), "\u5546\u4e1a\u8d37\u6b3e\u91d1\u989d")) return false;
//	if(!IsIntAmount($F('com_amount'), '1', "\u5546\u4e1a\u8d37\u6b3e\u91d1\u989d")) return false;
//	if(!CheckElem($('com_rate'), "\u5546\u4e1a\u8d37\u6b3e\u5229\u7387")) return false;
//	if(!IsIntAmount($F('com_rate'), '3', "\u5546\u4e1a\u8d37\u6b3e\u5229\u7387")) return false;
//	if(!CheckElem($('fund_amount'), "\u516c\u79ef\u91d1\u8d37\u6b3e\u91d1\u989d")) return false;
//	if(!IsIntAmount($F('fund_amount'), '4', "\u516c\u79ef\u91d1\u8d37\u6b3e\u91d1\u989d")) return false;
//	if(!CheckElem($('fund_rate'), "\u516c\u79ef\u91d1\u8d37\u6b3e\u5229\u7387")) return false;
//	if(!IsIntAmount($F('fund_rate'), '3', "\u516c\u79ef\u91d1\u8d37\u6b3e\u5229\u7387")) return false;
		
	var c	=	type;		//huan kuan fang shi
	var d	=	$F('year');		//dai kuan qi xian
	var e	=	d*12;				//dai kuan zong yue shu
	var f	=	$F('com_amount');	//shang dai dai kuan jin e
	var i	=  $F('fund_amount');	//gong ji jin dai kuan jin e
	var g	=	$F('com_rate')/1200;	//dai kuan yue li lv
	var h	=	$F('fund_rate')/1200;	//dai kuan yue li lv
	if (c == 'benxi')
	{
		changeBD(1, 'resultBenxi', 'resultBenjin', 'resultBenjinDetail');//界面切换到本息
		//yue jun huan kuan
		com_average_monthly = g*f+g*f/(Math.pow((1+g), e)-1);
		fund_average_monthly = h*i+h*i/(Math.pow((1+h), e)-1);
		//zhi fu li xi 
		com_pay_int = com_average_monthly*e - f;
		fund_pay_int = fund_average_monthly*e - i;
		//huan kuan zong e
		com_repay_total = com_average_monthly*e;
		fund_repay_total = fund_average_monthly*e;
		
		
//		$('com_average_monthly').value = com_average_monthly.toFixed(2);
//		$('fund_average_monthly').value = fund_average_monthly.toFixed(2);
//		$('com_pay_int').value = com_pay_int.toFixed(2);
//		$('fund_pay_int').value = fund_pay_int.toFixed(2);
//		$('com_repay_total').value = com_repay_total.toFixed(2);
//		$('fund_repay_total').value = fund_repay_total.toFixed(2);
		$('tot_average_monthly').value = parseInt(com_average_monthly.toFixed(2))+parseInt(fund_average_monthly.toFixed(2));
		$('tot_pay_int').value = parseInt(com_pay_int.toFixed(2))+parseInt(fund_pay_int.toFixed(2));
		$('tot_repay_total').value = parseInt(com_repay_total.toFixed(2))+parseInt(fund_repay_total.toFixed(2));
	}
	else if (c == 'benjin')
	{
		changeBD(2, 'resultBenxi', 'resultBenjin', 'resultBenjinDetail');//界面切换到本金
		com_y = f/e+f*g;//li xi zhi chu
		fund_y = i/e+i*h;
		com_x = 0;
		com_sum = 0;
		fund_x = 0;
		fund_sum = 0;
		
		template='<table cellspacing="0" rules="all" border="1"style="width:100%;border-collapse:collapse;">';
		//template += '<tr style="background-color:#58E1F1;"><td>还款期数</td><td>商贷当期月供</td><td>公积金当期月供</td><td>合计</td><td>剩余本金</td></tr>';
		for (k=1; k<=e; k++)
		{
			if (k%2==0)
				rowTag='<tr style="background-color:#DAFDF9;">';
			else
				rowTag='<tr style="background-color:#fff;">';
			com_sum +=  (f-com_x)*g;
			com_y = f/e + (f-com_x)*g;
			com_x += f/e;
			com_z = f-com_x;
			
			fund_sum +=  (i-fund_x)*h;
			fund_y = i/e + (i-fund_x)*h;
			fund_x += i/e;
			fund_z = i-fund_x;
			tot = com_y + fund_y;
			z = com_z + fund_z;
			template += rowTag+'<td width=20%>'+k+'</td><td width=20%>'+com_y.toFixed(2)+'</td><td  width=20%>'+fund_y.toFixed(2)+'</td><td  width=20%>'+tot.toFixed(2)+'</td><td  width=20%>'+z.toFixed(2)+'</td></tr>';
		}
		template+='</table>';
		$('resultBenjinDetail').innerHTML = template;
		
		com_int_expenditure = com_sum;
		fund_int_expenditure = fund_sum;
		com_repay_total_2 = parseInt(f)+ parseInt(com_int_expenditure);
		fund_repay_total_2 = parseInt(i)+ parseInt(fund_int_expenditure);
//		$('com_int_expenditure').value = com_int_expenditure.toFixed(2);
//		$('fund_int_expenditure').value = fund_int_expenditure.toFixed(2);
//		$('com_repay_total_2').value = com_repay_total_2.toFixed(2);
//		$('fund_repay_total_2').value = fund_repay_total_2.toFixed(2);
		$('int_expenditure').value = parseInt(com_int_expenditure.toFixed(2))+parseInt(fund_int_expenditure.toFixed(2));
		$('repay_total_2').value = parseInt(com_repay_total_2)+parseInt(fund_repay_total_2);
	}
}
function count(){//提前还贷
  if (document.tqhdjsq.dkzws.value==''){
       alert('请填入贷款总额');
	   document.tqhdjsq.dkzws.focus();
       return false;
  }else dkzys=parseFloat(document.tqhdjsq.dkzws.value);


if (document.tqhdjsq.dklv_class.value==''){
       alert('请填入利率');
	   document.tqhdjsq.dklv_class.focus();
       return false;
  }

  if(document.tqhdjsq.tqhkfs[1].checked && document.tqhdjsq.tqhkws.value==''){
    alert('请填入部分提前还款额度');
    return false;
   }
  s_yhkqs=parseInt(document.tqhdjsq.yhkqs.value);

  //月利率

//	if(document.tqhdjsq.dklx[0].checked){
	//	if (s_yhkqs>60){
	//		dklv=getlilv(parseInt(document.tqhdjsq.dklv_class.value),2,10)/12; //公积金贷款利率5年以上4.23%
	//	}else{
	//		dklv=getlilv(parseInt(document.tqhdjsq.dklv_class.value),2,3)/12;  //公积金贷款利率5年(含)以下3.78%
	//	}
	//}
	//if(document.tqhdjsq.dklx[1].checked){
	//	if (s_yhkqs>60){
	//		dklv=getlilv(parseInt(document.tqhdjsq.dklv_class.value),1,10)/12; //商业性贷款利率5年以上5.31%
	//	}else{
	//		dklv=getlilv(parseInt(document.tqhdjsq.dklv_class.value),1,3)/12; //商业性贷款利率5年(含)以下4.95%
	//	}
	//}
	dklv=document.tqhdjsq.dklv_class.value/1200;
  //已还贷款期数
  yhdkqs=(parseInt(document.tqhdjsq.tqhksjn.value)*12+parseInt(document.tqhdjsq.tqhksjy.value))-(parseInt(document.tqhdjsq.yhksjn.value)*12 + parseInt(document.tqhdjsq.yhksjy.value));

  if(yhdkqs<0 || yhdkqs>s_yhkqs){
    alert('预计提前还款时间与第一次还款时间有矛盾，请查实');
    return false;
   }

  yhk=dkzys*(dklv*Math.pow((1+dklv),s_yhkqs))/(Math.pow((1+dklv),s_yhkqs)-1);
  yhkjssj=Math.floor((parseInt(document.tqhdjsq.yhksjn.value)*12+parseInt(document.tqhdjsq.yhksjy.value)+s_yhkqs-2)/12)+'年'+((parseInt(document.tqhdjsq.yhksjn.value)*12+parseInt(document.tqhdjsq.yhksjy.value)+s_yhkqs-2)%12+1)+'月';
  yhdkys=yhk*yhdkqs;

  yhlxs=0;
  yhbjs=0;
  for(i=1;i<=yhdkqs;i++){
     yhlxs=yhlxs+(dkzys-yhbjs)*dklv;
     yhbjs=yhbjs+yhk-(dkzys-yhbjs)*dklv;
   }

  remark='';
  if(document.tqhdjsq.tqhkfs[1].checked){
    tqhkys=parseInt(document.tqhdjsq.tqhkws.value);
     if(tqhkys+yhk>=(dkzys-yhbjs)*(1+dklv)){
         remark='您的提前还款额已足够还清所欠贷款！';
     }else{
	        yhbjs=yhbjs+yhk;
            byhk=yhk+tqhkys;
			if(document.tqhdjsq.clfs[0].checked){
			  yhbjs_temp=yhbjs+tqhkys;
              for(xdkqs=0;yhbjs_temp<=dkzys;xdkqs++) yhbjs_temp=yhbjs_temp+yhk-(dkzys-yhbjs_temp)*dklv;
			  xdkqs=xdkqs-1;
              xyhk=(dkzys-yhbjs-tqhkys)*(dklv*Math.pow((1+dklv),xdkqs))/(Math.pow((1+dklv),xdkqs)-1);
              jslx=yhk*s_yhkqs-yhdkys-byhk-xyhk*xdkqs;
			  xdkjssj=Math.floor((parseInt(document.tqhdjsq.tqhksjn.value)*12+parseInt(document.tqhdjsq.tqhksjy.value)+xdkqs-2)/12)+'年'+((parseInt(document.tqhdjsq.tqhksjn.value)*12+parseInt(document.tqhdjsq.tqhksjy.value)+xdkqs-2)%12+1)+'月'; 
             }else{
		       xyhk=(dkzys-yhbjs-tqhkys)*(dklv*Math.pow((1+dklv),(s_yhkqs-yhdkqs)))/(Math.pow((1+dklv),(s_yhkqs-yhdkqs))-1);
               jslx=yhk*s_yhkqs-yhdkys-byhk-xyhk*(s_yhkqs-yhdkqs);
			   xdkjssj=yhkjssj;
			  }
       }
   }

  if(document.tqhdjsq.tqhkfs[0].checked || remark!=''){
    byhk=(dkzys-yhbjs)*(1+dklv);
    xyhk=0;
    jslx=yhk*s_yhkqs-yhdkys-byhk;
    xdkjssj=document.tqhdjsq.tqhksjn.value+'年'+document.tqhdjsq.tqhksjy.value+'月';
	}
  //show result
  show("result");
  document.tqhdjsq.ykhke.value=Math.round(yhk*100)/100;
  document.tqhdjsq.yhkze.value=Math.round(yhdkys*100)/100;
  document.tqhdjsq.yhlxe.value=Math.round(yhlxs*100)/100;
  document.tqhdjsq.gyyihke.value=Math.round(byhk*100)/100;
  document.tqhdjsq.xyqyhke.value=Math.round(xyhk*100)/100;
  document.tqhdjsq.jslxzc.value=Math.round(jslx*100)/100;
  document.tqhdjsq.yzhhkq.value=yhkjssj;
  document.tqhdjsq.xdzhhkq.value=xdkjssj;
  document.tqhdjsq.jsjgts.value=remark;
}



