/*********************************************************************************************************/
// JavaScript Document

//CODE A INCLURE DANS LA PAGE : 



//<link href="skins/calendar/calendar.css" rel="stylesheet" type="text/css">

//</head>

//<body>

//<div class="calendar_conclass" id="calendar_conclass" style="display: none;"><div id="calendar_calclass"></div></div>
//<script type="text/javascript" src="skins/calendar/calendar.js"></script>

//Exemple de bouton d'appel :

//<a onclick="get_calendar(this,document.getElementById('jour'),document.getElementById('mois'),document.getElementById('annee'));">Calendrier</a>



// Set the initial date.
var calendar_initial_date = new Date();
calendar_c_month = calendar_initial_date.getMonth() + 1;
calendar_c_year = calendar_initial_date.getFullYear();

// Get the left and the top of the element.
function calendar_getleft(el) {
	var tmp = el.offsetLeft;
	el = el.offsetParent
	while(el) {
		tmp += el.offsetLeft;
		el = el.offsetParent;
	}
	return tmp;
}
function calendar_gettop(el) {
	var tmp = el.offsetTop;
	el = el.offsetParent
	while(el) {
		tmp += el.offsetTop;
		el = el.offsetParent;
	}
	return tmp;
}


// Container
var calendar_ce = document.getElementById("calendar_conclass");
// Output Element
var calendar_oe = document.getElementById("calendar_calclass");

// Output Buffering
var calendar_ob = ''; 
function calendar_ob_clean() {
	calendar_ob = '';
}
function calendar_ob_flush() {
	calendar_oe.innerHTML = calendar_ob;
	calendar_ob_clean();
}
function calendar_echo(t) {
	calendar_ob += t;
}

var calendar_element_j; // Jour
var calendar_element_m; // Mois
var calendar_element_a; // Année

var calendar_element_jd; // Jour
var calendar_element_md; // Mois
var calendar_element_ad; // Année

var calendar_monthnames = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Décembre'];

var calendar_daynames = ['Dim', 'Lun', 'Mar', 'Me', 'Jeu', 'Ven', 'Sam'];

// Calendar template
function calendar_template_main_above(t) {
	return '<table cellpadding="1" cellspacing="0" class="calendar_tbl">'
	     + '<tr>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_py();">&lt;&lt;</td>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_pm();">&lt;</td>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_hi();" colspan="3">[Fermer]</td>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_nm();">&gt;</td>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_ny();">&gt;&gt;</td>'
		 + '</tr>'
	     + '<tr>'
		 + '<td colspan="7" class="calendar_head_date">' + t + '</td>'
		 + '</tr>'
		 + '<tr>';
}

function calendar_template_day_row(t) {
	return '<td class="calendar_subhead">' + t + '</td>';
	// Define width in CSS, XHTML 1.0 Strict doesn't have width property for it.
}

function calendar_template_new_week() {
	return '</tr><tr>';
}

function calendar_template_blank_cell(colspan) {
	return '<td colspan="' + colspan + '"></td>'
}

function calendar_template_day(d, m, y) {
	return '<td class="calendar_cell" onclick="calendar_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
	// Define width the day row.
}

function calendar_template_day2(d, m, y) {
	return '<td class="calendar_cell" onclick="calendar_onclick2(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
	// Define width the day row.
}

function calendar_template_main_below() {
	return '</tr>'
	     + '</table>';
}

// This one draws calendar...
function calendar_draw_calendar(m, y) {
	// First clean the output buffer.
	calendar_ob_clean();
	// Here we go, do the header
	calendar_echo (calendar_template_main_above(calendar_monthnames[m - 1] + ' ' + y));
	for (i = 0; i < 7; i ++) {
		calendar_echo (calendar_template_day_row(calendar_daynames[i]));
	}
	// Make a date object.
	var calendar_dc_date = new Date();
	calendar_dc_date.setMonth(m - 1);
	calendar_dc_date.setFullYear(y);
	calendar_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 = calendar_dc_date.getDay();
	var first_loop = 1;
	// Start the first week
	calendar_echo (calendar_template_new_week());
	// If sunday is not the first day of the month, make a blank cell...
	if (first_day != 0) {
		calendar_echo (calendar_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!!
			calendar_echo (calendar_template_new_week());
		}
		// Make a row of that day!
		calendar_echo (calendar_template_day(i + 1, m, y));
		// This is not first loop anymore...
		first_loop = 0;
		// What is the next day?
		j ++;
		j %= 7;
	}
	// Do the footer
	calendar_echo (calendar_template_main_below());
	// And let's display..
	calendar_ob_flush();
	// Scroll it into view.
	//calendar_ce.scrollIntoView();
}

// This one draws calendar...
function calendar_draw_calendar2(m, y) {
	// First clean the output buffer.
	calendar_ob_clean();
	// Here we go, do the header
	calendar_echo (calendar_template_main_above(calendar_monthnames[m - 1] + ' ' + y));
	for (i = 0; i < 7; i ++) {
		calendar_echo (calendar_template_day_row(calendar_daynames[i]));
	}
	// Make a date object.
	var calendar_dc_date = new Date();
	calendar_dc_date.setMonth(m - 1);
	calendar_dc_date.setFullYear(y);
	calendar_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 = calendar_dc_date.getDay();
	var first_loop = 1;
	// Start the first week
	calendar_echo (calendar_template_new_week());
	// If sunday is not the first day of the month, make a blank cell...
	if (first_day != 0) {
		calendar_echo (calendar_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!!
			calendar_echo (calendar_template_new_week());
		}
		// Make a row of that day!
		calendar_echo (calendar_template_day2(i + 1, m, y));
		// This is not first loop anymore...
		first_loop = 0;
		// What is the next day?
		j ++;
		j %= 7;
	}
	// Do the footer
	calendar_echo (calendar_template_main_below());
	// And let's display..
	calendar_ob_flush();
	// Scroll it into view.
	//calendar_ce.scrollIntoView();
}

