/*****************************************************************************
Copyright (C) 2006  Nick Baicoianu

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
Modified by Htsoftwares.com -- 2008
*****************************************************************************/

//constructor for the main Epoch class (ENGLISH VERSION)
function Epoch2(name,mode,targetelement,multiselect,sdateElement,edateElement, sdate, edate)
{

	var arrsDate=sdate.split("-");
	y1 = arrsDate[0];
	m1 = arrsDate[1];
	d1 = arrsDate[2];
	
	var arreDate=edate.split("-");
	y2 = arreDate[0];
	m2 = arreDate[1];
	d2 = arreDate[2];

	this.state = 0;
	this.name = name;
	this.curDate = new Date();
	this.mode = mode;
	this.selectMultiple = (multiselect == true); //'false' is not true or not set at all
	
	//the various calendar variables
	//this.selectedDate = this.curDate;
	this.selectedDates = new Array();
	this.calendar;
	this.calHeading;
	this.CalCells2;
	this.rows;
	this.cols;
	this.cells = new Array();
	
	this.d1 = d1;
	this.m1 = m1;
	this.y1 = y1;
	
	this.d2 = d2;
	this.m2 = m2;
	this.y2 = y2;
	
	//The controls
	this.monthSelect;
	this.yearSelect;
	
	//standard initializations
	this.mousein = false;
	this.calConfig(d1,m1,y1,d2,m2,y2);
	this.setDays();
	
	this.displayYear = this.y1;//this.displayYearInitial;
	this.displayMonth = this.m1-1//this.displayMonthInitial;
		
	this.createCalendar(); //create the calendar DOM element and its children, and their related objects
	if(this.mode == 'popup' && targetelement) //if the target element has been set to be an input text box
	{
		this.tgt = targetelement;
		this.calendar.style.position = 'absolute';
		this.topOffset = this.tgt.offsetHeight; // the vertical distance (in pixels) to display the calendar from the Top of its input element
		this.leftOffset = 0; 					// the horizontal distance (in pixels) to display the calendar from the Left of its input element
		this.calendar.style.top = this.getTop(targetelement) + this.topOffset + 'px';
		this.calendar.style.left = this.getLeft(targetelement) + this.leftOffset + 'px';
		document.body.appendChild(this.calendar);
		this.tgt.calendar = this;
		this.tgt.onclick = function () {/*window.alert('HERE!!!');*/ this.fromToBtn.checked=true; this.calendar.show();	}; //the calendar will popup when the input element is focused
		this.tgt.onblur = function () {if(!this.calendar.mousein){this.calendar.hide();}}; //the calendar will popup when the input element is focused
	} else
	{
		this.container = targetelement;
		this.container.appendChild(this.calendar);
	}
	
	// PATCH to refresh input element outside of the calendar... instead of those (hidden) of the calendar
	if(sdateElement!=null)
		this.fromToBoxContainer.fromInput = sdateElement; 
	
	if(edateElement!=null)
		this.fromToBoxContainer.toInput = edateElement;
		
	this.state = 2; //0: initializing, 1: redrawing, 2: finished!
	this.visible ? this.show() : this.hide();
}
//-----------------------------------------------------------------------------
Epoch2.prototype.calConfig = function (d1,m1,y1,d2,m2,y2) //PRIVATE: initialize calendar variables
{
	//this.mode = 'flat'; //can be 'flat' or 'popup'
	this.displayYearInitial = this.curDate.getFullYear(); //the initial year to display on load
	this.displayMonthInitial = this.curDate.getMonth(); //the initial month to display on load (0-11)
	
  this.rangeYearLower = this.y1;
	this.rangeYearUpper = this.y2;
	this.minDate = new Date(y1,m1-1,d1);
	this.maxDate = new Date(y2,m2-1,d2);
	this.startDay = 1; // the day the week will 'start' on: 0(Sun) to 6(Sat)
	this.showWeeks = false; //whether the week numbers will be shown
	this.selCurMonthOnly = false; //allow user to only select dates in the currently displayed month
	this.clearSelectedOnChange = true; 
	
	this.ftMode = true; 
	
	//flat mode-only settings:
	this.selectMultiple = true; //whether the user can select multiple dates (flat mode only)

	switch(this.mode) //set the variables based on the calendar mode
	{
		case 'popup': //popup options
			this.visible = false;
			break;
		case 'flat':
			this.visible = true;
			
			break;
	}
	this.setLang();
};
//-----------------------------------------------------------------------------
Epoch2.prototype.setLang = function()  //all language settings for Epoch are made here.  Check Date.dateFormat() for the Date object's language settings
{
	this.daylist = new Array('Su','Mo','Tu','We','Th','Fr','Sa','Su','Mo','Tu','We','Th','Fr','Sa'); /*<lang:en>*/
	//this.daylist = new Array('Di','Lu','Ma','Me','Je','Ve','Sa','Di','Lu','Ma','Me','Je','Ve','Sa'); /*<lang:en>*/
	this.months_sh = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	//this.months_sh = new Array('Jan','Fév','Mar','Avr','Mai','Jun','Jul','Aoû','Sep','Oct','Nov','Déc');
	this.monthup_title = 'Go to the next month';
	this.monthdn_title = 'Go to the previous month';
	
	this.oneByOneLblText = '';
	this.acceptSelectionCaption = 'Accepter';
};
//-----------------------------------------------------------------------------
Epoch2.prototype.getTop = function (element) //PRIVATE: returns the absolute Top value of element, in pixels
{
    var oNode = element;
    var iTop = 0;
    
    try {
	    while(oNode.tagName != 'BODY') {
	        iTop += oNode.offsetTop;
	        oNode = oNode.offsetParent;
	    }
    } catch	(e){}
    return iTop;
};
//-----------------------------------------------------------------------------
Epoch2.prototype.getLeft = function (element) //PRIVATE: returns the absolute Left value of element, in pixels
{
    var oNode = element;
    var iLeft = 0;
    
    try {
	    while(oNode.tagName != 'BODY') {
	        iLeft += oNode.offsetLeft;
	        oNode = oNode.offsetParent;        
	    }
    } catch	(e){}
    return iLeft;
};
//-----------------------------------------------------------------------------
Epoch2.prototype.show = function () //PUBLIC: displays the calendar
{
		this.calendar.style.display = 'block';
		this.visible = true;
	
	if(!this.oneByOneBtn.checked) {  
		this.fromToBtn.checked=false; this.fromToBtn.checked=true;
	}
};
//-----------------------------------------------------------------------------
Epoch2.prototype.hide = function () //PUBLIC: Hides the calendar
{
	//this.calendar.style.display = 'none';
	//this.visible = false;
};

