var monthInYear = new Array("janvier", "f&eacute;vrier", "mars", "avril", "mai", "juin", "juillet", "ao&ucirc;t", "septembre", "octobre", "novembre", "d&eacute;cembre");
var dayInWeek = new Array("dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi");

var calendar = new Calendar();

function Calendar()
{
	this.elementId = "";
	this.element = null;

	this.weekday = 0;
	this.day = 0;
	this.month = 0;
	this.year = 0;

	this.cday = 0;
	this.cmonth = 0;
	this.cyear = 0;
	this.cyear2 = 0;

	this.mappingArray = new Array();

	this.cal = document.getElementById("cal");
	this.cald = document.getElementById("cald");
	this.calm = document.getElementById("calm");
	this.caly = document.getElementById("caly");
}

function initCalendar(date)
{
	calendar.weekday = date.getDay();
	calendar.day = date.getDate();
	calendar.month = date.getMonth() + 1;
	calendar.year = date.getFullYear();

	calendar.cmonth = calendar.month;
	calendar.cyear = calendar.year;

	fillMonthChoice();
	fillYearChoice();
}

function initElement(id)
{
	calendar.elementId = "";
	calendar.element = document.getElementById(id);

	var hGap = -22;
	var vGap = 12;
	var elementStyle = calendar.cal.style;
	var rightedge = document.body.clientWidth - event.clientX - hGap;
	var bottomedge = document.body.clientHeight - event.clientY - vGap;
	if (rightedge < calendar.cal.offsetWidth)
	{
		elementStyle.left = document.body.scrollLeft + event.clientX - hGap - calendar.cal.offsetWidth;
	}
	else
	{
		elementStyle.left = document.body.scrollLeft + event.clientX + hGap;
	}
	if (bottomedge < calendar.cal.offsetHeight)
	{
		elementStyle.top = document.body.scrollTop + event.clientY - vGap - calendar.cal.offsetHeight;
	}
	else
	{
		elementStyle.top = document.body.scrollTop + event.clientY + vGap;
	}

	calendar.calm.style.left = elementStyle.left;
	calendar.calm.style.top = parseInt(elementStyle.top) + 28;
	calendar.caly.style.left = parseInt(elementStyle.left) + 6;
	calendar.caly.style.top = parseInt(elementStyle.top) + 33;
}

function displayCalendar(id)
{
	calendar = new Calendar();

	var i;
	var element;
	for (i=0 ; i<4 ; i++)
	{
		element = document.getElementById("cal_m_0" + i);
		element.onmouseover = highlightHead;
		element.onmouseout = unhighlightHead;
	}
	for (i=0 ; i<4 ; i++)
	{
		element = document.getElementById("cal_y_0" + i);
		element.onmouseover = highlightHead;
		element.onmouseout = unhighlightHead;
	}
	element = document.getElementById("cal_c");
	element.onmouseover = highlightHead;
	element.onmouseout = unhighlightHead;

	initElement(id);
	var dateValue = calendar.element.value;
	if (dateValue != "")
	{
		var s = dateValue.split("-");
		var d;
		var m;
		var y;
		var validDate = true;
		if (s.length == 3)
		{
			d = s[0];
			if (d.charAt(0) == "0")
			{
				d = d.substr(1);
			}
			m = s[1];
			if (m.charAt(0) == "0")
			{
				m = m.substr(1);
			}
			y = s[2];
		}
		else
		{
			validDate = false;
		}

		if (validDate)
		{
			initCalendar(new Date(y, m-1, d));
		}
		else
		{
			initCalendar(new Date());
		}
	}
	else
	{
		initCalendar(new Date());
	}
	fillCalendar();
	showCalendar();
}

function showCalendar()
{
	calendar.cal.style.visibility = "visible";
	showDayChoice();
	hideMonthChoice();
	hideYearChoice();
}

function hideCalendar()
{
	calendar.cal.style.visibility = "hidden";
	hideDayChoice();
	hideMonthChoice();
	hideYearChoice();
}

function showDayChoice()
{
	calendar.cald.style.visibility = "visible";
}

function hideDayChoice()
{
	calendar.cald.style.visibility = "hidden";
}


function showMonthChoice()
{
	calendar.calm.style.visibility = "visible";
}

function hideMonthChoice()
{
	calendar.calm.style.visibility = "hidden";
}

function showYearChoice()
{
	calendar.caly.style.visibility = "visible";
}

function hideYearChoice()
{
	calendar.caly.style.visibility = "hidden";
}

function updateMonthChoiceVisibility()
{
	if (calendar.calm.style.visibility == "visible")
	{
		showDayChoice();
		hideMonthChoice();
	}
	else
	{
		hideDayChoice();
		hideYearChoice();
		showMonthChoice();
	}
}