// A function to show the calendar.
// When user click on the date, it will set the content of t.
// t : l'élément appelant
// j : l'input correspondant au jour
// m : l'input correspondant au mois
// a : l'input correspondant à l'année
function get_calendar(t, j, m, a) {

	var x = ( calendar_getleft(t) + 5 );
	var y = ( calendar_gettop(t) + 10 );

	// Set the element to set...
	calendar_element_j = j;
	calendar_element_m = m;
	calendar_element_a = a;
	// Make a new date, and set the current month and year.
	var get_calendar_date = new Date(calendar_element_a.value, calendar_element_m.value -1, calendar_element_j.value);
	calendar_c_month = get_calendar_date.getMonth() + 1;
	calendar_c_year = get_calendar_date.getFullYear();
	// Draw the calendar
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
	// To change the position properly, we must show it first.
	calendar_ce.style.display = '';
	
	/*
	// Move the calendar container!
	the_left = calendar_getleft(t);
	the_top = calendar_gettop(t) + t.offsetHeight;
	calendar_ce.style.left = (the_left + 50) + 'px';
	calendar_ce.style.top = (the_top + 50 ) + 'px';
	// Scroll it into view.
	calendar_ce.scrollIntoView();
	*/

	
	calendar_ce.style.position = 'absolute';
	calendar_ce.style.left = x + 'px';
	calendar_ce.style.top = y + 'px';
	calendar_ce.style.margin = '0px';
	calendar_ce.style.padding = '0px';
	
}

// A function to show the calendar.
// When user click on the date, it will set the content of t.
// t : l'élément appelant
// j : l'input correspondant au jour
// m : l'input correspondant au mois
// a : l'input correspondant à l'année
function get_calendar2(t, j, m, a, jd, md, ad) {

	var x = ( calendar_getleft(t) + 5 );
	var y = ( calendar_gettop(t) + 10 );

	// Set the element to set...
	calendar_element_j = j;
	calendar_element_m = m;
	calendar_element_a = a;
	
	calendar_element_jd = jd;
	calendar_element_md = md;
	calendar_element_ad = ad;
	// Make a new date, and set the current month and year.
	var get_calendar_date = new Date(calendar_element_a.value, calendar_element_m.value -1, calendar_element_j.value);
	calendar_c_month = get_calendar_date.getMonth() + 1;
	calendar_c_year = get_calendar_date.getFullYear();
	// Draw the calendar
	calendar_draw_calendar2(calendar_c_month, calendar_c_year);
	// To change the position properly, we must show it first.
	calendar_ce.style.display = '';
	
	/*
	// Move the calendar container!
	the_left = calendar_getleft(t);
	the_top = calendar_gettop(t) + t.offsetHeight;
	calendar_ce.style.left = (the_left + 50) + 'px';
	calendar_ce.style.top = (the_top + 50 ) + 'px';
	// Scroll it into view.
	calendar_ce.scrollIntoView();
	*/

	
	calendar_ce.style.position = 'absolute';
	calendar_ce.style.left = x + 'px';
	calendar_ce.style.top = y + 'px';
	calendar_ce.style.margin = '0px';
	calendar_ce.style.padding = '0px';
	
}

// Hide the calendar.
function calendar_hi() {
	calendar_ce.style.display = 'none';
}

// Moves to the next month...
function calendar_nm() {
	// Increase the current month.
	calendar_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 (calendar_c_month > 12) {
		calendar_c_month = 1; 
		calendar_c_year++;
	}
	// Redraw the calendar.
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
}

// Moves to the previous month...
function calendar_pm() {
	calendar_c_month = calendar_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 (calendar_c_month < 1) {
		calendar_c_month = 12; 
		calendar_c_year = calendar_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
	}
	// Redraw the calendar.
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
}

// Moves to the next year...
function calendar_ny() {
	// Increase the current year.
	calendar_c_year++;
	// Redraw the calendar.
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
}

// Moves to the previous year...
function calendar_py() {
	// Decrease the current year.
	calendar_c_year = calendar_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
	// Redraw the calendar.
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
}

function calendar_onclick(d, m, y) {
	// Hide the calendar.
	calendar_hi();
	// Renseigne les champs jour, mois, et année
	calendar_element_j.value = d;
	calendar_element_m.value = m;
	calendar_element_a.value = y;
}

function calendar_onclick2(d, m, y) {
	// Hide the calendar.
	calendar_hi();
	// Renseigne les champs jour, mois, et année
	calendar_element_j.value = d;
	calendar_element_m.value = m;
	calendar_element_a.value = y;
	
	var lendemain = new Date(y, m - 1, d + 1);
	calendar_element_jd.value = lendemain.getDate();
	calendar_element_md.value = lendemain.getMonth() + 1;
	calendar_element_ad.value = lendemain.getFullYear();
}