//-----------------------------------------------------------------------------
Epoch2.prototype.toggle = function () //PUBLIC: Toggles (shows/hides) the calendar depending on its current state
{
	if(this.visible) {
		this.hide();
	}
	else {
		this.show();
	}
};
//-----------------------------------------------------------------------------
Epoch2.prototype.setDays = function ()  //PRIVATE: initializes the standard Gregorian Calendar parameters
{
	this.daynames = new Array();
	var j=0;
	for(var i=this.startDay; i< this.startDay + 7;i++) {
		this.daynames[j++] = this.daylist[i];
	}
		
	this.monthDayCount = new Array(31,((this.curDate.getFullYear() - 2000) % 4 ? 28 : 29),31,30,31,30,31,31,30,31,30,31);
};
//-----------------------------------------------------------------------------
Epoch2.prototype.setClass = function (element,className) //PRIVATE: sets the CSS class of the element, W3C & IE
{
	element.setAttribute('class',className);
	element.setAttribute('className',className); //<iehack>
};
//-----------------------------------------------------------------------------
Epoch2.prototype.createCalendar = function ()  //PRIVATE: creates the full DOM implementation of the calendar
{
	var tbody, tr, td;
	this.calendar = document.createElement('table');
	this.calendar.setAttribute('id',this.name+'_calendar');
	this.setClass(this.calendar,'calendar');
	//to prevent IE from selecting text when clicking on the calendar
	this.calendar.onselectstart = function() {return false;};
	this.calendar.ondrag = function() {return false;};
	tbody = document.createElement('tbody');
	
	//create the Main Calendar Heading
	tr = document.createElement('tr');
	td = document.createElement('td');
	td.appendChild(this.createMainHeading());
	tr.appendChild(td);
	tbody.appendChild(tr);
	
	//create the calendar Day Heading
	tr = document.createElement('tr');
	td = document.createElement('td');
	td.appendChild(this.createDayHeading());
	tr.appendChild(td);
	tbody.appendChild(tr);

	//create the calendar Day Cells
	tr = document.createElement('tr');
	td = document.createElement('td');
	td.setAttribute('id',this.name+'_cell_td');
	this.CalCellContainer = td;	//used as a handle for manipulating the calendar cells as a whole
	td.appendChild(this.createCalCells());
	tr.appendChild(td);
	tbody.appendChild(tr);
	
	//add the tbody element to the main calendar table
	this.calendar.appendChild(tbody);

	//and add the onmouseover events to the calendar table
	this.calendar.owner = this;
	this.calendar.onmouseover = function() {this.owner.mousein = true;};
	this.calendar.onmouseout = function() {this.owner.mousein = false;};
};
//-----------------------------------------------------------------------------
Epoch2.prototype.createMainHeading = function () //PRIVATE: Creates the primary calendar heading, with months & years
{
	//create the containing <div> element
	var container = document.createElement('div');
	container.setAttribute('id',this.name+'_mainheading');
	this.setClass(container,'mainheading');
	this.monthSelect = document.createElement('select');
	this.yearSelect = document.createElement('select');
	
	var fromToBtn = document.createElement('input');
	var oneByOneBtn = document.createElement('input');
	var fromToLbl = document.createElement('label');
	var oneByOneLbl = document.createElement('label');
	var fromLbl = document.createElement('label');
	var toLbl = document.createElement('label');
	var fromInput = document.createElement('input');
	var toInput = document.createElement('input');
	var fromToBoxContainer = document.createElement('div');
	
	var monthDn = document.createElement('input'), monthUp = document.createElement('input');
	var monthDnLbl = document.createElement('label');
	var monthUpLbl = document.createElement('label');
	this.monthDisplayLbl = document.createElement('label');
	
	var opt, i;
	//fill the month select box
	for(i=0;i<12;i++)
	{
		opt = document.createElement('option');
		opt.setAttribute('value',i);
		if(this.state == 0 && this.displayMonth == i) {
			opt.setAttribute('selected','selected');
		}
		opt.appendChild(document.createTextNode(this.months_sh[i]));
		this.monthSelect.appendChild(opt);
	}
	//and fill the year select box
	for(i=this.rangeYearLower;i<=this.rangeYearUpper;i++)
	{
		opt = document.createElement('option');
		opt.setAttribute('value',i);
		if(this.state == 0 && this.displayYear == i) {
			opt.setAttribute('selected','selected');
		}
		opt.appendChild(document.createTextNode(i));
		this.yearSelect.appendChild(opt);		
	}

	this.monthSelect.owner = this.yearSelect.owner = this;  //hack to allow us to access this calendar in the events (<fix>??)
	
	
	fromToBtn.setAttribute('title',this.fromToBtnTitle);
	fromToBtn.setAttribute('value','ft');
	fromToBtn.setAttribute('type','hidden');
	fromToBtn.setAttribute('name','selmode2');
	fromToBtn.setAttribute('checked','checked');
	fromToBtn.checked=true; 
	fromToBtn.onclick = function(){
		var owner=this.owner;
		if(!owner.ftMode) {
			owner.ftMode = true;
			this.checked=true; 
			owner.oneByOneBtn.checked=false; 
			
		}
	};
	fromToBtn.owner=this;
	
	this.fromToBtn=fromToBtn;
	oneByOneBtn.setAttribute('title',this.oneByOneBtnTitle);
	oneByOneBtn.setAttribute('value','obo');
	oneByOneBtn.setAttribute('type','hidden');
	oneByOneBtn.setAttribute('name','selmode2');
	oneByOneBtn.onclick = function(){
		var owner=this.owner;
		if(owner.ftMode) {
			owner.ftMode = false;
			this.checked=true; 
			owner.fromToBtn.checked=false; 
		}
	};
	oneByOneBtn.owner=this;
	this.oneByOneBtn=oneByOneBtn;
	
	fromToLbl.setAttribute('title',this.fromToLblTitle);
	oneByOneLbl.appendChild(document.createTextNode(this.oneByOneLblText));
	oneByOneLbl.setAttribute('title',this.oneByOneLblTitle);
	
	fromLbl.appendChild(document.createTextNode('From: '));
	fromLbl.setAttribute('title','mm/dd/yyyy');
	toLbl.appendChild(document.createTextNode('To: '));
	toLbl.setAttribute('title','mm/dd/yyyy');
	fromInput.setAttribute('value','N/A');
	fromInput.setAttribute('size','8');
	fromInput.setAttribute('title','mm/dd/yyyy');
	fromInput.owner=this;
	fromToLbl.onMouseOver= function() {
		alert("here");
		//this.calendar.style.display = 'none';
		//this.visible = false;
		};
	fromInput.onfocus= function() {this.select();};
	fromInput.onblur = function() {
		var month,day,year;
		var owner = this.owner;
		try {
			[month,day,year] = this.value.split('/',3);
		}	catch (err) {
			var array=Array();
			array = this.value.split('/',3);
			month=array[0]; day=array[1]; year=array[2];
		};
		month=(month!=null && month>0)?((month<=12)?month-1:false):false;
		year=(year!=null && year >= owner.rangeYearLower)?((year<=owner.rangeYearUpper)?year:false):false;
		day=(day!=null && day>0)?((day<=31)?day:false):false;
		if(year && day && (month || month === 0)) {
			var aux=new Date(year,month,day);
			if(owner.ftEndingPoint) { //If Start point mark not set
				if(aux.getTime()>owner.ftEndingPoint.getTime()) {
					owner.ftStartPoint = owner.ftEndingPoint;
					owner.ftEndingPoint = aux;
				} else {
					owner.ftStartPoint = aux;
				}
				//(Re)Create the selected dates array.
				var aux = new Date(owner.ftStartPoint);
				owner.selectedDates = new Array(new Date(aux));
				while(aux.stepDays(1) <= owner.ftEndingPoint.getTime()) owner.selectedDates.push(new Date(aux));
				
			} else {
				owner.ftStartPoint = aux;
				owner.selectedDates = new Array(aux);
			} // End If set
			owner.reDraw(); //redraw the calendar cell' style to reflect the changes
			owner.fromToBoxContainer.toInput.focus();
		} else {
			this.value='N/A';
		}
	};
	fromInput.owner=this;
	fromInput.onkeypress = function (event) {
		var code;
		if(!event) var event = window.event;
		if(event.keyCode) code = event.keyCode
			else if (event.which)	code = event.which;
		
		if (code == 13) {
			this.onblur();
		}
	}
	

	toInput.setAttribute('value','N/A');
	toInput.setAttribute('size','8');
	toInput.setAttribute('title','mm/dd/yyyy');
	toInput.onfocus= function() {this.select();};
	toInput.onblur = function() {
		var month,day,year;
		var owner = this.owner;
		try {
			[month,day,year] = this.value.split('/',3);
		}	catch (err) {
			var array=Array();
			array = this.value.split('/',3);
			month=array[0]; day=array[1]; year=array[2];
		};
		month=(month!=null && month>0)?((month<=12)?month-1:false):false;
		year=(year!=null && year >= owner.rangeYearLower)?((year<=owner.rangeYearUpper)?year:false):false;
		day=(day!=null && day>0)?((day<=31)?day:false):false;
		if(year && day && (month || month === 0)) {
			var aux=new Date(year,month,day);
			if(owner.ftStartPoint) { //If Start point mark not set
				if(aux.getTime()<owner.ftStartPoint.getTime()) {
					owner.ftEndingPoint = owner.ftStartPoint;
					owner.ftStartPoint = aux;
				} else {
					owner.ftEndingPoint = aux;
				}
				//(Re)Create the selected dates array.
				var aux = new Date(owner.ftStartPoint);
				owner.selectedDates = new Array(new Date(aux));
				while(aux.stepDays(1) <= owner.ftEndingPoint.getTime()) owner.selectedDates.push(new Date(aux));
				
			} else {
				owner.ftEndingPoint = aux;
				owner.selectedDates = new Array(aux);
			} // End If set
			owner.reDraw(); //redraw the calendar cell' style to reflect the changes
		} else {
			this.value='N/A';
		}
	};
	toInput.owner=this;
	toInput.onkeypress = function (event) {
		var code;
		if(!event) var event = window.event;
		if(event.keyCode) code = event.keyCode
			else if (event.which)	code = event.which;
		
		if (code == 13) {
			this.onblur();
		}
	}
	
	
	//Navigation Line Elements
	//add the appropriate children for the month buttons
	var navtable = document.createElement('table');
	var navtbody = document.createElement('tbody');
	var navrow = document.createElement('tr');
	var navback = document.createElement('td');
	var navdisplay = document.createElement('td');
	var navnext = document.createElement('td');
	
	navdisplay.setAttribute('width', '80%');
	navdisplay.style.verticalAlign='middle';
	navdisplay.style.textAlign='center';
	//navdisplay.innerHTML = ' November';
	navdisplay.appendChild(this.monthDisplayLbl);
	
	//navback.setAttribute('width', '5%');
	navback.style.fontFamily='Arial, Sans';
	navback.style.fontSize='11pt';
	navback.style.textAlign='center';
	navback.style.cursor='pointer';
	navback.innerHTML = '«';
	navback.owner = this;

	navnext.style.fontFamily='Arial, Sans';
	navnext.style.fontSize='11pt';
	navnext.style.textAlign='center';
	navnext.style.cursor='pointer';
	navnext.innerHTML = '»';
	navnext.owner = this;


	//assign the event handlers for the controls
	navnext.onmouseup = function () {this.owner.nextMonth();};
	navback.onmouseup = function () {this.owner.prevMonth();};


	navrow.appendChild(navback);
	navrow.appendChild(navdisplay);
	navrow.appendChild(navnext);
	
	//navtable.setAttribute('width', '100%');
	navtable.style.backgroundColor = '#a8c7f9';
	navtable.style.margin='0px';
	navtable.style.padding='0px';
	navtable.style.width='100%';
	navtbody.appendChild(navrow);
	navtable.appendChild(navtbody);
	//navtable.style.backgroundColor='blue';
	
	
	
	monthUp.setAttribute('type','button');
	monthUp.setAttribute('value','»');
	monthUp.setAttribute('title',this.monthup_title);
	monthDn.setAttribute('type','button');
	monthDn.setAttribute('value','«');
	monthDn.setAttribute('title',this.monthdn_title);
	monthUp.owner = monthDn.owner = this;  //hack to allow us to access this calendar in the events (<fix>??)
	monthDnLbl.appendChild(document.createTextNode('Prev.'));	//Remember the lang init func.
	monthUpLbl.appendChild(document.createTextNode('Next'));	//Remember the lang init func.
	
	//Label Element used to show the current month
	this.monthDisplayLbl.changeContent = function(str)	{this.innerHTML='&nbsp;'+str+'&nbsp;';};
	this.monthDisplayLbl.owner = this;
	this.monthDisplayLbl.changeContent(''+this.monthSelect.childNodes[this.monthSelect.value].text+'&nbsp;&nbsp;'+this.yearSelect.value);
	

	fromToBoxContainer.owner=this;
	fromToBoxContainer.fromInput=fromInput;
	fromToBoxContainer.toInput=toInput;
	
	fromToBoxContainer.appendChild(fromLbl);
	fromToBoxContainer.appendChild(fromInput);
	fromToBoxContainer.appendChild(toLbl);
	fromToBoxContainer.appendChild(toInput);
	//Hack to access this element faster
	this.fromToBoxContainer=fromToBoxContainer;
	//Hack to access the header faster
	this.headerContainer=container;
	
	
	container.style.margin='0px';
		container.style.padding='0px';
		container.setAttribute('style','margin-right: 4px; text-align: left;')
		
		container.appendChild(fromToBtn);
		container.appendChild(fromToLbl);
		container.appendChild(oneByOneBtn);
		container.appendChild(oneByOneLbl);
		
		
		container.appendChild(navtable);
		
		
	return container;
};


