﻿function popupEvent(year, day, mon, title, descript)
{
    var win3;
    var sum ="";

    win3 = window.open("", "window3", "width=420, height=510, scrollbars=yes, resizeable=no, menubar=no, toolbar=no, title=no, location=no, status=no");
    var titleRep = title.replace(/<apos>/g,"'");
    titleRep = titleRep.replace(/<quot>/g,"\"");
    win3.document.write("<h2>" + titleRep + "</h2>");
    year = (year.indexOf(":") > 0) ? year.substring(0,year.indexOf(":")) : year;
    win3.document.write("<h3>" + mon + "/" + day + "/" + year + "</h3>");
    var rep = descript.replace(/<apos>/g,"'");
    rep = rep.replace(/<quot>/g,"\"");
    win3.document.write("<div class=\"popup\">" + rep + "</div>");
}

function preCal(cm, year)
{
    genCal(events, parseInt(cm), parseInt(year));
}

function genCal(events, curMonth, year) 
{
    var txtMonth = new Array();
    var intMonthDays = new Array();
    var arrMonth = new Array();

    txtMonth[0] = "January";
    txtMonth[1] = "February";
    txtMonth[2] = "March";
    txtMonth[3] = "April";
    txtMonth[4] = "May";
    txtMonth[5] = "June";
    txtMonth[6] = "July";
    txtMonth[7] = "August";
    txtMonth[8] = "September";
    txtMonth[9] = "October";
    txtMonth[10] = "November";
    txtMonth[11] = "December";

    intMonthDays[0] = 31;
    intMonthDays[1] = 28;
    intMonthDays[2] = 31;
    intMonthDays[3] = 30;
    intMonthDays[4] = 31;
    intMonthDays[5] = 30;
    intMonthDays[6] = 31;
    intMonthDays[7] = 31;
    intMonthDays[8] = 30;
    intMonthDays[9] = 31;
    intMonthDays[10] = 30;
    intMonthDays[11] = 31;

    arrMonth[0] = "";
    arrMonth[1] = "";
    arrMonth[2] = "";
    arrMonth[3] = "";
    arrMonth[4] = "";
    arrMonth[5] = "";
    arrMonth[6] = "";
    arrMonth[7] = "";
    arrMonth[8] = "";
    arrMonth[9] = "";
    arrMonth[10] = "";
    arrMonth[11] = "";

    for(var r=0;r<12;r++) 
        arrMonth[r] = new Array();

    var c = 6;
    var now = new Date();
    var intYear = now.getFullYear(); 
    var intMonth;

    var j;
    for ( j=0; j<intYear; j++) // counts number of days i think
    {
        c++;
        if ((j % 400==0) || ((j % 4 ==0) && (j % 100 != 0))) {
            c++;
        }
    }
    if ((intYear % 400 == 0) || ((intYear % 4 ==0) && (intYear % 100!=0)))  //for leap years
    {
        intMonthDays[1] = 29;
    }

    c = c %7;
    var title = new Array();
    var yrTemp = new Array();
    //var eventMonth = new Array();
    //var eventDay = new Array();
    //var eventYear = new Array();
    //var des = new Array();
    //var monthArray = new Object();
    //var n=0;

    var temp = new Array(); //temp array for date seperation 
    //var counter = 0;
    var monthCount, eventCount=0;
    for(m in arrMonth) {
        for(i in events){
            temp = i.split("/"); //splits the events into seperate date, title, and description

            if (m==temp[0]-1) {
                yrTemp = temp[2].split(":");
                if(yrTemp[0] == year) {  //if the year in the event array equals the current year being searched
                    if (eventCount>0) {
                        arrMonth[m][eventCount] = [yrTemp[0], temp[1], temp[0], events[i][0], events[i][1]]; // [Year, Day, Month, Title, Description]
                        eventCount++;
                    } else {
                        eventCount = 0;
                        arrMonth[m][eventCount] = [yrTemp[0], temp[1], temp[0], events[i][0], events[i][1]];
                        eventCount++;
                    }
                }
            } 
        }
        eventCount=0;
    }
    var output, id, ed, x; 
    output = "";
    var used=false;
    for(intMonth =0; intMonth<12; intMonth++)
    {
        //if(arrMonth[intMonth] != "") //Determines if the event month array has any information
        if(intMonth == curMonth)
        {   
            if(arrMonth[intMonth] != "") // mon is the year from the month
            {
                var yr = ( arrMonth[intMonth][0][0].indexOf(":") > 0) ? arrMonth[intMonth][0][0].substring(0, arrMonth[intMonth][0][0].indexOf(":")) : arrMonth[intMonth][0][0];
            } else {
                var yr = year;
            }    
            var nm = 0;
            var yrStr = "";
            if(curMonth - 1 < 0) {
                nm = 11;
                yrStr = (curMonth== 11) ? yr : yr -1;
                //year = yr;
            } else {
                nm = curMonth -1;
                yrStr = year;
            }
            output += "<div class=\"topCal\"><span class=\"prev\"><a href=\"javascript:preCal('" + nm + "','" + yrStr + "')\">\<\< Previous</a></span>";
            output += "<span class=\"month\">" + txtMonth[intMonth] + " " + year + "</span>";
            if (curMonth +1 > 11) {
                nm = 0;
                yrStr = (curMonth==0) ? year : parseInt(yr) + 1;
                //year = yr; 
            } else {
                nm = curMonth + 1;
                yrStr = year
            }
            output += "<span class=\"next\"><a href=\"javascript:preCal('" + nm + "','" + yrStr + "')\">Next \>\></a></span></div>";

            output += "<table id=\"calendar\"><tr>";
            output+="<tr><td class=\"head\">Sunday</td><td class=\"head\">Monday</td><td class=\"head\">Tuesday</td><td class=\"head\">Wednesday</td><td class=\"head\">Thursday</td><td class=\"head\">Friday</td><td class=\"head\">Saturday</td></tr>";
            
            for(id = 1; id<=c; id++) // Fills in calendar empty cells before month starts
            {
                output += "<td class=\"empty\">&nbsp</td>";
            }
            for(id = 1; id<=intMonthDays[intMonth]; id++) // Fills in Month days
            {   
                var firstPass = true;
                for(i in arrMonth[intMonth]) { // Checks events array if there any for that day
                    if((arrMonth[intMonth][i][1]==id) && (yr == year)) // If there is a match, then fill in 
                    {
                        if(firstPass == true) {
                            output+= "<td class=\"event\">" + id;
                            firstPass= false;
                        }
                        var rep = arrMonth[intMonth][i][4].replace(/'/g, "<apos>");
                        rep = rep.replace(/"/g, "<quot>");
                        var titleRep = arrMonth[intMonth][i][3].replace(/'/g, "<apos>");
                        titleRep = titleRep.replace(/"/g, "<quot>");
                        output += /*"<td>" + id +*/ "<div class=\"eTitle\"><a href=\"javascript:popupEvent('" + arrMonth[intMonth][i][0] + "','" + arrMonth[intMonth][i][1] + "','" + arrMonth[intMonth][i][2] + "','"  + titleRep + "','"+ rep + "')\">" + arrMonth[intMonth][i][3] + "</a></div>";//</td>";
                        var yy = parseInt(i)+1;
                        if((arrMonth[intMonth].length>yy) && (arrMonth[intMonth][yy][1]!=id)){
                            output+="</td>";
                        }
                        else {
                            output+= "<br>";
                        }
                        used=true;
                    }
                }
                if(used==false)
                    output += "<td>" + id + "</td>";
                else
                    used=false;
                c++;
                if(c == 7) 
                {
                    output += "</tr><tr>";
                    c = 0;
                }
            }
            if(c!=0)
            {
                for(ed = c; ed<7; ed++) 
                {
                    output += "<td class=\"empty\">&nbsp</td>";
                }
            }
            output += "</tr></table><br><br>";
        }
        else //keeps up month count for skipped months
        {
            for(id=1; id<=intMonthDays[intMonth]; id++)
            {
                c++;
                if(c==7)c=0;
            }
        }

    }

    document.getElementById("calendarTxt").innerHTML = output;
}

function getLatestEvents(events) 
{
    var count = 0;

    var txtMonth = new Array();
    txtMonth[0] = "January";
    txtMonth[1] = "February";
    txtMonth[2] = "March";
    txtMonth[3] = "April";
    txtMonth[4] = "May";
    txtMonth[5] = "June";
    txtMonth[6] = "July";
    txtMonth[7] = "August";
    txtMonth[8] = "September";
    txtMonth[9] = "October";
    txtMonth[10] = "November";
    txtMonth[11] = "December";

    for(i in events)
    {
        temp = i.split("/");
        var year = temp[2].split(":");
        var now = new Date();
        var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
        var eventDate = new Date(year[0],temp[0]-1, temp[1]);
        if (eventDate >= today) {
            if(count<10) {
                document.write("<div class=\"eventLine\"><span class=\"eventDate\">" + txtMonth[temp[0]-1] + " " + temp[1] + ", " + year[0] + "</span> - <span class=\"eventShortTitle\">" + events[i][0] + "</span></div>");
                count++;
            }
        }
    }
}

function report(events) {
    var yearEnter = parseInt(prompt("?"));
    var output = "";
    var res = new Array();
    res[0] = "What are you trying to do?!?";
    res[1] = "Danger Danger!";
    res[2] = "Go Hawks!";
    res[3] = "DC Hawkeyes Rule!";
    res[4] = "up up down down left right left right b a b a start";
    res[5] = "Order BCTs at the Sports Pub next time!";
    for(i in events)
    {
        temp = i.split("/");
        var year = temp[2].split(":");
        var eventDate = new Date(year[0],temp[0]-1, temp[1]);
        if (yearEnter == year ) {
            output += ("<div class=\"eventLine\"><span class=\"eventDate\">" + temp[0] + "/" + temp[1] + "/" + year[0] + "</span> -&nbsp<span class=\"eventShortTitle\">" + events[i][0] + "</span> -&nbsp<span class=\"des\">" + events[i][1] + "</span></div><br>");
        }
    }
    if(output.length>0) {
        var win3 = window.open("", "window3", "width=800, height=600, scrollbars=yes, resizeable=no, menubar=no, toolbar=no, title=no, location=no, status=no");
        win3.document.write(output);
    } else {
        var rand_no = Math.floor((6)*Math.random());
        alert(res[rand_no]);
    }
}
