﻿/// <summary>
/// Get the client id of a control
/// </summary>
/// <remarks>
/// DvD 2008-07-04 Initial implementation
/// </remarks>
function GetClientId(id) {
    var elementCount = document.getElementsByTagName('*').length; // gets all elements, instead of Forms as this only returns FORM elements
    var i = 0;
    var clientId;
    for (i = 0; i < elementCount; i++) {
        clientId = document.getElementsByTagName('*')[i].id;
        pos = clientId.indexOf(id);
        if (pos >= 0) {
            break;
        }
    }
    return (clientId);
  }

  /// <summary>
  /// Fit the Page in the viewport of the browser
  /// </summary>
  /// <remarks>
  /// DvD 2009-08-12 Initial implementation
  /// </remarks>
  function SetPageDimensions() {
    var viewportDimensions = GetViewportDimensions();

    if (viewportDimensions != null) {
      var container = document.getElementById("container");

      if (container != null) {
        container.style.width = viewportDimensions.Width + "px";
        container.style.height = viewportDimensions.Height + "px";

        var header = document.getElementById("header");
        var menu = document.getElementById("menu");
        var content = document.getElementById("content");

        if (header != null && menu != null && content != null) {
          header.style.width = viewportDimensions.Width + "px";

          var menuHeight = viewportDimensions.Height > header.clientHeight ? (viewportDimensions.Height - header.clientHeight) : 0;
          menu.style.height = menuHeight + "px";

          var contentWidth = viewportDimensions.Width > menu.clientWidth ? (viewportDimensions.Width - menu.clientWidth) : 0;
          content.style.width = contentWidth + "px";

          var contentHeight = viewportDimensions.Height > header.clientHeight ? (viewportDimensions.Height - header.clientHeight) : 0;
          content.style.height = contentHeight + "px";        
        }
      }
    }
  }

  /// <summary>
  /// Returns the width and heigth of the viewport from the browser
  /// </summary>
  /// <remarks>
  /// DvD 2009-08-12 Initial implementation
  /// </remarks>
  function GetViewportDimensions() {
    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerHeight
    if (typeof window.innerWidth != 'undefined') {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0) {
      viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
    }

    // older versions of IE

    else {
      viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }

    return { Width: viewportwidth, Height: viewportheight };
  }

/// <summary>
/// Closing the commentsPopupExtender using the Esc Key
/// </summary>
/// <source>
/// http://msdn.microsoft.com/en-us/magazine/cc164247.aspx
/// </source>
/// <remarks>
/// DvD 2008-12-03 Initial implementation
/// </remarks>
function pageLoad(sender, args) {
  if (!args._isPartialLoad) {
    $addHandler(document, "keydown", OnKeyPress);
  }
}

function OnKeyPress(args) {
  if (args.keyCode == Sys.UI.Key.esc) {
    hidePopup();
  }
}


/// <summary>
/// Select the whole text in a textBox
/// </summary>
/// <remarks>
/// DvD 2008-12-09 Initial implementation
/// </remarks>
function SelectTextBoxContent(id) {
    var textBox = document.getElementById(GetClientId(id));
    textBox.focus();
    textBox.select();
}


/// <summary>
/// Enable or disable the fileUploadButton
/// </summary>
/// <remarks>
/// DvD 2008-12-15 Initial implementation
/// </remarks>
function EnableFileUploadButtonControl() {
    var control = document.getElementById(GetClientId('fileUploadControl'));
    if (control.value.length > 0) {
        EnableControl('fileUploadButton');
    }
    else {
        DisableControl('fileUploadButton');
    }
}

/// <summary>
/// Enable or disable a control
/// </summary>
/// <remarks>
/// DvD 2008-12-15 Initial implementation
/// </remarks>
function EnableControl(id) {
    document.getElementById(GetClientId(id)).disabled = false;
}

function DisableControl(id) {
    document.getElementById(GetClientId(id)).disabled = true;
}

/// <summary>
/// Handle the clicks from XSLT
/// </summary>
/// <remarks>
/// DvD 2008-07-04 Initial implementation
/// </remarks>
function getDetailRecord(itemOnPage) {
    fireXsltAsyncPostback('detailRecord;' + itemOnPage);
}

function pointerFileSearch(itemOnPage) {
    fireXsltAsyncPostback('pointerFileSearch;' + itemOnPage);
}

function deletePointerFile(itemOnPage) {
    fireXsltAsyncPostback('deletePointerFile;' + itemOnPage);
}

function refreshPointerFileList() {
    fireXsltAsyncPostback('refreshPointerFileList;');
}

function changeAccount() {
    fireXsltAsyncPostback('changeAccount;');
}

function selectRecord(priref) {
    fireXsltAsyncPostback('selectRecord;' + priref);
}

function deSelectRecord(priref) {
    fireXsltAsyncPostback('deSelectRecord;' + priref);
}

function searchLink(fieldName, value) {
    fireXsltAsyncPostback('searchLink;' + fieldName + '=' + value);
}

function showMultiMediaPopup(position) {
    showPopup('multiMediaViewer;' + position);
}

function showCommentsMultimediaPopup(priref) {
    showPopup('commentsMultiMediaViewer;' + priref);
}

function showPopup(contentControlId) {
    fireXsltAsyncPostback('showPopup;' + contentControlId);
}

function hidePopup() {
    fireXsltAsyncPostback('hidePopup');
}

function fireXsltAsyncPostback(value) {
    document.getElementById(GetClientId('PassTroughXsltAsyncPostBacks')).value = value;
    __doPostBack(GetClientId('PassTroughXsltAsyncPostBacks'), '');
}

function oldSchoolPopup(url) {
    mywindow = window.open(url, "popup", "location=0,status=0,scrollbars=1,width=640,height=480,resizable=1");
    mywindow.focus();
} 
