﻿//-----------------------------------------------------------------------
// <copyright file="MEDHOST.Web.ClientLibrary.Core.js" company="MEDHOST">
//   Copyright (c) MEDHOST, Inc. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

// MEDHOST helper methods and functions

// String.prototype.MEDHOSTtrim
// Removes all leading and trailing whitespace from the current String object.
//  sample:
//    " hello world     ".MEDHOSTtrim() -> "hello world"
//
//  input:
//    String (this)
//  return:
//    String - A copy of the string in which the format items have been replaced by the String equivalent of the corresponding instances of Object in arguments.
String.prototype.MEDHOSTtrim = function()
{
  var trimString = this;

  try
  {
    // Use a regular expression to replace leading and trailing spaces with the empty string.
    trimString = this.replace(/(^\s*)|(\s*$)/g, "");
  }
  catch (e)
  {
  }

  return trimString;
}

// String.prototype.MEDHOSTformat
// Replaces each format item in a specified String with the text equivalent of a corresponding instances of Object in arguments.
//  sample:
//    "{0} days around the {1}".MEDHOSTformat(80, "world") -> "80 days around the world"
//
//  input:
//    String (this)
//  return:
//    String - A copy of the string in which the format items have been replaced by the String equivalent of the corresponding instances of Object in arguments.
String.prototype.MEDHOSTformat = function()
{
  var formatString = this;

  try
  {
    for (var idxArgument = 0; idxArgument < arguments.length; idxArgument++)
    {
      formatString = formatString.replace("{" + idxArgument + "}", arguments[idxArgument]);
    }
  }
  catch (e)
  {
  }

  return formatString;
}

// MEDHOSTincludeJavascriptFile
// Injects an html tag into the document to include a javascript file.
//  sample:
//    MEDHOSTincludeJavascriptFile("SomeJavascriptFile.js");
//
//  input:
//    String filename - A path to a file.
//  return:
//    none
function MEDHOSTincludeJavascriptFile(filename)
{
  try
  {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = filename;
    var body = document.getElementsByTagName("head").item(0);
    body.appendChild(script);
  }
  catch (e)
  {
  }
}

var _MEDHOSTWebServiceRequestObject = null;

