/**
	This script contains all of the logic used for the presentation manipulation of the dateForm tag.
	@author Ellery Crane

*/

function setTimeClass() {
    var timeInputFieldClasses = "";
    if(document.getElementById("all-day-checkbox").checked)
    {
        timeInputFieldClasses = "all-day ";
    }
    if(!document.getElementById("multi-day-checkbox").checked)
    {
        timeInputFieldClasses += "single-day";
    }
    if(document.getElementById("all-day-checkbox").checked &&
            !document.getElementById("multi-day-checkbox").checked)
    {
        document.getElementById("toLabel").className = "no-end";
    }
    else
    {
        document.getElementById("toLabel").className = "";
    }

    document.getElementById("timeInputFields").className = timeInputFieldClasses;
}

/*
This function will change the class of the 'repeatDetails' div to the value of whatever is currently
in the 'RepeatOptions' combo box. All of the hiding/showing of form elements should be handled by the
css class.
*/
function updateRepeatInput()
{
    updatePeriodType();
    document.getElementById("repeatingEventInput").className = document.getElementById("RepeatOptions").value;
    updateSummary();
}


/*
This function updates the time period label for the repeat interval. So, when the user
selects "weekly" as the repeat option, this is the function that is used to change the label
to "weeks" instead of days/months/etc. This label appears next to the drop down box determining
the number of periods in between repeats. (3 weeks, 2 days, etc)
*/
function updatePeriodType()
{
    var periodType = "";
    switch (document.getElementById("RepeatOptions").value)
            {
        case "dailyRepeat":
            periodType = "day";
            break;
        case "weeklyRepeat":
            periodType = "week";
            break;
        case "monthlyRepeat":
            periodType = "month";
            break;
        case "yearlyRepeat":
            periodType = "year";
            break;
    }
    if (document.getElementById("periodSelectBox").value > 1)
    {
        periodType += "s";
    }
    document.getElementById("periodType").innerHTML = periodType;
}

/*
Shows the until input field and updates the summary if it's not empty. Note that there is no
input checking; malformed data in the until text field will break the summary.
*/
function showUntilField() {
    if (document.getElementById("untilDate").value == "")
    {
        document.getElementById("untilDate").value = getCurrentDate();
    }
    document.getElementById("untilDate").className = "showUntil";
    updateSummary();
}

function getCurrentDate() {
    var currentTime = new Date();
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();
    var dateString = month + "/" + day + "/" + year;
    return dateString;

}

/*
Hides the until text input field and updates the summary to reflect that the repeat doesn't end.
*/
function hideUntilField() {
    document.getElementById("untilDate").className = "neverEnd";
    updateSummary();
}


function updateSummary()
{
    switch (document.getElementById("RepeatOptions").value)
            {
        case "dailyRepeat":
            updateDailySummary()
            break;
        case "weeklyRepeat":
            updateWeeklySummary()
            break;
        case "monthlyRepeat":
            updateMonthlySummary()
            break;
        case "yearlyRepeat":
            updateYearlySummary()
            break;
    }
    updatePeriodType();
}

function updateDailySummary()
{
    var numDays = document.getElementById("periodSelectBox").value;
    var periodSummary = makeDailySummary(numDays);
    createAndSetSummaryText(periodSummary);

}

function makeDailySummary(numDays)
{
    var summaryString = "";
    if (numDays == "1")
    {
        summaryString += "Daily"
    }
    else
    {
        summaryString += "Every " + numDays + " Days"
    }
    return summaryString;
}

function createAndSetSummaryText(periodSpecificSummary)
{
    var summary = makeSummaryString(periodSpecificSummary);
    document.getElementById("repeatSummary").innerHTML = summary;
    document.getElementById("summaryInput").value = summary;
}


function makeSummaryString(periodSummary)
{
    var summaryString = periodSummary;
    if (document.getElementById("untilButton").checked)
    {
        summaryString += makeUntilSummary();
    }
    return summaryString;

}

function makeUntilSummary()
{
    var untilDate = getUntilDateAsString();
    return ", until " + untilDate;
}

function getUntilDateAsString()
{
    var untilDate = document.getElementById("untilDate").value
    var montharray = new Array("January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December");
    var dateComponents = untilDate.split("/");
    var day = dateComponents[1];
    var month = montharray[dateComponents[0] - 1];
    var year = dateComponents[2];
    return "" + month + " " + day + ", " + year;
}



var weekDayArray = new Array("Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday");

