var ccMaxDigit = 4;
var TaxCoefs = {};
TaxCoefs['car'] = {};
TaxCoefs['car']['0']    = {'a':400, 'b':0, 'c':0, 'd':0.92};
TaxCoefs['car']['600']  = {'a':400, 'b':0.25, 'c':600, 'd':0.92};
TaxCoefs['car']['1000'] = {'a':500, 'b':0.75, 'c':1000, 'd':0.92};
TaxCoefs['car']['1600'] = {'a':950, 'b':1.5, 'c':1600, 'd':0.92};
TaxCoefs['car']['3000'] = {'a':3050, 'b':2, 'c':3000, 'd':0.92};

TaxCoefs['motor'] = {};
TaxCoefs['motor']['0']    = {'a':80, 'b':0, 'c':0, 'd':0.92};
TaxCoefs['motor']['200']  = {'a':80, 'b':0.3, 'c':200, 'd':0.92};
TaxCoefs['motor']['1000'] = {'a':320, 'b':0.6, 'c':1000, 'd':0.92};

function calculateTax() {
  var result;
  var vehType = $('input[name=vehicle_type]:checked').val();
  var cc = $('#txtEC').val();
  var age = $('#txtAge').val();
  
  if (cc == "" || cc.indexOf('0') == 0) {
    alert('Invalid Engince Capacity');
    return false;
  }
  
	if(vehType=='car')
	{	//car, result for 6 month
		if(cc<=600)
		{
			result=200;
		}
		else if(cc<=1000 && cc>600)
		{
			result=200 + (cc - 600) * 0.125;
		}
		else if(cc<=1600 && cc>1000)
		{
			result=250 + (cc - 1000) * 0.375;
		}
		else if(cc<=3000 && cc>1600)
		{
			result=475 + (cc - 1600) * 0.75;
		}
		else
		{
			result=1525+ (cc - 3000);
		}
		result= result * 0.782;
		//surcharge
		if(age=="1")
		{
			surcharge=0;
		}
		else if(age=="2")
		{
			surcharge=result / 10;
		}
		else if(age=="3")
		{
			surcharge=result / 5;
		}
		else if(age=="4")
		{
			surcharge=result * 3 / 10;
		}
		else if(age=="5")
		{
			surcharge=result * 2 / 5;
		}
		else if(age=="6")
		{
			surcharge=result / 2;
		}
		
	}
	else if(vehType=='motor')
	{	//motor
		if(cc<=200)
		{
			result=40;
		}
		else if(cc<=1000 && cc>200)
		{
			result=40 + (cc - 200) * 0.15;
		}
		else if(cc>1000)
		{
			result=160+ (cc - 1000) * 0.3;
		}
		result= result * 0.782;
		//surcharge
		if(age=="1")
		{
			surcharge=0;
		}
		else if(age=="2")
		{
			surcharge=result / 10;
		}
		else if(age=="3")
		{
			surcharge=result / 5;
		}
		else if(age=="4")
		{
			surcharge=result * 3 / 10;
		}
		else if(age=="5")
		{
			surcharge=result * 2 / 5;
		}
		else if(age=="6")
		{
			surcharge=result / 2;
		}
	}
  
  /*if (vehType == 'car' && cc <= 600) {
    result = 400*0.92;
  }
  else if (vehType == 'motor' && cc <= 200) {
    result = 80*0.92;
  }
  else {   
    var coef = getTaxCoefs(cc, vehType);
    result = ((coef.a-0) + coef.b*(cc-coef.c))*coef.d;
  }*/
  //var output = "<hr/><b>The Road Tax is :<br> <span style='font-size:12pt'>S$" + round(result) + "</span> (for 6-month)<br> or <br><span style='font-size:12pt'>S$" + round((result*2)) + "</span> (for 1 year)</b>.";
  total=result+surcharge;
  resultyear=result*2;
  surchargeyear=surcharge*2;
  totalyear=total*2;
  var output="<hr/><span style='font-size:12pt'><b>For 6 month : </b></span></b><br/>Road Tax: <b>$ " + result.toFixed(2) +"</b><br/>Annual Road Tax Surcharge: <b>$ " + surcharge.toFixed(2) + "</b><br/>Total Road Tax: <b>$ " + total.toFixed(2) +"</b><br/><br/><span style='font-size:12pt'><b>For 1 Year : </b></span><br/>Road Tax: <b>$ " + resultyear.toFixed(2) +"</b><br/>Annual Road Tax Surcharge: <b>$ " + surchargeyear.toFixed(2) + "</b><br/>Total Road Tax: <b>$ " + totalyear.toFixed(2) + "</b>";
  
  $('#calculation_result').html(output);
}

function getTaxCoefs(cc, vehType) {
  var coef;
  if (vehType == 'car') {
    if (cc > 3000)
      coef = TaxCoefs['car']['3000'];
    else if (cc > 1600)
      coef = TaxCoefs['car']['1600'];
    else if (cc > 1000)
      coef = TaxCoefs['car']['1000'];
    else if (cc > 600)
      coef = TaxCoefs['car']['600'];
    else
      coef = TaxCoefs['car']['0'];
  }
  else if (vehType == 'motor') {
    if (cc > 1000)
      coef = TaxCoefs['motor']['1000'];
    else if (cc > 200)
      coef = TaxCoefs['motor']['200'];
    else
      coef = TaxCoefs['motor']['0'];
  }
  return coef;
}

function calculateLoan() {
  var type      = $('input[name=loan_type]:checked').val() 
  var principal = $("#principal").val();
  var dp        = $("#dp").val();
  var interest  = $("#interest").val();
  var payments  = $("#period").val();
  
  if (principal == "" || principal.indexOf('0') == 0) {
    alert('Invalid Car Price');
    return false;
  }
  if (dp == "") {
    alert('Invalid Down Payment');
    return false;
  }
  if (interest == "") {
    alert('Invalid Interests Rate');
    return false;
  }
  else {
    interest = interest/100;
  }

  // Now compute the monthly payment figure, using esoteric math.
  var x = Math.pow(1 + interest, payments);
  var total = (principal-dp)*interest*payments + (principal-dp);

  // Check that the result is a finite number. If so, display the results
  if (!isNaN(total) && 
      (total != Number.POSITIVE_INFINITY) &&
      (total != Number.NEGATIVE_INFINITY)) {
      var result = 0;
      var title = "";
      if (type == 'mp') {
        result = round(total/payments/12);
        title = "Monthly Payment";
      }
      else if (type == 'la') {
        result = round(total);
        title = "Loan Amount";
      }
      $("#loan_result").html("<hr/><b>"+title+" :<br> <span style='font-size:12pt'>S$" + result + "</span><br>");
  }
    
}

// This simple method rounds a number to two decimal places.
function round(x) {
  return Math.round(x*100)/100;
}

function IsNumeric(sText) {
 var ValidChars = "0123456789.";
 var IsNumber=true;
 var Char;


 for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
       {
       IsNumber = false;
       }
    }
 return IsNumber;
}

