// investment root path
var rootPath = '..';

function log10(val) {
	return Math.log(val) / Math.LN10;
}

function changeMFundAssetType() {
	document.form1.action = rootPath + '/exec/SearchMFund';
	document.form1.submit();
	return true;
}

function addNewMember() {
	document.form1.action = rootPath + '/exec/CustomerRegistration?act=add';
	document.form1.submit();
	return true;
}

function displayIPMainPage(userKey) {
	document.form1.userKey.value = userKey;
	document.form1.action = rootPath + '/exec/ViewIPCustomer';
	document.form1.submit();
	return true;
}

function displayCustomerMainPage(userKey) {
	document.form1.userKey.value = userKey;
	document.form1.action = rootPath + '/exec/CustomerMain';
	document.form1.submit();
	return true;
}

function deletePort() {
	if (window.confirm("Are you sure to delete ?") == true) {
		document.form1.action = rootPath + '/exec/DeletePort';
		document.form1.submit();
		return true;
	}
}

function getMFundDetail(mFundKey,portKey) {
	document.form1.mFundKey.value = mFundKey;
	document.form1.portKey.value=portKey;
	document.form1.action = rootPath + '/exec/MFundDetail';
	document.form1.submit();
	return true;	
}

// calculate actual diffrent
function calculateActualDiff(defaultValue ,actualValue ,diff){
	var result = 0;
	var totalResult = 0;
	var plus = "+";	
	if(actualValue.value == ""){
		diff.value = "";
		return;
	}
	result =  actualValue.value - defaultValue;
	if(result > 0 ){		
		diff.value=plus+result;		
	}else{
		diff.value=result;
	}
}
// calculate actual total
function calculateActualTotal(aForm){
	var total = 0;
	var v1 = 0;
	var v2 = 0;
	var v3 = 0;
	aForm.actualTotal.value = 0;
	v1 = aForm.INVASS0001.value;
	v2 = aForm.INVASS0002.value;
	v3 = aForm.INVASS0003.value;	
	total = v1 -(-v2);
	total = total -(-v3);	
	aForm.actualTotal.value = total;
}

// create customer registration
function createCustomerInfo() {
   	var status = validateForm(document.form1);
   	
   	if (status) {
		document.form1.action = rootPath + '/exec/reg_ac.asp';
		document.form1.submit();
		return true;
   	} else {
		alert('กรุณาใส่ข้อมูลให้ครบ.');
   	}
   	
   	return false;
}

// update customer registration
function updateCustomerInfo() {
   	var status = validateForm(document.form1);
   	
   	if (status) {
		document.form1.action = rootPath + '/exec/UpdateCustomerInfo';
		document.form1.submit();
		return true;
   	} else {
		alert('กรุณาใส่ข้อมูลให้ครบ.');
   	}
   	
   	return false;
}

// insurance plan tool
function calculateInsurancePlan() {
	var status = validateForm(document.form1);

   	if (status) {
		document.form1.action = rootPath + '/exec/CalculateInsurancePlan';
		document.form1.submit();
		return true;
   	} else {
		alert('กรุณาใส่ข้อมูลให้ครบ.');
   	}
   	
   	return false;
}

function calcuFamilyPayPerYear() {
	var familyPayMonth = currencyToNumber(document.form1.familyPayMonth.value);	
	document.form1.familyPayYear.value = formatCurrency(familyPayMonth * 12);
}

function calcuChildPayPerYear() {
	var childPayMonth = currencyToNumber(document.form1.childPayMonth.value);
	document.form1.childPayYear.value = formatCurrency(childPayMonth * 12);
}

// retirement plan tool
function calculateRetirementPlan() {
	var status = validateForm(document.form1);

   	if (status) {
		document.form1.action = rootPath + '/exec/CalculateRetirementPlan';
		document.form1.submit();
		return true;
   	} else {
		alert('กรุณาใส่ข้อมูลให้ครบ.');
   	}
   	
   	return false;
}

function calculateSalaryPerYear() {
	var salaryPerMonth = currencyToNumber(document.form1.salaryPerMonth.value);
	document.form1.salaryPerYear.value = formatCurrency(salaryPerMonth * 12);
}

// calculate network
function calculateNetworth(){
	document.form1.action = rootPath+"/exec/PerformNetworth";	
	document.form1.performNetworth.value="CALCULATE_NETWORTH";
	document.form1.submit();
}

// perform network
function saveNetworth(){
	document.form1.action = rootPath+"/exec/PerformNetworth";
	document.form1.performNetworth.value="SAVE_NETWORTH";
	document.form1.submit();
}

// calculate quick test
function calculateQuizTest(){
	document.form1.action = rootPath+"/exec/PerformQuizTest";
	document.form1.performQuizTest.value="CALCULATE_QUIZTEST";
	document.form1.submit();
}

// perform save quick test
function saveQuizTest(){
	document.form1.action = rootPath+"/exec/PerformQuizTest";
	document.form1.performQuizTest.value="SAVE_QUIZTEST";
	document.form1.submit();
}

function cancelPlan(action, plan) {
	
	if (action == 'calcu') {
		document.form1.action = rootPath + '/exec/DisplayOnlineTools';
	} else if (action == 'add') {
		document.form1.action = rootPath + '/exec/CustomerMain';
	} else if (action == 'edit') {
		if (plan == 'COL_PL') {
			document.form1.action = rootPath + '/exec/DisplayCollegePlanDetail';
		} else if (plan == 'RET_PL') {
			document.form1.action = rootPath + '/exec/DisplayRetirementPlanDetail';
		} else if (plan == 'INV_PL') {
			document.form1.action = rootPath + '/exec/DisplayInvestmentPlanDetail';
		}
	}
	
	document.form1.submit();
	return true;
}

