function getMainLoaneePfcAmount(){
	var val = 0;
	var obj = document.getElementById('txtMainLoaneePfcAmount');
	if(isNum(obj.value)){
		val = obj.value	
	}
	return val;
}

function getMainLoaneePfcMonthPay(){
	var val = 0;
	var obj = document.getElementById('txtMainLoaneePfcMonthPay');
	if(isNum(obj.value)){
		val = obj.value;
	}	
	return val;
}

function getMainLoaneeExPfcAmount(){
	var val = 0;
	var obj = document.getElementById('txtMainLoaneeExPfcAmount');
	if(isNum(obj.value)){
		val = obj.value;	
	}	
	return val;
}

function getMainLoaneeAge(){
	var val = 0;
	var obj = document.getElementById('txtMainLoaneeAge');
	if(isNum(obj.value)){
		val = obj.value;	
	}
	return val;	
}

function getHousePrice(){
	var val = 0;
	var obj = document.getElementById('txtHousePrice');
	if(isNum(obj.value)){
		val = obj.value * 10000;	
	}
	return val;
}

function getSecondaryPfcAmount(){
	var val = 0;
	var obj = document.getElementById('txtSecondaryPfcAmount');
	if(isNum(obj.value)){
		val = obj.value;	
	}	
	return val;
}

function getSecondaryExPfcAmount(){
	var val = 0;
	var obj = document.getElementById('txtSecondaryExPfcAmount');
	if(isNum(obj.value)){
		val = obj.value;	
	}
	return val;	
}

function getSecondaryMonthPay(){
	var val = 0;
	var obj = document.getElementById('txtSecondaryPfcMonthPay');
	if(isNum(obj.value)){
		val = obj.value;	
	}
	return val;
}

function getMainLoaneeGender(){
	var val = 0;
	var obj = document.getElementsByName('radMainLoaneeGender');
	if(obj[0].checked){
		val = 1;
	}else if(obj[1].checked){
		val = 2;	
	}
	return val;	
}

function getHouseType(){
	var val = 0;
	var obj = document.getElementsByName('radHouseType');
	if(obj[0].checked){
		val = 1;	
	}else if(obj[1].checked){
		val = 2;	
	}	
	return val;
}

function getMostLoanYear(houseType){
	var val = 0;
	if(houseType == 1){
		val = 30;	
	}else{
		val = 15;	
	}
	return val;
}

function getMostHousePriceRate(houseType){
	var val = 0.800;
	return val;
}

function getMainLoaneeMostAge(gender){
	var val = 0;
	if(gender == 1){
		val = 65;	
	}else if(gender == 2){
		val = 60;
	}
	return val;
}

function calcLoanYear(age,mostAge,mostLoanYear){
	var val = 0;
	if(mostAge > age){
		val = mostAge - age ;
		if(val > mostLoanYear){
			val = mostLoanYear;
		}	
	}
	return val;	
}

