function calPopup(theDiv) {
	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());
			theContent.style.display = "block";
		}
	} 
	return;
}

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function previousMonth(iMonth, iYear){
    var iPrevMonth = iMonth - 1;
    var iPMYear = iYear;
    if (iPrevMonth < 0){
        iPrevMonth = 11;
        iPMYear--;
    }
    return 'javascript:calShowMonth('+iPrevMonth+','+iPMYear+')';
}

function nextMonth(iMonth, iYear){
    var iNextMonth = iMonth + 1;
    var iNMYear = iYear;
    if (iNextMonth > 11){
        iNextMonth = 0;
        iNMYear++;
    }
    return 'javascript:calShowMonth('+iNextMonth+','+iNMYear+')';
}

function formatTwoChars(sInput){
    if (sInput < 10){
        return "0" + sInput;
    }
    return sInput;
}

function calShowMonth(iMonth, iYear){
    //calendar div
    var theContent = document.getElementById('calPopup');
    
    //number of days in this month (starting at 1)
    var iDaysInMonth = daysInMonth(iMonth,iYear);
    
    //previous month
    var iPrevMonth = iMonth - 1;
    var iPMYear = iYear;
    if (iPrevMonth < 0){
        iPrevMonth = 11;
        iPMYear--;
    }
    var iPrevMonthDays = daysInMonth(iPrevMonth,iPMYear);    
    
    //next month
    var iNextMonth = iMonth + 1;
    var iNMYear = iYear;
    if (iNextMonth > 11){
        iNextMonth = 0;
        iNMYear++;
    } 
    
    //today
    var today = new Date();
    
    var sTemp = "";
    sTemp = '\n<div class="topContent contain">\n';
    sTemp += '<div class="floatLeft">\n';
    sTemp += '<a href="'+previousMonth(iMonth, iYear)+'"><img src="/img/btnLeftArrow.gif" alt="Previous month" /></a></div>\n';
    sTemp += '<div class="headerInfo">';
    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 += '<div class="floatLeft">\n';
    
    //only show the next month if it isn't in the future
    if (today.getFullYear() >= iNMYear){
        if ((today.getMonth() >= iNextMonth) | (today.getFullYear() > iNMYear)){
            sTemp += '<a href="'+nextMonth(iMonth, iYear)+'"><img src="/img/btnRightArrow.gif" alt="Next month" /></a>\n';
        }
    }
    
    sTemp += '</div></div>\n';
    sTemp += '<div class="calMain">\n';
    sTemp += '<div class="calTop">\n';
    sTemp += '<div class="calBtm">\n';
    sTemp += '<div class="calPadding contain">\n';
    sTemp += '<div class="monthSelect"><a href="default.aspx?startdate=01'+formatTwoChars(iMonth+1)+iYear+'&enddate='+iDaysInMonth+formatTwoChars(iMonth+1)+iYear+'"><strong>Select whole <span class="red">month</span></strong></a></div>\n';
    sTemp += '<div class="clear"></div>\n';
    sTemp += '<table width="280px">\n';
    sTemp += '<colgroup>\n';
    sTemp += '<col width="75px" />\n';
    sTemp += '<col width="28px" />\n';
    sTemp += '<col width="28px" />\n';
    sTemp += '<col width="28px" />\n';
    sTemp += '<col width="28px" />\n';
    sTemp += '<col width="28px" />\n';
    sTemp += '<col width="28px" />\n';
    sTemp += '<col width="28px" />\n';
    sTemp += '</colgroup>\n';
    sTemp += '<tr><th></th>\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';
    if (iDayOfWeek == 0){
        sTemp += '<td><a href="/default.aspx?startdate=01'+formatTwoChars(iMonth+1)+iYear+'&enddate=07'+formatTwoChars(iMonth+1)+iYear+'">Select <span class="red">week</span></a></td>\n'; 
    } else {
        sTemp += '<td><a href="/default.aspx?startdate='+formatTwoChars((iPrevMonthDays-iDayOfWeek+1))+formatTwoChars(iPrevMonth+1)+iPMYear+'&enddate='+formatTwoChars((7 - iDayOfWeek))+formatTwoChars(iMonth+1)+iYear+'">Select <span class="red">week</span></a></td>\n';     
    }
    var iDayCountDown = iDayOfWeek;
    for (iDayCountDown = iDayOfWeek; iDayCountDown > 0; iDayCountDown--){
        sTemp += '<td class="noDay"><a href="/default.aspx?startdate='+formatTwoChars((iPrevMonthDays-iDayCountDown + 1))+formatTwoChars(iPrevMonth+1)+iPMYear+'&enddate='+formatTwoChars((iPrevMonthDays-iDayCountDown + 1))+formatTwoChars(iPrevMonth+1)+iPMYear+'">'+(iPrevMonthDays-iDayCountDown + 1)+'</a></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';
           if ((iCurrentDay + 6) <= iDaysInMonth){
            sTemp += '<td><a href="/default.aspx?startdate='+formatTwoChars(iCurrentDay)+formatTwoChars(iMonth+1)+iYear+'&enddate='+formatTwoChars(iCurrentDay+6)+formatTwoChars(iMonth+1)+iYear+'">Select <span class="red">week</span></a></td>\n';             
           } else {
            sTemp += '<td><a href="/default.aspx?startdate='+formatTwoChars(iCurrentDay)+formatTwoChars(iMonth+1)+iYear+'&enddate='+formatTwoChars(iCurrentDay+6-iDaysInMonth)+formatTwoChars(iNextMonth+1)+iNMYear+'">Select <span class="red">week</span></a></td>\n'; 
           }
        }
         
        if ((today.getDate() >= iCurrentDay) | (today.getMonth() > iMonth) | (today.getFullYear() > iYear)){    
            sTemp += '<td class="day"><a href="/default.aspx?startdate='+formatTwoChars(iCurrentDay)+formatTwoChars(iMonth+1)+iYear+'&enddate='+formatTwoChars(iCurrentDay)+formatTwoChars(iMonth+1)+iYear+'">'+iCurrentDay+'</a></td>\n';        
        } else {
            sTemp += '<td class="dayNoClick">'+iCurrentDay+'</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++){
            if ((today.getFullYear() >= iNMYear) & ((today.getMonth() >= iNextMonth) | (today.getFullYear() > iNMYear))){
                sTemp += '<td class="noDay"><a href="/default.aspx?startdate='+formatTwoChars(iCurrentDay)+formatTwoChars(iNextMonth+1)+iNMYear+'&enddate='+formatTwoChars(iCurrentDay)+formatTwoChars(iNextMonth+1)+iNMYear+'">'+iCurrentDay+'</a></td>\n';
            } else {
                sTemp += '<td class="noDayNoClick">'+iCurrentDay+'</td>\n';
            }
            iCurrentDay++;
        }
        sTemp += '</tr>\n';
    }
    
    sTemp += '</table>\n';
    sTemp += '</div>\n';
    sTemp += '</div>\n';
    sTemp += '</div>\n';
    sTemp += '</div>\n';
    sTemp += '<div class="closeCal">\n';
    sTemp += '<a href="javascript:calPopup(\'calPopup\');">close</a></div>\n';
    
    //show the calendar
    theContent.innerHTML = sTemp;
}