﻿// 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;

    // 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 disables 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 = '';
        //grayOut(true, {'opacity': '5'});
    }

    // 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 = '';
        //grayOut(true, {'opacity': '5'});
    }

    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;
}

function grayOut(vis, opt) {
    // Pass true to gray out screen, false to ungray
    // options are optional.  This is a JSON object with the following (optional) properties
    // opacity: 0-100
    // Lower number = less grayout higher = more of a blackout
    // zindex: # HTML elements with a higher zindex appear on top of the gray out
    // bgcolor: (#xxxxxx)
    // Standard RGB Hex color code
    // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
    // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
    // in any order.  Pass only the properties you need to set.
    var options = opt || {};
    var zindex = options.zindex || 50;
    var opacity = options.opacity || 70;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';
    var dark = document.getElementById('darkenScreenObject');

    if (!dark) {
        // The dark layer doesn't exist, it's never been created.  So we'll
        // create it here and apply some basic styles.
        // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
        var tbody = document.getElementsByTagName("body")[0];
        var tnode = document.createElement('div');
        // Create the layer.
        tnode.style.position = 'absolute';
        // Position absolutely
        tnode.style.top = '0px';
        // In the top
        tnode.style.left = '0px';
        // Left corner of the page
        tnode.style.overflow = 'hidden';
        // Try to avoid making scroll bars
        tnode.style.display = 'none';
        // Start out Hidden
        tnode.id = 'darkenScreenObject';
        // Name it so we can find it later
        tbody.appendChild(tnode);
        // Add it to the web page
        dark = document.getElementById('darkenScreenObject');
        // Get the object.
    }

    if (vis) {
        var pageWidth;
        var PageHeight;
        // Calculate the page width and height
        if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
            pageWidth = document.body.scrollWidth + 'px';
            pageHeight = document.body.scrollHeight + 'px';
        }
        else
            if (document.body.offsetWidth) {
            pageWidth = document.body.offsetWidth + 'px';
            pageHeight = document.body.offsetHeight + 'px';
        }
        else {
            pageWidth = '100%';
            pageHeight = '100%';
        }

        //set the shader to cover the entire page and make it visible.
        dark.style.opacity = opaque;
        dark.style.MozOpacity = opaque;
        dark.style.filter = 'alpha(opacity=' + opacity + ')';
        dark.style.zIndex = zindex;
        dark.style.backgroundColor = bgcolor;
        dark.style.width = pageWidth;
        dark.style.height = pageHeight;
        dark.style.display = 'block';
    }
    else {
        dark.style.display = 'none';
    }
}