Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // initialize moment.js
  2. moment().format();
  3.  
  4. $(document).ready(function() {
  5.     for (m = 0; m <= 11; m++) {
  6.         $('#month').append('<option value="' + m + '">' + moment().month(m).format('MMMM') + '</option>' );
  7.     }
  8.     $('.results-wrapper').hide();
  9. });
  10.  
  11. // call validation when the submit button is clicked
  12. $('#preg-submit').on('click keyup', function(e) {
  13.     return validateInput(e);
  14. });
  15.  
  16. // also call validation when the enter key is clicked on any form element
  17. $('#month, #date, #year, #cycle-days').on('keyup', function(e) {
  18.  
  19.     // set a variable that handles the key event for all browsers
  20.     var keycode = (event.keyCode ? event.keyCode : event.which);
  21.  
  22.     // if the return key (ASCII number 13) is pressed, run the function.
  23.     if (keycode == '13') {
  24.         return validateInput(e);
  25.     }
  26. });
  27. // check the validity of the #cycle-days value before
  28. // we allow the data to be parsed and displayed
  29. function validateInput(e) {
  30.     // Get input values
  31.     var month = parseInt($('#month').val());
  32.     var date = parseInt($('#date').val());
  33.     var year = parseInt($('#year').val());
  34.     var cycle = parseInt($('#cycle-days').val());
  35.  
  36.     return checkMonthInput();
  37.  
  38.     // check that a month has been selected
  39.     function checkMonthInput() {
  40.         if (!isNaN(month)) {
  41.             // make sure the error message container is hidden
  42.             $('.month-selector small').addClass('hidden');
  43.  
  44.             // check the year
  45.             return checkYearInput();
  46.         } else {
  47.             // put the brakes on form submission
  48.             e.stopPropagation();
  49.  
  50.             // rehide the content if it's been unhidden
  51.             $('.results-wrapper').hide();
  52.  
  53.             // alert the user to the problem
  54.             $('.month-selector small').addClass('has-error').removeClass('hidden').html('Please select a month.');
  55.  
  56.             // put focus back on the field that is missing information
  57.             $('#month').focus();
  58.         }
  59.     }
  60.  
  61.     function checkYearInput() {
  62.         var yearLength = $('#year').val().length;
  63.         if (!isNaN(year) && yearLength === 4) {
  64.             // make sure the error message container is hidden
  65.             $('.year-input small').addClass('hidden');
  66.  
  67.             // check the date
  68.             return checkDateInput();
  69.         } else {
  70.             // put the brakes on form submission
  71.             e.stopPropagation();
  72.  
  73.             // rehide the content if it's been unhidden
  74.             $('.results-wrapper').hide();
  75.  
  76.             // alert the user to the problem
  77.             $('.year-input small').addClass('has-error').removeClass('hidden').text('Please enter the year in YYYY format. For example, this year is ' + moment().year());
  78.  
  79.             // put focus back on the field that is missing information
  80.             $('#year').focus();
  81.         }
  82.     }
  83.  
  84.     // check that the date is valid given the month and year
  85.     function checkDateInput() {
  86.         var dateLength = $('#date').val().length;
  87.  
  88.         if (!isNaN(date) && (dateLength == 1 || dateLength == 2)) {
  89.             console.log(moment(month).format('MMMM'));
  90.             if (month === 1) {
  91.                 if (moment(year).isLeapYear() === false) {
  92.                     if (date > 28) {
  93.                         // put the brakes on form submission
  94.                         e.stopPropagation();
  95.  
  96.                         // rehide the content if it's been unhidden
  97.                         $('.results-wrapper').hide();
  98.  
  99.                         // alert the user to the problem
  100.                         $('.date-input small').addClass('has-error').removeClass('hidden').text('There are 28 days in ' + $('#month > option:selected').text() + ' in ' + moment().year() + '. Please correct your input.');
  101.  
  102.                         // put focus back on the field that is missing information
  103.                         $('#date').focus();
  104.                     } else {
  105.                         // make sure the error message container is hidden
  106.                         $('.date-input small').addClass('hidden').removeClass('has-error');
  107.  
  108.                         // check the cycle length
  109.                         return checkCycleLength();
  110.                     }
  111.                 } else if (moment(year).isLeapYear() === true) {
  112.                     if (date > 29) {
  113.                         // put the brakes on form submission
  114.                         e.stopPropagation();
  115.  
  116.                         // rehide the content if it's been unhidden
  117.                         $('.results-wrapper').hide();
  118.  
  119.                         // alert the user to the problem
  120.                         $('.date-input small').addClass('has-error').removeClass('hidden').text('There are 29 days in February in ' + $('#month > option:selected').text() + ' in ' + moment().year() + '. Please correct your input.');
  121.  
  122.                         // put focus back on the field that is missing information
  123.                         $('#date').focus();
  124.                     } else {
  125.                         console.log(moment(month).format('MMMM'));
  126.                         // make sure the error message container is hidden
  127.                         $('.date-input small').addClass('hidden').removeClass('has-error');
  128.  
  129.                         // check the cycle length
  130.                         return checkCycleLength();
  131.                     }
  132.                 }
  133.             } else if (month === 3 || month === 5 || month === 8 || month === 10) {
  134.                 if (date > 30) {
  135.                     // put the brakes on form submission
  136.                     e.stopPropagation();
  137.  
  138.                     // rehide the content if it's been unhidden
  139.                     $('.results-wrapper').hide();
  140.  
  141.                     // alert the user to the problem
  142.                     $('.date-input small').addClass('has-error').removeClass('hidden').text('There are 30 days in ' + $('#month > option:selected').text() + '. Please correct your input.');
  143.  
  144.                     // put focus back on the field that is missing information
  145.                     $('#date').focus();
  146.                 } else {
  147.                     // make sure the error message container is hidden
  148.                     $('.date-input small').addClass('hidden').removeClass('has-error');
  149.  
  150.                     // check the cycle length
  151.                     return checkCycleLength();
  152.                 }
  153.             } else {
  154.                 if (date > 31) {
  155.                     // put the brakes on form submission
  156.                     e.stopPropagation();
  157.  
  158.                     // rehide the content if it's been unhidden
  159.                     $('.results-wrapper').hide();
  160.  
  161.                     // alert the user to the problem
  162.                     $('.date-input small').addClass('has-error').removeClass('hidden').text('There are 31 days in ' + $('#month > option:selected').text() + '. Please correct your input.');
  163.  
  164.                     // put focus back on the field that is missing information
  165.                     $('#date').focus();
  166.                 } else {
  167.                     // make sure the error message container is hidden
  168.                     $('.date-input small').addClass('hidden').removeClass('has-error');
  169.  
  170.                     // check the cycle length
  171.                     return checkCycleLength();
  172.                 }
  173.             }
  174.         } else {
  175.             // put the brakes on form submission
  176.             e.stopPropagation();
  177.  
  178.             // rehide the content if it's been unhidden
  179.             $('.results-wrapper').hide();
  180.  
  181.             // alert the user to the problem
  182.             $('.date-input small').addClass('has-error').removeClass('hidden').text('Please correct your date entry. For example, today\'s date is: ' + moment().date());
  183.  
  184.             // put focus back on the field that is missing information
  185.             $('#date').focus();
  186.         }
  187.     }
  188.  
  189.     // check to see if the cycle length has been entered
  190.     function checkCycleLength() {
  191.         if (cycle > 23 && cycle < 44) {
  192.             // make sure the error message container is hidden
  193.             $('.cycle-input small').removeClass('has-error has-warning').addClass('hidden');
  194.  
  195.             // check the cycle length
  196.             return outputData();
  197.         } else if (cycle <= 22 || cycle >= 45) {
  198.             // if the cycle interval is too short
  199.             if (cycle <= 22) {
  200.                 // alert the user and allow the calculation to continue
  201.                 $('.cycle-input small').addClass('has-warning').removeClass('hidden has-error').html('The cycle length you entered is too short to make an accurate prediction, but we\'ll do our best.');
  202.  
  203.                 // process the provided data
  204.                 return outputData();
  205.             } else if (cycle >= 45) {
  206.                 // alert the user and allow the calculation to continue
  207.                 $('.cycle-input small').addClass('has-warning').removeClass('hidden has-error').html('The cycle length you entered is too long to make an accurate prediction, but we\'ll do our best.');
  208.  
  209.                 // process the provided data
  210.                 return outputData();
  211.             }
  212.         } else {
  213.             // put the brakes on form submission
  214.             e.stopPropagation();
  215.  
  216.             // rehide the content if it's been unhidden
  217.             $('.results-wrapper').hide();
  218.  
  219.             // alert the user to the problem
  220.             $('.cycle-input small').addClass('has-error').removeClass('hidden has-warning').html('We can\'t make a prediction until you\'ve completed all the fields. Please enter your cycle length.');
  221.  
  222.             // put focus back on the field that is missing information
  223.             $('#cycle-days').focus();
  224.         }
  225.     }
  226.  
  227.     // give the user some data
  228.     function outputData() {
  229.         // set the date of the end of the user's last menstrual cycle
  230.         var menstrualDate = moment().set({
  231.             'date': date,
  232.             'month': month,
  233.             'year': year
  234.         });
  235.  
  236.         // TODO: Confirm that we only want to show the beginning of the conception period
  237.         // set the ovulation date, conceptionStart date, and conceptionEnd date
  238.         // so we can work with them and output data later.
  239.         var ovulation = moment(menstrualDate + 86400000 * cycle - 1123200000);
  240.         var conceptionStart = moment(ovulation - 432000000);
  241.         //var conceptionEnd = moment(ovulation + 432000000);
  242.  
  243.         // Set the likely beginning date of the range
  244.         // in which conception could occur
  245.         $('.conception-begin').text(moment(conceptionStart).format('dddd, MMMM Do, YYYY'));
  246.  
  247.         // Set the likely ending date of the range
  248.         // in which conception could occur
  249.         //$('.conception-end').text(moment(conceptionEnd).format('dddd, MMMM Do, YYYY'));
  250.  
  251.         // Set the date of the end as the first trimester.
  252.         // Per my source, the first trimester ends at
  253.         // 83 days after the identified date of conception
  254.         $('.first-trimester').text(moment(conceptionStart).add(83, 'days').format('dddd, MMMM Do, YYYY'));
  255.  
  256.         // Set the date of the end as the first trimester.
  257.         // Per my source, the first trimester ends at
  258.         // 83 days after the identified date of conception
  259.         $('.second-trimester').text(moment(conceptionStart).add(195, 'days').format('dddd, MMMM Do, YYYY'));
  260.  
  261.         // Set the likely date of birth.
  262.         // Per the internet (and if it's on the internet,
  263.         // it must be true), the human gestational period
  264.         // averages 280 days.
  265.         $('.due-date').text(moment(conceptionStart).add(280, 'days').format('dddd, MMMM Do, YYYY'));
  266.  
  267.         //unhide the container
  268.         $('.results-wrapper').fadeIn(600);
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement