﻿// general purpose function to see if an input value has been entered at all

function isEmpty(inputStr) {

        if (inputStr == "" || inputStr == null) {

                return true

        }

        return false

}



// general purpose function to see if a suspected numeric input

// is a positive integer

function isNumber(inputStr) {

        for (var i = 0; i < inputStr.length; i++) {

                var oneChar = inputStr.charAt(i)

                if (oneChar < "0" || oneChar > "9") {

                        return false

                }

        }

        return true

}



// function to determine if value is in acceptable range for this application

function inRangeDay(inputStr) {

        num = parseInt(inputStr)

        if (num < 1 || num > 31) {

                return false

        }

        return true

}



// function to determine if value is in acceptable range for this application

function inRangeYear(inputStr) {

        num = parseInt(inputStr)

        if (num < 1900 || num > 3000) {

                return false

        }

        return true

}



// Master value validator routine for day

function isValidDay(inputStr) {

        if (isEmpty(inputStr)) {

                alert("Please enter the number of the day into the day field before clicking the Calculate button.")

                return false

        } else {

                if (!isNumber(inputStr)) {

                        alert("Please make sure the day is a number only.")

                        return false

                } else {

                        if (!inRangeDay(inputStr)) {

                                alert("Please enter a valid day.")

                                return false

                        }

                }

        }

        return true

}



// Master value validator routine for year

function isValidYear(inputStr) {

        if (isEmpty(inputStr)) {

                alert("Please enter the year into the year field before clicking the Calculate button.")

                return false

        } else {

                if (!isNumber(inputStr)) {

                        alert("Please make sure the year is a number only.")

                        return false

                } else {

                        if (!inRangeYear(inputStr)) {

                                alert("Please enter a valid year.")

                                return false

                        }

                }

        }

        return true

}



function makeArray(n) {

        this.length = n

        for (var i=1; i <= n; i++)

                this[i] = null

                return this

}





var maxday = new makeArray(12)

maxday[1] = 31

maxday[2] = 28

maxday[3] = 31

maxday[4] = 30

maxday[5] = 31

maxday[6] = 30

maxday[7] = 31

maxday[8] = 31

maxday[9] = 30

maxday[10] = 31

maxday[11] = 30

maxday[12] = 31



var monthname = new makeArray(12)

monthname[1] = "January"

monthname[2] = "February"

monthname[3] = "March"

monthname[4] = "April"

monthname[5] = "May"

monthname[6] = "June"

monthname[7] = "July"

monthname[8] = "August"

monthname[9] = "September"

monthname[10] = "October"

monthname[11] = "November"

monthname[12] = "December"

 

var adddays = new makeArray(7)

adddays[1] = 0

adddays[2] = 12



adddays[4] = 11

adddays[5] = 13

adddays[6] = 14

adddays[7] = 15

adddays[8] = 16

adddays[9] = 21

adddays[10] = 22

adddays[11] = 24

adddays[12] = 28

adddays[13] = 280




// Calculate the date string

function calcNewDate(month,day,year,adddays,cycle) {

        newday = eval(day) + adddays + cycle

        newmonth = month + 1

        newyear = eval(year)

        var max



        for (var i = 0; i < 12; i++) {

          if (newmonth == 2 && (newyear % 4) == 0) {

            max = 29

          } else

            max = maxday[newmonth]



          if (newday > max) {

            newday = newday - max

            newmonth = newmonth + 1

            if (newmonth > 12) {

              newyear = newyear + 1

              newmonth = 1

            }

          }

          else

            break

        }



        var datestring = monthname[newmonth] + " " + newday + ", " + newyear

        return datestring

}



// Get the date entered and calculate the rest of the dates

function calc(form) {



        day = form.day.value

        year = form.year.value

        monthnum = form.month.selectedIndex
        
        cycledays = form.cdays.selectedIndex + 22 - 28



        if (isValidDay(day)) {

          if (isValidYear(year)){



        form.conception.value = calcNewDate(monthnum,day,year,adddays[1],0)

            form.beginrisk.value = calcNewDate(monthnum,day,year,adddays[2],cycledays)

            

            form.week4.value = calcNewDate(monthnum,day,year,adddays[4],cycledays)

            form.weekeight.value = calcNewDate(monthnum,day,year,adddays[5],cycledays)

            form.weektwelve.value = calcNewDate(monthnum,day,year,adddays[6],cycledays)

            form.weeksixteen.value = calcNewDate(monthnum,day,year,adddays[7],cycledays)

            form.weektwenty.value = calcNewDate(monthnum,day,year,adddays[8],cycledays)

            form.weektwentyfour.value = calcNewDate(monthnum,day,year,adddays[9],cycledays)

            form.week27.value = calcNewDate(monthnum,day,year,adddays[10],cycledays)

            form.weekthirtytwo.value = calcNewDate(monthnum,day,year,adddays[11],cycledays)

            form.weekthirtyseven.value = calcNewDate(monthnum,day,year,adddays[12],cycledays)

            form.duedate.value = calcNewDate(monthnum,day,year,adddays[13],cycledays)

            



          }

        }



}





