<public:component>
<public:attach event="oncontentready" onevent="init()" />
<public:attach event="onblur" onevent="hideListWindow()" />
<script language="JScript">
var controlTable;
var chooserCell;
var inputCell;
var listWindow;
var listCell;
var valueControl;
var input = this.element;
var styleInfo = null;
var todaysCell;

function init(){
	buildControl();
	attachEvents()
}

function setFocus()
{
	
}
function buildControl(){
	controlTable = document.createElement("TABLE");
	
	var row = controlTable.insertRow();
	inputCell = row.insertCell();
	chooserCell = row.insertCell();
	chooserCell.innerText = "6";
	
	this.parentElement.insertBefore(controlTable, this.element);
	inputCell.appendChild(input);
	
	listWindow = document.createElement("TABLE");
	listCell = listWindow.insertRow().insertCell();
	listWindow.style.visibility = "hidden";
	listWindow.style.position = "absolute";
	controlTable.parentElement.appendChild(listWindow);

	buildListWindow();
		
	initControlStyle();
	
}

function customDefault(cssName, scriptName, defaultValue)
{
    if (currentStyle[cssName] == null)
    {
        style[cssName] = defaultValue;
    }
    else style[cssName] = currentStyle[cssName];
    
    style[scriptName] = style[cssName];
}

function normalDefault(cssName, scriptName, defaultValue)
{
    if (style[cssName] == "" || style[cssName] == null)
    {
		if (currentStyle[scriptName] == "" || (currentStyle[scriptName] + "") == "undefined")
		{
			style[cssName] = defaultValue;
			style[scriptName] = defaultValue;
			
		}
		else
		{
			style[cssName] = currentStyle[scriptName];
			style[scriptName] = currentStyle[scriptName];
		}
    }
}

function setStyleDefaults()
{

	if (currentStyle["height"]  == "auto") style["height"] = "19";

	normalDefault('font-family', 'fontFamily', 'Tahoma,Verdana,Arial');
	normalDefault('font-size', 'fontSize', '');
	normalDefault('font-weight', 'fontWeight', '');
	normalDefault('cursor', 'cursor', 'hand');
	normalDefault('background-color', 'backgroundColor', 'white');
	normalDefault('color', 'color', '');

	customDefault('border', 'border', '1 solid silver');	
	customDefault('font-size', 'fontSize', '');	
	
	customDefault('active-border', 'activeBorder', '1 solid black');
	customDefault('active-color', 'activeColor', '');
	customDefault('active-font-family', 'activeFontFamily', '');
	customDefault('active-font-size', 'activeFontSize', '');
	customDefault('active-font-weight', 'activeFontWeight', '');
	customDefault('active-background-color', 'activeBackgroundColor', 'white');
	customDefault('active-button-color', 'activeButtonColor', 'white');
	customDefault('active-button-background-color', 'activeButtonBackgroundColor', 'silver');
	
	customDefault('button-font-size', 'buttonFontSize', '12');
	
	customDefault('button-color', 'buttonColor', 'silver');
	customDefault('button-background-color', 'buttonBackgroundColor', 'silver');
	customDefault('button-font-size', 'buttonFontSize', '');

	customDefault('header-border', 'headerBorder', '');
	customDefault('header-color', 'headerColor', 'white');
	customDefault('header-font-family', 'headerFontFamily', '');
	customDefault('header-font-size', 'headerFontSize', '');
	customDefault('header-font-weight', 'headerFontWeight', '');
	customDefault('header-background-color', 'headerBackgroundColor', 'black');

	customDefault('inactive-day-border', 'inactiveDayBorder', '');
	customDefault('inactive-day-color', 'inactiveDayColor', 'silver');
	customDefault('inactive-day-font-family', 'inactiveDayFontFamily', '');
	customDefault('inactive-day-font-size', 'inactiveDayFontSize', '');
	customDefault('inactive-day-font-weight', 'inactiveDayFontWeight', '');
	customDefault('inactive-day-background-color', 'inactiveDayBackgroundColor', 'white');
	
	customDefault('weekend-day-border', 'weekendDayBorder', '');
	customDefault('weekend-day-color', 'weekendDayColor', 'red');
	customDefault('weekend-day-font-family', 'weekendDayFontFamily', '');
	customDefault('weekend-day-font-size', 'weekendDayFontSize', '');
	customDefault('weekend-day-font-weight', 'weekendDayFontWeight', '');
	customDefault('weekend-day-background-color', 'weekendDayBackgroundColor', '');

	customDefault('day-border', 'dayBorder', '');
	customDefault('day-color', 'dayDayColor', '');
	customDefault('day-font-family', 'dayFontFamily', '');
	customDefault('day-font-size', 'dayFontSize', '');
	customDefault('day-font-weight', 'dayFontWeight', '');
	customDefault('day-background-color', 'dayBackgroundColor', '');
	
	customDefault('highlighted-day-border', 'highlightedDayBorder', 'black');
	customDefault('highlighted-day-color', 'highlightedDayColor', 'white');
	customDefault('highlighted-day-font-family', 'highlightedDayFontFamily', '');
	customDefault('highlighted-day-font-size', 'highlightedDayFontSize', '');
	customDefault('highlighted-day-font-weight', 'highlightedDayFontWeight', '');
	customDefault('highlighted-day-background-color', 'highlightedDayBackgroundColor', 'navy');

}

