﻿function Calendar(target, box)
{
    this.ViewDays = function(year, month) {
        var datoNu = new Date(year, month, 1);
        var datoDenne = new Date(datoNu.getFullYear(), datoNu.getMonth(), 1);
        var datoForrige = new Date(datoNu.getFullYear(), datoNu.getMonth(), 0);
        var datoNaeste = new Date(datoNu.getFullYear(), datoNu.getMonth(), (DaysInMonth(datoDenne.getFullYear(), datoDenne.getMonth())+1));
        var output = "";
        
        document.getElementById("previous").innerHTML = "<a style='cursor:pointer;' onclick='cal.ViewDays(" + datoForrige.getFullYear() + ", " + datoForrige.getMonth() + ")'>&lt;&lt;</a>";
        document.getElementById("current").innerHTML = "<a style='cursor:pointer;' onclick='cal.ViewMonths(" + datoDenne.getFullYear() + ")'>" + months[datoDenne.getMonth()] + " " + datoDenne.getFullYear() + "</a>";
        document.getElementById("next").innerHTML = "<a style='cursor:pointer;' onclick='cal.ViewDays(" + datoNaeste.getFullYear() + ", " + datoNaeste.getMonth() + ")'>&gt;&gt;</a>";
        
        output += "<table width='200' border='0' cellspacing='0' cellpadding='0'>";
            output += "<tr align='right' valign='middle' height='17'>";
                output += "<td width='22'>&nbsp;</td>"
                output += "<td width='22' style='border-bottom:solid 1px black; padding-right:3px;'>M</td>";
                output += "<td width='22' style='border-bottom:solid 1px black; padding-right:3px;'>T</td>";
                output += "<td width='22' style='border-bottom:solid 1px black; padding-right:3px;'>O</td>";
                output += "<td width='22' style='border-bottom:solid 1px black; padding-right:3px;'>T</td>";
                output += "<td width='22' style='border-bottom:solid 1px black; padding-right:3px;'>F</td>";
                output += "<td width='22' style='border-bottom:solid 1px black; padding-right:3px;'>L</td>";
                output += "<td width='22' style='border-bottom:solid 1px black; padding-right:3px;'>S</td>";
        var count = 0;
        var dateArray = new Array();
        var monthArray = new Array();
        var yearArray = new Array();
        var forrigeStartDag = 1;
        var forrigeSlutDag = 1;
        
        if(datoForrige.getWeek(1) == datoDenne.getWeek(1))
        {
            var offSet = (datoForrige.getDay() - 1);
            if (offSet > 0)
                offSet *= -1;
            forrigeStartDag = datoForrige.getDate() + offSet;
            forrigeSlutDag = DaysInMonth(datoForrige.getFullYear(), datoForrige.getMonth());
        }
        else
        {
            var offSet = (datoDenne.getDay() - (7 - datoForrige.getDay()));
            if (offSet > 0)
                offSet *= -1;
            forrigeStartDag = (datoForrige.getDate() + offSet);
            forrigeSlutDag = DaysInMonth(datoForrige.getFullYear(), datoForrige.getMonth());
        }
        
        var denneStartDag = 1;
        var denneSlutDag = DaysInMonth(datoDenne.getFullYear(), datoDenne.getMonth());
        
        for (var i = forrigeStartDag; i <= forrigeSlutDag; i++)
        {
            dateArray[count] = i;
            monthArray[count] = datoForrige.getMonth();
            yearArray[count] = datoForrige.getFullYear();
            count++;
        }
        
        for (var i = denneStartDag; i <= denneSlutDag; i++)
        {
            dateArray[count] = i;
            monthArray[count] = datoDenne.getMonth();
            yearArray[count] = datoDenne.getFullYear();
            count++;
        }
        
        for (var i = 1; count < 42; i++)
        {
            dateArray[count] = i;
            monthArray[count] = datoNaeste.getMonth();
            yearArray[count] = datoNaeste.getFullYear();
            count++;
        }
        
        count = 7;
        previousMonth = false;
        nextMonth = false;
        if (dateArray[0] != 1)
            previousMonth = true;
        
        for (var i = 0; i < dateArray.length; i++)
        {
            
            if (dateArray[i] == 1)
                previousMonth = false;
            if (dateArray[i] == 1 && i > 7)
                nextMonth = true;
            
            var dato = new Date(yearArray[i], monthArray[i], dateArray[i]);
            
            var activeDayIndex = checkForActiveDate(dato);
            var isActiveDate = false;
            
            if (activeDayIndex >= 0)
                isActiveDate = true;
            
            var isToday = false;
            if (dateNow.getFullYear() == dato.getFullYear() && dateNow.getMonth() == dato.getMonth() && dateNow.getDate() == dato.getDate())
                isToday = true;
            
            if (count == 7)
            {
                var datoUge = new Date(yearArray[i], monthArray[i], dateArray[i]);
                var udUge = "" + datoUge.getWeek(1);
                if (udUge.length == 1)
                    udUge = "&nbsp;&nbsp;" + udUge;
                output +="</tr><tr align='right' valign='middle' height='17'>";
                output += "<td style='border-right:solid 1px black; cursor:default; padding-right:2px;'>" + udUge + "</td>";
                if (previousMonth || nextMonth)
                    if (isEventCalendar)
                    {
                        if (isActiveDate)
                            if (isToday)
                                output += "<td style='cursor:default; border:solid 1px #AA0000; padding-right:2px;'><a href='" + activeDayLinks[activeDayIndex] + "' style='cursor:pointer; font-weight:bold;'>" + dateArray[i] + "</a></td>";
                            else
                                output += "<td style='cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'><a href='" + activeDayLinks[activeDayIndex] + "' style='cursor:pointer; font-weight:bold;'>" + dateArray[i] + "</a></td>";
                        else
                            if (isToday)
                                output += "<td style='color:#BABABA; cursor:default; border:solid 1px #AA0000; padding-right:2px;'>" + dateArray[i] + "</td>";
                            else
                                output += "<td style='color:#BABABA; cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'>" + dateArray[i] + "</td>";
                    }
                    else
                    {
                        if (isToday)
                            output += "<td style='color:#BABABA; cursor:default; border:solid 1px #AA0000; padding-right:2px;'>" + dateArray[i] + "</td>";
                        else
                            output += "<td style='color:#BABABA; cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'>" + dateArray[i] + "</td>";
                    }
                else
                    if (isEventCalendar)
                    {
                        if (isActiveDate)
                            if (isToday)
                                output += "<td style='cursor:default; border:solid 1px #AA0000; padding-right:2px;'><a href='" + activeDayLinks[activeDayIndex] + "' style='cursor:pointer; font-weight:bold;'>" + dateArray[i] + "</a></td>";
                            else
                                output += "<td style='cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'><a href='" + activeDayLinks[activeDayIndex] + "' style='cursor:pointer; font-weight:bold;'>" + dateArray[i] + "</a></td>";
                        else
                            if (isToday)
                                output += "<td style='cursor:default; border:solid 1px #AA0000; padding-right:2px;'>" + dateArray[i] + "</td>";
                            else
                                output += "<td style='cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'>" + dateArray[i] + "</td>";
                    }
                    else
                    {
                        if (isToday)
                            output += "<td style='cursor:default; border:solid 1px #AA0000; padding-right:2px;'><a onclick='cal.SetSelectedDate(" + yearArray[i] + ", " + monthArray[i] + ", " + dateArray[i] + ")' style='cursor:pointer'>" + dateArray[i] + "</a></td>";
                        else
                            output += "<td style='cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'><a onclick='cal.SetSelectedDate(" + yearArray[i] + ", " + monthArray[i] + ", " + dateArray[i] + ")' style='cursor:pointer'>" + dateArray[i] + "</a></td>";
                    }
                count = 0;
            }
            else
            {
                if (previousMonth || nextMonth)
                    if (isEventCalendar)
                    {
                        if (isActiveDate)
                            if (isToday)
                                output += "<td style='cursor:default; border:solid 1px #AA0000; padding-right:2px;'><a href='" + activeDayLinks[activeDayIndex] + "' style='cursor:pointer; font-weight:bold;' href='/'>" + dateArray[i] + "</a></td>";
                            else
                                output += "<td style='cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'><a href='" + activeDayLinks[activeDayIndex] + "' style='cursor:pointer; font-weight:bold;'>" + dateArray[i] + "</a></td>";
                        else
                            if (isToday)
                                output += "<td style='color:#BABABA; cursor:default; border:solid 1px #AA0000; padding-right:2px;'>" + dateArray[i] + "</td>";
                            else
                                output += "<td style='color:#BABABA; cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'>" + dateArray[i] + "</td>";
                    }
                    else
                    {
                        if (isToday)
                            output += "<td style='color:#BABABA; cursor:default; border:solid 1px #AA0000; padding-right:2px;'>" + dateArray[i] + "/td>";
                        else
                            output += "<td style='color:#BABABA; cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'>" + dateArray[i] + "</td>";
                    }
                else
                    if (isEventCalendar)
                    {
                        if (isActiveDate)
                            if (isToday)
                                output += "<td style='cursor:default; border:solid 1px #AA0000; padding-right:2px;'><a href='" + activeDayLinks[activeDayIndex] + "' style='cursor:pointer; font-weight:bold;'>" + dateArray[i] + "</a></td>";
                            else
                                output += "<td style='cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'><a href='" + activeDayLinks[activeDayIndex] + "' style='cursor:pointer; font-weight:bold;'>" + dateArray[i] + "</a></td>";
                        else
                            if (isToday)
                                output += "<td style='cursor:default; border:solid 1px #AA0000; padding-right:2px;'>" + dateArray[i] + "</td>";
                            else
                                output += "<td style='cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'>" + dateArray[i] + "</td>";
                    }
                    else
                    {
                        if (isToday)
                            output += "<td style='cursor:default; border:solid 1px #AA0000; padding-right:2px;'><a onclick='cal.SetSelectedDate(" + yearArray[i] + ", " + monthArray[i] + ", " + dateArray[i] + ")' style='cursor:pointer'>" + dateArray[i] + "</a></td>";
                        else
                            output += "<td style='cursor:default; border:solid 1px #FFFFFF; padding-right:2px;'><a onclick='cal.SetSelectedDate(" + yearArray[i] + ", " + monthArray[i] + ", " + dateArray[i] + ")' style='cursor:pointer'>" + dateArray[i] + "</a></td>";
                    }
            }
            count++
        }
        output +="</tr></table>";
        document.getElementById("daysInMonth").innerHTML = output;
        document.getElementById("daysInMonth").style.display = "block";
        document.getElementById("monthsInYear").style.display = "none";
        document.getElementById("yearsInDecade").style.display = "none";
    }
    
    this.ViewMonths = function(year) {
        curYear = eval(year);
            
            document.getElementById("previous").innerHTML = "<a onclick='cal.ViewMonths(" + eval(eval(year)-1) + ")' style='cursor:pointer;'>&lt;&lt;</a>";
            document.getElementById("current").innerHTML = "<a onclick='cal.ViewDecade(" + year + ")' style='cursor:pointer;'>" + year + "</a>";
            document.getElementById("next").innerHTML = "<a onclick='cal.ViewMonths(" + eval(eval(year)+1) + ")' style='cursor:pointer;'>&gt;&gt;</a>";
            
        output = "<table width='200' cellpadding='0' cellspacing='0' border='0'>";
            output += "<tr height='30'>";
                output += "<td align='right' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 0 + ")'>Jan</a></td>";
                output += "<td>&nbsp;</td>"
                output += "<td align='center' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 1 + ")'>Feb</a></td>";
                output += "<td>&nbsp;</td>"
                output += "<td align='left' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 2 + ")'>Mar</a></td>";
            output += "</tr>";
            output += "<tr height='30'>";
                output += "<td align='right' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 3 + ")'>Apr</a></td>";
                output += "<td>&nbsp;</td>"
                output += "<td align='center' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 4 + ")'>Maj</a></td>";
                output += "<td>&nbsp;</td>"
                output += "<td align='left' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 5 + ")'>Jun</a></td>";
            output += "</tr>";
            output += "<tr height='30'>";
                output += "<td align='right' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 6 + ")'>Jul</a></td>";
                output += "<td>&nbsp;</td>"
                output += "<td align='center' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 7 + ")'>Aug</a></td>";
                output += "<td>&nbsp;</td>"
                output += "<td align='left' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 8 + ")'>Sep</a></td>";
            output += "</tr>";
            output += "<tr height='29'>";
                output += "<td align='right' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 9 + ")'>Okt</a></td>";
                output += "<td>&nbsp;</td>"
                output += "<td align='center' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 10 + ")'>Nov</a></td>";
                output += "<td>&nbsp;</td>"
                output += "<td align='left' valign='middle'><a style='cursor:pointer;' onclick='cal.ViewDays(" + year + ", " + 11 + ")'>Dec</a></td>";
            output += "</tr>";
        output += "</table>";
        document.getElementById("monthsInYear").innerHTML = output;
        document.getElementById("monthsInYear").style.display = "block";
        document.getElementById("yearsInDecade").style.display = "none";
        document.getElementById("daysInMonth").style.display = "none";
    };
    
    
    this.ViewDecade = function(year) {
        if (Math.floor(year / 10) % 10 > 0 && year % 10 == 0)
            year -= 10;
        
        var fromYear = (Math.floor(year / 10) * 10) + 1
        var toYear = fromYear + 9;
            
            document.getElementById("previous").innerHTML = "<a onclick='cal.ViewDecade(" + eval(fromYear - 5) + ")' style='text-decoration:none; cursor:pointer;'>&lt;&lt;</a>";
            document.getElementById("current").innerHTML = "<span id='decade'>" + fromYear + " - " + toYear + "</span>";
            document.getElementById("next").innerHTML = "<a onclick='cal.ViewDecade(" + eval(toYear + 5) + ")' style='text-decoration:none; cursor:pointer;'>&gt;&gt;</a>";
            
        var output = "<table width='200' cellpadding='0' cellspacing='0' border='0'>";
            output +="<tr align='center' height='24'>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + fromYear + ")' style='cursor:pointer;'>" + fromYear + "</a></td>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + eval(eval(fromYear)+1) + ")' style='cursor:pointer;'>" + eval(eval(fromYear)+1) + "</a></td>";
                output +="<td>&nbsp;</td>"
            output +="</tr>";
            output +="<tr align='center' height='24'>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + eval(eval(fromYear)+2) + ")' style='cursor:pointer;'>" + eval(eval(fromYear)+2) + "</a></td>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + eval(eval(fromYear)+3) + ")' style='cursor:pointer;'>" + eval(eval(fromYear)+3) + "</a></td>";
                output +="<td>&nbsp;</td>"
            output +="</tr>";
            output +="<tr align='center' height='24'>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + eval(eval(fromYear)+4) + ")' style='cursor:pointer;'>" + eval(eval(fromYear)+4) + "</a></td>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + eval(eval(fromYear)+5) + ")' style='cursor:pointer;'>" + eval(eval(fromYear)+5) + "</a></td>";
                output +="<td>&nbsp;</td>"
            output +="</tr>";
            output +="<tr align='center' height='24'>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + eval(eval(fromYear)+6) + ")' style='cursor:pointer;'>" + eval(eval(fromYear)+6) + "</a></td>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + eval(eval(fromYear)+7) + ")' style='cursor:pointer;'>" + eval(eval(fromYear)+7) + "</a></td>";
                output +="<td>&nbsp;</td>"
            output +="</tr>";
            output +="<tr align='center' height='23'>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + eval(eval(fromYear)+8) + ")' style='cursor:pointer;'>" + eval(eval(fromYear)+8) + "</a></td>";
                output +="<td>&nbsp;</td>"
                output +="<td width='40'><a onclick='cal.ViewMonths(" + toYear + ")' style='cursor:pointer;'>" + toYear + "</a></td>";
                output +="<td>&nbsp;</td>"
            output +="</tr>";
        output +="</table>";
        document.getElementById("yearsInDecade").innerHTML = output;
        document.getElementById("yearsInDecade").style.display = "block";
        document.getElementById("monthsInYear").style.display = "none";
        document.getElementById("daysInMonth").style.display = "none";
    };
    
    // Adds a marked date to the calendar
    this.AddActiveDay = function(nYear, nMonth, nDay, link) {
        var index = activeDays.length;
        activeDays[index] = new Date(nYear, nMonth, nDay);
        activeDayLinks[index] = link;
    };
    
    this.SetSelectedDate = function(nYear, nMonth, nDay) {
        document.getElementById(targetId).value = nDay + "-" + (nMonth + 1) + "-" + nYear;
        document.getElementById("Calendar").style.display = "none";
        document.getElementById(box).innerHTML = "<input id='CalendarBtn' align='abs-middle' type='image' src='/code/EventCalendar/calendaricon.gif' onclick='cal.Show(this)' />";
    };
    
    this.IsEventCalendar = function() { return isEventCalendar; };
    
    function DaysInMonth(year, month)
    {
        return 32 - new Date(year, month, 32).getDate();
    }
    
    function checkForActiveDate(date)
    {
        for (var i = 0; i < activeDays.length; i++)
        {
            if (activeDays[i].getTime() == date.getTime())
                return i;
        }
        return -1;
    }
    
    this.Show = function(sender) {
        this.Start();
        document.getElementById("Calendar").style.display = "block";
        document.getElementById("Calendar").style.position = "absolute";
    };
    
    var dateNow = new Date();
    var isEventCalendar = true;
    var targetId = "";

    var months = new Array();
    months[0] = "Januar";
    months[1] = "Februar";
    months[2] = "Marts";
    months[3] = "April";
    months[4] = "Maj";
    months[5] = "Juni";
    months[6] = "Juli";
    months[7] = "August";
    months[8] = "September";
    months[9] = "Oktober";
    months[10] = "November";
    months[11] = "December";
    
    var activeDays = new Array();
    var activeDayLinks = new Array();
    
    if (target != "")
    {
        //document.getElementById("Calendar").style.display = "none";
        isEventCalendar = false;
        targetId = target;
        document.getElementById(box).style.position = "absolute";
        document.getElementById(box).innerHTML = "<input id='CalendarBtn' type='image' src='/code/EventCalendar/calendaricon.gif' onclick='cal.Show(this)' />";
    }
    
    this.Start = function(){
        var output = "";
        output += "<table id='Calendar' width='200' cellpadding='0' cellspacing='0' border='0' style='font-family:Verdana; background-color:#FFFFFF;'>";
        if (isEventCalendar)
        {
            output += "<tr>";
                output += "<td>";
                    output += "&nbsp;";
                output += "</td>";
            output += "</tr>";
        }
            output += "<tr>";
                output += "<td>";
                    output += "<div id='calendarMenu' style='display:block;'>";
                        output += "<table width='200' border='0' cellspacing='0' cellpadding='0'>";
                            output += "<tr>";
                                output += "<td id='previous' width='22' align='right'></td>";
                                output += "<td id='current' align='center'></td>";
                                output += "<td id='next' width='22' align='right'></td>";
                            output += "</tr>";
                        output += "</table>";
                    output += "</div>";
                    output += "<div id='yearsInDecade' style='display:none;'>";
                    output += "</div>";
                    output += "<div id='monthsInYear' style='display:none;'>";
                    output += "</div>";
                    output += "<div id='daysInMonth' style='display:block;'>";
                    output += "</div>";
                    output += "<div style='display:block; margin-bottom:2px; margin-top:3px; font-weight:bold;' id='today'>";
                    output += "</div>";
                output += "</td>";
            output += "</tr>";
        output += "</table>";
        document.getElementById(box).innerHTML = output;

        var startTekst = "I dag";
        if (!isEventCalendar)
        {
            var startValue = document.getElementById(targetId).value;
            if (startValue != "")
            {
                var temp = startValue.split("-");
                dateNow.setFullYear(Number(temp[2]), Number(temp[1])-1, Number(temp[0]));
                this.ViewDays(dateNow.getFullYear(), dateNow.getMonth());
            }
            else
            {
                this.ViewDays(dateNow.getFullYear(), dateNow.getMonth());
            }
            startTekst = "Valgt";
        }
        else
        {
            this.ViewDays(dateNow.getFullYear(), dateNow.getMonth());
        }
        document.getElementById("today").innerHTML = "<a style='cursor:pointer;' onclick='cal.ViewDays(" + dateNow.getFullYear() + ", " + dateNow.getMonth() + ")'>" + startTekst + ": " + dateNow.getDate() + "-" + eval(dateNow.getMonth() + 1) + "-" + dateNow.getFullYear() + "</a><a style='color:#FFFFFF; text-decoration:none; border:solid 1px #AA0000; padding-left:17px; margin-left:1px;'>&nbsp;</a>";
    };
}

