var xmlHttp;


var m_strOut;
var ServerTimeData;
var clockIncrementMillis =1000; //60000;
var localTime;
var clockOffset;
var clockExpirationLocal;
var clockShowsSeconds = true;
var clockTimerID = null;
var clockLocalStartTime = new Date();
var clockServerStartTime =null;
 var serverResponse=null;
var imgpath;
function getSeverDateTime(urlpath,imagepath)
{
	
imgpath=imagepath;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  } 
//var url="severscript/currenttime.php";
var url=urlpath;
	
xmlHttp.onreadystatechange=stateChanged;
	
xmlHttp.open("GET",url,true);
	
xmlHttp.send(null);
	
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
	//alert(xmlHttp.responseText); return;
         serverResponse=stripAlphaChars(xmlHttp.responseText)
		 
var ChaArray=new Array();
ChaArray=serverResponse.split(',');
var Syear=ChaArray[0];
var Smonth=ChaArray[1];
var Sdate=ChaArray[2];
var Shour=ChaArray[3];
var Sminute=ChaArray[4];
var Ssecond=ChaArray[5];

//ServerTimeData = new Date(2009,2,28,10,49,18);

ServerTimeData = new Date(Syear,Smonth,Sdate,Shour,Sminute,Ssecond);
//alert(ServerTimeData);
clockInit(ServerTimeData);


 	
 } 
}function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


function stripAlphaChars(pstrSource) 
{ 

m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9\,]/g, ''); 

    return m_strOut; 
}






function clockInit(ServerTD)
{
	
   //alert("clockInit");
    clockServerStartTime=ServerTD;
    var localDateObject=clockLocalStartTime;
    var serverDateObject =clockServerStartTime;
    var origRemoteClock = parseInt(clockGetCookieData("remoteClock"));
    var origLocalClock = parseInt(clockGetCookieData("localClock"));
    var newRemoteClock = serverDateObject.getTime();
    // May be stale (WinIE); will check against cookie later
    // Can't use the millisec. ctor here because of client inconsistencies.
    var newLocalClock = localDateObject.getTime();
    var maxClockAge = 60 * 60 * 1000;   // get new time from server every 1hr

    if (newRemoteClock != origRemoteClock) {
        // new clocks are up-to-date (newer than any cookies)
        document.cookie = "remoteClock=" + newRemoteClock;
        document.cookie = "localClock=" + newLocalClock;
        clockOffset = newRemoteClock - newLocalClock;
        clockExpirationLocal = newLocalClock + maxClockAge;
        localTime = newLocalClock;  // to keep clockUpdate() happy
    }
    else if (origLocalClock != origLocalClock) {
        // error; localClock cookie is invalid (parsed as NaN)
        clockOffset = null;
        clockExpirationLocal = null;
    }
    else {
        // fall back to clocks in cookies
        clockOffset = origRemoteClock - origLocalClock;
        clockExpirationLocal = origLocalClock + maxClockAge;
        localTime = origLocalClock;
        // so clockUpdate() will reload if newLocalClock
        // is earlier (clock was reset)
    }
    /* Reload page at server midnight to display the new date,
       by expiring the clock then */
    var nextDayLocal = (new Date(serverDateObject.getFullYear(),
            serverDateObject.getMonth(),
            serverDateObject.getDate() + 1)).getTime() - clockOffset;
    if (nextDayLocal < clockExpirationLocal) {
        clockExpirationLocal = nextDayLocal;
    }
	clockUpdate();

}

function clockOnUnload() {
    clockClearTimeout();
}

function clockClearTimeout() {
    if (clockTimerID) {
        clearTimeout(clockTimerID);
        clockTimerID = null;
    }
}
function clockTimeString(inHours, inMinutes, inSeconds,inDays) {
//debugger
  /*  return inHours == null ? "-:--" : ((inHours == 0
                   ? "12" : (inHours <= 12 ?"0"+inHours : inHours - 12))
                + (inMinutes < 10 ? ":0" : ":") + inMinutes
                + (clockShowsSeconds
                   ? ((inSeconds < 10 ? ":0" : ":") + inSeconds) : "")
                + (inDays <= 99 ? "0"+inDays : inDays)
		+ (inHours < 12 ? ":AM" : ":PM"));*/

    return inHours == null ? "-:--" : ( (inHours < 10?"0"+inHours : inHours)
                + (inMinutes < 10 ? ":0" : ":") + inMinutes
                + (clockShowsSeconds
                   ? ((inSeconds < 10 ? ":0" : ":") + inSeconds) : "")
                + (inDays <= 99 ? "0"+inDays : inDays));



}

