// Code begin...
// Set the initial date.
var ds_i_date = new Date();
var ds_c_month;
var ds_c_year;

if(gup('mois')=='')
	ds_c_month = ds_i_date.getMonth() + 1;
else
	ds_c_month = gup('mois');
if(gup('annee')=='')
	ds_c_year = ds_i_date.getFullYear();
else
	ds_c_year = gup('annee');

var ListeValeur = new Array();


// 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;
}

var ds_oe;
var ds_ce;
var ds_ob;

function GetValeurs(){
	//on déclare l'objet qui permet d'accéder une url distante sans recharger
	if(window.XMLHttpRequest) // Firefox 
		var xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
		var xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	var Donnees="";
	//on définit les propriétés de l'objet
	Page = "images/skins/todomuseo/dataxml.asp?mois="+ds_c_month+"&annee="+ds_c_year;
	xhr_object.open("GET", Page,true);
	xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	//lors du changement de la propriété readystate
	xhr_object.onreadystatechange = function() {
		//la valeur 4 indique que tout le code a été reçu
		if(xhr_object.readyState == 4){
			//on stock tout le code reçu dans Temp
			Temp = xhr_object.responseText;
			ListeValeur = Temp.split(";");
			InitDs();
		}
	}
	xhr_object.send(Donnees);
}

function InitDs()
{
// Output Element
 ds_oe = ds_getel('ds_calclass');
// Container
 ds_ce = ds_getel('ds_conclass');

// Output Buffering
 ds_ob = ''; 
 ds_sh(document.getElementById('Calendrier'));
}

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 = [
'Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun',
'Jul', 'Ago', 'Set', 'Oct', 'Nov', 'Dic'
]; // You can translate it for your language.

var ds_daynames = [
'L', 'M', 'X', 'J', 'V', 'S','D'
]; // You can translate it for your language.

// Calendar template
function ds_template_main_above(t) {
	return '<table cellpadding="3" cellspacing="1" class="ds_tbl">'
	     + '<tr>'
		 + '<td colspan="7" class="ds_head" align="right"><nobr>'+t+'</nobr></td>'
		 + '</tr>'
		 + '<tr>';
		 /*
		 Fleches
		 '<input class="Bouton" type="button" style="padding:0px;margin-bottom:1px;margin-right:3px;width:auto" value="<" onclick="dsmove(0);">'
		 + t + '<input class="Bouton" type="button" style="padding:0px;margin-bottom:1px;width:auto" value=">" onclick="dsmove(1);">';
		 */
}

function dsmove(Dir)
{
	var Mois;
	var Annee;
	if(Dir!=3)
	{
		Mois=ds_c_month.toString();
		Annee = ds_c_year;
	
		if(Mois.substring(0,1)==0)
			Mois = Mois.substring(1);
		if(Dir == 0)
		{
			if(Mois!=1)
				Mois = parseInt(Mois)-1;
			else
			{
				Mois = 12;
				Annee--;
			}
		}
		else
		{
			if(Mois!=12)
				Mois = parseInt(Mois)+1;
			else
			{
				Mois = 1;
				Annee++;
			}
		}
		Mois = Mois.toString();
		if(Mois.length==1)
			Mois = "0"+Mois.toString();
	}
	else
	{
		Mois = document.forms['Cal'].elements['mois'].options[document.forms['Cal'].elements['mois'].selectedIndex].value;
		Annee = document.forms['Cal'].elements['annee'].options[document.forms['Cal'].elements['annee'].selectedIndex].value;
	}
	var Adresse = window.location.href.split("?");
	var reg = new RegExp(".asp");
	if(!reg.test(Adresse[0]))
		Adresse[0]+="index.asp";
	var Args = '';
	if(gup('sqc')!='')
		Args = '&sqc='+gup('sqc')+'&sqo='+gup('sqo')+'&sqv='+gup('sqv');
	if(gup('ID')!="")
		Args+="&ID="+gup('ID');
	if(gup('id')!="")
		Args+="&id="+gup('id');
	if(gup('id_sous_menu')!="")
		Args+="&id_sous_menu="+gup('id_sous_menu');
	window.location.href = Adresse[0]+"?mois="+Mois+"&annee="+Annee+Args;
}

