﻿
var DynamicsInfolog = {};

DynamicsInfolog.InfologCtrl = function(startElement)
{
	if(DynamicsCommon.IsNull(startElement))
		startElement = DynamicsCommon.GetFirstChildElement(document.body);
		
    return DynamicsCommon.FindInstance(startElement, "AxInfologCtrl");
}

DynamicsInfolog.InfologTable = function(startElement)
{
    return DynamicsCommon.FindInstance(startElement, "AxInfologMainTable");
}

DynamicsInfolog.OnClickClear = function(infologTableID)
{
	var table = $get(infologTableID);	
    if(table)
    {
		var row;
		for (row = 1; row < table.rows.length; row++)   //Skip first row which is the "Clear" link
		    table.deleteRow(row);
		
		table.style.display = 'none';   //hide the table
				
		var webPart = DynamicsInfolog.GetWebPartRoot();
		if (webPart)
		    webPart.style.display = 'none';   //hide the web part    
    }
}

DynamicsInfolog.AddError = function(message)
{	    
    DynamicsInfolog.AddInfologRow(message, "Error");
}

DynamicsInfolog.AddInfo = function(message)
{
    DynamicsInfolog.AddInfologRow(message, "Info");
}

DynamicsInfolog.AddWarning = function(message)
{
    DynamicsInfolog.AddInfologRow(message, "Warning");
}

DynamicsInfolog.AddInfologRow = function(message, exceptionType)
{
    var infologCtrl = DynamicsInfolog.InfologCtrl();
    
    // No infolog on the page
    if(infologCtrl == null)
        return;
        
    var infologTable = DynamicsInfolog.InfologTable(infologCtrl);
    
    if(infologTable == null)
        return;                    
    
    var row = infologTable.insertRow();
    var cellImage = row.insertCell(0);
    var cellText = row.insertCell(1);
    
    //Setup image cell
    cellImage.className = 'infologCellImage';
    cellImage.AxExceptionType = exceptionType;
    
    var img = document.createElement('IMG');
    img.src = '/_layouts/ep/images/' + exceptionType + '.gif';
    cellImage.appendChild(img);
    
    //Setup text cell
    cellText.className = 'infologCellText';
    cellText.AxExceptionType = exceptionType;
    cellText.innerHTML = DynamicsCommon.htmlEncode(message);
     
    infologTable.style.display = 'block';   //Make sure it is visible
    
    var webPart = DynamicsInfolog.GetWebPartRoot();
    if (webPart)
        webPart.style.display = '';         //Make sure the webPart is visible
        
    //Make sure we can see the infolog
    DynamicsInfolog.ScrollToTop(infologTable.id);    
}

//Make sure the infolog display style is properly set
//Since the infolog isn't always a web part, this might be skipped
DynamicsInfolog.InitializeWebPart = function(id)
{    
    if (DynamicsCommon.IsNull(id))
        return;
 
    var webPartID = 'MSOZoneCell_WebPart' + id;       
    var webPart = $get(webPartID);
    if (!webPart)    
        return;    
     
    var mainTable = DynamicsInfolog.InfologTable(webPart);
    if (!mainTable)
        return;
    
    var visible = (mainTable.rows.length > 1);    //We compare to 1 since first row is always "Clear" link
    if (visible)
    {
        //Make sure we can see the infolog
        //Do this on end request, after the postback finished
        var pageRequestMgr = Sys.WebForms.PageRequestManager.getInstance();	
        if (pageRequestMgr && pageRequestMgr.get_isInAsyncPostBack())        
            pageRequestMgr.add_endRequest(DynamicsInfolog.HandleEndRequest);               
    }
        
    //We need the row that hosts our web part zone
    var row = webPart.parentNode;
    if (!row)
        return;
     
    var zoneTable = DynamicsCommon.GetParentElement(row, "table");
    if (zoneTable && zoneTable.rows.length == 1)
    {
        //The row we want is for the whole zone, not just our web part zone
        row = DynamicsCommon.GetParentElement(zoneTable, "tr");
    }
     
    //Set row's visibility
    row.style.display = (visible?'':'none');
    
    //Store row in order to quickly retreive it later
    DynamicsInfolog.rootWPRow = row;
}

DynamicsInfolog.GetWebPartRoot = function()
{
    return DynamicsInfolog.rootWPRow;
}

DynamicsInfolog.HandleEndRequest = function()
{
    //Remove ourself from endRequest
    var pageRequestMgr = Sys.WebForms.PageRequestManager.getInstance();	
    if (pageRequestMgr)    
        pageRequestMgr.remove_endRequest(DynamicsInfolog.HandleEndRequest);    
    
    DynamicsInfolog.ScrollToTop();
}

DynamicsInfolog.ScrollToTop = function(infologCtrlID)
{
    var ctrl;
    if (infologCtrlID)
        ctrl = $get(infologCtrlID);
    if (!ctrl)
        ctrl = DynamicsInfolog.InfologCtrl();
        
    if (ctrl)
    {
        var tbl=DynamicsInfolog.InfologTable(ctrl);
        if(tbl && ((tbl.offsetTop < document.body.scrollTop) || ((tbl.offsetTop + tbl.offsetHeight) > (document.body.scrollTop + document.body.clientHeight))))
            tbl.scrollIntoView(true)
    }
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();