﻿//=========================================
// Application Ids
//=========================================
var idApp = -1;

var Id_GLC = 100;
var Id_IBA = 101;
var Id_BZC = 102;
var Id_IRS = 103;
var Id_MMC = 105;
var Id_BES = 104;
var Id_MMC = 105;
var Id_DDL = 106;
var Id_SBI = 107;
var Id_MGD = 108;	// Mergerstat P&D
var Id_MGE = 109;	// Mergerstat P/E
var Id_DLM = 110;
var Id_RMA = 111;

var Id_KVD_GLC = 200;
var Id_KVD_IBA = 201;
var Id_KVD_BZC = 202;

// NOTE: KVD AppIds are different.  Probably just need to add these to the switch() cases
var Id_KVD_GLC = 200;
var Id_KVD_IBA = 201;
var Id_KVD_BZC = 202;
var Id_KVD_IRS = 203;
var Id_KVD_RMA = 211;

//=========================================

var IGNORE = -9999;

//=========================================
function wgGetHdrMenuWidth()
{
	var cxMenu = 420;

	try
	{
		cxMenu = parseInt($("#hfCxMenu").val());
	}
	catch(err)
	{
	}
	return cxMenu;
	// old way
    // Use this for VS framework
    //return 270;

    // Use this for KVD framework
    //return 420;
}

function wgHidePane(pn) { if (pn != null) pn.style.visibility = "hidden"; }
function wgShowPane(pn) { if (pn != null) pn.style.visibility = "visible"; }
function wgSizePane(pn, nLeft, nTop, nWidth, nHeight)
{
    if (pn != null)
    {
        if (nWidth < 0) nWidth = 0;
        if (nHeight < 0) nHeight = 0;

        pn.style.left = nLeft + "px";
        pn.style.top = nTop + "px";
        pn.style.width = nWidth + "px";
        pn.style.height = nHeight + "px";
    }
}
function wgSetPaneSize(pn, nWidth, nHeight)
{
    if (pn != null)
    {
        if (nWidth < 0) nWidth = 0;
        if (nHeight < 0) nHeight = 0;

        pn.style.width = nWidth + "px";
        pn.style.height = nHeight + "px";
    }
}
function wgMovePane(pn, nLeft, nTop)
{
    if (pn != null)
    {
        pn.style.left = nLeft + "px";
        pn.style.top = nTop + "px";
    }
}
function wgSetPaneHeight(pn, cy)
{
   if (pn != null && cy >= 0)
      pn.style.height = cy + "px";
}
function wgSetPaneLeft(pn, nLeft)
{
    if (pn != null)
        pn.style.left = nLeft + "px";
}
function wgSetPaneTop(pn, nTop)
{
    if (pn != null)
        pn.style.top = nTop + "px";
}
function wgCenterPane(pn, nWidth)
{
    if (pn != null)
    {
        nWidth = parseInt(nWidth);
        var cxPane = parseInt(pn.style.width);
        var nLeft = (nWidth - cxPane) / 2;
        if (nLeft < 0) nLeft = 0;
                
        pn.style.left = nLeft + "px";
        pn.style.top = "0px";
    }
}
function wgSizePaneId(pnId, nLeft, nTop, nWidth, nHeight)
{
    var pn = $get(pnId);
    if (pn != null)
    {
        if (nWidth < 0) nWidth = 0;
        if (nHeight < 0) nHeight = 0;

        pn.style.left = nLeft + "px";
        pn.style.top = nTop + "px";
        pn.style.width = nWidth + "px";
        pn.style.height = nHeight + "px";
    }
    return pn;
}
function wgMovePaneId(pnId, nLeft, nTop)
{
    var pn = $get(pnId);
    if (pn != null)
        wgMovePane(pn, nLeft, nTop);
}
function wgCenterPaneInBrowser(pn)
{
    // center for width, not height
    if (pn != null)
    {
        var cx = 1024;    // if all else fails
        if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
            cx = document.documentElement.clientWidth;
        else
            cx = window.innerWidth;

        wgCenterPane(pn, cx);
    }
}
function wgVmovePaneTo(pn, x, y1, y2)
{
    if (pn != null)
    {
        pn.style.left = x + "px";
        pn.style.top = y1 + "px";
        pn.style.height = (y2 - y1) + "px";
        pn.style.width = "1px";
        wgShowPane(pn);
    }
}
function wgHmovePaneTo(pn, y, x1, x2)
{
    if (pn != null)
    {
        pn.style.left = x1 + "px";
        pn.style.top = y + "px";
        pn.style.width = (x2 - x1) + "px";
        pn.style.height = "1px";
        wgShowPane(pn);
    }
}

