var da,mo,yr,ho,mi,se;
var countdownwidth='130px';
var countdownheight='20px';
var dd;
var daystring, hourstring, minutestring, secondstring, endofauctionstring, classcount;

function CountDownClock(endtime, servtime, daystr, hourstr, minutestr, secondstr, endofauction ,countclass)
{
	classcount = countclass;
	daystring = daystr;
	hourstring = hourstr;
	minutestring = minutestr;
	secondstring = secondstr;
	endofauctionstring = endofauction;

	if(document.getElementById)
	{
		document.write('<div id="countdowndiv" class=bgcol1></div>')
	}
	
	dd=Date.parse(endtime)-Date.parse(servtime);
	window.onload=start_countdown;
}

function start_countdown()
{
	function resized()
	{
		if(pageWidth!=innerWidth||pageHeight!=innerHeight)
		{
			location.reload();
		}
	}

	if(document.layers)
	{
		pageWidth=innerWidth;
		pageHeight=innerHeight;
		window.onresize=resized;
		document.countdownnsmain.visibility="show";
	}

	countdown();
}

function countdown()
{

	var opentags='<CLASS=regemp>'
	var tabletags="<TABLE CLASS=bgcol1 Border=0>"
	
	if(dd>=1000){
		dday =Math.floor(dd/(60*60*1000*24)*1)
		dddhour =Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
		hdhour= Math.floor(((60*60*1000*24))/(60*60*1000)*1) * dday;
		dhour = dddhour+ hdhour;
		dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
		dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
		
		if(dday<=0&&dhour<=0&&dmin<=0&&dsec<1)
		{}
		else 
		{
			if(document.getElementById)
			{
				if(dhour<=9) {dhour= '0'+dhour;}
				if(dmin<=9) {dmin= '0'+dmin;}
				if(dsec<=9) {dsec= '0'+dsec;}
				content=tabletags;
				if(classcount == 'large_countdown'){
					content+="<tr CLASS=bgcol1><td align=left CLASS=large_countdown>";
				}else{
				    content+="<tr CLASS=bgcol1><td align=left CLASS=timetext>";
				}
				if (dhour != 0 || dmin != 0 || dsec != 0){
					content+=" <b>"+dhour+"</b>:";
				}else{
					dhour = '00';
					content+=" <b>"+dhour+"</b>";
				}
				if (dmin != 0 || dsec != 0){
					content+=" <b>"+dmin+"</b>:";
				}else{
					dmin = '00';
					content+=" <b>"+dmin+"</b>";
				}
				if (dsec != 0){
					content+=" <b>"+dsec+"</b>";
				}else{
					dsec = '00';
					content+=" <b>"+dsec+"</b>";
				}
				content+="</td></tr></table>";
				rng=document.createRange();
				el=document.getElementById('countdowndiv');
				rng.setStartBefore(el);
				htmlFrag=rng.createContextualFragment(content)
		
				while(el.hasChildNodes())
					el.removeChild(el.lastChild);
			
				el.appendChild(htmlFrag);
			}
		}
		
		dd=dd-1000;
		setTimeout("countdown()",1000);
	}
	else
	{
		if(document.getElementById)
		{
			content="<!-- invalid dd=" + dd + " -->";
			content=content+tabletags;
			content=content+"<tr CLASS=bgcol1><td align=center CLASS=reg><b>"+endofauctionstring+"</b></td></tr></table>"
			
			rng=document.createRange();
			el=document.getElementById('countdowndiv');
			rng.setStartBefore(el);
			htmlFrag=rng.createContextualFragment(content)

			while(el.hasChildNodes())
				el.removeChild(el.lastChild);
			
			el.appendChild(htmlFrag);
		}
			
		return
	}
}

function extcountdown(countdownid,fromhome)
{
	var opentags='<CLASS=regemp>'
	var tabletags="<TABLE CLASS=bgcol1 Border=0>"
	
	if(dd>=1000){
		dday =Math.floor(dd/(60*60*1000*24)*1)
		dddhour =Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
		hdhour= Math.floor(((60*60*1000*24))/(60*60*1000)*1) * dday;
		dhour = dddhour+ hdhour;
		dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
		dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
		
		if(dday<=0&&dhour<=0&&dmin<=0&&dsec<1)
		{}
		else{
			
			if(document.getElementById){
				if(dhour<=9) {dhour= '0'+dhour;}
				if(dmin<=9) {dmin= '0'+dmin;}
				if(dsec<=9) {dsec= '0'+dsec;}
				content=tabletags;
				content+="<tr CLASS=bgcol1><td align=left CLASS=timetext>";
				if (dhour != 0 || dmin != 0 || dsec != 0)
					content+=" <b>"+dhour+"</b>:";
				if (dmin != 0 || dsec != 0)
					content+=" <b>"+dmin+"</b>:";
				if (dsec != 0)
					content+=" <b>"+dsec+"</b>";
				content+="</td></tr></table>";
				document.getElementById(countdownid).innerHTML  = content;
				if(fromhome == 1){		
				setTimeout(function(){redcountdown(countdownid)},10);
				dd=dd-1000;
				setTimeout(function(){extcountdown(countdownid,0)},3000);
				}else{
				setTimeout(function(){extcountdown(countdownid,0)},1000);
				}
				dd=dd-1000;
			}
		}
	}else{
		if(document.getElementById){
			content="<!-- invalid dd=" + dd + " -->";
			content=content+tabletags;
			content=content+"<tr CLASS=bgcol1><td align=center CLASS=reg><b><font color=red>Closed</font></b></td></tr></table>"
			document.getElementById(countdownid).innerHTML  = content;
		}
			
		return
	}
		
}

function redcountdown(countdownid)
{

	var opentags='<CLASS=regemp>'
	var tabletags="<TABLE CLASS=bgcol1 Border=0>"
	if(dd>=1000){
		dday =Math.floor(dd/(60*60*1000*24)*1)
		dddhour =Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
		hdhour= Math.floor(((60*60*1000*24))/(60*60*1000)*1) * dday;
		dhour = dddhour+ hdhour;
		dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
		dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
		if(dday<=0&&dhour<=0&&dmin<=0&&dsec<1)
		{}
		else{
			
			if(document.getElementById){
				if(dhour<=9) {dhour= '0'+dhour;}
				if(dmin<=9) {dmin= '0'+dmin;}
				if(dsec<=9) {dsec= '0'+dsec;}
				content=tabletags;
				content+="<tr CLASS=bgcol1><td align=left CLASS=timered>";
				if (dhour != 0 || dmin != 0 || dsec != 0)
					content+=" <b>"+dhour+"</b>:";
				if (dmin != 0 || dsec != 0)
					content+=" <b>"+dmin+"</b>:";
				if (dsec != 0)
					content+=" <b>"+dsec+"</b>";
				content+="</td></tr></table>";
				document.getElementById(countdownid).style.ClassName = "timered";
				document.getElementById(countdownid).innerHTML  = content;
			}
		}
	}else{
		if(document.getElementById){
			content="<!-- invalid dd=" + dd + " -->";
			content=content+tabletags;
			content=content+"<tr CLASS=bgcol1><td align=center CLASS=reg><b><font color=red>Closed</font></b></td></tr></table>"
			document.getElementById(countdownid).innerHTML  = content;
		}
			
		return
	}
		
}
