function changeURL(tab) {
tab = tab.replace('include', '');
  switch(tab) {
    case 'loan':
      document.location = makeURL("loanCalc.jsp");
    break;
    case 'loanfull':
      document.location = makeURL("loanCalc.jsp") + "&mode=full";
    break;
    case 'affordability':
       document.location = makeURL("loanCalc.jsp") + "&affordability=yes";
    break;
    case 'affordabilityfull':
       document.location = makeURL("loanCalc.jsp") + "&mode=full" + "&affordability=yes";
    break;
    case 'incentives':
      document.location = makeURL("incentivesCalc.jsp");
    break;
    case 'incentivesfull':
      document.location = makeURL("incentivesCalc.jsp") + "&mode=full";
    break;
    case 'loanLease':
      document.location = makeURL("loanLeaseCalc.jsp") + "&flt=n_rval";
    break;
    case 'loanLeasefull':
      document.location = makeURL("loanLeaseCalc.jsp") + "&mode=full" + "&flt=n_rval";
    break;
  }
  return false;
}
function popURL(tab) {
	var newPopURL;
  switch(tab) {
    case 'loan':
      newPopURL = makeURL("loanCalc.jsp");
    break;
    case 'affordability':
       newPopURL = makeURL("loanCalc.jsp") + "&affordability=yes";
    break;
    case 'incentives':
      newPopURL = makeURL("incentivesCalc.jsp");
    break;
    case 'loanLease':
      newPopURL = makeURL("loanLeaseCalc.jsp") + "&flt=n_rval";
    break;
  }
  return "/go/advice/financing/calc/" + newPopURL;
}

function remExtraDecPlaces(numStr, newDecPlaces, addTrailingZerosFlag) {
  return setDecPlaces(numStr, newDecPlaces, addTrailingZerosFlag);
}

// Returns a value equal to numStr with newDecPlaces decimals at most.  Adds trailing zeros if flag is set to true
function setDecPlaces(numStr, newDecPlaces, addTrailingZerosFlag) {

  numStr = numStr.toString(); // make sure numStr is a string
  var decIndex = numStr.indexOf('.');
  var decPlaces = 0;
  if (decIndex != -1) {
    decPlaces = numStr.length - decIndex - 1;
  }
  
  if (newDecPlaces == 0 && decIndex != -1) {
    numStr = numStr.substring(0, decIndex); // strip decimal point
  } else if (decPlaces > newDecPlaces) {
    numStr = numStr.substring(0, (decIndex + newDecPlaces + 1)); // remove decimal places
  } else if (decPlaces < newDecPlaces && addTrailingZerosFlag) { // add trailing zeros if necessary
    if (decIndex == -1) {
      numStr = numStr + "."; // add a decimal point if there isn't one
    }
    for (; decPlaces < newDecPlaces; decPlaces++) { // add enough zeros such that decPlaces = newDecPlaces
      numStr = numStr + "0";
    }
  }

  return NaNToZero(numStr);
}

// Returns a string value of "0" if the argument is not a number or empty
function NaNToZero(value) {
  if (value == null || value == "" || isNaN(value)) {
    return "0";
  } else {
    return value;
  }
}

function fillZeros(theForm) {
  var e;
  for (var i = 0; i < theForm.elements.length - 1; i++) {
    e = theForm.elements[i];
    // modify hidden or text fields that are empty
    if ((e.value == null || e.value == "") &&
        (e.type == "text" || e.type == "hidden")) {
      theForm.elements[i].value = 0;
    }
  }
}

// Use this test string (which should clean up to 123.45678901):  1c,><2;:'"[]3\|=+-_)(*&^%$$#@!~`.sd 45678901
function cleanUpField(field) {
  stripIllegalChars(field);
  switch (field.name) { // strip decimals
    // no decimals
    case "vpLoan":  case "dpLoan":  case "tvLoan":  case "termLoan":
    case "vpLease": case "dpLease": case "tvLease": case "termLease": case "rvLease":
    case "cb_vprice":  case "cb_downpayment":  case "cb_tradein":  case "cb_rebate":  case "cb_term":
    case "lif_vprice": case "lif_downpayment": case "lif_tradein": case "lif_rebate": case "lif_term":
      field.value = setDecPlaces(field.value.toString(), 0);
      break;
    // 2 dec places
    case "mpLoan": case "mpLease":
      field.value = setDecPlaces(field.value.toString(), 2);
      break;
    // 3 dec places
    case "rateLoan": case "stPerLoan": case "stPerLease": case "cb_tax": case "lif_tax": case "cb_apr": case "lif_apr":
      field.value = setDecPlaces(field.value.toString(), 3);
      break;
    // 6 dec places
    case "rateLease":
      field.value = setDecPlaces(field.value.toString(), 6);
      break;
  }
}