function cancelOnlineTool() {
	document.form1.action = rootPath + '/exec/DisplayOnlineTools';	
	document.form1.submit();
	return true;
}
/****************************** plan (begin) ******************************/
function saveCollegePlan() {
	var status = false;
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';
	var subStatus1 = false;
	var subStatus2 = false;
	var subStatus3 = false;
	var subStatus4 = true;

	if (CheckBlank(document.form1.planName)) {
		document.form1.planName.className = 'bgred14';
		subStatus4 = false;
	}
	
	subStatus1 = validateCollegeSection1();
	subStatus2 = validateCollegeSection2();
	subStatus3 = validateCollegeSection3();

	status = subStatus1 && subStatus2 && subStatus3 && subStatus4;
	
	if (status) {
		document.form1.action = rootPath + '/exec/SaveCollegePlan';
		document.form1.submit();
		return true;
	} else {
		alert(message);
	}
	
	return false;
}

function saveInvestmentPlan() {
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';

	var status = false;
	var	subStatus1 = false;
	var	subStatus2 = false;
	var subStatus3 = true;
	
	if (CheckBlank(document.form1.planName)) {
		document.form1.planName.className = 'bgred14';
		subStatus3 = false;
	}

	subStatus1 = validateInvestmentSection1();
	subStatus2 = validateInvestmentSection2();
	status = subStatus1 && subStatus2 && subStatus3;
	
	if (status) {
		document.form1.action = rootPath + '/exec/SaveInvestmentPlan';
		document.form1.submit();
		return true;
	} else {
		alert(message);
	}
	
	return false;
}

function saveRetirementPlan() {
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';

	var status = false;
	var	subStatus1 = false;
	var	subStatus2 = false;
	var	subStatus3 = false;
	var	subStatus4 = false;
	var	subStatus5 = false;
	var	subStatus6 = true;

	if (CheckBlank(document.form1.planName)) {
		document.form1.planName.className = 'bgred14';
		subStatus6 = false;
	}
	
	subStatus1 = validateRetirementSection1();
	subStatus2 = validateRetirementSection2();
	subStatus3 = validateRetirementSection3();
	subStatus4 = validateRetirementSection4();
	subStatus5 = validateRetirementSection5();
	status = subStatus1 && subStatus2 && subStatus3 && subStatus4 && subStatus5 && subStatus6;
	
	if (status) {
		document.form1.action = rootPath + '/exec/SaveRetirementPlan';
		document.form1.submit();
		return true;
	} else {
		alert(message);
	}
	
	return false;
}

function getPlanDetail(planType, portKey) {
	document.form1.planType.value = planType;
	document.form1.portKey.value = portKey;

	document.form1.action = rootPath + '/exec/PlanDetail';
	document.form1.submit();
	return true;	
}

function getInvestmentPlan() {
	document.form1.action = rootPath + '/exec/DisplayInvestmentTool?act=edit';
	document.form1.submit();
	return true;	
}

function getRetirementPlan() {
	document.form1.action = rootPath + '/exec/DisplayRetirementTool?act=edit';
	document.form1.submit();
	return true;	
}

function getCollegePlan() {
	document.form1.action = rootPath + '/exec/DisplayCollegeTool?act=edit';
	document.form1.submit();
	return true;	
}
/******************************* plan (end) *******************************/


/****************************** credit card online tool (begin) ******************************/
function validateCreditCardSection1() {
	var status = true;

	//if (CheckBlank(document.form1.creditCardName)) {
	//	document.form1.creditCardName.className = 'bgred14';
	//	status = false;
	//}

	if (CheckBlank(document.form1.arrearsPayment)) {
		document.form1.arrearsPayment.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.interest)) {
		document.form1.interest.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.minimumPayment)) {
		document.form1.minimumPayment.className = 'inputInvalidDecimalField';
		status = false;
	}
		
	return status;
}

function validateCreditCardSection2() {
	var status = true;

	//if (CheckBlank(document.form1.creditCardName)) {
	//	document.form1.creditCardName.className = 'inputInvalidDecimalField';
	//	status = false;
	//}

	if (CheckBlank(document.form1.arrearsPayment)) {
		document.form1.arrearsPayment.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.interest)) {
		document.form1.interest.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.payYearAmount)) {
		document.form1.payYearAmount.className = 'inputInvalidDecimalField';
		status = false;
	}
		
	return status;
}

function calcuCreditCardPlan(section) {
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';
	var status = false;
	
	switch (section) {
		case 1:
			status = validateCreditCardSection1();
			break;
		case 2:
			status = validateCreditCardSection2();
			break;
	}
	
	if (status) {

		if ( (section == 1) || (section == 2) ) {
			var A,B,C,D,E,F,G,H,I;
			B = eval(currencyToNumber(document.form1.arrearsPayment.value));
			C = eval(currencyToNumber(document.form1.interest.value));
			D = eval(currencyToNumber(document.form1.minimumPayment.value));
	
			if (section == 1) {
				E = Math.floor( ( log10(1-( (C/1200) * (B/D) )) / log10(1 + (C/1200)) )  / -12 );
				F = Math.ceil( (( log10(1-( (C/1200) * (B/D) )) / log10(1 + (C/1200)) )*(-1) ) % 12 );
				if (F > 11)
				{
					F = 0;
					E += 1;
				}
				G = (((E * 12) + F) * D) - B;
				
				document.form1.totalPayYearAmount.value = E;
				document.form1.totalPayMonthAmount.value = F;
				document.form1.totalInterest1.value = formatCurrency(G);
			} else {				
				H = eval(document.form1.payYearAmount.value);
				I = B * ((C/1200) + ( (C/1200) / (( Math.pow(1+ (C/1200), H*12) ) - 1)));

				document.form1.totalPayAmount2.value = formatCurrency(I);
			}
		}
		
	} else {
		alert(message);
	}
	
	return false;
}
/****************************** credit card online tool (end) ******************************/

