// --------------------------------------------------------
// Gestion de la liste des variables
// --------------------------------------------------------

moisNom = ['jan.','feb.','mar.','apr.','may.','jun.','jul.','aug.','sep.','oct.','nov.','dec.'];
moisJours = [31,28,31,30,31,30,31,31,30,31,30,31];

// --------------------------------------------------------
// Constructeur
var objDate = function (ref, state) {
	this.reference = ref;
	this.isYear = state;
}

// --------------------------------------------------------
// Methodes
objDate.prototype = {

	create : function()
	{
		if(this.isYear == true)
			this.cont = document.getElementById("pickEndDate");
		else
			this.cont = document.getElementById("pickStartDate");

		this.contDay=document.createElement('td');
		this.contMonth=document.createElement('td');
		this.selectDay=document.createElement('select');
		this.selectMonth=document.createElement('select');

		this.selectMonth.style.width =
		this.selectDay.style.width = '60px';
		//this.selectMonth.size =
		//this.selectDay.size = 4;
		
		this.contDay.appendChild(this.selectDay);
		this.contMonth.appendChild(this.selectMonth);
		this.cont.appendChild(this.contDay);
		this.cont.appendChild(this.contMonth);

		//-----------------------------------
		// Création du select de jours
		for(i=1;i<32;i++)
		{
      this.menu=document.createElement('option');
			this.menu.value=i;
			this.menu.innerHTML=i;
			this.selectDay.appendChild(this.menu);
		}
		//-----------------------------------
		// Création du select de mois
		for(i=0;i<12;i++)
		{
			this.menu=document.createElement('option');
			this.menu.value = i;
			this.menu.innerHTML = moisNom[i];
			this.selectMonth.appendChild(this.menu);
		}
		//-----------------------------------
		// Création du select d'annees
		if(this.isYear == true)
		{
			this.contYear=document.createElement('td');
			this.selectYear=document.createElement('select');
			this.contYear.appendChild(this.selectYear);
			this.cont.appendChild(this.contYear);

			this.selectYear.style.width = '60px';
			//this.selectYear.size = 4;

			//-----------------------------------
			for(i=0;i<3;i++)
			{
				this.menu=document.createElement('option');
				this.menu.value = i;
				this.menu.innerHTML = "+"+i;
				this.selectYear.appendChild(this.menu);
			}
		} else
		{
			this.selectYear=document.createElement('td');
			this.selectYear.innerHTML = '<select style="width:60px" disabled><option>0</option></select>';
			this.cont.appendChild(this.selectYear);
		}

	},

	change : function()
	{
		// Se place sur le dernier jour du mois si positionne sur plus haut
		if(this.selectDay.options.selectedIndex+1 > moisJours[this.selectMonth.options.selectedIndex])
		  this.selectDay.options.selectedIndex = parseFloat(moisJours[this.selectMonth.options.selectedIndex])-1;
	},

	pushEndDate : function(dd,dm,dy)
	{
		if(dd < moisJours[this.selectMonth.options.selectedIndex] || dd == getStartDay())
			this.selectDay.options.selectedIndex = dd;
		else
		{
			dd -= moisJours[this.selectMonth.options.selectedIndex];
			this.selectDay.options.selectedIndex = dd;
			dm += 1;
		}
		if(dm < 12)
			this.selectMonth.options.selectedIndex = dm;
		else
		{
			dm -= 12;
			this.selectMonth.options.selectedIndex = dm;
			dy += 1;
		}
		this.selectYear.options.selectedIndex = dy;
		this.change();
	}
}

// --------------------------------------------------------
// fonction de creation d'objets
var sd = new objDate("startDate");
var ed = new objDate("endDate", true);

sd.create();
ed.create();
ed.pushEndDate(0,6,0);

sd.selectDay.onchange =
sd.selectMonth.onchange = function() {
	sd.change();
}
ed.selectDay.onchange =
ed.selectYear.onchange =
ed.selectMonth.onchange = function() {
	ed.change();
}
getStartDay = function(){
	return sd.selectDay.options.selectedIndex;
}
pushDate = function(nd,nm,ny) {
	var dd = nd + sd.selectDay.options.selectedIndex;
	var dm = nm + sd.selectMonth.options.selectedIndex;
	var dy = ny;
	ed.pushEndDate(dd,dm,dy);
	ed.pushEndDate(dd,dm,dy);
}

getStartDate = function() {
  var dd = sd.selectDay.options.selectedIndex+1;
  var dm = sd.selectMonth.options.selectedIndex+1;
  var eD = ed.selectDay.options.selectedIndex+1;
  var em = ed.selectMonth.options.selectedIndex+1;
  var ey = ed.selectYear.options.selectedIndex+1;

  if (11+''+(dm+10)+''+(dd+10) > (ey+10)+''+(em+10)+''+(eD+10))
    return false;
  else
    return [dd,dm,eD,em,ey];
}
	
// --------------------------------------
// vérification des dates
function verifDate()
{
	if (!getStartDate())
	{
		document.getElementById("timeResumeIcon").className = "error";
		document.getElementById("timeResume").innerHTML = "<ul><li>Start datetime is after end datetime !</li></ul>";
		return false;
	}
	else
	{
		document.getElementById("timeResumeIcon").className = "";
		var arrayDate = getStartDate();
		document.getElementById("timeResume").innerHTML = "<ul><li>Starts on : "+arrayDate[0]+'/'+arrayDate[1]+"/1</li><li>Finishes on : "+arrayDate[2]+'/'+arrayDate[3]+'/'+arrayDate[4]+"</li></ul>";
		return arrayDate.toJSONString();
	}
}

