// vscripts.js - use for index.html and vcalc.html

// VARIABLES

// vision titles
v = new Array("0, SPIRIT","1, KINDLER","2, MISTRESS of MAGIC","3, RIVER-DAUGHTER","4, ELDEST","5, LORE-GIVER","6, GIFT","7, COMMAND","8, TREE","9, PILGRIM","10, MIRROR","11, COUNSEL","12, BRIDGE","13, GATE","14, EVENSTAR","15, CHAINS of GOLD","16, TOWER","17, MOON","18, STAR","19, SUN","20, WORLD","21, WEST","22, SHADOW","23, POWER","24, EAST","25, SOUTH","26, NORTH","27, METAL","28, IRON","29, HOOK","30, BREEZE","31, BOOK","32, SINDARIN BREEZE","33, MOUTH","34, JAWS","35, TREASURE")

// vision text
vt = new Array()
vt[0]="I am the spirit of unchannelled potential at the heart of all beginnings"
vt[1]="I am the guide, showing you how to channel energy and ideas towards realising your goals"
vt[2]="I am the Mistress, wise fearless and fair, guiding through intuition and spirituality to resist attack, to nurture and protect"
vt[3]="I bring you the safety of haven-home where you may rest awhile from the trials and dangers of your journey - yet not forever"
vt[4]="I am Master of the Wild Magics, irrational and untamed, protector and friend to those in need"
vt[5]="I teach the path that lies before your feet and the burden you must bear along the road"
vt[6]="I am the gift and burden of choice. Who can tell in full the outcome of his choosing?"
vt[7]="I am the point of balance within you, upon which the world may turn"
vt[8]="Slow to endure, yet I am mighty in anger. Channel the forces that pulse within you"
vt[9]="I am your light and guide upon the road"
vt[10]="I am the mirror that shows the past, present and future. Beware."
vt[11]="I am the right unfolding of people, place and moment"
vt[12]="I am the bridge of transition: sacrifice without hesitation, hope or regret"
vt[13]="I am the gate through which all may pass at the right time - if you have the key"
vt[14]="I am the duty of doomed promise, faithful to the consequence of irrevocable choice"
vt[15]="I am the chain of sweet entrapment"
vt[16]="I am the tower of marvellous form in which you must confront the walls that surround your world"
vt[17]="I am the moon of entrapment, witness to your helpless delusion"
vt[18]="I am the light of hope in dark places, flaming like a star"
vt[19]="I am the sun that rides above all shadows, fear and desires"
vt[20]="I am the world - the stage upon which all your lives are played out, anvil of the gods"
vt[21]=""
vt[22]="I am the shadow that lies within and behind all that you are and may yet be"
vt[23] = ""
vt[24] = ""
vt[25] = ""
vt[26] = ""
vt[27] = ""
vt[28] = ""
vt[29] = ""
vt[30] = ""
vt[31] = ""
vt[32] = ""
vt[33] = ""
vt[34] = ""
vt[35] = ""

days = new Array(0,0,31,59,90,120,151,181,212,243,273,304,334)


// FUNCTIONS

// return reduced date total for ddmmyyyy as string
function getVision(inputDate) {

  // split date
  dayStr = inputDate.substring(0,2);  
  monStr = inputDate.substring(2,4);
  yrStr  = inputDate.substring(4,8);

  // convert to integers (needs ,10 to work properly)
  day = parseInt(dayStr,10);
  mon = parseInt(monStr,10);
  yr  = parseInt(yrStr,10);

  // test for leap
  testForLeap(yr);

  // calculate components of date total
  doy = days[mon]+day

  if (doy>59){
    doy = doy+leap
  }
  if (yr>2000) {
   yoa=yr-2000
   aow=7
  }
  else {
    yoa=yr
    aow=6
  }

  // compute date total
  var dt = doy + yoa + aow

  // reduce if >35
  if (dt>35) {
    reduce(dt)
    dt=reduced
  }
  // reduce again if still >35
  if (dt>35) {
    reduce(dt)
    dt=reduced
  }

  // return the value of the date total (0-35)
  return dt
  
} // end of getVision()


// reduce DT
function reduce(xxx) {
  a = Math.floor(xxx/1000);
  b = Math.floor((xxx-(a*1000))/100);
  c = Math.floor((xxx-(a*1000)-(b*100))/10);
  d = Math.floor((xxx-(a*1000)-(b*100)-(c*10))/1);
  reduced = a+b+c+d;
}


// test for leap
function testForLeap(yr) {
  t1 = yr/4 - Math.floor(yr/4)
  t2 = yr/100 - Math.floor(yr/100)
  t3 = yr/400 - Math.floor(yr/400)
  leap = false
  if (t1==0) {leap = true}
  if (t2==0) {leap = false}
  if (t3==0) {leap = true}
}


// validate a date
function validDate(inputDate){     // ddmmyy as string

   // split date
   dayStr = inputDate.substring(0,2);  
   monStr = inputDate.substring(2,4);
   yrStr  = inputDate.substring(4,8);

   // convert to integers
   day = parseInt(dayStr,10)
   mnth = parseInt(monStr,10)
   yrInt  = parseInt(yrStr,10)
  
   // test for leap
   testForLeap(yrInt);

   // validate number of days in month
   isValid = true
   if (mnth ==  1 && day > 31){isValid = false}
   if (mnth ==  2 && day > (28 + leap) ){isValid = false}
   if (mnth ==  3 && day > 31){isValid = false}
   if (mnth ==  4 && day > 30){isValid = false}
   if (mnth ==  5 && day > 31){isValid = false}
   if (mnth ==  6 && day > 30){isValid = false}
   if (mnth ==  7 && day > 31){isValid = false}
   if (mnth ==  8 && day > 31){isValid = false}
   if (mnth ==  9 && day > 30){isValid = false}
   if (mnth == 10 && day > 31){isValid = false}
   if (mnth == 11 && day > 30){isValid = false}
   if (mnth == 12 && day > 31){isValid = false}

   return isValid
}


// get vision for today's date
function getTodaysVision() {

  now = new Date()

  todayDay = now.getDate()     
  todayMm = now.getMonth()+1
  // next fn JS1.2
  todayYyyy  = now.getFullYear()

  // build as string ddmmyyyy 
  todayDdStr = todayDay.toString(10);
  todayMmStr = todayMm.toString(10);
  todayYyyyStr = todayYyyy.toString(10);

  // pad if 1-9
  if(todayDdStr.length == 1) {todayDdStr = '0'+todayDdStr}
  if(todayMmStr.length == 1) {todayMmStr = '0'+todayMmStr}

  todayDdMmYyyyStr = todayDdStr+todayMmStr+todayYyyyStr

  // calculate vision for today. Returns vision number (int)
  todaysVision = getVision(todayDdMmYyyyStr)

} // end of getTodaysVision()


// EXECUTE STUFF

// call function to calculate vision number (dt) for current date 
getTodaysVision()

// build text to be written to index.html page
visionStr = "<DIV CLASS='small'><BR>Today\'s <A HREF='dayvisions.html'>day-vision<\/A> is <B>"+v[todaysVision]+"<\/B><\/DIV>"
if (vt[todaysVision] != "") {
  visionStr = visionStr+"<DIV ALIGN='right' CLASS='small'>&nbsp;&nbsp;"+vt[todaysVision]+"<\/DIV>"
}


