/*****************************\
* ITALCOM S.p.A.              *
* Browser: IE 5.5             *
* Library: CDateTime.js       *
* Version: 1.0                *
* Require: Core.js            *
*          CWindows.js        *
* Objects: CDateTime          *
*          DateTime functions *
\*****************************/


//===========
// CDateTime
//===========
function CDateTime(id, type)
{
	if (IsEmpty(type)) type="date";
	
	this.id = id;
	this.cls = "CDateTime";
	this.type = type; // "date", "time", "datetime"
	this.lang = "IT";
	
	this.year = "";
	this.month = "";
	this.day = "";
	this.hour = "";
	this.min = "";
	this.sec = "";
	
	this.defTime = "";
	this.disabled = false;	
}

// Dialogo di default per pickdate() percorso relativo a web
CDateTime.prototype.dlgCalendar = "/KnoS/system/env/lib/dlg/DlgCalendar.asp";

CDateTime.prototype.HTML
=//=====================
function ()
{
	var sDay = "giorno";
	var sMonth = "mese";
	var sYear = "anno";
	var sHours = "ore";
	var sMinutes = "minuti";
	var sSeconds = "secondi";
	if (this.lang.toUpperCase() != "IT")
	{
		this.lang = "GB";
		sDay = "day";
		sMonth = "month";
		sYear = "year";
		sHours = "hours";
		sMinutes = "minutes";
		sSeconds = "seconds";
	}
		
	var HTML = '<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 CLASS="'+ this.cls +'-clsTABLE" ID="'+ this.id +'_idTABLE"><TR>';
	var disabled = this.disabled ? "DISABLED" : "";
	if (this.type == "date" || this.type == "datetime")
	{
		HTML += '\n<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-day" NOWRAP>';
		HTML += '<INPUT TYPE="text" CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsINPUT" ID="'+ this.id +'_idINPUT_day" VALUE="'+ this.day +'" TITLE="'+ sDay +'" MAXLENGTH=2 '+ disabled +'></TD>';
		HTML += '<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-datesep" NOWRAP>/</TD>';
		HTML += '\n<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-month" NOWRAP>';
		HTML += '<INPUT TYPE="text" CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsINPUT" ID="'+ this.id +'_idINPUT_month" VALUE="'+ this.month +'" TITLE="'+ sMonth +'" MAXLENGTH=2 '+ disabled +'></TD>';
		HTML += '<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-datesep" NOWRAP>/</TD>';
		HTML += '\n<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-year" NOWRAP>';
		HTML += '<INPUT TYPE="text" CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsINPUT" ID="'+ this.id +'_idINPUT_year" VALUE="'+ this.year +'" TITLE="'+ sYear +'" MAXLENGTH=4 '+ disabled +'></TD>';
		if (IE)
		{
			HTML += '<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-picksep" NOWRAP>&nbsp;</TD>';
			HTML += '<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-pickdate" NOWRAP>';
			HTML += '<INPUT TYPE=button CLASS="clsINPUT-ellipses" ID="'+ this.id +'_idBUTTON_pickdate" HIDEFOCUS VALUE="..." '+ disabled +' onclick="'+ this.id + '.bindCalendar();">';
			HTML += '<INPUT type=button CLASS="clsINPUT-cancel" HIDEFOCUS VALUE="X" onclick="'+ this.id +'.cancel();"></TD>';
		}
	}
	if (this.type == "datetime")
		HTML += '<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-datetimesep" NOWRAP>&nbsp;</TD>';
	if (this.type == "time" || this.type == "datetime")
	{
		HTML += '\n<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-hour" NOWRAP>';
		HTML += '<INPUT TYPE="text" CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsINPUT" ID="'+ this.id +'_idINPUT_hour" VALUE="'+ this.hour +'" TITLE="'+ sHours +'" MAXLENGTH=2 '+ disabled +'></TD>';
		HTML += '<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-timesep" NOWRAP>:</TD>';
		HTML += '\n<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-min" NOWRAP>';
		HTML += '<INPUT TYPE="text" CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsINPUT" ID="'+ this.id +'_idINPUT_min" VALUE="'+ this.min +'" TITLE="'+ sMinutes +'" MAXLENGTH=2 '+ disabled +'></TD>';
		HTML += '<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-timesep" NOWRAP>:</TD>';
		HTML += '\n<TD CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsTD-sec" NOWRAP>';
		HTML += '<INPUT TYPE="text" CLASS="'+ this.cls +'-clsFont '+ this.cls +'-clsINPUT" ID="'+ this.id +'_idINPUT_sec" VALUE="'+ this.sec +'" TITLE="'+ sSeconds +'" MAXLENGTH=2 '+ disabled +'></TD>';
	}	
	HTML += '</TR></TABLE>';
	return HTML;
}

