var err_cash_sale_rev;
var err_acct_recvable;
var err_int_recv;
var err_disp_of_asset;
var err_cap_inject;
var err_loan_drawdown;
var err_othr_cash_recpt;
var err_sal_n_alwanc;
var err_pymnt_of_acct_payable;
var err_int_paid;
var err_procumnt_of_goods;
var err_rent_n_util;
var err_repay_of_loan;
var err_othr_cash_pymnt;

if (lang=="eng")
{
	var Error_101 = 'Please select / enter ~~1.';

	Error_01 = '~~1 should be in numbers.';
	Error_03 = 'Please select / enter ~~1.';
	Error_06 = '~~1 is invalid.';
	Error_15 = '~~1 is invalid.';
	Error_22 = '~~1 is invalid.';
	Error_23 = 'Please select / enter ~~1.';
	Error_46 = '~~1 has an invalid numeric format.';
	Error_47 = '~~1 should be negative.';
	Error_48 = '~~1 should be positive.';
	Error_49 = 'Same ~~1 within same month is not allowed.';
}
else
{
	var Error_101 = '請選擇/輸入~~1。';

	Error_01 = '~~1必須是數字。';
	Error_03 = '請選擇/輸入~~1。';
	Error_06 = '~~1不正確。';
	Error_15 = '~~1不正確。';
	Error_22 = '~~1不正確。';
	Error_23 = '請選擇/輸入~~1。';
	Error_46 = '~~1包含不正確的符號。';
	Error_47 = '~~1必須為負數。';
	Error_48 = '~~1必須為正數。';
	Error_49 = '不可選擇相同~~1於同一月份內。';
}

function getListVal(dropdownList)
{
	return dropdownList.options[dropdownList.selectedIndex].value;
}	

function ltrim(x) {
	return x.replace(/^\s*/,"");
}

function rtrim(x) {
	return x.replace(/\s*$/,"");
}

function trim(x) {
	return ltrim(rtrim(x));
}

function stripCommas(numString) 
{
	var re = /,/g;
	return numString.replace(re,"");
}

function formatCommas(amount)
{
	var delimiter = ","; 
	var i = parseInt(amount);
	
	if(isNaN(i)) { return ''; }
	
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3) {
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	amount = n; 
	amount = minus + amount;
	return amount;
}

function doChangeOpenCashBal(form)
{
	//alert("on change open cash bal");
	if (!chkOpenCashBal(form)) {
		alertErrorMessage();
	} 
}

function doChangeCashSaleRev(form)
{
	//alert("on change cash sale rev");
	err_cash_sale_rev = 0;
	if (!chkInput("Cash Sales Revenue","現金銷售",form.cash_sale_rev,form.cash_sale_rev.value,true,true,false,true)) {
		err_cash_sale_rev = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_inflow(form);
	}
}

function doChangeAcctRecvable(form)
{
	//alert("on change acct recv");
	err_acct_recvable = 0;
	if (!chkInput("Collection of Account Receivables","應收賬",form.acct_recvable,form.acct_recvable.value,false,true,false,true)) {
		err_acct_recvable = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_inflow(form);
	}
}

function doChangeIntRecv(form)
{
	//alert("on change Interest Received");
	err_int_recv = 0;
	if (!chkInput("Interest/Dividend Received","利息/股息收入",form.int_recv,form.int_recv.value,false,true,false,true)) {
		err_int_recv = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_inflow(form);
	}
}

function doChangeDispOfAsset(form)
{
	//alert("on change Disposal of Assets");
	err_disp_of_asset = 0;
	if (!chkInput("Disposal of Assets","出售資產",form.disp_of_asset,form.disp_of_asset.value,false,true,false,true)) {
		err_disp_of_asset = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_inflow(form);
	}
}

function doChangeCapInject(form)
{
	//alert("on change Capital Injection");
	err_cap_inject = 0;
	if (!chkInput("Capital Injection","注入資金",form.cap_inject,form.cap_inject.value,false,true,false,true)) {
		err_cap_inject = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_inflow(form);
	}
}

function doChangeLoanDrawDown(form)
{
	//alert("on change Loan Drawdown");
	err_loan_drawdown = 0;
	if (!chkInput("Loan Drawdown","提取貸款",form.loan_drawdown,form.loan_drawdown.value,false,true,false,true)) {
		err_loan_drawdown = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_inflow(form);
	}
}

function doChangeOthrCashRecpt(form)
{
	//alert("on change Other Cash Receipts");
	err_othr_cash_recpt = 0;
	if (!chkInput("Other Cash Receipts","其他現金收入",form.othr_cash_recpt,form.othr_cash_recpt.value,false,true,false,true)) {
		err_othr_cash_recpt = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_inflow(form);
	}
}

