
//Used by default.asp, buy.asp (so far)
//NB: parent page must have a javascript variable called gNow 
//    (see default.asp and ajx4sale.asp to see how this is used)


function getTimeLeft(d,fmt){
var now=new Date(),d2,a,b,c,txt;
var mth,day,yr,hr,mn,sec;
var mth2,day2,yr2,hr2,mn2,sec2;
var diff,ydif,mdif,ddif,hdif,ndif,sdif;
d2=getJavaDate(d);
now=getJavaDate(gNow);
diff=d2.getTime()-now.getTime();
if(diff < 1)return "0";
ddif = Math.round((((diff / 1000) / 60) / 60) / 24);
diff-=ddif*24*60*60*1000;
hdif = Math.round(((diff / 1000) / 60) / 60);
diff-=hdif*60*60*1000;
mdif = Math.round((diff / 1000) / 60);
diff-=mdif*60*1000;
sdif = Math.round(diff / 1000);
if(sdif<0){mdif--;sdif=60+sdif;}
if(mdif<0){hdif--;mdif=60+mdif;}
if(hdif<0){ddif--;hdif=24+hdif;}
txt="";
if(!fmt){
 if(ddif)txt+=ddif+"<font color=gray>d</font>";
 if(ddif||hdif)txt+="&nbsp;"+hdif+"<font color=gray>h</font>";
 txt+="&nbsp;"+mdif+"<font color=gray>m</font>";
 if(!ddif&&!hdif)txt+="&nbsp;"+sdif+"<font color=gray>s</font>";
}else{
 if(ddif){
  txt+=ddif+"<small>&nbsp;day";
  if(ddif>1)txt+="s";
  txt+=",&nbsp;</small>";
 }
 if(ddif||hdif){
  txt+=""+hdif+"<small>&nbsp;hour";
  if(hdif!=1)txt+="s";
  txt+=",&nbsp;</small>";
 }
 txt+=""+mdif+"<small>&nbsp;minute";
 if(mdif!=1)txt+="s";
 if(!ddif&&!hdif){
  txt+=",&nbsp;</small>"+sdif+"<small>&nbsp;second";
  if(sdif!=1)txt+="s";
  txt+="</small>";
 }
 else txt+="</small>";
}return txt;}


function getJavaDate(d){
var a,b,c,dt=new Date();
var mth,day,yr,hr,mn,sec;
a=d.split("/");
b=a[2].split(" ");
c=b[1].split(":");
yr=parseInt(b[0]);
mth=parseInt(a[0])-1;
day=parseInt(a[1]);
hr=parseInt(c[0]);
mn=parseInt(c[1]);
sec=parseInt(c[2]);
if(b[2]!="AM"&&hr!=12)hr+=12;
dt.setSeconds(sec);
dt.setMinutes(mn);
dt.setHours(hr);
dt.setDate(day);
dt.setMonth(mth);
dt.setFullYear(yr);
return dt;}



