var months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Jan")
var msecondssecond=(1000)
var msecondsminute=(msecondssecond*60)
var msecondshour=(msecondsminute*60)
var msecondsday=(msecondshour*24)
var msecondsweek=(msecondsday*7)
var averagemonthdays=(365.25/12)
var msecondsmonth=(msecondsday*averagemonthdays)
var msecondsyear=(msecondsmonth*12)

function start_counter(hr, mn, sc, mo, da, year, dir, beforetext, aftertext, eventname, compact, countername, notyears){
	if(!document.getElementById(countername)){document.write('<span id="'+countername+'" class="dynupdown"><\/span>')}
	new_counter(hr, mn, sc, mo, da, year, dir, beforetext, aftertext, eventname, compact, countername, notyears)
}

function zerotime(comp, prevperiod, value, singular, plural){
	if((comp=="yes" && prevperiod=="" && value==0) || (comp=="strict" && value==0)){ outperiod=""
	} else if(value==1){ outperiod=" "+value+ " "+singular+" "
	} else { outperiod=" "+value+ " "+plural+" "}
	return outperiod
}

function new_counter(hr, mn, sc, mo, da, year, dir, beforetext, aftertext, eventname, compact, countername, notyears){
	var today=new Date()
	var todayy=today.getYear()
	if (todayy < 1000){todayy+=1900}
	var todaym=today.getMonth()
	var todayd=today.getDate()
	var todayh=today.getHours()
	var todaymin=today.getMinutes()
	var todaysec=today.getSeconds()
	
	if(aftertext){secondspace=" "}else{secondspace=""}
	
	outyear=""; outmonth=""; outweek=""; outday=""; outhour=""; outminute=""; secondand=""; outsecond=""; switchword=""; notyearserror=0;
	
	if (year=="all"){yr=todayy} else {yr=year}
	
	todayparse=Date.parse(months[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec)
	dateparse=Date.parse(months[mo-1]+" "+da+", "+yr+" "+hr+":"+mn+":"+sc)
	
	if(dir=="switch" && (dateparse>todayparse)){
		countdir="down"
		if(aftertext==""){switchword=""}else{switchword="to "}
	} else if(dir=="switch" && (dateparse<todayparse)){
		countdir="up"
		if(aftertext==""){switchword=""}else{switchword="since "}
	} else if(dir=="switch" && (dateparse==todayparse)){
		countdir="down"
		if(aftertext==""){switchword=""}else{switchword="to "}
	} else {
		countdir=dir
		switchword=""
	}
	
	if(countdir=="down"){predd=dateparse-todayparse} else {predd=todayparse-dateparse}
	if (predd<0 && year=="all"){yr=todayy+1; dateparse=Date.parse(datestring=months[mo-1]+" "+da+", "+yr+" "+hr+":"+mn+":"+sc)}

	if(countdir=="down"){
		dd=dateparse-todayparse
		timeupmsg=eventname+" has passed"
	} else {
		dd=todayparse-dateparse
		timeupmsg=eventname+" is in the future"
	}
	
	if(dd<0){notime=1}else{notime=0}

	if(notyears==0){
		if(countdir=="up"){
			dyear=todayy-yr
			if(todayparse<Date.parse(months[mo-1]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){dyear=dyear-1}
			outyear=zerotime(compact, 0, dyear, "ano,", "anos,")

			if(todaym==(mo-1)){
				if(todayparse<Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){
					dmonth=11
				}else{
					dmonth=0
				}
			}else if(todaym<(mo-1)){
				dmonth=todaym+12-(mo-1)
				if(todayparse<Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){dmonth=dmonth-1}
			}else{
				dmonth=todaym-(mo-1)
				if(todayparse<Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){dmonth=dmonth-1}
			}
			outmonth=zerotime(compact, outyear, dmonth, "mes,", "meses,")
			if(todayparse<Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){
				if(todaym==0){
					dd=todayparse-Date.parse(months[11]+" "+da+", "+(todayy-1)+" "+hr+":"+mn+":"+sc)
				}else{
					dd=todayparse-Date.parse(months[todaym-1]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)
				}
			}else{
				dd=todayparse-Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)
			}
		}else{
			dyear=yr-todayy
			if(todayparse>Date.parse(months[mo-1]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){dyear=dyear-1}
			outyear=zerotime(compact, 0, dyear, "ano,", "anos,")
			if(todaym==(mo-1)){
				if(todayparse<Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){
					dmonth=0
				}else{
					dmonth=11
				}
			}else if(todaym<(mo-1)){
				dmonth=(mo-1)-todaym
				if(todayparse>Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){dmonth=dmonth-1}
			}else{
				dmonth=(mo-1)+12-todaym
				if(todayparse>Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){dmonth=dmonth-1}
			}
			outmonth=zerotime(compact, outyear, dmonth, "mes,", "meses,")
			if(todayparse<Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)){
				dd=Date.parse(months[todaym]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)-todayparse
			}else{
				if(todaym==11){todayy=todayy+1}
				dd=Date.parse(months[todaym+1]+" "+da+", "+todayy+" "+hr+":"+mn+":"+sc)-todayparse
			}
		}
		dday=Math.floor(dd/msecondsday)
		dhour=Math.floor((dd-(dday*msecondsday))/msecondshour)
		dminute=Math.floor((dd-(dday*msecondsday)-(dhour*msecondshour))/msecondsminute)
		dsecond=Math.floor((dd-(dday*msecondsday)-(dhour*msecondshour)-(dminute*msecondsminute))/msecondssecond)
		outday=zerotime(compact, outmonth, dday, "dia,", "dias,")
		outhour=zerotime(compact, outday, dhour, "hora,", "horas,")
		outminute=zerotime(compact, outhour, dminute, "minuto", "minutos")
		if(dsecond==1){outsecond=dsecond+" segundo"}else{outsecond=dsecond+" segundos"}
		if(outyear=="" && outmonth=="" && outday=="" && outhour=="" && outminute==""){secondand=""}else{secondand="e "}
		if(dateparse==todayparse){
			outyear=zerotime(compact, 0, 0, "ano,", "anos,")
			outmonth=zerotime(compact, outyear, 0, "mes,", "meses,")
			outday=zerotime(compact, outmonth, 0, "dia,", "dias,")
			outhour=zerotime(compact, outday, 0, "hora,", "horas,")
			outminute=zerotime(compact, outhour, 0, "minuto", "minutos")
			secondand=""
		}
	}else if(notyears==1){
		dday=Math.floor(dd/msecondsday)
		dhour=Math.floor((dd-(dday*msecondsday))/msecondshour)
		dminute=Math.floor((dd-(dday*msecondsday)-(dhour*msecondshour))/msecondsminute)
		dsecond=Math.floor((dd-(dday*msecondsday)-(dhour*msecondshour)-(dminute*msecondsminute))/msecondssecond)
		outday=zerotime(compact, 0, dday, "dia,", "dias,")
		outhour=zerotime(compact, outday, dhour, "hora,", "horas,")
		outminute=zerotime(compact, outhour, dminute, "minuto", "minutos")
		if(dsecond==1){outsecond=dsecond+" segundo"}else{outsecond=dsecond+" segundos"}
		if(outyear=="" && outmonth=="" && outday=="" && outhour=="" && outminute==""){secondand=""}else{secondand="e "}
	}else if(notyears==2){
		dweek=Math.floor(dd/msecondsweek)
		dday=Math.floor((dd-(dweek*msecondsweek))/msecondsday)
		dhour=Math.floor((dd-(dweek*msecondsweek)-(dday*msecondsday))/msecondshour)
		dminute=Math.floor((dd-(dweek*msecondsweek)-(dday*msecondsday)-(dhour*msecondshour))/msecondsminute)
		dsecond=Math.floor((dd-(dweek*msecondsweek)-(dday*msecondsday)-(dhour*msecondshour)-(dminute*msecondsminute))/msecondssecond)
		outweek=zerotime(compact, 0, dweek, "semana,", "semanas,")
		outday=zerotime(compact, outweek, dday, "dia,", "dias,")
		outhour=zerotime(compact, outday, dhour, "hora,", "horas,")
		outminute=zerotime(compact, outhour, dminute, "minuto", "minutos")
		if(dsecond==1){outsecond=dsecond+" segundo"}else{outsecond=dsecond+" segundos"}
		if(outyear=="" && outmonth=="" && outday=="" && outhour=="" && outminute==""){secondand=""}else{secondand="e "}
	}else{
		notyearserror=1
	}

	if(notime){
		document.getElementById(countername).className="dynupdown elapsed"
		document.getElementById(countername).innerHTML=timeupmsg
	}else if(notyearserror){
		document.getElementById(countername).innerHTML="Error: Invalid value for NOTYEARS"
	}else{
		document.getElementById(countername).innerHTML=beforetext+outyear+outmonth+outweek+outday+outhour+outminute+secondand+outsecond+secondspace+switchword+aftertext
	}
	
	setTimeout('new_counter("'+hr+'", "'+mn+'", "'+sc+'", "'+mo+'", "'+da+'", "'+year+'", "'+dir+'", "'+beforetext+'", "'+aftertext+'", "'+eventname+'", "'+compact+'", "'+countername+'", "'+notyears+'")',1000)
}