﻿// JScript File

//-----------------------------------------------------------------
//	Date.prototype.dateShort()
//-----------------------------------------------------------------
//	Description: 	This function is a method added to the Date object.
//						It will return a string of the UTC date mm/dd/yyyy
//-----------------------------------------------------------------
Date.prototype.dateShort = function() {
    var d = this.getUTCDate();
    var m = (this.getUTCMonth() + 1);
    d = (d < 10 ? "0" : "") + d;
    m = (m < 10 ? "0" : "") + m;
    return (m + "/" + d + "/" + this.getUTCFullYear());
};
//-----------------------------------------------------------------
//	Date.prototype.dateShort()
//-----------------------------------------------------------------
//	Description: 	This function is a method added to the Date object.
//						It will return a string of the UTC date mm/dd/yyyy
//-----------------------------------------------------------------
Date.prototype.dateShortLocal = function() {
    var d = this.getDate();
    var m = (this.getMonth() + 1);
    d = (d < 10 ? "0" : "") + d;
    m = (m < 10 ? "0" : "") + m;
    return (m + "/" + d + "/" + this.getFullYear());
};
//-----------------------------------------------------------------
//	Date.prototype.datetimeShort()
//-----------------------------------------------------------------
//	Description: 	This function is a method added to the Date object.
//						It will return a string of the date mm/dd/yyyy mm:dd
//-----------------------------------------------------------------
Date.prototype.datetimeShort = function() {
    var d = this.getDate();
    var m = (this.getMonth() + 1);
    d = (d < 10 ? "0" : "") + d;
    m = (m < 10 ? "0" : "") + m;
    var ss = this.getSeconds();
    var mm = this.getMinutes();
    var hh = this.getHours();
    ss = (ss < 10 ? "0" : "") + ss;
    mm = (mm < 10 ? "0" : "") + mm;
    hh = (hh < 10 ? "0" : "") + hh;
    return (m + "/" + d + "/" + this.getFullYear() + " " + hh + ":" + mm + ":" + ss);
};


