function calcFuelCost (tankSize, mpgHwy, mpgCity) {
  if (tankSize) {
    //calculate the Cost to Fuel Up
    return parseFloat(( gasPrice * tankSize ).toFixed(2));
  }

  //calculate the Annual Fuel Cost
  var totalYrlyMiles = 15000;
  var percCity = .55;
  var percHwy = .45;


  //calculate the yearly cost
  var cost = Math.ceil(gasPrice * (Math.ceil( (percCity*totalYrlyMiles) / mpgCity ) + Math.ceil( (percHwy*totalYrlyMiles) / mpgHwy )));

  return cost;
}

//Add Tooltips to help icons
function addToolTips () {
  var el = $('greencars_list');
  var hlp;

  if(parseFloat(Prototype.Version) < 1.6){
    hlp = el.getElementsBySelector('a.help_icon');
  } else {
    //Version 1.6 and above prototype
    hlp = el.select('a.help_icon');
  }

  hlp.each(function (elm) {
    new Tip(elm, $(elm.rel).innerHTML, {
      hook: {target: 'topMiddle', tip: 'bottomRight'},
      stem: 'bottomRight',
      style: 'creamy'
    });
  });
}

//Update the list
function updateVals () {
  var Ul = $('greencars_list');
  var gas;

  //update the gasPrice var
  if ( !isNaN(gas = parseFloat($('gasPrice').value)) && gas >= 0 && gas < 10) {
    $('gasPrice').value = gas;
    gasPrice = gas;
  } else {
    alert("Please enter a dollar amount in the following format: #.## (example: 3.00).");
    $('gasPrice').value = gasPrice;
    return false;
  }

  //update content links
  updateContentLinks();

  //clean whitespace
  Ul.cleanWhitespace();

  var data;

  if(parseFloat(Prototype.Version) < 1.6){
    data = Ul.getElementsBySelector('li.greencar_entry');
  } else {
    //Version 1.6 and above prototype
    data = Ul.select('li.greencar_entry');
  }

  //change update btn image
  //$('gasPriceBtn').src = IMGPATH + '/updated-anim.gif';
  $('pbar').show();

  //recalculate values
  data.each(function (li) {
    if (li.nodeName !== "LI") return;
    var tankSize;
    var mpgCity;
    var mpgHwy;
    var costFuelUp;
    var costFuelYr;
    var costFuelUpElm;
    var costFuelYrElm;

    if(parseFloat(Prototype.Version) < 1.6){

      //grab the tankSize
      tankSize= parseFloat($(li).getElementsBySelector('td.greencars_data_gals')[0].firstChild.nodeValue);
      //grab the MPG City
      mpgCity= parseFloat($(li).getElementsBySelector('.greencars_data_mpgCity')[0].firstChild.nodeValue);
      //grab the MPG Hwy
      mpgHwy= parseFloat($(li).getElementsBySelector('.greencars_data_mpgHwy')[0].firstChild.nodeValue);

    } else {
      //Version 1.6 and above prototype

      //grab the tankSize
      tankSize = parseFloat($(li).select('td.greencars_data_gals')[0].firstChild.nodeValue);
      //grab the MPG City
      mpgCity = parseFloat($(li).select('.greencars_data_mpgCity')[0].firstChild.nodeValue);
      //grab the MPG Hwy
      mpgHwy = parseFloat($(li).select('.greencars_data_mpgHwy')[0].firstChild.nodeValue);
    }

    //run the calculations
    costFuelUp = calcFuelCost (tankSize);
    costFuelYr = calcFuelCost (null, mpgHwy, mpgCity);

    //set the values in the data table
    if(parseFloat(Prototype.Version) < 1.6){
      costFuelUpElm = $(li).getElementsBySelector('.greencars_data_costFuelUp')[0].firstChild;
      costFuelYrElm = $(li).getElementsBySelector('.greencars_data_costFuelYr')[0].firstChild;
      costFuelUpElm.nodeValue = numberFormat(costFuelUp, true);
      costFuelYrElm.nodeValue = numberFormat(costFuelYr, true);

    } else {

      //Version 1.6 and above prototype
      costFuelUpElm = $(li).select('.greencars_data_costFuelUp')[0].firstChild;
      costFuelYrElm = $(li).select('.greencars_data_costFuelYr')[0].firstChild;
      costFuelUpElm.nodeValue = numberFormat(costFuelUp);
      costFuelYrElm.nodeValue = numberFormat(costFuelYr, true);

    }

    //highlight the changes
    highlightChanges($(costFuelUpElm.parentNode));
    highlightChanges($(costFuelYrElm.parentNode));

  });

  //change the update btn image back
  var funcCall = "set_btn_img('" + IMGPATH + '/fuel_efficient/btn_update.png' + "')";
  window.setTimeout(funcCall,1000);

  return false;
}

