function calPopup(theDiv,toFrom) {
	if (document.getElementById) {
		var theContent = document.getElementById(theDiv);
		var currentVal = theContent.style.display;
	
		if (currentVal == "block") {
//			theContent.style.display = "none";
		} else {
			var today = new Date();
			calShowMonth(today.getMonth(),today.getFullYear(), toFrom, theDiv);
			theContent.style.display = "block";
		}
	} 
	return;
}

function daysInMonth(iMonth, iYear, toFrom)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function previousMonth(iMonth, iYear, toFrom, theDiv){
	var iPrevMonth = iMonth - 1;
	var iPMYear = iYear;
	if (iPrevMonth < 0){
		iPrevMonth = 11;
		iPMYear--;
	}
	return 'javascript:calShowMonth('+iPrevMonth+','+iPMYear+', \''+toFrom+'\', \'' + theDiv + '\')';
}

function nextMonth(iMonth, iYear, toFrom, theDiv){
	var iNextMonth = iMonth + 1;
	var iNMYear = iYear;
	if (iNextMonth > 11){
		iNextMonth = 0;
		iNMYear++;
	}
	return 'javascript:calShowMonth('+iNextMonth+','+iNMYear+', \''+toFrom+'\', \'' + theDiv + '\')';
}

function formatTwoChars(sInput){
	if (sInput < 10){
		return "0" + sInput;
	}
	return sInput;
}

function calShowMonth(iMonth, iYear, toFrom, theDiv){
	//calendar div
	var theContent = document.getElementById(theDiv);
	
	//number of days in this month (starting at 1)
	var iDaysInMonth = daysInMonth(iMonth,iYear,toFrom);
	
	//previous month
	var iPrevMonth = iMonth - 1;
	var iPMYear = iYear;
	if (iPrevMonth < 0){
		iPrevMonth = 11;
		iPMYear--;
	}
	var iPrevMonthDays = daysInMonth(iPrevMonth,iPMYear,toFrom);    
	
	//next month
	var iNextMonth = iMonth + 1;
	var iNMYear = iYear;
	if (iNextMonth > 11){
		iNextMonth = 0;
		iNMYear++;
	} 
	
	//today
	var today = new Date();
	
	var sTemp = "";
	
	if (((iMonth == 1) && (iYear == 1753)) == false){
//		sTemp += '<div class="calMain contain">\n';
		sTemp += '<div class="headerInfo contain">\n';
		
		sTemp += '<a href="'+previousMonth(iMonth, iYear, toFrom, theDiv)+'" class=\"floatLeft\"><img src="../img/arrowGoldBack.gif" alt="Previous month" /></a>\n';
		sTemp += '<a href="'+nextMonth(iMonth, iYear, toFrom, theDiv)+'" class=\"floatRight\"><img src="../img/arrowGold.gif" alt="Next month" /></a>\n';
	}
	switch(iMonth){
		case 0:
		sTemp += 'January ';
		break;
		
		case 1:
		sTemp += 'Febuary ';
		break;
		
		case 2:
		sTemp += 'March ';
		break;
		
		case 3:
		sTemp += 'April ';
		break;
		
		case 4:
		sTemp += 'May ';
		break;
		
		case 5:
		sTemp += 'June ';
		break;
		
		case 6:
		sTemp += 'July ';
		break;
		
		case 7:
		sTemp += 'August ';
		break;
		
		case 8:
		sTemp += 'September ';
		break;
		
		case 9:
		sTemp += 'October ';
		break;
		
		case 10:
		sTemp += 'November ';
		break;
		
		case 11:
		sTemp += 'December ';
		break;
	}
	
	sTemp += iYear + '</div>\n';
	sTemp += '\n<div class="clear"></div>\n';
	sTemp += '<table>\n';
	sTemp += '<th>S</th>\n';
	sTemp += '<th>M</th>\n';
	sTemp += '<th>T</th>\n';
	sTemp += '<th>W</th>\n';
	sTemp += '<th>T</th>\n';
	sTemp += '<th>F</th>\n';
	sTemp += '<th>S</th>\n';    
	sTemp += '</tr>\n';
	
	
	//current day of the month (starting at 1)
	var iCurrentDay = 1;
	//current day of the week (starting at 0)
	var iTempDay = new Date();
	iTempDay.setFullYear(iYear,iMonth,1);
	var iDayOfWeek = iTempDay.getDay();
	//current week of the month (starting at 1)
	var iCurrentWeek = 1;
	
	//start the first week
	sTemp += '<tr id="Week'+iCurrentWeek+'">\n';
	var iDayCountDown = iDayOfWeek;
	for (iDayCountDown = iDayOfWeek; iDayCountDown > 0; iDayCountDown--){
		sTemp += '<td class="noDay"></td>\n';        
	}
	
	//go through every day of the month
	for (iCurrentDay = iCurrentDay; iCurrentDay <= iDaysInMonth; iCurrentDay++){        
		//open the week
		if ((iDayOfWeek == 0) && (iCurrentWeek != 1)){
		   sTemp += '<tr id="Week'+iCurrentWeek+'">\n';
		}
		  
		sTemp += '<td class="day"><a href="javascript:pickDay('+iYear+","+(iMonth+1)+","+iCurrentDay+',\'' + toFrom + '\');">'+iCurrentDay+'</a></td>\n';        
		
		//close the week
		if (iDayOfWeek == 6){
			sTemp += '</tr>\n';
		}
		
		//cycle day of the week
		iDayOfWeek++;
		if (iDayOfWeek > 6){
			iCurrentWeek ++;
			iDayOfWeek = 0;
		}
	}
	
	//end the last week
	if (iDayOfWeek != 0){
		iCurrentDay = 1;
		for (iDayOfWeek = iDayOfWeek; iDayOfWeek < 7; iDayOfWeek++){
			sTemp += '<td class="noDay"></td>\n';
			iCurrentDay++;
		}
		sTemp += '</tr>\n';
	}
	
	sTemp += '</table>\n';
	
//	sTemp += '</div>\n';
//	sTemp += '<div class="closeCal"><a href=\"javascript:calPopup(\'calPopup\');\">Close</a></div>';
	
	//show the calendar
	theContent.innerHTML = sTemp;
}

function pickDay(iYear, iMonth, iDay, toFrom){
	var thisSelect = toFrom + "Date";
//	dateValue = escape(formatTwoChars(iDay)+"/"+formatTwoChars(iMonth)+"/"+iYear)	;
	dateValue = new Date(iYear, iMonth-1, iDay).getTime();
//	alert(dateValue);
	
	document.getElementById(thisSelect).value = dateValue;
	var location = new String(window.location);

	if (location.indexOf("?") != -1){
		dateOccurance = location.indexOf(toFrom+"date=");
		if (dateOccurance != -1){
			if (location.indexOf("&", dateOccurance) != -1){
				location = location.substr(0, dateOccurance) + 
					 toFrom + "date="+ dateValue + 
					location.substr(location.indexOf("&", dateOccurance), location.length - location.indexOf("&", dateOccurance));
			}else{
				location = location.substr(0, dateOccurance) +
					 toFrom + "date="+ dateValue;
			}
		}else{
			location += "&" + toFrom + "date="+dateValue;
		}
	}else{
		location += "?" + toFrom + "date="+dateValue;
	}
	window.location = location;
	

//	calPopup('calPopup',thisSelect);
}


