// <!-- <![CDATA[

// Project: Dynamic Date Selector (DtTvB) - 2006-03-16
// Script featured on JavaScript Kit- http://www.javascriptkit.com
// Code begin...
// Set today's date and save it for later.
var ds_full_date = new Date();

var ds_c_month = ds_full_date.getMonth() + 1;
var ds_c_year = ds_full_date.getFullYear();
var ds_c_date = ds_full_date.getDate();

var ds_i_month = ds_c_month;
var ds_i_year = ds_c_year;
var ds_i_date = ds_c_date;
var ds_c_type = "c";

// Get Element By Id
function ds_getel(id) {
	return document.getElementById(id);
}

// Get the left and the top of the element.
function ds_getleft(el) {
	var tmp = el.offsetLeft;
	el = el.offsetParent
	while(el) {
		tmp += el.offsetLeft;
		el = el.offsetParent;
	}
	return tmp;
}
function ds_gettop(el) {
	var tmp = el.offsetTop;
	el = el.offsetParent
	while(el) {
		tmp += el.offsetTop;
		el = el.offsetParent;
	}
	return tmp;
}

// Output Element
var ds_oe = ds_getel('ds_calclass');
// Container
var ds_ce = ds_getel('ds_conclass');

// Output Buffering
var ds_ob = ''; 
function ds_ob_clean() {
	ds_ob = '';
}
function ds_ob_flush() {
	ds_oe.innerHTML = ds_ob;
	ds_ob_clean();
}
function ds_echo(t) {
	ds_ob += t;
}

var ds_element; // Text Element...

var ds_monthnames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
]; // You can translate it for your language.

var ds_daynames = [
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
]; // You can translate it for your language.


// Annual calendar template
function ds_template_year_main_above(y) {
	if (ds_c_type == "pfy") {
		return '<table cellpadding="0" cellspacing="0" class="' + ds_c_type + 's_tbl">'
			+ '<tr>'
			+ '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_ypy();">&lt;&lt;</td>'
			+ '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_hi();" colspan="2">' + y + '</td>'
			+ '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_yny();">&gt;&gt;</td>'
			+ '</tr>'
			+ '<tr>';
	}
	else {
	return '<table cellpadding="0" cellspacing="0" class="' + ds_c_type + 's_tbl">'
	     + '<tr>'
		 + '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_ypy();"><b>&lt;&lt;</b></td>'
		 + '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_hi();" colspan="2">' + y + '</td>'
		 + '<td class="' + ds_c_type + 's_head">'
		 + '<img style="vertical-align:middle;" title="printer friendly calendar" src="images/buttons/printer.gif" onclick="ds_openpopup(1);">'
		 + '&nbsp;&nbsp;<b style="cursor: pointer" onclick="ds_yny();">&gt;&gt;</b></td>'
		 + '</tr>'
		 + '<tr>';
	}
}

