function ProcessCallBack(arg, context)
{
    var com = arg.substring(0, arg.indexOf(':'));
    arg = arg.substring(arg.indexOf(':') + 1);
    var divLoading = document.getElementById(arg.substring(0, arg.indexOf(':')));
    arg = arg.substring(arg.indexOf(':') + 1);
    if(com == "CreateTable")
        CreateTable(arg);
    else if(com == "REDIRECT")
        document.location.href = arg;
    divLoading.style.visibility = 'hidden';
    SetPageLocation();
    
}

function SetPageLocation()
{
    var pageDiv = document.getElementById('pageLocationDiv');
    var winHeight = getWindowHeight();
    var pageOffset = pageDiv.offsetTop;
    var topScroll;
    if(document.body.scrollTop)
    {
        topScroll = document.body.scrollTop;
    }
    else
    {
        topScroll = window.pageYOffset;
    }
    if(pageOffset < topScroll)
    {
        self.scrollTo(0, pageOffset - winHeight + 200)
    }
}


function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function CreateTable(tmpXML)
{
	tmpXML = tmpXML.replace(/&lsquo;/g,"'");
	var tableText = "";
	
	if (false)
	{
		var re = /&lt;/g;
		tmpXML=tmpXML.replace(re,"<");
		re = /&gt;/g;
		tmpXML=tmpXML.replace(re,">");
	}
	var xmlDoc;
	if (window.ActiveXObject) 
	{
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
	    xmlDoc.loadXML(tmpXML);
    // code for Mozilla, Firefox, Opera, etc.
    } 
    else 
    {
	    var xmlParser = new DOMParser();
	    xmlDoc = xmlParser.parseFromString(tmpXML, "text/xml");
    }
	
	
	tableText = "<table width='100%' border='0' cellspacing='0' cellpadding='0' class='SnapContent'>";
	tableText = tableText + getHeader(xmlDoc);
	tableText = tableText + getRows(xmlDoc);
	
	tableText = tableText + "</table>";
	tableText = tableText + getPages(xmlDoc);
    var HeaderCols = xmlDoc.getElementsByTagName("Header");
    if(HeaderCols.length > 0)
    {
	    
	    var tableDiv = document.getElementById(HeaderCols[0].getAttribute("ID"));
	    tableDiv.innerHTML = tableText;
	}
    
}

function getPages(xmlDoc)
{
    
    var pages = xmlDoc.getElementsByTagName("Pages");
    if(pages.length > 0)  
    {
        var page = pages[0].getElementsByTagName("Page");
        if(page.length > 1)
        {
            var pageText = "<table width='100%' border='0' cellpadding='2' cellspacing='0'><tr><td align='right'><b>Page:</b>&nbsp;";
	        for(k = 0; k < page.length; k++)
	        {
	            if(k > 0)
	            {
	                pageText = pageText + "&nbsp; | &nbsp;";
	            }
	            if(page[k].getAttribute("Type") == "AsyncPostBackLink")
	            {
	                pageText = pageText + "<a href='javascript:" + page[k].getAttribute("Command") + ";'>" + page[k].getAttribute("Text") + "</a>";
	            }
	            else
	            {
	                pageText = pageText + "<b>" + page[k].getAttribute("Text") + "</b>";
	            }
	        }
            pageText = pageText + "</td></tr></table>";
            return pageText;
        }
    }    
    return "";  
}

function getHeader(xmlDoc)
{
    var HeaderCols = xmlDoc.getElementsByTagName("Header");
    var HeaderText = "";
    if(HeaderCols.length > 0)
    {
        var HeaderCol = HeaderCols[0].getElementsByTagName("ColumnHeader");
	    HeaderText = "<tr class='grid'>";
	    for(k = 0; k < HeaderCol.length; k++)
	    {
	        HeaderText = HeaderText + "<th";
	        if(HeaderCol[k].getAttribute("Width") != null)
	            HeaderText = HeaderText + " style='width: " + HeaderCol[k].getAttribute("Width") + "px' ";
	        HeaderText = HeaderText + ">" + HeaderCol[k].getAttribute("Text")  + "</th>";
        }
	    HeaderText = HeaderText + "</tr>";
    }
    return HeaderText;
}

