function calPopup(theDiv,toFrom,left,top) {
	if (document.getElementById) {
		var theContent = document.getElementById(theDiv);
		var currentVal = theContent.style.display;
		theContent.style.left = left;
		
        theContent.style.visibility = 'hidden';		
		if (currentVal == "block") {
			theContent.style.display = "none";
		} else {
			var today = new Date();
			calShowMonth(today.getMonth(),today.getFullYear(), toFrom);
			theContent.style.display = "block";
		}
        
        if (window.ActiveXObject && !window.XMLHttpRequest && document.getElementById('searchPageSize')) {
            // fudge for ie6 so that the box won't overlap the searchPageSize select box because of the problem with
            // select boxes appearing in front of everything...
    		theContent.style.top = (document.getElementById('searchPageSize').offsetTop - (theContent.offsetHeight) - 30 ) + 'px';
		} else {
		    theContent.style.top = top;
		}
		theContent.style.visibility = 'visible';
	} 
	return;
}

function daysInMonth(iMonth, iYear, toFrom)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function previousMonth(iMonth, iYear, toFrom){
	var iPrevMonth = iMonth - 1;
	var iPMYear = iYear;
	if (iPrevMonth < 0){
		iPrevMonth = 11;
		iPMYear--;
	}
	return 'javascript:calShowMonth('+iPrevMonth+','+iPMYear+', \''+toFrom+'\')';
}

function nextMonth(iMonth, iYear, toFrom){
	var iNextMonth = iMonth + 1;
	var iNMYear = iYear;
	if (iNextMonth > 11){
		iNextMonth = 0;
		iNMYear++;
	}
	return 'javascript:calShowMonth('+iNextMonth+','+iNMYear+', \''+toFrom+'\')';
}

function previousYear(iMonth, iYear, toFrom) {
	var iPrevYear = iYear - 1;
	return 'javascript:calShowMonth(' + iMonth + ',' + iPrevYear + ',\'' + toFrom + '\')';
}

function nextYear(iMonth, iYear, toFrom) {
	var iNextYear = iYear + 1;
	return 'javascript:calShowMonth(' + iMonth + ',' + iNextYear + ',\'' + toFrom + '\')';
}

function formatTwoChars(sInput){
	if (sInput < 10){
		return "0" + sInput;
	}
	return sInput;
}

function calShowMonth(iMonth, iYear, toFrom){
	//calendar div
	var theContent = document.getElementById('calPopup');
	
	//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" dir="ltr" lang="en">\n';
		sTemp += '<div class="headerInfo contain" dir="ltr" lang="en">\n';
		
		sTemp += '<a href="'+previousMonth(iMonth, iYear, toFrom)+'" class=\"floatLeft\"><img src="../img/arrowGoldBack.gif" alt="Previous month" /></a>\n';
		sTemp += '<a href="'+nextMonth(iMonth, iYear, toFrom)+'" class=\"floatRight\"><img src="../img/arrowGold.gif" alt="Next month" /></a>\n';
		sTemp += '<span class="month">';
	}
	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 + '</span>';
	sTemp += '<a href="' + nextYear(iMonth,iYear,toFrom) + '" class="nextYear"><img src="../img/calArrowGoldUp.gif" width="9" height="5" title="Next Year" alt="Next Year" /></a>';
	sTemp += '<a href="' + previousYear(iMonth,iYear,toFrom) + '" class="prevYear"><img src="../img/calArrowGoldDwn.gif" width="9" height="5" title="Previous Year" alt="Previous Year" /></a>';
	sTemp += '</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";
	document.getElementById(thisSelect).value = formatTwoChars(iDay)+"/"+formatTwoChars(iMonth)+"/"+iYear;
	calPopup('calPopup',thisSelect,'','');
}



