﻿function Utilities() {
    var OneDay = 1000 * 60 * 60 * 24;
    this.monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    this.subtractDates = function(firstDate, secondDate) {
        return Math.ceil((new Date(firstDate).getTime()
        - new Date(secondDate).getTime()) / (OneDay));
    }

    this.addDays = function (dateToAdd, amountInDays) {
        return new Date(dateToAdd.valueOf() + OneDay * amountInDays);
    }

    this.daysInMonth = function(year, month) {
        
        var m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if (month != 2)
            return m[month - 1];
        if (year % 4 != 0)
            return m[1];
        if (year % 100 == 0 && year % 400 != 0)
            return m[1]
        return m[1] + 1;
    }

    this.isDate = function(dateStr) {
        var ok;
        try {
            var d = Date.parse(dateStr);
            return true;
        } catch (x) {
            return false;
        }
    }

    this.getOrdinal = function ordinal(num, options) {
        var options = options || {}; // setup the options
        var sScript = options.sScript; // get subscript if needed

        var mod1 = num % 100; // get the divided "remainder" of 100
        var mod2 = num % 10; // get the divided "remainder" of 10
        var ord; // ste the ordinal variable

        if ((mod1 - mod2) == 10) { // capture 10
            ord = "th"; // set the oridnal to th as in: 10th
        } else {// for everything else
            switch (mod2) {  // check the remainder of the 10th place
                case 1: // if 1 as in 1st
                    ord = "st"; // set the ordinal
                    break;
                case 2: // if 2 as in 2nd
                    ord = "nd"; // set the ordinal
                    break;
                case 3: // if 3 as in 3rd
                    ord = "rd"; // set the ordinal
                    break;
                default: // for everything else
                    ord = "th"; // set the ordinal
                    break;
            }
        }
        switch (sScript) {
            case "sub":
                return num + "<sub>" + ord + "<\/sub>"; // put the ordinal in the HTML sub script
                break;
            case "sup":
                return num + "<sup>" + ord + "<\/sup>"; // put the ordinal in the HTML super script
                break;
            default:
                return num + ord;
                break;
        }
    }
    
    this.parseTime = function(timeString){
     if (timeString == '') return null;

        var d = new Date();

        var time = timeString.match(/(\d+)(:(\d\d))?\s*(p?)/i);
        if (time == null) return null;

        var hours = parseInt(time[1], 10);
        if (hours == 12 && !time[4]) {
            hours = 0;
        }
        else {
            hours += (hours < 12 && time[4]) ? 12 : 0;
        }
        d.setHours(hours);
        d.setMinutes(parseInt(time[3], 10) || 0);
        d.setSeconds(0, 0);
        return d;
    }
}
