$(document).ready(function(){
  $.jtabber({
    mainLinkTag: "#optionsNav a",
    activeLinkClass: "selected",
    hiddenContentClass: "hiddencontent",
    showDefaultTab: 1,
    showErrors: false,
    effect: 'slide',
    effectSpeed: 'medium'
  });

  checkStepFour();
});

function addCost(optionId,optionCost) {
  //Check to make sure it's inactive
  if ($("#selectedOptions #option-" + optionId + "-checked").hasClass("hidden")) {
    //Total up new cost
    var currCost = $("#totalCost span#currentCost").text();
    currCost = parseInt(currCost) + parseInt(optionCost);
    $("#totalCost span#currentCost").html(currCost);

    //Display Option on sidebar
    $("#selectedOptions #option-" + optionId + "-checked").removeClass("hidden");
  }
}

function subtractCost(optionId,optionCost) {
  //Check to make sure it's active
  if (!$("#selectedOptions #option-" + optionId + "-checked").hasClass("hidden")) {
    //Total up new cost
    var currCost = $("#totalCost span#currentCost").text();
    currCost = parseInt(currCost) - parseInt(optionCost);
    $("#totalCost span#currentCost").html(currCost);

    //Hide option from sidebar
    $("#selectedOptions #option-" + optionId + "-checked").addClass("hidden");
  }
}

function countChecked(optionId,optionCost,stepId,maxNum) {
  if($("input[name='step" + stepId + "-options[]']:checked").length > maxNum) {
    $("input[id='option-" + optionId + "']").attr("checked",false);
    subtractCost(optionId,optionCost);
    alert("We're sorry but only " + maxNum + " options can be selected for this step.");
  }
}

function checkStepFour() {
  if($("input[name='step3-options[]']:checked").length > 0) {
    //Hide Top Two
    $("input[id='option-14']").attr("checked",false);
    subtractCost(14,0);
    $("input[id='option-14']").attr("disabled","disabled");
    $("#option-14-container").addClass("hidden");

    $("input[id='option-15']").attr("checked",false);
    subtractCost(15,0);
    $("input[id='option-15']").attr("disabled","disabled");
    $("#option-15-container").addClass("hidden");

    //Show Bottom Two
    $("input[id='option-16']").attr("disabled",false);
    $("#option-16-container").removeClass("hidden");

    $("input[id='option-17']").attr("disabled",false);
    $("#option-17-container").removeClass("hidden");
  }else{
    //Show Top Two
    $("input[id='option-14']").attr("disabled",false);
    $("#option-14-container").removeClass("hidden");

    $("input[id='option-15']").attr("disabled",false);
    $("#option-15-container").removeClass("hidden");

    //Hide Bottom Two
    $("input[id='option-16']").attr("checked",false);
    subtractCost(16,65);
    $("input[id='option-16']").attr("disabled","disabled");
    $("#option-16-container").addClass("hidden");

    $("input[id='option-17']").attr("checked",false);
    subtractCost(17,65);
    $("input[id='option-17']").attr("disabled","disabled");
    $("#option-17-container").addClass("hidden");
  }
}

function customizationValidation() {
  var hasErrors = false;
  var errorMessage = 'Please fix the following errors:\n\n';
  var step3 = $("input[name='step3-options[]']:checked").length;

  //Step 1
  if($("input[name='step1-options[]']:checked").length == 0) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please choose an enclosure.\n';
  }

  //Step 2
  if($("input[name='step2-options[]']:checked").length == 0) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please choose a touch screen.\n';
  }

  //Step 3
  if(step3 > 2) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose a maximum of two exspansion cards.\n';
  }

  //Step 4
  if($("input[id='option-14']:checked").length == 0 && $("input[id='option-15']:checked").length == 0 && $("input[id='option-16']:checked").length == 0 && $("input[id='option-17']:checked").length == 0) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please choose a wiring connector.\n';
  }
  if(step3 > 0 && $("input[id='option-14']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + '"Lateral sleeve housing, 1 lock, 24 position" is not available for this configuration.\n';
  }
  if(step3 > 0 && $("input[id='option-15']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + '"Straight sleeve housing, 1 lock, 24 position" is not available for this configuration.\n';
  }
  if(step3 == 0 && $("input[id='option-16']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + '"Lateral sleeve housing, 1 lock, 32 position" is not available for this configuration.\n';
  }
  if(step3 == 0 && $("input[id='option-17']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + '"Straight sleeve housing, 1 lock, 32 position" is not available for this configuration.\n';
  }
  if($("input[id='option-14']:checked").length == 1 && $("input[id='option-15']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose one wiring connector.\n';
  }
  if($("input[id='option-16']:checked").length == 1 && $("input[id='option-17']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose one wiring connector.\n';
  }

  //Step 5
  if($("input[name='step5-options[0][]']:checked").length == 0) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please choose a "Control Power On" button.\n';
  }
  if($("input[name='step5-options[1][]']:checked").length == 0) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please choose a "Control Power Off" button.\n';
  }
  if($("input[name='step5-options[2][]']:checked").length == 0) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please choose a "Emergancy Stop" button.\n';
  }
  if($("input[id='option-24']:checked").length == 1 && $("input[id='option-30']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose one "Control Power On" button.\n';
  }
  if($("input[id='option-25']:checked").length == 1 && $("input[id='option-31']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose one "Control Power Off" button.\n';
  }
  if($("input[id='option-26']:checked").length == 1 && $("input[id='option-32']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose one "Emergancy Stop" button.\n';
  }
  if($("input[id='option-27']:checked").length == 1 && $("input[id='option-33']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose one green indicator light.\n';
  }
  if($("input[id='option-28']:checked").length == 1 && $("input[id='option-34']:checked").length == 1) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose one red indicator light.\n';
  }

  //Step 6
  if($("input[name='step6-options[]']:checked").length > 2) {
    hasErrors = true;
    errorMessage = errorMessage + 'Please only choose a maximum of two additional contactors.\n';
  }

  if(hasErrors == true) {
    alert(errorMessage);
  }else{
    customizationForm.submit();
  }
}