var da,mo,yr,ho,mi,se;
var dd;
var dateq;
var stop_countdown=0;

function SetDD(ms, closed) {
	
	var agt=navigator.userAgent.toLowerCase();		
	isclosed = closed ? 1 : 0;
	if (dd<1)  
	setTimeout("countdown("+isclosed+")",1);
		
	dd = ms;
}


function setNewTime(NewTime) {
	dd = NewTime;
	}

function CountDownClock(endtime,servtime){
	var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	//var dateq=endtime;
	var datetoday=(servtime);
	dateq=endtime;
	yr=dateq.substring(0,4);
	mo=dateq.substring(5,7);
	da=dateq.substring(8,10);
	ho=dateq.substring(11,13);
	mi=dateq.substring(14,16);
	se=dateq.substring(17,19);
	var ampm=dateq.substring(17,19);
	ampm=ampm.toUpperCase();
	
	if(ampm=="PM") {
		if(ho!=12) {
			ho=parseInt(ho,10);
			ho=ho+12;
		}
	} else {
		if(ampm=="AM") {
			if(ho==12) {
				ho=0;
			}
		}
	}
	
	var qdatestring=da+","+mo+","+yr+","+ho+","+mi+","+se;
	
	/*
	if(document.all){
		document.write('<span id="countdownie"></span>')
	}
*/
	/*if(document.getElementById){
		document.write('<div id="countdowndiv"></div>')
	}*/
	
	futurestring=montharray[mo-1]+" "+da+" "+yr+" "+ho+":"+mi+":"+se;
	
	if(datetoday==0){
		today=new Date();
		today=today+clockOffset;
		todayy=today.getYear();
		if(todayy<1000){
			todayy+=1900;
		}
		todaym=today.getMonth();
		todayd=today.getDate();
		todayh=today.getHours();
		todaymin=today.getMinutes();
		todaysec=today.getSeconds();
		todaystring=montharray[todaym]+" "+todayd+" "+todayy+" "+todayh+":"+todaymin+":"+todaysec;
	}
	else{
		todayy=datetoday.substring(0,4);
		todaym=datetoday.substring(5,7);
		todayd=datetoday.substring(8,10);
		todayh=datetoday.substring(11,13);
		todaymin=datetoday.substring(14,16);
		todaysec=datetoday.substring(17,19);
		todaystring=montharray[todaym-1]+" "+todayd+" "+todayy+" "+todayh+":"+todaymin+":"+todaysec;
	}
	
	dd=(Date.parse(futurestring)-Date.parse(todaystring));

	//start_countdown();
}

function start_countdown(){
	function resized(){
		if(pageWidth!=innerWidth||pageHeight!=innerHeight){
			location.reload();
		}
	}
	
	/*if(document.layers){
	  alert("sfaas2");
		pageWidth=innerWidth;
		pageHeight=innerHeight;
		window.onresize=resized;
		document.countdownnsmain.visibility="show";
	}*/
	
//	isclosed = (dd <= 1000) ? 1:0;
	isclosed = (dd <= 1000) ? 1:0;
	
	countdown( isclosed );
}


function countdown( isclosed ){
	if(dd>=1000){
		dday=Math.floor(dd/(60*60*1000*24)*1)
		dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
		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<=0){}
		else{
			/*if(document.layers){
				document.countdownnsmain.document.countdownnssub.document.write(dday+"&nbsp;p&auml;eva,&nbsp;"+dhour+"&nbsp;h,&nbsp;"+dmin+"&nbsp;min,&nbsp;"+dsec+"&nbsp;s");
				document.countdownnsmain.document.countdownnssub.document.close();
			}
			else if(document.all){
				HTMLdisp=dday+"&nbsp;p&auml;eva&nbsp;"+dhour+"&nbsp;h&nbsp;"+dmin+"&nbsp;min&nbsp;"+dsec+"&nbsp;s";
				countdownie.innerHTML=HTMLdisp;
			}
			else*/
			if(document.getElementById){
				content="<span class=\"bluetext14\">"+dday+"&nbsp;p,&nbsp;"+dhour+"&nbsp;h,&nbsp;"+dmin+"&nbsp;min,&nbsp;"+dsec+"&nbsp;s"+"</span>";
				/*rng=document.createRange();
				el=document.getElementById('countdowndiv');
				rng.setStartBefore(el);
				htmlFrag=rng.createContextualFragment(content);
				while(el.hasChildNodes())
					el.removeChild(el.lastChild);
				el.appendChild(htmlFrag);*/
				parent.document.getElementById('countdowndiv').innerHTML=content;
			}
		}
		dd=dd-1000;

		if (stop_countdown==1)
		{
			exit();
		}

		setTimeout("countdown()",995);

	}
	else if (dd==0)
	{
		if(document.getElementById){
			content="<b>Palun oodake.</b>";
			/*rng=document.createRange();
			el=document.getElementById('countdowndiv');
			rng.setStartBefore(el);
			htmlFrag=rng.createContextualFragment(content);
			while(el.hasChildNodes())
				el.removeChild(el.lastChild);
			el.appendChild(htmlFrag);*/
			parent.document.getElementById('countdowndiv').innerHTML=content;
		}

		setTimeout("countdown()",3000);

	}
	else{

		/*
		if(document.layers){
			document.countdownnsmain.document.countdownnssub.document.write("<b>Oksjon l&otilde;ppenud!</b>")
			document.countdownnsmain.document.countdownnssub.document.close();
		}
		else if(document.all){
			HTMLdisp="<b>Oksjon l&otilde;ppenud!</b>";
			countdownie.innerHTML=HTMLdisp
		}
		else */if(document.getElementById){
			content="<b>Oksjon on l&otilde;ppenud!</b>";
			/*rng=document.createRange();
			el=document.getElementById('countdowndiv');
			rng.setStartBefore(el);
			htmlFrag=rng.createContextualFragment(content);
			while(el.hasChildNodes())
				el.removeChild(el.lastChild);
			el.appendChild(htmlFrag);*/
			parent.document.getElementById('countdowndiv').innerHTML=content;
			parent.location.reload();
		}
	return
	}
}