function cal_tot_cash_inflow(form)
{
	var cashin = 0;

	//alert(form.cash_sale_rev.value);
	if ((form.cash_sale_rev.value != "") && (form.cash_sale_rev.value != null) && (!err_cash_sale_rev)) 
		cashin += parseInt(stripCommas(trim(form.cash_sale_rev.value)),10);
	if ((form.acct_recvable.value != "") && (form.acct_recvable.value != null) && (!err_acct_recvable)) 
		cashin += parseInt(stripCommas(trim(form.acct_recvable.value)),10);
	if ((form.int_recv.value != "") && (form.int_recv.value != null) && (!err_int_recv)) 
		cashin += parseInt(stripCommas(trim(form.int_recv.value)),10);
	if ((form.disp_of_asset.value != "") && (form.disp_of_asset.value != null) && (!err_disp_of_asset)) 
		cashin += parseInt(stripCommas(trim(form.disp_of_asset.value)),10);
	if ((form.cap_inject.value != "") && (form.cap_inject.value != null) && (!err_cap_inject)) 
		cashin += parseInt(stripCommas(trim(form.cap_inject.value)),10);
	if ((form.loan_drawdown.value != "") && (form.loan_drawdown.value != null) && (!err_loan_drawdown)) 
		cashin += parseInt(stripCommas(trim(form.loan_drawdown.value)),10);
	if ((form.othr_cash_recpt.value != "") && (form.othr_cash_recpt.value != null) && (!err_othr_cash_recpt)) 
		cashin += parseInt(stripCommas(trim(form.othr_cash_recpt.value)),10);

	//alert("cash in flow = " + cashin);
	form.tot_cash_inflow.value = formatCommas(cashin);
}

function doChangeSalNAlwanc(form)
{
	//alert("on change Salary & Allowance");
	err_sal_n_alwanc = 0;
	if (!chkInput("Salary & Allowance","薪金及津貼",form.sal_n_alwanc,form.sal_n_alwanc.value,false,true,false,true)) {
		err_sal_n_alwanc = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_outflow(form);
	}
}

function doChangePymntOfAcctPayable(form)
{
	//alert("on change Payment of account payable");
	err_pymnt_of_acct_payable = 0;
	if (!chkInput("Payment of account payable","應付賬",form.pymnt_of_acct_payable,form.pymnt_of_acct_payable.value,false,true,false,true)) {
		err_pymnt_of_acct_payable = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_outflow(form);
	}
}

function doChangeIntPaid(form)
{
	//alert("on change Interest Paid");
	err_int_paid = 0;
	if (!chkInput("Interest Paid","利息支出",form.int_paid,form.int_paid.value,false,true,false,true)) {
		err_int_paid = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_outflow(form);
	}
}

function doChangeProcumntOfGoods(form)
{
	//alert("on change Procurement of goods");
	err_procumnt_of_goods = 0;
	if (!chkInput("Procurement of goods","購貨",form.procumnt_of_goods,form.procumnt_of_goods.value,false,true,false,true)) {
		err_procumnt_of_goods = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_outflow(form);
	}
}

function doChangeRentNUtil(form)
{
	//alert("on change Rental & Utilities");
	err_rent_n_util = 0;
	if (!chkInput("Rental & Utilities","租金及水電",form.rent_n_util,form.rent_n_util.value,false,true,false,true)) {
		err_rent_n_util = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_outflow(form);
	}
}

function doChangeRepayOfLoan(form)
{
	//alert("on change Repayment of Loan");
	err_repay_of_loan = 0;
	if (!chkInput("Repayment of Loan","信貸還款",form.repay_of_loan,form.repay_of_loan.value,false,true,false,true)) {
		err_repay_of_loan = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_outflow(form);
	}
}

function doChangeOthrCashPymnt(form)
{
	//alert("on change Other Cash Payments");
	err_othr_cash_pymnt = 0;
	if (!chkInput("Other Cash Payments","其他現金支出",form.othr_cash_pymnt,form.othr_cash_pymnt.value,false,true,false,true)) {
		err_othr_cash_pymnt = 1;
		alertErrorMessage();	
	} else {
		cal_tot_cash_outflow(form);
	}
}