function updateWeeklySummary()
{
    var numWeeks = document.getElementById("periodSelectBox").value;
    var weekDayCheckBox = document.getElementsByName("weekDayCheckBox");
    var numChecked = 0;
    var checkedDays = new Array();
    for (var i = 0; i < weekDayCheckBox.length; i++)
    {
        if (weekDayCheckBox[i].checked)
        {
            numChecked++;
            checkedDays.push(weekDayCheckBox[i].value);
        }
    }
    if (numChecked == 0)
    {
        checkedDays.push(weekDayArray[new Date().getDay()]);
    }

    var periodSummary = makeWeeklySummary(numWeeks, numChecked, checkedDays);
    createAndSetSummaryText(periodSummary);
}

function makeWeeklySummary(numWeeks, numChecked, checkedDays)
{

    var summaryString = "";
    if (numWeeks == 1)
    {
        summaryString += "Weekly";
    }
    else
    {
        summaryString += "Every " + numWeeks + " weeks";
    }

    summaryString += " on ";

    if (numChecked == 7)
    {
        summaryString += "all days";
    }
    else
    {
        for (var i = 0; i < checkedDays.length; i++)
        {
            summaryString += checkedDays[i];
            if (i != checkedDays.length - 1)
            {
                summaryString += ", ";
            }
        }
    }
    return summaryString;
}

function updateMonthlySummary()
{
    var numMonths = document.getElementById("periodSelectBox").value;
    var periodSummary = makeMonthlySummary(numMonths);
    createAndSetSummaryText(periodSummary);
}

function makeMonthlySummary(numMonths)
{
    var summaryString = "";
    if (numMonths == 1)
    {
        summaryString += "Monthly";
    }
    else
    {
        summaryString += "Every " + numMonths + " months";
    }

    summaryString += " on ";

    var currentTime = new Date();
    var currentDay = currentTime.getDate();
    var currentWeekDay = currentTime.getDay();

    if (document.getElementById("dayOfTheWeek").checked)
    {
        summaryString += monthlyDayOfTheWeekSummary(currentDay, currentWeekDay);
    }
    else
    {
        summaryString += "day " + currentDay;
    }
    return summaryString;
}

function monthlyDayOfTheWeekSummary(currentDay, currentWeekDay)
{
    var weekDayString = weekDayArray[currentWeekDay];

    var monthlyOccurrance = "--";
    if (currentDay >= 1 && currentDay <= 7)
    {
        monthlyOccurrance = "first";
    }
    else if (currentDay >= 8 && currentDay <= 14)
    {
        monthlyOccurrance = "second";
    }
    else if (currentDay >= 15 && currentDay <= 21)
    {
        monthlyOccurrance = "third";
    }
    else if (currentDay >= 22 && currentDay <= 28)
    {
        monthlyOccurrance = "fourth";
    }
    else if (currentDay > 28)
    {
        monthlyOccurrance = "last";
    }

    var summaryString = "the " + monthlyOccurrance + " " + weekDayString;
    return summaryString;
}

function updateYearlySummary()
{
    var numYears = document.getElementById("periodSelectBox").value;
    var periodSummary = makeYearlySummary(numYears);
    createAndSetSummaryText(periodSummary);
}

function makeYearlySummary(numYears)
{
    var summaryString = "";
    if (numYears == "1")
    {
        summaryString += "Annually"
    }
    else
    {
        summaryString += "Every " + numYears + " Years"
    }
    return summaryString;
}

/*
This method hides the normal inputs for the event form and shows a plain textfield where
users can input a custom description.
*/
function showCustomSchedule()
{
	var summaryInput = document.getElementById("summaryInput");
	summaryInput.readOnly = false; 
	summaryInput.value = "";	
	var startTime = document.getElementById("beginTime_Time");
	var endTime = document.getElementById("endTime_Time");
	var startDate = document.getElementById("start_Date");	
	var endDate = document.getElementById("end_Date");
	
	startTime.value = "";
	endTime.value = "";
	startDate.value = "";
	endDate.value = "";
	
	var startField = document.getElementById("ad.dateRange.beginDate");
	var beginTimeField =  document.getElementById("ad.dateRange.beginTime");
	var endField =  document.getElementById("ad.dateRange.endDate");
	var endTimeField =  document.getElementById("ad.dateRange.endTime");
	
	startField.value = "";
	beginTimeField.value = "";
	endField.value = "";
	endTimeField.value = "";
	
	document.getElementById("dateForm").className = "show-custom-schedule"; 
}

/*
This is the reverse of the showCustomSchedule. It restores the normal event form, hiding the custom
description field.
*/
function showNormalSchedule()
{
	document.getElementById("dateForm").className = ""; 
	setTimeout(function() {
	var summaryInput = document.getElementById("summaryInput");
		summaryInput.readOnly = true; 
	 	summaryInput.value = document.getElementById("repeatSummary").innerHTML; }, 50);

}