function ds_template_day_row(t) {
	return '<td class="ds_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) {
	var OnClick =  '';
	var Classe = 'ds_cell';
	if(in_array(ListeValeur,d))
	{
		m = m.toString();
		d = d.toString();
		if(m.length==1)
			m = "0"+m.toString();
		if(d.length==1)
			d = "0"+d.toString();
		var Url = "index.asp?id=389&sqc=COL_117&sqo=4&sqv="+y+"-"+m+"-"+d+"&mois="+m+"&annee="+y;
		OnClick = 'onclick="window.location.href=\''+Url+'\'"';
		Classe +="D";
	}
	if(d.length==1)
		d= "0"+d.toString();
	return '<td class="'+Classe+'"'+OnClick+'>' + d + '</td>';
	// Define width the day row.
}

function ds_template_main_below() {
	return '</tr>'
	     + '</table>';
}

// This one draws calendar...
function ds_draw_calendar(m, y) {
	// First clean the output buffer.
	ds_ob_clean();
	// Here we go, do the header
	var Temp = new Date();
	var DateCourante = Temp.getFullYear();
	Temp="";
	for(var i=0;i<ds_monthnames.length;i++)
	{
		var Sup = "";
		var TempMois = i+1;
		TempMois = TempMois.toString();
		if(TempMois.length==1)
			TempMois = "0"+TempMois;
		if(TempMois==m)
			Sup=" selected=selected";
		Temp +='<option value="'+TempMois+'"'+Sup+'>'+ds_monthnames[i]+'</option>';
	}
	var Chaine = '<select class="Menu_Select" style="font-size : 10px;padding:0px;margin:0px;width:auto" name="mois" onChange="dsmove(3)">'+Temp+'</select>';
	Temp="";
	for(var i=DateCourante-1;i<DateCourante+3;i++)
	{
		var Sup="";
		if(i==y)
			Sup = " selected=selected";
		Temp+='<option value="'+i+'"'+Sup+'>'+i+'</option>';
	}
	
	Chaine+='<select class="Menu_Select" style="font-size : 10px;padding:0px;margin:0px;width:auto" name="annee" onChange="dsmove(3)">'+Temp+'</select>';
	//ds_echo (ds_template_main_above(ds_monthnames[m - 1] + ' ' + y));
	ds_echo (ds_template_main_above(Chaine));
	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;
	// 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 && first_day!=1) {
		ds_echo (ds_template_blank_cell(first_day-1));
	}
	else if(first_day!=1)
	{
		ds_echo (ds_template_blank_cell(6));
	}
	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 == 1 && !first_loop) {
			// New week!!
			ds_echo (ds_template_new_week());
		}
		// Make a row of that day!
		ds_echo (ds_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
	ds_echo (ds_template_main_below());
	// And let's display..
	ds_ob_flush();
	// Scroll it into view.
	ds_ce.scrollIntoView();
}

// A function to show the calendar.
// When user click on the date, it will set the content of t.
function ds_sh(t) {
	// Set the element to set...
	ds_element = t;
	// Make a new date, and set the current month and year.
	var ds_sh_date = new Date();
	if(gup('mois')=='')
		ds_c_month = ds_sh_date.getMonth() + 1;
	else
		ds_c_month = gup('mois');
	if(gup('annee')=='')
		ds_c_year = ds_sh_date.getFullYear();
	else
		ds_c_year = gup('annee');
	// Draw the calendar
	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!
	/*the_left = ds_getleft(t);
	the_top = ds_gettop(t) - ds_ce.offsetHeight;
	ds_ce.style.left = the_left + 'px';
	ds_ce.style.top = the_top + 'px';*/
	// Scroll it into view.
	//ds_ce.scrollIntoView();
}

// Hide the calendar.
function ds_hi() {
	ds_ce.style.display = 'none';
}

// Format the date to output.
function ds_format_date(d, m, y) {
	// 2 digits month.
	m2 = '00' + m;
	m2 = m2.substr(m2.length - 2);
	// 2 digits day.
	d2 = '00' + d;
	d2 = d2.substr(d2.length - 2);
	// YYYY-MM-DD
//	return y + '-' + m2 + '-' + d2;
	return d2 + '/' + m2 + '/' + 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));
	}
}

// And here is the end.

function gup( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function in_array(Tab,Valeur)
{
	for(var i=0;i<Tab.length;i++)
	{
		if(Tab[i]==Valeur)
			return true;
	}
	return false;
}
