﻿<!--
/* Calendar
 * --------
 * Ver: 1.00
 * Author: Amir Habib
 * Last Update: 27 Sep, 2006
 * Terms of User: Allrights Reserved to Author.
*/


//---------------------------------------------------------------------------------//
//-------------------------------- Configurations ---------------------------------//
//---------------------------------------------------------------------------------//

var iMaxPreviousDate = new Array('01','2006'); // example ('01',2000') or leave empty for current date

var aDropDownObjects = 	["time_hh",
						 "time_mm",
						 "cityid",
						 "stadiumid",
						 "ruleids"];

//---------------------------------------------------------------------------------//
//------------------------- Don't Change Below This Line --------------------------//
//---------------------------------------------------------------------------------//

function CalendarControl(oTextbox,sHandlerName)
{
	this.handlername = sHandlerName;
	this.layer = null;
	this.textbox = oTextbox;
	
	this.init();
}

CalendarControl.prototype.init = function()
{
	var oThis = this;
	
	this.textbox.onfocus = function(oEvent)
	{
		if(oEvent)
		{
			oEvent = window.event;
		}
		
		oThis.handleFocus(oEvent);
	}
	
	
	this.textbox.onblur = function(oEvent)
	{
		if(oEvent)
		{
			oEvent = window.event;
		}
		
		oThis.handleBlur(oEvent);		
	}
	
	
	oThis.createDatePicker();
}

CalendarControl.prototype.createDatePicker = function()
{
	this.layer = document.createElement("div");
	//this.layer.style.visibility = "hidden";
	this.layer.style.display = "none";
	this.layer.style.width = this.textbox.offsetWidth + 50;

	document.body.appendChild(this.layer);

	return;
};

CalendarControl.prototype.getLeft = function()
{
	var oNode = this.textbox;
	var iLeft = 0;
	
	while(oNode.tagName != "BODY")
	{
		iLeft += oNode.offsetLeft;
		oNode = oNode.offsetParent;
	}
	
	return iLeft;
}

CalendarControl.prototype.getTop = function()
{
	var oNode = this.textbox;
	var iTop = 0;
	
	while(oNode.tagName != "BODY")
	{
		iTop += oNode.offsetTop;
		oNode = oNode.offsetParent;
	}
	
	return iTop;
}

CalendarControl.prototype.ShowDatePicker = function()
{   

	
	
    for (f = 0; f < aDropDownObjects.length; f++)
    {
    	if(document.getElementById(aDropDownObjects[f]))
    	{
    		document.getElementById(aDropDownObjects[f]).style.visibility = 'hidden';
    	}
    }


	var oDiv = null;
	var oThis = this;
	
	this.layer.style.border = "0px;";
	this.layer.className = 'calendar';
	this.layer.style.left = this.getLeft() + "px";
	this.layer.style.top = this.getTop() + 22 + "px";
	this.layer.style.display = "block";
	
	
		
}

CalendarControl.prototype.HideDatePicker = function()
{

    for (f = 0; f < aDropDownObjects.length; f++)
    {
    	if(document.getElementById(aDropDownObjects[f]))
    	{
    		document.getElementById(aDropDownObjects[f]).style.visibility = 'visible';
    	}
    }

	//this.layer.innerHTML = "";	
	this.layer.style.display = "none";
}

CalendarControl.prototype.handleFocus = function(oEvent)
{	
	var oThis = this;
	
	oThis.getLeft();
	oThis.getTop();
	oThis.ShowDatePicker();	
}

CalendarControl.prototype.handleBlur = function(oEvent)
{
	var oThis = this;

	//oThis.HideDatePicker();
}


CalendarControl.prototype.pick = function(dd,mm,yyyy)
{
	var oThis = this;

	oThis.textbox.value = dd + "/" + mm + "/" + yyyy;
	oThis.HideDatePicker();
}

