function isDate (year, month, day) {
	// month argument must be in the range 1 - 12
	month = month - 1; // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	if ( (getYear(tempDate.getYear()) == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate()) )
		return true;
	else
		return false
}


function getDays(month, year) {
  // create array to hold number of days in each month
	var ar = new Array(12);
	ar[1] = 31; // January
	ar[2] = (isLeapYear(year)) ? 29 : 28; // February
	ar[3] = 31; // March
	ar[4] = 30; // April
	ar[5] = 31; // May
	ar[6] = 30; // June
	ar[7] = 31; // July
	ar[8] = 31; // August
	ar[9] = 30; // September
	ar[10] = 31; // October
	ar[11] = 30; // November
	ar[12] = 31; // December
  return ar[month];
}

function getDayOfWeek(dow) {
	// create array to hold number of days in each month
	var ar = new Array(6);
	ar[0] = "Sun" 
	ar[1] = "Mon"
	ar[2] = "Tue"
	ar[3] = "Wed"
	ar[4] = "Thu"
	ar[5] = "Fri"
	ar[6] = "Sat"
	 
	return ar[dow];
}

function isCheckinDay(dow){
	switch(dow){
		case 0: return true; break 
		case 1: return true; break 
		case 2: return true; break 
		case 3: return true; break 
		case 4: return true; break 
		case 5: return true; break 
		case 6: return true; break 
		default: return false;
	}
}

function getDayType(day) {
	// create array to hold number of days in each month
	switch(day) {
		case 1 :   return "st"; break
		case 21 :   return "st"; break
		case 31 :   return "st"; break
		case 2 :   return "nd"; break
		case 22 :   return "nd"; break
		case 3 :   return "rd"; break
		case 23:   return "rd"; break
		default:    return "th";         
	}
}

function MinDays(){
	return 6
}

function DurationMultiples(){
	return 1
}

 function ListRooms() {
	if(getObject('Rooms')){
	qty = getObject('Rooms').value
	if(qty >=1){
			for (i=2;i<=5;i++) {
				getObject('Room_'+i).style.display = "none";
			}  
			for (i=2;i<=qty;i++) {
				getObject('Room_'+i).style.display = "block";
			}
		}
	}
}

function PopulateCheckinDate() {
	if(getObject('DeptMonthY')){
		getTheDay(getObject('DeptMonthY').value)
	}
}

function isLeapYear(datea)
{
	datea = parseInt(datea);
	if(datea%4 == 0)
	{
		if(datea%100 != 0)
		{
			return true;
		}
		else
		{
			if(datea%400 == 0)
				return true;
			else
				return false;
		}
	}
	return false;
}

function getTheDay(entry)
{
	out = ","
	pos = entry.indexOf(out);
	month = entry.substring(0,pos)
	year = entry.substring((pos + out.length), entry.length)

	daysInMonth = getDays(month, year)
	
	minDuration = MinDays()
	durMultiples = DurationMultiples()

	resetDays('DeptDay')
	index_value=0
	
	today = new Date()

	for (dm = 1; dm <= daysInMonth; dm++){
		var tempDate = new Date();
		tempDate.setYear(year);
		tempDate.setMonth(month-1);
		tempDate.setDate(dm);
	
		if (isCheckinDay(tempDate.getDay()) && (tempDate >= today)){
			AddOption('DeptDay',getDayOfWeek(tempDate.getDay()) + ' ' + dm + getDayType(dm),dm,index_value)
			index_value ++
		}
	}

	index_value=0
	for (dm = 1; dm <= 4; dm++){
		if(dm>1){minDuration = minDuration + durMultiples}
		AddOption('dtNights',minDuration,minDuration,dm-1)
	}
	
	if(durMultiples==1){
		for (dm = 5; dm <= 21; dm++){
			minDuration = minDuration + durMultiples		
			if (minDuration <=21){AddOption('dtNights',minDuration,minDuration,dm-1)}
		}
	}

	//resetDays('DtNights')
}

function AddOption(menu,name,value,index_value){
	getObject(menu).options[index_value] = new Option(name,value)
}


function RemoveOption(menu,name,value,index_value){
	getObject(menu).options[index_value] = null
}

function resetDays(y){
	x = getObject(y).length
	rl = 0

	for (rl = 0; rl <x; rl++){
		var cl = x - rl
		RemoveOption(y,"","",cl)
	}
	//AddOption(y,"Choose Month","",0)
}
