//Possible Time Formats
var SHORTTIME	= 1 // Time (hours:min) eg 12:00 AM 
var TIME		= 2 // Time (hours:min:secs) eg 12:00:23 AM
var SHORTTIME24 = 3 // Time 24hr clock (hours:min) eg 18:00
var TIME24		= 4 // Time 24hr clock (hours:min:secs) eg 18:00:34

//Possible Date Formats
var FULLDATE	= 10 // Long Date Friday 13th July 2007
var LONGDATE	= 20 // 13th July 2007
//following options you can pass a seperator value in eg / - 
var MEDDATE		= 30 // 13/Jul/2007, 13-Jul-2007, 13 Jul 2007
var SHORTDATE	= 40 // 13/07/2007, 13-07-2007, 13 07 2007
var ABVDATE		= 50 // 13/07/07, 13-07-07, 13 07 07
var ISODATE		= 60 // 2007-07-07
var USDATE		= 70 // 07/23/2007

var dayNames=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var monthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December');

function TimeSource(){
   x=new Date();
   x.setTime(x.getTime());
   return x;
}
function LeadingZero(x){
   return (x>9)?x:'0'+x;
}
function DateEnding(x){
   if(x==1||x==21||x==31){
      return 'st';
   }
   if(x==2||x==22){
      return 'nd';
   }
   if(x==3||x==23){
      return 'rd';
   }
   return 'th';
}
function fixYear4(x){
   return (x<500)?x+1900:x;
}
//function to show datetime at certain place and keep updating clock
function DisplayDateClock(DateFormat,TimeFormat,Sep,el)
{
	var dte = GetFormatedDateTime(DateFormat,TimeFormat,Sep);
	//alert("date is " + dte + " El Id = " + el)
	document.getElementById(el).innerHTML=dte;
	setTimeout(function(){DisplayDateClock(DateFormat,TimeFormat,Sep,el);return true;},1000);
}

function DisplayDate(DateFormat,TimeFormat,Sep,el)
{
	var dte = GetFormatedDateTime(DateFormat,TimeFormat,Sep)
	document.getElementById(el).innerHTML=dte;
}

function GetFormatedDateTime(DateFormat,TimeFormat,Sep)
{
	var dteSource	= TimeSource();
	var dte			= "";
	
	//might only want to return a time so check if we need to show the date part
	if(DateFormat>0){
		dte += GetFormatedDate(dteSource,DateFormat,Sep)
	}	
	//might not want to show a time eg just return a date
	if(TimeFormat>0){
		if(dte!="")
		{
			dte += " "
		}
		dte += GetFormatedTime(dteSource,TimeFormat) ;
	}
	return dte;
}

//returns a date in a certain format
function GetFormatedDate(dteSource,DateFormat,Sep)
{	
	var dte			= "";
	var month		= dteSource.getMonth() + 1
	var monthName	= monthNames[month-1];
	var shortMonth	= monthName.substring(0,3);
	var day			= dteSource.getDate();
	var weekDay		= dteSource.getDay();
	var year		= dteSource.getFullYear();
	
	//if no seperator value passed in (empty seperator allowed for ISODATE eg 20072312
	if(Sep==null)
		Sep = "/";

	switch (DateFormat)
	{
		case FULLDATE:	
			dte = dayNames[weekDay]+" "+day+DateEnding(day)+" "+monthName+" "+year; // Friday 13th July 2007
			break;
		case LONGDATE:
			dte = day+DateEnding(day)+" "+monthName+" "+year; // 13th July 2007
			break;
		case MEDDATE:
			dte = LeadingZero(day)+Sep+shortMonth+Sep+year; // 13/Jul/2007 or 13-Jul-2007
			break;
		case SHORTDATE:
			dte = LeadingZero(day)+Sep+LeadingZero(month)+Sep+year; // 13/07/2007 or 13-07-2007
			break;
		case ABVDATE:
			dte = LeadingZero(day)+Sep+LeadingZero(month)+Sep+year.subtring(2,4); // 13/07/07 or 13-07-07
			break;
		case ISODATE:
			dte = year+Sep+LeadingZero(month)+Sep+LeadingZero(day); //2007-10-01
			break;
		case USDATE:
			dte = LeadingZero(month)+Sep+LeadingZero(day)+Sep+year;
			break;

	}

	return dte;	
}

function GetFormatedTime(dteSource,TimeFormat)
{
	var hours	= dteSource.getHours();
	var minutes = dteSource.getMinutes();
	var seconds	= dteSource.getSeconds();
	
	//if not 24hr clock sort out
	if (TimeFormat!=SHORTTIME24 && TimeFormat!=TIME24)
	{
		if(hours>12)
			hours = hours - 12;
	}

	var dtTime = LeadingZero(hours) + ":" + LeadingZero(minutes)
	//if format requires seconds to be displayed
	if(TimeFormat==TIME || TimeFormat==TIME24){
		dtTime += ":" + LeadingZero(seconds)
	}
	//if format requires AM / PM to be displayed eg not 24hr clock
	if(TimeFormat==SHORTTIME || TimeFormat==TIME){
		if(hours > 11){
			dtTime += " PM"
		} else {
			dtTime +=" AM"	
		}
	}

	return dtTime;
}

