// JavaScript Document


/*
Any-Month calendar script- Rob Patrick (rpatrick@mit.edu)
Script featured on and available at:
http://www.javascriptkit.com/
*/

function setToday() 
{
        var now   = new Date();
        var day   = now.getDate();
        var month = now.getMonth();
        var year  = now.getYear();
        if (year < 2000)    // Y2K Fix, Isaac Powell
        year = year + 1900; // http://onyx.idbsu.edu/~ipowell
        this.focusDay = day;
        document.calControl.month.selectedIndex = month;
        document.calControl.year.value = year;
        displayCalendar(month, year);
}

function isFourDigitYear(year) 
{
        if (year.length != 4) 
        {
                alert ("Sorry, the year must be four-digits in length.");
                document.calControl.year.select();
                document.calControl.year.focus();
        } 
        else { return true; }
}


function selectDate() 
{
        var year  = document.calControl.year.value;
        if (isFourDigitYear(year))
        {
                var day   = 0;
                var month = document.calControl.month.selectedIndex;
                displayCalendar(month, year);
        }
}

function setPreviousYear() 
{
var year  = document.calControl.year.value;
if (isFourDigitYear(year)) {
var day   = 0;
var month = document.calControl.month.selectedIndex;
year--;
document.calControl.year.value = year;
displayCalendar(month, year);
   }
}
function setPreviousMonth() 
{
        var $year  = document.calControl.$year.value;
        if (isFourDigitYear($year)) 
        {
                var $day   = 0;
                var $month = document.calControl.$month.selectedIndex;
                if ($month == 0) 
                {
                        month = 11;
                        if ($year > 1000) 
                        {
                                $year--;
                                document.calControl.$year.value = $year;
                        }
                } 
                else 
                { 
                        $month--; 
                }
                document.calControl.month.selectedIndex = $month;
                displayCalendar($month, $year);
        }
}