// Calendar template
function ds_template_main_above(t) {
	if (ds_c_type == "c") {
	return '<table cellpadding="3" cellspacing="1" class="' + ds_c_type + 's_tbl">'
	     + '<tr>'
		 + '<td class="' + ds_c_type + 's_head"><img height="20" title="previous year.." src="images/buttons/lastyear.gif" onclick="ds_py();">'
		 + '<img height="20" title="previous month.." src="images/buttons/left.gif" onclick="ds_pm();">'
		 + '<img height="20" title="Annual calendar.." src="images/buttons/yearselect.gif" onclick="ds_openpopup(3);"></td>'

		 + '<td class="' + ds_c_type + 's_head" colspan="5">'
		 + '<img height="20" title="go to January" src="images/buttons/jcal.gif" onclick="ds_jmpm(1);">'
		 + '<img height="20" title="go to February" src="images/buttons/fcal.gif" onclick="ds_jmpm(2);">'
		 + '<img height="20" title="go to March" src="images/buttons/mcal.gif" onclick="ds_jmpm(3);">'
		 + '<img height="20" title="go to April" src="images/buttons/acal.gif" onclick="ds_jmpm(4);">'
		 + '<img height="20" title="go to May" src="images/buttons/mcal.gif" onclick="ds_jmpm(5);">'
		 + '<img height="20" title="go to June" src="images/buttons/jcal.gif" onclick="ds_jmpm(6);">'
		 + '<img height="20" title="go to July" src="images/buttons/jcal.gif" onclick="ds_jmpm(7);">'
		 + '<img height="20" title="go to August" src="images/buttons/acal.gif" onclick="ds_jmpm(8);">'
		 + '<img height="20" title="go to September" src="images/buttons/scal.gif" onclick="ds_jmpm(9);">'
		 + '<img height="20" title="go to October" src="images/buttons/ocal.gif" onclick="ds_jmpm(10);">'
		 + '<img height="20" title="go to November" src="images/buttons/ncal.gif" onclick="ds_jmpm(11);">'
		 + '<img height="20" title="go to December" src="images/buttons/dcal.gif" onclick="ds_jmpm(12);"></td>'

		 + '<td class="' + ds_c_type + 's_head"><img height="20" title="printer friendly calendar" src="images/buttons/printer.gif" onclick="ds_openpopup(2);">'
		 + '<img height="20" title="next month.." src="images/buttons/right.gif" onclick="ds_nm();">'
		 + '<img height="20" title="next year.." src="images/buttons/nextyear.gif" onclick="ds_ny();"></td>'
		 + '</tr>'
	     + '<tr>'
		 + '<td colspan="7" class="' + ds_c_type + 's_head">' + t + '</td>'
		 + '</tr>'
		 + '<tr>';

	}
	else if (ds_c_type == "pfc"){
	return '<table cellpadding="3" cellspacing="1" class="' + ds_c_type + 's_tbl">'
	     + '<tr>'
		 + '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_py();">&lt;&lt;</td>'
		 + '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_pm();">&lt;</td>'
		 + '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_hi();" colspan="3">' + t + '</td>'
		 + '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_nm();">&gt;</td>'
		 + '<td class="' + ds_c_type + 's_head" style="cursor: pointer" onclick="ds_ny();">&gt;&gt;</td>'
		 + '</tr>'
		 + '<tr>';
	}
	else if (ds_c_type == 'y' || ds_c_type == 'pfy'){
	return '<table cellpadding="3" cellspacing="1" class="' + ds_c_type + 's_tbl">'
	     + '<tr>'
		 + '<td colspan="7" class="' + ds_c_type + 's_head">' + t + '</td>'
		 + '</tr>'
		 + '<tr>';
	}
	return '<table cellpadding="3" cellspacing="1" class="ds_tbl">'
	     + '<tr>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_py();">&lt;&lt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_pm();">&lt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_hi();" colspan="3">[Close]</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_nm();">&gt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_ny();">&gt;&gt;</td>'
		 + '</tr>'
	     + '<tr>'
		 + '<td colspan="7" class="ds_head">' + t + '</td>'
		 + '</tr>'
		 + '<tr>';
}

function ds_template_day_row(t) {
	return '<td class="' + ds_c_type + 's_subhead">' + t + '</td>';
	// Define width in CSS, XHTML 1.0 Strict doesn't have width property for it.
}

function ds_template_new_week() {
	return '</tr><tr>';
}

function ds_template_blank_cell(colspan) {
	return '<td colspan="' + colspan + '"></td>'
}

function ds_template_day(d, m, y, w, dw) {
	
	if (ds_c_type == "c" || ds_c_type == "pfc"){
		var index = checkevents(d,m,y,w,dw);
		if (index >= 0){
			var label = geteventname(index);
			var type = geteventtype(index);
			var link = geteventlink(index);
			
			if (type == "S" ) { 
				if (link == "")
					return '<td class="' + ds_c_type + 's_scheduled_cell">' + d + ' <br>' + label + '</td>';
				else
					return '<td class="' + ds_c_type + 's_scheduled_cell"><a href="http://' + link + '" title="' + link + '" target="new">' + d + ' <br>' + label + '</a></td>';
			}
			else if (type == "T") {
				return '<td class="' + ds_c_type + 's_today_cell">' + d + ' <br>' + label + '</td>';
			}
			return '<td class="' + ds_c_type + 's_holiday_cell">' + d + ' <br>' + label + '</td>';
		}
		return '<td class="' + ds_c_type + 's_cell">' + d + '  <br> <br>&nbsp;</td>';
	}
	// Define width the day row.
	
	if (y < ds_i_year && ds_c_type != "y" && ds_c_type != "pfy")
		return '<td class="ds_past_cell" onclick="ds_past_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
	else if (m < ds_i_month && y <= ds_i_year && ds_c_type != "y" && ds_c_type != "pfy")
		return '<td class="ds_past_cell" onclick="ds_past_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
	else if (d <= ds_i_date && m <= ds_i_month && y <= ds_i_year && ds_c_type != "y" && ds_c_type != "pfy")
		return '<td class="ds_past_cell" onclick="ds_past_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
	else { 
		var index = checkevents(d,m,y,w,dw);
		if (index >= 0){
			var label = geteventname(index);
			var type = geteventtype(index);
			if (type == "B")
				return '<td class="' + ds_c_type + 's_holiday_cell" title="' + label + '" onclick="' + ds_c_type + 's_blocked_onclick(' + d + ',' + m + ',' + y + ',' + index + ')">' + d + '</td>';
			else if (type == "N") {
				if (ds_c_type == "y" || ds_c_type == "pfy")
					return '<td class="' + ds_c_type + 's_holiday_cell" title="' + label + '" onclick="' + ds_c_type + 's_blocked_onclick(' + d + ',' + m + ',' + y + ',' + index + ')">' + d + '</td>';
				return '<td class="' + ds_c_type + 's_nb_holiday_cell" title="' + label + '" onclick="' + ds_c_type + 's_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
			}
			else if (type == "T") {
				return '<td class="' + ds_c_type + 's_today_cell" title="' + label + '" onclick="' + ds_c_type + 's_blocked_onclick(' + d + ',' + m + ',' + y + ',' + index + ')">' + d + '</td>';
			}
			else
				return '<td class="' + ds_c_type + 's_scheduled_cell" title="' + label + '" onclick="' + ds_c_type + 's_blocked_onclick(' + d + ',' + m + ',' + y + ',' + index + ')">' + d + '</td>';
		}
	}
	// fall into enabling the click on this date
	return '<td class="' + ds_c_type + 's_cell" onclick="' + ds_c_type + 's_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
	// Define width the day row.
}