/****************************** investment online tool (begin) ******************************/
function validateInvestmentSection1() {
	var status = true;
	
	if (CheckBlank(document.form1.capital)) {
		document.form1.capital.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.expectReturn)) {
		document.form1.expectReturn.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.yearAmount)) {
		document.form1.yearAmount.className = 'inputInvalidDecimalField';
		status = false;
	}
	
	return status;
}

function validateInvestmentSection2() {
	var status = true;
	
	if (CheckBlank(document.form1.riskLimitRatio)) {
		document.form1.riskLimitRatio.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.reduceLimitRation)) {
		document.form1.reduceLimitRation.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;
}

function calcuInvestmentPlan(section) {
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';

	var status = false;
	var	subStatus1 = false;
	var	subStatus2 = false;
	
	switch (section) {
		case 1:
			status = validateInvestmentSection1();
			break;
		case 2:
			subStatus1 = validateInvestmentSection1();
			subStatus2 = validateInvestmentSection2();
			status = subStatus1 && subStatus2;
			break;
	}
	
	if (status) {
	
		var A,B,C,D;
		A = currencyToNumber(document.form1.capital.value);
		B = document.form1.expectReturn.value;
		C = document.form1.yearAmount.value;

		D = (A * Math.pow(1+(B/100),C) );
		
		document.form1.totalFutureCapital.value = formatCurrency(D);
		
		
	} else {
		alert(message);
	}
	
	return status;
}
/****************************** investment online tool (end) ******************************/

/****************************** insurance online tool (begin) ******************************/
function validateInsuranceSection1() {
	var status = true;
	
	if (CheckBlank(document.form1.familyPayMonth)) {
		document.form1.familyPayMonth.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.childPayMonth)) {
		document.form1.childPayMonth.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.eduPayYear)) {
		document.form1.eduPayYear.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.healthPay)) {
		document.form1.healthPay.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.otherPay)) {
		document.form1.otherPay.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.deptPay)) {
		document.form1.deptPay.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;	
}

function validateInsuranceSection2() {
	var status = true;
	
	if (CheckBlank(document.form1.investmentAsset)) {
		document.form1.investmentAsset.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.rmfAsset)) {
		document.form1.rmfAsset.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.insuranceAsset1)) {
		document.form1.insuranceAsset1.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.insuranceAsset2)) {
		document.form1.insuranceAsset2.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.otherAsset)) {
		document.form1.otherAsset.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;	
}

function calcuInsurancePlan(section) {
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';

	var status = false;
	var	subStatus1 = false;
	var	subStatus2 = false;
	
	switch (section) {
		case 1:
			status = validateInsuranceSection1();
			break;
		case 2:
			subStatus1 = validateInsuranceSection1();
			subStatus2 = validateInsuranceSection2();
			status = subStatus1 && subStatus2;
			break;
	}
	
	if (status) {
		document.form1.action = rootPath + '/exec/CalculateInsurance?s=' + section;
		document.form1.submit();
		return true;
	} else {
		alert(message);
	}
	
	return false;
}
/****************************** insurance online tool (end) ********************************/

/****************************** retirement online tool (begin) ******************************/
function validateRetirementSection1() {
	var status = true;

	if (CheckBlank(document.form1.salary)) {
		document.form1.salary.className = 'inputInvalidDecimalField';
		status = false;
	}
	
	if (CheckBlank(document.form1.salaryIncrementRatio)) {
		document.form1.salaryIncrementRatio.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.presentAge)) {
		document.form1.presentAge.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.retireAge)) {
		document.form1.retireAge.className = 'inputInvalidDecimalField';
		status = false;
	}
	
	return status;
}

function validateRetirementSection2() {
	var status = true;

	if (CheckBlank(document.form1.earningsAfterRetireRatio)) {
		document.form1.earningsAfterRetireRatio.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;
}

function validateRetirementSection3() {
	var status = true;
	
	if (CheckBlank(document.form1.liveLongAge)) {
		document.form1.liveLongAge.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.inflationRatio)) {
		document.form1.inflationRatio.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;	
}

function validateRetirementSection4() {
	var status = true;
	
	if (CheckBlank(document.form1.reserveFund)) {
		document.form1.reserveFund.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.reserveFundCollectionRatio)) {
		document.form1.reserveFundCollectionRatio.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.reserveFundContributionRatio)) {
		document.form1.reserveFundContributionRatio.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.RMF)) {
		document.form1.RMF.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.LTF)) {
		document.form1.LTF.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.RMFCollectionRatio)) {
		document.form1.RMFCollectionRatio.className = 'inputInvalidDecimalField';
		status = false;
	}
	
	if (CheckBlank(document.form1.LTFCollectionRatio)) {
		document.form1.LTFCollectionRatio.className = 'inputInvalidDecimalField';
		status = false;
	}
	
	if (CheckBlank(document.form1.returnFromFundRatio)) {
		document.form1.returnFromFundRatio.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;	
}

