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) {
   preValues = new Array (Year, Month, Day, Hour, Minute, Second)

   numPartsPresent=0
   for (i=5; i>=0; i--) if (preValues[i]!=0) numPartsPresent++

   text=''
   if(preValues[1] != 0)
    {
        for(i = preValues[1]; i != 0; i--)
        {
            if((DTb[1] - i) == -1)
            {
                DT[0]--;
                DT[1] = 11;
            }
            preValues[2] += DaysPerMonth(DTb[0], DTb[1] - i);
        }
    }

    for(i=5; i>=0; i--)
    {
        if(preValues[i]!=0)
            numPartsPresent++;
    }

   tYear   = TimeToString (Year, 'год', 'года', 'лет')
   tMonth  = TimeToString (Month, 'месяц', 'месяца', 'месяцев')
   tDay    = TimeToString (preValues[2], 'день', 'дня', 'дней')
   tHour   = TimeToString (Hour, 'час', 'часа', 'часов')
   tMinute = TimeToString (Minute, 'минута', 'минуты', 'минут')
   tSecond = TimeToString (Second, 'секунда', 'секунды', 'секунд')
   preTexts = new Array (tYear, tMonth, tDay, tHour, tMinute, tSecond)
   	
	// -------------------
	
	for(i = 2; i <= 5; i++)
    {
        if(preValues[i]!=0)
        {
            if(text == '')
            {
                text = preValues[i] + ' ' + preTexts[i];
            }
            else
            {
                text = text + ', ' + preValues[i] + ' ' + preTexts[i];
            }
        }
    }



   return text
}

function OnTimer(mYear, mMonth, mDay, mHour, mMinute, mSecond)
{
    Now = new Date();
    nowYear=Now.getYear();
    if(nowYear<200)
        nowYear=nowYear+1900;

    DTa = new Array(nowYear, Now.getMonth() + 1, Now.getDate(), Now.getHours(), Now.getMinutes(), Now.getSeconds());

    DTb = new Array(
        mYear, // год
        mMonth, // месяц
        mDay, // день
        mHour,
        mMinute,
        mSecond);

    DTd = DiffDateTime(DTb, DTa);
	DaysLeftText (DTd[0],DTd[1],DTd[2],DTd[3],DTd[4],DTd[5]);

    $('#topass').text(text);
}