function clockDisplayTime(inHours, inMinutes, inSeconds,inDays) {
    //debugger
	//alert("clockDisplayTime");
	clockWriteToDiv(clockTimeString(inHours, inMinutes, inSeconds,inDays));
}
function clockWriteToDiv(newValue) // APS 6/29/00
{
	//debugger
	//alert(newValue);
//alert(newValue.substring(4,5));


dg0 = new Image();dg0.src = imgpath+"/dg0.gif";
dg1 = new Image();dg1.src = imgpath+"/dg1.gif";
dg2 = new Image();dg2.src = imgpath+"/dg2.gif";
dg3 = new Image();dg3.src = imgpath+"/dg3.gif";
dg4 = new Image();dg4.src = imgpath+"/dg4.gif";
dg5 = new Image();dg5.src = imgpath+"/dg5.gif";
dg6 = new Image();dg6.src = imgpath+"/dg6.gif";
dg7 = new Image();dg7.src = imgpath+"/dg7.gif";
dg8 = new Image();dg8.src = imgpath+"/dg8.gif";
dg9 = new Image();dg9.src = imgpath+"/dg9.gif";
/*dgam= new Image();dgam.src= "dgam.gif";
dgpm= new Image();dgpm.src= "dgpm.gif";
dgc = new Image();dgc.src = "dgc.gif";*/


	document.hr1.src = imgpath+'/dg'+newValue.substring(0,1)+'.gif';
	document.hr2.src = imgpath+'/dg'+newValue.substring(1,2)+'.gif';

	document.mn1.src = imgpath+'/dg'+newValue.substring(3,4)+'.gif';
	document.mn2.src = imgpath+'/dg'+newValue.substring(4,5)+'.gif';

	document.se1.src = imgpath+'/dg'+newValue.substring(6,7)+'.gif';
	document.se2.src = imgpath+'/dg'+newValue.substring(7,8)+'.gif';

	document.dy1.src = imgpath+'/dg'+newValue.substring(8,9)+'.gif';
	document.dy2.src = imgpath+'/dg'+newValue.substring(9,10)+'.gif';
	document.dy3.src = imgpath+'/dg'+newValue.substring(10,11)+'.gif';
	//document.ampm.src= 'dg'+newValue.substring(12,14)+'.gif';
}


function clockGetCookieData(label) {
    /* find the value of the specified cookie in the document's
    semicolon-delimited collection. For IE Win98 compatibility, search
    from the end of the string (to find most specific host/path) and
    don't require "=" between cookie name & empty cookie values. Returns
    null if cookie not found. One remaining problem: Under IE 5 [Win98],
    setting a cookie with no equals sign creates a cookie with no name,
    just data, which is indistinguishable from a cookie with that name
    but no data but can't be overwritten by any cookie with an equals
    sign. */
    var c = document.cookie;
    if (c) {
        var labelLen = label.length, cEnd = c.length;
        while (cEnd > 0) {
            var cStart = c.lastIndexOf(';',cEnd-1) + 1;
            /* bug fix to Danny Goodman's code: calculate cEnd, to
            prevent walking the string char-by-char & finding cookie
            labels that contained the desired label as suffixes */
            // skip leading spaces
            while (cStart < cEnd && c.charAt(cStart)==" ") cStart++;
            if (cStart + labelLen <= cEnd && c.substr(cStart,labelLen) == label) {
                if (cStart + labelLen == cEnd) {                
                    return ""; // empty cookie value, no "="
                }
                else if (c.charAt(cStart+labelLen) == "=") {
                    // has "=" after label
                    return unescape(c.substring(cStart + labelLen + 1,cEnd));
                }
            }
            cEnd = cStart - 1;  // skip semicolon
        }
    }
    return null;
}
/* Called regularly to update the clock display as well as onLoad (user
   may have clicked the Back button to arrive here, so the clock would need
   an immediate update) */
function clockUpdate()
{
//	debugger
    var lastLocalTime = localTime;
    localTime = (new Date()).getTime();
    
    /* Sanity-check the diff. in local time between successive calls;
       reload if user has reset system clock */
    if (clockOffset == null) {
        clockDisplayTime(null, null, null);
    }
    else if (localTime < lastLocalTime || clockExpirationLocal < localTime) {
        /* Clock expired, or time appeared to go backward (user reset
           the clock). Reset cookies to prevent infinite reload loop if
           server doesn't give a new time. */
        document.cookie = 'remoteClock=-';
        document.cookie = 'localClock=-';
        location.reload();      // will refresh time values in cookies
    }
    else {
        // Compute what time would be on server 
        var serverTime = new Date(localTime + clockOffset);


	   Today = new Date();
         Todays_Year = Today.getFullYear();
         Todays_Month = Today.getMonth();                  
         
         //Convert both today's date and the target date into miliseconds.                           
         Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), 
                                 serverTime.getHours(), serverTime.getMinutes(),serverTime.getSeconds())).getTime();                                 
         Target_Date = (new Date(2010,5 - 1, 1, 00, 00, 00)).getTime();                  
         
         //Find their difference, and convert that into seconds.                  
         Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
         
         if(Time_Left < 0)
            Time_Left = 0;
         
        
                    //More datailed.
                    days = Math.floor(Time_Left / (60 * 60 * 24));
                    Time_Left %= (60 * 60 * 24);
                    hours = Math.floor(Time_Left / (60 * 60));
                    Time_Left %= (60 * 60);
                    minutes = Math.floor(Time_Left / 60);
                    Time_Left %= 60;
                    seconds = Time_Left;

                    clockDisplayTime(hours,minutes,seconds,days);
                    clockTimerID = setTimeout("clockUpdate()",1000);                  
                    

                    /*document.all.countdown.innerHTML = days + ' day' + dps + ' ';
                    document.all.countdown.innerHTML += hours + ' hour' + hps + ' ';
                    document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and ';
                    document.all.countdown.innerHTML += seconds + ' second' + sps;*/
                    
                	 //Recursive call, keeps the clock ticking.
        		// setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);

		


        	//clockDisplayTime(serverTime.getHours(), serverTime.getMinutes(),serverTime.getSeconds());
        
        // Reschedule this func to run on next even clockIncrementMillis boundary
        //clockTimerID = setTimeout("clockUpdate()",clockIncrementMillis - (serverTime.getTime() % clockIncrementMillis));
    }
}
