/**********************************************************
***********************************************************
*  Copyright 2004 (c) Aumediage S.P.R.L. All Rights Reserved.						*
*  This is a commercial software product, please visit								*
*  http://www.aumediage.net for more information. or send email to					*
*  aumediage@hotmail.com															*
*  See http://www.aumediage.net for Commercial License Agreement					*
*  All Copyright statements must always remain in place in all files at all times	*
*		***** PLEASE NOTE: THIS IS NOT FREE SOFTWARE	*****						*
*				*****	IT MUST BE LICENSED FOR ALL USE	*****						*
*  HISTORY																			*
*	Name			Date				Action			Version						*
*	GdB				27/06/2003			Creation							1.00	*
*	GdB				21/10/2003			Mutliple object creation	2.00			*
*																					*
* TO DO : image move up. Fix div settings
************************************************************
***********************************************************/
var g_calendarInstanceName;
//var aryMonthName = new Array("January|Jan","February|Feb","March|Mar","April|Apr","May|May","June|Jun","July|Jul","Augustus|Aug","September|Sep","October|Oct","November|Nov","December|Dec"); 
//var aryWeekDayName = new Array("Monday|Mon", "Tuesday|Tue", "Wednesday|Wed", "Thursday|Thu", "Friday|Fri", "Saturday|Sat", "Sunday|Sun");