function ifempty(val, altVal)
{
	if (val == null || val == "")
		return altVal;
	else
		return val;
}

function createStyleSheet()
{
	if (!element.document.body.BehaviorStyleSheet)
		element.document.body.BehaviorStyleSheet = element.document.createStyleSheet();
	styleInfo = element.document.body.BehaviorStyleSheet;		

	styleInfo.addRule(   '.contol-table-'+uniqueID,
		'height: ' + style.height + '; ' +
		'cursor: hand;'
	);

	styleInfo.addRule(   '.inactive-listwindow-'+uniqueID,
		'font-family: ' + style.fontFamily + '; ' +
		'font-size: ' + style.fontSize + '; ' +				
		'color: ' + style.color + '; ' +
		'background-color: ' + style.backgroundColor + '; ' +
		'border: ' + style.border + ';'
	);

	styleInfo.addRule(   '.listwindow-'+uniqueID,
		'font-family: ' + ifempty(style.activeFontFamily, style.fontFamily) + '; ' +
		'font-size: ' + ifempty(style.activeFontSize, style.fontSize) + '; ' +				
		'color: ' + ifempty(style.activeColor, style.color) + '; ' +
		'background-color: ' + ifempty(style.activeBackgroundColor, style.backgroundColor) + '; ' +
		'border: ' + ifempty(style.activeBorder, style.border)  + ';' 
	);

	styleInfo.addRule(   '.listwindow-table-'+uniqueID,
		'font-family: ' + ifempty(style.activeFontFamily, style.fontFamily) + '; ' +
		'font-size: ' + ifempty(style.activeFontSize, style.fontSize) + '; ' +				
		'color: ' + ifempty(style.activeColor, style.color) + '; ' +
		'background-color: ' + ifempty(style.activeBackgroundColor, style.backgroundColor) + '; '
	);

	styleInfo.addRule(   '.inactive-input-cell-'+uniqueID,
		'font-family: ' + style.fontFamily + '; ' +
		'font-size: ' + style.fontSize + '; ' +				
		'cursor: auto; ' +
		'color: ' + style.color + '; ' +
		'background-color: ' + style.backgroundColor + '; ' +
		'border: ' + style.border + ';'
	);

	styleInfo.addRule(   '.input-cell-'+uniqueID,
		'font-family: ' + ifempty(style.activeFontFamily, style.fontFamily) + '; ' +
		'font-size: ' + ifempty(style.activeFontSize, style.fontSize) + '; ' +				
		'cursor: auto; ' +
		'color: ' + ifempty(style.activeColor, style.color) + '; ' +
		'background-color: ' + ifempty(style.activeBackgroundColor, style.backgroundColor) + '; ' +
		'border: ' + ifempty(style.activeBorder, style.border) + ';'
	);

	styleInfo.addRule(   '.inactive-button-cell-'+uniqueID,
		'font-size: ' + style.buttonFontSize+ '; ' +				
		'cursor: ' + style.cursor + '; ' +
		'color: ' + style.color + '; ' +
		'background-color: ' + style.buttonBackgroundColor + '; ' +
		'border: ' + style.border + ';'
	);

	styleInfo.addRule(   '.button-cell-'+uniqueID,
		'font-size: ' + style.buttonFontSize + '; ' +				
		'cursor: ' + style.cursor + '; ' +
		'color: ' + ifempty(style.activeButtonColor, style.buttonColor) + '; ' +
		'background-color: ' + ifempty(style.activeButtonBackgroundColor, style.buttonBackgroundColor) + '; ' +
		'border: ' + ifempty(style.activeBorder, style.activeBorder) + ';'
	);

	styleInfo.addRule(   '.header-'+uniqueID,
		'font-family: ' + style.headerFontFamily + '; ' +
		'font-size: ' + style.headerFontSize + '; ' +				
		'color: ' + style.headerColor + '; ' +
		'background-color: ' + style.headerBackgroundColor + '; ' +
		'border: ' + style.headerBorder + ';'
	);

	styleInfo.addRule(   '.weekend-day-'+uniqueID,
		'font-family: ' + style.weekendDayFontFamily + '; ' +
		'font-size: ' + style.weekendDayFontSize + '; ' +				
		'color: ' + style.weekendDayColor + '; ' +
		'cursor: ' + style.cursor + '; ' +
		'background-color: ' + style.weekendDayBackgroundColor + '; ' +
		'border: ' + style.weekendDayBorder + ';'
	);

	styleInfo.addRule(   '.day-'+uniqueID,
		'font-family: ' + style.dayFontFamily + '; ' +
		'font-size: ' + style.dayFontSize + '; ' +				
		'color: ' + style.dayColor + '; ' +
		'cursor: ' + style.cursor + '; ' +
		'background-color: ' + style.dayBackgroundColor + '; ' +
		'border: ' + style.dayBorder + ';'
	);

	styleInfo.addRule(   '.inactive-day-'+uniqueID,
		'font-family: ' + style.inactiveDayFontFamily + '; ' +
		'font-size: ' + style.inactiveDayFontSize + '; ' +				
		'color: ' + style.inactiveDayColor + '; ' +
		'cursor: ' + style.cursor + '; ' +
		'background-color: ' + style.inactiveDayBackgroundColor + '; ' +
		'border: ' + style.inactiveDayBorder + ';'
	);

	styleInfo.addRule(   '.highlighted-'+uniqueID,
		'font-family: ' + style.highlightedDayFontFamily + '; ' +
		'font-size: ' + style.highlightedDayFontSize + '; ' +				
		'color: ' + style.highlightedDayColor + '; ' +
		'cursor: ' + style.cursor + '; ' +
		'background-color: ' + style.highlightedDayBackgroundColor + '; ' +
		'border: ' + style.highlightedDayBorder + ';'
	);

}

