if (typeof(LinkDev) == 'undefined')
{
    LinkDev = new Object();
}

if (typeof(LinkDev.WebMonkey) == 'undefined')
{
    LinkDev.WebMonkey = new Object();
}

LinkDev.WebMonkey.CalendarDay = function(dateString, calendarType, adjustment)
{
    this.calendarType = calendarType;
    this.dateString = dateString;
    var tempArray = dateString.split('-');
    this.year = parseInt(tempArray[0], 10);
    this.month = parseInt(tempArray[1], 10);
    this.day = parseInt(tempArray[2], 10);
    this.adjustment = adjustment;
    
    this.getString = function ()
    {
        return this.dateString;
    }
    
    this.getFullYear = function ()
    {
        return this.year;
    }
    
    this.getMonth = function ()
    {
        return this.month;
    }
    
    this.getDay = function ()
    {
        return this.day;
    }
    
    this.addDays = function (days)
    {
        var tday = this.day + days;
        var tmonth = this.month;
        var tyear = this.year;
        
        while(tday > LinkDev.WebMonkey.Dates.GetDaysInMonth(this.calendarType, tyear, tmonth))
        {
            tday -= LinkDev.WebMonkey.Dates.GetDaysInMonth(this.calendarType, tyear, tmonth);
            
            tmonth++;
            if(tmonth > LinkDev.WebMonkey.Dates.GetMonthsInYear(this.calendarType, tyear))
            {
                tmonth = 1;
                tyear++;
            }
        }
        while(tday <= 0)
        {
            tmonth--;
            if(tmonth <= 0)
            {
                tyear--;
                tmonth = LinkDev.WebMonkey.Dates.GetMonthsInYear(this.calendarType, tyear);
            }
            tday += LinkDev.WebMonkey.Dates.GetDaysInMonth(this.calendarType, tyear, tmonth);
        }
        var sDate = tyear.toString() + '-' + tmonth.toString() + '-' + tday.toString();
        return new LinkDev.WebMonkey.CalendarDay(sDate, this.calendarType, this.adjustment);
    }
    
    this.addMonths = function (months)
    {
        var tday = this.day;
        var tmonth = this.month;
        var tyear = this.year;
        
        tmonth = this.month + months;
        
        while(tmonth > LinkDev.WebMonkey.Dates.GetMonthsInYear(this.calendarType, tyear))
        {
            tmonth -= LinkDev.WebMonkey.Dates.GetMonthsInYear(this.calendarType, tyear);
            tyear++;
        }
        while(tmonth <= 0)
        {
            tyear--;
            tmonth += LinkDev.WebMonkey.Dates.GetMonthsInYear(this.calendarType, tyear);
        }
        var sDate = tyear.toString() + '-' + tmonth.toString() + '-' + tday.toString();
        return new LinkDev.WebMonkey.CalendarDay(sDate, this.calendarType, this.adjustment);
    }
    
    this.addYears = function(years)
    {
        var tday = this.day;
        var tmonth = this.month;
        var tyear = this.year;
        
        tyear = this.year + years;
        if(tmonth > LinkDev.WebMonkey.Dates.GetMonthsInYear(this.calendarType, tyear))
        {
            tmonth--;
        }
        var sDate = tyear.toString() + '-' + tmonth.toString() + '-' + tday.toString();
        return new LinkDev.WebMonkey.CalendarDay(sDate, this.calendarType, this.adjustment);
    }
    
    this.getDayOfWeek = function ()
    {
        var date = LinkDev.WebMonkey.Dates.GetDate(this.calendarType, this.year, this.month, this.day, this.adjustment);
        return date.getDay();
    }
    
    this.getDate = function ()
    {
        var date = LinkDev.WebMonkey.Dates.GetDate(this.calendarType, this.year, this.month, this.day, this.adjustment);
        return date;
    }
    
    this.getGregorianDate = this.getDate;
    
    this.formatDate = function (format, calendarInfo)
    {
        var monthNames = calendarInfo == null ? null : calendarInfo.monthNames;
        var shortMonthNames = calendarInfo == null ? null : calendarInfo.shortMonthNames;
        var dayNames = calendarInfo == null ? null : calendarInfo.dayNames;
        var shortDayNames = calendarInfo == null ? null : calendarInfo.shortDayNames;
        var calendarType = calendarInfo == null ? null : calendarInfo.calendarType;
        var rx = /(?:\""[^\""]+\"")|(?:\'[^\']+\')|(?:y{1,4})|(?:M{1,4})|(?:d+)/g;
        var isHebrew = (calendarType == 'Hebrew');
        if(isHebrew)
        {
            monthNames = LinkDev.WebMonkey.Dates.IsLeapYear(calendarType, this.year) ?  LinkDev.WebMonkey.Dates.Hebrew.LeapYearMonths : LinkDev.WebMonkey.Dates.Hebrew.NormalYearMonths;
            shortMonthNames = monthNames;
        }
        var lastindex = 0;
        var strResult = "";
        while((res = rx.exec(format)) != null)
        {
            var match = RegExp.lastMatch;
            var len = match.length;
            var index = res.index;
            if(index > lastindex)
            {
                strResult += format.substr(lastindex, index - lastindex);
            }
            
            lastindex = index + len;
            if(match.charAt(0) == '\'' || match.charAt(0) == '\"')
            {
                strResult += match.substr(1, len - 2);
            }
            else if(match.charAt(0) == '\\')
            {
                strResult += match.substr(1, 1);
            }
            else if(match == 'M')
            {
                if(isHebrew)
                    strResult += LinkDev.WebMonkey.Dates.Hebrew.FormatNumber(this.month);
                else
                    strResult += this.month.toString();
            }
            else if(match == 'MM')
            {
                if(isHebrew)
                {
                    strResult += LinkDev.WebMonkey.Dates.Hebrew.FormatNumber(this.month);
                }
                else
                {
                    if(this.month < 10)
                        strResult += "0";
                    strResult += this.month.toString();
                }
            }
            else if(match == 'MMM')
            {
                strResult += shortMonthNames[this.month - (isHebrew ? 0 : 1)];
            }
            else if(match == 'MMMM')
            {
                strResult += monthNames[this.month - (isHebrew ? 0 : 1)]
            }
            else if(match == 'd')
            {
                if(isHebrew)
                {
                    strResult += LinkDev.WebMonkey.Dates.Hebrew.FormatNumber(this.day);
                }
                else
                    strResult += this.day.toString();
            }
            else if(match == 'dd')
            {
                if(isHebrew)
                    strResult += LinkDev.WebMonkey.Dates.Hebrew.FormatNumber(this.day);
                else
                {
                    if(this.day < 10)
                        strResult += "0";
                    strResult += this.day.toString();
                }
            }
            else if(match == 'ddd')
            {
                strResult += shortDayNames[this.getDayOfWeek()];
            }
            else if(match == 'dddd')
            {
                strResult += dayNames[this.getDayOfWeek()];
            }
            else if(match == 'yyyy')
            {
                if(isHebrew)
                    strResult += LinkDev.WebMonkey.Dates.Hebrew.FormatNumber(this.year);
                else
                    strResult += this.year.toString();
            }
            else if(match == 'yy')
            {
                if(isHebrew)
                    strResult += LinkDev.WebMonkey.Dates.Hebrew.FormatNumber(this.year);
                else
                    strResult += (this.year % 100).toString();
            }
        }
        if(lastindex < format.length)
        strResult += format.substr(lastindex, format.length - lastindex);
        return strResult;
    }
    
    this.compareTo = function (otherDate)
    {
        if(otherDate.year > this.year)
            return -1;
        if(otherDate.year < this.year)
            return 1;
        if(otherDate.month > this.month)
            return -1;
        if(otherDate.month < this.month)
            return 1;
        if(otherDate.day > this.day)
            return -1;
        if(otherDate.day < this.day)
            return 1;
        return 0;
    }
    
    this.isInRange = function(firstDate, lastDate)
    {
        return this.compareTo(firstDate) >= 0 && this.compareTo(lastDate) <= 0;
    }
    
    this.toString = function()
    {
        return this.dateString;
    }
}
