fullMonth = ["January","February","March","April","May","June","July","August","September","October","November","December"];
today = new Date();
curntDate = today;


function setUp() {
	firstDate = new Date(curntDate.getFullYear(),curntDate.getMonth(),1);
	lastMonth = new Date(curntDate.getFullYear(),curntDate.getMonth()-1,1);
	nextMonth = new Date(curntDate.getFullYear(),curntDate.getMonth()+1,1);
	offSet   = firstDate.getDay() + 1;
	lastSlot = offSet + ((nextMonth - firstDate)/(3600*24*1000)) - 1;
	lastSlot = Math.round(lastSlot);
	calWeeks = Math.ceil(lastSlot/7);
	calDays  = calWeeks * 7;
	day = 1;
	
	tm = today.getMonth()+1;
	ty = today.getYear();
	if (ty<1900) {ty+=1900};
}

function goLast(boxMe,formMe) {
	curntDate = lastMonth;
	setUp();
	printCalendar(boxMe,formMe);
}

function goNext(boxMe,formMe) {
	curntDate = nextMonth;
	setUp();
	printCalendar(boxMe,formMe);
}

function printCalendar(boxMe,formMe) {
	setUp();
cM = curntDate.getMonth()+1;
cY = curntDate.getYear();
if (cY<1900) {cY+=1900};
eM = tm-1;
if (eM<1) {eM=12};
eY = ty;

pCal="";
pCal += "<table border=0 cellpadding=0 cellspacing=0>";

if ((cM==tm) && (cY==ty)) {
	pCal += "<tr><td class=chead>&nbsp;</td>";
	}else{
	pCal += "<tr><td class=chead><a href='javascript:;' onclick=goLast('"+boxMe+"','"+formMe+"')>&lt;</a></td>";
	}

pCal += "<td class=chead colspan=5 align=center><nobr>" + fullMonth[curntDate.getMonth()] + " " + curntDate.getFullYear() +"</nobr></td>";

if ((cM==eM) && (cY==eY)) {
	pCal += "<td class=chead>&nbsp;</td></tr>";
	}else{
	pCal += "<td class=chead><a href='javascript:;' onclick=goNext('"+boxMe+"','"+formMe+"')>&gt;</a></td></tr>";
	}

pCal += "<tr ><td class=cday>Sun</td><td class=cday>Mon</td><td class=cday>Tue</td><td class=cday>Wed</td><td class=cday>Thu</td><td class=cday>Fri</td><td class=cday>Sat</td></tr><tr align=center>";

	for (i=1; i<=calDays; i++) {
	if (i >= offSet && i <= lastSlot) {
		if (curntDate.getFullYear() > today.getFullYear() || (curntDate.getFullYear()==today.getFullYear() && curntDate.getMonth() > today.getMonth()) || (curntDate.getFullYear()==today.getFullYear() && curntDate.getMonth()==today.getMonth() && day >= today.getDate())){
			pCal += "<td class=cdate><a href='javascript:;' onclick=return_date(" + day + ",'"+ formMe +"','"+boxMe+"')>" + day + "</a></td>";
		}else{
			pCal += "<td class=nondate>" + day + "</td>";
		}
		day++;
	} else {
		pCal += "<td class=noday>&nbsp;</td>";
	}

	if (i%7 == 0) {
		pCal += "</tr>";
		}
	}
	
pCal += "</tr></table>";
document.getElementById(boxMe).innerHTML=pCal;

}


function return_date(z,formMe,boxMe) {
	
	// redo month and year selection
	newMY = ""+cM+","+cY;
	for(i=0; i<12; i++) {
		check = document.forms[formMe].arrivalMonthYear.options[i].value;
		if (check==newMY) {
			document.forms[formMe].arrivalMonthYear.selectedIndex = i;
		}
	}
	
	// redo dates
    maxDays = daysInMonth(cM,cY);
    document.forms[formMe].arrivalDate.length=0;
    for(i=1; i<=maxDays; i++) {
        document.forms[formMe].arrivalDate.options[i-1] = new Option(i,i);
    }
    
    // select right date
	document.forms[formMe].arrivalDate.selectedIndex = z-1;
	
	//close calendar
	jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
	}; 
	
	$("#"+boxMe).fadeToggle() 
}


function openCal(boxMe) {
	jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
	}; 
	
	$("#"+boxMe).fadeToggle() 
}










