// editable global variables that can be changed for different languages
var aryMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var aryDays = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

// non-editable global variables
var classSelCellPrev;
var objCalTarget='';							// target object for the calendar to place its date (if this value is left blank, nothing happens)




function writeCalendar(boolAssign,boolHide) {
// this function creates the html code for the calendar
// boolAssign	if this is set to 1 then the clicked date needs to be returned to a form object.
// boolHide	if this is set to 1 then upon clicking a date, the calendar will 'hide'.
   var now = new Date;
   var dow = now.getDay();
   var dd  = now.getDate();
   var mm  = now.getMonth();
   var yyyy = now.getFullYear();
   var cell = 0;

   var html = '<table id="tblCalendar">';
   html += '<tr><td id="tdTitle" colspan="7"><img src="./images/x.png" onClick="document.getElementById(\'divCalendar\').style.display=\'none\';" /><input type="textbox" name="txtCalYear" id="txtCalYear" value="'+yyyy+'" maxlength="4" class="txtCalendar" onKeyUp="if(this.value.length==4){changeCal();}" /><select name="cmbCalMonth" id="cmbCalMonth" size="1" class="cmbCalendar" onChange="changeCal()">';
   for (var i=0; i<=11; i++) {						// cycle through the months and "select" the current month
	if (i==mm)
	   { html += '<option value= '+i+' selected>'+aryMonths[i]+'</option>'; }
	else
	   { html += '<option value= '+i+'>'+aryMonths[i]+'</option>'; }
   }
   html += '</select></td></tr>';
   html += '<tr>';
   for (var i=0; i<=6; i++) { html += '<th>' + aryDays[i] + '</th>'; }	// write all the "days of the week" at the top
   html += '</tr>';

   for (var i=0; i<=5; i++) {						// for each row of the calendar
	html += '<tr>';
	for (var j=0; j<=6; j++) {					// for each cell of the table...
	   html += '<td><a href="#" id="aCell'+cell+'" onClick="';
// switched the format in which it is returned for the OPo project
//	   if (boolAssign != '') { html += "document.getElementById(objCalTarget).value=(parseInt(document.getElementById('cmbCalMonth').value)+1)+'/'+this.innerHTML+'/'+document.getElementById('txtCalYear').value;"; }
	   if (boolAssign != '') { html += "document.getElementById(objCalTarget).value=document.getElementById('txtCalYear').value+'/'+(parseInt(document.getElementById('cmbCalMonth').value)+1)+'/'+this.innerHTML;"; }
	   if (! boolHide) { html += 'adjBG(this.id,this.className);'; } else { html += "document.getElementById('divCalendar').style.display='none';"; }
	   html += '">&nbsp;</a></td>';
	   cell += 1;
	}
	html += '</tr>';
   }
   html += '</table>';
   document.write(html);
   changeCal();
}

function maxDays(mm, yyyy) {
   var mDay;
   if((mm == 3) || (mm == 5) || (mm == 8) || (mm == 10)){
	mDay = 30;
   } else {
	mDay = 31;
	if (mm == 1) {
	   if (yyyy/4 - parseInt(yyyy/4) != 0) { mDay = 28; } else { mDay = 29; }
	}
   }
   return mDay;
}

function adjBG(strID,strClass) {
// this function changes the background color for the clicked item
   if (document.getElementById(strID).className == 'aSel') { return 1; }	// if the user is double clicking a date, only go through this routine once
   for (var i=0; i<=41; i++) {
	if (document.getElementById('aCell'+i).className == 'aSel') {	// if we have a previously selected cell
	   document.getElementById('aCell'+i).className = classSelCellPrev;			// restore its previous className
	   break;							// stop the loop
	}
   }
   classSelCellPrev = strClass;						// set the variable value to the new cell's previous className
   document.getElementById(strID).className = 'aSel';			// now change its className to be "selected"
}

function changeCal() {
// this function fills the calendar in with values
   var now = new Date;
   var dow = now.getDay();
   var dd = now.getDate();
   var mm = now.getMonth();
   var yyyy = now.getFullYear();
   var temp;

   var prevMonth;
   var currMonth = parseInt(document.getElementById('cmbCalMonth').value);
   var currYear = parseInt(document.getElementById('txtCalYear').value);
   var mmyyyy = new Date();
   var arrN = new Array(41);

   if (currMonth != 0) { prevMonth = currMonth - 1; } else { prevMonth = 11; }
   mmyyyy.setFullYear(currYear);
   mmyyyy.setMonth(currMonth);
   mmyyyy.setDate(1);
   var day1 = mmyyyy.getDay();
   if (day1 == 0) { day1 = 7; }

   for (var i=0; i<day1; i++) { arrN[i] = maxDays((prevMonth),currYear) - day1 + i + 1; }	// sets the last days of the previous month
   temp = 1;
   for (var i=day1; i<=day1+maxDays(currMonth,currYear)-1; i++) {	// sets the days for the current month
	arrN[i] = temp;
	temp += 1;
   }
   temp = 1;
   for (var i=day1+maxDays(currMonth,currYear); i<=41; i++) {		// sets the days for the next month
	arrN[i] = temp;
	temp += 1;
   }
   var dCount = 0;
   for (var i=0; i<=41; i++) {
	if (((i<7) && (arrN[i]>20)) || ((i>27) && (arrN[i]<20))) {	// for all "non current month dates"
	   document.getElementById('aCell'+i).innerHTML = arrN[i];
	   document.getElementById('aCell'+i).className = 'aNC';
	} else {							// for all current month dates
	   document.getElementById('aCell'+i).innerHTML = arrN[i];
	   if (dCount==0 || dCount==6) { document.getElementById('aCell'+i).className = 'aWeekend'; } else { document.getElementById('aCell'+i).className = 'aWeekday'; }
	   if (arrN[i]==dd && mm==currMonth && yyyy==currYear) { document.getElementById('aCell'+i).className = 'aToday'; }
	}
	dCount += 1;
	if (dCount > 6) { dCount=0; }
   }
}

