/*****************************************************************************
*									Script By TeeLek
******************************************************************************/	

//-----------------------------
function suycCalendar( m, y ) {
//-----------------------------
    if (typeof(_calendar_prototype_called) == 'undefined') {
        _calendar_prototype_called = true;
        // Object methods
        this.BuildCalendar = _create;
        this.createCanvass = _createCanvass;
        this.showCalendar = _showCalendar;
        this.goToCurrent = _goToCurrent;
        this.toggleCalendar = _toggleCalendar;
        this.moveTo = _positionCanvass;
        this.hide = _hide;
        this.show = _show;
        this.init = _init;
        this.renderNavBar = _renderNavBar;
        this.GetDay = _getDayString;
        // The Navigator Panel used to toggle between Months if hasEvents is switched to true
        g_calNavPanel = " ";
        
        // Object properties
        this.name = 'default';
        this.currentDay = 0;
        this.currentMonth = 0;
        this.currentYear = 0;
        this.visible = false;
        this.posX = 10;
        this.posY = 10;
        this.isIE4 = new Boolean;
        this.isNav4 = new Boolean;
        this.NavAtTop = true;
        
        // If you set hasEvents fo FALSE then the calendar face is dumbed out.
        this.hasEvents = true;
        
        this.canvass = new Object;           // The DIV || LAYER that we display the calendar on.
        this.bindToElement = new Object;     // Bind to an ELEMENT on the page.
        
		// Array of day names
        this.days = new Array("Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday");
        // Array of month names
        this.months = new Array("January", "February", "March", "April", "May","June", "July", "August", "September", "October", "November", "December");
        // Array of total days in each month
        this.totalDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        
        // Call the Initialize() event.
        this.init( m, y );
    }
}


// Called on initialization of the Class
function _init( m, y )
{
    if ( parseInt( navigator.appVersion.charAt(0) )>= 4 )
    // Browser check.
    {
        this.isNav4 = ( navigator.appName == "Netscape" ) ? true : false;
        this.isIE4 = ( navigator.appName.indexOf( "Microsoft" ) != -1 ) ? true : false;
    }
    
    // Populate the current Day|Month|Year properties
    var objDate = new Date();
    this.currentDay = objDate.getDate();
    this.currentMonth = objDate.getMonth(); 
    var curYear = objDate.getYear(); 
    this.currentYear = (curYear < 1000) ? curYear + 1900 : curYear; 
    
    /* 
        The constructor optionally accepts m && y parameters
        if none are supplied, the calendar defaults to the current
        month 
    */
    this.month = m || this.currentMonth;
    this.year = y || this.currentYear;
    
    // Create the canvass that we will be displaying the calendar on
    this.createCanvass( );
}


// Called on initialization of the Class to create the acutal DIV|LAYER Object
function _createCanvass( ) {
    if (this.isNav4)  { 
        this.canvass = new Layer(200);
    } else if (this.isIE4) {
        var objDiv = document.createElement("<DIV STYLE='position: absolute' id='myCalendar' name='myCalendar' onClick=\"MM_dragLayer('myCalendar','',0,0,1000,25,true,false,-1,-1,-1,-1,false,false,0,'',false,'')\">");
        document.body.appendChild ( objDiv );
        this.canvass = objDiv;
    }    
    this.moveTo( this.posX, this.posY );
}

function _positionCanvass( x, y ) {
    // if either x or y were not supplied, default to the current settings.
    if( x==null || y==null )  {
        x=this.posX; 
        y=this.posY; 
    }    
    if( isNaN( x ) || isNaN( y ) ) {
        alert('You can only enter numbers for the x/y co-ordinates');
        return;
    }
    // apply Positioning
    if ( this.isNav4 ) { 
        this.canvass.left = this.posX = x;
        this.canvass.top = this.posY = y;
    } else if ( this.isIE4 ) {
        this.canvass.style.left = this.posX = x;
        this.canvass.style.top = this.posY = y;
    }
}

