var today = new Date();

function showDate(string) {	
	if (string.indexOf('_Weekday_') != -1) string = showDateReplace(string, '_Weekday_', 'showDate_weekdayFull', 'getDay');
	if (string.indexOf('_weekday_') != -1) string = showDateReplace(string, '_weekday_', 'showDate_weekdayFull', 'getDay');
	if (string.indexOf('_WEEKDAY_') != -1) string = showDateReplace(string, '_WEEKDAY_', 'showDate_weekdayFull', 'getDay');
	if (string.indexOf('_Wdy_') != -1) string = showDateReplace(string, '_Wdy_', 'showDate_weekdayShort', 'getDay');
	if (string.indexOf('_wdy_') != -1) string = showDateReplace(string, '_wdy_', 'showDate_weekdayShort', 'getDay');
	if (string.indexOf('_WDY_') != -1) string = showDateReplace(string, '_WDY_', 'showDate_weekdayShort', 'getDay');
	
	if (string.indexOf('_DD_') != -1) {
		var pos = string.indexOf('_DD_');
		var day = parseInt(today.getDate());
		if (day.toString.length == 1) day = '0' + day;
		string = string.substring(0, pos) + day + string.substring(pos + 4);
	}
	if (string.indexOf('_D_') != -1) {
		var pos = string.indexOf('_D_');
		string = string.substring(0, pos) + parseInt(today.getDate()) + string.substring(pos + 3);
	}

	if (string.indexOf('_YYYY_') != -1) {
		var pos = string.indexOf('_YYYY_');
		var year = today.getFullYear();
		if (year < 1000) year += 1900;
		string = string.substring(0, pos) + year + string.substring(pos + 6);
	}
	if (string.indexOf('_YY_') != -1) {
		var pos = string.indexOf('_YY_');
		var year = today.getFullYear();
		if (year > 99) year = year.toString().substring(year.toString().length - 2);
		string = string.substring(0, pos) + year + string.substring(pos + 4);
	}

	if (string.indexOf('_Month_') != -1) string = showDateReplace(string, '_Month_', 'showDate_monthFull', 'getMonth');
	if (string.indexOf('_month_') != -1) string = showDateReplace(string, '_month_', 'showDate_monthFull', 'getMonth');
	if (string.indexOf('_MONTH_') != -1) string = showDateReplace(string, '_MONTH_', 'showDate_monthFull', 'getMonth');
	if (string.indexOf('_Mon_') != -1) string = showDateReplace(string, '_Mon_', 'showDate_monthShort', 'getMonth');
	if (string.indexOf('_mon_') != -1) string = showDateReplace(string, '_mon_', 'showDate_monthShort', 'getMonth');
	if (string.indexOf('_MON_') != -1) string = showDateReplace(string, '_MON_', 'showDate_monthShort', 'getMonth');
	if (string.indexOf('_MM_') != -1) {
		var pos = string.indexOf('_MM_');
		var month = parseInt(today.getMonth() + 1);
		if (month.toString.length == 1) month = '0' + month;
		string = string.substring(0, pos) + month + string.substring(pos + 4);
	}
	if (string.indexOf('_M_') != -1) {
		var pos = string.indexOf('_M_');
		string = string.substring(0, pos) + parseInt(today.getMonth() + 1) + string.substring(pos + 3);
	}

	document.write(string);
}

function showDateReplace(string, replace, array, dateFunction) {
	var pos = string.indexOf(replace);
	var insert = eval(array + '[today.' + dateFunction + '()]');
	if (replace == replace.toLowerCase) insert = insert.toLowerCase();
	else if (replace == replace.toUpperCase) insert = insert.toUpperCase();
 	return (string.substring(0, pos) + insert + string.substring(pos + replace.length));
}


