﻿if (!SIGI) {
    throw ("SIGI.UI depends on SIGI.js.  Please insure that SIGI.js is loaded.");
}

/*****************************************************************/
/*		SIGI.DateTimeObject
/*		Accessible as SIGI.DateTime
/****************************************************************/
function DateTimeObject() { }
DateTimeObject.inherits(SIGI.Base);

DateTimeObject.prototype.getDate = function(date) {
    var oDate;
    if (typeof (date) == "object") {
        if (date instanceof Date) {
            return date;
        }
        else {
            if (date instanceof String) {
                oDate = this.parseDate(date.toString());
            }
            else {
                try {
                    if (date.className.toLowerCase() == "date") {
                        oDate = date.date;
                    }
                    else {
                        oDate = this.parseDate(date.value);
                    }
                }
                catch (exc) {
                    try {
                        oDate = new Date(date);
                    } catch (exc2) {
                        return null;
                    }
                }
            }
        }
    }
    else {
        oDate = this.parseDate(date);
    }

    if (oDate == null || isNaN(oDate)) {
        return null;
    }
    return oDate;
}

DateTimeObject.prototype.parseDate = function(date) {
    var oDate = null;
    var sDate = window.trim(date.toString());
    var sMonth = '';
    var sDay = '';
    var sYear = '';

    try {
        oDate = Date.parse(sDate);
        if (!(oDate == null || isNaN(oDate))) return new Date(oDate);
    } catch (ex) { }

    if (/^\d{6}|\d{8}$/.test(sDate)) {
        sMonth = sDate.slice(0, 2);
        sDay = sDate.slice(2, 4);
        sYear = sDate.slice(4);
    } else if (new RegExp("^\d{1,2}[/.-]\d{1,2}[/.-](\d{2}|\d{4})$").test(sDate)) {
        var aDate;
        aDate = sDate.split(new RegExp("[/.-]"));
        if (aDate.length == 3) {
            sMonth = aDate[0];
            sDay = aDate[1];
            sYear = aDate[2];
        }
    } else {
        oDate = new Date(date);
        if (isNaN(oDate)) {
            return null;
        } else {
            return oDate;
        }
    }
    if (sYear.length == 2) sYear = this.fullYear(sYear);
    oDate = new Date(sYear, sMonth - 1, sDay);
    var tmpStrDate = this.fixDigits(sMonth, 2) + this.fixDigits(sDay, 2) + this.fixDigits(sYear, 4)
    if (this.formatDate(oDate, "mmddyyyy") == tmpStrDate)
        return oDate;

    return null;
}

DateTimeObject.prototype.fullYear = function(year) {
    if (year.length == 2) {
        if (year > 60)
            year = "19" + year;
        else
            year = "20" + year;
    }
    else {
        if (year.length > 4)
            year = 0;
    }
    return year;
}

DateTimeObject.prototype.isValidDate = function(date) {
    var oDate = this.getDate(date);
    return (oDate != null);
}

DateTimeObject.prototype.dateDiff = function(interval, date1, date2) {
    var datea = this.getDate(date1);
    var dateb = this.getDate(date2);

    if (datea == null || dateb == null) {
        return 0;
    }

    var diff = datea.getTime() - dateb.getTime();
    var result = 0;

    switch (interval) {
        case "m":
            result = Math.round(diff / (1000 * 60 * 60 * 24 * 30));
            break;
        case "y":
            result = Math.round(diff / (1000 * 60 * 60 * 24 * 365));
            break;
        default:
            result = Math.round(diff / (1000 * 60 * 60 * 24));
            break;
    }
    return result;
}

DateTimeObject.prototype.dateAdd = function(interval, number, date) {
    var oDate = this.getDate(date);
    if (oDate == null) {
        return null;
    }

    var m = oDate.getMonth();
    var d = oDate.getDate();
    var y = oDate.getFullYear();

    switch (interval) {
        case "m":
            m = m + number;
            break;
        case "d":
            d = d + number;
            break;
        case "y":
            y = y + number;
    }

    var dt = new Date(y, m, d);
    return dt;
}