Date.prototype.getUTCMS = function() {
    return Date.UTC(this.getUTCFullYear(), this.getUTCMonth(), this.getUTCDate(), this.getUTCHours(), this.getUTCMinutes(), this.getUTCSeconds(), this.getUTCMilliseconds());
};
Date.prototype.getMS = function() {
    return Date.UTC(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
};

Date.prototype.firstOfMonth = function() {
    oD = new Date(this.getUTCFullYear(), this.getUTCMonth(), 1, 0, 0, 0);
    return (oD.getUTCDay());
};

Date.prototype.diff = function(oD1, oD2) {
    var r = { s: null, ms: null };
    var d = { raw: r, d: null, h: null, m: null, s: null, ms: null };
    d.raw.ms = parseInt((parseInt(oD1.getUTCMS()) - parseInt(oD2.getUTCMS())));
    d.raw.s = parseInt(d.raw.ms / 1000);
    d.d = parseInt(d.raw.ms / 84600000);
    d.h = parseInt((d.raw.ms % 86400000) / 3600000);
    d.m = parseInt((d.raw.ms % 3600000) / 60000);
    d.s = parseInt((d.raw.ms % 60000) / 1000);
    d.ms = parseInt(d.raw.ms % 1000);
    return (d);
};

Date.prototype.strDiff = function(oD1, oD2) {
    var d = this.diff(oD1, oD2)
    return ("d:" + d.d + " h:" + d.h + " m:" + d.m + " s:" + d.s + " ms:" + d.ms);
};

//-----------------------------------------------------------------
//	Date.prototype.parseSQLTime()
//-----------------------------------------------------------------
//	Description: 	This function is a method added to the Date object.
//						It will parse out SQL's crazy time
//-----------------------------------------------------------------
Date.prototype.parseSQLTime = function(strDate) {
    if (strDate) {
        if (strDate.length > 0) {
            if (strDate.indexOf("T") > 0) {
                var sDate = (strDate.split('T'))[0];
                sDate = sDate.split('-');
                sDate = sDate[0] + "/" + sDate[1] + "/" + sDate[2];
                var sTime = (strDate.split('T'))[1];

                var s = sDate + " " + sTime;
                return (new Date(s));
            }
            else
                return (new Date(strDate));
        }
    }
};


//-----------------------------------------------------------------
//	Date.prototype.validate()
//-----------------------------------------------------------------
//	Description: 	This function is a method added to the Date object.
//						It will validate the date string passed and return
//                false if not valid or a new date object containg the
//                date.
//
// Parameter 1:	Sring date mm/dd/yyyy
//-----------------------------------------------------------------
Date.prototype.validate = function(strDate) {
    if (strDate == "") {
        return (false);
    }
    strDate = new String(strDate);
    var arrDate = strDate.split("/");

    if ((arrDate.length < 3) || (arrDate.length > 3)) {
        return (false);
    }
    for (var c = 0; c < arrDate.length; c++) {
        if (!parseInt(arrDate[c], 10)) {
            return (false);
        }
        if (parseInt(arrDate[c], 10) != arrDate[c]) {
            return (false);
        }
        if (parseInt(arrDate[c]) < 0) {
            return (false);
        }
    }
    return (new Date(strDate));
};


//-----------------------------------------------------------------
//	Date.prototype.add()
//-----------------------------------------------------------------
//	Description: 	This function is a method added to the Date object.
//						It will return a new date object with the adjusted 
//                date/time. Works just like the VB script dateAdd()
//-----------------------------------------------------------------
Date.prototype.add = function(strInterval, intAmmount) {
    if ((intAmmount = parseInt(intAmmount)) == NaN) { return (this); }

    oD = new Date(this);

    intHours = this.getUTCHours();
    intMinutes = this.getUTCMinutes();
    intSeconds = this.getUTCSeconds();
    intMS = this.getUTCMilliseconds();

    intDay = this.getUTCDate();
    intMonth = this.getUTCMonth();
    intYear = this.getUTCFullYear();

    strInterval = String(strInterval);
    switch (strInterval.toLowerCase()) {
        case "yyyy": //Year
            return (new Date(oD.setUTCFullYear(this.getUTCFullYear() + intAmmount)));
            break;
        case "m":   //Month
            return (new Date(oD.setUTCMonth(this.getUTCMonth() + intAmmount)));
            break;
        case "d":   //Day
            return (new Date(oD.setUTCDate(this.getUTCDate() + intAmmount)));
            break;
        case "ww": //Week
            return (new Date(oD.setUTCDate(this.getUTCDate() + (intAmmount * 7))));
            break;
        case "h": //Hours
            return (new Date(oD.setUTCHours(this.getUTCHours() + intAmmount)));
            break;
        case "n": //Minutes
            return (new Date(oD.setUTCMinutes(this.getUTCMinutes() + intAmmount)));
            break;
        case "s": //Seconds
            return (new Date(oD.setUTCSeconds(this.getUTCSeconds() + intAmmount)));
            break;
    }
};
//-----------------------------------------------------------------
//	Date.prototype.add()
//-----------------------------------------------------------------
//	Description: 	This function is a method added to the Date object.
//						It will return a new date object with the adjusted 
//                date/time. Works just like the VB script dateAdd()
//-----------------------------------------------------------------
Date.prototype.addDate = function(strInterval, intAmmount) {
   if ((intAmmount = parseInt(intAmmount)) == NaN) { return (this); }

   oD = new Date(this);

   strInterval = String(strInterval);
   switch (strInterval.toLowerCase()) {
      case "yyyy": //Year
         return (new Date(oD.setFullYear(this.getFullYear() + intAmmount)));
         break;
      case "m":   //Month
         return (new Date(oD.setMonth(this.getMonth() + intAmmount)));
         break;
      case "d":   //Day
         return (new Date(oD.setDate(this.getDate() + intAmmount)));
         break;
      case "ww": //Week
         return (new Date(oD.setDate(this.getDate() + (intAmmount * 7))));
         break;
      case "h": //Hours
         return (new Date(oD.setHours(this.getHours() + intAmmount)));
         break;
      case "n": //Minutes
         return (new Date(oD.setMinutes(this.getMinutes() + intAmmount)));
         break;
      case "s": //Seconds
         return (new Date(oD.setSeconds(this.getSeconds() + intAmmount)));
         break;
   }
};