function AMGCalendarForm(__sID, __sLinkedName, __sLinkedStyle, __bNeedHours, __dDate, __dStartDate, __dEndDate, __sBgColor){
	this.Constructor			= AMGCalendar;
	this.Constructor			(__sID, __bNeedHours, __dDate, __dStartDate, __dEndDate, __sBgColor);
	this.HasValueViewer	= true;
	this.HasLinkedForm		= true;
	this.FormToFill				= __sLinkedName;
	this.FormToFillStyle		= __sLinkedStyle;
	this.AutoSave				= AMGCalendarFormSaveAs;
	this.AddOnload(this.ID+'.AutoSave();');
}
//===================================
function AMGCalendarFormSaveAs(){
	if (this.SelectedDate ){
		if (this.HasLinkedForm){
			this.FormToFill= (this.HasLinkedForm)?eval(this.FormToFill):null;
			var sFormat = (this.NeedHours)?'dd-mm-yyyy-hh-nn':'dd-mm-yyyy';
			this.FormToFill.value		= formatCustomDate(this.SelectedDate, sFormat, this.NeedHours);
		}
		if (this.HasValueViewer){
			writeLayer(getObject(this.Prefix+'_Linked', false) , formatCustomDate(this.SelectedDate, this.DateFormat, this.NeedHours));
		}
	}
}
/*******************************************************
********************************************************
********************************************************
 GO TO CLASS CREATION
********************************************************
*******************************************************/
function AMGCalendarGoto(__sID, __sUrl, __sViewType, __bNeedHours, __dDate, __dStartDate, __dEndDate, __sBgColor){
	this.Constructor	= AMGCalendar;
	this.Constructor	(__sID, __bNeedHours, __dDate, __dStartDate, __dEndDate, __sBgColor);
	this.goToUrl		= __sUrl;
	this.goToStyle		= __sViewType;
	this.AutoSave		= AMGCalendarGotoSaveAs;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function AMGCalendarGotoSaveAs(__iYear, __iMonth, __iDay){
	this.SelectedDate = new Date(__iYear, __iMonth, __iDay);
	alert('Go to :' + this.goToUrl	 + '?' + this.goToStyle);
}


// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
// SUPER CLASS CREATION
// *****************************************************************************
// *****************************************************************************
function AMGCalendar(__sID, __bNeedHours, __dDate, __dStartDate, __dEndDate){
	this.Constructor		= AMGBaseObject;
	this.Constructor		(__sID);
	this.Prefix				= 'AMGCalendar_'+ this.ID;
	
	this.DateFormat			= getDateFormat();
	this.Width				= 210;
	this.DayWidth			= Math.floor(this.Width/7);
	this.Height				= 300;
	this.Exists				= false;
	this.SelectedDate		= IsDate(__dDate)?__dDate:null;
	this.OriginalDate		= this.SelectedDate;
	this.TodayDate			= new Date();
	this.CurrentMonth		= this.SelectedDate || new Date();
	this.StartRangeDate		= __dStartDate;
	this.EndRangeDate		= __dEndDate;
	this.NeedHours			= (__bNeedHours == true)?true:false;
	this.CssPrefix			= 'calendar';
	this.WeekHeader			;
	this.AryMonthName		= g_AryMonthName || new Array("January|Jan","February|Feb","March|Mar","April|Apr","May|May","June|Jun","July|Jul","Augustus|Aug","September|Sep","October|Oct","November|Nov","December|Dec"); 
	this.AryWeekDayName		= g_AryWeekDayName || new Array("Monday|Mon", "Tuesday|Tue", "Wednesday|Wed", "Thursday|Thu", "Friday|Fri", "Saturday|Sat", "Sunday|Sun");
	this.TimeOutID			;
	this.FullContent		;
	this.ShowDelay			= 5000;
	this.PreviousYear		= 0;
	this.NextYear			= 25;
	this.PixPath			= '/pix/';
	this.FirstDayOfWeek		= 0;
	this.DefaultHour		= 12;
	this.AryCommonVoc	= new Array('This is not a valid date or the date is not in the range', 'Loading');	
	
	this.Init				= AMGCalendarInit;
	this.WriteHtml			= AMGCalendarWriteHtml;
	this.OpenClose			= AMGCalendarOpenClose;
	this.Populate			= AMGCalendarPopulate;
	this.GetHtmlHeader		= AMGCalendarGetHtmlHeader;
	this.ShowMonth			= AMGCalendarShowMonth;
	this.GetOtherDays		= AMGCalendarGetOtherDays;
	this.GetDay				= AMGCalendarGetDay;
	this.SelectDate			= AMGCalendarSelectDate;
	this.ChangeMonth		= AMGCalendarChangeMonth;
	this.ChangeYear			= AMGCalendarChangeYear;
	this.GetHtmlNav			= AMGCalendarGetHtmlNav;
	this.AutoSave			= AMGCalendarendarSaveAs;
	this.SetTimeout			= AMGCalendarSetTimeout;
	this.Hide				= AMGCalendarHide;
	this.ShowHide			= AMGCalendarShowHide;
	this.MoveTo				= AMGCalendarMoveTo;
	this.GetHtmlTime		= AMGCalendarGetHtmlTime;
	this.ChangeTime			= AMGCalendarChangeTime;
	this.SetWidth			= AMGCalendarSetWidth;
	this.CreateInvisible			= oCreateInvisible;
	this.Init();
}
function oCreateInvisible(){
	this.OpenClose();
	this.Hide();
}
//===================================
function AMGCalendarInit(){
	if (!IsDate(this.StartRangeDate)){
		this.StartRangeDate		= ( IsDateBigger(this.TodayDate, this.SelectedDate))?this.SelectedDate:this.TodayDate;
	}
	if (!IsDate(this.EndRangeDate))
		this.EndRangeDate		= doDateAdd(this.TodayDate, 'y', this.NextYear);
}
/*****************************************************************/
function AMGCalendarSetWidth(__iWidth){
	this.Width				= __iWidth;
	this.DayWidth			= Math.floor(this.Width/7);
}
/*****************************************************************/
function AMGCalendarendarSaveAs(){
	if (this.SelectedDate ){
		if (this.HasLinkedForm)		
			this.FormToFill.value		= formatCustomDate(this.SelectedDate, 'dd-mm-yyyy', this.NeedHours);
		if (this.HasValueViewer)		
			writeLayer(getObject(this.Prefix+'_Linked', false) , formatCustomDate(this.SelectedDate, this.DateFormat, this.NeedHours));
	}
}
/*****************************************************************/
function AMGCalendarWriteHtml(){
	var sBuffer = '';
	var sImage	= '<a href="javascript:'+this.ID+'.OpenClose();"><img src="'+this.PixPath+'calendar/icon.calendar.gif" id="'+this.Prefix+'_Image" border="0" height="16"></a>'; 
	if (this.HasValueViewer){
		sBuffer += '	<table cellpadding="0" cellspacing="0" border="0" class="'+this.FormToFillStyle+'FormCalendarTable">';
		sBuffer += '	<tr>';
		sBuffer += '	<td class="FormCalendarViewer" id="'+ this.FormToFill.substring(this.FormToFill.lastIndexOf('.')+1) +'Viewer">';
		sBuffer += '	<div id="'+this.Prefix+'_Linked" style="">&nbsp;</div>';
		sBuffer += '	</td>';
		sBuffer += '	<td class="FormCalendarButton">';
	}
	sBuffer += '	<div id="'+this.Prefix+'_ImageContainer" style="border:0px solid black;">' + sImage + '</div>';
	sBuffer += '	<div id="'+this.Prefix+'_Hour" style="display:none">'+this.DefaultHour+'</div>';
	sBuffer += '	<div id="'+this.Prefix+'_Minut" style="display:none">0</div>';
	if (this.HasValueViewer){
		sBuffer	+= '	</td>';
		sBuffer	+= '	</tr>';
		sBuffer	+= '	</table>';
	}
	sBuffer	+= '	<div id="'+this.Prefix+'" style="position:absolute;left:0;top:0;width:'+this.Width+';height:100;visibility:'+sHidden+';" class="'+this.CssPrefix+'Container">'+this.AryCommonVoc[1]+'</div>';
	this.WriteToDoc(sBuffer);
}
/*****************************************************************/
function AMGCalendarOpenClose(__bWhich){
	g_calendarInstanceName = (typeof(g_calendarInstanceName) == 'undefined' ) ? null : g_calendarInstanceName;
	if (!this.Exists){
		this.Hide();
	}
	if (g_calendarInstanceName != this.ID && g_calendarInstanceName != null){
		var oOtherCalendar = eval(g_calendarInstanceName);
		oOtherCalendar.Hide();
		oOtherCalendar.SetTimeout(false);
	}
	this.ShowHide(this.ZIndex, -this.ZIndex);
	var iImageWidth		= getObjWidth(this.Prefix+'_Image');
	var iOffsetLeft			= getObjWindowOffsetLeft(this.Prefix+'_ImageContainer');
	var iOffsetTop			= getObjWindowOffsetTop(this.Prefix+'_ImageContainer');
	this.MoveTo( (iOffsetLeft + iImageWidth+5), iOffsetTop);
	if (!this.Exists)	
		this.Populate();
	if (this.Container.style.visibility ==  sHidden){
		this.SetTimeout(false);
		g_calendarInstanceName = null;
	}
	else{
		this.SetTimeout(true);
		g_calendarInstanceName = this.ID;
	}
}
/*****************************************************************/
function AMGCalendarPopulate(){
	this.GetHtmlHeader();
	this.ShowMonth();
	this.Exists	= true;
}
/*****************************************************************/
function AMGCalendarChangeMonth(__iCount){
	this.CurrentMonth = doDateAdd(this.CurrentMonth, 'm', __iCount);
	this.ShowMonth();
}
/*****************************************************************/
function AMGCalendarSelectDate(__iYear, __iMonth, __iDay){
	var sOldSelectedString	= formatCustomDate(this.SelectedDate , 'yyyy_mm_dd');
	var sNewSelectedString	= formatCustomDate(new Date(__iYear, __iMonth, __iDay) , 'yyyy_mm_dd');
	var sTodayString		= formatCustomDate(this.TodayDate , 'yyyy_mm_dd');
	if (getExists(this.Prefix +'_Day_'+sOldSelectedString)){
		var oOldSelectedDay		= this.SelectedDate ? getObject(this.Prefix +'_Day_'+sOldSelectedString): null;
		var sOldClassName		= oOldSelectedDay.className;
		oOldSelectedDay.className = sOldClassName.replace('Sel','');
	}
	var oNewSelectedDay		= getObject(this.Prefix +'_Day'+ '_' + sNewSelectedString);
	var sNewClassName		= oNewSelectedDay.className;
	oNewSelectedDay.className = oNewSelectedDay.className + 'Sel';
	var iHour = getInnerHtml(this.Prefix+'_Hour');
	var iMinut = getInnerHtml(this.Prefix+'_Minut');
	this.SelectedDate = new Date(__iYear, __iMonth, __iDay, iHour, iMinut);
	this.AutoSave();
	this.Hide();
}
/*****************************************************************/
function AMGCalendarChangeYear(__iYear){
	var iCurrentYear = this.CurrentMonth.getFullYear();
	this.CurrentMonth = doDateAdd(this.CurrentMonth, 'y', __iYear-iCurrentYear);
	this.ShowMonth();
}
/*****************************************************************/
function AMGCalendarChangeTime(__iHour, __iFrom){
	var oHolder;
	if (__iFrom == 2){
		oHolder = getObject(this.Prefix+'_Minut');
		writeLayer(oHolder, __iHour);
	}
	else{
		oHolder = getObject(this.Prefix+'_Hour');
		writeLayer(oHolder, __iHour);
	}
	var iHour	= getInnerHtml(this.Prefix+'_Hour');
	var iMinut	= getInnerHtml(this.Prefix+'_Minut');
	if (IsDate(this.SelectedDate)){
		this.SelectedDate = new Date(this.SelectedDate.getFullYear(), this.SelectedDate.getMonth(), this.SelectedDate.getDate(), iHour, iMinut);
		this.AutoSave();
	}
}
/*****************************************************************/
function AMGCalendarGetOtherDays(__iCount, __dDate, __bIsBefore){
	var sBuffer		= '';
	if ( __iCount < 0 && __bIsBefore)
		__iCount = 7+__iCount;
	else if ( __iCount < 0 && !__bIsBefore)
		__iCount =Math.abs(__iCount);
	else if ( !__bIsBefore)
		__iCount = 7 - __iCount;
	if (__bIsBefore) 
		__dDate = doDateAdd(__dDate, 'd', -(__iCount-1));
	var iWhile = 0;
	while (__iCount > 0){
		sBuffer		+= this.GetDay(doDateAdd(__dDate, 'd', iWhile), false) ;
		iWhile++;
		__iCount--;
	}
	return(sBuffer);
}
/*****************************************************************/
function AMGCalendarShowMonth(){
	var sBuffer				= '';
	sBuffer					+= this.GetHtmlNav();
	sBuffer					+= '<table width="'+this.Width+'" cellpadding="0" cellspacing="0" border="0" class="'+this.CssPrefix+'Day">';
	sBuffer					+= this.WeekHeader;
	var dFirstDayOfMonth	= getFirstDayOfMonth(this.CurrentMonth);
	var dLastDayOfMonth		= getLastDayOfMonth(this.CurrentMonth);
	var iFirstDayOfMonth	= getFirstWeekDayOfMonth(this.CurrentMonth);
	var iDayOfWeekCount		= iFirstDayOfMonth;
	var iLastDayOfWeek		= (this.FirstDayOfWeek == 0)?6:this.FirstDayOfWeek-1;
	if ((iFirstDayOfMonth - this.FirstDayOfWeek) != 0){
		sBuffer				+= '<tr>' + this.GetOtherDays(iFirstDayOfMonth - this.FirstDayOfWeek, doDateAdd(dFirstDayOfMonth, 'd', -1), true);
	}
	for (var i = 1 ; i <= dLastDayOfMonth.getDate() ; i++){
		if (iDayOfWeekCount == this.FirstDayOfWeek)
			sBuffer			+= '<tr>';
		sBuffer				+= this.GetDay(new Date(this.CurrentMonth.getFullYear(), this.CurrentMonth.getMonth(), i), true);
		
		if (iDayOfWeekCount == iLastDayOfWeek)
			sBuffer			+= '</tr>';
		if (iDayOfWeekCount==6)
			iDayOfWeekCount = 0;
		else
			iDayOfWeekCount++;
	}
	if ((iDayOfWeekCount - this.FirstDayOfWeek) != 0){
		sBuffer				+= this.GetOtherDays((iDayOfWeekCount - this.FirstDayOfWeek), doDateAdd(dLastDayOfMonth, 'd', 1), false);
	}
	if (this.NeedHours)
		sBuffer					+= this.GetHtmlTime();
	sBuffer					+= '</table>';
	writeLayer(this.Container, sBuffer);
	return(sBuffer);
}
/*****************************************************************/
function AMGCalendarGetHtmlTime(){
	var sBuffer = '';
	var iHour = 12;
	var iMinut = 0;
	sBuffer += '<tr>';
	sBuffer += '<td colspan="7" class="calendarTime">';
	if (this.SelectedDate){
		iHour	= this.SelectedDate.getHours();
		iMinut	= this.SelectedDate.getMinutes();
	}
	if (this.NeedHours){
		sBuffer += '<select class="calendarYearSelect" style="'+this.GetScrollStyle()+'" onMouseOver="'+this.ID+'.SetTimeout(false);" onMouseOut="'+this.ID+'.SetTimeout(true);" onchange="'+this.ID+'.ChangeTime(this.value, 1)">';
					for(var i = 0; i <24 ; i++){
						sBuffer += '<option value="'+i+'"';
						if (i == iHour)
							sBuffer += ' selected="true"';
						sBuffer += '\>&nbsp;';
						if (i<10)
							sBuffer += '0';
						sBuffer += i+'&nbsp;';
					}
		sBuffer += '</select>&nbsp;:&nbsp;';
		sBuffer += '<select class="calendarYearSelect" style="'+this.GetScrollStyle()+'" onMouseOver="'+this.ID+'.SetTimeout(false);" onMouseOut="'+this.ID+'.SetTimeout(true);" onchange="'+this.ID+'.ChangeTime(this.value, 2)">';
					for(var i = 0; i <60 ; i++){
						sBuffer += '<option value="'+i+'"';
						if (i == iMinut)
							sBuffer += ' selected="true"';
						sBuffer += '\>&nbsp;';
						if (i<10)
							sBuffer += '0';
						sBuffer += i+'&nbsp;';
					}
		sBuffer += '</select>';
	}
	sBuffer += '<td>';
	sBuffer += '</tr>';
	return sBuffer;
}
/*****************************************************************/
function AMGCalendarSetTimeout(__bWhich){
	//if (!__bWhich && this.TimeOutID ){
	//	clearTimeout(this.TimeOutID);
	//	this.TimeOutID = null;
	//}
	//if (__bWhich)
	//	this.TimeOutID = setTimeout(this.ID+'.OpenClose('+this.ID+')',this.ShowDelay)	
}
/*****************************************************************/
function AMGCalendarHide(){
	hide(this.Container);
}
/*****************************************************************/
function AMGCalendarShowHide(__iZIndexOn, __iZIndexOff){
	showHide(this.Container, __iZIndexOn, __iZIndexOff);
}
/*****************************************************************/
function AMGCalendarMoveTo(__iX, __iY){
	moveTo(this.Container, __iX , __iY);
}
/*****************************************************************/
function AMGCalendarGetDay(__dDate, __bMainMonth){
	var sBuffer = '';
	var iYear	= __dDate.getFullYear();
	var iMonth	= __dDate.getMonth();
	var iDay	= __dDate.getDate();
	sBuffer += '<td id="'+ this.Prefix +'_Day_'+formatCustomDate(new Date(iYear, iMonth, iDay) , 'yyyy_mm_dd')+'" style="width:'+this.DayWidth+'px;height:'+this.DayWidth+'px;" title="'+ formatCustomDate(new Date(iYear, iMonth, iDay) , 'dddd dd mmmm yyyy')+ '" class="'+this.CssPrefix+'DayMonth';
	if (IsDateSameDay(new Date(iYear, iMonth, iDay),this.TodayDate)){
		sBuffer += 'Today';
	}
	if (!__bMainMonth)
		sBuffer += 'Other';
	
	if (IsDateSameDay(new Date(iYear, iMonth, iDay),this.SelectedDate)){
		sBuffer += 'Sel';
	}
	var iThisDay = Date.parse(__dDate);
	if (IsDateBigger(__dDate, this.StartRangeDate.Add('d', -1)) && IsDateBigger(this.EndRangeDate, __dDate) ){
		sBuffer += '" onclick="'+ this.ID +'.SelectDate('+iYear+','+iMonth+','+iDay+')"';
	}
	else{
		sBuffer += 'Locked" onclick="customMsgBox(\''+this.AryCommonVoc[0]+'\', vbOK+vbCritical , \'Calendar\')"';// style="background-color:#BEBDBD"';
	}
	sBuffer += ' onmouseover="'+ this.ID +'.SetTimeout(false);" onmouseout="'+ this.ID +'.SetTimeout(true);"';
	sBuffer += '>'+iDay+'</td>';
	return sBuffer;
}
/*****************************************************************/
function AMGCalendarGetHtmlNav(){
		var sBuffer		= '';
		var iThisYear	= this.StartRangeDate.getFullYear();
		var iToYear		=  this.EndRangeDate.getFullYear();
		var bSelected	= false;
		sBuffer += '<table style="width:'+this.Width+';" cellpadding="0" cellspacing="0" border="0" class="calendarHeader">';
		sBuffer += '<tr>';
		sBuffer += '<td style="width:'+this.DayWidth+';" class="'+this.CssPrefix+'HeaderNext" onmouseover="'+ this.ID +'.SetTimeout(false);" onmouseout="'+ this.ID +'.SetTimeout(true);"><a href="javascript:'+this.ID+'.ChangeMonth(-12)">&lt;&lt;</a></td>';
		sBuffer += '<td style="width:'+(this.DayWidth/2)+';" class="'+this.CssPrefix+'HeaderNext" onmouseover="'+ this.ID +'.SetTimeout(false);" onmouseout="'+ this.ID +'.SetTimeout(true);"><a href="javascript:'+this.ID+'.ChangeMonth(-1)">&lt;</a></td>';
		sBuffer += '<td style="width:'+(this.Width-(3*this.DayWidth))+';" class="'+this.CssPrefix+'Selector" onmouseover="'+ this.ID +'.SetTimeout(false);" onmouseout="'+ this.ID +'.SetTimeout(true);">';
		
		sBuffer += '<table cellpadding="0" cellspacing="0" border="1" width="100%">';
		sBuffer += '<tr>';
		sBuffer += '<td style="width:100%;">' + formatCustomDate(this.CurrentMonth, 'mmmm') + '</td>';
		sBuffer += '<td class="'+this.CssPrefix+'HeaderSelectYear">';
		sBuffer += '<select class="calendarYearSelect" style="'+this.GetScrollStyle()+'" onMouseOver="'+this.ID+'.SetTimeout(false);" onMouseOut="'+this.ID+'.SetTimeout(true);" onchange="'+this.ID+'.ChangeYear(this.value)">';
					for(var i = iThisYear; i <= iToYear ; i++){
						sBuffer += '<option value="'+i+'"';
						if (i == this.CurrentMonth.getFullYear()){
							sBuffer += ' selected="true"';
							bSelected = true;
						}
						sBuffer += '\>&nbsp;'+i+'&nbsp;';
					}
					if (!bSelected){
						sBuffer += '<option value="'+this.CurrentMonth.getFullYear()+'" selected="true">&nbsp;'+this.CurrentMonth.getFullYear();
					}
		sBuffer += '</select></td></tr></table>';
		sBuffer += '</td>';
		sBuffer += '<td class="'+this.CssPrefix+'HeaderNext" style="width:'+(this.DayWidth/2)+';" onmouseover="'+ this.ID +'.SetTimeout(false);" onmouseout="'+ this.ID +'.SetTimeout(true);"><a href="javascript:'+this.ID+'.ChangeMonth(1)">&gt;</a></td>';
		sBuffer += '<td class="'+this.CssPrefix+'HeaderNext" style="width:'+this.DayWidth+';" onmouseover="'+ this.ID +'.SetTimeout(false);" onmouseout="'+ this.ID +'.SetTimeout(true);"><a href="javascript:'+this.ID+'.ChangeMonth(12)">&gt;&gt;</a></td>';
		sBuffer += '</tr>';
		sBuffer += '</table>';
		return sBuffer;
}
/*****************************************************************/
function AMGCalendarGetHtmlHeader(){
	var iDayTemp = this.FirstDayOfWeek;
	var sBuffer = '';
	sBuffer		+= '<tr>';
	for (var i = 0; i < 7 ; i++){
		if (iDayTemp > 6)iDayTemp = 0;
		sBuffer += '<td style="width:'+this.DayWidth+'px;" class="'+this.CssPrefix+'DayHeader"';
		sBuffer += ' onmouseover="'+ this.ID +'.SetTimeout(false);" onmouseout="'+ this.ID +'.SetTimeout(true);">'+this.AryWeekDayName[iDayTemp].split("|")[1]+'</td>';
		iDayTemp++;
	}
	sBuffer		+= '</tr>';
	this.WeekHeader = sBuffer;
}