Epoch2.prototype.createFooter = function () 
{
	var container = document.createElement('div');

	return container;
};

//-----------------------------------------------------------------------------
Epoch2.prototype.resetSelections = function (returnToDefaultMonth)  //PRIVATE: reset the calendar's selection variables to defaults
{
	this.selectedDates = new Array();
	this.rows = new Array(false,false,false,false,false,false,false);
	this.cols = new Array(false,false,false,false,false,false,false);
	if(this.tgt)  //if there is a target element, clear it too
	{
		this.tgt.value = '';
	}
	
	this.fromToBoxContainer.fromInput.value='';
	this.fromToBoxContainer.toInput.value='';
	
	this.ftEndingPoint = null;
	this.ftStartPoint = null;
		
	if(returnToDefaultMonth == true) {
		this.goToMonth(this.displayYearInitial,this.displayMonthInitial);
	
			this.monthDisplayLbl.changeContent(''+this.monthSelect.childNodes[this.monthSelect.value].text+' '+this.yearSelect.value);
	
	}
	else {
		this.reDraw();
	}
};
Epoch2.prototype.resetTheWholeThing = function ()  //Public: reset the calendar's selection variables to defaults
{

	this.performanceTime.value =  '00:00';

	this.ftMode = true;
	this.fromToBtn.setAttribute('checked','checked');
	this.oneByOneBtn.removeAttribute('checked');
	this.fromToBtn.checked=true;
	this.oneByOneBtn.checked=false;
	
	this.resetSelections(true);
};
//-----------------------------------------------------------------------------
Epoch2.prototype.createDayHeading = function ()  //PRIVATE: creates the heading containing the day names
{
	//create the table element
	this.calHeading = document.createElement('table');
	this.calHeading.setAttribute('id',this.name+'_caldayheading');
	this.setClass(this.calHeading,'caldayheading');
	var tbody,tr,td;
	tbody = document.createElement('tbody');
	tr = document.createElement('tr');
	this.cols = new Array(false,false,false,false,false,false,false);
	
	//if we're showing the week headings, create an empty <td> for filler
	if(this.showWeeks)
	{
		td = document.createElement('td');
		td.setAttribute('class','wkhead');
		td.setAttribute('className','wkhead'); //<iehack>
		tr.appendChild(td);
	}
	//populate the day titles
	for(var dow=0;dow<7;dow++)
	{
		td = document.createElement('td');
		td.setAttribute('id','weekhead'+dow);
		
		td.appendChild(document.createTextNode(this.daynames[dow]));
		if(this.selectMultiple) { //if selectMultiple is true, assign the cell a CalHeading Object to handle all events
			td.headObj = new CalHeading2(this,td,(dow + this.startDay < 7 ? dow + this.startDay : dow + this.startDay - 7));
		}
		tr.appendChild(td);
	}
	tbody.appendChild(tr);
	this.calHeading.appendChild(tbody);
	return this.calHeading;	
};
//-----------------------------------------------------------------------------
Epoch2.prototype.createCalCells = function ()  //PRIVATE: creates the table containing the calendar day cells
{
	this.rows = new Array(false,false,false,false,false,false);
	this.cells = new Array();
	var row = -1, totalCells = (this.showWeeks ? 48 : 42);
	var beginDate = new Date(this.displayYear,this.displayMonth,1,0,0,0,0);
  var endDate = new Date(this.displayYear,this.displayMonth,this.monthDayCount[this.displayMonth],0,0,0,0);
	var sdt = new Date(beginDate);
	sdt.setDate(sdt.getDate() + (this.startDay - beginDate.getDay()) - (this.startDay - beginDate.getDay() > 0 ? 7 : 0) );
	//create the table element
	this.CalCells2 = document.createElement('table');
	this.CalCells2.setAttribute('id',this.name+'_CalCells2');
	this.setClass(this.CalCells2,'calcells2');
	var tbody,tr,td;
	tbody = document.createElement('tbody');
	for(var i=0;i<totalCells;i++)
	{
		if(this.showWeeks) //if we are showing the week headings
		{
			if(i % 8 == 0)
			{
				row++;
				tr = document.createElement('tr');
				td = document.createElement('td');
				if(this.selectMultiple) { //if selectMultiple is enabled, create the associated weekObj objects
					td.weekObj = new WeekHeading2(this,td,sdt.getWeek(),row)
				}
				else //otherwise just set the class of the td for consistent look
				{
					td.setAttribute('class','wkhead');
					td.setAttribute('className','wkhead'); //<iehack>
				}				
				td.appendChild(document.createTextNode(sdt.getWeek()));			
				tr.appendChild(td);
				i++;
			}
		}
		else if(i % 7 == 0) //otherwise, new row every 7 cells
		{
			row++;
			tr = document.createElement('tr');
		}
		//create the day cells
		td = document.createElement('td');
		td.appendChild(document.createTextNode(sdt.getDate()));// +' ' +sdt.getUeDay()));
	  var cell = new CalCell2(this,td,sdt,row);
		this.cells.push(cell);
    td.cellObj = cell;
		sdt.setDate(sdt.getDate() + 1); //increment the date
		tr.appendChild(td);
		tbody.appendChild(tr);
	}
	this.CalCells2.appendChild(tbody);
	this.reDraw();
	return this.CalCells2;
};
//-----------------------------------------------------------------------------
Epoch2.prototype.reDraw = function () //PRIVATE: reapplies all the CSS classes for the calendar cells, usually called after chaning their state
{
	this.state = 1;
	var i,j;
	for(i=0;i<this.cells.length;i++) {
		this.cells[i].selected = false;
	}
	for(i=0;i<this.cells.length;i++)
	{
		for(j=0;j<this.selectedDates.length;j++) { //if the cell's date is in the selectedDates array, set its selected property to true
			if(this.cells[i].date.getUeDay() == this.selectedDates[j].getUeDay() ) {
				this.cells[i].selected = true;
			}
		}

		this.cells[i].setClass();
	}
	//alert(this.selectedDates);
	this.state = 2;
};
//-----------------------------------------------------------------------------
Epoch2.prototype.deleteCells = function () //PRIVATE: removes the calendar cells from the DOM (does not delete the cell objects associated with them
{
	this.CalCellContainer.removeChild(this.CalCellContainer.firstChild); //get a handle on the cell table (optional - for less indirection)
	this.cells = new Array(); //reset the cells array
};
//-----------------------------------------------------------------------------
Epoch2.prototype.goToMonth = function (year,month) //PUBLIC: sets the calendar to display the requested month/year
{
	this.monthSelect.value = this.displayMonth = month;
	this.yearSelect.value = this.displayYear = year;
	this.deleteCells();
	this.CalCellContainer.appendChild(this.createCalCells());
};
//-----------------------------------------------------------------------------
Epoch2.prototype.nextMonth = function () //PUBLIC: go to the next month.  if the month is december, go to january of the next year
{
	
	//increment the month/year values, provided they're within the min/max ranges
	if(this.monthSelect.value < 11 && this.monthSelect.value< (this.m2-1)) {
		this.monthSelect.value++;
	}
	else
	{
	
 if(this.monthSelect.value == 11)
	  { 	
       if(this.yearSelect.value < this.rangeYearUpper)
		   {
			 this.monthSelect.value = 0;
			 this.yearSelect.value++;
	 	   }
	 	   else {
  			//alert(this.maxrange_caption);
		  }
		}
		else{
		    //alert("here"+this.yearSelect);
        //if the we are in a smaller year, continue lessthan
        if(this.yearSelect.value < this.y2)
        {
        this.monthSelect.value++;
        }
        else{
        	//alert(this.maxrange_caption);
        }
    
    }
	}
	
	//Refresh the display month label
	this.monthDisplayLbl.changeContent(''+this.monthSelect.childNodes[this.monthSelect.value].text+' '+this.yearSelect.value);
	
	//assign the currently displaying month/year values
	this.displayMonth = this.monthSelect.value;
	this.displayYear = this.yearSelect.value;
	
	//and refresh the calendar for the new month/year
	this.deleteCells();
	this.CalCellContainer.appendChild(this.createCalCells());
};
//-----------------------------------------------------------------------------
Epoch2.prototype.prevMonth = function () //PUBLIC: go to the previous month.  if the month is january, go to december of the previous year
{

	//increment the month/year values, provided they're within the min/max ranges
	if(this.monthSelect.value > 0 && (this.monthSelect.value > (this.m1-1) || this.yearSelect.value > this.y1))
		this.monthSelect.value--;
	else
	{
	 	  if(this.monthSelect.value ==0)
	  {
		  if(this.yearSelect.value > this.rangeYearLower)
		  {
  			this.monthSelect.value = 11;
  			this.yearSelect.value--;
	   	}
		  else {
  			//alert(this.maxrange_caption);
  		}
  	 }
  	 else if(this.monthSelect.value>(this.m1-1))
  	 {
  	    //if the we are in a bigger year, continue biggerequal
        if(this.yearSelect.value >= this.y1)
        {
        this.monthSelect.value--;
        }
      else {
  			//alert(this.maxrange_caption);
  		}
     }
     else if(this.monthSelect.value<=(this.m1-1))
     {
          //if the year is bigger
         if(this.yearSelect.value > this.y1)
         {
         this.monthSelect.value--;
         }
          else {
  			//alert(this.maxrange_caption);
  		}
     }
     else{
     	//alert(this.maxrange_caption);
     }
	}
	
	//Refresh the display month label
	this.monthDisplayLbl.changeContent(''+this.monthSelect.childNodes[this.monthSelect.value].text+' '+this.yearSelect.value);

	//assign the currently displaying month/year values
	this.displayMonth = this.monthSelect.value;
	this.displayYear = this.yearSelect.value;
	
	//and refresh the calendar for the new month/year
	this.deleteCells();
	this.CalCellContainer.appendChild(this.createCalCells());
};
//-----------------------------------------------------------------------------
Epoch2.prototype.addZero = function (vNumber) //PRIVATE: pads a 2 digit number with a leading zero
{
	return ((vNumber < 10) ? '0' : '') + vNumber;
};
//-----------------------------------------------------------------------------
Epoch2.prototype.addDates = function (dates,redraw)  //PUBLIC: adds the array "dates" to the calendars selectedDates array (no duplicate dates) and redraws the calendar
{
	var j,in_sd;
	for(var i=0;i<dates.length;i++)
	{	
		in_sd = false;
		for(j=0;j<this.selectedDates.length;j++)
		{
			if(dates[i].getUeDay() == this.selectedDates[j].getUeDay())
			{
				in_sd = true;
				break;
			}
		}
		if(!in_sd) { //if the date isn't already in the array, add it!
			this.selectedDates.push(dates[i]);
		}
	}
	if(redraw != false) {//redraw  the calendar if "redraw" is false or undefined
		this.reDraw();
	}
};
//-----------------------------------------------------------------------------
Epoch2.prototype.removeDates = function (dates,redraw)  //PUBLIC: adds the dates to the calendars selectedDates array and redraws the calendar
{
	var j;
	for(var i=0;i<dates.length;i++)
	{
		for(j=0;j<this.selectedDates.length;j++)
		{
			if(dates[i].getUeDay() == this.selectedDates[j].getUeDay()) { //search for the dates in the selectedDates array, removing them if the dates match
				this.selectedDates.splice(j,1);
			}
		}
	}
	if(redraw != false) { //redraw  the calendar if "redraw" is false or undefined
		this.reDraw();
	}
};
//-----------------------------------------------------------------------------
Epoch2.prototype.outputDate = function (vDate, vFormat) //PUBLIC: outputs a date in the appropriate format (DEPRECATED)
{
	var vDay			= this.addZero(vDate.getDate()); 
	var vMonth			= this.addZero(vDate.getMonth() + 1); 
	var vYearLong		= this.addZero(vDate.getFullYear()); 
	var vYearShort		= this.addZero(vDate.getFullYear().toString().substring(3,4)); 
	var vYear			= (vFormat.indexOf('yyyy') > -1 ? vYearLong : vYearShort);
	var vHour			= this.addZero(vDate.getHours()); 
	var vMinute			= this.addZero(vDate.getMinutes()); 
	var vSecond			= this.addZero(vDate.getSeconds()); 
	return vFormat.replace(/dd/g, vDay).replace(/mm/g, vMonth).replace(/y{1,4}/g, vYear).replace(/hh/g, vHour).replace(/nn/g, vMinute).replace(/ss/g, vSecond);
};
//-----------------------------------------------------------------------------
Epoch2.prototype.updatePos = function (target) //PUBLIC: moves the calendar's position to target's location (popup mode only)
{
	this.offsetHeight = this.calendar.offsetHeight;
	this.calendar.style.top = (this.getTop(target) - this.offsetHeight) + 'px';
	this.calendar.style.left = this.getLeft(target) + this.leftOffset + 'px'
}
//-----------------------------------------------------------------------------
Epoch2.prototype.recalEnds = function() {	
	try {
		this.selectedDates.sort(this.selectedDates[0].sortfunc);
		if(this.ftStartPoint.getTime()<this.selectedDates[0].getTime()) {
			this.ftStartPoint=this.selectedDates[0];
			this.fromToBoxContainer.fromInput.value=this.ftStartPoint.dateFormat('m/d/Y');
		}
		
		if (this.ftEndingPoint.getTime()>this.selectedDates[this.selectedDates.length-1].getTime()) {
			this.ftEndingPoint=this.selectedDates[this.selectedDates.length-1];
			this.fromToBoxContainer.toInput.value=this.ftEndingPoint.dateFormat('m/d/Y');
		}
	} catch (err) {}

}
/*****************************************************************************/
function CalHeading2(owner,tableCell,dow)
{
	this.owner = owner;
	this.tableCell = tableCell;
	this.dayOfWeek = dow;
	
	//the event handlers
	this.tableCell.onclick = this.onclick;
}
//-----------------------------------------------------------------------------
CalHeading2.prototype.onclick = function ()
{
//select weekend
	//reduce indirection:
	var owner = this.headObj.owner;
	var sdates = owner.selectedDates;
	var cells = owner.cells;
	
	owner.cols[this.headObj.dayOfWeek] = !owner.cols[this.headObj.dayOfWeek];
	if(!owner.ftStartPoint && !owner.ftEndingPoint) {  
	
		owner.ftStartPoint=new Date(owner.displayYear,owner.displayMonth,1);
		owner.ftEndingPoint=new Date(owner.displayYear,owner.displayMonth,owner.ftStartPoint.monthDays());
		//Set the toInput && fromInput value
		owner.fromToBoxContainer.fromInput.value=owner.ftStartPoint.dateFormat('m/d/Y');
		owner.fromToBoxContainer.toInput.value=owner.ftEndingPoint.dateFormat('m/d/Y');
	}
	
	var aux = new Date(owner.ftStartPoint);
	var aux2;
	var dateArr=Array();
	var atLeastOne = false;
	
	do { 
		if(aux.getDay() == this.headObj.dayOfWeek) {
			if((aux2=sdates.arrayDateIndex(aux)) == -1) {//Inserting
				//sdates.push(new Date(aux));
				if(!atLeastOne) dateArr.push(new Date(aux));
			} else	{
				//sdates.splice(aux2,1);
				if(!atLeastOne) dateArr=Array();
				dateArr.push(new Date(aux));
				atLeastOne=true;
			}
		}
	} while(aux.stepDays(1)<=owner.ftEndingPoint.getTime());

	if(atLeastOne) { 
		for(var i=0; i<dateArr.length; i++) {
			sdates.splice(sdates.arrayDateIndex(dateArr[i]),1);
		}
	} else {
		for(var i=0; i<dateArr.length; i++)
			sdates.push(dateArr[i]);
	}

	owner.reDraw();
};
/*****************************************************************************/
function WeekHeading2(owner,tableCell,week,row)
{
	this.owner = owner;
	this.tableCell = tableCell;
	this.week = week;
	this.tableRow = row;
	this.tableCell.setAttribute('class','wkhead');
	this.tableCell.setAttribute('className','wkhead'); //<iehack>
	//the event handlers
	this.tableCell.onclick = this.onclick;
}
//-----------------------------------------------------------------------------
WeekHeading2.prototype.onclick = function ()
{

	//reduce indirection:
	var owner = this.weekObj.owner;
	var cells = owner.cells;
	var sdates = owner.selectedDates;
	var i,j;
	
	owner.rows[this.weekObj.tableRow] = !owner.rows[this.weekObj.tableRow];
	if(!owner.ftStartPoint && !owner.ftEndingPoint) {
	
		owner.ftStartPoint=new Date(owner.displayYear,owner.displayMonth,1);
		owner.ftEndingPoint=new Date(owner.displayYear,owner.displayMonth,owner.ftStartPoint.monthDays());
		//Set the toInput && fromInput value
		owner.fromToBoxContainer.fromInput.value=owner.ftStartPoint.dateFormat('m/d/Y');
		owner.fromToBoxContainer.toInput.value=owner.ftEndingPoint.dateFormat('m/d/Y');
	}
	var dateArr=Array();
	var atLeastOne = false;
	for(i=0;i<cells.length;i++)
	{
		if(cells[i].tableRow == this.weekObj.tableRow && cells[i].date.getTime()>=owner.ftStartPoint.getTime() && cells[i].date.getTime()<=owner.ftEndingPoint.getTime())
		{
			if((!owner.selCurMonthOnly || cells[i].date.getMonth() == owner.displayMonth && cells[i].date.getFullYear() == owner.displayYear) && (!owner.ftEndingPoint || owner.ftEndingPoint.getTime() >= cells[i].date.getTime()) && (!owner.ftStartPoint || owner.ftStartPoint.getTime() <= cells[i].date.getTime()) ) { 
				var aux;
				if((aux=sdates.arrayDateIndex(cells[i].date)) == -1) {//Inserting
					if(!atLeastOne) dateArr.push(cells[i].date);
				} else	{
					if(!atLeastOne) dateArr=Array();
					dateArr.push(cells[i].date);
					atLeastOne=true;
				}	
			}
		}
	}
	if(atLeastOne) { 
		for(var i=0; i<dateArr.length; i++) {
			sdates.splice(sdates.arrayDateIndex(dateArr[i]),1);
		}
	} else {
		for(var i=0; i<dateArr.length; i++)
			{
    
      sdates.push(dateArr[i]);
      }
	}
	

	owner.reDraw();
};
/*****************************************************************************/
//-----------------------------------------------------------------------------
function CalCell2(owner,tableCell,dateObj,row)
{
	this.owner = owner;		//used primarily for event handling
	this.tableCell = tableCell; 			//the link to this cell object's table cell in the DOM
	this.cellClass;			//the CSS class of the cell
	this.selected = false;	//whether the cell is selected (and is therefore stored in the owner's selectedDates array)
	this.date = new Date(dateObj);
  //alert("here"+this.date.getFullYear());
	this.dayOfWeek = this.date.getDay();
	this.week = this.date.getWeek();
	this.tableRow = row;
	
	//assign the event handlers for the table cell element
	this.tableCell.onclick = this.onclick;
	this.tableCell.onmouseover = this.onmouseover;
	this.tableCell.onmouseout = this.onmouseout;
	
	//and set the CSS class of the table cell
	this.setClass();
}
//-----------------------------------------------------------------------------
CalCell2.prototype.onmouseover = function () //replicate CSS :hover effect for non-supporting browsers <iehack>
{
	this.setAttribute('class',this.cellClass + ' hover');
	this.setAttribute('className',this.cellClass + ' hover');
};
//-----------------------------------------------------------------------------
CalCell2.prototype.onmouseout = function () //replicate CSS :hover effect for non-supporting browsers <iehack>
{
	this.cellObj.setClass();
};
//-----------------------------------------------------------------------------
CalCell2.prototype.onclick = function () 
{
	//reduce indirection:
	var cell = this.cellObj;
	var owner = cell.owner;
	
	   var check = 0;
     
       
       if(cell.date.getFullYear() == owner.y2 && cell.date.getMonth() == (owner.m2-1))
       {
          if(cell.date.getDate() <= owner.d2)
          check = 1;
          else
          check = 0;
       }
       else if(cell.date.getFullYear() == owner.y1 && cell.date.getMonth() == (owner.m1-1)) 			 
       {
          if(cell.date.getDate() >= owner.d1)
          check = 1;
          else
          check = 0;
       }   
       else
       {
          check = 1;
       }
      //add extra condition for the days that appear in the next month of the last month displayed 
      if(cell.date.getFullYear() == owner.y2 &&  cell.date.getMonth()>owner.monthSelect.value && cell.date.getMonth()>(owner.m2-1))
      {
        check =0;
      }
      //add another extra condition if we are at the first month m1 and the previous month cells appears 
      if(cell.date.getFullYear() == owner.y1 &&  cell.date.getMonth()<(owner.m1-1))
      {
        check =0;
      }
      if(cell.date.getFullYear() < owner.y1)
      {
        check =0;
      }  
      //add condition for the days be4 the starting day in the same month 
      if(cell.date.getDate() < owner.d1 && cell.date.getMonth() == owner.monthSelect.value && cell.date.getMonth() == (owner.m1-1))
      {
        check=0;
      }
	
	if(check == 1){
	if(!owner.ftMode) { 
		if(!owner.selCurMonthOnly || cell.date.getMonth() == owner.displayMonth && cell.date.getFullYear() == owner.displayYear)
		{
			
			if(!owner.ftStartPoint) { //If Start point mark not set
				owner.ftStartPoint = cell.date;
				owner.selectedDates = new Array(cell.date);
				//Set the fromInput value
				owner.fromToBoxContainer.fromInput.value=owner.ftStartPoint.dateFormat('m/d/Y');
			} else { // Start Point is set....
				if(!owner.ftEndingPoint) { //Ending Point not yet set
					if(cell.date.getTime()<owner.ftStartPoint) {
						owner.ftEndingPoint = owner.ftStartPoint;
						owner.ftStartPoint = cell.date;
						//Set the toInput && fromInput value
						owner.fromToBoxContainer.fromInput.value=owner.ftStartPoint.dateFormat('m/d/Y');
						owner.fromToBoxContainer.toInput.value=owner.ftEndingPoint.dateFormat('m/d/Y');
					} else {
						owner.ftEndingPoint = cell.date;
						//Set the toInput value
						owner.fromToBoxContainer.toInput.value=owner.ftEndingPoint.dateFormat('m/d/Y');
					}
				} else {
					if(cell.date.getTime()> owner.ftEndingPoint.getTime()) {
						owner.ftEndingPoint = cell.date;
						//Set the toInput value
						owner.fromToBoxContainer.toInput.value=owner.ftEndingPoint.dateFormat('m/d/Y');
					} else if(cell.date.getTime()< owner.ftStartPoint.getTime()) {
						owner.ftStartPoint = cell.date;
						//Set the fromInput value
						owner.fromToBoxContainer.fromInput.value=owner.ftStartPoint.dateFormat('m/d/Y');
					}
				}
				var aux;
				if((aux=owner.selectedDates.arrayDateIndex(cell.date)) == -1) {//Inserting
					owner.selectedDates.push(cell.date);
				} else	{
					owner.selectedDates.splice(aux,1);
				}
					
				//owner.selectedDates.push(cell.date);
			}			
		
			owner.reDraw(); //redraw the calendar cell styles to reflect the changes
		}
		owner.recalEnds(); 
	}	else {
		if(!owner.ftStartPoint) { //If Start point mark not set
			owner.ftStartPoint = cell.date;
			owner.selectedDates = new Array(cell.date);
			//Set the fromInput value
			owner.fromToBoxContainer.fromInput.value=owner.ftStartPoint.dateFormat('m/d/Y');
		} else { // Start Point is set....
			if(cell.date.getTime()<owner.ftStartPoint) {
				owner.ftEndingPoint = owner.ftStartPoint;
				owner.ftStartPoint = cell.date;
				//Set the toInput && fromInput value
				owner.fromToBoxContainer.fromInput.value=owner.ftStartPoint.dateFormat('m/d/Y');
				owner.fromToBoxContainer.toInput.value=owner.ftEndingPoint.dateFormat('m/d/Y');
			} else {
				owner.ftEndingPoint = cell.date;
				//Set the toInput value
				owner.fromToBoxContainer.toInput.value=owner.ftEndingPoint.dateFormat('m/d/Y');
			}
			//(Re)Create the selected dates array.
			var aux = new Date(owner.ftStartPoint);
			owner.selectedDates = new Array(new Date(aux));
			while(aux.stepDays(1) <= owner.ftEndingPoint.getTime()) owner.selectedDates.push(new Date(aux));
		}
		owner.reDraw(); //redraw the calendar cell styles to reflect the changes
	}
	
	}
	else{
  //alert("out of range");
  }
};
//-----------------------------------------------------------------------------
CalCell2.prototype.setClass = function ()  //private: sets the CSS class of the cell based on the specified criteria
{
  //check if the date fits the allowed dates
       var cell = this;
       var owner = this.owner;
       var check = 0;
       //check the day only if the month and year are at one of the edges
       
       if(cell.date.getFullYear() == owner.y2 && cell.date.getMonth() == (owner.m2-1))
       {
          if(cell.date.getDate() <= owner.d2)
          check = 1;
          else
          check = 0;
       }
       else if(cell.date.getFullYear() == owner.y1 && cell.date.getMonth() == (owner.m1-1)) 			 
       {
          if(cell.date.getDate() >= owner.d1)
          check = 1;
          else
          check = 0;
       }   
       else
       {
          check = 1;
       }
      //add extra condition for the days that appear in the next month of the last month displayed 
      if(cell.date.getFullYear() == owner.y2 &&  cell.date.getMonth()>owner.monthSelect.value && cell.date.getMonth()>(owner.m2-1))
      {
        check =0;
      }
         //add another extra condition if we are at the first month m1 and the previous month cells appears 
      if(cell.date.getFullYear() == owner.y1 &&  cell.date.getMonth()<(owner.m1-1))
      {
        check =0;
      }
      if(cell.date.getFullYear() < owner.y1)
      {
        check =0;
      } 
      //add condition for the days be4 the starting day in the same month 
      if(cell.date.getDate() < owner.d1 && cell.date.getMonth() == owner.monthSelect.value && cell.date.getMonth() == owner.m1-1)
      {
         
        check=0;
      }
	if(this.selected) {
		this.cellClass = 'cell_selected';
	}
	
	else if(this.owner.displayMonth != this.date.getMonth() && check==1) {
    this.cellClass = 'niznotmonth';	
	}
	else if(this.owner.displayMonth != this.date.getMonth() && check==0) {
		this.cellClass = 'notmnth';	
	}
	else if(check == 1){
  	this.cellClass = 'niz';
  }
	else if(this.date.getDay() > 0 && this.date.getDay() < 6) {
		this.cellClass = 'wkday';
	}
	else {
		this.cellClass = 'wkend';
	}
	
	if(this.date.getFullYear() == this.owner.curDate.getFullYear() && this.date.getMonth() == this.owner.curDate.getMonth() && this.date.getDate() == this.owner.curDate.getDate() && !this.selected) {
		//this.cellClass = this.cellClass + ' curdate';
				 	this.cellClass = 'tday';
	}

	this.tableCell.setAttribute('class',this.cellClass);
	this.tableCell.setAttribute('className',this.cellClass); //<iehack>
};
/*****************************************************************************/
Date.prototype.getDayOfYear = function () //returns the day of the year for this date
{
	return parseInt((this.getTime() - new Date(this.getFullYear(),0,1).getTime())/86400000 + 1);
};
//-----------------------------------------------------------------------------
Date.prototype.getWeek = function () //returns the day of the year for this date
{
	return parseInt((this.getTime() - new Date(this.getFullYear(),0,1).getTime())/604800000 + 1);
};
//-----------------------------------------------------------------------------

