function IsLeapYear (Year) {
  return ((Year % 4) == 0) && (((Year % 100) != 0) || ((Year % 400) == 0))
}

function DaysPerMonth (Year, Month) {
   DaysInMonth = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

   if (Month == -1) Month = 11

   days=DaysInMonth [Month]
   if ((Month == 1) && IsLeapYear(Year)) {days++}
   return days
}

function DiffDateTime (DTa, DTb) {
   maxValues = new Array (0, 12, 0, 24, 60, 60)

   for (i=5; i>=0; i--) {
     if (DTa[i]<DTb[i] && i>0) {
        DTa[i-1]--
        DTa[i] += maxValues[i] - DTb[i]
        if (i==2) DTa[i] += DaysPerMonth (DTa[0], DTa[1])
     } else {
       DTa[i] -= DTb[i]
     }
   }

   return DTa
}

function TimeToString (value, str1, str2, str5) { 
   if (!value) return 0
   mod = value % 10
   if ((value%100)>=10 && (value%100)<=19) return str5
   if (mod == 1) return str1
   if (mod >= 2 && mod <= 4) return str2
   return str5
}  

function DaysLeftText (Year, Month, Day, Hour, Minute, Second) {

   tYear   = TimeToString (Year, 'год', 'года', 'лет')
   tMonth  = TimeToString (Month, 'месяц', 'месяца', 'месяцев')
   tDay    = TimeToString (Day, 'день', 'дня', 'дней')
   tHour   = TimeToString (Hour, 'час', 'часа', 'часов')
   tMinute = TimeToString (Minute, 'минута', 'минуты', 'минут')
   tSecond = TimeToString (Second, 'секунда', 'секунды', 'секунд')

   preValues = new Array (Year, Month, Day, Hour, Minute, Second)
   preTexts = new Array (tYear, tMonth, tDay, tHour, tMinute, tSecond)

   numPartsPresent=0
   for (i=5; i>=0; i--) if (preValues[i]!=0) numPartsPresent++

   text=''
   for (i=5; i>=0; i--) {
      if (preValues[i]!=0) {
         text=preValues[i]+' '+preTexts[i]+' '+text
         if (numPartsPresent>1) {
            text='и '+text
            numPartsPresent=0
         } 
      }
   }

   return text
}

function OnTimer () {
   Now = new Date()
   nowYear=Now.getYear ()
   if (nowYear<200) nowYear=nowYear+1900

   // Time passed since my birth
   DTa = new Array (nowYear, Now.getMonth (), Now.getDate (), Now.getHours (), Now.getMinutes (), Now.getSeconds ())
   DTb = new Array (1978, 6, 17, 12, 0, 0)
   DTc = DiffDateTime (DTa, DTb)

   // Time to my nearest birtday
   nextBDY=nowYear
   if ((Now.getMonth()>6) || ((Now.getMonth()==6) && (Now.getDate()>16))) nextBDY=nextBDY+1
   DTa = new Array (nowYear, Now.getMonth (), Now.getDate (), Now.getHours (), Now.getMinutes (), Now.getSeconds ())
   DTb = new Array (nextBDY, 6, 17, 12, 0, 0)

   DTd = DiffDateTime (DTb, DTa)

   document.dates.passed.value=DaysLeftText (DTc[0],DTc[1],DTc[2],DTc[3],DTc[4],DTc[5]);

   nowMonth=Now.getMonth()
   nowDay=Now.getDate()

   if (nowMonth=="6" && nowDay=="17")
      document.dates.topass.value="Ничего не осталось, поздравляем и дарим подарки!";
   else
      document.dates.topass.value=DaysLeftText (DTd[0],DTd[1],DTd[2],DTd[3],DTd[4],DTd[5]);
}

setInterval("OnTimer()",500);