function updateYearChoiceVisibility()
{
	if (calendar.caly.style.visibility == "visible")
	{
		showDayChoice();
		hideYearChoice();
	}
	else
	{
		calendar.cyear2 = calendar.cyear;
		hideDayChoice();
		hideMonthChoice();
		fillYearChoice();
		showYearChoice();
	}
}

function fillCalendar()
{
	document.getElementById("cal_m_02").innerHTML = getStringMonth(calendar.cmonth);
	document.getElementById("cal_y_02").innerHTML = calendar.cyear;
	var element;

	var i = 0;
	var j = 0;
	for (i=0 ; i<6 ; i++)
	{
		for (j=0 ; j<7 ; j++)
		{
			element = document.getElementById("cal_d_" + i + j);
			element.innerHTML = "";
			element.onclick = "";
			element.onmouseover = "";
			element.onmouseout = "";
		}
	}

	i = 0;
	j = (new Date(calendar.cyear, calendar.cmonth-1, 1)).getDay();
	j = (j + 6) % 7;
	var count = 1;
	var days = getMonthLastDay();
	while (count <= days)
	{
		element = document.getElementById("cal_d_" + i + j);
		element.innerHTML = count;
		element.onclick = selectDay;
		element.onmouseover = highlightDay;
		element.onmouseout = unhighlightDay;
		calendar.mappingArray[10 * i + j] = count;
		j++;
		if (j == 7)
		{
			i++;
			j = 0;
		}
		count++;
	}
}

function fillMonthChoice()
{
	var i;
	var element;
	for (i=1 ; i<=12 ; i++)
	{
		element = document.getElementById("calm_" + i);
		element.innerHTML = getStringMonth(i);
		element.onclick = selectMonth;
		element.onmouseover = highlightHead;
		element.onmouseout = unhighlightHead;
	}
}

function fillYearChoice()
{
	var i;
	var element;
	var gap = calendar.cyear2 - 11;
	for (i=1 ; i<=25 ; i++)
	{
		element = document.getElementById("caly_" + i);
		element.innerHTML = gap + i;
		element.onclick = selectYear;
		element.onmouseover = highlightHead;
		element.onmouseout = unhighlightHead;
	}
}

function highlightDay()
{
	event.srcElement.className = "sel";
}

function unhighlightDay()
{
	event.srcElement.className = "";
}

function highlightHead()
{
	var element = event.srcElement;
	if (element.tagName == "IMG")
	{
		element = element.parentNode;
	}
	element.className = "sel";
	if (element.id == "cal_m_02")
	{
		document.getElementById("cal_m_03").className = "sel";
	}
	else if (element.id == "cal_m_03")
	{
		document.getElementById("cal_m_02").className = "sel";
	}
	else if (element.id == "cal_y_02")
	{
		document.getElementById("cal_y_03").className = "sel";
	}
	else if (element.id == "cal_y_03")
	{
		document.getElementById("cal_y_02").className = "sel";
	}
}

function unhighlightHead()
{
	var element = event.srcElement;
	if (element.tagName == "IMG")
	{
		element = element.parentNode;
	}
	element.className = "";
	if (element.id == "cal_m_02")
	{
		document.getElementById("cal_m_03").className = "";
	}
	else if (element.id == "cal_m_03")
	{
		document.getElementById("cal_m_02").className = "";
	}
	else if (element.id == "cal_y_02")
	{
		document.getElementById("cal_y_03").className = "";
	}
	else if (element.id == "cal_y_03")
	{
		document.getElementById("cal_y_02").className = "";
	}
}

function selectDay()
{
	var id = event.srcElement.id;
	calendar.cday = calendar.mappingArray[parseInt(id.substr(id.length - 2))];
	hideCalendar();
	var d = calendar.cday;
	var m = calendar.cmonth;
	var y = calendar.cyear;
	calendar.element.value = (d < 10 ? "0" : "") + d + "-" + (m < 10 ? "0" : "") + m + "-" + y;
}

function selectMonth()
{
	var id = event.srcElement.id;
	var s = id.split("_");
	calendar.cmonth = parseInt(s[s.length - 1]);
	hideMonthChoice();
	showDayChoice();
	fillCalendar();
}

function selectYear()
{
	var id = event.srcElement.id;
	var s = id.split("_");
	calendar.cyear = parseInt(s[s.length - 1]) + calendar.cyear2 - 11;
	hideYearChoice();
	showDayChoice();
	fillCalendar();
}

function incrementMonth()
{
	if (calendar.cmonth == 12)
	{
		calendar.cmonth = 1;
		calendar.cyear = calendar.cyear + 1;
	}
	else
	{
		calendar.cmonth = calendar.cmonth + 1;
	}
	fillCalendar();
}