function validateRetirementSection5() {
	var status = true;
	
	if (CheckBlank(document.form1.expectReturnRatio)) {
		document.form1.expectReturnRatio.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;	
}

function calcuRetirementPlan(section) {
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';

	var status = false;
	var	subStatus1 = false;
	var	subStatus2 = false;
	var	subStatus3 = false;
	var	subStatus4 = false;
	var	subStatus5 = false;
	switch (section) {
		case 1:
			status = validateRetirementSection1();
			break;
		case 2:
			subStatus1 = validateRetirementSection1();
			subStatus2 = validateRetirementSection2();
			status = subStatus1 && subStatus2;
			break;
		case 3:
			subStatus1 = validateRetirementSection1();
			subStatus2 = validateRetirementSection2();
			subStatus3 = validateRetirementSection3();
			status = subStatus1 && subStatus2 && subStatus3;
			break;
		case 4:
			subStatus1 = validateRetirementSection1();
			subStatus2 = validateRetirementSection2();
			subStatus3 = validateRetirementSection3();
			subStatus4 = validateRetirementSection4();
			status = subStatus1 && subStatus2 && subStatus3 && subStatus4;
			break;
		case 5:
			subStatus1 = validateRetirementSection1();
			subStatus2 = validateRetirementSection2();
			subStatus3 = validateRetirementSection3();
			subStatus4 = validateRetirementSection4();
			subStatus5 = validateRetirementSection5();
			status = subStatus1 && subStatus2 && subStatus3 && subStatus4 && subStatus5;
			break;
	}
	
	if (status) {

		if ((section == 1) || (section == 2) || (section == 3) || (section == 4) || (section == 5)) {
			
			var A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V;
			
			A = eval(currencyToNumber(document.form1.salary.value));
			B = eval(document.form1.salaryIncrementRatio.value);
			C = eval(document.form1.presentAge.value);
			D = eval(document.form1.retireAge.value);
			
			E = (A * Math.pow(1+(B/100),(D-C)) );
//alert (E);
			document.form1.totalSalaryBeforeRetire.value = formatCurrency(E);
	
			if ((section == 2) || (section == 3) || (section == 4) || (section == 5)) {
			
				F = eval(document.form1.earningsAfterRetireRatio.value);
				
				G=(E * (F/100));
	
				document.form1.totalEarningsAfterRetire.value = formatCurrency(G);

				if ((section == 3) || (section == 4) || (section == 5)) {
				
					H = eval(document.form1.liveLongAge.value);
					I = eval(document.form1.inflationRatio.value);
					
					J = ((G*12)*(( Math.pow(1+(I/100),H-D) )-1)/(I/100))*(1+(I/100));
		
					document.form1.totalMemberContributions.value = formatCurrency(J);

					if ((section == 4) || (section == 5)) {
					
						K = eval(currencyToNumber(document.form1.reserveFund.value));
						L = eval(document.form1.reserveFundCollectionRatio.value);
						M = eval(document.form1.reserveFundContributionRatio.value);
						N = eval(currencyToNumber(document.form1.RMF.value));
						O = eval(document.form1.RMFCollectionRatio.value);
						U = eval(currencyToNumber(document.form1.LTF.value));
						V = eval(document.form1.LTFCollectionRatio.value);
						P = eval(document.form1.returnFromFundRatio.value);
						
			
						var buf = 0;
						var Year = 0;
						var Salary = 0;
						var Contribute = 0;
						var Accum = 0;
						for (i=(D-C); i >= 0; i--) {
							Salary = (A * 12) * Math.pow((1 + (B / 100)),Year);
							Contribute = Salary * (L + M) / 100;
							if ((Salary * O / 100) > 300000)
							{
								Contribute += 300000;
							}	else	{
								Contribute += (Salary * (O / 100));
							}
							if ((Salary * V / 100) > 300000)
							{
								Contribute += 300000;
							} else {
								Contribute += (Salary * (V / 100));
							}
							Accum += Contribute * Math.pow((1 + (P / 100)),i);
							Year++;
						}
						Q = ((K + N + U) * Math.pow((1 + (P / 100)),(D-C))) + Accum;
						
						document.form1.totalEarningsFromFund.value = formatCurrency(Q);

						if ((section == 5)) {
						
							S = eval(document.form1.expectReturnRatio.value);
							if (J < Q)
							{
								R	= 0;
								T  = 0;
							} else {
								R = J - Q;
								T = ((R / ( Math.pow(1+(S/100),D-C)) )*((S/100)+( (S/100) / (( Math.pow(1+(S/100),D-C) )-1)))) / 12;
							}
							
							document.form1.totalAdditionCollectMoney.value = formatCurrency(R);
							document.form1.totalAdditionCollectMoneyPerMonth.value = formatCurrency(T);
						}

					}

				}

			}
		}

	} else {
		alert(message);
	}
	
	return status;
}
/****************************** retirement online tool (end) ******************************/

/****************************** college online tool (begin) ******************************/
function initialCollegePlan() {
	if (document.form1.primarySchoolType.value == 0) {
		document.form1.primaryEducationAge.disabled = true;
		document.form1.primaryEducationAge.className = 'readOnlyDecimalField';	
		document.form1.primaryYearAmount.disabled = true;
		document.form1.primaryYearAmount.className = 'readOnlyDecimalField';

	} 
    if (document.form1.primarySchoolType.value != 999) {
        document.form1.primaryAmount.className = 'readOnlyDecimalField';
        document.form1.primaryAmount.disabled = true;
    }

	if (document.form1.bachelorUniversityType.value == 0) {
		document.form1.bachelorEducationAge.disabled = true;
		document.form1.bachelorEducationAge.className = 'readOnlyDecimalField';	
		document.form1.bachelorYearAmount.disabled = true;
		document.form1.bachelorYearAmount.className = 'readOnlyDecimalField';

	}
    if (document.form1.bachelorUniversityType.value != 999) {
        document.form1.bachelorAmount.className = 'readOnlyDecimalField';
        document.form1.bachelorAmount.disabled = true;
    }
	
	if (document.form1.masterUniversityType.value == 0) {
		document.form1.masterEducationAge.disabled = true;
		document.form1.masterEducationAge.className = 'readOnlyDecimalField';	
		document.form1.masterYearAmount.disabled = true;
		document.form1.masterYearAmount.className = 'readOnlyDecimalField';
	}
    if (document.form1.masterUniversityType.value != 999) {

        document.form1.masterAmount.className = 'readOnlyDecimalField';
        document.form1.masterAmount.disabled = true;
    }
    
	if (document.form1.doctorUniversityType.value == 0) {	
		document.form1.doctorEducationAge.disabled = true;
		document.form1.doctorEducationAge.className = 'readOnlyDecimalField';
		document.form1.doctorYearAmount.disabled = true;
		document.form1.doctorYearAmount.className = 'readOnlyDecimalField';
	}
    if (document.form1.doctorUniversityType.value != 999) {

        document.form1.doctorAmount.className = 'readOnlyDecimalField';
        document.form1.doctorAmount.disabled = true;
    }
}

function selectCollegePlanType(elm1, elm2, elm3, elm4) {
	if (elm1.value == 0) {
		elm2.value = '';	
		elm2.className = 'readOnlyDecimalField';		
		elm2.disabled = true;
		
		elm3.value = '';
		elm3.className = 'readOnlyDecimalField';
		elm3.disabled = true;

        elm4.value = '';
        elm4.className = 'readOnlyDecimalField';
        elm4.disabled = true;
	} else {
		elm2.className = 'inputDecimalField';		
		elm2.disabled = false;

		elm3.className = 'inputDecimalField';		
		elm3.disabled = false;
        
        if (elm1.value == 999) {
            elm4.className = 'inputDecimalField';
            elm4.disabled = false;
        } else {
            elm4.value = '';
            elm4.className = 'readOnlyDecimalField';
            elm4.disabled = true;
        }
	}
}

function validateCollegeSection1() {
	var status = true;
	var selectedPlan = 0;

	if (CheckBlank(document.form1.childPresentAge)) {
		document.form1.childPresentAge.className = 'inputInvalidDecimalField';
		status = false;
	}
	
	if (CheckBlank(document.form1.educationExpenseIncrementRatio)) {
		document.form1.educationExpenseIncrementRatio.className = 'inputInvalidDecimalField';
		status = false;
	}
	
	if (document.form1.primarySchoolType.value != 0) {
		selectedPlan ++;
		if (CheckBlank(document.form1.primaryEducationAge)) {
			document.form1.primaryEducationAge.className = 'inputInvalidDecimalField';
			status = false;
		}
		
		if (CheckBlank(document.form1.primaryYearAmount)) {
			document.form1.primaryYearAmount.className = 'inputInvalidDecimalField';
			status = false;
		}
	}
	
	if (document.form1.bachelorUniversityType.value != 0) {
		selectedPlan ++;
		if (CheckBlank(document.form1.bachelorEducationAge)) {
			document.form1.bachelorEducationAge.className = 'inputInvalidDecimalField';
			status = false;
		}
		
		if (CheckBlank(document.form1.bachelorYearAmount)) {
			document.form1.bachelorYearAmount.className = 'inputInvalidDecimalField';
			status = false;
		}
	}
	
	if (document.form1.masterUniversityType.value != 0) {
		selectedPlan ++;
		if (CheckBlank(document.form1.masterEducationAge)) {
			document.form1.masterEducationAge.className = 'inputInvalidDecimalField';
			status = false;
		}
		
		if (CheckBlank(document.form1.masterYearAmount)) {
			document.form1.masterYearAmount.className = 'inputInvalidDecimalField';
			status = false;
		}
	}
	
	if (document.form1.doctorUniversityType.value != 0) {
		selectedPlan ++;
		if (CheckBlank(document.form1.doctorEducationAge)) {
			document.form1.doctorEducationAge.className = 'inputInvalidDecimalField';
			status = false;
		}
		
		if (CheckBlank(document.form1.doctorYearAmount)) {
			document.form1.doctorYearAmount.className = 'inputInvalidDecimalField';
			status = false;
		}
	}
	
	if (selectedPlan == 0) {
		status = false;
	}

	return status;
}

function validateCollegeSection2() {
	var status = true;

	if (CheckBlank(document.form1.capital)) {
		document.form1.capital.className = 'inputInvalidDecimalField';
		status = false;
	}
	
	if (CheckBlank(document.form1.expectReturn)) {
		document.form1.expectReturn.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;	
}

function validateCollegeSection3() {
	var status = true;

	if (CheckBlank(document.form1.scholarship)) {
		document.form1.scholarship.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;	
}

function calcuCollegePlan(section) {

	var status = false;
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';
	var subStatus1 = false;
	var subStatus2 = false;
	var subStatus3 = false;
	
	var valPrimary = 0;
	var valBachelor = 0;
	var valMaster = 0;
	var valDoctor = 0;
	
	var T;
	var K;
	var U;
	var V;
	var A;
	var B;
	var C;
	var D;
	var E;
	var F;
	var G;
	var H;
	var I;
	var J;
	var L;
	var M;
	var N;
	var O, P, Q, R, S;
	
	switch (section) {
		case 1:
			status = validateCollegeSection1();
			break;
		case 2:
			subStatus1 = validateCollegeSection1();
			subStatus2 = validateCollegeSection2();
			status = subStatus1 && subStatus2;
			break;
		case 3:
			subStatus1 = validateCollegeSection1();
			subStatus2 = validateCollegeSection2();
			subStatus3 = validateCollegeSection3();
			status = subStatus1 && subStatus2 && subStatus3;
			break;
	}
	
	if (status) {
//		document.form1.action = rootPath + '/exec/CalculateCollege?s=' + section;
//		document.form1.submit();

		if ( (section == 1) || (section == 2) || (section == 3) ) {

				
				T = document.form1.primarySchoolType.value;
				K = document.form1.educationExpenseIncrementRatio.value;
				
				U = document.form1.primaryEducationAge.value;
				V = document.form1.primaryYearAmount.value;
				A = document.form1.childPresentAge.value;
				
				B = document.form1.bachelorUniversityType.value;
				C = document.form1.bachelorEducationAge.value;
				D = document.form1.bachelorYearAmount.value;
			
				E = document.form1.masterUniversityType.value;
				F = document.form1.masterEducationAge.value;
				G = document.form1.masterYearAmount.value;

				H = document.form1.doctorUniversityType.value;
				I = document.form1.doctorEducationAge.value;
				J = document.form1.doctorYearAmount.value;

				if (document.form1.primarySchoolType.value != 0) {
                    if (document.form1.primarySchoolType.value == 999) {
                        T = currencyToNumber(document.form1.primaryAmount.value);
                    }
					valPrimary = ( T * Math.pow( 1+(K/100),(U-A) ) ) * ( ( Math.pow(1+(K/100),V ) -1) / (K/100));
				}
				if (document.form1.bachelorUniversityType.value != 0) {
                    if (document.form1.bachelorUniversityType.value == 999) {
                        B = currencyToNumber(document.form1.bachelorAmount.value);
                    }
					valBachelor = ( B * Math.pow( 1+(K/100),(C-A) ) ) * ( ( Math.pow(1+(K/100),D ) -1) / (K/100));
				}
				if (document.form1.masterUniversityType.value != 0) {
                    if (document.form1.masterUniversityType.value == 999) {
                        E = currencyToNumber(document.form1.masterAmount.value);
                    }
					valMaster = ( E * Math.pow( 1+(K/100),(F-A) ) ) * ( ( Math.pow(1+(K/100),G ) -1) / (K/100));
				}
				if (document.form1.doctorUniversityType.value != 0) {
                    if (document.form1.doctorUniversityType.value == 999) {
                        H = currencyToNumber(document.form1.doctorAmount.value);
                    }
					valDoctor = ( H * Math.pow( 1+(K/100),(I-A) ) ) * ( ( Math.pow(1+(K/100),J ) -1) / (K/100));
				}
				
				L = valPrimary+valBachelor+valMaster+valDoctor;
				
				document.form1.futureExpense.value = formatCurrency(L);
				
		}
		
		if ( (section == 2) || (section == 3) ){
				
				var buf;
				
				if (document.form1.doctorUniversityType.value != 0) {
					buf = I;
				}
				if (document.form1.masterUniversityType.value != 0) {
					buf = F;
				}
				if (document.form1.bachelorUniversityType.value != 0) {
					buf = C;
				}
				if (document.form1.primarySchoolType.value != 0) {
					buf = U;
				}
			
				M = currencyToNumber(document.form1.capital.value);
				N = document.form1.expectReturn.value;
				O = M * Math.pow( (1+(N/100)),(buf-A) );
				
				document.form1.futureCapital.value = formatCurrency(O);

				if (section == 3) {
					
					P = currencyToNumber(document.form1.scholarship.value);

					Q = eval(O) + eval(P);
					if (L < Q)
					{
					    R = 0;
						S = 0;
					} else {
						R = L - Q;
					    S = ( (R/ Math.pow((1+(N/100)),(buf-A)) ) * ((N/100) + ((N/100) / (( Math.pow((1+(N/100)),(buf-A)) )-1)) )) / 12;
					}
					document.form1.futureContributions.value = formatCurrency(Q);
					document.form1.totalFutureCollect.value = formatCurrency(R);
					document.form1.totalCollectPerMonth.value = formatCurrency(S);
			
				}
		}
	} else {
		alert(message);
	}
	
	return status;
}
/****************************** college online tool (end) ******************************/

/****************************** saving online tool (begin) *****************************/
// reset value Future Value
function resetFutureValue(form){
	form.initialInvestmentFV.value="";
	form.installmentPaymentFV.value="";
	form.investmentPeriodFV.value="";
	form.rateOfReturnFV.value="";
	form.futureValueFV.value="";	
}

// reset Periodic Payment
function resetPeriodicPayment(form){
	form.initialInvestmentPP.value="";
	form.requiredInvestmentPP.value="";
	form.investmentPeriodPP.value="";
	form.rateOfReturnPP.value="";
	form.installmentPaymentPP.value="";
}

// reset Time Frame
function resetTimeFrame(form){
	form.initialInvestmentTF.value = "";
	form.installmentPaymentTF.value = "";
	form.requiredInvestmentTF.value = "";
	form.rateOfReturnTF.value = "";
	form.investmentPeriodTF.value = "";	
}

// reset Investment Plan
function resetInvestPlan (form) {
	form.capital.value = "";
	form.expectReturn.value="";
	form.yearAmount.value="";
	form.totalFutureCapital.value="";
}

function validateSavingSection1() {
	var status = true;

	if (CheckBlank(document.form1.initialInvestmentFV)) {
		document.form1.initialInvestmentFV.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.installmentPaymentFV)) {
		document.form1.installmentPaymentFV.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.investmentPeriodFV)) {
		document.form1.investmentPeriodFV.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.rateOfReturnFV)) {
		document.form1.rateOfReturnFV.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;
} 

function validateSavingSection2() {
	var status = true;

	if (CheckBlank(document.form1.initialInvestmentPP)) {
		document.form1.initialInvestmentPP.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.requiredInvestmentPP)) {
		document.form1.requiredInvestmentPP.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.investmentPeriodPP)) {
		document.form1.investmentPeriodPP.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.rateOfReturnPP)) {
		document.form1.rateOfReturnPP.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;
} 