function set_btn_img (path) {
  //$('gasPriceBtn').src = path;
  $('pbar').hide();
}

function content_hover () {
  var el = $('fuelefficient_content_list');
  var trs;

  if(parseFloat(Prototype.Version) < 1.6){
    trs = el.getElementsBySelector('tr');
  } else {
    //Version 1.6 and above prototype
    trs = el.select('tr');
  }


  trs.each(function (e) {
    var main_class = 'fuelefficient_content_tab';
    var hover_class = 'fuelefficient_content_tab_hover';
    var sel_class = 'fuelefficient_content_tab_HL';
    var cur_class;

    e.cleanWhitespace();

    var td = $(e.firstChild);

    if (td.nodeName == "TD" && ( td.hasClassName(main_class) || td.hasClassName(sel_class) ) ) {
      td.cleanWhitespace();

      cur_class = td.className;

      td.observe('mouseover', function() {

        if(td.hasClassName(main_class)) {
          td.removeClassName(cur_class);
          td.addClassName(hover_class);
        }
      }.bindAsEventListener(td));

      td.observe('mouseout', function() {

        if(td.hasClassName(hover_class)){
          td.removeClassName(hover_class);
          td.addClassName(cur_class);
        }
      }.bindAsEventListener(td));

      td.observe('click', function(tgt) {

        if((tgt.target.nodeName !== 'A')&& td.firstChild.href) {
          location.href = td.firstChild.href;
        }
      }.bindAsEventListener(td));

    }

  });
}

function updateContentLinks () {
  var el = $('fuelefficient_content_list');
  var lnks;

  if(parseFloat(Prototype.Version) < 1.6){
    lnks = el.getElementsBySelector('a');
  } else {
    //Version 1.6 and above prototype
    lnks = el.select('a');
  }

  lnks.each (function (a) {

    var host = '/index.php/Green';
    var srchStr = 'action=';
    var idxStart = a.href.indexOf(srchStr);
    if (idxStart < 1) {
      a.href = host + '?gasPrice=' + gasPrice;
      return;
    }
    idxStart += srchStr.length;
    var idxEnd = a.href.indexOf('&',idxStart);
    idxEnd = (idxEnd < 1) ? a.href.length : idxEnd;
    var action = a.href.substring(idxStart, idxEnd);
    a.href = host + '?action=' + action + '&gasPrice='+gasPrice;
  });
}

function numberFormat(num, is_int) {
  //convert float to a money-format
  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));

  if (Boolean(is_int)) {
    return (((sign)?'':'-') + '$' + num);
  }
  return (((sign)?'':'-') + '$' + num + '.' + cents);
  //return num;
}

if ( !Number.prototype.toFixed ) Number.prototype.toFixed = function(fractionDigits)
{
  var m = Math.pow(10,fractionDigits);
  return Math.round(this*m,0)/m;
}

function localCheck(elm, frm) {
  var zip = elm.value;
  zipForm = frm;
  if (checkZIP(zip)) {
    ajaxCheckZIP(zip);
    return false;
  } else {
    wrongZIP();
    return false;
  }
}

function resZIP (originalRequest, json) {
  var feed = originalRequest.responseText.evalJSON();
  if (feed.valid) {
    zipForm.submit();
    return true;
  } else {
    wrongZIP();
    return false;
  }
}

function wrongZIP() {
  alert('Please enter a valid U.S. ZIP code to continue.');
  zipForm.zipcode.value = "ZIP";
}
