<PUBLIC:COMPONENT>
	<PUBLIC:ATTACH EVENT="onload" ONEVENT="StartClock()" FOR="window" />

	<script Language="Javascript">
		var mDateTime;
		var mCutoffTime;
		var aCriticalWarning = 5;
		var aModerateWarning = 15;
		var aWarning = 30;
		var aReminder = 59;
		var mOrderType;
		var mActionType;

		function StartClock()
		{
			mDateTime = new Date(element.year, element.month, element.day, element.hours, element.minutes, element.seconds);
			mCutoffTime = new Date(element.year, element.month, element.day, element.cutoffHours, element.cutoffMinutes, element.cutoffSeconds);
			mOrderType = element.orderType;
			mActionType = element.actionType;
			
			UpdateClock();
			window.setInterval(UpdateClock, 1000);
		}
		
		function ShowWarningWindow(inclass, intimeleft)
		{
			var aCookie = GetCookie(mOrderType + mActionType + intimeleft);

			if(aCookie == null)
			{
				// we haven't shown this window before so do so now
				
				// need to work out how long the dialog is open for so that we can then update the time after it is closed
				var aBeforeShowDate = new Date();
				window.showModalDialog(encodeURI("../../UnAuthenticated/DeadlineWarning.aspx?class=" + inclass + "&ordertype=" + mOrderType + "&actiontype=" + mActionType + "&timeremaining=" + intimeleft));
				var aAfterShowDate = new Date();
				
				// set the cookie so we don't show that warning again
				var aEndOfDay = new Date();
				aEndOfDay.setHours(23, 59, 59);
				SetCookie(mOrderType + mActionType + intimeleft, "shown", aEndOfDay, "/");

				// update our store date time				
				mDateTime.setTime(mDateTime.getTime() + (aAfterShowDate - aBeforeShowDate));
			}
		}
		
		function UpdateClock()
		{
			mDateTime.setSeconds(mDateTime.getSeconds() + 1);
			
			if(mDateTime < mCutoffTime)
			{
				var aDiff = mCutoffTime - mDateTime;
				var aClass = 'normal';

				var aHours = aDiff/(1000 * 60 * 60);
				aHours = Math.floor(aHours);
				aDiff = aDiff - (1000 * 60 * 60 * aHours);
				
				var aMins = aDiff/(1000 * 60);
				aMins = Math.floor(aMins);
				aDiff = aDiff - (1000 * 60 * aMins);
				
				if(aHours == 0)
				{
					if(aMins <= aCriticalWarning)
					{
						aClass = 'criticalwarning';
						if(aMins == aCriticalWarning)
						{
							ShowWarningWindow('criticalwarning', aCriticalWarning + " mins");
						}
					}
					else if(aMins <= aModerateWarning)
					{
						aClass = 'moderatewarning';
						if(aMins == aModerateWarning)
						{
							ShowWarningWindow('moderatewarning', aModerateWarning + " mins");
						}
					}
					else if(aMins <= aWarning)
					{
						aClass = 'warning';
						if(aMins == aWarning)
						{
							ShowWarningWindow('warning', aWarning + " mins");
						}
					}
					else if(aMins <= aReminder)
					{
						aClass = 'reminder';
						if(aMins == aReminder)
						{
							ShowWarningWindow('reminder', aReminder + " mins");
						}
					}
				}
				
				var aSecs = aDiff/1000;
				aSecs = Math.floor(aSecs);
				
				element.innerHTML = '<span class="' + aClass + '">' + aHours + 'h ' + aMins + 'm ' + aSecs + 's</span>';
			}
			else
			{
				element.innerHTML = "Expired";
			}
		}
		

	</script>
</PUBLIC:COMPONENT>
