function getCalendar(aField, aLocale) {
	// Set fields
	triggerElement = document.getElementById(aField);
	dataField = document.getElementById(aField);
	// Set global variables
	calElement = document.getElementById('sy_calendar_' + aField);
	calElement.style.display = (calElement.style.display) ? calElement.style.display : 'none';
	curDate = new Date();
	LOCALE_EN = 'en_CA';
	LOCALE_FR = 'fr_CA';
	
	//today
	todayYear = curDate.getFullYear();
	todayMonth = curDate.getMonth();
	todayDay = curDate.getDate();
	
	elementCount = 0;
	
	//debug;
	isDebug = false;
	
	locale = (aLocale == null || aLocale == '') ? LOCALE_EN:aLocale;
	if ( calElement.style.display == 'none') {
		hideCalendar();
		drawCalendar(curDate.getFullYear(), curDate.getMonth(), curDate.getDate());
	} else {
		hideCalendar();
	}
}

function drawCalendar(aYear, aMonth, aDay) {
	var aDate = new Date(aYear, aMonth, 1);
	var aYear = aDate.getFullYear();
	var aMonth = aDate.getMonth();
	var aDay = aDate.getDate();
	var aWeek = aDate.getDay();
	var aWidth = 15;
	var currentDay = 1;				
	var weekArray = getWeekArray();
	
	// make title
	var html = "<table cellspacing=0 width=105 bgcolor=#ffffff>";
	html = html + "<tr><td colspan=7 align=center>";
	html = html + "	<table cellpadding=0 cellspacing=0 width=\"100%\">";
	html = html + "		<tr><td align=\"left\"><a href=\"javascript:drawCalendar("+(aYear - 1)+"," +(aMonth)+","+ aDay+");\" title=\"previous year\" "+getStyle('move')+">&laquo;</a></td><td "+getStyle('cal')+">"+ aYear + "</td><td align=\"right\"><a href=\"javascript:drawCalendar("+ ( aYear+1)+"," +(aMonth)+","+aDay +");\" title=\"next year\" "+getStyle('move')+">&raquo;</a></td></tr>";
	html = html + "		<tr><td align=\"left\"><a href=\"javascript:drawCalendar("+(aYear)+"," +(aMonth - 1 )+","+ aDay+");\" title=\"previous month\" "+getStyle('move')+">&laquo;</a></td><td "+getStyle('cal')+">" +  getMonth(aMonth) + "</td><td align=\"right\"><a href=\"javascript:drawCalendar("+ ( aYear)+"," +(aMonth + 1)+","+aDay +");\" title=\"next month\" "+getStyle('move')+">&raquo;</a></td></tr>";
	html = html + "	</table>\n";
	html = html + "</td></tr>";
	html = html + "<tr>";
	
	for (var i = 0 ; i < weekArray.length ; i++) {
		html = html + "<td "+ ((i == 0)?getStyle('sundayTitle'):getStyle('title')) + " width=\"" + aWidth + "\">" + weekArray[i] + "</td>";
	}

	// make content
	for (var i = 0 ; i < 31; i++) {
		if (aWeek == 0 || i == 0) {
			html = html + "<tr>";
			if (aWeek != 0) {
				for (var j = 0 ; j < aWeek; j ++) {
					html = html + "<td "+ getStyle('line')+">&nbsp;</td>";
				}
			}
		}
		var cellStyle = (aYear == todayYear && aMonth == todayMonth && currentDay == todayDay)?getStyle('today'):getStyle('line');
		html = html + "<td " + cellStyle + "><a href=\"javascript:setDataField( "+ aYear+"," + (aMonth + 1) + "," + currentDay + ");\"" + ((aWeek==0)?getStyle('sundayA'):getStyle('a'))+ ">" + currentDay + "</a></td>";

		// Set week value
		if ( aWeek < 6 ) {
			aWeek++;
		} else {
			html = html + "</tr>";
			aWeek = 0;
		}
		
		// Break when it is the last day of the month
		aDate = new Date(aYear, aMonth, ++currentDay);
		if (aDate.getMonth() != aMonth) {
			if (aWeek != 0 ) {
				for (aWeek ; aWeek <= 6; aWeek++) {
					html = html + "<td "+ getStyle('line')+">&nbsp;</td>";
				}
			}							
			break;
		}		
	}
	
	html = html + "</tr></table>";
	calElement.innerHTML = html;	
	// Show Calendar
	calElement.style.position = 'absolute';
	calElement.style.zIndex = '100';
	calElement.style.borderLeft = '1px #000000 solid';
	calElement.style.borderRight = '2px #000000 solid';
	calElement.style.borderTop = '1px #000000 solid';
	calElement.style.borderBottom = '1px #000000 solid';
	calElement.style.background = '#FFFFFF';
	elementCount = 0; // reset element count
	var posXY = getElementPosition(triggerElement);
	calElement.style.left = posXY[0] ;
	calElement.style.top =  posXY[1] + triggerElement.offsetHeight + 2;
	calElement.style.display = 'block';
}

