// JavaScript Document
 
/***********************************************
* Javascript Countdown Timer With Given Seconds Difference
* Modified by Keith (keith@phpdiary.org)
* Source:
* Dynamic Countdown script- © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
 
function cdtime(container, seconds_to_countdown)
{

if (!document.getElementById || !document.getElementById(container)) return
    this.container=document.getElementById(container)
    this.elapsedTime=0
    //this.seconds_to_countdown=new Date(seconds_to_countdown)
    this.seconds_to_countdown=seconds_to_countdown
    this.timesup=false
    this.updateTime()

}
 
cdtime.prototype.updateTime=function()
{
    var thisobj=this
    this.elapsedTime++
    setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
	
}
 
cdtime.prototype.displaycountdown=function(baseunit, functionref)
{
    this.baseunit=baseunit
    this.formatresults=functionref
    this.showresults()
}
 
cdtime.prototype.showresults=function()
{
    var thisobj=this
 
    var timediff=this.seconds_to_countdown-this.elapsedTime //difference btw target date and current date, in seconds
	
	if(timediff==(24*3600)){ 
//	alert('Please checkout and pay !!\n\nThe time for you to checkout and pay\nis less then 10 minutes after this message ("+new Date()+").');
    var displayEndstring="ending soon!"
	}
	
    if (timediff<0){ //if time is up
        this.timesup=true
        this.container.innerHTML=this.formatresults()
        return
    }
    var oneMinute=60 //minute unit in seconds
    var oneHour=60*60 //hour unit in seconds
    var oneDay=60*60*24 //day unit in seconds
    var dayfield=Math.floor(timediff/oneDay)
    var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
    var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
    var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
    if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
        hourfield=dayfield*24+hourfield
        dayfield="n/a"
    }
    else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
        minutefield=dayfield*24*60+hourfield*60+minutefield
        dayfield=hourfield="n/a"
    }
    else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
        var secondfield=timediff
        dayfield=hourfield=minutefield="n/a"
    }
    this.container.innerHTML=this.formatresults(dayfield, hourfield, minutefield, secondfield)
    setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}
 
/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////
 
//Create your own custom format function to pass into cdtime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left
 
//The values of these arguments may change depending on the "baseunit" parameter of cdtime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc

//-------------------------------------------------------------------------------------------- RECALCULATES CLOCK TIME:


/*
var d = new Date(); // create Date object for current location (of visitor)

var localTime = d.getTime(); // convert to msec since Jan 1 1970

// obtain local UTC offset and convert to msec
//var localOffsetMiliSec = d.getTimezoneOffset() * 60000;
var localOffsetSec = d.getTimezoneOffset() * 60;
// obtain UTC time in msec
//var utc = localTime + localOffsetMiliSec;
var utcs = Math.ceil((localTime + localOffsetSec)/1000);

document.write('local time: '+d+'<br>');
document.write('local time in sec sinds 1970: '+localTime+'<br><br>');
//document.write('UTC OFFSET in milisec: '+localOffsetMiliSec+'<br>');
document.write('UTC OFFSET in sec: '+localOffsetSec+'<br><br>');
//document.write('UTC time in milisec: '+utc+'<br>');
document.write('UTC time in sec: '+utcs+'<br>');
*/
//-----------------------------------------------
/*
// function to calculate local time
// in a different city
// given the city's UTC offset
function calcTime(city, offset) {

    // create Date object for current location
    d = new Date();
    
    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    
    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));
    
    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();

}

//document.write(calcTime('Rotterdam', '+2')+'<br>');
*/

//-------------------------------------------------------------------------------------------- END RECALCULATES CLOCK TIME:
 
function formatresults()
{
//---------------------------------------------------------------------------	
//var propID = document.form_time.pid.value
//var propID = document.getElementById('pid').value;
var D = arguments[0];
var H = arguments[1];
var M = arguments[2];
var S = arguments[3];

var dayNr = (D*24*60*60);
var hourNr = (H*60*60);
var minNr = (M*60);
var secNr = (S);

var total = dayNr+hourNr+minNr+secNr;

//---------------------------------------------------------------------------	
	
    if (this.timesup==false){//if target date/time not yet met
	
//        var displaystring=arguments[0]+" days "+arguments[1]+" hours "+arguments[2]+" minutes "+arguments[3]+" seconds left"

//    if(arguments[0] == '0'){
//        var displaystring="<img src='./imgs/time.gif' width='19' height='19' border='0' alt='duration auction for thyis property' style='position:relative; top:4px;' /> "+arguments[1]+"h "+arguments[2]+"m "
//		}else{
//		if(arguments[1] == '0'){
//        var displaystring="<img src='./imgs/time.gif' width='19' height='19' border='0' alt='duration auction for thyis property' style='position:relative; top:4px;' /> "+arguments[2]+"min "+arguments[3]+" sec"
//		}else{
//		if(arguments[2] == '0'){
//        var displaystring="<img src='./imgs/time.gif' width='19' height='19' border='0' alt='duration auction for thyis property' style='position:relative; top:4px;' /> "+arguments[3]+" seconds"
//		}else{
	
        var displaystring="<img src='./imgs/time.gif' width='19' height='19' border='0' alt='duration auction for this property' style='position:relative; top:4px;' /> "+D+"d "+H+"h "+M+"m "
		
//	    }
//	  }
//	}

	
	if(total <= (24*3600)){ 
//	alert('Please checkout and pay !!\n\nThe time for you to checkout and pay\nis less then 10 minutes after this message ("+new Date()+").');
    if(D == '0'){
    var displaystring="ending soon!<br><img src='./imgs/time.gif' width='19' height='19' border='0' alt='duration auction for this property' style='position:relative; top:4px;' /> "+H+"h "+M+"m "
		}
//		else{
		if(H == '0'){
        var displaystring="ending soon!<br><img src='./imgs/time.gif' width='19' height='19' border='0' alt='duration auction for this property' style='position:relative; top:4px;' /> "+M+"min "+S+" sec"
		}
//		else{
		if(M == '0'){
        var displaystring="ending soon!<br><img src='./imgs/time.gif' width='19' height='19' border='0' alt='duration auction for this property' style='position:relative; top:4px;' /> "+S+" seconds"
//	    }
//	  }
	}
  }
//---------------------------------------------------------------------------	
	}else { //else if target date/time met
        var displaystring="<img src='./imgs/time_out.gif' width='19' height='19' border='0' alt='this auction has expired' style='position:relative; top:4px;' />&nbsp;Expired !"
		
    }


    return displaystring
}
 
//-------------------------------------------------------------------------------------------- AJAX
/* 
function display_newpacks(product_id) { 
//        alert ("product_idID: "+product_id);
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null) {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="ajax_new_pack.php";
    url=url+"?productId="+product_id;
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
            document.getElementById('new_packs').innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

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; 
}  
*/
//-------------------------------------------------------------------------------------------- AJAX

