var waluta = "zł";
var P_AMT;
var A_AMT;
var A_LPG_CC;
var P_GAL;
var A_GAL;
var CONVERTER =	1;
var P_FUEL_C;
var A_FUEL_C;

function tamounts() {
  var mileage = document.frmsave.Pannu_mile.value;
  if (isPosInterger(mileage)) {
  if (mileage == null || mileage == "") {
  alert("Wpisz poprawn± warto¶ć kilometrów. np: 5000");
  } else {
  showme();
  }
  } else {
  alert("Bł±d: wprowadĽ liczbę - np: 5000");
  }
}

function showme() {
var Pmile, Pmpl, Amile, Ampl, Acost, Pcost, Pmaint, Amaint,Pt, At, Pts, Ats, mpgV, mpgA, LpA, LpV, Amile_perl;
Pmile = document.frmsave.Pannu_mile.value;
Amile = document.frmsave.Aannu_mile.value;
Amile_perl = document.frmsave.Pmile_perl.value * 1.15;
mpgV = 100 / document.frmsave.Pmile_perl.value;
mpgA = 100 / Amile_perl;
LpV = format(mpgV / CONVERTER, 2);
LpA = format(mpgA / CONVERTER, 2);
Pmpl = parseFloat(LpV);
Ampl = parseFloat(LpA);
Pcost = document.frmsave.Pfuel_cost.value; 
Acost = document.frmsave.Afuel_cost.value; 
Pmaint = document.frmsave.Pann_amaint.value;
Pmaint = parseFloat(Pmaint);
Amaint = document.frmsave.Aann_amaint.value;
Amaint = parseFloat(Amaint);
LPG_CC_u = document.frmsave.Afuel_con.value;
LPG_CC_u = parseFloat(LPG_CC_u);

document.frmsave.Amile_perl.value = format(Amile_perl, 1);
document.frmsave.Pfuel_comp.value = format(Pmile / Pmpl , 2);
document.frmsave.Afuel_comp.value = format(Amile / Ampl , 2);
document.frmsave.Pfuel_acost.value = format(((Pmile / Pmpl) * Pcost), 2) + waluta;
document.frmsave.Afuel_acost.value = format(((Amile / Ampl) * Acost), 2) + waluta;
document.frmsave.Afuel_asave.value = format((((Pmile / Pmpl) * Pcost) - ((Amile / Ampl) * Acost)), 2) + waluta;
document.frmsave.Ptotal_acost.value = format(((Pmile / Pmpl) * Pcost) + Pmaint, 2) + waluta;
document.frmsave.Atotal_acost.value = format(((Amile / Ampl) * Acost) + Amaint + LPG_CC_u, 2) + waluta;

}

function valid_hole() {
var one = document.frmsave.Pnov.value;
var message = "none";
var check = "good";

if (isPosInterger(one)) {
if (one == null || one == "") {
message = "Wprowadź poprawną wartość";
check = "bad";
}
} else {
message = "Wprowadź poprawną wartość";
check = "bad";
}

var two = document.frmsave.Pmpv.value;
if (isPosInterger(two)) {
if (two == null || two == "") {
message = "Wprowadź poprawną wartość";
check = "bad";
}
} else {
message = "Wprowadź poprawną wartość";
check = "bad";
}

var thr = document.frmsave.Pmpg.value;

if (isNumber(thr)) {thr = parseInt(thr);}
if (isPosInterger(thr)) {
if (thr == null || thr == "") {
message = "Wprowadź poprawną wartość";
check = "bad";
}
} else {
message = "Wprowadź poprawną wartość";
check = "bad";
}

if (check == "good") {
analyst();
} else {
alert(message);
}
}

function analyst() {
var NoV, MperV, mpgV, LpV, Fcons, AFcons, FperL, Psave, Asave, Psaves, Asaves;

Nov = document.frmsave.Pnov.value;
MperV = document.frmsave.Pmpv.value;
MperV = Nov * MperV;
mpgV = document.frmsave.Pmpg.value;
LpV = format(mpgV / CONVERTER, 2);
Fcons = format(MperV / LpV, 2);
FperL = document.frmsave.Pfuel.value / 100;
Psave = format(Fcons * FperL, 2);
AFcons = format(MperV / A_MILE, 2)
Asave = format(AFcons * (A_FUEL_C / 100), 2);
if (parseInt(Psave) >= parseInt(Asave)) {
Psaves = "0.00" + waluta;
Asaves = (format(Psave - Asave, 2)) + waluta;
} else {
Asaves = "0.00" + waluta;
Psaves = (format(Asave - Psave, 2)) + waluta;
}
document.frmsave.Pcalc.value = Psaves;
document.frmsave.Acalc.value = Asaves;
}

function format(expr, decplaces) {
var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
while (str.length <= decplaces) {
	str = "0" + str
	}
var decpoint = str.length - decplaces
return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}

function isPosInterger(inputVal) {
 var inputStr = inputVal.toString();
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			return false;
			}
		}
		return true;
	}

function isNumber(inputVal) {
	var oneDecimal = false;
	var inputStr = inputVal.toString();
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
		continue;
		}
		if (oneChar == "." && !oneDecimal) {
		oneDecimal = true;
		continue;
		}
		if (oneChar < "0" || oneChar > "9") {
		return false;
		}
		}
		return true;
		}

function transptoa() {
var jay;
jay = document.frmsave.Pannu_mile.value;
document.frmsave.Aannu_mile.value = jay;
}

function checknumb(fig, message) {

if (isNumber(fig)) {fig = parseInt(fig);}
if (isPosInterger(fig)) {
if (fig == null || fig == "") {
alert (message)
}
} else {
alert (message)
}
}