// MEDHOSTinstantiateWebServiceRequestObject
// Returns the web service request object.
// If the web service request object does not exist, it is created.
//  sample:
//    var webServiceRequestObj = MEDHOSTinstantiateWebServiceRequestObject();
//
//  input:
//    none
//  return:
//    IXMLHttpRequest - An IXMLHttpRequest object.
function MEDHOSTinstantiateWebServiceRequestObject()
{
  try
  {
    if (_MEDHOSTWebServiceRequestObject == null)
    {
      if (typeof (XMLHttpRequest) == "undefined")
      {
        // Special instantiation of web request object for IE 5.x-6.x.
        if (_MEDHOSTWebServiceRequestObject == null)
        {
          try { _MEDHOSTWebServiceRequestObject = new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) { }
        }

        if (_MEDHOSTWebServiceRequestObject == null)
        {
          try { _MEDHOSTWebServiceRequestObject = new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) { }
        }

        if (_MEDHOSTWebServiceRequestObject == null)
        {
          try { _MEDHOSTWebServiceRequestObject = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
        }

        if (_MEDHOSTWebServiceRequestObject == null)
        {
          try { _MEDHOSTWebServiceRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
        }
      }
      else
      {
        // Standard instantiation of web request object for most browsers.
        _MEDHOSTWebServiceRequestObject = new XMLHttpRequest();
      }
    }
  }
  catch (e)
  {
  }

  return _MEDHOSTWebServiceRequestObject;
}

// MEDHOSTreleaseWebServiceRequestObject
// Releases the web service request object.
//  sample:
//    MEDHOSTreleaseWebServiceRequestObject();
//
//  input:
//    none
//  return:
//    none
function MEDHOSTreleaseWebServiceRequestObject()
{
  try
  {
    if (_MEDHOSTWebServiceRequestObject != null)
    {
      delete _MEDHOSTWebServiceRequestObject;
    }
  }
  catch (e)
  {
  }
  finally
  {
    _MEDHOSTWebServiceRequestObject = null;
  }
}

// MEDHOSTgetWebServiceSingleResultValue
// Gets the result value from the response of a web method call.
//  sample:
//    var myValue = MEDHOSTgetWebServiceSingleResultValue(resultNode, "");
//
//  input:
//    IXMLDOMElement resultNode - An IXMLDOMElement representing the response of a web method call.
//    String defaultValue - A default for the response value in the case where the response value does not exist.
//  return:
//    String - A response value.
function MEDHOSTgetWebServiceSingleResultValue(resultNode, defaultValue)
{
  var responseValue = defaultValue;

  var valueIndex = 0;
  try
  {
    if (resultNode != null)
    {
      if (resultNode.childNodes.length > valueIndex)
      {
        // the result is the first child node value
        responseValue = resultNode.childNodes[valueIndex].nodeValue;
      }
    }
  }
  catch (e)
  {
    //window.status = e.toString();
  }

  return responseValue;
}

// MEDHOSTgetWebServiceMultipleResultValue
// Gets the result value from the response set of a web method call.
//  sample:
//    var myValue = MEDHOSTgetWebServiceMultipleResultValue(resultNode, 2, "");
//
//  input:
//    IXMLDOMElement resultNode - An IXMLDOMElement representing the response set of a web method call.
//    Number valueIndex - The indexed position (0-based) of the response value in the result set.
//    String defaultValue - A default for the response value in the case where the response value does not exist.
//  return:
//    String - A response value.
function MEDHOSTgetWebServiceMultipleResultValue(resultNode, valueIndex, defaultValue)
{
  var responseValue = defaultValue;

  try
  {
    if (resultNode != null)
    {
      if ((resultNode.childNodes.length > valueIndex) && (resultNode.childNodes[valueIndex].firstChild != null))
      {
        // the result value is the first child node value of the indexed child node
        responseValue = resultNode.childNodes[valueIndex].firstChild.nodeValue;
      }
    }
  }
  catch (e)
  {
    //window.status = e.toString();
  }

  return responseValue;
}

// MEDHOSTformatDateUniversalSortable
// Formats a date as a universal sortable date/time pattern.
//  sample:
//    MEDHOSTformatDateUniversalSortable(new Date(2010,0,21,23,7,29)) -> "2010-01-21 23:07:29Z"
//
//  input:
//    Date date - A Date object.
//  return:
//    String - A formatted date string.
function MEDHOSTformatDateUniversalSortable(date)
{
  var formatString = "{0}-{1}-{2} {3}:{4}:{5}Z";

  var year = date.getUTCFullYear();
  var month = date.getUTCMonth() + 1;
  var day = date.getUTCDate();
  var hour = date.getUTCHours();
  var minute = date.getUTCMinutes();
  var second = date.getUTCSeconds();

  if (year < 10)
  {
    year = "000" + year;
  }
  else if (year < 100)
  {
    year = "00" + year;
  }
  else if (year < 1000)
  {
    year = "0" + year;
  }

  if (month < 10)
  {
    month = "0" + month;
  }

  if (day < 10)
  {
    day = "0" + day;
  }

  if (hour < 10)
  {
    hour = "0" + hour;
  }

  if (minute < 10)
  {
    minute = "0" + minute;
  }

  if (second < 10)
  {
    second = "0" + second;
  }

  var formattedString = formatString.MEDHOSTformat(year, month, day, hour, minute, second);
  return formattedString;
}

// MEDHOSTformatTotalMinutesLong
// Formats a minute value as a long hour-minute pattern.
//  sample:
//    MEDHOSTformatTotalMinutesLong(1) -> "1 minute"
//    MEDHOSTformatTotalMinutesLong(59) -> "59 minutes"
//    MEDHOSTformatTotalMinutesLong(60) -> "1 hour 0 minutes"
//    MEDHOSTformatTotalMinutesLong(61) -> "1 hour 1 minute"
//    MEDHOSTformatTotalMinutesLong(129) -> "2 hours 9 minutes"
//
//  input:
//    Number totalMinutes - The total number of minutes.
//  return:
//    String - A formatted minutes string.
function MEDHOSTformatTotalMinutesLong(totalMinutes)
{
  if (totalMinutes < 0)
  {
    totalMinutes = 0;
  }

  var hours = Math.floor(totalMinutes / 60);
  var minutes = totalMinutes - (hours * 60);
  var hoursLabel = "";
  var minutesLabel = "";

  var formatString = "{2} {3}";
  if (hours > 0)
  {
    formatString = "{0} {1} " + formatString;
  }

  if (hours == 1)
  {
    hoursLabel = "hour";
  }
  else if (hours > 1)
  {
    hoursLabel = "hours";
  }

  if (minutes == 1)
  {
    minutesLabel = "minute";
  }
  else
  {
    minutesLabel = "minutes";
  }

  var formattedString = formatString.MEDHOSTformat(hours, hoursLabel, minutes, minutesLabel);
  return formattedString;
}

// MEDHOSTformatTotalMinutesShort
// Formats a minute value as a shour hour:minute pattern.
//  sample:
//    MEDHOSTformatTotalMinutesShort(1) -> "1 min"
//    MEDHOSTformatTotalMinutesShort(59) -> "59 min"
//    MEDHOSTformatTotalMinutesShort(60) -> "1:00 hr"
//    MEDHOSTformatTotalMinutesShort(61) -> "1:01 hrs"
//    MEDHOSTformatTotalMinutesShort(129) -> "2:09 hrs"
//
//  input:
//    Number totalMinutes - The total number of minutes.
//  return:
//    String - A formatted minutes string.
function MEDHOSTformatTotalMinutesShort(totalMinutes)
{
  if (totalMinutes < 0)
  {
    totalMinutes = 0;
  }

  var hours = Math.floor(totalMinutes / 60);
  var minutes = totalMinutes - (hours * 60);
  var hoursLabel = "";
  var minutesLabel = "";

  var formatString = "{2} {3}";
  if (hours > 0)
  {
    formatString = "{0}:{2} {1}";
  }

  if ((hours == 1) && (minutes == 0))
  {
    hoursLabel = "hr";
  }
  else
  {
    hoursLabel = "hrs";
  }

  if (minutes == 1)
  {
    minutesLabel = "min";
  }
  else
  {
    minutesLabel = "min";
  }

  if ((hours > 0) && (minutes < 10))
  {
    minutes = "0" + minutes;
  }

  var formattedString = formatString.MEDHOSTformat(hours, hoursLabel, minutes, minutesLabel);
  return formattedString;
}

// MEDHOSTformatTotalMinutes
// Formats a minute value according to the pattern.
//  sample:
//    MEDHOSTformatTotalMinutes(1, "hh:mm") -> "00:01"
//    MEDHOSTformatTotalMinutesShort(59 "hh:mm") -> "00:59"
//    MEDHOSTformatTotalMinutesShort(60 "hh:mm") -> "01:00"
//    MEDHOSTformatTotalMinutesShort(61 "hh:mm") -> "01:01"
//    MEDHOSTformatTotalMinutesShort(129 "hh:mm") -> "02:09"
//
//  input:
//    Number totalMinutes - The total number of minutes.
//    String formatString - The format of the total minutes.
//  return:
//    String - A formatted minutes string.
function MEDHOSTformatTotalMinutes(totalMinutes, formatString)
{
  if (totalMinutes < 0)
  {
    totalMinutes = 0;
  }

  var hours = Math.floor(totalMinutes / 60);
  var minutes = totalMinutes - (hours * 60);

  var formattedString = formatString;

  var hoursString = hours;
  if (hours < 10)
  {
    hoursString = "0" + hoursString;
  }
  formattedString = formattedString.replace(/hh/g, hoursString);

  hoursString = hours;
  formattedString = formattedString.replace(/h/g, hoursString);

  var minutesString = minutes;
  if (minutes < 10)
  {
    minutesString = "0" + minutesString;
  }
  formattedString = formattedString.replace(/mm/g, minutesString);

  minutesString = minutes;
  formattedString = formattedString.replace(/mm/g, minutesString);

  return formattedString;
}

