
$(function(){
  var sliderTime = 300;

  // let's attach some event handlers
  $("div.hidden").hide();
  
  $("#youNo").click(function(){
    $("#nonPatientSection").slideDown(sliderTime);
  });

  $("#youYes").click(function(){
    $("#nonPatientSection").slideUp(sliderTime);
  });
  
  $("#contactMethod").change(function(){
    if ($(this).val() == "phone"){
      $("#emailSection").slideUp(sliderTime);
      $("#phoneSection").slideDown(sliderTime)
    }

    if ($(this).val() == "email"){
      $("#phoneSection").slideUp(sliderTime)
      $("#emailSection").slideDown(sliderTime);
    }

    if ($(this).val() == "both"){
      $("#phoneSection").slideDown(sliderTime)
      $("#emailSection").slideDown(sliderTime);
    }

    if ($(this).val() == ""){
      $("#phoneSection").slideUp(sliderTime)
      $("#emailSection").slideUp(sliderTime);
    }
  });
	
  $("#doctorName").click(function(){
    if ($(this).val() == "Name") $(this).val("");
  });

  $("#serviceName").click(function(){
    if ($(this).val() == "Name") $(this).val("");
  });
  
  $("#referralYes").click(function(){
    $("#referralInfo").slideDown(sliderTime)
  });

  $("#referralNo").click(function(){
    $("#referralInfo").slideUp(sliderTime)
  });
  
  $("#doctorName").focus(function(){
    $(this).val("").removeClass("dim");
  });

  $("#serviceName").focus(function(){
    $(this).val("").removeClass("dim");
  });
  
  // select all checkboxes
  $("span#selectAll").click(function(){
    $("#appointments input").each(function(){
      this.checked = "checked";
      SetAppointmentLinks();
    });
  });

  // clear all checkboxes
  $("span#clearAll").click(function(){
    $("#appointments input").each(function(){
      this.checked = "";
      SetAppointmentLinks();
    });
  });
  
  // appointment checkboxes
  $("table#appointments input").click(function(){
    SetAppointmentLinks();
  });

  // show questionnaires
  $("#appointmentReason").change(function(){
    $(".reasonQuestionnaire").slideUp(sliderTime); // clear previous questionnaires
    if ($(this).val() != ""){
      $("#" + $(this).val()).slideDown(sliderTime); // show questionnaire. assumes id of questionnaire = value of option chosen
    }
  });
  
  $("#appointmentType").change(function(){
    if ($(this).val() == "21"){
        HideAllClinics();
        ResetAllClinics();
      $("#endocrinologyItems").attr("validate", "true");
      $("#endocrinologySection").slideDown(sliderTime);
    }
	
	if ($(this).val() == "12"){
        HideAllClinics();
        ResetAllClinics();
    }
	
    if ($(this).val() == "104"){
        HideAllClinics();
        ResetAllClinics();
      $("#pulmonologyItems").attr("validate", "true");
      $("#pulmonologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "60"){
        HideAllClinics();
        ResetAllClinics();
      $("#allergyItems").attr("validate", "true");
      $("#allergySection").slideDown(sliderTime);
    }
    if ($(this).val() == "16"){
        HideAllClinics();
        ResetAllClinics();
      $("#audiologyItems").attr("validate", "true");
      $("#audiologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "143"){
        HideAllClinics();
        ResetAllClinics();
      $("#bmtItems").attr("validate", "true");
      $("#bmtSection").slideDown(sliderTime);
    }

    if ($(this).val() == "18"){
        HideAllClinics();
        ResetAllClinics();
    }

    if ($(this).val() == "73"){
        HideAllClinics();
        ResetAllClinics();
      $("#cancerItems").attr("validate", "true");
      $("#cancerSection").slideDown(sliderTime);
    }

    if ($(this).val() == "17"){
        HideAllClinics();
        ResetAllClinics();
    }

    if ($(this).val() == "19"){
        HideAllClinics();
        ResetAllClinics();
      $("#criticalcareSection").attr("validate", "true");
      $("#criticalcareSection").slideDown(sliderTime);
    }

    if ($(this).val() == "41"){
        HideAllClinics();
        ResetAllClinics();
      $("#dermatologyItems").attr("validate", "true");
      $("#dermatologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "102"){
        HideAllClinics();
    }

    if ($(this).val() == "56"){
        HideAllClinics();
        ResetAllClinics();
      $("#gastroenterologyItems").attr("validate", "true");
      $("#gastroenterologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "87"){
        HideAllClinics();
        ResetAllClinics();
      $("#heartItems").attr("validate", "true");
      $("#heartSection").slideDown(sliderTime);
    }

    if ($(this).val() == "124"){
        HideAllClinics();
        ResetAllClinics();
      $("#hematologyItems").attr("validate", "true");
      $("#hematologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "119"){
        HideAllClinics();
        ResetAllClinics();
      $("#kidneydiseaseItems").attr("validate", "true");
      $("#kidneydiseaseSection").slideDown(sliderTime);
    }

    if ($(this).val() == "130"){
        HideAllClinics();
        ResetAllClinics();
      $("#laboratorymedicineItems").attr("validate", "true");
      $("#laboratorymedicineSection").slideDown(sliderTime);
    }

    if ($(this).val() == "145"){
        HideAllClinics();
        ResetAllClinics();
      $("#medicalgeneticsItems").attr("validate", "true");
      $("#medicalgeneticsSection").slideDown(sliderTime);
    }

    if ($(this).val() == "5"){
        HideAllClinics();
        ResetAllClinics();
      $("#neurologyItems").attr("validate", "true");
      $("#neurologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "155"){
        HideAllClinics();
        ResetAllClinics();
    }

    if ($(this).val() == "99"){
        HideAllClinics();
        ResetAllClinics();
    }

    if ($(this).val() == "1"){
        HideAllClinics();
        ResetAllClinics();
      $("#organtransplantItems").attr("validate", "true");
      $("#organtransplantSection").slideDown(sliderTime);
    }

    if ($(this).val() == "26"){
        HideAllClinics();
        ResetAllClinics();
      $("#orthopedicsItems").attr("validate", "true");
      $("#orthopedicsSection").slideDown(sliderTime);
    }

    if ($(this).val() == "93"){
        HideAllClinics();
        ResetAllClinics();
    }

    if ($(this).val() == "64"){
        HideAllClinics();
        ResetAllClinics();
      $("#plasticsurgeryItems").attr("validate", "true");
      $("#plasticsurgerySection").slideDown(sliderTime);
    }

    if ($(this).val() == "81"){
        HideAllClinics();
        ResetAllClinics();
      $("#primarycareItems").attr("validate", "true");
      $("#primarycareSection").slideDown(sliderTime);
    }

    if ($(this).val() == "14"){
        HideAllClinics();
        ResetAllClinics();
      $("#psychiatryItems").attr("validate", "true");
      $("#psychiatrySection").slideDown(sliderTime);
    }

    if ($(this).val() == "111"){
        HideAllClinics();
        ResetAllClinics();
      $("#radiationoncologyItems").attr("validate", "true");
      $("#radiationoncologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "68"){
        HideAllClinics();
        ResetAllClinics();
      $("#radiologyItems").attr("validate", "true");
      $("#radiologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "44"){
        HideAllClinics();
        ResetAllClinics();
      $("#rehabilitationItems").attr("validate", "true");
      $("#rehabilitationSection").slideDown(sliderTime);
    }

    if ($(this).val() == "66"){
        HideAllClinics();
        ResetAllClinics();
    }

    if ($(this).val() == "32"){
        HideAllClinics();
        ResetAllClinics();
      $("#surgeryItems").attr("validate", "true");
      $("#surgerySection").slideDown(sliderTime);
    }

    if ($(this).val() == "94"){
        HideAllClinics();
        ResetAllClinics();
      $("#teenhealthItems").attr("validate", "true");
      $("#teenhealthSection").slideDown(sliderTime);
    }

    if ($(this).val() == "34"){
        HideAllClinics();
        ResetAllClinics();
      $("#urologyItems").attr("validate", "true");
      $("#urologySection").slideDown(sliderTime);
    }

    if ($(this).val() == "67"){
        HideAllClinics();
        ResetAllClinics();
    }

  });
 
  $("#birthDate").blur(function(){
    if ($(this).val() == "__/__/____"){
      $(this).next().filter("span.validationError").html("Field cannot be empty");
      $(this).val("");
      retVal = false;
    }
  });

  // validate onblur
  $("form input[validate!=false], form select[validate!=false], form textarea[validate!=false]").each(function(){
    $(this).blur(function(){
      if ($.Form.Validate.IsEmpty($(this))){
        $(this).next().filter(".validationError").remove();
        var label = $(this).prev().filter("label").text();
        var errorMessage = $("<div class=\"validationError\">" + label + " field cannot be empty</div>").hide();
        errorMessage.insertAfter($(this)).slideDown(300);
        retVal = false;
      }
      else{
        $(this).next().filter(".validationError").remove();
      }
    });
  });
  
  $("#email").blur(function(){
    try
    {
      if (!$.Form.Validate.IsEmpty($(this))){
        if(!$.Form.Validate.Email($("#email").val())){
          $(this).next().filter(".validationError").remove();
          var label = $(this).prev().filter("label").text();
          var errorMessage = $("<div class=\"validationError\">We are unable to recognize this email address. Please format like yourname@example.com</div>").hide();
          errorMessage.insertAfter($(this)).slideDown(300);

          retVal = false;
        }
        else{
          $(this).next().filter(".validationError").remove();
        }
      }
    }
    catch(e){}
  });

  // validate form on submit
  //$("button#bttnSend").click(function(){
  $("form#patientInfo").submit(function(){
    var retVal = true;
    $("form#patientInfo input[validate!=false], form#patientInfo select[validate!=false], form#patientInfo textarea[validate!=false]").each(function(){
      if ($.Form.Validate.IsEmpty($(this))){
        $(this).next().filter(".validationError").remove();
        var label = $(this).prev().filter("label").text();
        var errorMessage = $("<div class=\"validationError\">" + label + " field cannot be empty</div>").hide();
        errorMessage.insertAfter($(this)).slideDown(300);
        retVal = false;
      }
      else{
        $(this).next().filter(".validationError").remove();
      }
    });
    //if (!IsAppointmentTimeValid()) retVal = false;
    //if (!IsRadioValid()) retVal = false;
    return retVal;
  });
  // end events
  
  // initialize year dropdown
  var date = new Date();
  var year = date.getFullYear();
  for (var i = 1905; i < year; i++){
    $('<option value="' + i + '">' + i + '</option>').appendTo("select#dobYear");
  }
  $("select#dobYear option:first").attr("selected", "selected");

  // initialize select all/clear all states
  SetAppointmentLinks();

  function SetAppointmentLinks(){
    // if nothing is checked, hide "clear all"
    if ($("table#appointments input:checked").length == 0) $("span#clearAll").hide();
    
    // if all boxes are checked, hide "select all"
    if ($("table#appointments input:checked").length == 10) $("span#selectAll").hide();

    // reset clear all
    if ($("table#appointments input:checked").length > 0) $("span#clearAll").show();

    // reset select all
    if ($("table#appointments input:checked").length < 10) $("span#selectAll").show();
  }  
  // validate appointment time checkboxes -- at least one checked
  function IsAppointmentTimeValid(){
     $("table#appointments").next().filter("span.validationError").html("");
    var retVal = false;
    $("table#appointments input").each(function(){
      if (this.checked){
        retVal = true;
      }
    });
    
    if (!retVal){
     $("table#appointments").next().filter("span.validationError").html("Please select an appointment time");
    }
    return retVal;
  }
  
  function IsRadioValid(){
    var retVal = false;
    $("form div.radios").each(function(){
      if ($(this).children().filter("input")[0].checked || $(this).children().filter("input")[1].checked){
        retVal = true;
      }
      else {
       $(this).next().filter("span.validationError").html("Please select one option");
      }
    });
    return retVal;
  }

  function HideAllClinics(){
      $("#allergySection").slideUp(sliderTime);
      $("#audiologySection").slideUp(sliderTime);
      $("#endocrinologySection").slideUp(sliderTime);
      $("#pulmonologySection").slideUp(sliderTime);
      $("#bmtSection").slideUp(sliderTime);
      $("#cancerSection").slideUp(sliderTime);
      $("#criticalcareSection").slideUp(sliderTime);
      $("#dermatologySection").slideUp(sliderTime);
      $("#gastroenterologySection").slideUp(sliderTime);
      $("#heartSection").slideUp(sliderTime);
      $("#hematologySection").slideUp(sliderTime);
      $("#kidneydiseaseSection").slideUp(sliderTime);
      $("#laboratorymedicineSection").slideUp(sliderTime);
      $("#medicalgeneticsSection").slideUp(sliderTime);
      $("#neurologySection").slideUp(sliderTime);
      $("#organtransplantSection").slideUp(sliderTime);
      $("#orthopedicsSection").slideUp(sliderTime);
      $("#plasticsurgerySection").slideUp(sliderTime);
      $("#primarycareSection").slideUp(sliderTime);
      $("#psychiatrySection").slideUp(sliderTime);
      $("#radiationoncologySection").slideUp(sliderTime);
      $("#radiologySection").slideUp(sliderTime);
      $("#rehabilitationSection").slideUp(sliderTime);
      $("#surgerySection").slideUp(sliderTime);
      $("#teenhealthSection").slideUp(sliderTime);
      $("#urologySection").slideUp(sliderTime);

      $("#allergyItems").attr("validate", "false");
      $("#audiologyItems").attr("validate", "false");
      $("#endocrinologyItems").attr("validate", "false");
      $("#pulmonologyItems").attr("validate", "false");
      $("#bmtItems").attr("validate", "false");
      $("#cancerItems").attr("validate", "false");
      $("#criticalcareSection").attr("validate", "false");
      $("#dermatologyItems").attr("validate", "false");
      $("#gastroenterologyItems").attr("validate", "false");
      $("#heartItems").attr("validate", "false");
      $("#hematologyItems").attr("validate", "false");
      $("#kidneydiseaseItems").attr("validate", "false");
      $("#laboratorymedicineItems").attr("validate", "false");
      $("#medicalgeneticsItems").attr("validate", "false");
      $("#neurologyItems").attr("validate", "false");
      $("#organtransplantItems").attr("validate", "false");
      $("#orthopedicsItems").attr("validate", "false");
      $("#plasticsurgeryItems").attr("validate", "false");
      $("#primarycareItems").attr("validate", "false");
      $("#psychiatryItems").attr("validate", "false");
      $("#radiationoncologyItems").attr("validate", "false");
      $("#radiologyItems").attr("validate", "false");
      $("#rehabilitationItems").attr("validate", "false");
      $("#surgeryItems").attr("validate", "false");
      $("#teenhealthItems").attr("validate", "false");
      $("#urologyItems").attr("validate", "false");
  }

  function ResetAllClinics(){
      $("#allergyItems").val("");
      $("#audiologyItems").val("");
      $("#endocrinologyItems").val("");
      $("#pulmonologyItems").val("");
      $("#bmtItems").val("");
      $("#cancerItems").val("");
      $("#criticalcareItems").val("");
      $("#dermatologyItems").val("");
      $("#gastroenterologyItems").val("");
      $("#heartItems").val("");
      $("#hematologyItems").val("");
      $("#kidneydiseaseItems").val("");
      $("#laboratorymedicineItems").val("");
      $("#medicalgeneticsItems").val("");
      $("#neurologyItems").val("");
      $("#organtransplantItems").val("");
      $("#orthopedicsItems").val("");
      $("#plasticsurgeryItems").val("");
      $("#primarycareItems").val("");
      $("#psychiatryItems").val("");
      $("#radiationoncologyItems").val("");
      $("#radiologyItems").val("");
      $("#rehabilitationItems").val("");
      $("#surgeryItems").val("");
      $("#teenhealthItems").val("");
      $("#urologyItems").val("");
  }

  function IsValidDate(obj, mm, dd, yy){
    if (isNaN(mm) || mm == "mm"){
      $(obj).next().filter("span.validationError").html("Month must be a number (1 through 12)");
      return false;
    }
    if (mm < 1 || mm > 12){
      $(obj).next().filter("span.validationError").html("Month must be a number (1 through 12)");
      return false;
    }
    if (isNaN(dd) || dd == "dd"){
      $(obj).next().filter("span.validationError").html("Date must be a number (1 through 31)");
      return false;
    }
    if (dd < 1 || dd > 31){
      $(obj).next().filter("span.validationError").html("Date must be a number (1 through 31)");
      return false;
    }
    if (isNaN(yy) || yy == "yyyy"){
      $(obj).next().filter("span.validationError").html("Year must be a number (four digits)");
      return false;
    }
    var date = new Date();
    var year = date.getFullYear();
    if (yy > year){
      $(obj).next().filter("span.validationError").html("Invalid year");
      return false;
    }
  }
});

