// JavaScript Document

//returns a date object with the specified restrictions.
function mktime(hour,mins,sec,month,day,year){
	var myDate = new Date();
	myDate.setDate(day);
	myDate.setMonth(month);
	myDate.setYear(year);
	//myDate.setHours(hour);
	//myDate.setMinutes(mins);
	//myDate.setSeconds(sec);
	myDate.setFullYear(year,month-1,day);
	//alert(hour+":"+mins+":"+sec+" "+month+","+day+","+year)
	return myDate;
	
}

function get_reserve_hours(myDate){
	var DoW = myDate.getDay();

	if(DoW == 0){ // sunday
		return new Array(45000,84600);
	}
	else if(DoW > 0 && DoW< 5){
		return new Array(37800,84600);
	}
	else if(DoW == 5){
		return new Array(37800,59400);
	}
	else{
		return new Array(0,0);	
	}
	
}
function get_date(year,mon,day){
	//alert(year + "-" + mon + "-" + day);
	var date = mktime(1,1,1,mon,day,year);
	var times = get_reserve_hours(date);
//	alert(times[0] + " " + times[1]);
	set_hours(times[0],times[1]);
	}
function set_hours(start_time,end_time){
		var hours = document.getElementById('hours');
		hours.options.length = 0;
		var count = 0;
		if(start_time != 0){	
			for(var i = start_time;i<=end_time;i+=30*60){
					
					var hour = Math.floor((i/43200)*12);
					if(hour > 12){
						hour -= 12;
					}
					var minute = (((i/43200)*12 - Math.floor((i/43200)*12))*60).toString();;
					if(minute == "0"){
						minute = "00";	
					}
					hours.options[count] = new Option(hour + ":" + minute,i);
					count += 1;
			}
		}
		else{
			document.getElementById('length').options.length = 0;	
		}
		update_length(hours);
}
function update_length(sel){
	var len = document.getElementById('length');
	len.options.length = 0;
	var count = 4;
	if(sel.selectedIndex+1 == sel.options.length){
		count = 1;
	}
	if(sel.selectedIndex+1 == sel.options.length-1){
		count = 2;
	}
	if(sel.selectedIndex+1 == sel.options.length-2){
		count = 3;
	}
	for(i = 1; i <= count; i++){
		len.options[i-1] = new Option(i*(.5),i*(.5)*60*60);
			
	}
}