CalendarControl.prototype.calendar = function(month,year)
{

	// set default iMaxPreviousYear to current year //
	var sCurrentDate = new Date();
		
	var iMaxPreviousMonth = eval(iMaxPreviousDate[0]);
	var iMaxPreviousYear = eval(iMaxPreviousDate[1]);
	
	if(!iMaxPreviousMonth) iMaxPreviousMonth = sCurrentDate.getMonth() + 1;
	if(!iMaxPreviousYear) iMaxPreviousYear = sCurrentDate.getFullYear();
	

	var iMonth = eval(month);
	var iYear = Math.max(eval(year),iMaxPreviousYear);
	
	// limit Max Previous Date
	if(iMonth <= iMaxPreviousMonth && iYear <= iMaxPreviousYear)
	{
		// reset month and year
		iMonth = iMaxPreviousMonth;
		iYear = iMaxPreviousYear;		
		
		pMonth = iMonth;
		pYear = iYear;		
	}	
	
	
	
	var sDate = new Date(iYear,iMonth - 1,1);
	var iFirstDay = sDate.getDay();
	var i,j;

	// get month info //
	var iDayInMonth;
	var sMonthName;
	
	switch(iMonth)
	{
		case 1:		iDayInMonth = 31; 
					sMonthName = 'Jan';
					break;
					
		case 2:		(year % 4) ? iDayInMonth = 28 : iDayInMonth = 29;  // leap year checking
					sMonthName = 'Feb';
					break; 
					
		case 3:		iDayInMonth = 31; 
					sMonthName = 'Mar';
					break;
					
		case 4:		iDayInMonth = 30; 					
					sMonthName = 'Apr';
					break;
					
		case 5:		iDayInMonth = 31; 					
					sMonthName = 'May';
					break;
					
		case 6:		iDayInMonth = 30; 					
					sMonthName = 'Jun';
					break;
					 
		case 7:		iDayInMonth = 31; 					
					sMonthName = 'Jul';
					break;
					
		case 8:		iDayInMonth = 31; 					
					sMonthName = 'Aug';
					break;
					
		case 9:		iDayInMonth = 30; 					
					sMonthName = 'Sep';
					break;
					
		case 10:	iDayInMonth = 31; 					
					sMonthName = 'Oct';
					break;
					
		case 11:	iDayInMonth = 30; 					
					sMonthName = 'Nov';
					break;
					
		case 12:	iDayInMonth = 31; 					
					sMonthName = 'Dec';
					break;
	}
	
	
	// set next and previouse calendare parameters //
	var nMonth, nYear, pMonth, pYear;
	
	if(iMonth == 12)
	{
		nMonth = 1;
		nYear = iYear + 1;
	}	
	else
	{
		nMonth = iMonth + 1;
		nYear = iYear;
	}
	
	if(iMonth == 1)
	{
		pMonth = 12;		
		pYear = Math.max(iYear - 1,iMaxPreviousYear);
		if(iYear == iMaxPreviousYear) pMonth = 1;
		
	}	
	else
	{
		pMonth = Math.max(iMonth - 1,1);
		pYear = iYear;
	}
	

	
	
	
	// draw day titles line //
	var sCalendarHTML;


	sCalendarHTML = "<table  border='0' cellpadding='0' cellspacing='0' class='calendar'>";	
	sCalendarHTML += "<tr>";
	sCalendarHTML += "<td colspan='7'>";
	
		sCalendarHTML += "<table width=100% border='0' cellpadding=0 cellspacing=0 class='calendar_navigator'>";
		sCalendarHTML += "<tr>";
		
		sCalendarHTML += "<td align=left width=35>";
			sCalendarHTML += "<input type=button title='Previous Month' value='‹' style='width: 15px;' onclick=" + this.handlername + ".calendar(" + pMonth + "," + pYear + ") >";
			sCalendarHTML += "<input type=button title='Previous Year' value='«' style='width: 15px;' onclick=" + this.handlername + ".calendar(" + iMonth + "," + (iYear - 1) + ") >";
		sCalendarHTML += "</td>";
	
		sCalendarHTML += "<td align=center><b>" + sMonthName  + ", " + iYear + "<b></td>";
		
		sCalendarHTML += "<td align=right width=35>";
			sCalendarHTML += "<input type=button title='Next Year' value='»' style='width: 15px;' onclick=" + this.handlername + ".calendar(" + iMonth + "," + (iYear + 1) + ") >";
			sCalendarHTML += "<input type=button title='Next Month' value='›' style='width: 15px;' onclick=" + this.handlername + ".calendar(" + nMonth + "," + nYear + ") >";
		sCalendarHTML += "</td>";	
		
		sCalendarHTML += "</tr>";
		sCalendarHTML += "</table>";
	
	sCalendarHTML += "</td>";
	sCalendarHTML += "</tr>";
	
	sCalendarHTML += "<tr>";
	sCalendarHTML += "<th title='Sunday' height='20'>S</th>";
	sCalendarHTML += "<th title='Monday'>M</th>";
	sCalendarHTML += "<th title='Tuesday'>T</th>";
	sCalendarHTML += "<th title='Wednesday'>W</th>";
	sCalendarHTML += "<th title='Thursday'>T</th>";
	sCalendarHTML += "<th title='Friday'>F</th>";
	sCalendarHTML += "<th title='Saturday'>S</th>";
	sCalendarHTML += "</tr>";
	
	
	sCalendarHTML += "<tr>"
	
	// skip first days till first day of month //	
	for(i=0; i < iFirstDay; i++) sCalendarHTML += "<td>&nbsp;</td>";
	
	// fill first line from first day to end of first week //
	for(i=1; i <= 7 - iFirstDay; i++) sCalendarHTML += "<td><input type='button' value='" + i + "' class='calendar_btn'  onclick=" + this.handlername + ".pick(" + i + "," + iMonth + "," + iYear + ")></td>";
	
	// fill in calendar //	
	var iCurrentDay = i;
	var iRows = (iDayInMonth - (7 - iFirstDay)) / 7;

	
	for(j=0; j < iRows ; j++)
	{
		sCalendarHTML += "<tr>";
		
			for(i=0; i < 7 && iCurrentDay <= iDayInMonth ; i++, iCurrentDay++) sCalendarHTML += "<td><input type='button' value='" + iCurrentDay  + "' class='calendar_btn' onclick=" + this.handlername + ".pick(" + iCurrentDay + "," + iMonth + "," + iYear + ")></td>";
		
		sCalendarHTML += "</tr>";
	}
	

	sCalendarHTML += "</tr>";

	sCalendarHTML += "<tr>";
	sCalendarHTML += "<td colspan=7 align=center><a href='javascript:void(0);' onclick=" + this.handlername + ".HideDatePicker();>close</a>";
	sCalendarHTML += "</td>";
	sCalendarHTML += "</tr>";

	this.layer.innerHTML = sCalendarHTML;
}
-->