Date.prototype.getFirstDayOfAWeek = function (week,year,offset) {
	year = (year?year:this.getFullYaer());
	offset = (offset!=null?offset:0);

	
	var aux = new Date((week-1)*604800000 + new Date(year,0,1).getTime());
	aux.setDate(aux.getDate()+offset);
	aux.setTime(aux.getTime() + (aux.getTimezoneOffset() - this.getTimezoneOffset()) * 60000);
	return (aux);
};

Date.prototype.getUeDay = function () //returns the number of DAYS since the UNIX Epoch - good for comparing the date portion
{
	return parseInt(Math.floor((this.getTime() - this.getTimezoneOffset() * 60000)/86400000)); //must take into account the local timezone
};
//-----------------------------------------------------------------------------
Date.prototype.dateFormat = function(format)
{
	if(!format) { // the default date format to use - can be customized to the current locale
		format = 'm/d/Y';
	}
	LZ = function(x) {return(x < 0 || x > 9 ? '' : '0') + x};
	//Janvier · 	Février · 	Mars · 	Avril · 	Mai · 	Juin · 	Juillet · 	Août · 	Septembre · 	Octobre · 	Novembre · 	Décembre
	var MONTH_NAMES = new Array('Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre','Jan','Fév','Mar','Avr','Mai','Jun','Jul','Aoû','Sep','Oct','Nov','Déc');
	//var MONTH_NAMES = new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	//var DAY_NAMES = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	var DAY_NAMES = new Array('Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dim','Lun','Mar','Mer','Jeu','Ven','Sam');
	format = format + "";
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=this.getFullYear().toString();
	var M=this.getMonth()+1;
	var d=this.getDate();
	var E=this.getDay();
	var H=this.getHours();
	var m=this.getMinutes();
	var s=this.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
	// Convert real this parts into formatted versions
	var value = new Object();
	//if (y.length < 4) {y=''+(y-0+1900);}
	value['Y'] = y.toString();
	value['y'] = y.substring(2);
	value['n'] = M;
	value['m'] = LZ(M);
	value['F'] = MONTH_NAMES[M-1];
	value['M'] = MONTH_NAMES[M+11];
	value['j'] = d;
	value['d'] = LZ(d);
	value['D'] = DAY_NAMES[E+7];
	value['l'] = DAY_NAMES[E];
	value['G'] = H;
	value['H'] = LZ(H);
	if (H==0) {value['g']=12;}
	else if (H>12){value['g']=H-12;}
	else {value['g']=H;}
	value['h']=LZ(value['g']);
	if (H > 11) {value['a']='pm'; value['A'] = 'PM';}
	else { value['a']='am'; value['A'] = 'AM';}
	value['i']=LZ(m);
	value['s']=LZ(s);
	//construct the result string
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
		}
	return result;
};
//-----------------------------------------------------------------------------

