/*

JS files needed:
	- StdLibrary.js
	
Usage:
	- HTML page
		<%@ Page language="c#" Codebehind="HumanReadableDateTime.aspx.cs" AutoEventWireup="false" Inherits="DynamicLayer.HumanReadableDateTime" %>
		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 

		<html>
		<head>
			<title>HumanReadableDateTime</title>
			<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
			<meta name="CODE_LANGUAGE" Content="C#">
			<meta name=vs_defaultClientScript content="JavaScript">
			<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
			<script lang="text/javascript" src="StdLibrary.js"></script>
			<script lang="text/javascript" src="HumanReadableDateTime.js"></script>
		</head>
		<body MS_POSITIONING="GridLayout">
			
			<form id="Form1" method="post" runat="server">
				<h1><div id="DateTimeDIV"></div></h1>
					<script lang="text/javascript" src="HumanReadableDateTimeTest.js"></script>
			</form>
			
		</body>
		</html>


	- HumanReadableDateTimeTest.js
		var hrDTDiv = new HumanReadableDateTimeDIV('DateTimeDIV');
		hrDTDiv.SetDelay(1);
		hrDTDiv.GetHrDTRef().GetDateRef().GetLanguageRef().SetCurLanguage(hrDTDiv.GetHrDTRef().GetDateRef().GetLanguageRef().ROMANIAN);
		hrDTDiv.GetHrDTRef().GetTimeRef().GetTimeFormatRef().SetCurFormat(hrDTDiv.GetHrDTRef().GetTimeRef().GetTimeFormatRef().HRS_MINS_SECS_MILLS);
		hrDTDiv.Start();

*/





/******************************* HumanReadableLanguage *****************************/

// Returns the current language
function GetCurLanguage_HumanReadableLanguage()
{
	return this.curLanguage;
}

// Sets the current language
function SetCurLanguage_HumanReadableLanguage(language)
{
    var lng = parseInt(language, 10);

	if (lng < 0 || lng >= this.value)
	{
		return;
	}

	this.curLanguage = lng;
}

// Used to create a HumanReadableLanguage object
function HumanReadableLanguage()
{
	// --private
	this.value				= 0;
	this.ENGLISH			= this.value++;
	this.ROMANIAN			= this.value++;
	this.curLanguage		= this.ENGLISH;

	// --public
	this.GetCurLanguage		= GetCurLanguage_HumanReadableLanguage;
	this.SetCurLanguage		= SetCurLanguage_HumanReadableLanguage;
}

/**************************** END - HumanReadableLanguage **************************/




/****************************** HumanReadableMonthFormat ***************************/

// Returns the current month format
function GetCurFormat_HumanReadableMonthFormat()
{
	return this.curFormat;
}

// Sets the current month format
function SetCurFormat_HumanReadableMonthFormat(format)
{
    var frmt = parseInt(format, 10);

	if (frmt < 0 || frmt >= this.value)
	{
		return;
	}

	this.curFormat = frmt;
}

// Used to create a HumanReadableMonthFormat object
function HumanReadableMonthFormat()
{
	// --private
	this.value				= 0;
	this.TEXT				= this.value++;
	this.NUMBER				= this.value++;
	this.curFormat			= this.TEXT;
	
	// --public
	this.GetCurFormat		= GetCurFormat_HumanReadableMonthFormat;
	this.SetCurFormat		= SetCurFormat_HumanReadableMonthFormat;
}

/**************************** END - HumanReadableMonthFormat ***********************/




/***************************** HumanReadableDayOfWeekFormat ************************/

// Returns the current DayOfWeek format
function GetCurFormat_HumanReadableDayOfWeekFormat()
{
	return this.curFormat;
}

// Sets the current DayOfWeek format
function SetCurFormat_HumanReadableDayOfWeekFormat(format)
{
	if (format < 0 || format >= this.value)
	{
		return;
	}

	this.curFormat = format;
}

// Used to create a HumanReadableDayOfWeekFormat object
function HumanReadableDayOfWeekFormat()
{
	// --private
	this.value				= 0;
	this.NONE				= this.value++;
	this.BEFORE_DATE		= this.value++;
	this.AFTER_DATE			= this.value++;
	this.curFormat			= this.NONE;

	// --public
	this.GetCurFormat		= GetCurFormat_HumanReadableDayOfWeekFormat;
	this.SetCurFormat		= SetCurFormat_HumanReadableDayOfWeekFormat;
}

/**************************** END - HumanReadableDayOfWeekFormat ***********************/




/********************************** HumanReadableDateFormat ****************************/

// Returns the current date format
function GetCurFormat_HumanReadableDateFormat()
{
	return this.curFormat;
}

// Sets the current date format
function SetCurFormat_HumanReadableDateFormat(format)
{
	if(format < 0 || format >= this.value)
	{
		return;
	}

	this.curFormat = format;
}

// Returns the current separator
function GetSeparator_HumanReadableDateFormat()
{
	return this.separator;
}

// Sets the current separator
function SetSeparator_HumanReadableDateFormat(separator)
{
	if (null == separator || 0 == separator.length)
	{
		return;
	}

	this.separator = separator;
}

// Returns the reference to the monthFormat member
function GetMonthFormatRef_HumanReadableDateFormat()
{
	return this.monthFormat;
}

// Returns the reference to the dayOfWeekFormat member
function GetDayOfWeekFormatRef_HumanReadableDateFormat()
{
	return this.dayOfWeekFormat;
}

// Used to create a HumanReadableDateFormat object
function HumanReadableDateFormat()
{
	// --private
	this.value					= 0;
	this.YEAR_MONTH_DAY			= this.value++;
	this.YEAR_DAY_MONTH			= this.value++;
	this.MONTH_YEAR_DAY			= this.value++;
	this.MONTH_DAY_YEAR			= this.value++;
	this.DAY_YEAR_MONTH			= this.value++;
	this.DAY_MONTH_YEAR			= this.value++;

	this.separator				= ', ';
	this.monthFormat			= new HumanReadableMonthFormat();
	this.dayOfWeekFormat		= new HumanReadableDayOfWeekFormat();

	
	// --public
	this.GetCurFormat			= GetCurFormat_HumanReadableDateFormat;
	this.SetCurFormat			= SetCurFormat_HumanReadableDateFormat;
	this.GetSeparator			= GetSeparator_HumanReadableDateFormat;
	this.SetSeparator			= SetSeparator_HumanReadableDateFormat;
	this.GetMonthFormatRef		= GetMonthFormatRef_HumanReadableDateFormat;
	this.GetDayOfWeekFormatRef	= GetDayOfWeekFormatRef_HumanReadableDateFormat;
}

/*************************** END - HumanReadableDateFormat *************************/



/********************************* HumanReadableDate *******************************/

// Called to init the object
function OnInit_HumanReadableDate()
{
	/************** MONTH array **************/

	// ENGLISH
	for ( var i = 0; i < 12; ++i )
	{
		this.monthArray[this.language.ENGLISH]		= new Object();
	}
	this.monthArray[this.language.ENGLISH][0]		= 'January';
	this.monthArray[this.language.ENGLISH][1]		= 'February';
	this.monthArray[this.language.ENGLISH][2]		= 'Mars';
	this.monthArray[this.language.ENGLISH][3]		= 'April';
	this.monthArray[this.language.ENGLISH][4]		= 'May';
	this.monthArray[this.language.ENGLISH][5]		= 'June';
	this.monthArray[this.language.ENGLISH][6]		= 'July';
	this.monthArray[this.language.ENGLISH][7]		= 'August';
	this.monthArray[this.language.ENGLISH][8]		= 'September';
	this.monthArray[this.language.ENGLISH][9]		= 'October';
	this.monthArray[this.language.ENGLISH][10]		= 'November';
	this.monthArray[this.language.ENGLISH][11]		= 'December';

	// ROMANIAN
	for ( var i = 0; i < 12; ++i )
	{
		this.monthArray[this.language.ROMANIAN]		= new Object();
	}
	this.monthArray[this.language.ROMANIAN][0]		= 'Ianuarie';
	this.monthArray[this.language.ROMANIAN][1]		= 'Februarie';
	this.monthArray[this.language.ROMANIAN][2]		= 'Martie';
	this.monthArray[this.language.ROMANIAN][3]		= 'Aprilie';
	this.monthArray[this.language.ROMANIAN][4]		= 'Mai';
	this.monthArray[this.language.ROMANIAN][5]		= 'Iunie';
	this.monthArray[this.language.ROMANIAN][6]		= 'Iulie';
	this.monthArray[this.language.ROMANIAN][7]		= 'August';
	this.monthArray[this.language.ROMANIAN][8]		= 'Septembrie';
	this.monthArray[this.language.ROMANIAN][9]		= 'Octombrie';
	this.monthArray[this.language.ROMANIAN][10]		= 'Noiembrie';
	this.monthArray[this.language.ROMANIAN][11]		= 'Decembrie';

	/*************** END - MONTH array ****************/


	/************* DAY of the week array **************/
	
	// ENGLISH
	for ( var i = 0; i < 7; ++i )
	{
		this.dayArray[this.language.ENGLISH]		= new Object();
	}
	this.dayArray[this.language.ENGLISH][0]			= 'Sunday';
	this.dayArray[this.language.ENGLISH][1]			= 'Monday';
	this.dayArray[this.language.ENGLISH][2]			= 'Tuesday';
	this.dayArray[this.language.ENGLISH][3]			= 'Wednesday';
	this.dayArray[this.language.ENGLISH][4]			= 'Thursday';
	this.dayArray[this.language.ENGLISH][5]			= 'Friday';
	this.dayArray[this.language.ENGLISH][6]			= 'Saturday';

	// ROMANIAN
	for ( var i = 0; i < 7; ++i )
	{
		this.dayArray[this.language.ROMANIAN]		= new Object();
	}
	this.dayArray[this.language.ROMANIAN][0]		= 'Duminica';
	this.dayArray[this.language.ROMANIAN][1]		= 'Luni';
	this.dayArray[this.language.ROMANIAN][2]		= 'Marti';
	this.dayArray[this.language.ROMANIAN][3]		= 'Miercuri';
	this.dayArray[this.language.ROMANIAN][4]		= 'Joi';
	this.dayArray[this.language.ROMANIAN][5]		= 'Vineri';
	this.dayArray[this.language.ROMANIAN][6]		= 'Sambata';

	/*********** END - DAY of the week array ************/
}

// Return a reference to the language member
function GetLanguageRef_HumanReadableDate()
{
	return this.language;
}

// Return a reference to the dateFormat member
function GetDateFormatRef_HumanReadableDate()
{
	return this.dateFormat;
}

// Returns the curent date in a human readable format
function GetDate_HumanReadableDate()
{
	var date = new Date();

	return this.GetDateEx(date);
}

// Returns the date given as a parameter in a human readable format
function GetDateEx_HumanReadableDate(date)
{
	var str = '';
	var dayOfWeek = this.dayArray[this.language.GetCurLanguage()][date.getDay()];
	var separator = this.dateFormat.GetSeparator();
	var year = date.getFullYear();
	var month = '';
	var day = date.getDate();
	
	if ( this.dateFormat.GetDayOfWeekFormatRef().GetCurFormat() == this.dateFormat.GetDayOfWeekFormatRef().BEFORE_DATE )
	{
		str += dayOfWeek;
		str += separator;
	}

	switch ( this.dateFormat.GetMonthFormatRef().GetCurFormat() )
	{
		default:
		case this.dateFormat.GetMonthFormatRef().TEXT:
		{
			month = this.monthArray[this.language.GetCurLanguage()][date.getMonth()];
			break;
		}
		case this.dateFormat.GetMonthFormatRef().NUMBER:
		{
			month = date.getMonth();
			break;
		}
	}
	
	switch ( this.dateFormat.GetCurFormat() )
	{
		default:
		case this.dateFormat.YEAR_MONTH_DAY:
		{
			str += year + separator + month + separator + day;
			break;
		}
		case this.dateFormat.YEAR_DAY_MONTH:
		{
			str += year + separator + day + separator + month;
			break;
		}
		case this.dateFormat.MONTH_YEAR_DAY:
		{
			str += month + separator + year + separator + day;
			break;
		}
		case this.dateFormat.MONTH_DAY_YEAR:
		{
			str += month + separator + day + separator + year;
			break;
		}
		case this.dateFormat.DAY_YEAR_MONTH:
		{
			str += day + separator + year + separator + month;
			break;
		}
		case this.dateFormat.DAY_MONTH_YEAR:
		{
			str += day + separator + month + separator + year;
			break;
		}
	}
	
	if ( this.dateFormat.GetDayOfWeekFormatRef().GetCurFormat() == this.dateFormat.GetDayOfWeekFormatRef().AFTER_DATE )
	{
		str += separator;
		str += dayOfWeek;
	}
	
	return str;
}


// Used to create a HumanReadableDate object
function HumanReadableDate()
{
	// --private
	this.language				= new HumanReadableLanguage();
	this.dateFormat				= new HumanReadableDateFormat();
	this.monthArray				= new Object();
	this.dayArray				= new Object();

	
	// --public
	this.GetLanguageRef			= GetLanguageRef_HumanReadableDate;
	this.GetDateFormatRef		= GetDateFormatRef_HumanReadableDate;
	this.GetDateEx				= GetDateEx_HumanReadableDate;
	this.GetDate				= GetDate_HumanReadableDate;
	
	// --private
	this.OnInit					= OnInit_HumanReadableDate;
	
	
	this.OnInit();
}

/**************************** END - HumanReadableDate ********************************/




/***************************** HumanReadableTimeFormat *******************************/

// Returns the current Time format
function GetCurFormat_HumanReadableTimeFormat()
{
	return this.curFormat;
}

// Sets the current Time format
function SetCurFormat_HumanReadableTimeFormat(format)
{
	if (format < 0 || format >= this.value)
	{
		return;
	}

	this.curFormat = format;
}

// Returns the current separator
function GetSeparator_HumanReadableTimeFormat()
{
	return this.separator;
}

// Sets the current separator
function SetSeparator_HumanReadableTimeFormat(separator)
{
    if (null == separator || 0 == separator.length)
	{
		return;
	}

	this.separator = separator;
}

// Used to create a HumanReadableTimeFormat object
function HumanReadableTimeFormat()
{
	// --private
	this.value					= 0;
	this.HRS					= this.value++;
	this.HRS_MINS				= this.value++;
	this.HRS_MINS_SECS			= this.value++;
	this.HRS_MINS_SECS_MILLS	= this.value++;
	this.curFormat				= this.HRS_MINS;
	this.separator				= ':';

	// --public
	this.GetCurFormat			= GetCurFormat_HumanReadableTimeFormat;
	this.SetCurFormat			= SetCurFormat_HumanReadableTimeFormat;
	this.GetSeparator			= GetSeparator_HumanReadableTimeFormat;
	this.SetSeparator			= SetSeparator_HumanReadableTimeFormat;
}

/**************************** END - HumanReadableTimeFormat ***********************/




/********************************* HumanReadableTime *******************************/

// Return a reference to the timeFormat member
function GetTimeFormatRef_HumanReadableTime()
{
	return this.timeFormat;
}

// Returns the curent time in a human readable format
function GetTime_HumanReadableTime()
{
	var time = new Date();

	return this.GetTimeEx(time);
}

// Returns the time given as a parameter in a human readable format
function GetTimeEx_HumanReadableTime(time)
{
	var str = '';
	var separator = this.timeFormat.GetSeparator();
	var hrs = time.getHours();
	var mins = time.getMinutes();
	var secs = time.getSeconds();
	var mills = time.getMilliseconds();
	
	switch ( this.timeFormat.GetCurFormat() )
	{
		case this.timeFormat.HRS:
		{
			str += hrs;
			break;
		}
		case this.timeFormat.HRS_MINS:
		{
			str += hrs;
			str += separator;
			str += mins;
			break;
		}
		case this.timeFormat.HRS_MINS_SECS:
		{
			str += hrs;
			str += separator;
			str += mins;
			str += separator;
			str += secs;
			break;
		}
		case this.timeFormat.HRS_MINS_SECS_MILLS:
		{
			str += hrs;
			str += separator;
			str += mins;
			str += separator;
			str += secs;
			str += separator;
			str += mills;
			break;
		}
	}
	
	return str;
}


// Used to create a HumanReadableTime object
function HumanReadableTime()
{
	// --private
	this.timeFormat				= new HumanReadableTimeFormat();
	
	// --public
	this.GetTimeFormatRef		= GetTimeFormatRef_HumanReadableTime;
	this.GetTimeEx				= GetTimeEx_HumanReadableTime;
	this.GetTime				= GetTime_HumanReadableTime;
}

/**************************** END - HumanReadableTime ********************************/




/************************** HumanReadableDateTimeFormat ******************************/

// Returns the current DateTime format
function GetCurFormat_HumanReadableDateTimeFormat()
{
	return this.curFormat;
}

// Sets the current DateTime format
function SetCurFormat_HumanReadableDateTimeFormat(format)
{
	if (format < 0 || format >= this.value)
	{
		return;
	}

	this.curFormat = format;
}

// Returns the current separator
function GetSeparator_HumanReadableDateTimeFormat()
{
	return this.separator;
}

// Sets the current separator
function SetSeparator_HumanReadableDateTimeFormat(separator)
{
	if (null == separator || 0 == separator.length)
	{
		return;
	}

	this.separator = separator;
}

// Used to create a HumanReadableDateTimeFormat object
function HumanReadableDateTimeFormat()
{
	// --private
	this.value					= 0;
	this.DATE					= this.value++;
	this.TIME					= this.value++;
	this.DATE_TIME				= this.value++;
	this.TIME_DATE				= this.value++;
	this.curFormat				= this.DATE_TIME;
	this.separator				= '&nbsp;&nbsp;&nbsp;';

	// --public
	this.GetCurFormat			= GetCurFormat_HumanReadableDateTimeFormat;
	this.SetCurFormat			= SetCurFormat_HumanReadableDateTimeFormat;
	this.GetSeparator			= GetSeparator_HumanReadableDateTimeFormat;
	this.SetSeparator			= SetSeparator_HumanReadableDateTimeFormat;
}

/*********************** END - HumanReadableDateTimeFormat ***************************/




/****************************** HumanReadableDateTime ********************************/

// Returns a reference to the dateTimeFormat member object
function GetDateTimeFormatRef_HumanReadableDateTime()
{
	return this.dateTimeFormat;
}

// Returns a reference to the date member object
function GetDateRef_HumanReadableDateTime()
{
	return this.date;
}

// Returns a reference to the time member object
function GetTimeRef_HumanReadableDateTime()
{
	return this.time;
}

// Returns the datetime given as a parameter in a human readable format
function GetDateTimeEx_HumanReadableDateTime(dateTime)
{
	var str = '';
	var separator = this.dateTimeFormat.GetSeparator();
	var date = this.date.GetDateEx(dateTime);
	var time = this.time.GetTimeEx(dateTime);
	
	switch ( this.dateTimeFormat.GetCurFormat() )
	{
		case this.dateTimeFormat.DATE:
		{
			str = date;	
			break;
		}
		case this.dateTimeFormat.TIME:
		{
			str = time;
			break;
		}
		case this.dateTimeFormat.DATE_TIME:
		{
			str = date + separator + time;
			break;
		}
		case this.dateTimeFormat.TIME_DATE:
		{
			str = time + separator + date;
			break;
		}
	}
	
	return str;
}