function ds_template_main_below() {
	return '</tr>'
	     + '</table>';
}

// This one draws a one year calendar...
function ds_draw_year(y) {
	// First clean the output buffer.
	ds_ob_clean();
	// Here we go, do the header
	ds_echo (ds_template_year_main_above(y));
	month = 1;
	for (r = 0; r < 3; r++)	{
		ds_echo (ds_template_new_week());
		for (column = 0; column < 4; column++) {
			ds_echo ('<td style="vertical-align:top;">');
			ds_draw_calendar(month,y);
			ds_echo ('</td>');
			month++;
		}
	}
	ds_ob_flush();
}

// This one draws calendar...
function ds_draw_calendar(m, y) {
	// First clean the output buffer.
	if (ds_c_type != "y" && ds_c_type != "pfy") {
		ds_ob_clean();
		ds_echo (ds_template_main_above(ds_monthnames[m - 1] + ' ' + y));
	}
	else
		ds_echo (ds_template_main_above(ds_monthnames[m - 1]));
	
	for (i = 0; i < 7; i ++) {
		ds_echo (ds_template_day_row(ds_daynames[i]));
	}
	// Make a date object.
	var ds_dc_date = new Date();
	ds_dc_date.setMonth(m - 1);
	ds_dc_date.setFullYear(y);
	ds_dc_date.setDate(1);
	if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
		days = 31;
	} else if (m == 4 || m == 6 || m == 9 || m == 11) {
		days = 30;
	} else {
		days = (y % 4 == 0) ? 29 : 28;
	}
	var first_day = ds_dc_date.getDay();
	var first_loop = 1;
	var w = 1;
	
	// Start the first week
	ds_echo (ds_template_new_week());
	// If sunday is not the first day of the month, make a blank cell...
	if (first_day != 0) {
		ds_echo (ds_template_blank_cell(first_day));
	}
	var j = first_day;
	for (i = 0; i < days; i ++) {
		// Today is sunday, make a new week.
		// If this sunday is the first day of the month,
		// we've made a new row for you already.
		if (j == 0 && !first_loop) {
			// New week!!
			ds_echo (ds_template_new_week());
			w ++;
		}
		// Make a row of that day!
		ds_echo (ds_template_day(i + 1, m, y, w, j));
		// This is not first loop anymore...
		first_loop = 0;
		// What is the next day?
		j ++;
		j %= 7;
	}
	// Do the footer
	ds_echo (ds_template_main_below());
	// And let's display..
	if (ds_c_type != "y" && ds_c_type != "pfy")
		ds_ob_flush();
}

// A function to show the calendar.
// When user clicks on the date, it will set the contents of t.
function ds_sh(ct) {
	// Set the element to set...
	ds_element = document.getElementById("date");
	ds_c_type = ct;
	
	// check to see if we are popping up a calendar for printing
	// and if so, grab the date from the URL
	if (ct == "y" || ct == "pfy" || ct == "pfc") {
		var date_str = location.search;
		var length = date_str.length;
		if (length == 13) 
			ds_c_month = date_str.substr(6, 2);
		else
			ds_c_month = date_str.substr(6, 1);
		ds_c_year = date_str.substr(length - 4,4);
	}
	else {
	// make a new date, and set the current month and year.
		var ds_sh_date = new Date();
		ds_c_month = ds_sh_date.getMonth() + 1;
		ds_c_year = ds_sh_date.getFullYear();
	}
	// Draw the calendar
	if (ct == "y" || ct == "pfy")
		ds_draw_year(ds_c_year);
	else
		ds_draw_calendar(ds_c_month, ds_c_year);
	// To change the position properly, we must show it first.
	ds_ce.style.display = '';
	// Move the calendar container!
	if (ct == "d") {
		the_left = ds_getleft(ds_element) - 70;
		the_top = ds_gettop(ds_element) + ds_element.offsetHeight - 140;
	}
	else {
		the_left = ds_getleft(ds_element);
		the_top = ds_gettop(ds_element) + ds_element.offsetHeight;
	}
	ds_ce.style.left = the_left + 'px';
	ds_ce.style.top = the_top + 'px';
}