function wgSizeGrid(pn, gridId)
{
    if (pn != null)
    {
        var grid = igtbl_getGridById(gridId);
        if (grid != null)
        {
            grid.resize(pn.clientWidth, pn.clientHeight);
            //wgSizeLastGridCol(grid, pn.clientWidth);
        }
    }
}
function wgRemoveCommas(sText)
{
    var regexp = /,/g;
    return(sText.replace(regexp, ""));
}
function wgSyncIgButton(id, bEnable)
{
    var btn = ig_getWebControlById(id);
    if (btn != null)
    {
        btn.setEnabled(bEnable);
        btn.paint(true);
    }
}
function wgSetTextIgButton(id, sText)
{
    var btn = ig_getWebControlById(id);
    if (btn != null)
    {
        btn.setText(sText);
    }
}
function wgClearIgValue(id)
{
    var ctl = igedit_getById(id);
    if (ctl != null)
        ctl.setValue("");
}
function wgClearIgText(id)
{
    var ctl = igedit_getById(id);
    if (ctl != null)
        ctl.setText("");
}
function wgGetIgText(id)
{
   var rc = "";
   var ctl = igedit_getById(id);
   if (ctl != null)
      rc = ctl.getText();
   return rc;
}
function wgIgFilterDigits(oEdit, keyCode, oEvent)
{
    // do not accept any chars that are not in the allowed string below
    var filter = "0123456789";
    var newChar = String.fromCharCode(keyCode);
    var rc = filter.indexOf(newChar);

    if (rc == -1)
        oEvent.cancel = true;
}
function wgIgFilterFloatNeg(oEdit, keyCode, oEvent)
{
   // do not accept any chars that are not in the allowed string below
   var filter = "0123456789.-";
   var newChar = String.fromCharCode(keyCode);
   var rc = filter.indexOf(newChar);

   if (rc == -1)
      oEvent.cancel = true;
}
function wgIgFilterFloat(oEdit, keyCode, oEvent)
{
    // do not accept any chars that are not in the allowed string below
    var filter = "0123456789.";
    var newChar = String.fromCharCode(keyCode);
    var rc = filter.indexOf(newChar);

    if (rc == -1)
        oEvent.cancel = true;
}
function wgIgFilterInt(oEdit, keyCode, oEvent)
{
    // do not accept any chars that are not in the allowed string below
    var filter = "1234567890,+-";
    var newChar = String.fromCharCode(keyCode);
    var rc = filter.indexOf(newChar);

    if (rc == -1)
        oEvent.cancel = true;
}
function wgIgFilterAlphaNumericSp(oEdit, keyCode, oEvent)
{
    // do not accept any chars that are not in the allowed string below
    var filter = "abcdefghijklmnopqrstuvwxyz0123456789 ";
    var newChar = String.fromCharCode(keyCode);
    newChar = newChar.toLowerCase();
    var rc = filter.indexOf(newChar);

    if (rc == -1)
        oEvent.cancel = true;
}
function wgIgFilterSicNaics(oEdit, keyCode, oEvent)
{
    // do not accept any chars that are not in the allowed string below
    var filter = "0123456789. ";
    var newChar = String.fromCharCode(keyCode);
    var rc = filter.indexOf(newChar);

    if (rc == -1)
        oEvent.cancel = true;
}
// Not currently using this.  (Curently using AjaxControlToolkit for filtering.)
//function gc_keyPressFilterSicNaics(oEdit, keyCode, oEvent)
//{
//    // do not accept any chars that are not in the allowed string below
//    var filter = "0123456789, ";
//    var newChar = String.fromCharCode(keyCode);
//    var rc = filter.indexOf(newChar);
//
//    if (rc == -1)
//        oEvent.cancel = true;
//}
function wg_GridCellClick(gridName, cellId, button)
{
    // server processes column clicks, but does not want cell clicks
    return true;    // cancel the server event
}
function wg_goToBlankWindow(url)
{
    var newWin;
    newWin = window.open(url, '_blank');
    if (newWin != null)
        newWin.focus();
    return true;
}
function wg_goToVsHelpWindow(url)
{
    var newWin;
    newWin = window.open(url, 'Vs_Help');
    if (newWin != null)
        newWin.focus();
    return true;
}
function wg_goToVsAppWindow(url)
{
    var newWin;
    newWin = window.open(url, 'Vs_App', 'width=1024,height=768,close,minimizable,resizable');
    if (newWin != null)
        newWin.focus();
    return true;
}
function wg_goToKvdAppWindow(url)
{
    var newWin;
    newWin = window.open(url, 'Vs_App', 'width=1024,height=768,close,minimizable');
    if (newWin != null)
        newWin.focus();
    return true;
}
function getTabContentWidth(pTabs, cTabs)
{
   return pTabs.clientWidth - cTabs.Tabs[0].elemDiv.offsetLeft - 2;
}
function getTabContentHeight(pTabs, cTabs)
{
   var tabItem = cTabs.Tabs[0];
   return pTabs.clientHeight - tabItem.elemDiv.offsetTop - tabItem.element.offsetHeight - 1;
}
function sizeTabContent(cTabs, iTabIndex, contentId, cxContent, cyContent)
{
   var pn = cTabs.Tabs[iTabIndex].findControl(contentId);
   if (pn != null)
      wgSizePane(pn, 0, 0, cxContent, cyContent);
   return pn;
}