function _show( ) {
	if(this.isNav4) {
        this.canvass.visibility = 'show';
    } else if (this.isIE4) {
        this.canvass.style.visibility = 'visible';
    }
    if(!this.visible) this.moveTo( this.posX, this.posY );
    this.visible = true; 
}

function _hide( ) {
    if(this.isNav4) {
        this.canvass.visibility = 'hide';   
    } else if (this.isIE4) {
        this.canvass.style.visibility = 'hidden';
    }
    this.visible = false;
}

function _showCalendar ( s ) {
    if( this.isNav4 ) {
        this.canvass.document.open( );
        this.canvass.document.writeln( s );
        this.canvass.document.close( );
    } else if ( this.isIE4 ) {
        this.canvass.innerHTML = s;
    }
    this.show( );
}

function _goToCurrent() {
    this.year = this.currentYear;
    this.month = this.currentMonth;
    this.BuildCalendar();
}

// Responsible for deciding where and what type of NavBar is produced
// arguments: pos = 'top' or 'bottom'
function _renderNavBar( pos ) {
    if( pos == 'top' && this.NavAtTop && this.hasEvents ) {
        return g_calNavPanel;
    } else {
		if( pos == 'bottom' && !(this.NavAtTop) && this.hasEvents ) {
			return g_calNavPanel;
	    } else {
			return '';
		}
	}
}

// Toggles the Calendar by +/- 1 Month
function _toggleCalendar(n) {
    if((this.month + n) < 0) {
        this.month = 11;
        -- this.year;
    } else if((this.month + n)>= 12) { 
        this.month = 0; 
        ++ this.year; 
    } else {
        this.month = this.month + n;
    }
    this.BuildCalendar();
}