// Hide the calendar.
function ds_hi() {
	ds_ce.style.display = 'none';
}

// Moves to the next month...
function ds_nm() {
	// Increase the current month.
	ds_c_month ++;
	// We have passed December, let's go to the next year.
	// Increase the current year, and set the current month to January.
	if (ds_c_month > 12) {
		ds_c_month = 1; 
		ds_c_year++;
	}
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the previous month...
function ds_pm() {
	ds_c_month = ds_c_month - 1; // Can't use dash-dash here, it will make the page invalid.
	// We have passed January, let's go back to the previous year.
	// Decrease the current year, and set the current month to December.
	if (ds_c_month < 1) {
		ds_c_month = 12; 
		ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
	}
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the specified month... even if it is already there...
function ds_jmpm(month) {
	ds_c_month = month;  
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}


// Moves to the next year...
function ds_ny() {
	// Increase the current year.
	ds_c_year++;
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the next year...
function ds_yny() {
	// Increase the current year.
	ds_c_year++;
	// Redraw the calendar.
	ds_draw_year(ds_c_year);
}

// Moves to the previous year...
function ds_py() {
	// Decrease the current year.
	ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the previous year...
function ds_ypy() {
	// Decrease the current year.
	ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
	// Redraw the calendar.
	ds_draw_year(ds_c_year);
}
// Format the date to output.
function ds_format_date(d, m, y) {
	// month name
	month = ds_monthnames[m - 1];
	// Month DD, YYYY
	return month + ' ' + d + ', ' + y;
}

// When the user clicks the day.
function ds_onclick(d, m, y) {
	// Hide the calendar.
	ds_hi();
	// Set the value of it, if we can.
	if (typeof(ds_element.value) != 'undefined') {
		ds_element.value = ds_format_date(d, m, y);
	// Maybe we want to set the HTML in it.
	} else if (typeof(ds_element.innerHTML) != 'undefined') {
		ds_element.innerHTML = ds_format_date(d, m, y);
	// I don't know how should we display it, just alert it to user.
	} else {
		alert (ds_format_date(d, m, y));
	}
}

// When the user clicks an invalid day.
function ds_past_onclick(d, m, y) {
	alert ("You cannot schedule the studio for a date in the past, please \nselect a date after today's date, " 
	+ ds_format_date(ds_i_date, ds_i_month, ds_i_year) + " .... ");
}

// When the user clicks a holiday or scheduled day.
function ds_blocked_onclick(d, m, y, i) {
	alert ("You cannot schedule the studio for this date, " + geteventname(i) + " already\nscheduled for " 
	+ ds_format_date(d, m, y) + " .... ");
}

// When the user clicks the day.
function ys_onclick(d, m, y) {
	alert (ds_format_date(d, m, y));
}

// When the user clicks a holiday or scheduled day.
function ys_blocked_onclick(d, m, y, i) {
	alert ("Event: " + geteventname(i) + " \non: " 
	+ ds_format_date(d, m, y) + " .... ");
}
// When the user clicks the day.
function pfys_onclick(d, m, y) {
	alert (ds_format_date(d, m, y));
}

// When the user clicks a holiday or scheduled day.
function pfys_blocked_onclick(d, m, y, i) {
	alert ("Event: " + geteventname(i) + " \non: " 
	+ ds_format_date(d, m, y) + " .... ");
}

function ds_openpopup(target){
	var width=screen.width;
	var height=screen.height;

	var x, y = 0;

	if (target == 1) {
		var targetUrl="pfy_calendar.php?date=" + ds_c_month + "," + ds_c_year + "";
		width = 900;
		height = 700;
		}
	else if (target == 2) {
		var targetUrl="pfm_calendar.php?date=" + ds_c_month + "," + ds_c_year + "";
		width = 600;
		height = 550;
		}
	else {
		var targetUrl="y_calendar.php?date=" + ds_c_month + "," + ds_c_year + "";
		width = 900;
		height = 700;
	}
	// adjust window to the center
	x = (screen.width - width) / 2;
	y = (screen.height - height) / 2;
	
	newwin=window.open("","","resizable=1,menubar=1")
	if (document.all){
		newwin.moveTo(x,y)
		newwin.resizeTo(width,height)
	}
	newwin.location=targetUrl;
}

// And here is the end.