//=========================================
// Generic
//=========================================
function doc_get(sId)
{
   return document.getElementById(sId);
}
function eleSetPos(ele, nLeft, nTop, nWidth, nHeight)
{
   if(nLeft >= 0) ele.style.left = nLeft + "px";
   if(nTop >= 0) ele.style.top = nTop + "px";
   if(nWidth >= 0) ele.style.width = nWidth + "px";
   if(nHeight >= 0) ele.style.height = nHeight + "px";
}
function ele_sizeToBrowser(pn)
{
   if (pn != null)
   {
      var cx = 1024, cy = 768;    // if all else fails
      if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
      {
         cx = document.documentElement.clientWidth;
         cy = document.documentElement.clientHeight;
      }
      else
      {
         cx = window.innerWidth;
         cy = window.innerHeight;
      }

      eleSetPos(pn, 0, 0, cx, cy);
   }
   return pn;
}
function pn_sizeToBrowser(pnId)
{
   var pn = $get(pnId);
   if (pn != null)
   {
      var cx = 1024, cy = 768;    // if all else fails
      if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
      {
         cx = document.documentElement.clientWidth;
         cy = document.documentElement.clientHeight;
      }
      else
      {
         cx = window.innerWidth;
         cy = window.innerHeight;
      }

      wgSizePane(pn, 0, 0, cx, cy);
   }
   return pn;
}
function pn_resize(pn, nLeft, nTop, nWidth, nHeight)
{
   if (pn != null)
   {
      if (nLeft != IGNORE) pn.style.left = nLeft + "px";
      if (nTop != IGNORE) pn.style.top = nTop + "px";

      if (nWidth != IGNORE)
      {
         if (nWidth < 0) nWidth = 0;
         pn.style.width = nWidth + "px";
      }
      if (nHeight != IGNORE)
      {
         if (nHeight < 0) nHeight = 0;
         pn.style.height = nHeight + "px";
      }
   }
}
function pnId_left(pnId, nLeft)
{
   var pn = $get(pnId);
   if (nLeft != IGNORE) pn.style.left = nLeft + "px";
   return pn;
}
function pnId_moveTo(pnId, nLeft, nTop)
{
   var pn = $get(pnId);
   if (nLeft != IGNORE) pn.style.left = nLeft + "px";
   if (nTop != IGNORE) pn.style.top = nTop + "px";
   return pn;
}
function pnId_resize(pnId, nLeft, nTop, nWidth, nHeight)
{
   var pn = $get(pnId);
   pn_resize(pn, nLeft, nTop, nWidth, nHeight);
   return pn;
}
function pnId_stretch(pnId, nLeft, nWidth)
{
   var pn = $get(pnId);
   pn_resize(pn, nLeft, IGNORE, nWidth, IGNORE);
   return pn;
}
function pnId_show(pnId)
{
   var pn = $get(pnId);
   wgShowPane(pn);
   return pn;
}
function pnId_hide(pnId)
{
   var pn = $get(pnId);
   wgHidePane(pn);
   return pn;
}
function pnId_showHide(pnId, bShow)
{
   if (bShow)
      pnId_show(pnId);
   else
      pnId_hide(pnId);
}
//=========================================
// Named
//=========================================
function pnMain_resize()
{
   return pn_sizeToBrowser("pnMain");
}