Date.prototype.getWeek = function (dowOffset) {
    /*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

    dowOffset = typeof(dowOffset) == 'number' ? dowOffset : 0; //default dowOffset to zero
    var newYear = new Date(this.getFullYear(),0,1);
    var day = newYear.getDay() - dowOffset; //the day of week the year begins on
    day = (day >= 0 ? day : day + 7);
    var daynum = Math.floor((this.getTime() - newYear.getTime() - (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
    var weeknum;
    //if the year starts before the middle of a week
    if(day < 4)
    {
        weeknum = Math.floor((daynum+day-1)/7) + 1;
        if(weeknum > 52)
        {
            nYear = new Date(this.getFullYear() + 1,0,1);
            nday = nYear.getDay() - dowOffset;
            nday = nday >= 0 ? nday : nday + 7;
            /*if the next year starts before the middle of
            the week, it is week #1 of that year*/
            weeknum = nday < 4 ? 1 : 53;
        }
    }
    else
    {
        weeknum = Math.floor((daynum+day-1)/7);
    }
    return weeknum;
};

    function getPos (elm)
    {
        for(var zx=zy=0;elm!=null;zx+=elm.offsetLeft,zy+=elm.offsetTop,elm=elm.offsetParent);
        return {x:zx,y:zy};
    }
