var c = new(Array);

if(lang == 'en'){
	c['income_must_fill'] = 'Income must be filled';
	c['income_must_numeric'] = 'Income must be a numeric value';
	c['credit_must_fill'] = 'Credit must be filled';
	c['credit_must_numeric'] = 'Credit must be a numeric value';
	c['income_too_low'] = 'Minimum revenue is Rp. 2,500,000!';
	c['costs_must_numeric'] = 'Costs must be a numeric value';
	c['credit_too_low'] = 'Credit needed must be superior or equal to Rp. 100,000,000!';
	c['installments_too_high'] = 'Your current installments represents more than 35% of your monthly revenue.';
	c['available_revenue_too_low'] = new Array();
	c['available_revenue_too_low'][1] = 'Your available revenue (Rp.';
	c['available_revenue_too_low'][2] = ') is lower than the monthly reimbursement cost (Rp.';
	c['available_revenue_too_low'][3] = '). Please raise credit reimbursement duration or lower requested credit amount';
	c['costs_must_positive'] = 'Cost cannot be a negative number';
	c['costs_too_high'] = new Array();
	c['costs_too_high'][1] = 'Current Installments represent '; 
	c['costs_too_high'][2] = '% of your monthly revenue. Ratio of monthly installments must be lower than 35% to be eligible.';
	c['success_message'] = new Array();
	c['success_message'][1] = 'You are eligible for a credit of Rp.';
	c['success_message'][2] = ', reimbursable on a monthly basis of Rp.';
	c['success_message'][3] = '(duration: '+years+' years)';
}
else {
	c['income_must_fill'] = 'Pendapatan harus diisi';
	c['income_must_numeric'] = 'Pendapatan harus dalam bentuk angka';
	c['credit_must_fill'] = 'Kredit harus diisi';
	c['credit_must_numeric'] = 'Kredit harus dalam bentuk angka';
	c['income_too_low'] = 'Minimum pendapatan adalah Rp. 2,500,000!';
	c['costs_must_numeric'] = 'Cicilan lain harus dalam bentuk angka';
	c['credit_too_low'] = 'Kredit yang diajukan harus setidaknya Rp. 100,000,000!';
	c['installments_too_high'] = 'Cicilan yang harus anda tanggung tidak boleh lebih dari 35% dari pendapatan bulanan';
	c['available_revenue_too_low'] = new Array();
	c['available_revenue_too_low'][1] = 'Dana yang anda miliki (Rp.';
	c['available_revenue_too_low'][2] = ') lebih rendah dari jumlah cicilan (Rp.';
	c['available_revenue_too_low'][3] = '). Silahkan tingkatkan periode pinjaman atau kurangi jumlah pinjaman';
	c['costs_must_positive'] = 'Cicilan lain tidak bisa dalam bentuk angka negatif';
	c['costs_too_high'] = new Array();
	c['costs_too_high'][1] = 'Pinjaman sekarang mewakili '; 
	c['costs_too_high'][2] = '% dari pendapatan bulanan anda. Rasio dari jumlah cicilan per bulan harus lebih rendah dari 35%';
	c['success_message'] = new Array();
	c['success_message'][1] = 'Anda dapat mengajukan kredit sebesar Rp.';
	c['success_message'][2] = ', dengan cicilan bulana sebesar Rp.';
	c['success_message'][3] = '(selama: '+years+' tahun)';
	
	
}

