// JavaScript Document
function isDigit(c) {
  return((c>="0")&&(c<="9")||(c=="."))
}

function numCheck(num) {
  var valid = true;
  var i = 0;
  for (i; i < num.length; i++) {
    var c = num.charAt(i);
    if(!isDigit(c)){
      valid = false;
      break;
    }
  }
  if(valid){ return true; } else { return false; }      
}

function validate() {    
  var miles = $("#miles").val();
  var mpg = $("#mpg").val();
  var commute = $("#commute").val();
  var childcare = $("#childcare").val();
  var commutetime = $("#commutetime").val();
  var labor = $("#labor").val();
  var gas = $("#gas").val();
  var valid = true;
  var errors = "";
  if(miles == null || miles == "") {
    errors += "Miles field is blank!<br />";
    valid = false;
  } else {
    if(!numCheck(miles)) {
      errors += "Miles field is not a valid number.<br />";
      valid = false;
    }        
  }
  if(mpg == null || mpg == "") {
    errors += "Miles Per Gallon field is blank!<br />";
    valid = false;
  } else {
    if(!numCheck(mpg)) {
      errors += "Miles Per Gallon field is not a valid number.<br />";
      valid = false;
    }
  }
  if(commute == null || commute == "") {
    errors += "Commute field is blank!<br />";
    valid = false;
  } else {
    if(!numCheck(commute)) {
      errors += "Commute field is not a valid number.<br />";
      valid = false;
    }
  }
  if(childcare == null || childcare == "") {
    errors += "Child Care field is blank!<br />";
    valid = false;
  } else {
    if(!numCheck(childcare)) {
      errors += "Child Care field is not a valid number.<br />";
      valid = false;
    }
  }
  if(commutetime == null || commutetime == "") {
    errors += "Commute Time field is blank!<br />";
    valid = false;
  } else {
    if(!numCheck(commutetime)) {
      errors += "Commute Time field is not a valid number.<br />";
      valid = false;
    }
  }
  if(labor == null || labor == "") {
    errors += "Labor field is blank!<br />";
    valid = false;
  } else {
    if(!numCheck(labor)) {
      errors += "Labor field is not a valid number.<br />";
      valid = false;
    }
  }
  if(gas == null || gas == "") {
    errors += " Gas field is blank!<br />";
    valid = false;
  }   else {
    if(!numCheck(gas)) {
      errors += "Gas field is not a valid number.<br />";
      valid = false;
    }
  } 
  if (!valid) {
		$('#tuitionError').html(errors);
		$('#tuitionError').show();
  } else {		
		$('#tuitionError').hide();
    $('#tuitionError').html('');
    calcCost();
  }    
} 
    
function calcCost() {    
  var miles = Number($("#miles").val());
  var mpg = Number($("#mpg").val());
  var commute = Number($("#commute").val());
  var childcare = Number($("#childcare").val());
  var commutetime = Number($("#commutetime").val());
  var labor = Number($("#labor").val());
  var gas = Number($("#gas").val());
  var dexpenses = 0;
  labor = labor / 60;
  if((miles > 0) && (mpg > 0) && (gas > 0)){
    dexpenses = miles / mpg * gas;
  };
  var laborcost = labor * commutetime;
  var oneday = dexpenses + commute + childcare + laborcost;
  var semestercost = oneday * 15;
  var semestercost2 = oneday * 30;

  $('#drivingLabel').html("Your driving expense (assuming gas cost is $" + gas.toFixed(2) + " per gallon):");
  $('#drivingCost').html(dexpenses.toFixed(2));
  $('#otherCommute').html(commute.toFixed(2));
  $('#childCare').html(childcare.toFixed(2));
  $('#personalCommute').html(laborcost.toFixed(2));
  $('#dayCommute').html(oneday.toFixed(2));
  $('#oneClass').html('$' + semestercost.toFixed(2));
  $('#twoClasses').html('$' + semestercost2.toFixed(2));
}

function clearForm() {
	// Reset the main form
	$("#miles").val('0');
  $("#mpg").val('0');
  $("#commute").val('0.00');
  $("#childcare").val('0.00');
  $("#commutetime").val('0');
  $("#labor").val('0.00');
  $("#gas").val('0.00');	
	
	// Reset the results area
  $('#drivingLabel').html("Your driving expense (assuming gas cost is $0.00 per gallon):");
  $('#drivingCost').html('0.00');
  $('#otherCommute').html('0.00');
  $('#childCare').html('0.00');
  $('#personalCommute').html('0.00');
  $('#dayCommute').html('0.00');
  $('#oneClass').html('$0.00');
  $('#twoClasses').html('$0.00');
}