function validateSavingSection3() {
	var status = true;

	if (CheckBlank(document.form1.initialInvestmentTF)) {
		document.form1.initialInvestmentTF.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.installmentPaymentTF)) {
		document.form1.installmentPaymentTF.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.requiredInvestmentTF)) {
		document.form1.requiredInvestmentTF.className = 'inputInvalidDecimalField';
		status = false;
	}

	if (CheckBlank(document.form1.rateOfReturnTF)) {
		document.form1.rateOfReturnTF.className = 'inputInvalidDecimalField';
		status = false;
	}

	return status;
} 

function calcuSaving(section) {
	var status = false;
	var message = 'กรุณาใส่ข้อมูลให้ครบ.';
	
	switch (section) {
		case 1:
			status = validateSavingSection1();
			break;
		case 2:
			status = validateSavingSection2();
			break;
		case 3:
			status = validateSavingSection3();
			break;
	}
	
	if (status) {

		if (section == 1) {
		
			var A,B,C,D,E;
			
			A = eval(currencyToNumber(document.form1.initialInvestmentFV.value));
			B = eval(currencyToNumber(document.form1.installmentPaymentFV.value));
			C = eval(document.form1.investmentPeriodFV.value);
			D = eval(document.form1.rateOfReturnFV.value);

			E = ( B*(( Math.pow(1+(D/100),C) )-1)/ (D/100) ) + ( A * Math.pow(1+ (D/100), C) );
			
			document.form1.futureValueFV.value = formatCurrency(E);
			
		}
		if (section == 2) {
		
			var F,G,H,I,J;
	
			F = eval(currencyToNumber(document.form1.initialInvestmentPP.value));
			G = eval(currencyToNumber(document.form1.requiredInvestmentPP.value));
			H = eval(document.form1.investmentPeriodPP.value);
			I = eval(document.form1.rateOfReturnPP.value);

			J = ((G - (F * Math.pow(1+ (I/100),H) )) / Math.pow(1+(I/100),H) ) * ((I/100) + ((I/100) / (( Math.pow(1+ (I/100),H) - 1))));
			
			document.form1.installmentPaymentPP.value = formatCurrency(J);
		}
		if (section == 3) {
		
			var K,L,M,N,O;
	
			K = eval(currencyToNumber(document.form1.initialInvestmentTF.value));
			L = eval(currencyToNumber(document.form1.installmentPaymentTF.value));
			M = eval(currencyToNumber(document.form1.requiredInvestmentTF.value));
			N = eval(document.form1.rateOfReturnTF.value);

			J = Math.round(( log10(M+(L / (N/100))) - log10(K+(L/ (N/100)))) / log10(1+(N/100)),0);
//			J = Math.round(( log10(5000+(200 / (10/100))) - log10(500+(200/ (10/100)))) / log10(1+(10/100)),0);

//			alert (log10(M+(L / (N/100))));
			document.form1.investmentPeriodTF.value = J;
		}

	} else {
		alert(message);
	}	
	
	return true;
}
/****************************** saving online tool (end) *******************************/