//----- Create and Display Calendar --------------------
function _create() {
	//-----> Counters to count rows and days 
    var rowCount = 0;
    var sOut = new String;   // String to store calendar output.
	sOut = "";

	//-----> Leap year correction
    if (this.year % 4 == 0 && (this.year % 100 != 0 || this.year % 400 == 0)) {
    	this.totalDays[1] = 29;
    }
    
    //-----> Get the first and last Day numbers of the month being fetched
    var objDate = new Date( this.year, this.month, 1 );
    var firstDayOfMonth = objDate.getDay();
    objDate.setDate(31);
    var lastDayOfMonth = objDate.getDay();
    objDate = null;
    
    /*------------ create the cells for the table header ----------------*/
	sOut +="<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#000000\"  bordercolorlight=\"#000000\" bordercolordark=\"#E0E0E0\">"
		 + "    <tr bgcolor=\"#D4D4D4\" height=\"25\"> "
		 + "      <td height=\"25\" bgcolor=\"3A6EA5\"><table width=\"100%\" height=\"25\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"CalBText\">"
		 + "          <tr> "
		 + "            <td width=\"25\" height=\"25\" align=\"center\" valign=\"middle\">&nbsp;</td>"
		 + "            <td align=\"center\" valign=\"middle\" class=\"CalBWhite\"><table height=\"25\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" class=\"CalBWhite\">"
		 + "                <tr id=\"TextTable\"> "
		 + "                  <td align=\"left\" valign=\"middle\"><img src=\"images/prev.gif\"   onMouseOver=\"this.style.cursor=\'hand\'\" onClick=\"toggleCalendar(-1); return false;\"></td>"
		 + "                  <td width=\"100\" align=\"center\"><font color=\"#FFFFFF\"  onMouseOver=\"this.style.cursor='hand'\" onClick=\"objCalendar.goToCurrent();\"><b>" + this.months[this.month] + " " + this.year + "</b></font></td>"
		 + "                  <td align=\"right\" valign=\"middle\"><img src=\"images/next.gif\"  onMouseOver=\"this.style.cursor=\'hand\'\" onClick=\"toggleCalendar(1); return false;\"></td>"
		 + "                </tr>"
		 + "              </table></td>"
		 + "            <td width=\"25\" height=\"25\" align=\"center\" valign=\"middle\"><img src=\"images/bullet_close.gif\" onMouseOver=\"this.style.cursor=\'hand\'\" onclick=\"calClose(); return false;\" alt=\"Close\"></td>"
		 + "          </tr>"
		 + "        </table></td>"
		 + "    </tr>";
    
    // Test for Calendar NavBar.
    //sOut += this.renderNavBar ( 'top' );

    /*------------ create the cells for the day names ie: mon, tue... ------------*/
    sOut += "    <tr bgcolor=\"#D4D4D4\" height=\"20\"> "
		 + "      <td bgcolor=\"#D4D4D4\"><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\"  bordercolor=\"#000000\"  bordercolorlight=\"#000000\" bordercolordark=\"#E0E0E0\" height=\"20\">"
		 + "          <tr align=\"center\" valign=\"middle\" height=\"20\" id=\"TextTable\">";

	for (x=0; x<7; x++) {
        // Call the method to create the cell contents
        sOut += this.GetDay ( "<b>" + this.days[x].substring(0,3) + "</b>", false );
	}

    /*------------ start of calendar body ------------*/
    if (firstDayOfMonth>1) {
	    sOut += "</fomt></tr>";
		sOut += "          <tr align=\"center\" valign=\"middle\" height=\"20\" id=\"TextTable\">";
		for (x=1; x<=firstDayOfMonth; x++) {
			/*------------ pad the blank days at the beginning of the month. ------------*/
			rowCount++;
			sOut += this.GetDay ( "&nbsp;", false );
		}
    }    
    /*------------ create the cells for each day in the current month ------------*/
    var dayCount = 1;
    
    while (dayCount <= this.totalDays[this.month]) {
    	/*------------ display new row after each 7 day block.  ------------*/
    	if (rowCount % 7 == 0) {
    	    if (dayCount>1)  sOut += "</tr>";
			sOut += "          <tr align=\"center\" valign=\"middle\" height=\"20\" id=\"TextTable\">";
    	}
    	
    	// Call the method to create the cell contents
    	sOut += this.GetDay ( dayCount, true );
    	++rowCount;
    	++dayCount;
    }

    while ( rowCount % 7 != 0 ) {
        /* pad the blank days at the end of the month. */
        ++rowCount;
       sOut += this.GetDay ( "&nbsp;", false );
    }

	sOut += "</tr>";
	// End of BODY
    //sOut += this.renderNavBar ( 'bottom' );
    
    sOut += "        </table></td>"
		 + "    </tr>"
		 + "  </table>";

    // Render the calendar
    this.showCalendar ( sOut );
}


/*
    Responsible for displaying a Cell on the calendar face
*/
function _getDayString ( dayNum, isDay ) {
    var blnIsCurrentMonth = ( this.year == this.currentYear && this.month == this.currentMonth );
    var blnIsCurrentDay = ( blnIsCurrentMonth && ( dayNum == this.currentDay ) );
    
    if (blnIsCurrentDay) {
        thisCellBGColor = "#FF9900";
    } else {
        thisCellBGColor = "#E4E4E4";
    }

	var tmpCellOuter = '';
	
	// Display Cell as link ??
    if( isDay ) {
			tmpCellOuter = "            <td width=\"30\" height=\"18\" bgcolor=\"" + thisCellBGColor + "\" onMouseOver=\" this.style.cursor=\'hand\'; this.style.backgroundColor=\'#FFFFFF\';\"  onMouseOut=\"this.style.backgroundColor=\'\'; this.style.color=\'\';\"  onClick=\"clickhandler(" + dayNum + "," + this.month + "," + this.year + ");\">" + dayNum + "</td>";
    } else {
		if(dayNum=="&nbsp;") {
			tmpCellOuter = "            <td width=\"30\" height=\"18\">&nbsp;</td>";
		} else {
			tmpCellOuter = "            <td width=\"30\" height=\"18\" bgcolor=\"#A4A4A4\" class=\"CalBText\">" + dayNum  + "</td>";
		}
    }
    return tmpCellOuter;

}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			End Of suycCalendar Class
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */