﻿// JScript File Utils.js
// Contains JScript functions used throughout the web application.


function GetPageWidth()
{
   if (window.innerWidth)
   {
      return window.innerWidth;
   }
   else if (document.documentElement && document.documentElement.clientWidth)
   {
      return document.documentElement.clientWidth;
   }
   else if (document.body)
   {
      return document.body.clientWidth;
   }

   // TODO: implement error message for this case.
   throw "Unable to determine the HTML page width";
}

function GetPageHeight()
{
   if (window.innerHeight)
   {
      return window.innerHeight;
   }
   else if (document.documentElement && document.documentElement.clientHeight)
   {
      return document.documentElement.clientHeight;
   }
   else if (document.body)
   {
      return document.body.clientHeight;
   }

   // TODO: implement error message for this case.
   throw "Unable to determine the HTML page height";
}


function GetElementHeight(elementName)
{
   var element = document.getElementById(elementName);
   return element.offsetHeight;
}


function GetElementWidth(elementName)
{
   var element = document.getElementById(elementName);
   return element.offsetWidth;
}

function ClearHtmlElement(htmlElement)
{
   while (htmlElement.firstChild != undefined)
   {
      htmlElement.removeChild(htmlElement.firstChild);
   }
}

function GetExceptionMessage(ex)
{
   if (ex.toString) return ex.toString();
   else if (ex.message) return ex.message;

   return "Unknown error. Error type: " + typeof (ex);
}


function PrepareBrowsers()
{
   DetectBrowser();
}

var browserType = '';
var browserVersion = 0;

function DetectBrowser()
{

   if (navigator.userAgent.indexOf('Mozilla') >= 0 &&
      navigator.userAgent.indexOf('compatible') >= 0 &&
      navigator.userAgent.indexOf('MSIE') >= 0)
   {
      browserType = 'MsIE';

      if (navigator.userAgent.indexOf('MSIE 6.') >= 0)
      {
         browserVersion = 6;
      }
      else if (navigator.userAgent.indexOf('MSIE 7.') >= 0)
      {
         browserVersion = 7;
      }
      else if (navigator.userAgent.indexOf('MSIE 8.') >= 0)
      {
         browserVersion = 8;
      }

      return;
   }


   if (navigator.userAgent.indexOf('Mozilla') >= 0 &&
      navigator.userAgent.indexOf('Firefox') >= 0)
   {
      browserType = 'Firefox';
      return;
   }

   if (navigator.userAgent.indexOf('Chrome') >= 0)
   {
      browserType = 'Chrome';
      return;
   }

   if (navigator.userAgent.indexOf('Safari') >= 0)
   {
      browserType = 'Safari';
      return;
   }

}

