/* date selection with three modes:
 *
 * mode 3: Date and time
 * mode 2: Date only
 * mode 1: Time only
 *
 */

//set todays date
Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();
NowHour = Now.getHours();
NowMinute = Now.getMinutes();
if (NowYear < 2000) NowYear += 1900; //for Netscape

var d_locale = { 'en': {'MN_1':'January', 'MN_2':'February', 'MN_3':'March', 'MN_4':'April', 'MN_5':'May', 'MN_6':'June', 'MN_7':'July', 'MN_8':'August', 'MN_9':'September', 'MN_10':'October', 'MN_11':'November', 'MN_12':'December'},
                 'no': {'MN_1':'Januar', 'MN_2':'Februar', 'MN_3':'Mars', 'MN_4':'April', 'MN_5':'Mai', 'MN_6':'Juni', 'MN_7':'Juli', 'MN_8':'August', 'MN_9':'September', 'MN_10':'Oktober', 'MN_11':'November', 'MN_12':'Desember'},
                 'sv': {'MN_1':'Januari', 'MN_2':'Februari', 'MN_3':'Mars', 'MN_4':'April', 'MN_5':'Maj', 'MN_6':'Juni', 'MN_7':'Juli', 'MN_8':'Augusti', 'MN_9':'September', 'MN_10':'Oktober', 'MN_11':'November', 'MN_12':'December'},
                 'nl': {'MN_1':'Januari', 'MN_2':'Februari', 'MN_3':'Maart', 'MN_4':'April', 'MN_5':'Mei', 'MN_6':'Juni', 'MN_7':'Juli', 'MN_8':'Augustus', 'MN_9':'September', 'MN_10':'Oktober', 'MN_11':'November', 'MN_12':'December'}
               };
function dateSelect(Which, mode, from, to)
{
  if (from == null || from==0) from = 1995;
  if (to == null || to==0)  to = from + 20;
  try {
    var clang = currentLanguage;
  } catch(e) {
    var clang = system.lang;
  }
  if (!d_locale[clang])
  {
    var alangs = { 'nb':'no', 'nb-no':'no', 'nn':'no', 'nn-no':'no', 'se':'sv'};
    clang = alangs[clang];
    if (!d_locale[clang]) clang = 'en';
  }

  if (mode & 2)
  {
    document.write('<select name="' + Which + 'Day" id="' + Which + 'Day" onchange="checkField(\'' + Which + '\'); validateField(\'' + Which + '\')">');
    document.write(WriteRangeOptions(1, 31));
    document.write('</select>');

    document.write('<select name="' + Which + 'Month" id="' + Which + 'Month" onchange="checkField(\'' + Which + '\'); ChangeOptionDays(\'' + Which +'\')">');

    for (i=1; i <= 12; i += 1)
    {
      document.write("<option value=\"" + i + "\">" + d_locale[clang]['MN_'+i] + "</option>\n");
    }
    document.write('</select>');

    document.write('<select name="' + Which + 'Year" id="' + Which + 'Year" onchange="checkField(\'' + Which + '\'); ChangeOptionDays(\'' + Which +'\')">');
    document.write(WriteYearOptions(from, to));
    document.write('</select> ');
  }
  else
  {
    document.write('<input type="hidden" name="' + Which + 'Day" value="1" />');
    document.write('<input type="hidden" name="' + Which + 'Month" value="1" />');
    document.write('<input type="hidden" name="' + Which + 'Year" value="1970" />');
  }

  if (mode & 1)
  {
    document.write('<select name="' + Which + 'Hour" id="' + Which + 'Hour" onchange="checkField(\'' + Which + '\'); validateField(\'' + Which +'\')">');
    document.write(WriteRangeOptions(0, 23));
    document.write('</select>');


    document.write('<select name="' + Which + 'Minute" id="' + Which + 'Minute" onchange="checkField(\'' + Which + '\'); validateField(\'' + Which +'\')">');
    document.write(WriteRangeOptions(0, 59));
    document.write('</select>');
  }
  else
  {
    document.write('<input type="hidden" name="' + Which + 'Hour" value="0" />');
    document.write('<input type="hidden" name="' + Which + 'Minute" value="0" />');
  }

  document.write('<input type="hidden" name="' + Which + '" id="' + Which + '" value="" />');

}

//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == "4" || WhichMonth == "6" || WhichMonth == "9" || WhichMonth == "11") DaysInMonth = 30;
  if (WhichMonth == "2" && (WhichYear/4) != Math.floor(WhichYear/4))  DaysInMonth = 28;
  if (WhichMonth == "2" && (WhichYear/4) == Math.floor(WhichYear/4))  DaysInMonth = 29;

  return DaysInMonth
}

