


function buildCalendar() {


    month = document.theform.month.value*1;
    firstcal = month + 1;
    
    month = evalMonth(month);
    var firstmonth = month;
    
    year  = document.theform.year.value*1;

	monthName = months[month]

   	text = monthName+" "+year;
	document.getElementById('calendarhead').innerHTML=text


    NumDays=31;
    
    if (month==3||month==5||month==8||month==10) {
    	NumDays=30;
    }
    
    if (month==1) {
        NumDays=28;
        if((year%4==0)&&(year%100!=0)) {NumDays=29;}
        if(year%400==0) {NumDays=29;};
    }

	topNumDays = NumDays;

    TempDate = new Date(year, month, 1);
    FirstDay = TempDate.getDay();
    
    topFirst = FirstDay; 
    i = FirstDay + 1;
    //43  85
    for (n=1; n<43; n++) {
    	dateDiv=document.getElementById('div'+n)
    	dateDiv.innerHTML="&nbsp;"
    }
    
    
    for(c=1;c<=NumDays;c++) {
        
        dateDiv=document.getElementById('div'+i)
        
        if (dateDiv==null) alert(dateDiv);
        
        
        for (j=0; j<posts2.length; j++) {
        
			if (c==posts2[j]) {
			dateDiv.innerHTML="<a href='#"+firstcal+c+"' onclick='getDateBlock(\"div"+i+"\",\""+c+"\")' style='color:#C74F00' class='date' id=link"+c+"><b>"+c+"<\/b><\/a>";
			break;
			}
			
			else
			dateDiv.innerHTML="<span class='date' id=link"+c+">"+c+"<\/span>";
		}
	
		
        
        i++;
    }
	
	
	
	
	
	month = document.theform.month.value*1;
	secondcal = month;
	month = month-1;
	
	month = evalMonth(month)
	
	year  = document.theform.year.value*1;
	if (firstmonth==0) year=year-1;

	monthName = months[month]

   	text = monthName+" "+year;
	document.getElementById('calendarhead2').innerHTML=text
		
	
	
	    NumDays=31;
    if (month==3||month==5||month==8||month==10) {
    	NumDays=30;
    }
    
    if (month==1) {
        NumDays=28;
        if((year%4==0)&&(year%100!=0)) {NumDays=29;}
        if(year%400==0) {NumDays=29;};
    }

    TempDate = new Date(year, month, 1);
    FirstDay = TempDate.getDay();
    i = 42 + FirstDay + 1;
    //43  85
    for (n=43; n<85; n++) {
    	dateDiv=document.getElementById('div'+n)
    	dateDiv.innerHTML="&nbsp;"
    }

	
	
	

  	for(c=1;c<=NumDays;c++) {
        
        dateDiv=document.getElementById('div'+i)
        
        if (dateDiv==null) alert(dateDiv);
        
        
        
        for (j=0; j<posts1.length; j++) {
        
			if (c==posts1[j]) {
			dateDiv.innerHTML="<a href='#"+secondcal+c+"' onclick='getDateBlock(\"div"+i+"\",\""+c+"\")' style='color:#C74F00' class='date' id=link"+c+"><b>"+c+"<\/b><\/a>";
			break;
			}
			
			else
			dateDiv.innerHTML="<span class='date' id=link"+c+">"+c+"<\/span>";
	

		}
        
        i++;
    }



    // cleanup extra calendar row
    
    if ((topFirst==5 && topNumDays==31) || topFirst==6) {
    	if (ie5 || ns6) {
    		document.getElementById('calspacer').style.display="block";
    		//alert('add');
    	}
    }
    else {
    	 if (ie5 || ns6) {
    		document.getElementById('calspacer').style.display="none";
    		//alert('dont add, ' + topFirst + ", " + topNumDays);
    	}
    }



}

function evalMonth(month) {
	month=month*1
    if (month<0) {
    	newmonth=month-(-144)
    	newmonth=newmonth%12
    }
    
    else if (month>11) {
    	newmonth=month%12;
	}
	else newmonth=month;
	return newmonth;
}



var startMonthFloater;
var endMonthFloater;

var startFloater;
var endFloater;

