function MakeActive(MAIN,SUB) {
  document.getElementById(MAIN).className = 'active';
  if(document.getElementById(MAIN + '-menu')) {
    document.getElementById(MAIN + '-menu').style.display = 'block';
    if(SUB != '') {
      document.getElementById(SUB).className = 'active';
    }
  }
}

function NewsCheckSemester()
{
  form = document.getElementById('newsform');

  if(form.elements['semester'])
  {
    value = form.elements['semester'].value;

    if(value.length != 4 || (value.substr(0,2) != 'fa' && value.substr(0,2) != 'sp') || isNaN(value.substr(2,2)))
    {
      messages += "The semester field must be valid. Please use the form.\n";
      return false;
    }
    return true;
  }
  return false;
}

function NewsCheckInput(field,minlen,maxlen,message)
{
  form = document.getElementById('newsform');

  if(form.elements[field])
  {
    value = form.elements[field].value;

    if(minlen > value.length || value.length > maxlen)
    {
      messages += message + "\n";
      return false;
    }
    return true;
  }
  return false;
}

function IsLeapYear(year)
{
  if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
  {
      return true;
  }
  return false;
}

function NewsCheckDate()
{
  form = document.getElementById('newsform');

  if(form.elements['date'])
  {
    value = form.elements['date'].value;
    today = new Date();
    days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    if(value.length != 19)
    {
      messages += "The date needs to be a valid date in the following format: yyyy-mm-dd hh:mm:ss\n";
      return false;
    }

    year = value.substr(0,4);
    mon = value.substr(5,2);
    day = value.substr(8,2);
    hour = value.substr(11,2);
    min = value.substr(14,2);
    sec = value.substr(17,2);

    if(year.length == 4 && !isNaN(year) && IsLeapYear(year))
    {
      days[1] = 29;
    }

    if(isNaN(mon) || mon.length != 2 || mon < 1 || mon > 12 || isNaN(day) || day.length != 2 || day < 1 || day > days[mon-1] || isNaN(year) || year.length != 4 || year < 1860 || year > today.getFullYear() || isNaN(hour) || hour.length != 2 || hour >= 24 || hour < 0 || isNaN(min) || min.length != 2 || min >= 60 || min < 0 || isNaN(sec) || sec.length != 2 || sec >= 60 || sec < 0)
    {
      messages += "The date needs to be a valid date in the following format: yyyy-mm-dd hh:mm:ss\n";
      return false;
    }
    return true;
  }
  return false;
}

function NewsCheckId()
{
  form = document.getElementById('newsform');

  if(form.elements['id'])
  {
    value = form.elements['id'].value;

    if(isNaN(value))
    {
      messages += "The id must be a number.\n";
      return false;
    }
    return true;
  }
  return false;
}

function NewsPostCheck()
{
  if(document.getElementById)
  {
    error = false;
    messages = "The following errors have occurred:\n\n";

    if(!NewsCheckSemester()) error = true;
    if(!NewsCheckInput('title',1,50,'The title must be between 1 and 50 characters long.')) error = true;
    if(!NewsCheckInput('message',1,65535,'The message text must be between 1 and 65535 characters long.')) error = true;
    if(!NewsCheckInput('author',1,50,'The authors name must be between 1 and 50 characters long.')) error = true;
    if(!NewsCheckInput('password',1,50,'The password must be between 1 and 50 characters long.')) error = true;

    messages += "\nIf you are still receiving errors and think you shouldn\'t, you can contact technical support from the contact page.";

    if(error)
    {
      alert(messages);
      return false;
    }
    else
    {
      return true;
    }
  }
  return true;
}

function NewsEditCheck()
{
  if(document.getElementById)
  {
    error = false;
    messages = "The following errors have occurred:\n\n";

    if(!NewsCheckId()) error = true;
    if(!NewsCheckSemester()) error = true;
    if(!NewsCheckInput('title',1,50,'The title must be between 1 and 50 characters long.')) error = true;
    if(!NewsCheckInput('message',1,65535,'The message text must be between 1 and 65535 characters long.')) error = true;
    if(!NewsCheckInput('author',1,50,'The authors name must be between 1 and 50 characters long.')) error = true;
    if(!NewsCheckDate()) error = true;
    if(!NewsCheckInput('password',1,50,'The password must be between 1 and 50 characters long.')) error = true;

    messages += "\nIf you are still receiving errors and think you shouldn\'t, you can contact technical support from the contact page.";

    if(error)
    {
      alert(messages);
      return false;
    }
    else
    {
      return true;
    }
  }
  return true;
}

function NewsDeleteCheck()
{
  if(document.getElementById)
  {
    error = false;
    messages = "The following errors have occurred:\n\n";

    if(!NewsCheckId()) error = true;

    messages += "\nIf you are still receiving errors and think you shouldn\'t, you can contact technical support from the contact page.";

    if(error)
    {
      alert(messages);
      return false;
    }
    else
    {
      return true;
    }
  }
  return true;
}

function PasswordsCheck()
{
  if(document.getElementById)
  {
    error = false;
    messages = "The following errors have occurred:\n\n";

    form = document.getElementById('passwordsform');

    if(form.elements['password'])
    {
      value = form.elements['password'].value;

      if(1 > value.length || value.length > 50)
      {
        messages += "The password must be between 1 and 50 characters long." + "\n";
        error = true;
      }
    }

    messages += "\nIf you are still receiving errors and think you shouldn\'t, you can contact technical support from the contact page.";

    if(error)
    {
      alert(messages);
      return false;
    }
    else
    {
      return true;
    }
  }
  return true;
}

function SemestersCheck()
{
  if(document.getElementById)
  {
    error = false;
    messages = "The following errors have occurred:\n\n";

    form = document.getElementById('semestersform');

    if(form.elements['semester'])
    {
      value = form.elements['semester'].value;
      re = /^(sp|fa)[0-9][0-9]$/;

      if(!(re.test(value)))
      {
        messages += "The password must be in the format xxnn where xx denotes the semester (\"sp\" or \"fa\") and nn is the last two digits of the year." + "\n";
        error = true;
      }
    }

    messages += "\nIf you are still receiving errors and think you shouldn\'t, you can contact technical support from the contact page.";

    if(error)
    {
      alert(messages);
      return false;
    }
    else
    {
      return true;
    }
  }
  return true;
}