var sameDates = true;

function updateEndDate(){
	var startIsLater = false;
	var startDayVal = parseInt(startDaySelect.value);
	var startMonthVal = parseInt(startMonthSelect.value);
	var startYearVal = parseInt(startYearSelect.value);
	var endDayVal = parseInt(endDaySelect.value);
	var endMonthVal = parseInt(endMonthSelect.value);
	var endYearVal = parseInt(endYearSelect.value);

	if(startYearVal>endYearVal){
		startIsLater = true;
	}else if(startYearVal==endYearVal){
		if(startMonthVal>endMonthVal){
			startIsLater = true;
		}else if(startMonthVal==endMonthVal){
			if(startDayVal>endDayVal){
				startIsLater = true;
			}else if(startDayVal==endDayVal){
				sameDates = true;
			}else{
				sameDates = false;
			}
		}else{
			sameDates = false;
		}
	}else{
		sameDates = false;
	}

	if(startIsLater){
		endYearSelect.options[startYearSelect.selectedIndex].selected=true;
		endMonthSelect.options[startMonthSelect.selectedIndex].selected=true;
		endDaySelect.options[startDaySelect.selectedIndex].selected=true;
		getDaysForMonth("end");
		sameDates = true;
	}
	
}


 function updateEndTime(){
	if(sameDates){
		var startIsLater = false;
		var startHourVal = parseInt(startHourSelect.value);
		var startMinuteVal = parseInt(startMinuteSelect.value);
		var endHourVal = parseInt(endHourSelect.value);
		var endMinuteVal = parseInt(endMinuteSelect.value);

		if(startHourVal>endHourVal){
			startIsLater = true;
		}else if(startHourVal==endHourVal){
			if(startMinuteVal>endMinuteVal){
				startIsLater = true;
			}
		}
	}

	if(startIsLater){
		endHourSelect.options[startHourSelect.selectedIndex].selected=true;
		endMinuteSelect.options[startMinuteSelect.selectedIndex].selected=true;
	}
}

function checkForLeapYear(selectType){
	eval("var yearSelect = " + selectType + "YearSelect;");
	eval("var monthSelect = " + selectType + "MonthSelect;");
	if(yearSelect.value/4==Math.round(yearSelect.value/4)){
		if(monthSelect.value=="2"){
			getDaysForMonth(selectType);
		}
	}
}

function getDaysForMonth(selectType){
	eval("var daySelect = " + selectType + "DaySelect;");
	eval("var monthSelect = " + selectType + "MonthSelect;");
	eval("var yearSelect = " + selectType + "YearSelect;");

	var daysInMonth = 0;
	
	var monthsWith31Days = new Array("1","3","5","7","8","10","12");
	var monthsWith30Days = new Array("4","6","9","11");

	switch(true){
		case monthsWith30Days.inArray(monthSelect.value):
			daysInMonth = 30;
			break;
		case monthsWith31Days.inArray(monthSelect.value):
			daysInMonth = 31;
			break;
		case monthSelect.value == "2":
			if(Math.round(yearSelect.value/4)==yearSelect.value/4){
				daysInMonth = 29;
			}else{
				daysInMonth = 28;
			}
			break;
	}

	var selectedDay = parseInt(daySelect.value);
	//first clear the current list //
	 if(daySelect.hasChildNodes()){
		 while (daySelect.hasChildNodes()) daySelect.removeChild(daySelect.lastChild);
	}
	for(x=1;x<=daysInMonth;x++){
		optionItem = document.createElement("option");
		optionItem.setAttribute("value", x);
		if(x==selectedDay)optionItem.setAttribute("selected", "true");
		textNode = document.createTextNode(x);	
		optionItem.appendChild(textNode);
		daySelect.appendChild(optionItem);
	}
}