//function to change the available days in a months
function ChangeOptionDays(Which)
{
  DaysObject = eval("document.form1." + Which + "Day");
  MonthObject = eval("document.form1." + Which + "Month");
  YearObject = eval("document.form1." + Which + "Year");

  Month = MonthObject[MonthObject.selectedIndex].value;
  Year = YearObject[YearObject.selectedIndex].Text;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;
  if (CurrentDaysInSelection > DaysForThisSelection)
  {
    for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
    {
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
    for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
    {
      NewOption = new Option(DaysObject.options.length + 1);
      DaysObject[DaysObject.length] = NewOption;
    }
  }
  if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;

  validateField(Which)
}

//function to set options to today
function SetToToday(Which)
{
  DayObject = eval("document.form1." + Which + "Day");
  MonthObject = eval("document.form1." + Which + "Month");
  YearObject = eval("document.form1." + Which + "Year");

  if (DayObject.type == 'hidden' && MonthObject.type == 'hidden' && YearObject.type == 'hidden')
    return;

  for (i = 0; i < YearObject.length; i++)
  {
    if (YearObject[i].value == NowYear)
    {
      YearObject[i].selected = true;
    }
  }

  MonthObject[NowMonth].selected = true;

  HourObject = eval("document.form1." + Which + "Hour");
  if (HourObject && HourObject.type != 'hidden')
    HourObject[NowHour].selected = true;

  MinuteObject = eval("document.form1." + Which + "Minute");
  if(MinuteObject  && MinuteObject.type != 'hidden')
    MinuteObject[NowMinute].selected = true;

  ChangeOptionDays(Which);

  DayObject[NowDay-1].selected = true;
}

//function to set options to today
function SetDate(Which, year, month, day, hour, minute)
{

  DayObject   = eval("document.form1." + Which + "Day");
  MonthObject = eval("document.form1." + Which + "Month");
  YearObject  = eval("document.form1." + Which + "Year");
  var mode    = 1;

  if (DayObject.type != 'hidden' && MonthObject.type != 'hidden' && YearObject.type != 'hidden')
  {
    mode = 3;
    FirstYear = YearObject.options[0].value;
  }

  try
  {
    if (mode & 2)
    {
      if (year < FirstYear)
        year = NowYear;

      YearObject[ year - FirstYear].selected = true;
      MonthObject[month-1].selected = true;
    }

    HourObject = eval("document.form1." + Which + "Hour");
    if (HourObject && HourObject.type != 'hidden')
      HourObject[hour].selected = true;

    MinuteObject = eval("document.form1." + Which + "Minute");
    if(MinuteObject  && MinuteObject.type != 'hidden')
      MinuteObject[minute].selected = true;

    if (mode & 2)
    {
      ChangeOptionDays(Which);

      DayObject[day-1].selected = true;
    }
  }
  catch (e)
  {}
}

//function to write option years plus x
function WriteYearOptions(from, to)
{
  line       = "";
  YearsAhead = to - from;

  for (i=0; i < YearsAhead; i++)
  {
    line += "<option value=\"" + (from + i) + "\">";
    line += from + i;
  }

  return line;
}

//function to write option years plus x
function WriteHourOptions(YearsAhead)
{
  line = "";
  for (i=1; i<25; i++)
  {
    line += "<option>";
    line += i;
  }
  return line;
}

function WriteRangeOptions(start, end)
{
  line = "";
  for (i=start; i <= end; i++)
  {
    line += "<option>";
    if (i < 10)
      line += '0';
    line += i;
  }
  return line;
}

function validateField(Which)
{
  DayObject = eval("document.form1." + Which + "Day");
  MonthObject = eval("document.form1." + Which + "Month");
  YearObject = eval("document.form1." + Which + "Year");

  if (DayObject.type == 'hidden' && MonthObject.type == 'hidden' && YearObject.type == 'hidden')
    return;

  Day = DayObject[DayObject.selectedIndex].text;
  Month = MonthObject[MonthObject.selectedIndex].value;
  Year = YearObject[YearObject.selectedIndex].text;

  HourObject = eval("document.form1." + Which + "Hour");
  if (HourObject && HourObject.type != 'hidden')
    Hour = HourObject[HourObject.selectedIndex].text;
  else
    Hour = 0

  MinuteObject = eval("document.form1." + Which + "Minute");
  if (MinuteObject && MinuteObject.type != 'hidden')
    Minute = MinuteObject[MinuteObject.selectedIndex].text;
  else
    Minute = 0

  TextObject = eval("document.form1." + Which)
  TextObject.value = MonthObject.selectedIndex+1
}

function checkField(Which)
{
  var el = Which + 'ModeB';

  d = document.getElementById(el);

  if (d != null)
  {
    d.checked = true;
  }
}