DateTimeObject.prototype.getMonthText = function(date) {
    var oDate = this.getDate(date)
    var abbr = false;
    if (arguments.length > 1)
        abbr = arguments[1];

    var sMonthName = ""
    var sMonthAbbr = "";

    switch (oDate.getMonth()) {
        case 0:
            sMonthName = "January";
            sMonthAbbr = "Jan";
            break;
        case 1:
            sMonthName = "February";
            sMonthAbbr = "Feb";
            break;
        case 2:
            sMonthName = "March";
            sMonthAbbr = "Mar";
            break;
        case 3:
            sMonthName = "April";
            sMonthAbbr = "Apr";
            break;
        case 4:
            sMonthName = "May";
            sMonthAbbr = "May";
            break;
        case 5:
            sMonthName = "June";
            sMonthAbbr = "Jun";
            break;
        case 6:
            sMonthName = "July";
            sMonthAbbr = "Jul";
            break;
        case 7:
            sMonthName = "August";
            sMonthAbbr = "Aug";
            break;
        case 8:
            sMonthName = "September";
            sMonthAbbr = "Sep";
            break;
        case 9:
            sMonthName = "October";
            sMonthAbbr = "Oct";
            break;
        case 10:
            sMonthName = "November";
            sMonthAbbr = "Nov";
            break;
        case 11:
            sMonthName = "December";
            sMonthAbbr = "Dec";
            break;
    }
    if (abbr == true)
        return sMonthAbbr;
    else
        return sMonthName;
}

DateTimeObject.prototype.getDayText = function(date) {
    var oDate = this.getDate(date);
    var abbr = false;
    if (arguments.length > 1)
        abbr = arguments[1];

    var sDayAbbr = "";
    var sDayName = "";

    switch (oDate.getDay()) {
        case 0:
            sDayName = "Sunday";
            sDayAbbr = "Sun";
            break;
        case 1:
            sDayName = "Monday";
            sDayAbbr = "Mon";
            break;
        case 2:
            sDayName = "Tuesday";
            sDayAbbr = "Tue";
            break;
        case 3:
            sDayName = "Wednesday";
            sDayAbbr = "Wed";
            break;
        case 4:
            sDayName = "Thursday";
            sDayAbbr = "Thu";
            break;
        case 5:
            sDayName = "Friday";
            sDayAbbr = "Fri";
            break;
        case 6:
            sDayName = "Saturday";
            sDayAbbr = "Sat";
            break;
    }
    if (abbr == true)
        return sDayAbbr;
    else
        return sDayName;

}

DateTimeObject.prototype.formatDate = function(date, format) {
    try {
        var oDate = this.getDate(date);
        if (oDate == null) return "";
    }
    catch (exc) {
        return "";
    }

    var month = /m+/g;
    var day = /d+/g;
    var Day = /D+/g;
    var year = /y+/g;

    var monthArr = month.exec(format);
    var dayArr = day.exec(format);
    var DayArr = Day.exec(format);
    var yearArr = year.exec(format);

    var iMonth = oDate.getMonth() + 1;
    var iday = oDate.getDate();
    var iDay = oDate.getDay();
    var iYear = oDate.getFullYear();

    var sReturn = "";

    var sMonth = iMonth.toString();
    if (monthArr != null) {
        switch (monthArr[0].length) {
            case 2:
                var sMonth2 = "0" + sMonth;
                sMonth = sMonth2.slice(sMonth2.length - 2)
                break;
            case 3:
                sMonth = this.getMonthText(oDate, true);
                break;
            case 4:
                sMonth = this.getMonthText(oDate);
                break;
        }
    }


    var sday = iday.toString();
    if (dayArr != null) {
        if (dayArr[0].length > 1) {
            var sday2 = "0" + sday;
            sday = sday2.slice(sday2.length - 2);
        }
    }

    var sDay = "";
    if (DayArr != null) {
        sDay = this.getDayText(oDate, !(DayArr[0].length == 4));
    }

    var sYear = iYear.toString();
    if (yearArr != null) {
        if (yearArr[0].length < 5) {
            sYear = sYear.slice(sYear.length - yearArr[0].length);
        }
    }

    format = format.replace(/y+/gi, sYear);
    format = format.replace(/d+/g, sday);
    format = format.replace(/D+/g, sDay);
    format = format.replace(/m+/g, sMonth);

    return format;
}

DateTimeObject.prototype.fixDigits = function(value, digits) {
    if (typeof (value) == "undefined")
        return "";

    if (value.length == 0)
        return "";

    var sDigits = new String("0");
    while (sDigits.length < digits) {
        sDigits = sDigits.concat("0");
    }
    value = sDigits.concat(value);
    value = value.slice(value.length - (digits));
    return value;
}

DateTimeObject.prototype.now = function() {
    return new Date();
}

