//------------------------------------------------------------------------------
// Gets element by the ID.
function getElementById(id)
{
  if (document != null && 
      document.getElementById != null && 
      id != null && 
      id != "")
  {
    return document.getElementById(id);
  }
  return null;
}
//------------------------------------------------------------------------------
// Returns true if s ends with suffix.
function endsWith(s, suffix)
{
  if (s != null && suffix != null)
  {
    var lenBeforeSuffix = s.length - suffix.length;
    return (s.lastIndexOf(suffix) == lenBeforeSuffix);
  }
  return false;
}
//------------------------------------------------------------------------------
// Returns absolute left coordinate of the element on the page.
function getAbsoluteLeft(element)
{
	var left = 0;
	while(element != null)
	{					
		left += element.offsetLeft;
		element = element.offsetParent; 				
	}
	return left; 
}
//------------------------------------------------------------------------------
// Returns absolute top coordinate of the element on the page.
function getAbsoluteTop(element)
{
	var top = 0;
	while(element != null)
	{					
		top += element.offsetTop;
		element = element.offsetParent; 				
	}
	return top; 
}
//------------------------------------------------------------------------------
// Displays confirmation window with the confirmation text.
// If user answers 'yes', navigates to the given URL.
// URL may be javascript: call.
function confirmUrl(url, confirmationText)
{
  if (window.confirm(confirmationText))
  {
    window.navigate(url);
  }
}
//------------------------------------------------------------------------------
// Page element to save/restore scroll position.
var pageScrollElement = null;
var pageScrollElementId = null;
//------------------------------------------------------------------------------
// Returns page element to save/restore scroll position.
function getPageScrollElement()
{
  if (pageScrollElement != null)
  {
    return pageScrollElement;
  }
  else if (pageScrollElementId != null)
  {
    return getElementById(pageScrollElementId);
  }
  return document != null ? document.body : null;
}
//------------------------------------------------------------------------------
// Sets page element to save/restore scroll position.
function setPageScrollElement(element)
{
  pageScrollElement = element;
}
//------------------------------------------------------------------------------
// Sets page element ID to save/restore scroll position.
function setPageScrollElementId(elementId)
{
  pageScrollElementId = elementId;
}
//------------------------------------------------------------------------------
// Returns main form element.
function getForm()
{
  return document != null && 
         document.forms != null && 
         document.forms.length > 0 ? document.forms[0] : null;
}
//------------------------------------------------------------------------------
// Saves current scroll position of the page.
function savePageScrollPosition()
{
  var element = getPageScrollElement();
  var form = getForm();
  if (element != null &&
      form != null &&
      form.pageScrollLeft != null &&
      form.pageScrollTop != null)
  {
    form.pageScrollLeft.value = element.scrollLeft;
    form.pageScrollTop.value = element.scrollTop;
  }
}
//------------------------------------------------------------------------------
// Restores page scroll position on document load.
function restorePageScrollPosition()
{
  var element = getPageScrollElement();
  var form = getForm();
  if (element != null &&
      form != null &&
      form.pageScrollLeft != null &&
      form.pageScrollTop != null)
  {
    if (element.scrollWidth > element.clientWidth)
    {
      element.scrollLeft = form.pageScrollLeft.value;
      element.scrollTop = form.pageScrollTop.value;
    }
    attachSubmitEvent(savePageScrollPosition);
  }
}
//------------------------------------------------------------------------------
// Functions to attach 'onSubmit' and 'onDoPostBack' event to the window.
//------------------------------------------------------------------------------
var doPostBackBody = null;
var postBackHandlerList = new Array();
//------------------------------------------------------------------------------
function __doFrameworkPostBack(eventTarget, eventArgument)
{
  for (var i = 0; i < postBackHandlerList.length; i++)
  {
    postBackHandlerList[i]();
  }
  if (doPostBackBody != null)
  {
    var originalHandler = new Function("eventTarget", "eventArgument", doPostBackBody);
    originalHandler(eventTarget, eventArgument);
  }
}
//------------------------------------------------------------------------------
function attachSubmitEvent(eventHandler)
{
  if (window.__doPostBack != null && doPostBackBody == null)
  {
    var funcText = window.__doPostBack.toString();
    doPostBackBody = funcText.substring(funcText.indexOf("{") + 1, funcText.lastIndexOf("}"));
    window.__doPostBack = 
      new Function("eventTarget", "eventArgument", "__doFrameworkPostBack(eventTarget, eventArgument);");
  }
  postBackHandlerList[postBackHandlerList.length] = eventHandler;
  var form = getForm();
  if (form != null)
  {
    form.attachEvent("onsubmit", eventHandler);
  }
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Sets focus to the given control.
function setFocus(element)
{
  if (element != null && 
      element.display != "none" && 
      element.disabled != true && 
      element.focus != null)
  {
    try
    {
      element.focus();
      return true;
    }
    catch (e)
    {
      return false;
    }
  }
  return false;
}
//------------------------------------------------------------------------------
// Sets focus to the control with the given ID.
function setFocusById(elementId)
{
  setFocus(getElementById(elementId));
}
//------------------------------------------------------------------------------
// Saves current scroll position of the page.
function saveFocusedElement()
{
  var form = getForm();
  if (document != null && 
      document.activeElement != null &&
      document.activeElement.id != null &&
      form != null &&
      form.focusedElementId != null)
  {
    form.focusedElementId.value = document.activeElement.id;
  }
}
//------------------------------------------------------------------------------
// Restores page scroll position on document load.
function restoreFocusedElement()
{
  var form = getForm();
  if (document != null &&
      form != null &&
      form.focusedElementId != null)
  {
    var elementId = form.focusedElementId.value;
    setFocusById(elementId);
    attachSubmitEvent(saveFocusedElement);
  }
}
//------------------------------------------------------------------------------
var idOfElementToFocusOnLoad = null;
//------------------------------------------------------------------------------
// Sets ID of element to be focused on page load.
function setOnLoadFocusElementId(id)
{
  idOfElementToFocusOnLoad = id;
}
//------------------------------------------------------------------------------
// Sets focus to element defined by setOnLoadFocusElementId function.
function setFocusOnLoad()
{
  setFocusById(idOfElementToFocusOnLoad);
}
//------------------------------------------------------------------------------
// Initializes utility scripts on page load.
function initUtilsOnLoad()
{
  restorePageScrollPosition();
  setFocusOnLoad();
  restoreFocusedElement();
}
//------------------------------------------------------------------------------
// Attaches OnLoad event handler to the window.
function attachOnLoadEvent(eventHandler)
{
  if (window.attachEvent)
  {
    window.attachEvent("onload", eventHandler);
  }
  else if (window.addEventListener)
  {
    window.addEventListener("load", eventHandler, false);
  }
  else if (document.getElementById)
  {
    window.onload = eventHandler;
  }
}
//------------------------------------------------------------------------------
// Removes query string parameters from URL.
function removeUrlParams(url)
{
  if (url != null)
  {
    var i = url.indexOf("?");
    if (i > 0)
    {
      url = url.substr(0, i);
    }
    else if (i == 0)
    {
      url = "";
    }
  }
  return url;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Initialization script. Commented out to make event attachment later (in Portal.js).
// window.attachEvent("onload", initUtilsOnLoad);
//------------------------------------------------------------------------------
