﻿if(!window["shopperCurrentDateTimeList"])window["shopperCurrentDateTimeList"]=[];
function shopperCurrentDateTime(id, opts)
{
	this.LayerId=id;
	this.Layer=null;
	this.ThreeLetters=opts&&(opts.df==2);
	shopperCurrentDateTimeList.push(this);
	shopperCurrentDateTimeList[this.Index=shopperCurrentDateTimeList.length]=this;
	this.CurText="";
	this.Pattern="";
	if(opts&&(opts.bef!=null))this.Pattern+=opts.bef;
	switch((opts&&opts.df)?opts.df:2)
	{
		case 0:
			//DateFormats.None
			break;
		case 1:
			//DateFormats.Short
			this.Pattern+=this.Escape(this.ShortDatePattern);
			break;
		default:
			//DateFormats.LongThreeLetters | DateFormats.LongFull
			this.Pattern+=this.Escape(this.LongDatePattern);
			break;
	}
	if(opts&&(opts.sep!=null))this.Pattern+=opts.sep;
	switch((opts&&opts.tf)?opts.tf:2)
	{
		case 0:
			//TimeFormats.None
			break;
		case 1:
			//DateFormats.Short
			this.Pattern+=this.Escape("H"+(this.TimeSeparator?this.TimeSeparator:"")+"mm");
			break;
		default:
			//DateFormats.Long
			this.Pattern+=this.Escape("H"+(this.TimeSeparator?this.TimeSeparator:"")+"mm"+(this.TimeSeparator?this.TimeSeparator:"")+"ss");
			break;
	}
	if(opts&&(opts.aft!=null))this.Pattern+=opts.aft;
	setInterval("shopperCurrentDateTimeList["+this.Index+"].Update()",350);
}
shopperCurrentDateTime.prototype=
{
	P:
	{
		Day:"\x01d\x02",
		Day_2:"\x01dd\x02",
		WeekDayName:"\x01ddd\x02",
		Hour12:"\x01h\x02",
		Hour12_2:"\x01hh\x02",
		Hour24:"\x01H\x02",
		Hour24_2:"\x01HH\x02",
		Minute:"\x01m\x02",
		Minute_2:"\x01mm\x02",
		Month:"\x01M\x02",
		Month_2:"\x01MM\x02",
		MonthName:"\x01MMM\x02",
		Second:"\x01s\x02",
		Second_2:"\x01ss\x02",
		AMPM_1:"\x01t\x02",
		AMPM_2:"\x01tt\x02",
		Year_2:"\x01yy\x02",
		Year:"\x01y\x02"
	},
	_escape:function(s,a,P)
	{
		return s.replace(new RegExp("(?=(^|\\b|_))"+a+"(?=($|\\b|_))","gm"),P);
	},
	Escape:function(s)
	{
		if(s==null)return "";
		s=this._escape(s,"d",this.P.Day);
		s=this._escape(s,"dd",this.P.Day_2);
		s=this._escape(s,"d{3,}",this.P.WeekDayName);
		s=this._escape(s,"h",this.P.Hour12);
		s=this._escape(s,"h{2,}",this.P.Hour12_2);
		s=this._escape(s,"H",this.P.Hour24);
		s=this._escape(s,"H{2,}",this.P.Hour24_2);
		s=this._escape(s,"m",this.P.Minute);
		s=this._escape(s,"m{2,}",this.P.Minute_2);
		s=this._escape(s,"M",this.P.Month);
		s=this._escape(s,"MM",this.P.Month_2);
		s=this._escape(s,"M{3,}",this.P.MonthName);
		s=this._escape(s,"s",this.P.Second);
		s=this._escape(s,"s{2,}",this.P.Second_2);
		s=this._escape(s,"t",this.P.AMPM_1);
		s=this._escape(s,"t{2,}",this.P.AMPM_2);
		s=this._escape(s,"yy",this.P.Year_2);
		s=this._escape(s,"y+",this.P.Year);
		return s;
	},
	Update:function()
	{
		var s,d,n,pm=false;
		if(document&&document.readyState)switch(document.readyState)
		{
			case "uninitialized":
			case "loading":
			case "interactive":
				setTimeout("shopperCurrentDateTimeList["+this.Index+"].Update();",200);
				return;
		}
		if(!this.Layer)
		{
			try
			{ this.Layer=document.getElementById(this.LayerId); }
			catch(e)
			{ }
			if(!this.Layer)return;
		}
		d=new Date();
		s=this.Pattern;
		s=s.replace(this.P.Day, d.getDate()).replace(this.P.Day_2, this._to2(d.getDate()));
		s=s.replace(this.P.WeekDayName,this._get3(this.WeekDayNames[d.getDay()]));
		n=d.getHours();
		if(!n)
		{
			n=12;
			pm=true;
		}
		else if(n>12)
		{
			n-=12;
			pm=true;
		}
		s=s.replace(this.P.Hour12,n).replace(this.P.Hour12_2,this._to2(n));
		s=s.replace(this.P.Hour24,d.getHours()).replace(this.P.Hour24_2,this._to2(d.getHours()));
		s=s.replace(this.P.Minute,d.getMinutes()).replace(this.P.Minute_2,this._to2(d.getMinutes()));
		s=s.replace(this.P.Month,1+d.getMonth()).replace(this.P.Month_2,this._to2(1+d.getMonth())).replace(this.P.MonthName,this._get3(this.MonthNames[d.getMonth()]));
		s=s.replace(this.P.Second,d.getSeconds()).replace(this.P.Second_2,this._to2(d.getSeconds()));
		s=s.replace(this.P.AMPM_1,pm?"P":"A").replace(this.P.AMPM_2,pm?"PM":"AM");
		s=s.replace(this.P.Year,d.getFullYear()).replace(this.P.Year_2,this._to2(d.getFullYear()));
		if(this.CurText!=s)
			this.Layer.innerHTML=this.CurText=s;
	},
	_get3:function(s)
	{
		return (this.ThreeLetters&&(s.length>3))?s.substr(0,3):s;
	},
	_to2:function(n)
	{
		n=n.toString();
		while(n.length<2)n="0"+n;
		if(n.length>2)
			n=n.substr(n.length-2,2);
		return n;
	}
};