alert(c['en']['income_must_fill']);
function number_format (number, decimals, dec_point, thousands_sep) {
	var n = number, prec = decimals;
	var toFixedFix = function (n,prec) {
		var k = Math.pow(10,prec);
		return (Math.round(n*k)/k).toString();
	};
	n = !isFinite(+n) ? 0 : +n;
	prec = !isFinite(+prec) ? 0 : Math.abs(prec);
	var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
	var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
	var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
	var abs = toFixedFix(Math.abs(n), prec);
	var _, i;
	if (abs >= 1000) {
		_ = abs.split(/\D/);
		i = _[0].length % 3 || 3;
		_[0] = s.slice(0,i + (n < 0)) +
		_[0].slice(i).replace(/(\d{3})/g, sep+'$1');
		s = _.join(dec);
	} 
	else
		s = s.replace('.', dec);
	var decPos = s.indexOf(dec);
	if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec)
		s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
	else if (prec >= 1 && decPos === -1)
		s += dec+new Array(prec).join(0)+'0';
	return s;
}
function Calculate(){
	var fields = new Array('income','costs','credit');
	var income = GetId('income').value;
	var costs = GetId('costs').value;
	var credit = GetId('credit').value;
	var years = GetId('years').options[GetId('years').selectedIndex].value;
	
	var err = new(Array);
	if(income == '')  err['income'] = c['income_must_fill'];
	else if(isNaN(income)) err['income'] = c['income_must_numeric'];
	else if(eval(income) < 2500000) err['income'] = c['income_too_low'];
	
	if(costs == '') GetId('costs').value = 0;
	if(isNaN(costs)) err['costs'] = c['costs_must_numeric'];
	else if(costs < 0) err['costs'] = c['costs_must_positive'];
	
	if(credit == '')  err['credit'] = c['credit_must_fill'];
	else if(isNaN(credit)) err['credit'] = c['credit_must_numeric'];
	else if(eval(credit) < 100000000)
	err['credit'] = c['credit_too_low'];
	else{
		// Get Interest Rates
		if(eval(credit) <= 250000000) var rate = 11.99;
		else if(eval(credit) <= 999999999) var rate = 11.49;
		else var rate = 10.99;
	}
	var errors = 0;
	for(i=0;i!=fields.length;i++){
		if(err[fields[i]]){
			GetId(fields[i]+'_err').innerHTML = err[fields[i]];	
			GetId(fields[i]+'_err').className = 'err';	
			GetId(fields[i]).className = 'err';	
			errors++;
		}
		else{
			GetId(fields[i]+'_err').innerHTML = '';	
			GetId(fields[i]+'_err').className = '';
			GetId(fields[i]).className = 'ok';	
		}
	}
	if(errors == 0) {
		var costs_ratio = (eval(costs)/eval(income)*100).toFixed(2);
		if(costs_ratio >= 35){
			GetId('msg').innerHTML = c['costs_too_high'][1]+costs_ratio+c['costs_too_high'][2];	
			GetId('msg').className = 'error';
		}
		else {			
			var max_monthly_amount = Math.round(.35*eval(income));
			var months = years*12;
			// Calculate interest amount :
			var interests = (rate/100)*years*credit;
			var total_cost = interests+eval(credit);
			GetId('interests').innerHTML = number_format(interests,2,'.',',')+' ('+rate+'% / year)';
			GetId('total_cost').innerHTML = number_format(total_cost,2,'.',',');
			
			// Calculate monthly reimbursement cost for client
			var monthly_cost = total_cost/(years*12);
			GetId('monthly_cost').innerHTML = number_format(monthly_cost,2,'.',',');
			// Check If available revenue for customer is more than the monthly cost
			var available_revenue = (.35*eval(income))-costs;
			if(available_revenue >= monthly_cost){
				GetId('msg').innerHTML = c['success_message'][1]+number_format(credit,2,'.',',')+c['success_message'][2]+number_format(monthly_cost,2,'.',',')+c['success_message'][3];	
				GetId('msg').className = 'ok';
			}
			else{
				if(available_revenue < 0) {
					GetId('msg').innerHTML = c['installments_too_high'];
				}
				else {
				GetId('msg').innerHTML = c['available_revenue_too_low'][1]+number_format(available_revenue,2,'.',',')+c['available_revenue_too_low'][2]+number_format(monthly_cost,2,'.',',')+c['available_revenue_too_low'][3];
				}
				GetId('msg').className = 'error';
			}
		}
	GetId('result_box').style.display = "block";
	document.location = '#results';
	}
	else{
		GetId('result_box').style.display = "none";
		document.location = '#';
	}
}
function CommIt(id){
	var val = GetId(id).value;
	if(!isNaN(val)){ 
		var lg = val.length;
		var c = 0;
		if(lg >= 4 && lg <= 6) c = 1
		else if(lg >= 7 && lg <= 9) c = 2;
		else if(lg >= 10) c = 3;
		if(c != 0)
		GetId(id).style.backgroundImage = 'url(/gfx/commas/'+c+'.gif)';	
		else
		GetId(id).style.backgroundImage = '';	
		
	}
	
}