function showDateCustom(string,year,month,day) {	
	theDay = new Date(year,month,day);
	if (string.indexOf('_Weekday_') != -1) string = showDateReplace(string, '_Weekday_', 'showDate_weekdayFull', 'getDay');
	if (string.indexOf('_weekday_') != -1) string = showDateReplace(string, '_weekday_', 'showDate_weekdayFull', 'getDay');
	if (string.indexOf('_WEEKDAY_') != -1) string = showDateReplace(string, '_WEEKDAY_', 'showDate_weekdayFull', 'getDay');
	if (string.indexOf('_Wdy_') != -1) string = showDateReplace(string, '_Wdy_', 'showDate_weekdayShort', 'getDay');
	if (string.indexOf('_wdy_') != -1) string = showDateReplace(string, '_wdy_', 'showDate_weekdayShort', 'getDay');
	if (string.indexOf('_WDY_') != -1) string = showDateReplace(string, '_WDY_', 'showDate_weekdayShort', 'getDay');
	
	if (string.indexOf('_DD_') != -1) {
		var pos = string.indexOf('_DD_');
		var day = parseInt(theDay.getDate());
		if (day.toString.length == 1) day = '0' + day;
		string = string.substring(0, pos) + day + string.substring(pos + 4);
	}
	if (string.indexOf('_D_') != -1) {
		var pos = string.indexOf('_D_');
		string = string.substring(0, pos) + parseInt(theDay.getDate()) + string.substring(pos + 3);
	}

	if (string.indexOf('_YYYY_') != -1) {
		var pos = string.indexOf('_YYYY_');
		var year =theDay.getFullYear();
		if (year < 1000) year += 1900;
		string = string.substring(0, pos) + year + string.substring(pos + 6);
	}
	if (string.indexOf('_YY_') != -1) {
		var pos = string.indexOf('_YY_');
		var year =theDay.getFullYear();
		if (year > 99) year = year.toString().substring(year.toString().length - 2);
		string = string.substring(0, pos) + year + string.substring(pos + 4);
	}

	if (string.indexOf('_Month_') != -1) string = showDateReplace(string, '_Month_', 'showDate_monthFull', 'getMonth');
	if (string.indexOf('_month_') != -1) string = showDateReplace(string, '_month_', 'showDate_monthFull', 'getMonth');
	if (string.indexOf('_MONTH_') != -1) string = showDateReplace(string, '_MONTH_', 'showDate_monthFull', 'getMonth');
	if (string.indexOf('_Mon_') != -1) string = showDateReplace(string, '_Mon_', 'showDate_monthShort', 'getMonth');
	if (string.indexOf('_mon_') != -1) string = showDateReplace(string, '_mon_', 'showDate_monthShort', 'getMonth');
	if (string.indexOf('_MON_') != -1) string = showDateReplace(string, '_MON_', 'showDate_monthShort', 'getMonth');
	if (string.indexOf('_MM_') != -1) {
		var pos = string.indexOf('_MM_');
		var month = parseInt(theDay.getMonth() + 1);
		if (month.toString.length == 1) month = '0' + month;
		string = string.substring(0, pos) + month + string.substring(pos + 4);
	}
	if (string.indexOf('_M_') != -1) {
		var pos = string.indexOf('_M_');
		string = string.substring(0, pos) + parseInt(theDay.getMonth() + 1) + string.substring(pos + 3);
	}

	document.write(string);
}

<!-- CALENDAR SCRIPTING -->
// months as they appear in the calendar's title
var ARR_MONTHS = showDate_monthFull;
// week day titles as they appear on the calendar
var ARR_WEEKDAYSNAMED = showDate_weekdayFull;
var ARR_WEEKDAYS = showDate_weekdayShort;
// day week starts from (normally 0-Su or 1-Mo)
var NUM_WEEKSTART = 1;
// path to the directory where calendar images are stored. trailing slash req.

var re_url = new RegExp('datetime=(\\-?\\d+)');

//var dt_current = (re_url.exec(String(window.location))
//	? new Date(new Number(RegExp.$1)) : new Date());

var re_id = new RegExp('id=(\\d+)');
var num_id = (re_id.exec(String(window.location))
	? new Number(RegExp.$1) : 0);

//var obj_caller = (window.opener ? window.opener.calendars[num_id] : null);
var obj_caller = null;

// get same date in the previous year
var dt_prev_year = new Date(dt_current);
dt_prev_year.setFullYear(dt_prev_year.getFullYear() - 1);
var dt_prev_year_str = dt_prev_year.getDate() + '-' + (dt_prev_year.getMonth()+1) + '-' + dt_prev_year.getFullYear();
if (dt_prev_year.getDate() != dt_current.getDate())
        dt_prev_year.setDate(0);

// get same date in the next year
var dt_next_year = new Date(dt_current);
dt_next_year.setFullYear(dt_next_year.getFullYear() + 1);
var dt_next_year_str = dt_next_year.getDate() + '-' + (dt_next_year.getMonth()+1) + '-' + dt_next_year.getFullYear();
if (dt_next_year.getDate() != dt_current.getDate())
        dt_next_year.setDate(0);

