var LITERS_IN_GALLON=3.7854118;
var DAYS_IN_YEAR=365.25;

function initFields() {
	//document.getElementById('resultsPanel').style.visibility = 'hidden';
	jQuery('#resultsPanel').hide();
	jQuery('#contentPanel').show();
}
function calculateGasAnnualCost(gasPricePerLiter, dailyMiles, mpg) {
	gasPricePerGallon=gasPricePerLiter*LITERS_IN_GALLON;
	annualMiles=dailyMiles*DAYS_IN_YEAR;
	gasAnnualCost=annualMiles/mpg*gasPricePerGallon;
	return gasAnnualCost;
}
function calculateAnnualGallonsConserved(dailyMiles, vehicleMPG, toyotaVehicleMPG) {
	annualMiles=dailyMiles*DAYS_IN_YEAR;
	annualGallonsConserved=annualMiles/vehicleMPG-annualMiles/toyotaVehicleMPG;
	return annualGallonsConserved;
}
function calculate() {
	if (validateForm()){;
	
	writeSelectedVehicle();
	
	f=document.frmCalculator;
	// +1 temp fix
	currIdx=f.mpg_vehicle.selectedIndex + 1;
	
//	annualCost = calculateGasAnnualCost(f.gas_price.value/100, f.daily_miles.value, f.vehicle_mpg.value);
//	annualCostToyota = calculateGasAnnualCost(f.gas_price.value/100, f.daily_miles.value, vehicles[currIdx].mpg);
    annualCost          = calculateGasAnnualCost(f.gas_price.value, f.daily_miles.value, f.vehicle_mpg.value);
	annualCostToyota = calculateGasAnnualCost(f.gas_price.value, f.daily_miles.value, vehicles[currIdx].mpg);
	annualGallonsConserved = calculateAnnualGallonsConserved(f.daily_miles.value, f.vehicle_mpg.value, vehicles[currIdx].mpg)
	
	writeText('annual_cost', formatCurrency(annualCost));
	writeText('annual_cost_toyota', formatCurrency(annualCostToyota));
	writeText('annual_savings_toyota', formatCurrency(annualCost-annualCostToyota));
	writeText('galons_savings', Round(annualGallonsConserved, 2));
	
	//document.getElementById('resultsPanel').style.visibility = 'visible';
	//document.getElementById('contentPanel').style.visibility = 'none';
	jQuery('#resultsPanel').show();
	jQuery('#contentPanel').hide();
    }
}
function validateForm() {
	f=document.frmCalculator;
	if ( !isPosNumber(f.mpg_vehicle.value) || !isPosNumber(f.gas_price.value) || !isPosNumber(f.daily_miles.value) || !isPosNumber(f.vehicle_mpg.value) ) {
		alert("Recuerde llenar todos los encasillados requeridos con valores validos.")
		return false
	} else {
		return true
	}
}
function writeText(theID, theText) {
	//document.getElementById(theID).innerHTML = theText;
	theID = '#' + theID;
	jQuery(theID).html(theText);
}
function writeSelectedVehicle() {
	l=document.frmCalculator.mpg_vehicle;
	// +1 temp fix
	currIdx=l.selectedIndex + 1;
	if (currIdx > 0)
		for(var j=1; j<=3; j++) writeText('selected_model_'+j, vehicles[currIdx].caption + ' ('+vehicles[currIdx].model + ')');
	else
		for(var j=1; j<=3; j++) writeText('selected_model_'+j, '');
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function isPosNumber(inputVal) {
	if (inputVal == "") {
		return false
	} else {
		oneDecimal = false
		inputStr = inputVal.toString()
		for (var i = 0; i < inputStr.length; i ++) {
			var oneChar = inputStr.charAt(i)
			if(i == 0 && oneChar == "-") {
				return false
			}
			if(oneChar == "." && !oneDecimal) {
				oneDecimal = true
				continue
			}
			if(oneChar < "0" || oneChar > "9") {
				return false
			}
		}
	}
	return true
}
function removeCurrency(strValue) {
  var objRegExp = /\(/;
  var strMinus = '';
  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }
  objRegExp = /\)|\(|[%]/g;
  strValue = strValue.replace(objRegExp,'');
  return strMinus + strValue;
}
function CStr(VALUE) {
	return VALUE.toString();
}
function Round(NUMBER,PLACES) {
	NUMBER = CStr(NUMBER);
	arrNUMBER = NUMBER.split(".");
	if(arrNUMBER.length==1){return NUMBER;}
	if(PLACES){
		NUMBER = Math.round(NUMBER*Math.pow(10,PLACES))/Math.pow(10,PLACES);
	} else {
		NUMBER = Math.round(NUMBER);
	}
	return NUMBER;
}