function setNextMonth() {
var year  = document.calControl.year.value;
if (isFourDigitYear(year)) {
var day   = 0;
var month = document.calControl.month.selectedIndex;
if (month == 11) {
month = 0;
year++;
document.calControl.year.value = year;
} else { month++; }
document.calControl.month.selectedIndex = month;
displayCalendar(month, year);
   }
}
function setNextYear() {
var year = document.calControl.year.value;
if (isFourDigitYear(year)) {
var day = 0;
var month = document.calControl.month.selectedIndex;
year++;
document.calControl.year.value = year;
displayCalendar(month, year);
   }
}



        function calendar_form($year, $month)
        {
        
        }
        
        function display_calendar() 
        {       
                $month = document.getElementById("month_selection").options[document.getElementById("month_selection").selectedIndex].value;
                $year = document.getElementById("year_selection").value;
                $venue_id = document.getElementById("venue_pulldown").options[document.getElementById("venue_pulldown").selectedIndex].value;
                
                // clear the dates form field
                document.getElementById("form_dates_list").value = "";
                
                // only execute if both month and year are set
                if (!isNaN($month) && !isNaN($year) && !isNaN($venue_id))
                {
                        $table = build_calendar($month, $year); // builds the table

                        document.getElementById("month_table").innerHTML = $table;
                        
                        // display the source pulldown and submit button too
                        document.getElementById("form_source_pulldown").style.visibility = "visible";
                        document.getElementById("submit_calendar_dates").style.visibility = "visible";
                }
        }
        
        function display_calendar_events() 
        {       
                $month = document.getElementById("month_selection").options[document.getElementById("month_selection").selectedIndex].value;
                $year = document.getElementById("year_selection").value;
               
                // clear the dates form field
                document.getElementById("form_dates_list").value = "";
                
                // only execute if both month and year are set
                if (!isNaN($month) && !isNaN($year))
                {
                        $table = build_calendar($month, $year); // builds the table

                        document.getElementById("month_table").innerHTML = $table;
                        
                        // display the source pulldown and submit button too
                        document.getElementById("form_source_pulldown").style.visibility = "visible";
                        document.getElementById("submit_calendar_dates").style.visibility = "visible";
                }
        }
        
        function build_calendar($month, $year)
        {        
                var $table = "<table class='run_calendar'>";
		$table += "<thead>";
		$table += "<tr>";
		$table += "<th>S</th>";
		$table += "<th>M</th>";
		$table += "<th>T</th>";
		$table += "<th>W</th>";
		$table += "<th>T</th>";
		$table += "<th>F</th>";
		$table += "<th>S</th>";
		$table += "</tr>";
		$table += "</thead>";
		
		$table += "<tbody>";
		$table += "<tr>";

                var $days = getDaysInMonth($month, $year); // get the days in the month
                var $firstOfMonth = new Date ($year, $month - 1, 1); 
                var $lastOfMonth = new Date ($year, $month - 1, $days);
                var $startingPos = $firstOfMonth.getDay(); // get the day of the week for the first day of the month
                var $endingPos = $lastOfMonth.getDay();
                $days += $startingPos;
                
                // get the number of days in previous month
                if ($month == 1)
                {
                        var $prev_month = 12; 
                        var $prev_year = $year - 1;
                }
                else
                {
                        var $prev_month = $month - 1;
                        var $prev_year = $year;
                }
                $days_in_prev_month = getDaysInMonth($prev_month, $year);                        
                $last_sunday = $days_in_prev_month - $startingPos;
                
                // calculate the next month
                if ($month == 12)
                {
                        var $next_month = 1;
                        var $next_year = $year + 1;
                }
                else
                {
                        var $next_month = parseInt($month) + 1;
                        var $next_year = $year;
                }                        

                for ($i = 0; $i < $startingPos; $i++) 
                {
                        if ( $i%7 == 0 ) 
                        {
                                $table += "</tr>";
                        }
                        var $date = $last_sunday + $i + 1; 
                        if ($prev_month < 10)
                        {
                                var $lead_zero_prev_month = "0" + $prev_month;
                        }
                        else
                        {
                                var $lead_zero_prev_month = $prev_month;
                        }
                        $table += "<td class='form_cal_cell_other_month'><a href='#' id='form_cal"+$prev_year+"-"+$lead_zero_prev_month+"-"+$date+"' onclick='add_date(\""+$prev_year+"-"+$lead_zero_prev_month+"-"+$date+"\"); return false;'>"+$date+"</a></td>";
                }
                for ($i = $startingPos; $i < $days; $i++) 
                {
                        if ( $i%7 == 0 ) 
                        {
                                $table += "</tr>";
                        }
                        var $date = $i-$startingPos+1;
                        if ($date < 10)
                        {
                                var $lead_zero_date = "0"+$date;
                        }
                        else
                        {
                                var $lead_zero_date = $date;
                        }
                        if ($month < 10)
                        {
                                var $lead_zero_month = "0" + $month;
                        }
                        else
                        {
                                var $lead_zero_month = $month;
                        }
                        $table += "<td class='form_cal_cell'><a href='#' id='form_cal"+$year+"-"+$lead_zero_month+"-"+$lead_zero_date+"' onclick='add_date(\""+$year+"-"+$lead_zero_month+"-"+$lead_zero_date+"\"); return false;'>"+$date+"</a></td>";
                }
                for ($i = 1; $i <= 6 - $endingPos; $i++)
                {
                         if ( $i%7 == 0 ) 
                        {
                                $table += "</tr>";
                        }
                        if ($next_month < 10)
                        {
                                var $lead_zero_next_month = "0" + $next_month;
                        }
                        else
                        {
                                var $lead_zero_next_month = $next_month;
                        }
                        $table += "<td class='form_cal_cell_other_month'><a href='#' id='form_cal"+$year+"-"+$lead_zero_next_month+"-0"+$i+"' onclick='add_date(\""+$year+"-"+$lead_zero_next_month+"-0"+$i+"\"); return false;'>"+$i+"</a></td>";                       
                }
                
                $table += "</tbody>";
                $table += "</table>";
                        
                return $table               
        }
        
        function getDaysInMonth($month, $year) 
        {
                var $days;
                if ($month==1 || $month==3 || $month==5 || $month==7 || $month==8 || $month==10 || $month==12)
                {
                        $days = 31;            
                }  
                else if ($month==4 || $month==6 || $month==9 || $month==11) 
                {
                        $days = 30;
                }
                else if ($month==2)  
                {
                        if (isLeapYear($year)) 
                        { 
                                $days = 29; 
                        }
                        else 
                        { 
                                $days = 28;
                        }
                }
                return $days;
        }
        
        function isLeapYear($year) 
        {
                if ((($year % 4)==0) && (($year % 100)!=0) || (($year % 400)==0)) 
                {
                        return true;
                } 
                else 
                { 
                        return false;
                }
        }
        
        function add_date($date)
        {
                // get the screening dates
                var $dates_list = document.getElementById("form_dates_list").value;
                
                // split it into an array
                if ($dates_list.length > 0)
                {
                        var $dates_array = $dates_list.split(",");   
                }       
                else
                {
                        var $dates_array = Array();
                }     
                
                if ($dates_list.indexOf($date) !== -1)
                {
                        // remove it
        	        for(var $i=0; $i < $dates_array.length; $i++)
                        {
        	            if($dates_array[$i] == $date)
                            {
        	                $index = $i;
        	            }
        	        }
        	        
                        // remove the element
                        $dates_array.splice($index , 1);
                        
                        // unmark the date cell as selected
                        document.getElementById("form_cal"+$date).className = " ";
                        
                }
                
                else
                {
                        // add it
                        $dates_array.push($date);
                        
                        // mark the date cell as selected
                        if (document.getElementById("venue_pulldown"))
                        {
                                document.getElementById("form_cal"+$date).className = "venue" + document.getElementById("venue_pulldown").options[document.getElementById("venue_pulldown").selectedIndex].value + " selected";
                        }
                        else
                        {
                                document.getElementById("form_cal"+$date).className = "selected";
                        }
                        
                }
                
                $dates_list = $dates_array.toString();
                
                document.getElementById("form_dates_list").value = $dates_list;
        }
        
        function color_venue_pulldown()
        {
                $venue_id = document.getElementById("venue_pulldown").options[document.getElementById("venue_pulldown").selectedIndex].value;
                
                document.getElementById("venue_pulldown").className = "venue" + $venue_id;
        }
        
        function validate_calendar_submission()
        {
                if (parseInt(document.getElementById("month_selection").options[document.getElementById("month_selection").selectedIndex].value) == NaN || parseInt(document.getElementById("year_selection").value) == NaN || document.getElementById("form_dates_list").value.length == 0)
                {
                        alert ("You must add at least one date before submitting this form.");
                        return false;
                }
                
                else if (document.getElementById("form_source_pulldown").options[document.getElementById("form_source_pulldown").selectedIndex].value.length == 0)
                {
                        alert ("You must provide a citation for this information in the source pulldown menu.");
                        return false;                 
                }
        }
        
        
        function toggle_display($id)
        {
                if (document.getElementById($id).style.display == "block")
                {
                        document.getElementById($id).style.display = "none";
                }
                else
                {
                        document.getElementById($id).style.display = "block";
                }
        }
        
        function create_citation($title, $date)
        {
                document.getElementById("form_source_citation").innerHTML = "&lt;cite&gt;" + $title + "&lt;/cite&gt;, " + $date + ".";
        }
        
	function venue_graph(venue_class)
	{
		// first turn off all bargraphs
		venue_bars = getElementsByClass("venue_bar");

		for (var n = 0; n < venue_bars.length; n++)
		{
			venue_bars[n].style.visibility = "hidden";
		}

		// next turn the specified venue back on
		for (var n = 0; n < venue_bars.length; n++)
		{
			// put the classnames in an array
			var class_string = venue_bars[n].className;
			var classes = class_string.split(" ");

			// loop through the class.  If the item has the class specified, turn its visibility back on
			for (var x = 0; x < classes.length; x++)
			{
				if (classes[x] == venue_class)
				{
					venue_bars[n].style.bottom = 0;
					venue_bars[n].style.visibility = "visible";
				}
			}
		}
		
		// display the reset bargraph text
		
		document.getElementById("reset_bargraph").style.display = "block";
	}
        
        function venue_graph_restore($type)
        {
                // get each column
                $columns = getElementsByClass("column");
              
                for (var $x = 0; $x < $columns.length; $x++)
                {
                        $bottom = 0; // initialize bottom value
                        
                        // get each venue's items
                        $venue_bars = $columns[$x].getElementsByTagName("li");
                        for (var $y = 0; $y < $venue_bars.length; $y++)
                        {
                        		// if no type is specified, restore everything
                        		// also restore if the type matches
                        		if (!$type || $venue_bars[$y].className.indexOf($type) != -1)
                        		{
                                $venue_bars[$y].style.bottom = $bottom+"px";
                                $venue_bars[$y].style.visibility = "visible";
                                $bottom += parseInt($venue_bars[$y].style.height.substring(0, $venue_bars[$y].style.height.length - 2));
                              }
                              else
                              {
                              	$venue_bars[$y].style.bottom = 0;
                                   $venue_bars[$y].style.visibility = "hidden";
						}
                        }
                }
        }        
        
	function getElementsByClass(searchClass,node,tag)
	{
		var classElements = new Array();
		if ( node == null )
			node = document;
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}        

        


