﻿//==========================================================================================================================================
//	(c) Education Direct
//
//	Change History
//	=============
// 20/10/2009  PE   XX    Added AutoUpdateElementWithFormattedDateTimeToLower() and LowerCaseAMPM() to allow date to have lowercase AM/PM
// 05/10/2009  RT   XX    Added open_window_simple() and open_window()
// 30/09/2009  DC   XX    Added SetElementVisibility
// 16/09/2009  DC   XX    Corrected setTimeout call for AutoUpdateElementWithFormattedDateTime()
// 16/09/2009  DC   XX    Added AutoUpdateElementDateTime() and AutoUpdateElementWithFormattedDateTime()
// 16/09/2009  DC   XX    Updated the GetFormattedDate() which was using the wrong format for minutes
// 11/09/2009  DC   XX    Added GetFormattedDate() and GetSpecificFormattedDate()
// 10/09/2009  RT   XX    Added GetCookie() and SetCookie()
// 02/09/2009  DC	XX   Created
//==========================================================================================================================================

/// <summary>
/// Takes an element that is to have it's visibility changed and inverses it
/// </summary>
/// <param name="elementName">The name of the element</param>
function ToggleElementVisibility(elementName) {

  var element = document.getElementById(elementName);

  if (element != undefined) {
    // If an item doesn't have the style set to block (i.e. be visible) then the toggle should hide the item.
    element.style.display = (element.style.display == 'block' ? 'none' : 'block');
  }
}

/// <summary>
/// Takes an element that is to have it's visibility changed and set it
/// </summary>
/// <param name="elementName">The name of the element</param>
/// <param name="visible">Whether it is visible or not</param>
function SetElementVisibility(elementName, visible) {

  var element = document.getElementById(elementName);

  if (element != undefined) {
    // set the display block or none value.
    element.style.display = (visible == false ? 'none' : 'block');
  }

}

///<summary>
/// Reads a value out of a cookie
/// </summary>
/// <param name="c_name">The name of the cookie value to return</param>
/// <returns>The cookie value, or an empty string if not present</returns>
function GetCookie(c_name) {
  var result = "";
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start != -1) {
      c_start = c_start + c_name.length + 1;
      c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) {
        c_end = document.cookie.length;
      }
      result = unescape(document.cookie.substring(c_start, c_end));
    }
  }
  return result;
}

/// <summary>
/// sets the value for a cookie
/// </summary>
/// <param name="c_name">The name of the cookie value to set</param>
/// <param name="value">The value to set</param>
/// <param name="expiredays">The number of days the cookie should last for.  Note, leave this null if you just want a session cookie</param>
function SetCookie(c_name, value, expiredays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

/// <summary>
/// retrieves the current date/time formatted
/// </summary>
function GetFormattedDate() {
  var dt = new Date();
  return GetSpecificFormattedDate("hh:mmtt, dd MMMM yyyy");
}

///<summary>
/// retrieves the current date/time formatted to that format that is specified
/// </summary>
/// <param name="dateFormat">The format to use</param>
function GetSpecificFormattedDate(dateFormat) {
  var dt = new Date();
  return dt.format(dateFormat);
}

/// <summary>
/// Finds the supplied element by its ID on the page then updates its time automatically
/// </summary>
/// <param name="elementID">ID of the element to find to set the date on</param>
function AutoUpdateElementDateTime(elementID) {
  var element = document.getElementById(elementID);
  element.innerText = GetFormattedDate();
  setTimeout("AutoUpdateElementDateTime('" + elementID + "')", 1000);
}

/// <summary>
/// Finds the supplied element by its ID on the page then updates its time automatically
/// </summary>
/// <param name="elementID">ID of the element to find to set the date on</param>
/// <param name="dateFormat">Format of the date to be displayed</param>
function AutoUpdateElementWithFormattedDateTime(elementID, dateFormat) {
  var element = document.getElementById(elementID);
  element.innerText = GetSpecificFormattedDate(dateFormat);
  setTimeout("AutoUpdateElementWithFormattedDateTime('" + elementID + "', '" + dateFormat + "')", 1000);
}

/// <summary>
/// AutoUpdateElementWithFormattedDateTimeToLower, returns the date formatted with the supplied date format,
/// lowercase am pm and recalls itself every second
/// </summary>
/// <param name="elementID">ID of the element to find to set the date on</param>
/// <param name="dateFormat">Format of the date to be displayed</param>
function AutoUpdateElementWithFormattedDateTimeToLower(elementID, dateFormat) {
  var element = document.getElementById(elementID);
  var dateTimeValue = GetSpecificFormattedDate(dateFormat);
  element.innerText = LowerCaseAMPM(dateTimeValue);
  setTimeout("AutoUpdateElementWithFormattedDateTimeToLower('" + elementID + "', '" + dateFormat + "')", 1000);
}

/// <summary>
/// LowerCaseAMPM, Changes AM and PM to lower case am and pm
/// </summary>
/// <param name="dateTimeValue">The date time string</param>
function LowerCaseAMPM(dateTimeValue) {
  dateTimeValue = dateTimeValue.replace('AM', 'am');
  dateTimeValue = dateTimeValue.replace('PM', 'pm');
  return dateTimeValue;
}

/// <summary>
/// OpenWindowSimple, opens a url into a new window using a default width & height
/// </summary>
/// <param name="url">The url to open</param>
/// <param name="title">The window title</param>
function OpenWindowSimple(url, title) {
  var sw = screen.width / 2, sh = screen.height / 2;
  return OpenWindow(url, title, sw, sh);
}

/// <summary>
/// OpenWindow, opens a  url into a new window, and centres it
/// </summary>
/// <param name="url">The url to open</param>
/// <param name="title">The window title</param>
/// <param name="width">The window width</param>
/// <param name="height">The window height</param>
function OpenWindow(url, title, width, height) {
  var sw = screen.width, sh = screen.height;
  var ulx = ((sw - width) / 2), uly = ((sh - height) / 2);
  var paramz = 'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbars=yes,width=' + width + ',height=' + height + '';
  var oSubWin = window.open("", title, paramz);
  oSubWin.moveTo(ulx, uly);
  oSubWin.location.replace(url);
  return false;
}