/* Date.js** Copyright (c) 1999 Christopher D. Doemel
**
** Permission to use and modify this script is granted, so long as
** the above copyright is maintained, any modifications are
** documented, and credit is given for use of the script.
**
** This script has been modified by S. Leigh Geiger, Ph.D. for use in JavaScript classes
** Some comments have been removed to improve readability; other comments have been added
** for specialized class use and statements have been modified to simplify the script
** 
** This script can be used in two ways: the get(Date) and get(Time) functions may be 
** used to display a default format or they may be used with getFormatted Date()/Time()to
** produce a variety of formats as displayed in the Appendix of the Class Manual

*/

function getFormattedDate(theFormat) {
    var i = 0;
    var lastChar = "";
    var fieldCode = "";
    var fieldResult = "";
    var theDate = new Date();
    
    /* Convert theFormat to a string, even if it is already a string.
    ** If I don't do this, a lot of the String methods don't work.
    */
    theFormat = new String(theFormat);
    
    // Handle meridiem codes -- you know, AM or PM stuff.
    if (theFormat.charAt(i) == "a" || theFormat.charAt(i) == "A") {
        if (theFormat.indexOf("am/pm") == 0) {
            fieldResult = lcMeridiem(theDate);
            i += 5;
        }
        else if (theFormat.indexOf("AM/PM") == 0) {
            fieldResult = ucMeridiem(theDate);
            i += 5;
        }
        else if (theFormat.indexOf("a/p") == 0) {
            fieldResult = lcAbbrMeridiem(theDate);
            i += 3;
        }
        else if (theFormat.indexOf("A/P") == 0) {
            fieldResult = ucAbbrMeridiem(theDate);
            i += 3;
        }
        else {
            fieldResult = theFormat.charAt(i);
            i++;
        }
    }
    // Handle the oddball ordinal date code
    else if (theFormat.indexOf("dth") == 0) {
       fieldResult = getOrdinalDate(theDate);
       i += 3;
    }
    // Handle literal text (text enclosed by single quotes).
    else if (theFormat.charAt(i) == "'") {
        while (++i <= theFormat.length && theFormat.charAt(i) != "'") {
            fieldResult += theFormat.charAt(i);
        }
        if (theFormat.charAt(i) == "'") { ++i };
    }
    else {
        // Collect the next field code by collecting repeating characters.
        while (i <= theFormat.length) {
            if (lastChar == "") {
                // First time through the loop; grab a char!
                lastChar = theFormat.charAt(i);
                fieldCode += lastChar;
                i++;
                continue;
            }
            if (lastChar == theFormat.charAt(i)) {
                // The char is the same; save it and move on
                fieldCode += lastChar;
                i++;
                continue;
            }
            if (lastChar != theFormat.charAt(i)) {
                // The char is different; time to parse the field!
                break;
            }
        }
        
        // Handle month formats
        if      (fieldCode == "M")    { fieldResult = getOneDigitMonth(theDate);  }
        else if (fieldCode == "MM")   { fieldResult = getTwoDigitMonth(theDate);  }
        else if (fieldCode == "MMM")  { fieldResult = getShortMonthName(theDate); }
        else if (fieldCode == "MMMM") { fieldResult = getLongMonthName(theDate);  }
        
        // Handle date formats
        else if (fieldCode == "d")    { fieldResult = getOneDigitDate(theDate); }
        else if (fieldCode == "dd")   { fieldResult = getTwoDigitDate(theDate); }
        else if (fieldCode == "ddd")  { fieldResult = getShortDayName(theDate); }
        else if (fieldCode == "dddd") { fieldResult = getLongDayName(theDate);  }
        
        // Handle year formats
        else if (fieldCode == "yy")   { fieldResult = getTwoDigitYear(theDate);  }
        else if (fieldCode == "yyyy") { fieldResult = getFourDigitYear(theDate); }
        
        // Handle hour formats
        else if (fieldCode == "h")    { fieldResult = getOneDigitHour(theDate);         }
        else if (fieldCode == "H")    { fieldResult = getOneDigitMilitaryHour(theDate); }
        else if (fieldCode == "hh")   { fieldResult = getTwoDigitHour(theDate);         }
        else if (fieldCode == "HH")   { fieldResult = getTwoDigitMilitaryHour(theDate); }
        
        // Handle minute formats
        else if (fieldCode == "m")    { fieldResult = getOneDigitMinutes(theDate); }
        else if (fieldCode == "mm")   { fieldResult = getTwoDigitMinutes(theDate); }
        
        // Handle second formats
        else if (fieldCode == "s")    { fieldResult = getOneDigitSeconds(theDate); }
        else if (fieldCode == "ss")   { fieldResult = getTwoDigitSeconds(theDate); }
        
        // Handle everything else
        else                          { fieldResult = fieldCode; };
    }
    
    // If we're not done parsing the format codes, call getFormattedDate() with the
    // remaining codes.
    if (i == theFormat.length) { return fieldResult; }
    else { return fieldResult + getFormattedDate(theFormat.substring(i,theFormat.length)); }
}

/* The getDate() and getTime() functions call getFormattedDate() with default
** formats -- this saves time when you just want a standard date or time.
*/

// Returns date, e.g. "December 9, 2001"
function getDate() { return getFormattedDate("MMMM d, yyyy"); }

// Returns time, e.g., "6:03 pm"
function getTime() { return getFormattedDate("h:mm am/pm"); }

