//验证是否为数字
function reg_Num(str){
	if (str.length==0){return false;}
	var Letters = "1234567890.";

	for (i=0;i<str.length;i++){
		var CheckChar = str.charAt(i);
		if (Letters.indexOf(CheckChar) == -1){return false;}
	}
	return true;
}

//本金还款的月还款额(参数: 年利率 / 贷款总额 / 贷款总月份 / 贷款当前月0～length-1)
function getMonthMoney2(lilv,total,month,cur_month){
	var lilv_month = lilv / 12;//月利率
	var benjin_money = total/month;
	return (total - benjin_money * cur_month) * lilv_month + benjin_money;
}

//本息还款的月还款额(参数: 年利率/贷款总额/贷款总月份)
function getMonthMoney1(lilv,total,month){
	var lilv_month = lilv / 12;//月利率
	return total * lilv_month * Math.pow(1 + lilv_month, month) / ( Math.pow(1 + lilv_month, month) -1 );
}

//主要计算函数main（按商业贷款模式进行）
function ext_total(){
	//先清空月还款数下拉框
	while ((k=document.calc2.month_money.length-1)>=0){
		document.calc2.month_money.options.remove(k);
	}

//    alert("calc2 =" + document.calc2.daikuan_total.value);
//    alert("this =" + document.calc2.daikuan_total.value);
//    alert("trial_form =" + document.trial_form.daikuan_total.value); return false;
	//贷款总额
	if ((document.calc2.daikuan_total.value.length==0 || reg_Num(document.calc2.daikuan_total.value)!=true))
	{alert("请以数字方式填写贷款总额");document.calc2.daikuan_total.focus();return false;}
	else{
		var daikuan_total = document.calc2.daikuan_total.value;
	}

	//贷款期限
	if ((document.calc2.years.value.length==0 || reg_Num(document.calc2.years.value)!=true))
	{
		alert("请以数字方式填写贷款期数");document.calc2.years.focus();return false;
	}
	else{
		var month = document.calc2.years.value;
	}

	//还款方式
	var payback_type=document.calc2.payback_type.value;

	//利率
	if ((document.calc2.lilvinput.value.length==0 || reg_Num(document.calc2.lilvinput.value)!=true))
	{alert("请以数字方式填写利率,注意不要含有\"%\"");document.calc2.lilvinput.focus();return false;}
	else{
		var lilv = document.calc2.lilvinput.value/100;
	}

	//	alert(daikuan_total);
	//	alert(month);
	//	alert(payback_type);
	//	alert(lilv);

	//1.本金还款
	if(payback_type == 1){
		//月还款
		var all_total2 = 0;
		var month_money = "";
		for(j=0;j<month;j++) {
			//调用函数计算: 本金月还款额
			huankuan = getMonthMoney2(lilv,daikuan_total,month,j);
			all_total2 += huankuan;
			huankuan = Math.round(huankuan*100)/100;
			month_money += (j+1) +"月," + huankuan + "(元)\n";
		}
		changeLoantime(payback_type);
		//				alert("月换款:"+month_money);
		document.calc2.month_money.value = month_money;
		//支付利息款
		//				alert("支付利息款:"+Math.round( (all_total2 - daikuan_total) *100)/100);
		document.calc2.rest_total.value = Math.round( (all_total2 - daikuan_total) *100)/100;
		//还款总额
		//				alert("还款总额:"+Math.round(all_total2*100)/100);
		document.calc2.all_total2.value = Math.round(all_total2*100)/100;
	}
	//2.本息还款
	if(payback_type == 2){
		//月均还款
		var month_money1 = getMonthMoney1(lilv,daikuan_total,month);//调用函数计算
		document.calc2.month_money.value = Math.round(month_money1*100)/100 + "(元)";
		changeLoantime(payback_type);
		//				alert("月均还款:"+Math.round(month_money1*100)/100 + "(元)");
		//还款总额
		var all_total1 = month_money1 * month;
		//				alert("还款总额:"+Math.round(all_total1*100)/100);
		document.calc2.all_total2.value = Math.round(all_total1*100)/100;
		//支付利息款
		//				alert("支付利息款:"+Math.round( (all_total1 - daikuan_total) *100)/100);
		document.calc2.rest_total.value = Math.round( (all_total1 - daikuan_total) *100)/100;
	}
}

function changeLoantime(type)
{
	if(type == 1) 
	{
		document.getElementById("title").innerHTML="各月本息合计";		
		document.getElementById("list2bg").style.height= '160px';
		document.getElementById("list2bg").style.background= 'transparent url(../trialtools/images/j_07_2.gif) no-repeat scroll center top';
		document.getElementById("testareaheight").style.height= '50px';
		//#由于IE浏览器不支持下面的方式而改用纯JS
		//$("#list2bg").css({"height":"160px;","background":"transparent url(../trialtools/images/j_07_2.gif) no-repeat scroll center top"});
		//$("#testareaheight").css({ "height":"50px;"});
 	}
	else
	{		
		document.getElementById("title").innerHTML="每月平均还款";
		document.getElementById("list2bg").style.height= '120px';
		document.getElementById("list2bg").style.background= 'transparent url(../trialtools/images/j_07.gif) no-repeat scroll center top';
		document.getElementById("testareaheight").style.height= '20px';
		//$("#list2bg").css({"height":"120px;","background":"transparent url(../trialtools/images/j_07.gif) no-repeat scroll center top" });
		//$("#testareaheight").css({ "height":"20px;" });
	}
}

$(document).ready(function(){
    document.getElementById("list2bg").style.height= '120px';
	document.getElementById("list2bg").style.background= 'transparent url(../trialtools/images/j_07.gif) no-repeat scroll center top';
	document.getElementById("testareaheight").style.height= '20px';
}); 


function clear_all()
{
	var obj=document.calc2.elements;
	for(var m=0;m<obj.length;m++){
		var v=obj[m].value;
		if(v!="")
		{
			if(v=="计算" ){
			obj[m].value="计算";
			}else{
				if(v=="清除" ){
					obj[m].value="清除";
					}else{
					obj[m].value="";}
			}
			
		}
	}
}