Date.prototype.sortfunc = function (a,b) {
	if(a.getFullYear() == b.getFullYear()) { // same year
		if(a.getMonth() == b.getMonth()) { //same month
			if(a.getDate() == b.getDate()) { //same day
				return 0;
			}	else { //same day
				if(a.getDate() > b.getDate())	return 1;
					else	return -1;
			}// same day
		} else { //same month
			if(a.getMonth() > b.getMonth())	return 1;
				else	return -1;
		} // same month
	} else { // same year
		if(a.getFullYear() > b.getFullYear())	return 1;
			else	return -1;
	} //same year
}

//-----------------------------------------------------------------------------

Date.prototype.stepDays = function(offset) {
	this.setDate(this.getDate()+offset);
	return this.getTime();
}
//-----------------------------------------------------------------------------

Date.prototype.monthDays = function(myDate) {
	myDate=(myDate==null)?this:myDate;
	var days = Array(31,((myDate.getFullYear()%4)?28:29),31,30,31,30,31,31,30,31,30,31);
	return days[myDate.getMonth()];
}
/*****************************************************************************/
Array.prototype.arrayIndex = function(searchVal,startIndex) //similar to array.indexOf() - created to fix IE deficiencies
{
	startIndex = (startIndex != null ? startIndex : 0); //default startIndex to 0, if not set
	for(var i=startIndex;i<this.length;i++)
	{
		if(searchVal == this[i]) {
			return i;
		}
	}
	return -1;
};
//-----------------------------------------------------------------------------

Array.prototype.arrayDateIndex = function(searchVal,startIndex) //similar to array.indexOf() - created to fix IE deficiencies
{
	startIndex = (startIndex != null ? startIndex : 0); //defaults startIndex to 0, if not set
	for(var i=startIndex;i<this.length;i++)
	{
		if(searchVal.getTime() == this[i].getTime()) {
			return i;
		}
	}
	return -1;
	
};
/*****************************************************************************/