//=========================================
//=========================================
function wgEndRequestErrorHandled(sender, args)
{
   if (args.get_error() != undefined)
   {
      var errorMessage;
      if (args.get_response().get_statusCode() == '200')
         errorMessage = args.get_error().message;    // error on server
      else
         errorMessage = 'An unspecified error occurred. ';   // error not on server

      args.set_errorHandled(true);
      alert(errorMessage);
      return true;
   }
   else
      return false;
}

function startDownload(sUrl)
{
   //sUrl = "wg_res/bzcExportData.xls";
   //window.open(sUrl, 'Download');	// works, new window
   //window.location.href = sUrl;	// works
   //window.location = sUrl;	// works

   window.location = sUrl; // works
}
function wgSizeProgress(cxMain, cyMain)
{
   var cxProg = 240;
   var cyProg = 80;
   pnId_resize("ctlProgress_pnProgress", (cxMain - cxProg) / 2, (cyMain - cyProg) / 2, cxProg, cyProg);
}
function jQ_enable(selector, bEnable)
{
   if (bEnable)
      $(selector).removeAttr("disabled");
   else
      $(selector).attr("disabled", "disabled");
}
function jQ_show(selector, bShow)
{
   if (bShow)
      $(selector).show();
   else
      $(selector).hide();
}

//==============================================================================
//                           Client Side Dialog
//==============================================================================
var PM_Hdr_GetMsgRenew = 1;
var PM_Hdr_GetMsgExports = 2;
var m_id_dlgMsg = "ctlDlgMsg_dlgIMsg";

function hdr_setExportsLeft(sExports)
{
   $("#lblExports").text(sExports);
}
function hdr_renewMsg_Success(results)
{
   var aList = results.split("\r");
   if(idApp == Id_IBA)
	   wjDlgMsg_show(aList[0], aList[1], "259px");
   else
		wjDlgMsg_show(aList[0], aList[1], "209px");
}
function hdr_renewMsg_clicked()
{
   wgPageMethodMsg(PM_Hdr_GetMsgRenew, "", hdr_renewMsg_Success, wgMsg_Failure);
   return false; // cancel default action
}
function hdr_exportsMsg_Success(results)
{
   var aList = results.split("\r");
     if(idApp == Id_IBA)
	   wjDlgMsg_show(aList[0], aList[1], "243px");
   else
		wjDlgMsg_show(aList[0], aList[1], "193px");
}
function hdr_exportsMsg_clicked()
{
   wgPageMethodMsg(PM_Hdr_GetMsgExports, "", hdr_exportsMsg_Success, wgMsg_Failure);
   return false; // cancel default action
}
function getDlgMsgId()
{
   return m_id_dlgMsg;
}
function wgGetAppId()   { return $("#hfidApp").val(); }
function IsValidAppId() { return (idApp > 0); }
function wgGetUserId()  { return $("#hfidUsr").val(); }
function htmlOption(sVal, sText)
{
   return "<option value='" + sVal + "'>" + sText + "</option>";
}
function wgPageMethodMsg(idMsg, sData, funSuccess, funFailure)
{
   var idUsr = wgGetUserId();
   PageMethods.wgMsg(idMsg, idApp, idUsr, sData, funSuccess, funFailure);
   return false; // cancel default action
}
function wgMsg_Failure(errors)
{
   alert(errors.get_message());
}
function wjDlgMsg_onClick()
{
   var dlgId = getDlgMsgId();
   var dialog = $find(dlgId);
   dialog.hide();
   return false;
}
function wjDlgMsg_show(sCaption, sMsg, sHeight)
{
   // make sure you are using the ucjDlgMsg control
   var dlgId = getDlgMsgId();
   var dialog = $find(dlgId);

   $("#" + dlgId + " .igdw_Office2007BlueHeaderCaption").text(sCaption);
   $("#" + dlgId + " .lblPopMsg").html(sMsg);

   dialog.show();
   if (sHeight != "")
      dialog.setSize(null, sHeight, null);   // cx, cy.  Docs say cy, cx

   // header is null until shown
   //dialog.get_header().setCaptionText(sCaption);
}
//=========================================
// End
//=========================================
// must have this, unless is part of an assembly
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

