// JavaScript Document
function countdown(obj)
{
	this.obj			= obj;
	this.Div			= "stock";
	this.BackColor		= "white";
	this.ForeColor		= "black";
	this.TargetDate;
	this.TargetStock;
	this.InStock;
	this.Delivery;
	this.DisplayFormat	= "%%D%%";
	this.CountActive	= true;
	
	this.DisplayStr;
	this.FinishMessage;
	this.Calcage		= cd_Calcage;
	this.CountBack		= cd_CountBack;
	this.Setup			= cd_Setup;
}



function cd_Calcage(secs, num1, num2) {
	s = ((Math.floor(secs/num1))%num2).toString();
	if (this.InStock > 0 && this.InStock < 10 && this.TargetDate != "")
			s = "<p>" + this.InStock + " in stock<br />" + this.TargetStock + " units expected in " +  s + " days</p>";
	else if (this.InStock <= 0 && this.TargetDate != "")
			if (s < 2)
    			s = "<p>Out of stock<br />" + this.TargetStock + " units expected<br />in " + s + " day</p>";
			else
				s = "<p>Out of stock<br />" + this.TargetStock + " units expected<br />in " +  s + " days</p>";

		else if (this.InStock == "")
			s = "<p>Out of stock</p>";
		else
			s = "<p>" + this.InStock + " in stock</p>";

	return s ; 
}

function cd_CountBack(secs)
{
  if (secs < 0) {
	this.FinishMessage = "Out of stock<br />Stock overdue<br />" + this.TargetStock + " units expected";
    document.getElementById(this.Div).innerHTML = this.FinishMessage;
    return;
  }
  this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
  this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
  this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
  this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));

  document.getElementById(this.Div).innerHTML = this.DisplayStr;
  if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
}

function cd_Setup()
{
	var dthen	= new Date(reformDate(this.TargetDate));
  	var dnow	= new Date();
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(ddiff.valueOf()/1000);
	this.CountBack(gsecs);
}

function reformDate(dateString)
{
	var dateAr=dateString.split('/')
	return dateAr[1]+'/'+dateAr[0]+'/'+dateAr[2]
}