function cal_tot_cash_outflow(form)
{
	var cashout = 0;

	if ((form.sal_n_alwanc.value != "") && (form.sal_n_alwanc.value != null) && (!err_sal_n_alwanc)) 
		cashout += parseInt(stripCommas(trim(form.sal_n_alwanc.value)),10);
	if ((form.pymnt_of_acct_payable.value != "") && (form.pymnt_of_acct_payable.value != null) && (!err_pymnt_of_acct_payable)) 
		cashout += parseInt(stripCommas(trim(form.pymnt_of_acct_payable.value)),10);
	if ((form.int_paid.value != "") && (form.int_paid.value != null) && (!err_int_paid)) 
		cashout += parseInt(stripCommas(trim(form.int_paid.value)),10);
	if ((form.procumnt_of_goods.value != "") && (form.procumnt_of_goods.value != null) && (!err_procumnt_of_goods)) 
		cashout += parseInt(stripCommas(trim(form.procumnt_of_goods.value)),10);
	if ((form.rent_n_util.value != "") && (form.rent_n_util.value != null) && (!err_rent_n_util)) 
		cashout += parseInt(stripCommas(trim(form.rent_n_util.value)),10);
	if ((form.repay_of_loan.value != "") && (form.repay_of_loan.value != null) && (!err_repay_of_loan)) 
		cashout += parseInt(stripCommas(trim(form.repay_of_loan.value)),10);
	if ((form.othr_cash_pymnt.value != "") && (form.othr_cash_pymnt.value != null) && (!err_othr_cash_pymnt)) 
		cashout += parseInt(stripCommas(trim(form.othr_cash_pymnt.value)),10);

	//alert("cash out flow = " + cashout);
	form.tot_cash_outflow.value = formatCommas(cashout);
}

function validate(form)
{
	var valid=true;
	
	Flag = false;
	if (!chkOpenCashBal(form)) valid=false;

	if (!chkInput("Cash Sales Revenue","現金銷售",form.cash_sale_rev,form.cash_sale_rev.value,true,true,false,true)) valid=false;
	if (!chkInput("Collection of Account Receivables","應收賬",form.acct_recvable,form.acct_recvable.value,false,true,false,true)) valid=false;
	if (!chkInput("Interest/Dividend Received","利息/股息收入",form.int_recv,form.int_recv.value,false,true,false,true)) valid=false;
	if (!chkInput("Disposal of Assets","出售資產",form.disp_of_asset,form.disp_of_asset.value,false,true,false,true)) valid=false;
	if (!chkInput("Capital Injection","注入資金",form.cap_inject,form.cap_inject.value,false,true,false,true)) valid=false;
	if (!chkInput("Loan Drawdown","提取貸款",form.loan_drawdown,form.loan_drawdown.value,false,true,false,true)) valid=false;
	if (!chkInput("Other Cash Receipts","其他現金收入",form.othr_cash_recpt,form.othr_cash_recpt.value,false,true,false,true)) valid=false;

	if (!chkInput("Salary &amp; Allowance","薪金及津貼",form.sal_n_alwanc,form.sal_n_alwanc.value,false,true,false,true)) valid=false;
	if (!chkInput("Payment of account payable","應付賬",form.pymnt_of_acct_payable,form.pymnt_of_acct_payable.value,false,true,false,true)) valid=false;
	if (!chkInput("Interest Paid","利息支出",form.int_paid,form.int_paid.value,false,true,false,true))  valid=false;
	if (!chkInput("Procurement of goods","購貨",form.procumnt_of_goods,form.procumnt_of_goods.value,false,true,false,true)) valid=false;
	if (!chkInput("Rental &amp; Utilities","租金及水電",form.rent_n_util,form.rent_n_util.value,false,true,false,true))  valid=false;
	if (!chkInput("Repayment of Loan","信貸還款",form.repay_of_loan,form.repay_of_loan.value,false,true,false,true)) valid=false;
	if (!chkInput("Other Cash Payments","其他現金支出",form.othr_cash_pymnt,form.othr_cash_pymnt.value,false,true,false,true)) valid=false;

	if (!chkExceptExp(form)) valid=false;

	if (valid) 
		form.submit();
	else {
		alertErrorMessage();
	}
}

function chkOpenCashBal(form)
{
	var d=(lang=="eng"?"Opening Cash Balance":"期初現金");
	var ret=true;
	
	var strBal = trim(form.open_cash_bal.value);
	//test for mandatory input
	if(!chkNotBlank(strBal, d, true, form.open_cash_bal)) return false;
	
	//test for numeric
	var f = parseFloat(strBal);
	if (isNaN(f)) {
		setErrorMessage(Error_01.replace(/~~1/, d), form.open_cash_bal)
		return false;
	}
	
	// test for valid numeric format
	if (!chkSeparator(strBal,d,form.open_cash_bal))
	{
		setErrorMessage(Error_46.replace(/~~1/, d), form.open_cash_bal);
		return false;
	}

	var intBal = parseInt(Math.round(stripCommas(strBal)),10);
	//alert(intBal);
	// test for valid range of values
	if ((intBal > 0) && (intBal > parseInt(999999999,10)))
	{
		setErrorMessage(Error_13.replace(/~~1/, d).replace(/~~2/, "999,999,999"), form.open_cash_bal);
		return false;
	}

	if ((intBal < 0) && (Math.abs(intBal) >  parseInt(999999999,10)))
	{
		setErrorMessage(Error_14.replace(/~~1/, d).replace(/~~2/, "-999,999,999"), form.open_cash_bal);
		return false;
	}

	form.open_cash_bal.value = formatCommas(intBal);
	//alert("open cash bal = " + form.open_cash_bal.value);
	return true;
}

