text: function main() {
      document.getElementById("clearBtn").onclick = clearFields;
      document.getElementById("preOpsAddBtn").onclick = preOpsRowAdd;
      document.getElementById("postOpsAddBtn").onclick = postOpsRowAdd;
      document.getElementById("saveBtn").onclick = saveSurgery;
  }
  
  //Clears fields from all textboxes on page
  function clearFields() {
      //Clear About Op Textarea
      document.getElementById("aboutOp").value = "";
  
      //Clear all textboxes
      var titleElements = document.getElementsByClassName("title");
      for (var i =0; i < titleElements.length; i++) {
          titleElements[i].value = "";
      }
  
      var secDetElements = document.getElementsByClassName("secDet");
      for (var i =0; i < secDetElements.length; i++) {
          secDetElements[i].value = "";
      }
  
      var addToCalElements = document.getElementsByClassName("addToCal");
      for (var i =0; i < addToCalElements.length; i++) {
          addToCalElements[i].value = "";
      }
  
      var recurrentElements = document.getElementsByClassName("recurrent");
      for (var i =0; i < recurrentElements.length; i++) {
          recurrentElements[i].value = "";
      }
  
      var freqElements = document.getElementsByClassName("freq");
      for (var i =0; i < freqElements.length; i++) {
          freqElements[i].value = "";
      }
  
      var datepick1Elements = document.getElementsByClassName("datepick1");
      for (var i =0; i < datepick1Elements.length; i++) {
          datepick1Elements[i].value = "";
      }
  
      var datepick2Elements = document.getElementsByClassName("datepick2");
      for (var i =0; i < datepick2Elements.length; i++) {
          datepick2Elements[i].value = "";
      }
  }
  
  //Adds row for PreOps Section
  function preOpsRowAdd() {
      var newSec = document.createElement('div');
      var num = document.getElementById("bSections").childNodes.length + 1;
      newSec.setAttribute("id", "bSec" + num);
  
      //Add Minus Button
      //var minusButton = document.createElement('button');
      //minusButton.appendChild(document.createTextNode("-"));
      //newSec.appendChild(minusButton);
  
      //Add Title TextArea
      var title = document.createElement('textarea');
      title.setAttribute("class", "title");
      title.setAttribute("rows", "1");
      title.setAttribute("cols", "50");
      title.setAttribute("placeholder", "Title");
      newSec.appendChild(title);
      newSec.appendChild(document.createElement('br'));
  
      //Add Section Details TextArea
      var secDet = document.createElement('textarea');
      secDet.setAttribute("class", "secDet");
      secDet.setAttribute("rows", "4");
      secDet.setAttribute("cols", "50");
      secDet.setAttribute("placeholder", "Enter Details about Operation here!");
      newSec.appendChild(secDet);
  
      newSec.appendChild(document.createElement('br'));
  
      //Add AddToCal Input Box
      var addToCal = document.createElement('input');
      addToCal.setAttribute("class", "addToCal");
      addToCal.setAttribute("type", "checkbox");
      newSec.appendChild(addToCal);
      addToCal.style.display = 'none';
      //newSec.appendChild(document.createTextNode("Add to Patient's Calendar"));
  
      //Add Recurrent Input Box
      var recurrent = document.createElement('input');
      recurrent.setAttribute("class", "recurrent");
      recurrent.setAttribute("type", "checkbox");
      newSec.appendChild(recurrent);
      recurrent.style.display = 'none';
      //newSec.appendChild(document.createTextNode("Recurrent"));
  
      //Add Frequency Selector
      var freq = document.createElement('select');
      freq.setAttribute("class", "freq");
  
      var dailyOption = document.createElement('option');
      dailyOption.appendChild(document.createTextNode("Daily"));
      dailyOption.setAttribute("value", "daily");
      freq.appendChild(dailyOption);
  
      var weeklyOption = document.createElement('option');
      weeklyOption.appendChild(document.createTextNode("Weekly"));
      weeklyOption.setAttribute("value", "weekly");
      freq.appendChild(weeklyOption);
  
      var monthlyOption = document.createElement('option');
      monthlyOption.appendChild(document.createTextNode("Monthly"));
      monthlyOption.setAttribute("value", "monthly");
      freq.appendChild(monthlyOption);
      freq.style.display = 'none';
      newSec.appendChild(freq);
  
      var days = document.createElement('input');
      days.setAttribute("id", "daysRelativeToOp")
      newSec.appendChild(days);
  
      var textnodeDays = document.createTextNode(" days ");
      newSec.appendChild(textnodeDays);
  
      //Add BeforeAfter Drop down
      var beforeAfter = document.createElement('select');
      beforeAfter.setAttribute("class", "beforeAfter");
      beforeAfter.setAttribute("id", "beforeAfterID")
  
      var beforeOption = document.createElement('option');
      beforeOption.appendChild(document.createTextNode("before"));
      beforeOption.setAttribute("value", "before");
      beforeAfter.appendChild(beforeOption);
  
      var afterOption = document.createElement('option');
      afterOption.appendChild(document.createTextNode("after"));
      afterOption.setAttribute("value", "after");
      beforeAfter.appendChild(afterOption);
  
      newSec.appendChild(beforeAfter);
  
      var textnodeDays2 = document.createTextNode(" surgery date");
      newSec.appendChild(textnodeDays2);
  
      var breakline = document.createElement('br');
      newSec.appendChild(breakline);
      var breakline2 = document.createElement('br');
      newSec.appendChild(breakline2);
  
      var durationLabel = document.createTextNode("Do that for ");
      newSec.appendChild(durationLabel);
  
      var duration = document.createElement('input');
      duration.setAttribute("id", "duration")
      newSec.appendChild(duration);
  
      var days2 = document.createTextNode(" days, once every ");
      newSec.appendChild(days2);
  
      var interval = document.createElement('input');
      interval.setAttribute("id", "interval")
      newSec.appendChild(interval);
  
      var days = document.createTextNode(" days at ");
      newSec.appendChild(days);
  
      var time = document.createElement("input");
      newSec.appendChild(time);
      //
      ////Add BeforeAfter Drop down
      //var select = document.createElement('select');
      //
      //var am = document.createElement('option');
      //am.setAttribute("value", "am");
      //am.innerHTML = "AM";
      //select.appendChild(am);
      //
      //var pm = document.createElement('option');
      //pm.setAttribute("value", "pm");
      //pm.innerHTML = "PM";
      //select.appendChild(pm);
      //newSec.appendChild(select);
  
      //Add BeforeAfter Drop down
      var timeSelect = document.createElement('select');
  
      var am = document.createElement('option');
      am.appendChild(document.createTextNode("AM"));
      am.setAttribute("value", "AM");
      timeSelect.appendChild(am);
  
      var pm = document.createElement('option');
      pm.appendChild(document.createTextNode("PM"));
      pm.setAttribute("value", "PM");
      timeSelect.appendChild(pm);
  
      newSec.appendChild(timeSelect);
  
  
  
      //Add Start Date Picker
      var startDatePicker = document.createElement('span');
      startDatePicker.appendChild(document.createTextNode("Start Date: "));
      var datepickerInput = document.createElement('input');
      datepickerInput.setAttribute("class", "datepick1");
      datepickerInput.setAttribute("type", "text");
      datepickerInput.setAttribute("id", "datepickerB" + num);
      startDatePicker.appendChild(datepickerInput);
      newSec.appendChild(startDatePicker);
      startDatePicker.style.display = 'none';
  
      //Add End Date Picker
      var endDatePicker = document.createElement('span');
      endDatePicker.appendChild(document.createTextNode("End Date: "));
      var datepickerInput2 = document.createElement('input');
      datepickerInput2.setAttribute("class", "datepick2");
      datepickerInput2.setAttribute("type", "text");
      datepickerInput2.setAttribute("id", "edatepickerB" + num);
      endDatePicker.appendChild(datepickerInput2);
      newSec.appendChild(endDatePicker);
      endDatePicker.style.display = 'none';
      newSec.setAttribute("class", "well");
  
      //Add newSec to beforeOp
      document.getElementById("bSections").appendChild(newSec);
  
      //Activate datepicker
      $("#datepickerB" + num).datepicker();
      $("#edatepickerB" + num).datepicker();
  }
  
  //Adds row for PostOps Section
  function postOpsRowAdd() {
      var newSec = document.createElement('div');
      var num = document.getElementById("aSections").childNodes.length + 1;
      newSec.setAttribute("id", "aSec" + num);
  
      //Add Minus Button
      //var minusButton = document.createElement('button');
      //minusButton.appendChild(document.createTextNode("-"));
      //newSec.appendChild(minusButton);
  
      //Add Title TextArea
      var title = document.createElement('textarea');
      title.setAttribute("class", "title");
      title.setAttribute("rows", "1");
      title.setAttribute("cols", "50");
      title.setAttribute("placeholder", "Title");
      newSec.appendChild(title);
      newSec.appendChild(document.createElement('br'));
  
      //Add Section Details TextArea
      var secDet = document.createElement('textarea');
      secDet.setAttribute("class", "secDet");
      secDet.setAttribute("rows", "4");
      secDet.setAttribute("cols", "50");
      secDet.setAttribute("placeholder", "Enter Details about Operation here!");
      newSec.appendChild(secDet);
  
      //Add AddToCal Input Box
      var addToCal = document.createElement('input');
      addToCal.setAttribute("class", "addToCal");
      addToCal.setAttribute("type", "checkbox");
      newSec.appendChild(addToCal);
      addToCal.style.display = 'none';
      //newSec.appendChild(document.createTextNode("Add to Patient's Calendar"));
  
      //Add Recurrent Input Box
      var recurrent = document.createElement('input');
      recurrent.setAttribute("class", "recurrent");
      recurrent.setAttribute("type", "checkbox");
      newSec.appendChild(recurrent);
      recurrent.style.display = 'none';
      //newSec.appendChild(document.createTextNode("Recurrent"));
  
      //Add Frequency Selector
      var freq = document.createElement('select');
      freq.setAttribute("class", "freq");
  
      var days = document.createElement('input');
      newSec.appendChild(days);
  
      var textnodeDays = document.createTextNode(" days ");
      newSec.appendChild(textnodeDays);
  
      //Add BeforeAfter Drop down
      var beforeAfter = document.createElement('select');
      beforeAfter.setAttribute("class", "beforeAfter");
  
      var beforeOption = document.createElement('option');
      beforeOption.appendChild(document.createTextNode("before"));
      beforeOption.setAttribute("value", "before");
      beforeAfter.appendChild(beforeOption);
  
      var afterOption = document.createElement('option');
      afterOption.appendChild(document.createTextNode("after"));
      afterOption.setAttribute("value", "after");
      beforeAfter.appendChild(afterOption);
  
      newSec.appendChild(beforeAfter);
  
      var textnodeDays2 = document.createTextNode(" surgery date");
      newSec.appendChild(textnodeDays2);
  
      var breakline = document.createElement('br');
      newSec.appendChild(breakline);
      var breakline2 = document.createElement('br');
      newSec.appendChild(breakline2);
  
      var durationLabel = document.createTextNode("Do that for ");
      newSec.appendChild(durationLabel);
  
      var duration = document.createElement('input');
      newSec.appendChild(duration);
  
      var days2 = document.createTextNode(" days, once every ");
      newSec.appendChild(days2);
  
      var interval = document.createElement('input');
      newSec.appendChild(interval);
  
      var days = document.createTextNode(" days ");
      newSec.appendChild(days);
  
      var dailyOption = document.createElement('option');
      dailyOption.appendChild(document.createTextNode("Daily"));
      dailyOption.setAttribute("value", "daily");
      freq.appendChild(dailyOption);
  
      var weeklyOption = document.createElement('option');
      weeklyOption.appendChild(document.createTextNode("Weekly"));
      weeklyOption.setAttribute("value", "weekly");
      freq.appendChild(weeklyOption);
  
      var monthlyOption = document.createElement('option');
      monthlyOption.appendChild(document.createTextNode("Monthly"));
      monthlyOption.setAttribute("value", "monthly");
      freq.appendChild(monthlyOption);
      freq.style.display = 'none';
      newSec.appendChild(freq);
  
      //Add Start Date Picker
      var startDatePicker = document.createElement('span');
      startDatePicker.appendChild(document.createTextNode("Start Date: "));
      var datepickerInput = document.createElement('input');
      datepickerInput.setAttribute("class", "datepick1");
      datepickerInput.setAttribute("type", "text");
      datepickerInput.setAttribute("id", "datepickerA" + num);
      startDatePicker.appendChild(datepickerInput);
      newSec.appendChild(startDatePicker);
      startDatePicker.style.display = 'none';
  
      //Add End Date Picker
      var endDatePicker = document.createElement('span');
      endDatePicker.appendChild(document.createTextNode("End Date: "));
      var datepickerInput2 = document.createElement('input');
      datepickerInput2.setAttribute("class", "datepick2");
      datepickerInput2.setAttribute("type", "text");
      datepickerInput2.setAttribute("id", "edatepickerA" + num);
      endDatePicker.appendChild(datepickerInput2);
      newSec.appendChild(endDatePicker);
      endDatePicker.style.display = 'none';
      newSec.setAttribute("class", "well");
  
      //Add newSec to beforeOp
      document.getElementById("aSections").appendChild(newSec);
  
      //Activate datepicker
      $("#datepickerA" + num).datepicker();
      $("#edatepickerA" + num).datepicker();
  }
  
  function calculateTime(time, pmOrAm) {
      switch (pmOrAm) {
          case ("AM"):
              return parseInt(time)*60;
          case ("PM"):
              return (12 + parseInt(time))*60;
      }
  }
  
  //Makes the request to save surgery for patient given hcnum
  function saveSurgery() {
      if (!document.getElementById("private").checked || document.getElementById("email").value == "") {
          alert("You can only customize for private use with a valid email address.")
      } else {
          var hcNum = "1";
  
      //Add data from preOpsDiv into preOps array
      preOps = [];
      var preOpsDiv = document.getElementById("bSections").childNodes;
      for (i=0; i < preOpsDiv.length; i++) {
          var childNodes = preOpsDiv[i].childNodes;
          var temp = {
              title: childNodes[0].value,
              details: childNodes[2].value,
              daysRelativeToOp: childNodes[7].value,
              beforeAfter: childNodes[9].value,
              duration: childNodes[14].value,
              interval: childNodes[16].value,
              time: calculateTime(childNodes[18].value, childNodes[19].value)
          }
          preOps.push(temp);
      }
  
      postOps = [];
      var postOpsDiv = document.getElementById("aSections").childNodes;
  
      
      var privateVar = false;
          if (document.getElementById("private").value == "Private") {
              privateVar = true;
          }
      var id = window.location.pathname;
      id = id.substring(13);
  
      var submissionData = {
          id: id,
          surgeryName: document.getElementById("surgName").value,
          email: document.getElementById("email").value,
          private: privateVar,
          aboutOp: document.getElementById("aboutOp").value,
          preOps: preOps,
          postOps: postOps,
          registryDate: Date.now,
          updatedDate: Date.now
      };
  
      $.ajax({
          type: 'POST',
          data: JSON.stringify(submissionData),
          contentType: 'application/json',
          url: '/surgery',
          success: function(data) {
              console.log('success');
              console.log(JSON.stringify(data));
              window.location.href = '../surgeryLookup';
          }
      });
      }
      
  
  } 
  
  window.onload = main;