CDateTime.prototype.writeHTML
=//==========================
function () { document.write(this.HTML()); }

CDateTime.prototype.refresh
=//========================
function () { document.all[this.id +"_idTABLE"].outerHTML = this.HTML(); }

CDateTime.prototype.showCalendar
=//=============================
function (day, month, year)
{
	var arg = new Object();
	arg.day = day;
	arg.month = month;
	arg.year = year;
	if (window.showModalDialog(this.dlgCalendar +"?lang="+ this.lang, arg, "directories:no;location:no;menubar:no;resizable:no;dialogWidth:245px;dialogHeight:242px;status:no;help:no;"))
	{
		this.day = arg.day;
		this.month = arg.month;
		this.year = arg.year;
		return true;
	}
	else
		return false;
}

CDateTime.prototype.bindCalendar
=//=============================
function (idDay, idMonth, idYear)
{
	if (arguments.length==0)
	{
		idDay = this.id+"_idINPUT_day";
		idMonth = this.id+"_idINPUT_month";
		idYear = this.id+"_idINPUT_year";
	}
	idDay = document.getElementById(idDay);
	idMonth = document.getElementById(idMonth);
	idYear = document.getElementById(idYear);
	if (IsEmpty(idDay) || IsEmpty(idMonth) || IsEmpty(idYear))
		return;
	if (this.showCalendar(idDay.value, idMonth.value, idYear.value))
	{
		if (this.disabled)
			return;
		idDay.value = (this.day != "" && this.day<10) ? "0"+this.day : this.day;
		idMonth.value = (this.month != "" && this.month<10) ? "0"+this.month : this.month;
		idYear.value = this.year;
	}
}

CDateTime.prototype.cancel
=//=======================
function () { this.setDate("", "", ""); this.setTime("", "", "") }


CDateTime.prototype.setDate
=//========================
function (y, m, d)
{ 
	if (arguments.length == 3) 
	{ 
		this.year = y;
		this.month = m;
		this.day = d;
	}
	try
	{ 
		document.getElementById(this.id+"_idINPUT_day").value = this.day; 
		document.getElementById(this.id+"_idINPUT_month").value = this.month; 
		document.getElementById(this.id+"_idINPUT_year").value = this.year; 
	}
	catch(e){} 
}

CDateTime.prototype.setTime
=//========================
function (h, m, s)
{ 
	if (arguments.length == 3) 
	{ 
		this.hour = h;
		this.min = m;
		this.sec = s;
	}
	try
	{ 
		document.getElementById(this.id+"_idINPUT_hour").value = this.hour; 
		document.getElementById(this.id+"_idINPUT_min").value = this.min; 
		document.getElementById(this.id+"_idINPUT_sec").value = this.sec; 
	}
	catch(e){} 
}

CDateTime.prototype.setDateTime
=//============================
function (year, month, day, hour, min, sec) 
{	
	if (arguments.length >= 3)
		this.setDate(year, month, sec);
	else
		this.setDate();
	if (arguments.length == 6)
		this.setTime(hour, min, sec);
	else
		this.setTime();
}

CDateTime.prototype.setStdDateTime
=//===============================
function (strDateTime) { StdDateTimeObject(strDateTime, this); this.setDateTime(); }

