/*****************************************
* POPUP CALENDAR CONTROL TO RETURN DATE
* Majority from Javascript.internet.com
* Remaining Design Chris Cole
* 
* Freeware license.
* Feel free to use this control royalty
* free as long as this copyright remains.
* No repackaging or sale of this control
* as a separate entity or part of a control
* package is allowed!
*****************************************/


var currentField
var calendarOpen=0;
var sFlag

monthNames = new Array(
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December");

daysInMonth = new Array(12);
	daysInMonth [0]=31;
	daysInMonth [1]=28;
	daysInMonth [2]=31;
	daysInMonth [3]=30;
	daysInMonth [4]=31;
	daysInMonth [5]=30;
	daysInMonth [6]=31;
	daysInMonth [7]=31;
	daysInMonth [8]=30;
	daysInMonth [9]=31;
	daysInMonth [10]=30;
	daysInMonth [11]=31;

weekDays = new Array(7);
	weekDays[0]="Su";
	weekDays[1]="M";
	weekDays[2]="Tu";
	weekDays[3]="W";
	weekDays[4]="Th";
	weekDays[5]="F";
	weekDays[6]="Sa";



/************************************************
* FUNCTION THAT RETURNS HTML FOR A CALENDAR
* BASED ON DAY, MONTH, YEAR
************************************************/

function genCalMonth(thisdate,thismonth,thisyear,selectDate){

	var calendarOut;
	var adjDaycount

	//** DAYOFWEEK TO START
	var tmpDay = new Date(thisyear,thismonth,1);
	var startday = tmpDay.getDay();


	//** FIGURE Y2K
	thisyear = thisyear % 100;
	thisyear = ((thisyear < 70) ? (2000 + thisyear) : (1900 + thisyear));

	adjDaycount = daysInMonth [thismonth]

	//** FIGURE LEAP YEAR
	if(thismonth==1){
		if (((thisyear % 4 == 0)
			&& !(thisyear % 100 == 0))
			||(thisyear % 400 == 0)) ++adjDaycount;
//			alert('changed: '+adjDaycount)
	}



	//** WRITING OUT CALENDAR

	calendarOut = "<TABLE border=0>"
	calendarOut += "<tr><TD>"
	calendarOut += "<table border=1 bgcolor='#E2F9C7' width=210 BORDERCOLOR=White CELLSPACING=0 CELLPADDING=2 bordercolorlight=White bordercolordark=White>"
	calendarOut += "<tr bgcolor=#7CC130>"
	calendarOut += "<td width=30>"
	calendarOut += createMonthLink(thisdate,thismonth,thisyear,0)
	calendarOut += "</td>"
	calendarOut += "<td align=center colspan=5>"
	calendarOut += "<span onMouseDown='showCalendar(document.forms[0].elements[0])' >"
	calendarOut += "<B><font class=verblu>"
	calendarOut += monthNames[thismonth] + " " + thisyear
	calendarOut += "</font></B>"
	calendarOut += "</span>"
	calendarOut += "</td>"
	calendarOut += "<td width=30>"
	calendarOut += createMonthLink(thisdate,thismonth,thisyear,1)
	calendarOut += "</td>"
	calendarOut += "</tr>"
	calendarOut += "<tr>"
	for(i=0;i<7;i++)
		calendarOut += "<td width=30 align=center><Font class=verblu><b>" + weekDays[i] + "</td>";
	calendarOut += "</tr><tr>"


	//** BLANK DAYS
	for (i=0;i<startday;i++)
		calendarOut += "<td> </td>";


	//** NOW DO DAYS
	count=1;
	while (count <= adjDaycount) {
		for (b = startday;b<7;b++) {
			calendarOut += "<td>"
			calendarOut += createDayLink(count,thisdate,thismonth,thisyear,adjDaycount)
			calendarOut += "</td>";
			count++;
		}
		startday=0;
		calendarOut += "</tr><tr>"
	}


	calendarOut += "</tr>"
	calendarOut += "</table>"
	calendarOut += "</TD></tr>"
	calendarOut += "</TABLE>"


	return 	calendarOut;

}




/************************************************
* RETURNS A FORMATTED DAY LINK FOR THE CALENDAR
************************************************/

function createDayLink(count,thisdate,thismonth,thisyear,adjDaycount){
	var returnVal = "";

	var xMonth;
	var xYear;
	var xdate=new Date();
	xMonth=xdate.getMonth();
	xYear=xdate.getYear();
	
	//** FIGURE Y2K
	xYear = xYear % 100;
	xYear = ((xYear < 70) ? (2000 + xYear) : (1900 + xYear));

	//** BEGIN LINK
	returnVal += "<a href=javascript:doDayLink(" + count
	returnVal += "," + thismonth + "," + thisyear + ")>"

	if(count <= adjDaycount){
		if (count==thisdate){
			if(xMonth==thismonth && xYear==thisyear){
				returnVal += "<font size=2 color=red><b>" + count + "</b></font>";
			}else{
				returnVal += "<font size=2>" +count +"</Font>";
			}
		}else{
			returnVal += "<font size=2>" +count +"</Font>";
		}
	}

	//** END LINK
	returnVal += "</a>"

	return returnVal;
}


/************************************************
* RETURNS A FORMATTED PREVIOUS/NEXT LINK
* FOR THE HEAD OF THE CALENDAR
************************************************/

function createMonthLink(thisdate,thismonth,thisyear,direction){
	var returnVal = "";
	var newmonth, imgLink;

	if(direction){
		//** GOING FORWARD
		
		if(++thismonth>11){
		  thismonth = 0;
		  ++thisyear;
		}
		  
		imgLink = createImage("../images/next.gif",14,14,"Next",0,3,1);
	}else{
		//** HEADING BACK
		if(--thismonth<0){
		  thismonth = 11;
		  --thisyear;
		}
		imgLink = createImage("../images/prev.gif",14,14,"Previous",0,3,1);
	}

	//** BEGIN LINK


	returnVal += '<a href=javascript:'
	returnVal += "moveMonth(" + thisdate + "," + thismonth + "," + thisyear + "," + "calendar" + ")"
	returnVal += ' >'
	returnVal += imgLink

	//** END LINK
	returnVal += "</a>"

	return returnVal;
}



/************************************************
* RETURNS AN IMAGE REFERENCE
************************************************/
function createImage(imgRef,width,height,alt,border,hpad,vpad){
	var returnVal = "";

	//** BEGIN IMAGE
	returnVal += "<img src='" + imgRef + "' width=" + width
	returnVal += " hspace=" + hpad + " vspace=" + vpad
	returnVal +=  " height=" + height + " alt='" + alt + "' "
	returnVal +=  " border=" + border + ">"

	return returnVal;
}





/************************************************
* DAY CLICK PROCESSOR
************************************************/
function doDayLink(thisday,thismonth,thisyear){
	var myDate
	var newMonth = parseInt(thismonth)
	++newMonth
	
	myDate="";

	myDate = newMonth + "/" + thisday + "/" + thisyear
	
	//alert(msg)
	//document.forms[0].date1.value = myDate;
	//alert(myDate.toString());
	currentField.value = myDate.toString();

	var calHideid = "CALHIDE:";
	var i = 0
	while(document.all(calHideid+i)){
		showSectionID(calHideid+i);
		i++;
	}
		


	hideSectionID('calendar');
	calendarOpen=0;
	thismonth=todayDate.getMonth();
	thisdate=todayDate.getDate();
	thisyear=todayDate.getYear();
	document.all("calendar").innerHTML = genCalMonth(thisdate,thismonth,thisyear,1);
	//window.changeflag.value = 1;
	//document.all("DataChanged").value='yes';
}




/************************************************
* DISPLAYS POPUP CALENDAR
************************************************/
function showCalendar(frmObj, Dflag) {
	var calHideid = "CALHIDE:";
	var i = 0
	sFlag = Dflag;
    window.statuschangeflag.value = window.changeflag.value;
    window.changeflag.value = 0;
    
    if(calendarOpen==0){
		
		document.all("calendar").style.top = this.event.clientY - 100;
		document.all("calendar").style.left = this.event.clientX;
		document.all("calendar").style.top = this.event.clientY - 100;
		document.all("calendar").style.top = this.event.clientY - 100;
		currentField = frmObj
		showSectionID('calendar');
		
		while(document.all(calHideid+i)){
			hideSectionID(calHideid+i);
			i++;
		}
		
		calendarOpen=1;
	}else{
		hideSectionID('calendar');
		while(document.all(calHideid+i)){
			showSectionID(calHideid+i);
			i++;
		}
		
		calendarOpen=0;
	}

}




/******************************************
* showSectionID()
* SHOW THE PASSED SECTION
******************************************/
function showSectionID(section) {
	if(document.all(section)){
		var ptmp = document.all(section).style;
		ptmp.visibility = 'visible';
		ptmp.display = "block";
	}
	if (sFlag == 1){
			document.form1.hours.style.display = 'none';
			document.form1.timeselect.style.display = 'none';
	}
	
}




/******************************************
* hideSectionID()
* HIDE THE PASSED SECTION
******************************************/
function hideSectionID(section) {
	if(document.all(section)){
		var ptmp = document.all(section).style;
		ptmp.visibility = 'hidden';
		ptmp.display = "none";
	}
	
	if (sFlag == 1){
		
		document.form1.hours.style.display = 'inline';
		document.form1.timeselect.style.display = 'inline';
		
	}
	
}



/************************************************
* CODE TO MOVE TO NEXT MONTH, PREVIOUS MONTH
************************************************/
function moveMonth(xDay, xMonth, xYear, sectID){
	document.all("calendar").innerHTML= genCalMonth(xDay, xMonth, xYear)
}