function addAssetCalAssetValue(assetUnit ,price ,totalValue ,commission ,capital,averageCapital){
	
	var aU = currencyToNumber(assetUnit.value);
	var p = currencyToNumber(price.value);
	var tValue = 0;	
	var comm = 0;	
	var cp = 0;
	var aCp = 0;	
	if(assetUnit.value != "" && price.value != "" && isCurrency(assetUnit) && isCurrency(price) ){
		tValue = aU * p;
		comm = (tValue * (0.25/100));
		cp = (tValue + comm);
		if(aU != 0){
			aCp = cp / aU;
		}
		totalValue.value = formatCurrency(tValue);
		commission.value = formatCurrency(comm);
		capital.value = formatCurrency(cp);
		averageCapital.value = formatCurrency(aCp);
		commission.className = 'inputDecimalField';
	}else { //if(assetUnit.value == "" || price.value == ""){
		totalValue.value = "";
		commission.value = "";
		capital.value = "";
		averageCapital.value = "";
	}
}

function addAssetCalCapital(aUnit,totalAssetValue, com, capital,averageCapital){
	var tav = currencyToNumber(totalAssetValue.value);
	var c = currencyToNumber(com.value);
	var u = currencyToNumber(aUnit.value);
	var cp = 0;
	var aCp = 0;
	if(totalAssetValue.value != "" && com.value != "" && isCurrency(totalAssetValue) && isCurrency(com)){
		cp = (1*tav + 1*c);
		if(u != 0){
			aCp = cp/u;			
		}
		capital.value = formatCurrency(cp);
		averageCapital.value = formatCurrency(aCp);
	}else{
		capital.value = "";
		averageCapital.value = "";
	}
}

