var aryDays; 
var aryOcc; 
var startMonth; 

	function setBackColor(oCell)
	{
		switch(oCell.className)
		{
			case "selectedCell":
				oCell.className = "unselectedCell";
				break;
			case "unselectedCell":
				oCell.className = "selectedCell";
				break;
			case "todayCell":
				oCell.className = "todayCellSelected";
				break;
			case "todayCellSelected":
				oCell.className = "todayCell";
				break;
			case "holidayCell":
				oCell.className = "holidayCellSelected";
				break;
			case "holidayCellSelected":
				oCell.className = "holidayCell";
				break;
		}
	
		return;
	}
	
	function setBackHighlightColor(oCell)
	{
		if(oCell == null) return;
		
		switch(oCell.className)
		{
			case "unselectedCell":
				oCell.className = "selectedCell";
				break;
			case "todayCell":
				oCell.className = "todayCellSelected";
				break;
			case "holidayCell":
				oCell.className = "holidayCellSelected";
				break;
		}
	
	}
	
	function buildString()
	{
		
		var oTable;
		var oTBody;
		var sToSubmit = "";
		
		for(i = 0; i < maxNum; i++)
		{
			oTable = document.getElementById("CalTable" + i)
			
			for(j = 0; j < oTable.childNodes.length; j++)
			{
				if(oTable.childNodes[j].tagName == "TBODY")
				{
					oTBody = oTable.childNodes[j];
					break;
				}
			}
			for(j = 2; j < oTBody.childNodes.length; j++)
			{
				for(ii = 0; ii < oTBody.childNodes[j].childNodes.length; ii++)
				{
					if(oTBody.childNodes[j].childNodes[ii].tagName == "TD")
					{
						if(oTBody.childNodes[j].childNodes[ii].className == "selectedCell" || oTBody.childNodes[j].childNodes[ii].className == "todayCellSelected" || oTBody.childNodes[j].childNodes[ii].className == "holidayCellSelected")
						sToSubmit += oTBody.childNodes[j].childNodes[ii].id + "|"
					}
				}
			}
		}
		
		document.getElementById("submitText").value = sToSubmit;
	}
	
	function highlightCells()
	{
	
		var startYear;
		var startDay;
		var lastDayOfYear = 365;
		var currMonth;
		var counter = 0;
		
		if(aryDays.length > 0 && aryOcc.length > 0) //do we have days
		{
			for(var arrayItem = 0; arrayItem < aryDays.length; arrayItem++)
			{
				counter = 0;
				startDay = origStartDay;
				startYear = origStartYear;
				
				if(isLeapYear(startYear))
					lastDayOfYear = 366;
				else
					lastDayOfYear = 365;
					
				while(aryDays[arrayItem] != document.getElementById(startDay + "_" + startYear).getAttribute("day"))
				{
					startDay++;
					if(startDay > lastDayOfYear)
					{
						startDay = 1;
						startYear++;
					}
				}
				if(isLeapYear(startYear))
					lastDayOfYear = 366;
				else
					lastDayOfYear = 365;
					
					
				
				while( (startYear < endYear) || ((startDay < endDay) && (startYear <= endYear)) )
				{
					for(ii = 0; ii < aryOcc.length; ii++)
					{
						//Freq check here
						if(aryOcc[ii] == "Every")
						{
							setBackHighlightColor(document.getElementById(startDay + "_" + startYear))
						}
						if(aryOcc[ii] == "Every Other")
						{
							if(counter % 2 == 0)
									setBackHighlightColor(document.getElementById(startDay + "_" + startYear))
								
						}
						if(document.getElementById(startDay + "_" + startYear))
						{
							if(aryOcc[ii] == document.getElementById(startDay + "_" + startYear).getAttribute("position"))
								setBackHighlightColor(document.getElementById(startDay + "_" + startYear))
						}
						else
							break;	
							
							
			
					startDay += 7;
					if(startDay > lastDayOfYear)
					{
						startDay = 1;
						startYear++
						if(isLeapYear(startYear))
							lastDayOfYear = 366;
						else
							lastDayOfYear = 365;
						if(document.getElementById(startDay + "_" + startYear))
						{
							while(aryDays[arrayItem] != document.getElementById(startDay + "_" + startYear).getAttribute("day"))
							{
								startDay++;
							}
						}
						else
						{
							break;
						}
					}
					
					}
					
					counter++
				}
			}
			
		}
	}
	
	
	function doNothing()
	{
		return;
	}
	
	function isLeapYear(year)
	{
		return (year % 4 == 0 && ( year % 100 != 0 || year % 400 ==0 ));
	}
	
	
	function showPatternWindow()
	{
		if(document.getElementById("datePatternChooser").className == "hide")
			document.getElementById("datePatternChooser").className = "show";
		else
			document.getElementById("datePatternChooser").className = "hide";
		
		return;
	}
	
	//var lastCalIndex = 2;
	
	function GoPrevious()
	{
		if(totalNumOfCals <= 2)
			lastCalIndex = totalNumOfCals;
					
		nextButton.disabled = false;
		if(lastCalIndex >= 2)
		{
			//Hide previous
			if(document.getElementById("calCell" + (lastCalIndex - 2)))
				document.getElementById("calCell" + (lastCalIndex - 2)).className = "hideCalendar";
			if(document.getElementById("calCell" + (lastCalIndex - 1)))
				document.getElementById("calCell" + (lastCalIndex - 1)).className = "hideCalendar";
			if(document.getElementById("calCell" + (lastCalIndex)))
				document.getElementById("calCell" + lastCalIndex).className = "hideCalendar";
			lastCalIndex -= 2 //3	
			if(lastCalIndex < 2)
			{
				if(totalNumOfCals <= 2)
					lastCalIndex = totalNumOfCals
				else
					lastCalIndex = 2
			}		
				
			for(i = lastCalIndex; i >= (lastCalIndex - 1) ; i--)
			{
				document.getElementById("calCell" + (i - 1)).className = "showCalendar";
			}	
		}
		
		if(lastCalIndex == 2)
			previousButton.disabled = true;
		return
		
	}
	
		
	function GoNext()
	{	
		previousButton.disabled = false;
		
		if(document.getElementById("calCell" + (lastCalIndex + 2)))
		{
			document.getElementById("calCell" + (lastCalIndex)).className = "hideCalendar";
			document.getElementById("calCell" + (lastCalIndex - 1)).className = "hideCalendar";
			document.getElementById("calCell" + (lastCalIndex - 2)).className = "hideCalendar";
			
			document.getElementById("calCell" + (lastCalIndex)).className = "showCalendar";
			document.getElementById("calCell" + (lastCalIndex + 1)).className = "showCalendar";
			
			lastCalIndex += 2;
			if(!document.getElementById("calCell" + (lastCalIndex)))
				nextButton.disabled = true;
			return;
		}
		
		if(document.getElementById("calCell" + (lastCalIndex + 1)))
		{
			document.getElementById("calCell" + (lastCalIndex - 1)).className = "hideCalendar";
			document.getElementById("calCell" + (lastCalIndex - 2)).className = "hideCalendar";
			
			document.getElementById("calCell" + (lastCalIndex)).className = "showCalendar";
			document.getElementById("calCell" + (lastCalIndex + 1)).className = "showCalendar";
			
			lastCalIndex += 2;
			if(!document.getElementById("calCell" + (lastCalIndex + 1)))
				nextButton.disabled = true;
			return;
		}
		if(document.getElementById("calCell" + (lastCalIndex)))
		{
			document.getElementById("calCell" + (lastCalIndex - 2)).className = "hideCalendar";
			
			document.getElementById("calCell" + (lastCalIndex)).className = "showCalendar";
			
			lastCalIndex += 1;
			if(!document.getElementById("calCell" + (lastCalIndex + 1)))
				nextButton.disabled = true;
			return;
		}
			
		return
	}
	
	
	function GoToDatebyIndex(indexOf)
	{
		indexOf = parseFloat(indexOf);
		previousButton.disabled = false;
		
		if(document.getElementById("calCell" + (indexOf + 2)))
		{
			document.getElementById("calCell" + (lastCalIndex)).className = "hideCalendar";
			document.getElementById("calCell" + (lastCalIndex - 1)).className = "hideCalendar";
			document.getElementById("calCell" + (lastCalIndex - 2)).className = "hideCalendar";
			
			document.getElementById("calCell" + (indexOf)).className = "showCalendar";
			document.getElementById("calCell" + (indexOf + 1)).className = "showCalendar";
			
			lastCalIndex = (indexOf + 2);
		
			
			if(!document.getElementById("calCell" + (lastCalIndex)))
				nextButton.disabled = true;
			return;
		}
		if(document.getElementById("calCell" + (indexOf + 1)))
		{
			document.getElementById("calCell" + (lastCalIndex - 1)).className = "hideCalendar";
			document.getElementById("calCell" + (lastCalIndex - 2)).className = "hideCalendar";
			
			document.getElementById("calCell" + (indexOf)).className = "showCalendar";
			document.getElementById("calCell" + (indexOf + 1)).className = "showCalendar";
			
			lastCalIndex = (indexOf + 1);
			if(!document.getElementById("calCell" + (lastCalIndex + 1)))
				nextButton.disabled = true;
			return;
		}
		if(document.getElementById("calCell" + (indexOf)))
		{
			document.getElementById("calCell" + (lastCalIndex - 1)).className = "hideCalendar";
			document.getElementById("calCell" + (lastCalIndex - 2)).className = "hideCalendar";
			
			document.getElementById("calCell" + (indexOf)).className = "showCalendar";
			document.getElementById("calCell" + (indexOf - 1)).className = "showCalendar";
			
			lastCalIndex = indexOf;
			if(!document.getElementById("calCell" + (lastCalIndex + 1)))
				nextButton.disabled = true;
			return;
		}
			
		return
	}
	
	
	
	var aryDatesToHightlight = new Array();
	
	function LoadHighlights()
	{
		for(i = 0; i < aryDatesToHightlight.length; i++)
		{
			if(document.getElementById(aryDatesToHightlight[i]))
			{
				setBackHighlightColor(document.getElementById(aryDatesToHightlight[i]));
			}
		}
		return;	
	}
	


	
	function clearAllDates()
	{
		var oTable;
		var oTBody;
		var sToSubmit = "";
		
		for(i = 0; i < maxNum; i++)
		{
			oTable = document.getElementById("CalTable" + i)
			
			for(j = 0; j < oTable.childNodes.length; j++)
			{
				if(oTable.childNodes[j].tagName == "TBODY")
				{
					oTBody = oTable.childNodes[j];
					break;
				}
			}
			for(j = 2; j < oTBody.childNodes.length; j++)
			{
				for(ii = 0; ii < oTBody.childNodes[j].childNodes.length; ii++)
				{
					if(oTBody.childNodes[j].childNodes[ii].tagName == "TD")
					{
						if(oTBody.childNodes[j].childNodes[ii].className == "selectedCell")
							oTBody.childNodes[j].childNodes[ii].className = "unselectedCell";
						if(oTBody.childNodes[j].childNodes[ii].className == "todayCellSelected")
							oTBody.childNodes[j].childNodes[ii].className = "todayCell";
						if(oTBody.childNodes[j].childNodes[ii].className == "holidayCellSelected")
							oTBody.childNodes[j].childNodes[ii].className = "holidayCell";
					}
				}
			}
		}
	}
