// *****************************************************************************
// * Begin Doc Object                                                          *
// *****************************************************************************
function Doc ()
{ 
} 

// Public Static Methods
Doc.Element = function (id) { return document.getElementById(id); };
// *****************************************************************************
// * End Doc Object                                                            *
// *****************************************************************************

// *****************************************************************************
// * Begin Utilities Object                                                    *
// *****************************************************************************
function Utilities ()
{
}

Utilities.IsNullOrEmpty = function (string) { return (string == null || string == ''); };
Utilities.IsArrowKey = function (keyCode) { return (keyCode >= 37 && keyCode <= 40); };
Utilities.IsLeftKey = function (keyCode) { return (keyCode == 37); };
Utilities.IsRightKey = function (keyCode) { return (keyCode == 39); };
Utilities.IsLeftRightKey = function (keyCode) { return (keyCode == 37 || keyCode == 39); };
Utilities.IsUpDownKey = function (keyCode) { return (keyCode == 38 || keyCode == 40); };
Utilities.IsUpKey = function(keyCode) { return (keyCode == 38); };
Utilities.IsDownKey = function(keyCode) { return (keyCode == 40); };
Utilities.IsLetterKey = function(keyCode) { return ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)); };
Utilities.IsBackspaceKey = function(keyCode) { return (keyCode == 8); };
Utilities.IsDeleteKey = function(keyCode) { return (keyCode == 46); };
Utilities.IsTabKey = function(keyCode) { return (keyCode == 9); };

Utilities.GetMouseX = function(e) 
{ 
  var x = 0;
  
  e = e || window.event;
  
  if(e.pageX)
  {
    y = e.pageX;
  }
  else if(e.clientX) 
  {
    y = e.clientX + document.documentElement.scrollLeft;
  }
  
  return y;
};

Utilities.GetMouseY = function(e) 
{ 
  var y = 0;
  
  e = e || window.event;
  
  if(e.pageY)
  {
    y = e.pageY;
  }
  else if(e.clientY) 
  {
    y = e.clientY + document.documentElement.scrollTop;
  }
  
  return y;
};

Utilities.GetXMLHttpRequest = function()
{
  var result = null;
  
  try
  {
    result = new XMLHttpRequest();  
  }
  catch (e)
  {
    try
    {
      result = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        result = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) { }
    }    
  }
  
  return result;
};

Utilities.Format = function(text)
{
  if ( arguments.length <= 1 )
  {
    return text;
  }

  var tokenCount = arguments.length - 2;

  for( var token = 0; token <= tokenCount; token++ )
  {
    text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
  }

  return text;
};

Utilities.Trim = function(text)
{
  return text.replace('/^\s+|\s+$/g', '');
};

Utilities.GetValueFromQueryString = function(key)
{
  var result = null;
  var queryString = window.location.search.substring(1, window.location.search.length);
  
  if(queryString.length > 0)
  {
    queryString = queryString.replace(/\+/g, ' ');
    
    var args = queryString.split('&');
    
    for(var i = 0; i < args.length; i++)
    {
      var pair = args[i].split('=');
      
      if(pair.length == 2)
      {
        var name = unescape(pair[0]);
        
        if(name == key)
        {
          result = pair[1];
          break;
        }
      }
    }
  }
  
  return result;
};

Utilities.SetCookie = function(name, value, expires, path, domain, secure) 
{
  var expiresDate = new Date();
  
  expiresDate.setDate(expiresDate.getDate() + expires);
  
  document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expiresDate.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=.uregina.ca" : "") + ((secure) ? "; secure" : "");
};

Utilities.GetCookieVal = function(offset) 
{
  var endstr = document.cookie.indexOf (";", offset);

  if (endstr == -1)
  {
    endstr = document.cookie.length;
  }
  
  return unescape(document.cookie.substring(offset, endstr));
};

Utilities.GetCookie = function(name) 
{
  var result = null;
  
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  
  while (i < clen) 
  {
    var j = i + alen;
    
    if (document.cookie.substring(i, j) == arg)
    {
      result = Utilities.GetCookieVal (j);
    }
    
    i = document.cookie.indexOf(" ", i) + 1;
    
    if (i == 0)
    {
      break; 
    }
  }
  
  return result;
};

Utilities.BuildManagerGetRequest = function(manager, action, data, id)
{
  return Utilities.Format('{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}', manager, Actions.Action, action, Actions.Data, data, Actions.ID, id, Actions.Data2);
};

Utilities.HtmlEncode = function(html) 
{
  var result = escape(html);     
  
  result = result.replace('/\//g','%2F');
  result = result.replace('/\?/g','%3F');
  result = result.replace('/=/g','%3D');
  result = result.replace('/&/g','%26');
  result = result.replace('/@/g','%40');

  return result;
}; 

Utilities.PositionToString = function(pos)
{
  var result = '';
  
  var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  
  if(pos < 26)
  {
    result = alphabet.charAt(pos);  
  }
  else
  {
    result = alphabet.charAt(25) + Utilities.PositionToString(pos - 26);
  }
  
  return result;
};

Utilities.PositionFromString = function(value)
{
  var result = 0;
  
  var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  
  for(var i = 0; i < value.length; i++)
  {
    result += alphabet.indexOf(value.charAt(i));
  }
  
  return result;
};

Utilities.OpenDateTimePicker = function (datePickerURL, formField)
{
  window.open(datePickerURL + '?field=' + formField + '&current=' + Doc.Element(formField).value, '', 'width=250,height=215,resizable=yes');
};
// *****************************************************************************
// * End Utilities Object                                                      *
// *****************************************************************************

function AddDocumentLoadEvent(func) 
{
  var oldonload = window.onload;
  
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() 
    {
        if (oldonload) 
        {
            oldonload();
        }
        func();
    }
  }
}

// *****************************************************************************
// * Copyright 2007, Wizards of the Coast                                      *
// *****************************************************************************