var clockHolder = 'clock';
var _timediff;
function StartClock(parentId, date){

//	if(navigator.appName == "Netscape") {
//	document.write('<layer id="clock"></layer>');
//	}

	try {
		var re = /_/g;
		_timediff = (new Date(date.replace(re, '/'))) - (new Date());
	} catch(e) {
		_timediff = null;
	}
		

	if (navigator.appVersion.indexOf("MSIE") != -1)
	{
		pDiv = document.getElementById(parentId);
		if(pDiv)
		{
			clockHolder = parentId;
		}
		else
		{
			document.write('<span id="clock"></span>');
		}
	}
	upclock();
	setInterval("upclock()",1000)
}

function upclock(){ 
 
	var dte = new Date();
	
	var timeZoneOffset = dte.getTimezoneOffset();
	
	//Set to GMT
	//dte.setTime(dte.getTime() + timeZoneOffset*60*1000);
	//dte.setTime(dte.getTime() - 5*60*60*1000);
	
    //Adjust clock for daylight savings
    var dst = getDST(dte);
   	//dte.setTime(dte.getTime() + dst*60*60*1000);
	if (_timediff) dte.setTime(dte.getTime() + _timediff);
	
	var date = dte.toLocaleDateString();
	var hrs = dte.getHours();
	var min = dte.getMinutes(); 
	var sec = dte.getSeconds();
	var col = ":";
	var spc = " ";
	var apm;
	var zone = (!dst)? " EST" : " EDT";

	/*
	if (12 < hrs) { 
		apm="PM";
		hrs-=12;
	}
	else {
		apm="AM";
	}
	*/

	if (hrs == 0) hrs=12;
	if (min<=9) min="0"+min;
	if (sec<=9) sec="0"+sec;


//	if(navigator.appName == "Netscape") {
//		document.clock.document.write(date+spc+hrs+col+min+col+sec+spc+apm+zone);
//		document.clock.document.close();
//	}

	if (navigator.appVersion.indexOf("MSIE") != -1)
	{
		ctl = document.getElementById(clockHolder);
		if(ctl)
			ctl.innerHTML = date+spc+hrs+col+min+col+sec+spc; //+apm+zone;
	}
} 

// returns 1 for EDT and 0 for normal EST.
/* The United States and Canada will extend Daylight Saving Time from 2007, 
   and probably other regions and countries in Northern America will follow. 
   The new start date will be the second Sunday in March (before 2007 first 
   Sunday in April), to the first Sunday in November (before 2007 last Sunday 
   in October).*/
function getDST(date){
    var dst = 0;
    with (date){
        if (getFullYear() <= 2006){
            var dstStart = new Date(getFullYear(), 3, 1);
            var dstEnd = new Date(getFullYear(), 9, 31);
            if (dstStart.getDay() > 0) dstStart.setDate(dstStart.getDate()+7-dstStart.getDay());
            if (dstEnd.getDay() > 0) dstEnd.setDate(dstEnd.getDate()-dstEnd.getDay());
            if (date >= dstStart && date < dstEnd) dst = 1;
        }
        else{ 
            var dstStart = new Date(getFullYear(), 2, 8);
            var dstEnd = new Date(getFullYear(), 10, 1);
            if (dstStart.getDay() > 0) dstStart.setDate(dstStart.getDate()+7-dstStart.getDay());
            if (dstEnd.getDay() > 0) dstEnd.setDate(dstEnd.getDate()+7-dstEnd.getDay());
            if (date >= dstStart && date < dstEnd) dst = 1;
        }
    }
    return dst;
}

/*
	CountDown Ticker
	usage:
		new CountDown(<id of ticker container>, <miliseconds to count>);
*/
function CountDown(el, ms, interval, direction)
{
	this.init(el, ms, interval, direction);
}

CountDown.getValue = function(d)
{
    var sec = Math.floor(d / 1000);
    var hour = Math.floor(sec / 3600);
    sec -= hour * 3600;
    var min = Math.floor(sec / 60);
    sec -=  min * 60;
        
    // CountDown.padZeroes(d.getHours(), 2)+":"+
	return CountDown.padZeroes(hour, 2)+":"+CountDown.padZeroes(min, 2)+":"+CountDown.padZeroes(sec, 2);
}

CountDown.padZeroes = function(s, z)
{
	return new String(Math.pow(10, z) + s).substr(1, z);
}

CountDown.prototype.tick = function()
{
	var d = this.deadline - new Date();
	if (d > 0)
		this.element.innerText = CountDown.getValue(d);
	else
		clearInterval(this.ticker);
}

CountDown.prototype.tickUp = function()
{
	var d = new Date() - this.startTime;
	if (d < this.duration)
		this.element.innerText = CountDown.getValue(d);
	else
		clearInterval(this.ticker);
}

CountDown.prototype.stop = function()
{
	if (this.ticker) 
		clearInterval(this.ticker);
}

CountDown.prototype.init = function(el, ms, interval, direction)
{
	this.stop();
	if (typeof(el) == "string")
		this.element = document.getElementById(el);
	else
		this.element = el;
		
	if (this.element == null) return;
	this.deadline = new Date();
	this.startTime = new Date();
	this.duration = parseInt(ms);
	this.deadline.setTime(this.startTime.getTime() + this.duration);
	this.interval = (interval) ? parseInt(interval) : 1000;
	this.direction = (direction) ? parseInt(direction) : 1;

	if (document.countdowns == null)
		document.countdowns = new Array();
	if (!this.globalID) 
	{
		this.globalID = document.countdowns.length;
		document.countdowns[this.globalID] = this;
	}
	if (this.direction > 0) 
	{
		this.tick();
		this.ticker = setInterval("document.countdowns[" + this.globalID + "].tick()",  this.interval);
	}
	else
	{
		this.tickUp();
		this.ticker = setInterval("document.countdowns[" + this.globalID + "].tickUp()",  this.interval);
	}	
}
