//*****************************************************
// This script will calculate the age of the society 
// based on the current date, and return the date in
// Anno Socieitas format.
//
// Copyright 2004, William J. Knight
// All rights reserved.
//
// SCA Webministers may contact Lord Conal MacNachtan 
// at william.j.knight@verizon.net for permission 
// to utilize this script 
// *****************************************************

function getAS() {
    // First get the current date and parse it into sections
    now = new Date();
    yearNow = now.getFullYear();
    monthNow = now.getMonth();
    dateNow = now.getDate();
    hoursNow = now.getHours();
    minutesNow = now.getMinutes();
    secondsNow = now.getSeconds();

    // Assemble the time    
    timeStr = hoursNow;
    timeStr += ((minutesNow < 10) ? ":0" : ":") + minutesNow;
    timeStr += ((secondsNow < 10) ? ":0" : ":") + secondsNow;


    // We'll need a few arrays to handle translations.
    monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")	
    onesArray = new Array("I","II","III","IV","V","VI","VII","VIII","IX");
    tensArray = new Array("X","XX","XXX","XL","L","LX","LXX","LXXX","XC");
    hundredsArray = new Array("C","CC","CCC","CD","D","DC","DCC","DCCC","CM");
	
    // Step one, calculate the age of the society
	
    // First, set yearAS to the current year less 1965 
    yearAS = yearNow - 1965;

    // if the current month is >= 4, Subtract 4 from the current month
    // otherwise subtract 1 from yearAS and add 12 to the current month - 4
    if (monthNow >= 4)
    monthAS = monthNow - 4;
    else {
        yearAS--;
        monthAS = 12 + monthNow - 4;
    }
    // if the current date is >= 1 set dateAS to the current date - 1
    // otherwise subrtact one from monthAS and add 31 to the current date -1
    if (dateNow >= 1)
        dateAS = dateNow - 1;
    else {
        monthAS--;
        dateAS = 31 + dateNow - 1;
        // if this forces the month <0 set the month to december
        // and subtract one from the yearAS
        if (monthAS < 0) {
           monthAS = 11;
           earAS--; 
        }
    }
    // set num = yearAS 
    num = yearAS
	
    // determine the values for 1s, 10a, and 100s
    ones = num % 10;
    num = (num - ones) / 10;
    tens = num % 10;
    num = (num - tens) / 10;
    hundreds = num % 10;
    num = (num - hundreds) / 10;

    Roman = "";
   
    // add "M"s until were out of 1000s (1-3)
    for (i=0; i < num; i++){Roman += "M";}
    // Use the array to dispay the appropriate symbol for each decimal position
    if (hundreds) {Roman += hundredsArray[hundreds-1];}
    if (tens) {Roman += tensArray[tens-1];}
    if (ones) {Roman += onesArray[ones-1];}

    suffix = "th"
    if (dateNow == 1) {suffix = "st";}
    if (dateNow == 21) {suffix = "st";}
    if (dateNow == 31) {suffix = "st";}
    if (dateNow == 2) {suffix = "nd";}
    if (dateNow == 22) {suffix = "nd";}
    if (dateNow == 3) {suffix = "rd";}
    if (dateNow == 23) {suffix = "rd";}
	
    // document.write ('The society is ' + yearAS + " years, " + monthAS + " Months, and " + dateAS + " Days old.")	
    // document.write (monthArray[monthNow] + " " + dateNow + ", A.S. "+ Roman + ", being " + yearNow+ " Gregorian.")
    document.write (monthArray[monthNow] + " " + dateNow + "<SUP>" + suffix + "</SUP>" + ", A.S. " + Roman + " at " + timeStr)
}