function initControlStyle(){

	setStyleDefaults()
	createStyleSheet();
	
	//controlTable.style.border = "1 solid gray";
	
	controlTable.cellPadding = 0;
	controlTable.cellSpacing = 0;
	controlTable.style.borderCollapse = "collapse";
	controlTable.className = 'contol-table-'+uniqueID;

	input.style.border = "none";		
	
	chooserCell.className = "inactive-button-cell-" + uniqueID;
	inputCell.className = "inactive-input-cell-" + uniqueID;
	listWindow.className = "inactive-listwindow-" + uniqueID;
	//chooserCell.style.border = "1 solid gray";
	//chooserCell.style.background = "silver";
	chooserCell.style.fontFamily = "Marlett";
	//chooserCell.style.cursor = "hand";		
	
	//listWindow.style.backgroundColor = "white";
	//listWindow.style.height = "100";
	//listWindow.style.width = "100";
	//listWindow.style.border = "1 solid black";
	//listWindow.style.cursor = "arrow"
	
	
}

function attachEvents(){
	controlTable.attachEvent("onmouseenter", doOnMouseEnter);
	input.attachEvent("onmouseenter", doOnMouseEnter);
	controlTable.attachEvent("onmouseout", doOnMouseOut);
	input.attachEvent("onblur", doOnInputBlur);
	chooserCell.attachEvent("onmousedown", function(){input.focus();chooserCell.setCapture()});
	chooserCell.attachEvent("onmouseup", function(){chooserCell.releaseCapture()});	
	chooserCell.attachEvent("onclick", toggleListWindow);
	chooserCell.attachEvent("ondblclick", toggleListWindow);		
	chooserCell.attachEvent("onselectstart", cancelSelection);
}

