﻿// Get a reference to the PageRequestManager.
var postBackElement;
var prm;

// Use the prm reference, hook _initializeRequest and _endRequest
// so this code is executed at the begin & end of all async postbacks that occur.
prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
prm.add_pageLoaded(PageLoaded);
prm.add_beginRequest(BeginRequest);

function PageLoaded(sender, args) {
  if (typeof (postBackElement) == "undefined") {
    return;
  }
  
  // ---------------------------------------------------
  // Initiated by Search Button
  // ---------------------------------------------------
  if (postBackElement.id == "cmdSearch") {
    if ($get("AccountListRowCount").value > 0)
      OpenPanel("AccountListPanelBehavior");

    $get("pnlAccountListScrollPos").value = "0"
    $get("pnlLeftScrollPos").value = "0"
  }

  // ---------------------------------------------------
  // Initiated by Combobox
  // ---------------------------------------------------
  if (postBackElement.id == "cboSearch") {
    $get("pnlAccountList").scrollTop = $get("pnlAccountListScrollPos").value;
    $get("pnlLeft").scrollTop = $get("pnlLeftScrollPos").value;
  }

  // ---------------------------------------------------
  // Initiated by link in grid
  // ---------------------------------------------------
  if (postBackElement.id.toLowerCase().indexOf("grdaccountlist") > -1) {
    // reset the scroll positions of all panels that need to maintain vert scroll position
    $get("pnlAccountList").scrollTop = $get("pnlAccountListScrollPos").value;

    // Open Panels
    OpenPanel("AccountDetailPanelBehavior");

    if ($get("AccountDetailRowCount").value > 0) {
      OpenPanel("AccountListPanelBehavior");

      if ($get("SalesListRowCount").value > 0)
        OpenPanel("SalesListPanelBehavior");

      if ($get("ValuesListRowCount").value > 0)
        OpenPanel("ValuesListPanelBehavior");

      if ($get("ModelOccurrenceListRowCount").value > 0)
        OpenPanel("ModelsListPanelBehavior");
    }
  }

  if (postBackElement.id.toLowerCase().indexOf("dalmodeloccurrence") > -1) {
    // reset the scroll positions of all panels that need to maintain vert scroll position
    $get("pnlLeft").scrollTop = $get("pnlLeftScrollPos").value;
  }
}

function setScroll(val, hiddenFieldName) {
  var hiddenField;
  hiddenField = document.getElementById(hiddenFieldName);
  hiddenField.value = val.scrollTop;
}

// Executed anytime an async postback occurs.
function InitializeRequest(sender, args) {
  var pnlPopup;
  var pnlPopupTop;
  var pnlPopupLeft;

  // Cancel postback if lnkDisclaimer or cmdSearch
  var dlg = $get("pnlLoginOuter");
  if (args._postBackElement.id == 'lnkDisclaimer' || (args._postBackElement.id == 'cmdSearch' && dlg.style.display == '')) {
    args.set_cancel(true);
    return;
  }

  // Any postback except cboSearch and Model Occurrence Attributes
  if (args._postBackElement.id != 'cboSearch' && args._postBackElement.id.toLowerCase().indexOf("dalmodeloccurrence") <= -1) {
    // Get a reference to the element that raised the postback and disable it.
    args._postBackElement.disabled = true;

    pnlPopup = $get("pnlPopup");
    pnlPopupTop = $get("pnlUpdate").offsetTop + $get("pnlInside").offsetTop + $get("pnlAccountListHeader").offsetTop + 20;
    pnlPopupLeft = (f_clientWidth() / 2) - (parseInt(pnlPopup.style.width) / 2);

    pnlPopup.style.left = pnlPopupLeft + "px";
    pnlPopup.style.top = pnlPopupTop + "px";
    pnlPopup.style.display = '';
  }

  // If Model Occurrence Attribute link was clicked
  if (args._postBackElement.id.toLowerCase().indexOf("dalmodeloccurrence") > -1) {
    // Get a reference to the element that raised the postback and disables it.
    args._postBackElement.disabled = true;

    pnlPopup = $get("pnlPopup");
    pnlPopupTop = $get("pnlUpdate").offsetTop + $get("pnlInside").offsetTop + $get("pnlModelsHeader").offsetTop + 20;
    pnlPopupLeft = (f_clientWidth() / 2) - (parseInt(pnlPopup.style.width) / 2);

    pnlPopup.style.left = pnlPopupLeft + "px";
    pnlPopup.style.top = pnlPopupTop + "px";
    pnlPopup.style.display = '';
  }

  document.body.style.cursor = "wait";
}

function BeginRequest(sender, args) {
  postBackElement = args.get_postBackElement();
}

// Executed when the async postback completes.
function EndRequest(sender, args) {
  if (sender._postBackSettings.sourceElement.id != 'cboSearch' && sender._postBackSettings.sourceElement.id != 'trvModels') {
    // Get a reference to the element that raised the postback and disables it.
    sender._postBackSettings.sourceElement.disabled = false;
    // get the update progress div
    var pnlPopup = $get("pnlPopup");
    // make it invisible
    pnlPopup.style.display = 'none';
    //grayOut(false);
  }
  document.body.style.cursor = "auto";
}

function f_clientWidth() {
  return f_filterResults(window.innerWidth ? window.innerWidth : 0, document.documentElement ? document.documentElement.clientWidth : 0, document.body ? document.body.clientWidth : 0);
}

function f_clientHeight() {
  return f_filterResults(window.innerHeight ? window.innerHeight : 0, document.documentElement ? document.documentElement.clientHeight : 0, document.body ? document.body.clientHeight : 0);
}

function f_filterResults(n_win, n_docel, n_body) {
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel)))
    n_result = n_docel;
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

window.size = function() {
  var w = 0;
  var h = 0;

  //IE
  if (!window.innerWidth) {
    //strict mode
    if (!(document.documentElement.clientWidth == 0)) {
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
    }
    //quirks mode
    else {
      w = document.body.clientWidth;
      h = document.body.clientHeight;
    }
  }
  //w3c
  else {
    w = window.innerWidth;
    h = window.innerHeight;
  }
  return { width: w, height: h };
}

window.center = function() {
  var hWnd = (arguments[0] != null) ? arguments[0] : { width: 0, height: 0 };

  var _x = 0;
  var _y = 0;
  var offsetX = 0;
  var offsetY = 0;

  //IE
  if (!window.pageYOffset) {
    //strict mode
    if (!(document.documentElement.scrollTop == 0)) {
      offsetY = document.documentElement.scrollTop;
      offsetX = document.documentElement.scrollLeft;
    }
    //quirks mode
    else {
      offsetY = document.body.scrollTop;
      offsetX = document.body.scrollLeft;
    }
  }
  //w3c
  else {
    offsetX = window.pageXOffset;
    offsetY = window.pageYOffset;
  }

  _x = ((this.size().width - hWnd.width) / 2) + offsetX;
  _y = ((this.size().height - hWnd.height) / 2) + offsetY;

  return { x: _x, y: _y };
}

function showCenter(point, url) {
  var target = "_blank";
  var top = point.y + "px";
  var left = point.x + "px";
  // var win = window.open(url, target, "width=800, height=600, left=" + left + ", top=" + top + ", " + "location=no, menubar=yes, status=no, toolbar=no, scrollbars=yes, resizable=yes");
  var win = window.open(url, target, "width=800, height=600, left=" + left + ", top=100, " + "location=no, menubar=yes, status=no, toolbar=no, scrollbars=yes, resizable=yes");
  win.focus;
}