function reduceAssetCalculator(assetPrice ,assetUnit ,totalAssetValue ,commission ,totalCash){
	var ap = currencyToNumber(assetPrice.value);
	var au = currencyToNumber(assetUnit.value);
	var atv = 0;
	var com = 0;
	var tc = 0;
	if(assetPrice.value != "" && assetUnit.value != "" && isCurrency(assetPrice) && isCurrency(assetUnit)){
		atv = ap * au;
		com = (0.25/100)*atv;
		tc = atv - com;		
		totalAssetValue.value = formatCurrency(atv);
		commission.value = formatCurrency(com);
		totalCash.value = formatCurrency(tc);
	}else{
		totalAssetValue.value = "";
		commission.value = "";
		totalCash.value = "";		
	}
}

function calculateTotalCash(assetPrice ,assetUnit ,totalAssetValue ,commission ,totalCash){
	var ap = currencyToNumber(assetPrice.value);
	var au = currencyToNumber(assetUnit.value);	
	var com = currencyToNumber(commission.value);
	var atv = 0;
	var tc = 0;
	if(assetPrice.value != "" && assetUnit.value != "" && isCurrency(assetPrice) && isCurrency(assetUnit) && commission.value != "" && isCurrency(commission)){
		atv = ap*au;	
		tc = atv - com;		
		totalAssetValue.value = formatCurrency(atv);
		commission.value = formatCurrency(com);
		totalCash.value = formatCurrency(tc);
	}else{		
		commission.value = "";
		totalCash.value = "";		
	}

}

