﻿function dateAddExtention(p_Interval, p_Number) {


    var thing = new String();


    //in the spirt of VB we'll make this function non-case sensitive
    //and convert the charcters for the coder.
    p_Interval = p_Interval.toLowerCase();

    if (isNaN(p_Number)) {

        //Only accpets numbers 
        //throws an error so that the coder can see why he effed up    
        throw "The second parameter must be a number. \n You passed: " + p_Number;
        return false;
    }

    p_Number = new Number(p_Number);
    switch (p_Interval.toLowerCase()) {
        case "yyyy":
            {// year
                this.setFullYear(this.getFullYear() + p_Number);
                break;
            }
        case "q":
            {        // quarter
                this.setMonth(this.getMonth() + (p_Number * 3));
                break;
            }
        case "m":
            {        // month
                var tempDate = new Date(this.getTime());
                tempDate.setDate(1);
                tempDate.setMonth(this.getMonth() + p_Number);
                var maxDaysMonth = daysInMonth(tempDate.getMonth(), tempDate.getYear())
                if (this.getDate() > maxDaysMonth) {
                    this.setDate(maxDaysMonth)
                }
                this.setMonth(this.getMonth() + p_Number);
                break;
            }
        case "y":        // day of year
        case "d":        // day
        case "w":
            {        // weekday
                this.setDate(this.getDate() + p_Number);
                break;
            }
        case "ww":
            {    // week of year
                this.setDate(this.getDate() + (p_Number * 7));
                break;
            }
        case "h":
            {        // hour
                this.setHours(this.getHours() + p_Number);
                break;
            }
        case "n":
            {        // minute
                this.setMinutes(this.getMinutes() + p_Number);
                break;
            }
        case "s":
            {        // second
                this.setSeconds(this.getSeconds() + p_Number);
                break;
            }
        case "ms":
            {        // second
                this.setMilliseconds(this.getMilliseconds() + p_Number);
                break;
            }
        default:
            {

                //throws an error so that the coder can see why he effed up and
                //a list of elegible letters.
                throw "The first parameter must be a string from this list: \n" +
                    "yyyy, q, m, y, d, w, ww, h, n, s, or ms. You passed: " + p_Interval;
                return false;
            }
    }
    return this;
}
Date.prototype.dateAdd = dateAddExtention;
