/* Republican Calendar Widget 1.3
     Copyright 2007 Jose Luis Martin Mas

*/

var timer = null //Create the timer that controls the hour as a global for onshow and onhide being able to manipulate it

var meses = new Array("Vendémiaire","Brumaire","Frimaire","Nivôse","Pluviôse","Ventôse","Germinal","Floréal","Prairial","Messidor","Thermidor","Fructidor","Sans-culottide")
var sansculottides= new Array("Fête de la Vertu","Fête du Génie","Fête du Travail","Fête de l'Opinion","Fête des Récompenses","Fête de la Révolution")
var diassemana = new Array("Primidi","Duodi","Tridi","Quartidi","Quintidi","Sextidi","Septidi","Octidi","Nonidi","Décadi")

function tdecimal()
{
var hora=new Date()
var h=hora.getHours()
var m=hora.getMinutes()
var s=hora.getSeconds()
segsdtotales=(s+(m*60)+(h*3600))*1.15740 //1 second = 1.15740 decimal seconds
hd=Math.floor(segsdtotales/10000)
md=Math.floor((segsdtotales % 10000)/100)
sd=Math.floor(segsdtotales % 100)
hd=ponercero(hd)
md=ponercero(md)
sd=ponercero(sd)
var ponerhora = document.getElementById("Textohora").innerHTML="Decimal time<br>"+hd+":"+md+":"+sd
t=setTimeout('tdecimal()',500)  
}

function ponercero(i) //just adding a left zero when mins and secs are 1 char
{
if (i<10) 
	{
	i="0" + i
	}
return i
}


function romano(anyo)
{
//this is not a full conversion to roman numerals function
//since I don't expect this javascript to work for thousands of years, 
//I assume today's year is never going to be a number with more or less than 3 digits

var unirom = new Array("","I","II","III","IV","V","VI","VII","VIII","IX");
var decrom = new Array("","X","XX","XXX","XL","L","LX","LXX","LXXX","XC");
var centrom = new Array("","C","CC","CCC","CD","D","DC","DCC","DCCC","CM");

centena = (anyo - (anyo % 100))/100
anyo = (anyo % 100)
decena = (anyo - (anyo % 10))/10
uni = (anyo % 10)

return(centrom[centena]+decrom[decena]+unirom[uni])
}

function fecharep()
{
var fechagreg=new Date()
var anyogreg = fechagreg.getFullYear()
var mesgreg = (fechagreg.getMonth())+1
//getMonth returns month from 0-11. I add 1 to get a more human-readable result
var diagreg = fechagreg.getDate()
var anyorev = anyogreg-1792

//The Revolutionary Calendar decree didn't make it clear which years were leap years
//There's still an open debate about the correct rule
//After checking several sources, I've decided to follow the 4/128 rule
//March 2007: John Hynes's (decimaltime.hynes.net) has offered me really throrough and good explanations of failures in my calculations
//and he has made me notice that with the 4/128 rule, all Republican years since Gregorian 1920 till Gregorian 2047 should begin on Sep. 23rd
//Considering this and that I don't think this software will be working after 2047 :) I've decided to remove my leap years calculations
//and statically fix the first day date on 23rd

var primerdia = 23
var mesprimerdia = 09

//we must check if Gregorian date is past the Rev New Year included in
//that Gregorian year, since then we have to add 1 to the Rev Year calculation
//This also let us know in which Gregorian year was Rev New Year of the date
//to calculate, something useful later

if (((mesgreg-mesprimerdia) >= 1) || (((mesgreg-mesprimerdia) == 0) && ((diagreg-primerdia) >= 0)))
	{
	anyorev++
	anyoprimerdia = anyogreg
	}
else
	{
	anyoprimerdia = anyogreg -1
	}

//now that we're sure about the Rev year, we convert it to Roman numerals
//following the Edict regulations

var anyorevrom = romano(anyorev)

//now we calculate how many days have passed since Rev New Year till today

var d1 = new Date()
mespri = mesprimerdia-1 //month in a Date Object goes from 0  to 11
milisecsdia = 86400000 //the days difference will be in milliseconds
d1.setFullYear(anyoprimerdia,mespri,primerdia)
difdias = (parseInt((fechagreg-d1)/milisecsdia))+1

//Final calculations for month and day

var mesindex = (parseInt(difdias/30))+1
var diarev = difdias % 30
if (diarev == 0) //30th day of month
	{
	diarev = 30
	mesindex = mesindex -1 //30th day --> mesindex "jumps" to next month
	}
var mesrev =  meses[mesindex-1] //Arrays begin in 0. Dumb comment, I know... :)
if (diarev <= 10)
	{
	var decade = 1
	var diasem = diarev
	}
else if ((diarev > 10) && (diarev <= 20))
	{
	var decade = 2
	var diasem = diarev - 10
	}
else
	{
	var decade = 3
	var diasem = diarev - 20
	}
diasemana = diassemana [diasem-1]

var ponerfecha = document.getElementById("Textofecha").innerHTML=diarev+" "+mesrev+" "+anyorevrom 
if (mesindex == 13) //Sans-culottide
	{
	var ponersans= document.getElementById("Textosemana").innerHTML=sansculottides[diarev-1]
	}
else
	{
	var ponersem = document.getElementById("Textosemana").innerHTML=diasemana+", Décade "+decade
	}
}