// get same date in the previous month
var dt_prev_month = new Date(dt_current);
dt_prev_month.setMonth(dt_prev_month.getMonth() - 1);
var dt_prev_month_str = dt_prev_month.getDate() + '-' + (dt_prev_month.getMonth()+1) + '-' + dt_prev_month.getFullYear();
if (dt_prev_month.getDate() != dt_current.getDate())
        dt_prev_month.setDate(0);

// get same date in the next month
var dt_next_month = new Date(dt_current);
dt_next_month.setMonth(dt_next_month.getMonth() + 1);
var dt_next_month_str = dt_next_month.getDate() + '-' + (dt_next_month.getMonth()+1) + '-' + dt_next_month.getFullYear();
if (dt_next_month.getDate() != dt_current.getDate())
        dt_next_month.setDate(0);

// get first day to display in the grid for current month

var dt_firstday = new Date(dt_current);
dt_firstday.setDate(1);
dt_firstday.setDate(1 - (7 + dt_firstday.getDay() - NUM_WEEKSTART) % 7);

// function passing selected date to calling window
function set_datetime(n_datetime, b_close) {
	document.selecteddate.currentDate.value = n_datetime;

	// Set the hidden field 'firstWeekDate'
	dt = new Date(n_datetime);
	yr = dt.getFullYear();	mt = dt.getMonth()+1;	dy = dt.getDate();
	if (mt < 10) { mt = '0' + mt; }
	if (dy < 10) { dy = '0' + dy; }
	dd = yr + '-' + mt + '-' + dy;
	document.selecteddate.currentDateText.value = dd;
	document.selecteddate.popup.value = '0';
	document.selecteddate.target = '_self';
	document.selecteddate.submit();
}

function set_datetime_popup(n_datetime, b_close) {
	document.selecteddate.currentDate.value = n_datetime;

	// Set the hidden field 'firstWeekDate'
	dt = new Date(n_datetime);
	yr = dt.getFullYear();	mt = dt.getMonth()+1;	dy = dt.getDate();
	if (mt < 10) { mt = '0' + mt; }
	if (dy < 10) { dy = '0' + dy; }
	dd = yr + '-' + mt + '-' + dy;
	document.selecteddate.currentDateText.value = dd;
	document.selecteddate.popup.value = 1;
	document.selecteddate.target = 'calendar';
	window.open('','calendar','menubar=no,width=815,height=562,scrollbars=yes,resizable=yes,status=no');
	document.selecteddate.submit();
}

// Showing today's date
function showToday(){
 	var today = new Date();
 	var tt = today.getTime()
 	document.selecteddate.currentDate.value=tt;

	// Set the hidden field 'firstWeekDate'
	dt = new Date(tt);
	yr = dt.getFullYear();	mt = dt.getMonth()+1;	dy = dt.getDate();
	if (mt < 10) { mt = '0' + mt; }
	if (dy < 10) { dy = '0' + dy; }
	dd = yr + '-' + mt + '-' + dy;
	document.selecteddate.currentDateText.value = dd;

	document.selecteddate.submit();
}
// finding the week number of a day
function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
             Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d;
}

function timeDifference(laterdate,earlierdate) {
    var difference = laterdate.getTime() - earlierdate.getTime();

    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24
    var hoursDifference = Math.floor(difference/1000/60/60);
    difference -= hoursDifference*1000*60*60
    var minutesDifference = Math.floor(difference/1000/60);
    difference -= minutesDifference*1000*60
    var secondsDifference = Math.floor(difference/1000);
    
    daysDifference++;
    return daysDifference
    //document.write('['+daysDifference+']');
}


function WriteDateTop(dateR,i){
	i=i-1;
	var d = (i + 1) % 7;
	var rDate = new Date(dateR.getFullYear(),dateR.getMonth(),dateR.getDate()+i)
	var cmpDate = new Date(dt_current.getFullYear(),dt_current.getMonth(),dt_current.getDate())
	var displayedday = ARR_WEEKDAYSNAMED[d] +', '+(rDate.getDate())+' '+ARR_MONTHS[rDate.getMonth()]+' '+rDate.getFullYear()
	if ((timeDifference(rDate,dt_current)) == 0 ){displayedday = '<b>'+displayedday+'</b>'}
	document.write(displayedday)
	return rDate
}

function zeropad(num) {
  if (num < 10)
    return ("0" + num);
  else
    return num;
  }

StartOfLastWeekMS = dt_current.getTime() - ( 24*60*60*1000*(dt_current.getDay()-1)); 
if (dt_current.getDay() == 0){StartOfLastWeekMS = dt_current.getTime() - ( 24*60*60*1000*(dt_current.getDay()+6));}
StartOfLastWeek = new Date(StartOfLastWeekMS);

