var timerID = null;
var timerRunning = false;

function stopclock(){

    // cannot directly test timerID on DEC OSF/1 in beta 4.
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false;
}

function startclock(){

    // Make sure the clock is stopped & then reinstantiated after each
    // setTimeout
    stopclock();
    showtime();
    resizeClocks();
}

function resizeClocks() {
    /*var layer = document.getElementById('clockswrapper');
    layer.style.width = document.body.clientWidth + 'px';
    alert('Resized to width ' + document.body.clientWidth);*/
}

function showtime(){

    //call new instance of Date object
    var now = new Date();
    

    var hours = now.getUTCHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();

    //timezone offsets
    
    var londonHour = hours + 0;
    var sydneyHour = hours + 10;
//    var chicagoHour = hours - 6;   DST OFFEST NEEDS AN AUTOMATIC REPLACEMENT FOR THIS MANUAL CHANGE
//    var nyorkHour = hours - 8; 
    var chicagoHour = hours - 5;
    var nyorkHour = hours - 4;
    
    //format hours display for each timezone----------------
    var gmtValue = "" + ((hours < 10) ? "0" : "") + hours;

    
   
    //adjust london offset
    if(londonHour <= 24){
    	var londonValue = "" + ((londonHour < 10) ? "0" : "") + londonHour;
    }
    else{
    	londonValue = "" + (((londonHour - 24) < 10) ? "0" : "") + (londonHour - 24);
    }

    //adjust sydney offset
    if(sydneyHour <= 24){
    	var sydneyValue = "" + ((sydneyHour < 10) ? "0" : "") + sydneyHour;
    }
    else{
    	sydneyValue = "" + (((sydneyHour - 24) < 10) ? "0" : "") + (sydneyHour - 24);
    }

    //adjust chicago offset
    if(chicagoHour >= 0){
    	var chicagoValue = "" + ((chicagoHour < 10) ? "0" : "") + chicagoHour;
    }
    else{
    	chicagoValue = "" + (((chicagoHour + 6) < 10) ? "0" : "") + (chicagoHour + 6);
    }	
    
    //adjust new york offset
    if(nyorkHour >= 0){
    	var nyorkValue = "" + ((nyorkHour < 10) ? "0" : "") + nyorkHour;
    }
    else{
    	nyorkValue = "" + (((nyorkHour + 5) < 10) ? "0" : "") + (nyorkHour + 5);
    }	
    
    //format minutes display for each timezone----------------
    gmtValue  += ((minutes < 10) ? ":0" : ":") + minutes;
   
    londonValue += ((minutes < 10) ? ":0" : ":") + minutes;    
    sydneyValue += ((minutes < 10) ? ":0" : ":") + minutes;
    chicagoValue += ((minutes < 10) ? ":0" : ":") + minutes;
    nyorkValue += ((minutes < 10) ? ":0" : ":") + minutes;
    
    //format seconds display for each timezone----------------
    gmtValue  += ((seconds < 10) ? ":0" : ":") + seconds;
   
    londonValue += ((seconds < 10) ? ":0" : ":") + seconds;
    sydneyValue += ((seconds < 10) ? ":0" : ":") + seconds;
    chicagoValue += ((seconds < 10) ? ":0" : ":") + seconds;
    nyorkValue += ((seconds < 10) ? ":0" : ":") + seconds;
    
    //write concatenated strings for each timezone to document
    document.clock.face.value = gmtValue;
    document.clock3.face3.value = sydneyValue;
    document.clock2.face2.value = londonValue;
    document.clock5.face5.value = nyorkValue;
    document.clock4.face4.value = chicagoValue;

    timerID = setTimeout("showtime()",1000);
    
    timerRunning = true;
}

