/*********************************

		BEGIN CLASS clsWeekView

**********************************/



// This is the class to create a calendar for the given month

function clsWeekView_popup(strInit, callback, intYear, intMonth, intDay) {



// Check to see if they referenced this as a function or a class

	//if( !(this instanceof clsWeekView) ) return new clsWeekView();



	// Object methods

	this.init = _init_popup;

	this.setWeek = _setWeek_popup;

	this.setDay = _setDay_popup;

	this.drawCal = _drawCal_popup;

	this.setHeader = _setHeader_popup;

	this.setBackground = _setBackground_popup;



	// Object properties

	this.sizeX = 150;

	this.sizeY = 150;

	this.initName = strInit;

	this.selectedWeek = null;

	this.colorOver = "#E7F0F0";

  this.colorNormal = "#FFFFFF";

  this.dateCallback = callback;

  this.curWeek = null;

  this.dayOffset = null;

  this.curYear = null;

  this.curMonth = null;

  this.curDay = null;

  this.dateDiv = null;

  this.backgroundImage = null;



	this.init(intYear, intMonth, intDay);



}



function _init_popup(intYear, intMonth, intDay) {



  var calHTML, whatDay, loopDay, firstDay, dayOffset, weekID, thisWeek;



  if (!intYear) {

    var newDate = new Date();

    intYear = newDate.getFullYear();

    intMonth = newDate.getMonth();

    intDay = newDate.getDay();

  }



	// Check the browser version

	if (parseInt(navigator.appVersion.charAt(0)) >= 4)

	{

		this.isNav = (navigator.appName == "Netscape") ? true : false;

		this.isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

	}



  // Setup our calID (CHANGE THIS TO MAKE IT DYNAMIC)

  this.calID = this.initName;



  // Create the window

	//var newCal = document.createElement("<div style='height: " + this.sizeY + "px; width: " + this.sizeX + "px; position: relative' id='" + this.calID + "'>");

	//document.body.appendChild(newCal);



  this.drawCal(intYear, intMonth, intDay);



  // Get our starting week

  thisWeek = Math.ceil((intDay + this.dayOffset) / 7);

  //this.setWeek(thisWeek);





}



function _drawCal_popup(intYear, intMonth, intDay) {



  this.curYear = intYear;

  this.curMonth = intMonth;

  this.curDay = intDay;

  

  //alert("HERE");



  // Setup the HTML to insert into the table

  calHTML = '<div class="cal_container" style="border: 1px #747777 solid;">\n';

  calHTML += '<table border="0" cellpadding="5" cellspacing="0" width="100%" height="100%">\n';

  calHTML += '<tr height="15%" bgcolor="#92A8B5"><td id="' + this.initName + '_calHeader" colspan="7" class="calTop"><a href="#" onClick="prevMonth();">&lt;</a>&nbsp;&nbsp;' + aryMonths[parseInt(intMonth) - 1] + " " + intYear + '&nbsp;&nbsp;<a href="#" onClick="nextMonth();">&gt;</a></td></tr>\n';

  calHTML += '<tr height="1"><td colspan="7" bgcolor="#CCCCCC"></td></tr>\n';

  calHTML += '<tr height="15%" bgcolor="#EFEFEF"><td width="15%" class="calSubHeader">S</td><td width="15%" class="calSubHeader">M</td><td width="14%" class="calSubHeader">T</td><td width="14%" class="calSubHeader">W</td><td width="14%" class="calSubHeader">T</td><td width="14%" class="calSubHeader">F</td><td width="14%" class="calSubHeader">S</td></tr>\n';

  calHTML += '<tr height="1"><td colspan="7" bgcolor="#CCCCCC"></td></tr>\n';



  // Get the date attributes

  whatDay = getDayOfWeek(intYear, intMonth, intDay);

  this.dayOffset = getDayOfWeek(intYear, intMonth, 1);



  weekID = 1;



  // Setup our loop

  for (loopDay = 1; loopDay <= 38; loopDay++) {



    var thisDay = loopDay - this.dayOffset;



    if ((loopDay % 7) == 1) {

      calHTML += '<tr bgcolor="#FFFFFF" height="14%" id="' + this.initName + '_rowWeek' + weekID + '">';

      weekID++;

    }



    if (thisDay >= 1 && thisDay <= aryMonthDays[intMonth - 1]) {

      //if (thisDay == intDay) {

        //calHTML += '<td height="100%" valign="middle" onClick="top.' + this.initName + '.setDay(\'' + thisDay + '\')" class="calCurDay"><div class="calCurDay">' + thisDay + "</div></td>";

      //} else {

        calHTML += '<td height="100%" valign="middle" onClick="top.' + this.initName + '.setDay(\'' + thisDay + '\')" onMouseOver="this.style.cursor=\'hand\'; if (this.id != ' + this.initName + '.selectedWeek) { this.style.backgroundColor=\'' + this.colorOver + '\'; }" onMouseOut="if (this.id != ' + this.initName + '.selectedWeek) { this.style.backgroundColor=\'' + this.colorNormal + '\'; }"><div class="calDay">' + thisDay + "</div></td>";

      //}

    } else {

      calHTML += '<td height="100%"><div class="calDay"></div></td>';

    }



    if ((loopDay % 7) == 0) {

      calHTML += '</tr>\n';

    }

  }



  calHTML += '</table>\n';

	calHTML += '</div>';

	//alert(calHTML);

	//alert(this.selectedWeek);



	document.getElementById("cal_" + this.calID).innerHTML = calHTML;

}