DateTimeObject.prototype.grgJul = function(date) {

    var result = 0;

    var lOutDate = 0;
    var lDays = new Array();
    var lGrgConvert = 0;
    var lGrgIntermediate = 0;
    var lYearr = 0;
    var lTestYear = 0;
    var lJulianConvert = 0;
    var lTestMonth = 0;
    var lMonth = 0;
    var lDayy = 0;

    lDays[1] = 0;
    lDays[2] = 31;
    lDays[3] = 59;
    lDays[4] = 90;
    lDays[5] = 120;
    lDays[6] = 151;
    lDays[7] = 181;
    lDays[8] = 212;
    lDays[9] = 243;
    lDays[10] = 273;
    lDays[11] = 304;
    lDays[12] = 334;

    lOutDate = 0;
    lGrgConvert = toNumber(date);

    lGrgIntermediate = Math.floor(lGrgConvert / 100);
    lGrgIntermediate = lGrgIntermediate * 100;

    lYearr = (lGrgConvert - lGrgIntermediate);
    lTestYear = Math.floor(lYearr / 4);
    lTestYear = (lTestYear * 4);

    lJulianConvert = lYearr * 1000;
    lYearr = lYearr - lTestYear;
    lMonth = Math.floor(lGrgConvert / 10000);
    lJulianConvert = lJulianConvert + lDays[lMonth];

    lTestMonth = lMonth * 10000;
    lGrgIntermediate = lGrgConvert - lTestMonth;
    lDayy = Math.floor(lGrgIntermediate / 100);
    lJulianConvert = lJulianConvert + lDayy;

    lOutDate = lJulianConvert;
    result = lJulianConvert;
    if (Math.floor(lOutDate / 1000) <= 50) {  //(YEAR 2000)
        result = lOutDate + 100000;
    }

    if (result == 100000) {  // IF DATE PASSED IS 0
        result = 0;           // DON'T ADD
    }
    return result;
}

DateTimeObject.prototype.isLeapYear = function(year) {
    return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}

DateTimeObject.prototype.daysInMonth = function(month, year) {
    if (month == 2) return this.isLeapYear(year) ? 29 : 28;
    else if (month == 9 || month == 4 || month == 6 || month == 11) return 30;
    else return 31;
}
SIGI.DateTimeObject = DateTimeObject;
DateTimeObject = null;
SIGI.DateTime = new SIGI.DateTimeObject();

function ConvertObject() {
    this.Base();
}
ConvertObject.inherits(SIGI.Base);
ConvertObject.prototype.toString = function(value) {
    return value.toString();
}
ConvertObject.prototype.toBoolean = function(value) {
    return value.toString().toBoolean();
}
ConvertObject.prototype.toNumber = function(value) {
    return value.toString.toNumber();
}
ConvertObject.prototype.toInteger = function(value) {
    return parseInt(value.toString().toNumber().toString());
}
ConvertObject.prototype.toDate = function(value) {
    return SIGI.DateTime.getDate(value);
}
SIGI.ConvertObject = ConvertObject;
ConvertObject = null;

SIGI.Convert = new SIGI.ConvertObject();

function CollectionObject() {
    this.Base();
    this.length = 0;
    this.onadd = new Function();
    this.onremove = new Function();
}
CollectionObject.inherits(SIGI.Base);

CollectionObject.prototype.add = function(item) {
    if (item == null) return;
    var index = this.length;
    this.length++;
    this[index] = item;
    this.onadd(item, index);
    return item;
}

CollectionObject.prototype.remove = function(index) {
    if (index < 0 || index > this.length - 1) return;
    var item = this[index];
    this.onremove(item, index);
    this[index] = null;
    for (var i = index; i <= this.length; i++)
        this[i] = this[i + 1];
    this.length--;
    return item;
}

CollectionObject.prototype.isEmpty = function() {
    return this.length == 0;
}
CollectionObject.prototype.count = function() {
    return this.length;
}
CollectionObject.prototype.clear = function() {
    for (var i = 0; i < this.length; i++)
        this[i] = null;
    this.length = 0;
}
CollectionObject.prototype.clone = function() {
    var c = new SIGI.CollectionObject();
    for (var i = 0; i < this.length; i++)
        c.add(this[i]);
    return c;
}
CollectionObject.prototype.indexOf = function(item) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == item)
            return i;
    }
    return -1;
}
CollectionObject.prototype.findByAttribute = function(attribute, value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i].getAttribute) {
            val = this[i].getAttribute(attribute);
            if (val) {
                if (val == value) {
                    return this[i];
                }
            }
        }
    }
    return null;
}
CollectionObject.prototype.push = function(item) {
    return this.add(item);
}
CollectionObject.prototype.pop = function() {
    return this.remove(this.length - 1);
}
SIGI.CollectionObject = CollectionObject;
CollectionObject = null;