function chkInput(e_desc, c_desc, obj, fieldValue, mandatory, allowPos, allowNeg, allowZero)
{
	var d=(lang=="eng"?e_desc:c_desc);
	var ret=true;
	
	//alert(d);
	var strBal = trim(fieldValue);
	//alert("strBal='"+strBal+"'");

	//test for mandatory input
	if (mandatory) {
		if(!chkNotBlank(strBal, d, true, obj)) return false;
	} else {
		if ((strBal == "") || (strBal == null)) return true;
	}
	
	//test for numeric
	var f = parseFloat(strBal);
	if (isNaN(f)) {
		setErrorMessage(Error_01.replace(/~~1/, d), obj)
		return false;
	}
	
	// test for valid numeric format
	if (!chkSeparator(strBal,d,obj)) {
		setErrorMessage(Error_46.replace(/~~1/, d), obj);
		return false;
	}

	var intBal = parseInt(Math.round(stripCommas(strBal)),10);
	//alert(intBal);
	// test for valid range of values
	if (allowPos) {
		if ((intBal > 0) && (intBal > parseInt(999999999,10)))	{
			setErrorMessage(Error_13.replace(/~~1/, d).replace(/~~2/, "999,999,999"), obj);
			return false;
		}
	} else	{
		if (intBal > 0 || (!allowZero && intBal ==0))	{
			setErrorMessage(Error_47.replace(/~~1/, d), obj);
			return false;
		}
	}

	if (allowNeg) {
		if ((intBal < 0) && (Math.abs(intBal) >  parseInt(999999999,10))) {
			setErrorMessage(Error_14.replace(/~~1/, d).replace(/~~2/, "-999,999,999"), obj);
			return false;
		}
	} else	{
		if (intBal < 0 || (!allowZero && intBal ==0))	{
			setErrorMessage(Error_48.replace(/~~1/, d), obj);
			return false;
		}
	}


	obj.value = formatCommas(intBal);
	//alert(e_desc + " = " + obj.value);
	return true;
}

function chkExceptExp(form)
{
	var d;
	var ret=true;
	var i, j;
	var dupMth;

	dupMth = 0;

	for (i=1;i<=3;i++) {
		
		if ((form.elements["ee_mth_" + i].selectedIndex == 0) &&
		    (form.elements["ee_purpose_" + i].selectedIndex == 0) &&
		    ((form.elements["ee_amt_" + i].value == '') || (form.elements["ee_amt_" + i].value == null))) 
	        {
		   continue
		} else {
		   d=(lang=="eng"?"Month of Exceptional Expenses":"非經常性現金支出月份");
		   if (form.elements["ee_mth_" + i].selectedIndex == 0) {
			setErrorMessage(Error_03.replace(/~~1/, d), form.elements["ee_mth_" + i]);
			ret = false;
		   }
		   d=(lang=="eng"?"Purpose of Exceptional Expenses":"非經常性現金支出用途");
		   if (form.elements["ee_purpose_" + i].selectedIndex == 0) {
			setErrorMessage(Error_03.replace(/~~1/, d), form.elements["ee_purpose_" + i]);
			ret = false;
		   }
		   d=(lang=="eng"?"Amount of Exceptional Expenses":"非經常性現金支出金額");
		   if ((form.elements["ee_amt_" + i].value == '') || (form.elements["ee_amt_" + i].value == null)) {
			setErrorMessage(Error_03.replace(/~~1/, d), form.elements["ee_amt_" + i]);
			ret = false;
		   } else {
			if (!chkInput("Amount of Exceptional Expenses","非經常性現金支出金額",form.elements["ee_amt_" + i],form.elements["ee_amt_" + i].value,true,true,false,false)) ret=false;
		   }	
		   if (ret==true) {
			for (j=i+1;j<=3;j++) {
		   		d=(lang=="eng"?"Purpose of Exceptional Expenses " + j:"非經常性現金支出用途");
				if ((form.elements["ee_mth_" + i].options[form.elements["ee_mth_" + i].selectedIndex].value == form.elements["ee_mth_" + j].options[form.elements["ee_mth_" + j].selectedIndex].value) &&
				    (form.elements["ee_purpose_" + i].options[form.elements["ee_purpose_" + i].selectedIndex].value == form.elements["ee_purpose_" + j].options[form.elements["ee_purpose_" + j].selectedIndex].value)) {
					setErrorMessage(Error_49.replace(/~~1/, d), form.elements["ee_purpose_" + j]);
					ret = false;			    	
				}
			}	   	
		   }	
		}   
	}
	return ret;
}