function _setWeek_popup(weekID) {

  this.curWeek = weekID;

  tmpYear = this.curYear;

  tmpMonth = (this.curMonth.toString().length == 1) ? "0" + this.curMonth : this.curMonth;

  tmpDay = (weekID * 7) - parseInt(this.dayOffset);

  tmpDay = (tmpDay > aryMonthDays[this.curMonth - 1]) ? tmpDay - aryMonthDays[this.curMonth - 1] : tmpDay;

  lastDay = tmpDay;

  tmpDay = (weekID * 7) - parseInt(this.dayOffset);

  //tmpDay = (tmpDay > aryMonthDays[this.curMonth - 1]) ? aryMonthDays[this.curMonth - 1] : tmpDay;

  tmpMonthDays = (this.curMonth != 1) ? aryMonthDays[this.curMonth - 2] : aryMonthDays[0];

  firstDay = ((parseInt(tmpDay) - 6) >= 1) ? (parseInt(tmpDay) - 6) : tmpMonthDays + (parseInt(tmpDay) - 6);

  var beginMonthOffset = (lastDay < firstDay && this.curWeek == 1) ? 11 : 0;

  beginDate = new Date(this.curYear, this.curMonth - 1 + beginMonthOffset, firstDay);

  var endMonthOffset = (lastDay < firstDay && this.curWeek != 1) ? 1 : 0;

  endDate = new Date(this.curYear, this.curMonth - 1 + endMonthOffset, lastDay);

  tmpDay = (tmpDay.toString().length == 1) ? "0" + tmpDay : tmpDay;

  //weekStr = tmpYear.toString() + tmpMonth.toString() + tmpDay.toString();

  weekStr = tmpMonth.toString() + "/" + tmpDay.toString() + "/" + tmpYear.toString();

  strDateRange = aryAbbrDays[getDayOfWeek(this.curYear, this.curMonth, firstDay)] + "., " + aryMonths[beginDate.getMonth()] + " " + firstDay + " - " + aryAbbrDays[getDayOfWeek(this.curYear, this.curMonth, lastDay)] + "., " + aryMonths[endDate.getMonth()] + " " + lastDay;

  //alert(strDateRange);

  if (this.dateDiv != null) {

    document.getElementById(this.dateDiv).innerHTML = strDateRange;

  }



  eval("" + this.dateCallback + "(\'" + weekStr + "\')");

  //callbackFunc();

  if (this.selectedWeek != null) {

    document.getElementById(this.selectedWeek).style.backgroundColor = this.colorNormal;

  }

  this.selectedWeek = this.initName + "_rowWeek" + weekID;

  document.getElementById(this.initName + "_rowWeek" + weekID).style.backgroundColor = this.colorOver;

  //testFunc();

}



function _setDay_popup(whichDay) {

  tmpYear = this.curYear;

  tmpMonth = (this.curMonth.toString().length == 1) ? "0" + this.curMonth : this.curMonth;

  tmpDay = (whichDay.toString().length == 1) ? "0" + whichDay : whichDay;

  weekStr = tmpMonth.toString() + "/" + tmpDay.toString() + "/" + tmpYear.toString();

  //weekStr = tmpYear.toString() + tmpMonth.toString() + tmpDay.toString();

  //strDateRange = aryAbbrDays[getDayOfWeek(this.curYear, this.curMonth, firstDay)] + "., " + aryMonths[beginDate.getMonth()] + " " + firstDay + " - " + aryAbbrDays[getDayOfWeek(this.curYear, this.curMonth, lastDay)] + "., " + aryMonths[endDate.getMonth()] + " " + lastDay;

  //alert(strDateRange);

  if (this.dateDiv != null) {

    document.getElementById(this.dateDiv).innerHTML = strDateRange;

  }



  eval("" + this.dateCallback + "(\'" + weekStr + "\')");

}



function _setHeader_popup() {

  thisWeek = Math.ceil((this.curDay + this.dayOffset) / 7);

  this.setWeek(thisWeek);

}



function _setBackground_popup() {

  document.getElementById(this.initName + "_calHeader").style.backgroundImage = 'url("' + this.backgroundImage + '")';

}