function stripIllegalChars(field) {
//  alert(field.value + " " + field.name);
  var regExp = /[^0123456789.]+/;
  var val = field.value;
  while (val.match(regExp)) val = val.replace(regExp, ""); // remove all non-numbers (keep decimals)
  val = NaNToZero(val); // set blank fields to zero
  field.value = val;
}

/*
  Checks _field_ for errors. The type of error check is determined by _name_. x, y & z hold other necessary values
  Originally intended to be used with all calculators. It allows different pages to access the same error functionality
  even if they use different field names
*/
function checkError (field, name, x, y, z) {
  var errorFlag = false;
  // Set flag if the field name begins with "cb" or "lif"
  var incentivesFlag = (field.name.indexOf("cb") == 0 || field.name.indexOf("lif") == 0) ? true : false;

  switch (name) {

    case "vp": // Vehicle Price
      var vp = parseFloat(field.value);
      var dp = parseFloat(x);
      var tv = parseFloat(y);
      var rebate = parseFloat(NaNToZero(z)); // sometimes no rebate is passed
      if ( (vp < dp + tv + rebate) || (vp > 500000) ) {
        errorFlag = true;
        if (incentivesFlag) { // display rebate msg for incentives calc only
          alert("Vehicle Price must be greater than Down Payment + Trade-In Value + Rebate and less than $500,000.");
        } else {
          alert("Vehicle Price must be greater than Down Payment + Trade-In Value and less than $500,000.");
        }
      }
    break;

    case "dp": // Down Payment
      var dp = parseFloat(field.value);
      var vp = parseFloat(x);
      var tv = parseFloat(y);
      var rebate = parseFloat(NaNToZero(z)); // sometimes no rebate is passed
      if (vp < dp + tv + rebate) {
        errorFlag = true;
        if (incentivesFlag) { // display rebate msg for incentives calc only
          alert("Trade-In Value + Down Payment + Rebate must be less than Vehicle Price.");
        } else {
          alert("Trade-In Value + Down Payment must be less than Vehicle Price.");
        }
      }
    break;
      
    case "tv": // Trade-In Value
      var tv = parseFloat(field.value);
      var vp = parseFloat(x);
      var dp = parseFloat(y);
      var rebate = parseFloat(NaNToZero(z)); // sometimes no rebate is passed
      if (vp < dp + tv + rebate) {
        errorFlag = true;
        if (incentivesFlag) { // display rebate msg for incentives calc only
          alert("Trade-In Value + Down Payment + Rebate must be less than Vehicle Price.");
        } else {
          alert("Trade-In Value + Down Payment must be less than Vehicle Price.");
        }
      }
    break;

    case "stPer": // Sales Tax
      if (parseFloat(field.value) < 0 || parseFloat(field.value) > 100) {
        errorFlag = true;
        alert("Sales Tax must be between 0 and 100.");
      }
    break;
      
    case "rebate": // Rebate
      var rebate = parseFloat(field.value);
      var vp = parseFloat(x);
      var dp = parseFloat(y);
      var tv = parseFloat(z);
      if (vp < dp + tv + rebate) {
        errorFlag = true;
        alert("Trade-In Value + Down Payment + Rebate must be less than Vehicle Price.");
      }
      break;

    case "rate":
      if (parseFloat(field.value) < 0 || parseFloat(field.value) > 50) {
        errorFlag = true;
        alert("Interest Rate must be between 0 and 50.");
      }
    break;

    case "term":
      if (parseFloat(field.value) < 6 || parseFloat(field.value) > 96) {
        errorFlag = true;
        alert("Term of loan must be between 6 and 96." );
      }
    break;
  }
  
  if (errorFlag) {
  // Put cursor in field and highlight. Delay so that 2 fields aren't highlighted on tab
    var calcForm;
    if (document.LoanLeaseResultForm) calcForm = "document.LoanLeaseResultForm)";
    else calcForm = "document.calculator";
    setTimeout(calcForm + "." + field.name + ".select()", 10);
    setTimeout(calcForm + "." + field.name + ".focus()", 10);
    return true;
  } else {
    return false;
  }
}