function validAddAsset(aForm,message){
	var status = true;
	if(aForm.assetType.value == "cash"){		
		if(aForm.assetName.value=="" ){			
			aForm.assetName.className = 'bgred14';			
			status = false;
		}
		if(aForm.cashValue.value==""){			
			aForm.cashValue.className = 'bgred14';			
			status = false;
		}
	}else{
		if(aForm.assetName.value=="" ){ 
			aForm.assetName.className = 'bgred14';
			status = false;
		}
		if(aForm.assetPrice.value==""){
			aForm.assetPrice.className = 'bgred14';
			status = false;
		}
		if(aForm.assetUnit.value=="" ){
			aForm.assetUnit.className = 'bgred14';
			status = false;
		}		
		if(aForm.commission.value==""){
			aForm.commission.className = 'bgred14';
			status = false;
		}
	}   
	if(!status){
		alert(message);
	}	
   	return status;	
}



function validReduceAsset(aForm){
	var status = validateForm(aForm);
   	
   	if (status) {
		aForm.action = rootPath + '/exec/ReduceAssetInPort';		
		aForm.submit();
		return true;
   	} else {
		alert('กรุณาใส่ข้อมูลให้ครบ.');
   	}
   	
   	return false;
}

function clearTextClass(argv){
	argv.className = 'inputTextField';
}

function findAsset(aForm,assetType){		
	if(aForm.assetType.value == "stock"){
		aForm.action = rootPath + '/exec/SearchStock';
		aForm.submit();
	}else if (aForm.assetType.value == "mfund"){
		aForm.action = rootPath + '/exec/SearchMFund';
		aForm.submit();
	}	
}

function cancelSearch(aForm){
	aForm.action = rootPath + '/exec/DisplayAddAsset';
	aForm.submit();
}

function cancelImportStock(aForm){
	aForm.action = rootPath + '/exec/AdminMain';
	aForm.submit();
}

function initInvisible(){	
	eml=document.getElementById("asset8");
	eml.style.display="none";
}

function changeAssetType(aForm){
	// clear style
	aForm.assetName.className = 'inputTextField';
	aForm.cashValue.className = 'inputDecimalField';	
	aForm.assetPrice.className = 'inputDecimalField';
	aForm.assetUnit.className = 'inputDecimalField';
	aForm.commission.className = 'inputDecimalField';
	
	// clear value
	aForm.assetName.value = "";
	aForm.cashValue.value = "";	
	aForm.assetPrice.value = "";
	aForm.assetUnit.value = "";
	aForm.commission.value = "";
	aForm.assetTotalValue.value = "";
	aForm.averageCapital.value = "";
	aForm.capital.value = "";
	aForm.cashValue.value = "";	
	aForm.cashPortName.options[0].selected = true;	
	
	if(aForm.assetType.value == "cash"){
		eml1=document.getElementById("asset1");
		eml2=document.getElementById("asset2");
		eml3=document.getElementById("asset3");
		eml4=document.getElementById("asset4");
		eml5=document.getElementById("asset5");
		eml6=document.getElementById("asset6");
		eml7=document.getElementById("asset7");
		eml8=document.getElementById("asset8");
		emlButton=document.getElementById("buttonSearch");
		eml1.style.display="none";
		eml2.style.display="none";
		eml3.style.display="none";
		eml4.style.display="none";
		eml5.style.display="none";
		eml6.style.display="none";
		eml7.style.display="none";
		eml8.style.display="block";
		emlButton.style.display="none";
	}else{
		eml1=document.getElementById("asset1");
		eml2=document.getElementById("asset2");
		eml3=document.getElementById("asset3");
		eml4=document.getElementById("asset4");
		eml5=document.getElementById("asset5");
		eml6=document.getElementById("asset6");
		eml7=document.getElementById("asset7");
		eml8=document.getElementById("asset8");
		emlButton=document.getElementById("buttonSearch");
		eml1.style.display="block";
		eml2.style.display="block";
		eml3.style.display="block";
		eml4.style.display="block";
		eml5.style.display="block";
		eml6.style.display="block";
		eml7.style.display="block";
		eml8.style.display="none";
		emlButton.style.display="block";
	}
}

function addAsset(aForm ,assetName ,assetType ,portKey,perform){
	aForm.perform.value = perform;	
	aForm.assetName.value = assetName;
	aForm.assetType.value = assetType;
	aForm.portKey.value = portKey;
	aForm.action = rootPath + '/exec/DisplayAddAsset';
	aForm.submit();
}

function reduceAsset(aForm ,assetName ,assetType ,portKey){	
	aForm.assetName.value = assetName;
	aForm.assetType.value = assetType;
	aForm.portKey.value = portKey;
	aForm.action = rootPath + '/exec/DisplayReduceAsset';
	aForm.submit();
}

function stockDetail(aForm ,stockCode,portKey){
	aForm.code.value = stockCode;
	aForm.portKey.value=portKey;
	aForm.action = rootPath + '/exec/StockDetail';
	aForm.submit();
}

function assetNameToUpperCase(aForm){
	var str = aForm.assetName.value;
	if(aForm.assetType.value == "stock" || aForm.assetType.value == "mfund" ){
		aForm.assetName.value = str.toUpperCase();
	}
}