// Returns the current datetime in a human readable format
function GetDateTime_HumanReadableDateTime()
{
	var dateTime = new Date();
	
	return this.GetDateTimeEx(dateTime);
}


// Used to create a HumanReadableDateTime object
function HumanReadableDateTime()
{
	// --private
	this.dateTimeFormat			= new HumanReadableDateTimeFormat();
	this.date					= new HumanReadableDate();
	this.time					= new HumanReadableTime();

	// --public
	this.GetDateTimeFormatRef	= GetDateTimeFormatRef_HumanReadableDateTime;
	this.GetDateRef				= GetDateRef_HumanReadableDateTime;
	this.GetTimeRef				= GetTimeRef_HumanReadableDateTime;
	this.GetDateTimeEx			= GetDateTimeEx_HumanReadableDateTime;
	this.GetDateTime			= GetDateTime_HumanReadableDateTime;
}

/*************************** END - HumanReadableDateTime *****************************/




/***************************** HumanReadableDateTimeDIV ******************************/

// Called to initialize the object
function OnInit_HumanReadableDateTimeDIV(divID)
{
    if (null == divID || 0 == divID.length)
	{
		return;
	}
	
	this.divElem = document.getElementById(divID);
}

// Return the reference to the divElem member object
function GetDivElemRef_HumanReadableDateTimeDIV()
{
	return this.divElem;
}

// Return the delay between loops
function GetDelay_HumanReadableDateTimeDIV()
{
	return this.delay;
}

// Sets the delay between loops
function SetDelay_HumanReadableDateTimeDIV(delay)
{
	if (delay < 1)
	{
		return;
	}

	this.delay = delay;
}

// Return the reference to the hrDT member object
function GetHrDTRef_HumanReadableDateTimeDIV()
{
	return this.hrDT;
}

function FillDateTime_HumanReadableDateTimeDIV()
{
	this.divElem.innerHTML = this.hrDT.GetDateTime();
}

// Global function for the loop
function Sheaky_Start_HumanReadableDateTimeDIV()
{
	window.humanReadableDateTimeDIVRef.FillDateTime();
	
	window.setTimeout(window.Sheaky_Start_HumanReadableDateTimeDIV, window.humanReadableDateTimeDIVRef.GetDelay());
}

// Called to start showing the DateTime object
function Start_HumanReadableDateTimeDIV()
{
	window.humanReadableDateTimeDIVRef				= this;
	window.Sheaky_Start_HumanReadableDateTimeDIV	= Sheaky_Start_HumanReadableDateTimeDIV;
	
	window.Sheaky_Start_HumanReadableDateTimeDIV();
}

// Used to create an HumanReadableDateTimeDIV object
function HumanReadableDateTimeDIV(divID)
{
	// --private
	this.divElem			= null;
	this.delay				= 1000;
	this.hrDT				= new HumanReadableDateTime();
	
	// --public
	this.GetDivElemRef		= GetDivElemRef_HumanReadableDateTimeDIV;
	this.GetDelay			= GetDelay_HumanReadableDateTimeDIV;
	this.SetDelay			= SetDelay_HumanReadableDateTimeDIV;
	this.GetHrDTRef			= GetHrDTRef_HumanReadableDateTimeDIV;
	this.FillDateTime		= FillDateTime_HumanReadableDateTimeDIV;
	this.Start				= Start_HumanReadableDateTimeDIV;
	//--private
	this.OnInit				= OnInit_HumanReadableDateTimeDIV;


	this.OnInit(divID);
}

/************************** END - HumanReadableDateTimeDIV ***************************/