function doOnInputBlur()
{
	if (input.value != "" && calendarStringToDate(input.value)== null)
	{
		alert( input.value + " is not valid date value.");
		input.focus();
	}
	else
		doOnMouseOut();	
}

function highlightControl(){
	chooserCell.className = "button-cell-" + uniqueID;
	inputCell.className = "input-cell-" + uniqueID;
	listWindow.className = "listwindow-" + uniqueID;
}

var cellPerviousClassName = "";
var perviousCell = null;

function highlightCell(cell)
{
	if (cell)
	{
		cellPerviousClassName = cell.className;
		cell.className = "highlighted-"+uniqueID;
	}
}

function unhighlightCell(cell)
{
	if (cell)
	{
		cell.className = cellPerviousClassName;
	}
}

function showObjProperties(obj)
{
	var s = "";
	for (var el in obj)
		s += el + " : " + obj[el] + "\n";
	alert(s)
}

function unhighlightControl(){		
	if (this.element.document.activeElement != input){
	//alert(this.element.document.activeElement.tagName + " - " + input.tagName);
	chooserCell.className = "inactive-button-cell-" + uniqueID;
	inputCell.className = "inactive-input-cell-" + uniqueID;
	listWindow.className = "inactive-listwindow-" + uniqueID;
	}
}

function isListWindowVisible(){
	return listWindow.style.visibility == "visible";	
}

function toggleListWindow(){
	if (isListWindowVisible())
		hideListWindow();
	else
		showListWindow();
}

function showListWindow(){
	if (input.value != "" && calendarStringToDate(input.value)!= null) calendarCurrentDate = calendarStringToDate(input.value);
	setCalendarMonth();
	setListWindowPosition();
	chooserCell.innerText = "5";
	listWindow.style.visibility = "visible";
}

function hideListWindow(){
	chooserCell.innerText = "6";
	listWindow.style.visibility = "hidden";
}

function getAbsoluteTop(el){
	var result = 0;
	while (el){
		result += el.offsetTop;
		el = el.offsetParent;
	}
	return result
}

function getAbsoluteLeft(el){
	var result = 0;
	while (el){
		result += el.offsetLeft;
		el = el.offsetParent;					
	}
	return result
}

function setListWindowPosition(){
	listWindow.style.left = getAbsoluteLeft(controlTable);
	listWindow.style.top = getAbsoluteTop(controlTable) + controlTable.clientHeight + 4;
	listWindow.style.width = controlTable.clientWidth;
}

function cancelSelection(){
	event.returnValue = null;
}

function doOnMouseEnter(){
	highlightControl();
}

function doOnMouseOut(){
	unhighlightControl();	
}


function isCursorInControl(){
	return true;
}

var monthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var weekDays = new Array('Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
var firstWeekDay = 1;
var weekendDays = new Array(0,6);
var dates = new Array();
var calendarCurrentDate = new Date(new Date(new Date().getYear(), new Date().getMonth(), new Date().getDate()));
var calendarMonthCell;
var calendarYearCell;	


function calendarStringToDate(str){
	var calendarDateOrder = "dmy";
	var clendarDateSeparator  = "/";
	var values = new Array();
	if (str && str != ""){
		var time = String(str).split(" ");
		if (time.length == 2)
		{
			str = time[0];
			time = time[1].split(":");

			while (time.length<2)
				time[time.length] = 0;
		}
		else
		  time = new Array("0","0","0");
		  
		str = String(str).split(clendarDateSeparator);

		for (var i=0;i<str.length;i++)
			while (str[i].indexOf("0")==0)
				str[i] = str[i].substring(1, str[i].length);
		
		if (str.length >2){
			var year = (str[calendarDateOrder.indexOf('y')]);
			if (String(year).length<3)
			{
				if (parseInt(year)>=0 && parseInt(year)<=20)
					year =  2000 + parseInt(year);
			}
			return new Date(year, parseInt(str[calendarDateOrder.indexOf('m')]) -1, parseInt(str[calendarDateOrder.indexOf('d')]), time[0], time[1], time[2]);
		}
		return null;
	}
	else return null;
}

function calendarDateToString(d){
	var result = "";
	var calendarDateOrder = "dmy";
	var clendarDateSeparator  = "/";
	if (d && !isNaN(d)){
		for (var i=0;i<calendarDateOrder.length;i++){
			var s = calendarDateOrder.substring(i,i+1);
			switch(s){
				case "y":{
					result += (result != "") ? clendarDateSeparator : "";
					result += ((d.getFullYear() < 10) ? "0" + d.getFullYear() : d.getFullYear());
					
					break;
				}
				case "m":{
					result += (result != "") ? clendarDateSeparator : "";				
					result += (((d.getMonth()+1)<10) ? "0" + (d.getMonth() + 1) : d.getMonth() + 1);
					
					break;
				}
				case "d":{
					result += (result != "") ? clendarDateSeparator : "";				
					result += ((d.getDate() < 10 ) ? "0" + d.getDate(): d.getDate() );

					break;
				}
			}
		}
	}
	if (d.getHours() != 0 || d.getMinutes() != 0 || d.getSeconds() != 0)
		result += " " + d.getHours()  + ":" + d.getMinutes() + ":" + d.getSeconds();

	return result;
}

function setCalendarPriorMonth(byYear)
{
	var i = (byYear) ? 12 : 1;
	calendarCurrentDate.setMonth(calendarCurrentDate.getMonth()-i);
	setCalendarMonth()
}

function setCalendarNextMonth(byYear)
{
	var i = (byYear) ? 12 : 1;
	calendarCurrentDate.setMonth(calendarCurrentDate.getMonth()+i);
	setCalendarMonth()
}

function setCalendarMonth()
{
	fillCalendarValues(calendarCurrentDate);
	calendarMonthCell.innerText = monthNames[((calendarCurrentDate == null) ? new Date().getMonth() : calendarCurrentDate.getMonth())];
	calendarYearCell.innerText = ((calendarCurrentDate == null) ? new Date().getFullYear() : calendarCurrentDate.getFullYear());	
}

function fillCalendarValues(initDate)
{
	var d = null;
	if (initDate != "" && !isNaN(new Date(initDate))) d = new Date(initDate); else d = new Date(new Date().getYear(), new Date().getMonth(), new Date().getDate());
	var firstDay = new Date(d.getFullYear(), d.getMonth(), 1);
	var begOffset = (firstDay.getDay() == 1) ? 0 : ((firstDay.getDay() == 0) ? 6 : (firstDay.getDay() - 1));
	var startDate = new Date(d.getFullYear(), d.getMonth(), 1 - begOffset, d.getHours(), d.getMinutes(), d.getSeconds());
	for (var i=0;i<6;i++)
		for (var j=0;j<7;j++){
			dates[i][j]["value"] = new Date(startDate);				
			dates[i][j]["cell"].innerText = startDate.getDate(); 
			if (startDate.getMonth() == initDate.getMonth() && startDate.getYear() == initDate.getYear() && startDate.getDate() == initDate.getDate())
			{
				dates[i][j]["cell"].className = "highlighted-" + uniqueID;
			}
			else
			{
				if (startDate.getMonth() != initDate.getMonth())
					dates[i][j]["cell"].className = "inactive-day-" + uniqueID;
				else
				{
					dates[i][j]["cell"].className = (startDate.getDay() >5 || startDate.getDay() == 0) ? "weekend-day-" + uniqueID : "day-" + uniqueID;
				}
			}
	 		startDate.setDate(startDate.getDate() + 1);		
		}
		
}

function chooseCalendarValue(el)
{
	hideListWindow();
	if (event.srcElement == todaysCell)
	{
		SetInputValue(calendarDateToString(new Date(new Date(new Date().getYear(), new Date().getMonth(), new Date().getDate()))));	
	}
	else
	{
		for (var i=0;i<6;i++)
			for (var j=0;j<7;j++){
				if (dates[i][j]["cell"] == event.srcElement)
					SetInputValue(calendarDateToString(dates[i][j]["value"]));
			}
	}

}

function SetInputValue(dateVal)
{
	input.value = dateVal;
	if(input.onchange)
		input.onchange();	
}

function buildListWindow()
{
	var table = document.createElement("TABLE");
	table.className = 'listwindow-table-' + uniqueID;
	var calendarChosersRow = table.insertRow();
	var leftCell = calendarChosersRow.insertCell();
	leftCell.style.fontFamily = "Marlett";
	leftCell.style.fontSize = "12";
	leftCell.innerHTML = "3";
	leftCell.attachEvent("onmouseenter", function(){highlightCell(event.srcElement)});	
	leftCell.attachEvent("onmouseout", function(){unhighlightCell(event.srcElement)});		
	leftCell.attachEvent("onclick", function(){setCalendarPriorMonth(event.ctrlKey)});
	leftCell.attachEvent("ondblclick", function(){setCalendarPriorMonth(event.ctrlKey)});	
	leftCell.attachEvent("onmousedown", function(){event.srcElement.setCapture()});						
	leftCell.attachEvent("onmouseup", function(){event.srcElement.releaseCapture()});									
	
	var monthCell = calendarChosersRow.insertCell();		
	monthCell.colSpan = 5;
	
	var monthTable = document.createElement("TABLE");
	monthTable.className = 'listwindow-table-' + uniqueID;
	monthTable.style.width = "100%";
	var monthRow = monthTable.insertRow();
	calendarMonthCell = monthRow.insertCell();
	calendarYearCell = monthRow.insertCell();	
	calendarYearCell.style.width = "40%";
	monthCell.appendChild(monthTable);
	
	var rightCell = calendarChosersRow.insertCell();
	rightCell.style.fontFamily = "Marlett";
	rightCell.style.fontSize = "12";	
	rightCell.innerHTML = "4";

	rightCell.align="right";
	rightCell.attachEvent("onclick", function(){setCalendarNextMonth(event.ctrlKey)});
	rightCell.attachEvent("ondblclick", function(){setCalendarNextMonth(event.ctrlKey)});	
	rightCell.attachEvent("onmousedown", function(){event.srcElement.setCapture()});						
	rightCell.attachEvent("onmouseup", function(){event.srcElement.releaseCapture()});									
	rightCell.attachEvent("onmouseenter", function(){highlightCell(event.srcElement)});	
	rightCell.attachEvent("onmouseout", function(){unhighlightCell(event.srcElement)});		
	
	var daysRow = table.insertRow();
	for (var i= firstWeekDay;i< firstWeekDay + 7;i++){
		var cell = daysRow.insertCell();
		cell.innerText = weekDays[i];
		cell.className = 'header-' + uniqueID;
	}
	
	for (var i=0;i<6;i++){
		var row = table.insertRow();
		var week = new Array();
		for (var j=0;j<7;j++){
			var cell = row.insertCell();
			cell.innerText = j +"" + j;
			cell.align="right";
			cell.className = "day-" + uniqueID;
			cell.attachEvent("onclick", chooseCalendarValue);			
			cell.attachEvent("onmousedown", function(){event.srcElement.setCapture()});						
			cell.attachEvent("onmouseup", function(){event.srcElement.releaseCapture()});									
			cell.attachEvent("onmouseenter", function(){highlightCell(event.srcElement)});	
			cell.attachEvent("onmouseout", function(){unhighlightCell(event.srcElement)});		
			
			var dayElement = new Array();
			dayElement["value"] = "";
			dayElement["cell"] = cell;
			week[week.length] = dayElement;
		}
		dates[dates.length] = week;
	}
	var todaysRow = table.insertRow();
	todaysCell = todaysRow.insertCell();
	todaysCell.colSpan = 7;
	todaysCell.align = "center";
	todaysCell.innerText = "Today";
	todaysCell.style.paddingTop = "3";
	todaysCell.style.paddingBottom = "3";
	todaysCell.attachEvent("onclick", chooseCalendarValue);			
	todaysCell.attachEvent("onmousedown", function(){event.srcElement.setCapture()});						
	todaysCell.attachEvent("onmouseup", function(){event.srcElement.releaseCapture()});									
	todaysCell.attachEvent("onmouseenter", function(){highlightCell(event.srcElement)});	
	todaysCell.attachEvent("onmouseout", function(){unhighlightCell(event.srcElement)});		

	todaysCell.className = "day-" + uniqueID;
	listCell.appendChild(table);
}
</script>
<public:component>