/* The following routines construct various portions of the final
** date, and are typically called by the getFormattedDate()
** function -- but you could use them yourself by providing a
** date.  To get the current two-digit date, for example:
**
**    getTwoDigitDate(new Date());
**
** The current version of the script is hard-coded for English
** month and day names.  If you want to use a different language,
** modify the getShortMonthName(), getLongMonthName(),
** getShortDayName(), and getLongDayName(), and getOrdinalDate(),
** functions to use the appropriate month and day names.
*/

function getOneDigitMonth(theDate) {
    return theDate.getMonth() + 1;
}

function getTwoDigitMonth(theDate) {
    var theMonth = getOneDigitMonth(theDate);
    return (theMonth < 10 ? "0" + theMonth : theMonth);
}

function getShortMonthName(theDate) {
    var theMonth = getOneDigitMonth(theDate);
    if      (theMonth == 1)  { return "Jan"; }
    else if (theMonth == 2)  { return "Feb"; }
    else if (theMonth == 3)  { return "Mar"; }
    else if (theMonth == 4)  { return "Apr"; }
    else if (theMonth == 5)  { return "May"; }
    else if (theMonth == 6)  { return "Jun"; }
    else if (theMonth == 7)  { return "Jul"; }
    else if (theMonth == 8)  { return "Aug"; }
    else if (theMonth == 9)  { return "Sep"; }
    else if (theMonth == 10) { return "Oct"; }
    else if (theMonth == 11) { return "Nov"; }
    else                     { return "Dec"; }
}

function getLongMonthName(theDate) {
    var theMonth = getOneDigitMonth(theDate);
    if      (theMonth == 1)  { return "January";   }
    else if (theMonth == 2)  { return "February";  }
    else if (theMonth == 3)  { return "March";     }
    else if (theMonth == 4)  { return "April";     }
    else if (theMonth == 5)  { return "May";       }
    else if (theMonth == 6)  { return "June";      }
    else if (theMonth == 7)  { return "July";      }
    else if (theMonth == 8)  { return "August";    }
    else if (theMonth == 9)  { return "September"; }
    else if (theMonth == 10) { return "October";   }
    else if (theMonth == 11) { return "November";  }
    else                     { return "December";  }
}

function getOneDigitDate(theDate) {
    return theDate.getDate();
}

function getOrdinalDate(theDate) {
    var theDay = getOneDigitDate(theDate);
    var ordDay = parseInt(theDay) % 10;
         if (ordDay == 1) { return theDay + "st"; }
    else if (ordDay == 2) { return theDay + "nd"; }
    else if (ordDay == 3) { return theDay + "rd"; }
    else                  { return theDay + "th"; }
}

function getTwoDigitDate(theDate) {
    var theDay = getOneDigitDate(theDate);
    return (theDay < 10 ? "0" + theDay : theDay);
}

function getShortDayName(theDate) {
    var theDay = theDate.getDay();
    if      (theDay == 0) { return "Sun"; }
    else if (theDay == 1) { return "Mon"; }
    else if (theDay == 2) { return "Tue"; }
    else if (theDay == 3) { return "Wed"; }
    else if (theDay == 4) { return "Thu"; }
    else if (theDay == 5) { return "Fri"; }
    else                 { return "Sat"; }
}

function getLongDayName(theDate) {
    var theDay = theDate.getDay();
    if      (theDay == 0) { return "Sunday";    }
    else if (theDay == 1) { return "Monday";    }
    else if (theDay == 2) { return "Tuesday";   }
    else if (theDay == 3) { return "Wednesday"; }
    else if (theDay == 4) { return "Thursday";  }
    else if (theDay == 5) { return "Friday";    }
    else                 { return "Saturday";  }
}

function getFourDigitYear(theDate) {
    var theYear = theDate.getYear();
    if (theYear < 1000) { theYear += 1900; }
    return theYear;
}

function getTwoDigitYear(theDate) {
    var theYear = getFourDigitYear(theDate);
    var strYear = new String(theYear);
    return strYear.substring(2,4);
}

function getOneDigitHour(theDate) {
    var theHour = theDate.getHours() % 12;
    if (theHour == 0) { theHour = 12; }
    return theHour;
}

function getTwoDigitHour(theDate) {
    var theHour = getOneDigitHour(theDate);
    return (theHour < 10 ? "0" + theHour : theHour);
}

function getOneDigitMilitaryHour(theDate) {
    return theDate.getHours();
}

function getTwoDigitMilitaryHour(theDate) {
    var theHour = getOneDigitMilitaryHour(theDate);
    return (theHour < 10 ? "0" + theHour : theHour);
}

function getOneDigitMinutes(theDate) {
    return theDate.getMinutes();
}

function getTwoDigitMinutes(theDate) {
    var theMinutes = getOneDigitMinutes(theDate);
    return (theMinutes < 10 ? "0" + theMinutes : theMinutes);
}

function getOneDigitSeconds(theDate) {
    return theDate.getSeconds();
}

function getTwoDigitSeconds(theDate) {
    var theSeconds = getOneDigitSeconds(theDate);
    return (theSeconds < 10 ? "0" + theSeconds : theSeconds);
}

function lcMeridiem(theDate) {
    var theHour = getOneDigitMilitaryHour(theDate);
    return (theHour < 12 ? "am" : "pm");
}

function lcAbbrMeridiem(theDate) {
    var theHour = getOneDigitMilitaryHour(theDate);
    return (theHour < 12 ? "a" : "p");
}

function ucMeridiem(theDate) {
    var theHour = getOneDigitMilitaryHour(theDate);
    return (theHour < 12 ? "AM" : "PM");
}

function ucAbbrMeridiem(theDate) {
    var theHour = getOneDigitMilitaryHour(theDate);
    return (theHour < 12 ? "A" : "P");
}
