/*计算器*/
function Calculator()
{
	/*借款人月工资总额*/
	this.borrowerSalary=0.0;
	/*借款人配偶月工资总额*/
	this.spouseSalary=0.0;
	/*借款人所在单位住房公积金缴存比例*/
	this.borrowerProportion=0.0;
	/*借款人配偶所在单位住房公积金缴存比例*/
	this.spouseProportion=0.0;
	/*缴存比例*/
	this.proportion=0;
	/*可贷款的年限*/
	/*还款月数*/
	this.expire=0;
	/*本金*/
	this.principal=0;
	/*月利率*/
	this.rate=0.0;
	/*月均还款*/
	this.monthDebt=0.0;
	/*还款总额*/
	this.totalDebt=0.0;
	/*支付利息款*/
	this.totalInterest=0.0;
	/*贷款总额*/
	this.totalLoan=0.0;
	/*首期付款*/
	this.intallment=0.0;
	/*单价*/
	this.priceUnit=0.0;
	/*面积*/
	this.area=0.0;
	/*按揭成数*/
	this.discount=0;
	/*月均还款----等额本息专用*/
	this.averageDebt=0.0;
	/*单价\面积\按揭成数*/
	this.COMPOUND=1000;
	/*根据贷款总额计算*/
	this.SIMPLE=1001;
}
/*设置月利率*/
Calculator.prototype.setRate=function(value)
{
	if(isNaN(parseFloat(value)))
	{
		alert("月利率必须为数字");
		return;
	}
	switch(parseInt(value))
	{
		case 1://商业贷款优惠利率(优惠15%) 2007年9月15日后执行 6-30年期
			this.rate=0.066555/12;
			break;
		case 2://商业贷款优惠利率(优惠15%) 2007年9月15日后执行 3-5年期
			this.rate=0.065025/12;
			break;
		case 3://商业贷款优惠利率(优惠15%) 2007年9月15日后执行 1-3年期
			this.rate=0.063495/12;
			break;
		case 4://商业贷款优惠利率(优惠15%) 2007年9月15日日后执行 1年期
			this.rate=0.061965/12;
			break;
		case 5://商业贷款基准利率 2007年9月15日后执行 6-30年期
			this.rate=0.0783/12;
			break;
		case 6://商业贷款基准利率 2007年9月15日后执行 3-5年期
			this.rate=0.0765/12;
			break;
		case 7://商业贷款基准利率 2007年9月15日后执行 1-3年期
			this.rate=0.0747/12;
			break;
		case 8://商业贷款基准利率 2007年9月15日后执行 1年期
			this.rate=0.0729/12;
			break;
		case 9://公积金贷款利率 2007年9月15日后执行 1-5年期
			this.rate=0.0477/12;
			break;
		case 10://公积金贷款利率 2007年9月15日后执行 6-30年期
			this.rate=0.0522/12;
			break;
	}
}
/*设置按揭成数*/
Calculator.prototype.setDiscount=function(value)
{
	if(isNaN(parseFloat(value)))
	{
		alert("按揭成数必须为数字");
		return;
	}
	this.discount=parseFloat(value)/10;
}
/*公积金计算器*/
Calculator.prototype.calculateAccumulationFund=function()
{
	if(isNaN(parseFloat(this.borrowerSalary)))
	{
		alert("借款人月工资总额必须为数字!");
		return 0;
	}
	if(isNaN(parseFloat(this.spouseSalary)))
	{
		alert("借款人配偶月工资总额必须为数字!");
		return 0;
	}
	if(isNaN(parseFloat(this.borrowerProportion)))
	{
		alert("借款人所在单位住房公积金缴存比例必须为数字!");
		return 0;
	}
	if(isNaN(parseFloat(this.spouseProportion)))
	{
		alert("借款人配偶所在单位住房公积金缴存比例必须为数字!");
		return 0;
	}
	if(isNaN(parseFloat(this.expire)))
	{
		alert("可贷款的年限必须为数字");
		return 0;
	}
	var temp=(parseFloat(this.borrowerSalary)*(0.4+parseFloat(this.borrowerProportion))+parseFloat(this.spouseSalary)*(0.4+parseFloat(this.spouseProportion)))*12*parseFloat(this.expire);
	if(temp>400000)
	{
		temp=400000;
		alert("可贷款额度不能超过400000");
	}
	else
		temp=Math.round(temp*100)/100;
	return temp;
}
/*等额本金列表计算器*/
Calculator.prototype.calculateEqualPrincipal=function(type)
{
	if(isNaN(parseFloat(this.expire)) || parseFloat(this.expire)==0)
	{
		alert("还款月数必须为数字，且不能为0");
		return new Array();
	}
	
	switch(type)
	{
		case this.COMPOUND://多参数模式。
			if(isNaN(parseFloat(this.priceUnit)))
			{
				alert("单价必须为数字");
				return new Array();
			}
			if(isNaN(parseFloat(this.area)))
			{
				alert("面积必须为数字");
				return new Array();
			}
			this.principal=parseFloat(this.priceUnit)*parseFloat(this.area);
			this.totalLoan=parseFloat(this.priceUnit)*parseFloat(this.area)*parseFloat(this.discount);
			this.intallment=parseFloat(this.priceUnit)*parseFloat(this.area)*(1-parseFloat(this.discount));
			break;
		case this.SIMPLE://单参数模式。
			if(isNaN(parseFloat(this.totalLoan)))
			{
				alert("贷款总额必须为数字");
				return new Array();
			}
			break;
		default:
			alert("运算类型错误!")
			return new Array();
	}
	
	this.monthDebt=parseFloat(this.totalLoan)/parseFloat(this.expire);
	this.monthDebt=Math.round(this.monthDebt*100)/100;//月还款额。
	var list=new Array();
	this.totalDebt=0.0;
	this.totalInterest=0.0;
	for(var index=0;index<parseFloat(this.expire);index++)
	{
		var month=new Array();
		month["interest"]=(parseFloat(this.totalLoan)-this.monthDebt*index)*parseFloat(this.rate);
		month["interest"]=Math.round(month["interest"]*100)/100;
		month["debt"]=this.monthDebt;
		month["entirety"]=month["interest"]+month["debt"];
		month["entirety"]=Math.round(month["entirety"]*100)/100;
		list.push(month);
		this.totalDebt+=month["entirety"];
		this.totalInterest+=month["interest"];
	}
	this.totalDebt=Math.round(this.totalDebt*100)/100;
	this.totalInterest=Math.round(this.totalInterest*100)/100;
	this.intallment=Math.round(this.intallment*100)/100;
	this.totalLoan=Math.round(this.totalLoan*100)/100;
	this.principal=Math.round(this.principal*100)/100;
	return list;
}
/*等额本息计算器*/
Calculator.prototype.calculateEqualInterest=function(type)
{
	if(isNaN(parseFloat(this.expire)) || parseFloat(this.expire)==0)
	{
		alert("还款月数必须为数字，且不能为0");
		return new Array();
	}
	
	switch(type)
	{
		case this.COMPOUND://多参数模式。
			if(isNaN(parseFloat(this.priceUnit)))
			{
				alert("单价必须为数字");
				return new Array();
			}
			if(isNaN(parseFloat(this.area)))
			{
				alert("面积必须为数字");
				return new Array();
			}
			this.principal=parseFloat(this.priceUnit)*parseFloat(this.area);
			this.totalLoan=parseFloat(this.priceUnit)*parseFloat(this.area)*parseFloat(this.discount);
			this.intallment=parseFloat(this.priceUnit)*parseFloat(this.area)*(1-parseFloat(this.discount));
			break;
		case this.SIMPLE://单参数模式。
			if(isNaN(parseFloat(this.totalLoan)))
			{
				alert("贷款总额必须为数字");
				return new Array();
			}
			break;
		default:
			alert("运算类型错误!")
			return new Array();
	}
	this.averageDebt=this.totalLoan*this.rate*Math.pow(1+this.rate,this.expire)/(Math.pow(1+this.rate,this.expire)-1);
	this.averageDebt=Math.round(this.averageDebt*100)/100;
	var list=new Array();
	this.totalDebt=0.0;
	this.totalInterest=0.0;
	for(var index=1;index<=this.expire;index++)
	{
		var month=new Array();
		month["interest"]=this.totalLoan*this.rate*(Math.pow(1+this.rate,this.expire)-Math.pow(1+this.rate,index-1))/(Math.pow(1+this.rate,this.expire)-1);
		month["debt"]=this.totalLoan*this.rate*Math.pow(1+this.rate,index-1)/(Math.pow(1+this.rate,this.expire)-1);
		month["entirety"]=this.averageDebt;
		month["interest"]=Math.round(month["interest"]*100)/100;
		month["debt"]=Math.round(month["debt"]*100)/100;
		list.push(month);
		this.totalDebt+=month["entirety"];
		this.totalInterest+=month["interest"];
	}
	this.totalDebt=Math.round(this.totalDebt*100)/100;
	this.totalInterest=Math.round(this.totalInterest*100)/100;
	this.intallment=Math.round(this.intallment*100)/100;
	this.totalLoan=Math.round(this.totalLoan*100)/100;
	this.principal=Math.round(this.principal*100)/100;
	return list;
}
/*组全计算器函数组*/
Calculator.prototype.calculateTotalLoanValue=function()
{
	if(isNaN(parseFloat(this.priceUnit)))
	{
		alert("单价必须为数字");
		return 0;
	}
	if(isNaN(parseFloat(this.area)))
	{
		alert("面积必须为数字");
		return 0;
	}
	var temp=parseFloat(this.priceUnit)*parseFloat(this.area)*parseFloat(this.discount);
	return Math.round(temp*100)/100;
}
/*非负*/
function positiveNumber(me)
{
	if(me.value.indexOf("-")!=-1)
	{
		alert("您不能输入负数!");
		me.value="0";	
		me.focus();
	}
}