CDateTime.prototype.getStdDateTimeString
=//=====================================
// DateTime con anno sempre a 4 cifre:
//	"" se la data e' in bianco
//	null se la data e' errata o qualche campo e' in bianco
//  data nel formato YYYY-MM-GG hh:mm:ss se non sono rilevati errori 
//
// alert e focus in caso di errore e campo obbligatorio opzionali
//	
function (bAlert, bOptional)
{
	if (IsEmpty(bAlert)) bAlert=true;
	if (IsEmpty(bOptional)) bOptional=true;

	var tmp, iDay, iMonth, iYear, iHour, iMin, iSec;
	var date = "";
	if (this.type == "date" || this.type == "datetime")
	{
		iDay = document.getElementById(this.id+"_idINPUT_day");
		iMonth = document.getElementById(this.id+"_idINPUT_month");
		iYear = document.getElementById(this.id+"_idINPUT_year");
		this.day = parseInt(iDay.value, 10);
		this.month = parseInt(iMonth.value, 10);
		this.year = parseInt(iYear.value, 10);
		if (isNaN(this.day) && isNaN(this.month) && isNaN(this.year))
		{
			if (!bOptional)
			{
				window.alert(this.lang=="IT" ? "Data obbligatoria" : "Date is mandatory");
				iDay.focus();
				return null;
			}
			date = "";
		}
		else if (isNaN(this.day) || isNaN(this.month) || isNaN(this.year))
		{
			window.alert(this.lang=="IT" ? "Data incompleta" : "Partial date")
			if (isNaN(this.year)) tmp = iYear;
			if (isNaN(this.month)) tmp = iMonth;
			if (isNaN(this.day)) tmp = iDay;
			tmp.value = "";
			tmp.focus();
			return null;
		}
		else
		{
			if (iYear.value.length < 4)
			{
				window.alert(this.lang=="IT" ? "Inserire l'anno nel formato a quattro cifre" : "Insert the year in the century form YYYY");
				iYear.focus();
				return null;
			}
			tmp = IsInvalidDate(this.day, this.month, this.year);
			switch (tmp)
			{
				case 1: iDay.focus(); break;
				case 2:	iMonth.focus();	break;
				case 3:	iYear.focus(); break;
			}
			if (tmp)
			{
				window.alert(this.lang=="IT" ? "Data non valida" : "Invalid date");
				return null;
			}
			date = StdDateString(this.year, this.month, this.day);
		}
	}
	var time = "";
	if (this.type == "time" || this.type == "datetime")
	{
		iHour = document.getElementById(this.id+"_idINPUT_hour");
		iMin = document.getElementById(this.id+"_idINPUT_min");
		iSec = document.getElementById(this.id+"_idINPUT_sec");
		this.hour = parseInt(iHour.value, 10);
		this.min = parseInt(iMin.value, 10);
		this.sec = parseInt(iSec.value, 10);
		if (isNaN(this.hour) && isNaN(this.min) && isNaN(this.sec))
		{
			if (!bOptional)
			{
				window.alert(this.lang=="IT" ? "Ora obbligatoria" : "Time is mandatory");
				iHour.focus();
				return null;
			}
			time = "";
		}
		else if (isNaN(this.hour) || isNaN(this.min) || isNaN(this.sec))
		{
			window.alert(this.lang=="IT" ? "Ora incompleta" : "Partial time")
			if (isNaN(this.sec)) tmp = iSec;
			if (isNaN(this.min)) tmp = iMin;
			if (isNaN(this.hour)) tmp = iHour;
			tmp.value = "";
			tmp.focus();
			return null;
		}
		else
		{
			tmp = IsInvalidTime(this.hour, this.min, this.sec);
			switch (tmp)
			{
				case 1: iHour.focus(); break;
				case 2:	iMin.focus(); break;
				case 3:	iSec.focus(); break;
			}
			if (tmp)
			{
				window.alert(this.lang=="IT" ? "Ora non valida" : "Invalid time");
				return null;
			}
			time = StdTimeString(this.hour, this.min, this.sec);
		}
	}
	switch(this.type)
	{
		case "date":
			if (!IsEmpty(date) && !IsEmpty(this.defTime))
				time = this.defTime;
			return Trim(date +" "+ time);
		case "time":
			return time;
		case "datetime": 
			if (date == "" && time != "")
			{
				window.alert(this.lang=="IT" ? "Se l'ora è indicata la data è obbligatoria" : "If time is placed date is mandatory");
				iDay.focus();
				return null;
			}
			if (date != "" && time == "")
				time = this.defTime;
			return Trim(date +" "+ time);
	}
}


//====================
// DateTime functions
//====================

function MonthLength(m, y) 
{
	switch (parseInt(m))
	{
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
		case 2:
			return (!(y % 400) || (!(y % 4) && (y % 100))) ? 29 : 28;
		default:
			return 31;
	}
}

// Controllo sulla data: 
//	0 se la data è corretta
//	1, 2, 3 se l'errore è su giorno, mese o anno
//
function IsInvalidDate(d, m, y)
{
	d = parseInt(d, 10); if (isNaN(d)) return 1;
	m = parseInt(m, 10); if (isNaN(m)) return 2;
	y = parseInt(y, 10); if (isNaN(y)) return 3;
	if (y < 0) return 3;
	if (m < 0 || m > 12) return 2;
	if (d < 1 || d > MonthLength(m, y)) return 1;
	return 0;
}

// Controllo sull'ora con h, m, s, ms (millisecondi opzionali):
//	0 se l'ora e' corretta
//	1, 2, 3, 4 se l'errore è su ora, minuti, secondi o millisecondi
//
function IsInvalidTime(h, m, s, ms)
{
	if (arguments.length<4) ms = 0;
	h = parseInt(h, 10); if (isNaN(h)) return 1;
	m = parseInt(m, 10); if (isNaN(m)) return 2;
	s = parseInt(s, 10); if (isNaN(s)) return 3;
	ms = parseInt(ms, 10); if (isNaN(ms)) return 4;
	if ( h < 0 || h > 23 ) return 1;
	if ( m < 0 || m > 59 ) return 2;
	if ( s < 0 || s > 59 ) return 3;
	if ( ms< 0 || ms>999 ) return 4;
	return 0;
}

