//--------------------------------------------------
// FILE:    formatDate.js
// PURPOSE: Contains assorted date-related functions
// NOTE:    Just a reminder, we're dealing w/ months
//          as 0-11 here.  EMP and many of our web
//          interfaces use 1-12.  Be sure to account
//          for the offset before passing values here.
//--------------------------------------------------



//--------------------------------------------------
// FUNCTION: IsLeapYear
// PURPOSE:  Checks whether a given year is a leap year
// PARAM:    year (integer)
//--------------------------------------------------
function IsLeapYear(year){
  // Ensure we're dealing w/ integer
  intYear = parseInt(year,10);

  if(intYear % 4 != 0){
    // Not divisible by 4, so not a leap year
    return false;
  }else if(intYear % 400 == 0){
    // Divisible by 400, so it is a leap year
    return true;
  }else if(intYear % 100 == 0){
    // Divisible by 100 but not by 400, so not a leap year
    return false;
  }else{
    // Divisible by 4, so it is a leap year
    return true;
  }
}

//--------------------------------------------------
// FUNCTION: IsValidMonth
// PURPOSE:  Checks validity of a given month value
// PARAM:    month (integer)
//--------------------------------------------------
function IsValidMonth(month){
  if(parseInt(month,10) < 0 || parseInt(month,10) > 11){
    return false;
  }else{
    return true;
  }
}

//--------------------------------------------------
// FUNCTION: IsValidDate
// PURPOSE:  Checks validity of a given date 
// PARAM:    month (integer)
// PARAM:    date (integer)
// PARAM:    year (integer)
// PARAM:    verbose (boolean, optional -- defaults true)
//--------------------------------------------------
function IsValidDate(month, date, year, verbose){
  // Ensure that we're dealing w/ integers
  var intMonth = parseInt(month,10);
  var intDate = parseInt(date,10);
  var intYear = parseInt(year,10);
  
  if (isNaN(intMonth) || isNaN(intDate) || isNaN(intYear)){
    return false;
  }

  // See whether we're overriding verbose mode
  var showErrors = true;
  if(verbose != null && verbose == false){
    showErrors = false;
  }

  if(!IsValidMonth(intMonth)){
    if(showErrors){
      alert("Please enter a valid month.(" + intMonth + ")"); 
    }
    return false;
  }
  
  // Check for 31 days in a month w/ 30 days
  if((intMonth == 3 || intMonth == 5 || intMonth == 8 || intMonth == 10) && intDate == 31){
    if(showErrors){
      alert(GetMonthName(intMonth) + " 31-дневный месяц.  Пожалуйста, укажите правильную дату."); 
    }
    return false;
  }

  // Handle February
  if(intMonth == 1){
    if(intDate > 29){
      if(showErrors){
        alert(GetMonthName(intMonth) + " не состоит из " + intDate + " дней.  Пожалуйста, укажите правильную дату."); 
      } 
      return false;
    }else if(!IsLeapYear(intYear) && intDate == 29){
      if(showErrors){
        alert(intYear + " не високосный год.  Пожалуйста, укажите правильную дату."); 
      }
      return false;
    }
  }

  return true;
}

//--------------------------------------------------
// FUNCTION: GetMonthName
// PURPOSE:  Returns month name
// PARAM:    month (integer)
//--------------------------------------------------
function GetMonthName(month) {
  if( !IsValidMonth(month) ) {
    return false;
  }

  // Load our names into an array
  var monthName = new Array(12);
      monthName[0] = "Январь";
      monthName[1] = "Февраль";
      monthName[2] = "Март";
      monthName[3] = "Апрель";
      monthName[4] = "Май";
      monthName[5] = "Июнь";
      monthName[6] = "Июль";
      monthName[7] = "Август";
      monthName[8] = "Сентябрь";
      monthName[9] = "Октябрь";
      monthName[10] = "Ноябрь";
      monthName[11] = "Декабрь";

      return monthName[month];
}

//--------------------------------------------------
// FUNCTION: GetDayName
// PURPOSE:  Returns day of week
// PARAM:    day (integer)
//--------------------------------------------------
function GetDayName(day) {
  if( isNaN(day) || day < 0 || day > 6 ) {
    return false;
  }

  // Load our names into an array
  var dayName = new Array(7);
      dayName[0] = "Sunday";
      dayName[1] = "Monday";
      dayName[2] = "Tuesday";
      dayName[3] = "Wednesday";
      dayName[4] = "Thursday";
      dayName[5] = "Friday";
      dayName[6] = "Saturday";

      return dayName[day];
}

//--------------------------------------------------
// FUNCTION: GetAge
//--------------------------------------------------
function GetAge(bMonth, bDate, bYear){
  var today = new Date();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  var tYear = today.getFullYear();

  // The caller should have validated the date already,
  // but let's double-check (w/ errors suppressed) just in case.
  if ( !IsValidDate(bMonth, bDate, bYear) ){
    return false;
  }

  // Calculate difference in years
  var age = tYear - bYear;

  // Now see whether the birthday has already occurred this year and adjust accordingly
  if ( tMonth < bMonth || (tMonth == bMonth && tDate < bDate) ){
    age = age - 1;
  }

  // Can't have a negative age
  if (age < 0){
    age = 0;
  }
  
  return age;
}
