function initPage(){
	// trianing log calendar
	calendarInit(_SYSTEM_USER_ID, intDay='', intMonth='', intYear='');
	
	// Calendar
	$("#txtDailyLogDate").datepicker({ dateFormat: _CALENDAR_DATE_FORMAT, speed: '', firstDay: _CALENDAR_FIRST_DAY_OF_WEEK }); 
}
		
function calendarInit(intUserID, intDay, intMonth, intYear) {
	var myCal = new createCalendar('training_calendar', 'calendar', 'this_calendar', intUserID, intDay, intMonth, intYear);
}

function calendarLoaded() {
	$(".tooltip").each(function(i){
		$(this).cluetip({local:true, arrows: true, cluetipClass: "ui-widget" });
		});
}

var reqCalendar = makeXHR();

function createCalendar(parentID, theID, idname, userID, intDay, intMonth, intYear) {
	var theBody = document.getElementById(parentID);
	var theCal = document.createElement('div');
	var theInput = document.createElement('input');
	var compareCal = 0;

	theInput.id = theID;
	theCal.id = idname;
	theCal.name = idname;
	theInput.value = idname;
	theInput.type = 'hidden';
	theBody.appendChild(theInput);
	theBody.appendChild(theCal);

	var currentTime = new Date();

	if(intMonth == "") {
		var month = currentTime.getMonth() + 1;
	} else {
		var month = intMonth;
	}
	if(intDay == "") {
		var day = currentTime.getDate();
	} else {
		var day = intDay;
	}
	if(intYear == "") {
		var year = currentTime.getFullYear();
	} else {
		var year = intYear;
	}

	ajax_regenerateCal(month, year, day, theID, userID);
}

function ajax_regenerateCal(nmonth, nyear, nday, xnid, nUserID){
	var param = '?nday=' + nday + '&nmonth=' + nmonth + '&nyear=' + nyear + '&nid=' + xnid + '&user_id=' + nUserID;
	reqCalendar.open('GET', 'lib/generate_training_calendar.php' + param, true);
	reqCalendar.send('');
	reqCalendar.onreadystatechange = parse_regenerateCal;
}

function parse_regenerateCal(){
	if(reqCalendar.readyState == 4){
		var htmlCalendar = trimme(reqCalendar.responseText);
		document.getElementById('training_calendar').innerHTML = htmlCalendar;
	}
}

function trimme(sInString) {
	sInString = sInString.replace ( /^s+/g, "" );// strip leading
	return sInString.replace( /s+$/g, "" );// strip trailing
}

function regenerateCal(nmonth, nyear, nday, nid, nUserID) {
	var compareCal = 0;
	if(compareCal == 1) {
		var val_cal1 = (nid == cal1 ? nyear : document.getElementById(cal1 + '_year').value) + '' + (nid == 'cal1' ? (nmonth < 10 ? '0'+nmonth : nmonth) : (document.getElementById(cal1 + '_month').value < 10 ? '0'+document.getElementById(cal1 + '_month').value : document.getElementById(cal1 + '_month').value)) + '' + (nid == cal1 ? (nday < 10 ? '0'+nday : nday) : (document.getElementById(cal1 + '_day').value < 10 ? '0'+document.getElementById(cal1 + '_day').value : document.getElementById(cal1 + '_day').value));
		var val_cal2 = (nid == cal2 ? nyear : document.getElementById(cal2 + '_year').value) + '' + (nid == cal2 ? (nmonth < 10 ? '0'+nmonth : nmonth) : (document.getElementById(cal1 + '_month').value < 10 ? '0'+document.getElementById(cal1 + '_month').value : document.getElementById(cal1 + '_month').value)) + '' + (nid == cal2 ? (nday < 10 ? '0'+nday : nday) : (document.getElementById(cal1 + '_day').value < 10 ? '0'+document.getElementById(cal1 + '_day').value : document.getElementById(cal1 + '_day').value));

		if(val_cal2 < val_cal1) {
			document.getElementById(alertCal).innerHTML = 'Invalid time selection';
		}
		else if(val_cal2 >= val_cal1) {
			document.getElementById(alertCal).innerHTML = '';
		}
	}
	ajax_regenerateCal(nmonth, nyear, nday, nid, nUserID);
}