function decrementMonth()
{
	if (calendar.cmonth == 1)
	{
		calendar.cmonth = 12;
		calendar.cyear = calendar.cyear - 1;
	}
	else
	{
		calendar.cmonth = calendar.cmonth - 1;
	}
	fillCalendar();
}

function incrementYear()
{
	calendar.cyear = calendar.cyear + 1;
	fillCalendar();
}

function decrementYear()
{
	calendar.cyear = calendar.cyear - 1;
	fillCalendar();
}

function increaseYear()
{
	calendar.cyear2 = calendar.cyear2 + 20;
	fillYearChoice();
}

function decreaseYear()
{
	calendar.cyear2 = calendar.cyear2 - 20;
	fillYearChoice();
}

function getMonthLastDay()
{
	var testDate = new Date(calendar.cyear, calendar.cmonth-1, 29);
	if (testDate.getDate() == 29)
	{
		testDate.setDate(30);
		if (testDate.getDate() == 30)
		{
			testDate.setDate(31);
			if (testDate.getDate() == 31)
			{
				return 31
			}
			else
			{
				return 30;
			}
		}
		else
		{
			return 29;
		}
	}
	else
	{
		return 28;
	}
}

function getStringWeekDay(index)
{
	return dayInWeek[index];
}

function getStringMonth(index)
{
	return monthInYear[index - 1];
}

//Valider le format d'une date
function ValiderFormatDate(strDate)
{
	tabDate = strDate.split("-");
	tempErrFormatDate = tabDate.length != 3 || tabDate[0].length != 2 || tabDate[1].length != 2 || tabDate[2].length != 4;
	tempErrFormatDate = tempErrFormatDate || isNaN(parseInt(tabDate[0],10)) || isNaN(parseInt(tabDate[1],10)) || isNaN(parseInt(tabDate[2],10));
	return tempErrFormatDate;
}

//Valider une date
function ValiderDate(strDate)
{
	tabDate = strDate.split("-");
	var ArrJourMoisMax = new Array(12);
	ArrJourMoisMax[0] = 31;
	ArrJourMoisMax[1] = 28;
	ArrJourMoisMax[2] = 31;
	ArrJourMoisMax[3] = 30;
	ArrJourMoisMax[4] = 31;
	ArrJourMoisMax[5] = 30;
	ArrJourMoisMax[6] = 31;
	ArrJourMoisMax[7] = 31;
	ArrJourMoisMax[8] = 30;
	ArrJourMoisMax[9] = 31;
	ArrJourMoisMax[10] = 30;
	ArrJourMoisMax[11] = 31;
	
	intAnnee = parseInt(tabDate[2],10);
	intMois = parseInt(tabDate[1],10);
	intJour = parseInt(tabDate[0],10);

	var boolErrDateExist = false;
	boolErrDateExist = boolErrDateExist || intAnnee < 1900 || intAnnee > 2200 || intMois > 12 || intMois <= 0 || intJour <= 0;
	if(intMois == 2)
	{
		boolErrDateExist = boolErrDateExist || (intAnnee % 4 == 0 && intJour > 29) || (intAnnee % 4 != 0 && intJour > 28);
		
	}
	else
	{
		boolErrDateExist = boolErrDateExist || ArrJourMoisMax[intMois - 1] < intJour;
	}
	
	return boolErrDateExist;
}

//Afficher le calendrier
function ChoisirDate(Id)
{
	displayCalendar(Id);
}

//Filtrer les touche pour les dates
function FiltrerToucheDate()
{
	return FiltrerTouche(true,false,false,false,false,"-");
}

//Filtrer les touche entré au clavier
function FiltrerTouche(Numeric,Alpha,Espace,Point,Ctrl,AcceptChar)
{
	var retour = false;
	
	if(window.event)
	{
		if(window.event.type == "keypress") 
		{ 
		
			carCode = window.event.which? window.event.which : window.event.keyCode;
			retour |= carCode == 13;
			if(carCode > 0 && !retour)
			{
				retour |= carCode == 18;
				retour |= Numeric && carCode >= 48 && carCode <= 57;
				retour |= Alpha && ((carCode >= 65 && carCode <= 90) || (carCode >= 97 && carCode <= 122));
				retour |= Espace && carCode == 32;
				retour |= Point && carCode == 46;
				retour |= Ctrl && carCode >= 0 && carCode <= 31;
				var tempcar = String.fromCharCode(carCode);
				retour |= AcceptChar.indexOf(tempcar) != -1;
			}
		}
	} 
	if(!retour)
	{
		if(window.event.which)
		{
			window.event.which = 0;
		}
		else
		{
			window.event.keyCode = 0;
		}
	}
	return retour; 
}
	
	

//Initialiser le calandrier
function init()
{
	window.TimCacheMsg = window.setInterval(CacherMsg,5000);
	displayCalendar("initCalendar");
	hideCalendar();
}