function setDataField(yy,mm,dd) {
	//alert('setDataField');
	if (locale == LOCALE_EN) {
		dataField.value = mm + "/" + dd + "/" + yy;
	} else if (locale == LOCALE_FR) {
		dataField.value = yy + "-" + mm + "-" + dd;
	}
	calElement.style.display = "none";
}

function getWeekArray(){
	//alert('getWeekArray');
	if (locale == LOCALE_EN) {
		var val = ['S','M','T','W','T','F','S'];
	} else if (locale == LOCALE_FR) {
		var val = ['S','M','T','W','T','F','S'];
	}
	return val;
}

function getMonth(mm){
	//alert('getMonth');
	if (locale == LOCALE_EN) {
		var val = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	} else if (locale == LOCALE_FR) {
		var val = ['Jan','F&eacute;v','Mars','Avr','Mai','Juin','Juil','Ao&ucirc;t','Sept','Oct','Nov','D&eacute;c'];
	}
	return val[mm];
}

function getStyle(param) {
	titleStyle = "style=\"text-align:center; border-top:#000000 2px solid; border-bottom:#000000 2px solid;font-family: Arial; font-size: 10px; font-weight:bold; color:#000000;\"";
	lineStyle = "style=\"text-align:center; border-bottom:#000000 1px solid; height:11px;\"";
	aStyle = "style=\"font-family: Arial; font-size: 10px; color:#000000;text-decoration:none;\"";
	calStyle = "style=\"text-align:center; font-family: Arial; font-size: 11px; color:#000000;text-decoration:none;\"";
	moveStyle = "style=\"font-family: Arial; font-size: 12px; color:blue; text-decoration:none;\"";
	sundayTitleStyle = "style=\"text-align:center; border-top:#000000 2px solid; border-bottom:#000000 2px solid;font-family: Arial; font-size: 10px; font-weight:bold; color:red;\"";
	sundayAStyle = "style=\"font-family: Arial; font-size: 10px; color:red;text-decoration:none;\"";
	todayStyle = "style=\"background-color:#e6e6e6;text-align:center; border-bottom:#000000 1px solid; height:11px;\"";	
	
	return eval( param + 'Style');
}

function hideCalendar(){
	// hide all calendar
	var collDIV = document.getElementsByTagName('DIV');
	for (var i = 0 ; i < collDIV.length ;i++) {
		debug(i + 'th HideCalendar loop ' + collDIV[i].id);
		if (collDIV[i].id != null && collDIV[i].id != ''){
			debug(i + " in if");
			if ( collDIV[i].id.indexOf('sy_calendar_') == 0 ) {
				collDIV[i].style.display = 'none';
			}
		}
	}	
}

function getElementPosition (o) {
	//alert('getElementPosition');
	var basicX = o.style.posLeft + o.offsetLeft;// + o.clientLeft;
	var basicY =  o.style.posTop + o.offsetTop; // + o.clientTop;
	var posArray;
	var elementCount  = 0;
	
	if (o.tagName == 'TR'|| o.tagName == 'FORM') {
		posArray = [0, 0];
	} else {
		posArray = [basicX, basicY];
	}
	debug( o.tagName + " has  " + o.style.posTop + "," + o.offsetTop + "," + o.clientTop);
	var pNode = o.parentNode;
	if (elementCount++ < 50) { // for limitation of looping 
		if (pNode != null && o.tagName.toUpperCase() != 'BODY'){
			var parentPosArray = getElementPosition (pNode);
			posArray[0] = posArray[0] + parentPosArray[0];
			posArray[1] = posArray[1] + parentPosArray[1];
		}
	}
	return posArray;
} 

function debug(o) {
	if (isDebug) {
		var debugwin = window.open('', 'calDebug');
		debugwin.document.writeln(o)		
		debugwin.document.writeln('<br>');
	}
}