function StdDateString(y, m, d) { y = "0000"+ y; m = "00"+ m; d = "00"+ d; return y.slice(-4) +"-"+ m.slice(-2) +"-"+ d.slice(-2); }
function StdTimeString(h, m, s) { h = "00"+ h; m = "00"+ m;	s = "00"+ s; return h.slice(-2) +":"+ m.slice(-2) +":"+ s.slice(-2); }
function StdDateTimeString(year, month, day, hour, min, sec) { if (IsEmpty(hour)) hour = 0; if (IsEmpty(min)) min = 0; if (IsEmpty(sec)) sec = 0; return StdDateString(year, month, day) +" "+ StdTimeString(hour, min, sec) }
Date.prototype.StdDateString = function () { return StdDateString(this.getFullYear(), 1+this.getMonth(), this.getDate()) };
Date.prototype.StdTimeString = function () { return StdTimeString(this.getHours(), this.getMinutes(), this.getSeconds()) };
Date.prototype.StdDateTimeString = function () { return StdDateTimeString(this.getFullYear(), 1+this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds()) };
function StdDateTimeObject(stdDateTime_Date, object)
{
	var d = stdDateTime_Date;
	if (IsEmpty(object)) object = new Object();
	if (IsEmpty(d)) 
	{
		object.year = "";
		object.month = "";
		object.day = "";
		object.hour = "";
		object.min = "";
		object.sec = "";
	}
	else
	{		
		if (IsEmpty(d.getDate))
		{
			object.year = d.slice(0,4);
			object.month = d.slice(5,7);
			object.day = d.slice(8,10);
			object.hour = d.slice(11,13);
			object.min = d.slice(14,16);
			object.sec = d.slice(17,19);
		}
		else
		{
			var d = new Date();
			object.year = d.getFullYear();
			object.month = 1+d.getMonth();
			object.day = d.getDate();
			object.hour = d.getHours();
			object.min = d.getMinutes();
			object.sec = d.getSeconds();
		}
	}
	return object;
}

function BindCalendar(idBase, dlgCalendar)
{
	var idDay = document.getElementById(idBase+"_day");
	idMonth = document.getElementById(idBase+"_month");
	idYear = document.getElementById(idBase+"_year");
	if (IsEmpty(idDay) || IsEmpty(idMonth) || IsEmpty(idYear))
		return;
	
	var arg = new Object();
	arg.day = idDay.value;
	arg.month = idMonth.value;
	arg.year = idYear.value;

	if ( IsEmpty(dlgCalendar) )
		dlgCalendar = CDateTime.prototype.dlgCalendar;
	if (window.showModalDialog(dlgCalendar, arg, "directories:no;location:no;menubar:no;resizable:no;dialogWidth:245px;dialogHeight:242px;status:no;help:no;"))
	{
		idDay.value = (arg.day != "" && arg.day<10) ? "0"+arg.day : arg.day;
		idMonth.value = (arg.month != "" && arg.month<10) ? "0"+arg.month : arg.month;
		idYear.value = arg.year;
	}
}


function SearchExpressionForDateTimeRange(colName, cdt1, cdt2, bAlert)
{
	if (IsEmpty(bAlert)) bAlert = true;
	
	var tmp;
	var result = "";
	
	tmp = cdt1.defTime;
	cdt1.defTime = "00:00:00";
	var	min = cdt1.getStdDateTimeString(bAlert, true);
	cdt1.defTime = tmp;
	if ( min == null )
		return null;
		
	tmp = cdt2.defTime;
	cdt2.defTime = "23:59:59";
	var max = cdt2.getStdDateTimeString(bAlert, true);
	cdt2.defTime = tmp;
	if ( max == null )
		return null;				
	if ( min > max && max != "")
	{
		if (bAlert)
		{
			window.alert("La seconda data dell'intervallo è antecedente alla prima")
			document.getElementById(cdt2.id+"_idINPUT_day").focus();	
			return null;
		}
	}
	if ( min != "" )
		min = "(" + colName + ">='" + min + "')";
	if ( max != "" )
		max = "(" + colName + "<='" + max + "')";
	if (min == "" || max == "")
		result = min + max;
	else
		result = min + " AND " + max;
	return result;
}