function calcLoanAmount(){
	var mainLoaneePfcAmount = getMainLoaneePfcAmount();
	if(mainLoaneePfcAmount <= 0){
		alert('请填写主贷人的基本公积金余额');document.getElementById('txtMainLoaneePfcAmount').select();return false;	
	}
	var mainLoaneeExPfcAmount = getMainLoaneeExPfcAmount();
	var mainLoaneePfcMonthPay = getMainLoaneePfcMonthPay();
	if(mainLoaneePfcMonthPay <= 0){
		alert('请填写主贷人公积金月缴存额');document.getElementById('txtMainLoaneePfcMonthPay').select();return false;	
	}
	
	var mainLoaneeAge = parseInt(getMainLoaneeAge());
	if(mainLoaneeAge <= 17 || mainLoaneeAge >=66){
		alert('主贷人年龄必须为18岁到65岁之间');document.getElementById('txtMainLoaneeAge').focus();return false;
	}
	var mainLoaneeGender = parseInt(getMainLoaneeGender());
	if(mainLoaneeGender <= 0 || mainLoaneeGender > 2){
		alert('请选择主贷人性别');document.getElementById('radMainLoaneeGender1').focus();return false;
	}
	var housePrice = getHousePrice();
	if(housePrice <= 0){
		alert('请填写房屋总价');document.getElementById('txtHousePrice').select();return false;	
	}
	var houseType = parseInt(getHouseType());
	if(houseType<=0 || houseType>2){
		alert('请选择房屋类型');document.getElementById('radHouseType1').focus();return false;	
	}
	var secondaryPfcAmount = getSecondaryPfcAmount();
	var secondaryExPfcAmount = getSecondaryExPfcAmount();
	var secondaryMonthPay = getSecondaryMonthPay();
	
	
	var basicLoanLimit = 300000;
	var extraLoanLimit = 100000;
	var basicTotalLoanLimit = 600000;
	var extraTotalLoanLimit = 200000;
	//var onePersonExtraLoanLimit = 100000;
	var basicLoanParm = 40;
	var extraLoanParm = 20;
	var mostLoanYear = getMostLoanYear(houseType);
	var housePriceRate = getMostHousePriceRate(houseType);
	var houseLoanLimit = housePrice * housePriceRate;
	
	var loanYear = calcLoanYear(mainLoaneeAge,getMainLoaneeMostAge(mainLoaneeGender),mostLoanYear);
	var mainLoaneeYearPay = mainLoaneePfcMonthPay / 0.14 / 2 * 12;
	var secondaryYearPay = secondaryMonthPay / 0.14 / 2 * 12;
	var mainLoaneeTotalPay = mainLoaneeYearPay * loanYear;
	var secondaryTotalPay = secondaryYearPay * loanYear;
	
	var mainLoaneePfcLoanAmount = Math.round(mainLoaneePfcAmount * basicLoanParm/ 1000) * 1000;
	if(mainLoaneePfcLoanAmount > basicLoanLimit)
		mainLoaneePfcLoanAmount = basicLoanLimit;
		
	var secondaryPfcLoanAmount = Math.round(secondaryPfcAmount * basicLoanParm / 1000) * 1000;
	if(secondaryPfcLoanAmount > basicLoanLimit)
		secondaryPfcLoanAmount = basicLoanLimit;
		
	var mainLoaneeExPfcLoanAmount = Math.round(mainLoaneeExPfcAmount * extraLoanParm / 1000) * 1000;
	if(mainLoaneeExPfcLoanAmount > extraLoanLimit)
		 mainLoaneeExPfcLoanAmount = extraLoanLimit;
		 
	var secondaryExPfcLoanAmount = Math.round(secondaryExPfcAmount * extraLoanParm / 1000) * 1000;
	if(secondaryExPfcLoanAmount > extraLoanLimit)
		 secondaryExPfcLoanAmount = extraLoanLimit;
	
	if(mainLoaneePfcLoanAmount > 0 && mainLoaneePfcLoanAmount > mainLoaneeTotalPay)
		mainLoaneePfcLoanAmount = Math.ceil(mainLoaneeTotalPay / 1000) * 1000;
	
	if(secondaryPfcLoanAmount > 0 && secondaryPfcLoanAmount > secondaryTotalPay)
		secondaryPfcLoanAmount = Math.ceil(secondaryTotalPay / 1000) * 1000;
	
	var pfcLoanAmount = mainLoaneePfcLoanAmount + secondaryPfcLoanAmount;
	var exPfcLoanAmount = mainLoaneeExPfcLoanAmount + secondaryExPfcLoanAmount;
	
	if(pfcLoanAmount > basicTotalLoanLimit)
		pfcLoanAmount = basicTotalLoanLimit;
	
	if(exPfcLoanAmount > extraTotalLoanLimit)
		exPfcLoanAmount = extraTotalLoanLimit;
		
	
	var loanAmount = pfcLoanAmount + exPfcLoanAmount;
	if(loanAmount>houseLoanLimit)
		loanAmount = houseLoanLimit;
	
	var tmpLoanYear = loanYear;
	if(mainLoaneeYearPay + secondaryYearPay > 0)
		tmpLoanYear = Math.ceil(loanAmount / (mainLoaneeYearPay + secondaryYearPay));
	
	var minLoanYear = loanYear;
	var maxLoanYear = loanYear;
	if(tmpLoanYear < loanYear){
		minLoanYear = tmpLoanYear;	
	}
	
	document.getElementById('txtPfcLoanAmount').value = pfcLoanAmount;
	document.getElementById('txtExPfcLoanAmount').value = exPfcLoanAmount;
	document.getElementById('txtLoanAmount').value = loanAmount;
	document.getElementById('txtLoanYears').value = minLoanYear + '-' + maxLoanYear;
	
	setSMSHouseType(houseType);
	document.getElementById('txtSmsHousePrice').value = housePrice / 10000;
	
	
}

function setSMSHouseType(houseType){
	var obj = document.getElementsByName('txtSmsHouseType');
	if(houseType == 1){
		obj[0].checked = true;	
	}else if(houseType = 2){
		obj[1].checked = true;	
	}	
}