function getRows(xmlDoc)
{
    var RowCols = xmlDoc.getElementsByTagName("Row");
    var RowText = "";
    if(RowCols.length > 0)
    {
	    for(j = 0; j < RowCols.length - 1; j++)
	    {
	        RowText = RowText + getRow(RowCols[j]);
        }
        RowText = RowText + getLastRow(RowCols[RowCols.length-1]);
    }
    else
    {
        var NoItems = xmlDoc.getElementsByTagName("NoItems");
        if(NoItems.length > 0 && NoItems[0].getAttribute("Columns"))
        {
            if(NoItems[0].getAttribute("Columns"))
                RowText = "<tr><td align='center' colspan='" + NoItems[0].getAttribute("Columns") + "'><b>" + NoItems[0].getAttribute("Text") + "</b></td></tr>"
            
        }
    }
    return RowText;
}

function getRow(xmlNode)
{
    var RowText = "<tr class='gridItem' onmouseover=\"this.className = 'gridItemHover';\" onmouseout=\"this.className = 'gridItem';\">";
    var RowCol = xmlNode.getElementsByTagName("ColumnRow");
    for(k = 0; k < RowCol.length; k++)
    {
        RowText = RowText + getCol(RowCol[k]);
    }
    RowText = RowText + "</tr>";
    return RowText;
}

function getLastRow(xmlNode)
{
    var RowText = "<tr class='gridItemLast' onmouseover=\"this.className = 'gridItemHover';\" onmouseout=\"this.className = 'gridItemLast';\">";
    var RowCol = xmlNode.getElementsByTagName("ColumnRow");
    for(k = 0; k < RowCol.length; k++)
    {
        RowText = RowText + getCol(RowCol[k]);
    }
    RowText = RowText + "</tr>";
    return RowText;
}

function getCol(xmlNode)
{
    var colText = "<td";
    if(xmlNode.getAttribute("Align") != null)
        colText = colText + " align='" + xmlNode.getAttribute("Align") + "' ";
    if(xmlNode.getAttribute("Width") != null)
        colText = colText + " style='width: " + xmlNode.getAttribute("Width") + "px' ";
    colText = colText + ">"
    var type = xmlNode.getAttribute("Type");
    if(type == "Link")
    {
        colText = colText + "<a href='" + xmlNode.getAttribute("URL") + "'>" + xmlNode.getAttribute("Text") + "</a>";
    }
    else if(type == "AsyncPostBackLink")
    {
        colText = colText + "<a ";
        if(xmlNode.getAttribute("Class") != null)
            colText = colText + "class='" + xmlNode.getAttribute("Class") + "' ";
        colText = colText + "href='javascript:" + xmlNode.getAttribute("Command") + ";'>" + xmlNode.getAttribute("Text") + "</a>";
    }
    else if(type == "CheckBox")
    {
        if(xmlNode.getAttribute("Checked") == "True")
            colText = colText + "<input type='checkbox' checked='checked' disabled='disabled' />";
        else
            colText = colText + "<input type='checkbox' disabled='disabled' />";
    }
    else
    {
        colText = colText + xmlNode.getAttribute("Text");
    }
    colText = colText + "</td>";
    return colText;    
}
function DisplayLoadingMessage(loadDivId, tableDivId)
{

    var loaddiv = document.getElementById(loadDivId);
    var tablediv = document.getElementById(tableDivId);
    
    loaddiv.style.pixelLeft = (tablediv.offsetWidth / 2) - (44);
    loaddiv.style.pixelTop = (tablediv.offsetHeight / 2) - 5;
    loaddiv.style.visibility = 'visible';

}

var urlAddress = "http://naples.adagetech.com"; 
var pageName = "UL Standards Certification Customer Library"; 

function addToFavorites() 
{ 
    if (window.sidebar) 
        window.sidebar.addPanel(pageName, urlAddress,"");
    else if( window.opera && window.print )
    {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',urlAddress);
        mbm.setAttribute('title',pageName);
        mbm.click();
    }
    else if( document.all ) 
        window.external.AddFavorite( urlAddress, pageName);
    else 
        alert("Sorry! Your browser doesn't support this function."); 
}

function PageQuery(q) 
{
    if(q.length > 1) 
        this.q = q.substring(1, q.length);
    else 
        this.q = null;
    this.keyValuePairs = new Array();
    if(q) 
    {
        for(var i=0; i < this.q.split("&").length; i++) 
        {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) 
    {
        for(var j=0; j < this.keyValuePairs.length; j++) 
        {
            if(this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
        }
        return false;
    }
    this.getParameters = function() 
    {
        var a = new Array(this.getLength());
        for(var j=0; j < this.keyValuePairs.length; j++) 
        {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key)
{
    var page = new PageQuery(window.location.search);
    return unescape(page.getValue(key));
}

function AddToBookmark()
{
    var fav = queryString('Fav');
    if(fav == 'true')
    {
        if(window.confirm('Would you like to add the UL Standards Certification Customer Library to your Favorites?'))
        {
            addToFavorites();
        }
    }
}