var months=new Array("January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var month=8
var year=2002


function getDiv(date) {
	if (document.getElementById('link'+date).parentNode) {
		return document.getElementById('link'+date).parentNode.id
	}
}


function clearBlocks(blocks) {
curMonth=document.theform.month.value*1
for (i=1; i<43; i++) {
	theDiv=document.getElementById('div'+i);
	theDiv.style.backgroundColor="";
	theDiv.style.padding="1px";
}

if (startMonthFloater==curMonth) {
	theDiv=document.getElementById(getDiv(startFloater));
	theDiv.style.backgroundColor="#F1F1E7";
	theDiv.style.padding="1px";
}

if (endMonthFloater==curMonth) {
	theDiv=document.getElementById(getDiv(endFloater));
	//theDiv.style.backgroundColor="#F1F1E7";
	//theDiv.style.color="#C74F00";
	theDiv.style.padding="1px";
}

} 


function getStartBlock(date) {
startFloater=date;

newdate=months[evalMonth(document.theform.month.value)]+" "+date+", "+document.theform.year.value
document.theform.startClientDate.value=(evalMonth(document.theform.month.value)*1+1)+"/"+date+"/"+document.theform.year.value;

startMonthFloater=document.theform.month.value*1;
start=document.getElementById('startdate');

//start.innerHTML=newdate;

clearBlocks();
}


function getEndBlock(date) {
endFloater=date;

newdate=months[evalMonth(document.theform.month.value)]+" "+date+", "+document.theform.year.value

document.theform.endClientDate.value=(evalMonth(document.theform.month.value)*1+1)+"/"+date+"/"+document.theform.year.value;

endMonthFloater=document.theform.month.value*1;
end=document.getElementById('enddate');

end.innerHTML=newdate;

clearBlocks();
}



function prevMonth() {
	nextGet=null;
	
	thisMonth=document.theform.month.value;
	thisYear=document.theform.year.value;
	newYear=thisYear;
	newMonth=thisMonth-1;
	
	
	if (evalMonth(thisMonth)==0) newYear=thisYear-1;
	
	
	document.theform.month.value=newMonth;
	document.theform.year.value=newYear	

	clearBlocks();

	buildCalendar()
	
	if (newMonth==startMonthFloater) {
		getStartBlock(startFloater)
	}

	if (newMonth==endMonthFloater) {
		getEndBlock(endFloater)
	}	
	
	//getRange();
}

function nextMonth() {
	nextGet=null;


	thisMonth=document.theform.month.value;
	thisYear=document.theform.year.value;
	newYear=thisYear;
	
	newMonth=thisMonth-(-1);
	
	
	if (evalMonth(thisMonth)==11) newYear=thisYear-(-1);
	
	
	document.theform.month.value=newMonth;
	document.theform.year.value=newYear

	clearBlocks();

	buildCalendar()
	
	if (newMonth==startMonthFloater) {
		getStartBlock(startFloater)
	}

	if (newMonth==endMonthFloater) {
		getEndBlock(endFloater)
	}	
	
	//getRange();	
	

}


var nextGet=null;
var switchBlock=null;

function getDateBlock(div,num) {

/*
numStart=startFloater*1;
numEnd=endFloater*1;
num=num*1


curMonth=document.theform.month.value*1
curYear=document.theform.year.value*1


if (curMonth==startMonthFloater && curMonth==endMonthFloater) {
	if (num<startFloater) {
		getStartBlock(num);
	}
	
	else if (num>numStart && num<numEnd) {
		st=Math.abs(num-startFloater);
		en=Math.abs(num-endFloater);
		if (Math.abs(st-en) > 0) {
			if (nextGet==null) {
				if (st<en) {
					getStartBlock(num);
					nextGet="end"
				}
				else {
					getEndBlock(num);
					nextGet="start"
				}
			}
			else {
				nextGet=="start" ? getStartBlock(num):getEndBlock(num);
				nextGet=null;
			}
		}
		else { 
			getStartBlock(num)
			getEndBlock(num)
		}
	}
	
	else if (num>endFloater) {
		getEndBlock(num);
	}
}

else if (startMonthFloater<curMonth && endMonthFloater<curMonth) {
	getEndBlock(num)
}

else if (startMonthFloater>curMonth && endMonthFloater>curMonth) {
	getStartBlock(num)
}

else if (startMonthFloater<curMonth && endMonthFloater==curMonth) {
	if (num<endFloater) {
		getStartBlock(num);
	}	
	else {
		getEndBlock(num);
	}

}

else if (startMonthFloater==curMonth && endMonthFloater>curMonth) {	
	if (num>startFloater) {
		getEndBlock(num);
	}	
	else {
		getStartBlock(num);
	}
}


else if (startMonthFloater<curMonth && endMonthFloater>curMonth) {
	getStartBlock(num)
	getEndBlock(num)
}


getRange();
*/
}


function getRange() {
divStart=getDiv(startFloater);
divEnd=getDiv(endFloater);

curMonth=document.theform.month.value*1;

if (startMonthFloater==endMonthFloater && curMonth==startMonthFloater) {
	divStart=getDiv(startFloater);
	divEnd=getDiv(endFloater);
}
	
else if (startMonthFloater<curMonth && curMonth==endMonthFloater) {
	divStart='div0';
	divEnd=getDiv(endFloater);		
}

else if (startMonthFloater==curMonth && curMonth<endMonthFloater) {
	divStart=getDiv(startFloater);
	divEnd='div43';

}

else if (startMonthFloater<curMonth && endMonthFloater>curMonth) {
	divStart='div0';
	divEnd='div43';
}

divStart=divStart.substring(3,divStart.length)*1;
divEnd=divEnd.substring(3,divEnd.length)*1;

if (startMonthFloater<=curMonth && endMonthFloater>=curMonth) {
	for(i=divStart-(-1); i<divEnd; i++) {
		theDiv=document.getElementById('div'+i);
		//theDiv.style.backgroundColor="#DDD";
	}
}

}



function calendar() {

var today=new Date()

var thisday = today.getDate();
var thismonth = today.getMonth();
var thisyear = today.getFullYear();

monthFloater=thismonth;


document.theform.month.value=monthFloater
document.theform.year.value=thisyear
/*
document.theform.startClientDate.value=thisday+"/"+thismonth+"/"+thisyear;
document.theform.endClientDate.value=thisday+"/"+thismonth+"/"+thisyear;
*/

buildCalendar();

todayslink=document.getElementById('link'+thisday).parentNode.id

startFloater=todayslink;
//endFloater=todayslink;

getStartBlock(thisday);

//getEndBlock(thisday);

}