var g_BrowserIsOK = false;
var g_debug = false;
var _DEBUG_ = null;
var g_baseURI = null;
var g_rootURI = null;
var g_isHTTP = null;
var g_hasDfLauncher = null;
var g_appVersion = null;
var g_XMLProgID = null;
var g_XSLProgID = null;
var g_XSLTemplateProgID = null;
var g_XMLHTTPProgID = null;
var g_GMKEY = null;
var g_egnVer = null;

function closeApp() // This is for g_isHTTP == false only
{
    DFEngine.cleanSession();
/*
    if (window.top.g_sessionsXML && window.top.g_sessionsXML.xml)
    {
        window.top.g_sessionsXML.selectNodes('/d/sessions/session[@id="' + window.top.g_sessionID + '"]').removeAll();
        window.top.g_sessionsXML.save(window.top.g_sessionsXMLFilePath);
    }
    window.opener = window.top; // for IE 6
    window.open('','_self'); // for IE 7
    window.close();
*/    
}

function createRecentProgID(arrProgIDs)
{
    try
    {
        for (var i = 0; i < arrProgIDs.length; i++)
        {
          try
          {
            var retObj = new ActiveXObject(arrProgIDs[i]);
            if (retObj) return arrProgIDs[i];
          } catch (e){};
        };
        arrProgIDs = null;
	}
	catch(e) {}
    return null;
}

function dfDiagnostics(isDebug)
{
    if (isDebug == undefined) isDebug = false;
    var strRes = '';

    var cookieEnable = true;
    if (g_isHTTP)
    {
        var chkcookie = ((new Date()).getTime() + '');
        document.cookie = 'chkcookie=' + chkcookie + '; path=/';
        cookieEnable = (document.cookie.indexOf(chkcookie, 0) >= 0);
    }
    if (!cookieEnable)
        return 'Your browser\'s cookie functionality is turned off, please turn it on.';

    var _isIE = document.all && window.ActiveXObject && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1;
    if (!_isIE)
        strRes += '<a href="http://www.microsoft.com/windows/internet-explorer/download-ie.aspx">Microsoft Internet Explorer</a> is required.<br/>';
    else
    {
        var _navigator = navigator.userAgent.toLowerCase();
        var _IEVerAtLeast55 = parseFloat(_navigator.substr(_navigator.indexOf("msie") + 5, 3)) >= 5.5;
        if (!_IEVerAtLeast55)
            strRes += '<a href="http://www.microsoft.com/windows/internet-explorer/download-ie.aspx">Microsoft Internet Explorer 5.5+</a> is required.\n';
        else
        {
            var _ActiveXObject = window.ActiveXObject;
            if (!_ActiveXObject)
                strRes += 'ActiveX must be enabled.\n';
            else
            {
                g_XMLProgID = createRecentProgID(["Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.3.0"]);
                g_XSLProgID = createRecentProgID(["Msxml2.FreeThreadedDOMDocument.6.0", "Msxml2.FreeThreadedDOMDocument.3.0"]);
                g_XSLTemplateProgID = createRecentProgID(["Msxml2.XSLTemplate.6.0", "Msxml2.XSLTemplate.3.0"]);
                g_XMLHTTPProgID = createRecentProgID(["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0"]);

     		    if (g_XMLProgID == null || g_XSLProgID == null || g_XSLTemplateProgID == null || g_XMLHTTPProgID == null)
     		    {
     		        if (isDebug)
     		        {
     		            if (g_XMLProgID == null) alert('Creating DOMDocument failed.');
     		            if (g_XSLProgID == null) alert('Creating FreeThreadedDOMDocument failed.');
     		            if (g_XSLTemplateProgID == null) alert('Creating XSLTemplate failed.');
     		            if (g_XMLHTTPProgID == null) alert('Creating XMLHTTP failed.');
     		        }
     		        strRes += '<a href="http://www.microsoft.com/downloads/details.aspx?familyid=d21c292c-368b-4ce1-9dab-3e9827b70604">MSXML ActiveX objects</a> cannot be created.\n';
                }
     		    else
     		    {
     		        if (isDebug) alert(g_XMLProgID + '\n' + g_XSLProgID + '\n' + g_XSLTemplateProgID + '\n' + g_XMLHTTPProgID);
     		        var objXML = new ActiveXObject(g_XMLProgID);
     		        var objXSL = new ActiveXObject(g_XSLProgID);
     		        var sss = '';
     		        try
     		        {
     		            var xmlFile = 'egn/xml/diagnostics.xml';
					    objXML.async = false;
					    objXML.load(xmlFile);
					    g_egnVer = objXML.selectSingleNode('/d').getAttribute('egn-ver');
					    objXSL.async = false;
					    objXSL.loadXML('<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1"><xsl:output method="text" omit-xml-declaration="yes"/><xsl:template match="/"><xsl:value-of select="count(/d)"/></xsl:template></xsl:stylesheet>');
					    sss = objXML.transformNode(objXSL);
					}
					catch(e)
					{
         		        if (isDebug) alert('Transform 1 failed.\n' + sss);
					};
					if (sss != '1')
					{
					    if (isDebug)
					    {
					        alert(xmlFile + '\n' 
					            + '<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1"><xsl:output method="text" omit-xml-declaration="yes"/><xsl:template match="/"><xsl:value-of select="count(/d)"/></xsl:template></xsl:stylesheet>');
					        alert('Transform 1 failed.\nobjXML=' + objXML.xml + '\nobjXSL=' + objXSL.xml + '\nResult=' + sss);
                        }
					    strRes += '<a href="http://www.microsoft.com/downloads/details.aspx?familyid=d21c292c-368b-4ce1-9dab-3e9827b70604">XML/XSL transformation</a> failed.\n';
                    }

     		        var objXMLHTTP = new ActiveXObject(g_XMLHTTPProgID);
     		        var sss = '';
     		        try
     		        {
     		            var xmlFile = 'app/xml/config.xml';
					    objXMLHTTP.open('GET', xmlFile, false);
					    objXMLHTTP.send(null);
					    objXML.async = false;
					    objXML.loadXML(objXMLHTTP.responseText);
					    sss = objXML.selectNodes('/d').length;
					    g_appVersion = unescape(objXML.selectSingleNode('/d/config/appVersion[text()!=""]').text);
					    g_debug = (objXML.selectSingleNode('/d/config/debug[text()="1"]') != null);
					    if (objXML.selectSingleNode('/d/config/msxml3[text()="1"]') != null)
					    {
                            g_XMLProgID = createRecentProgID(["Msxml2.DOMDocument.3.0"]);
                            g_XSLProgID = createRecentProgID(["Msxml2.FreeThreadedDOMDocument.3.0"]);
                            g_XSLTemplateProgID = createRecentProgID(["Msxml2.XSLTemplate.3.0"]);
                            g_XMLHTTPProgID = createRecentProgID(["Msxml2.XMLHTTP.3.0"]);
					    }
					}
					catch(e)
					{
					    if (isDebug)
					    {
					        alert(xmlFile + '\n' 
					            + '<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1"><xsl:output method="text" omit-xml-declaration="yes"/><xsl:template match="/"><xsl:value-of select="count(/d)"/></xsl:template></xsl:stylesheet>');
					        alert('Transform 2 failed.\nobjXML=' + objXML.xml + '\nobjXSL=' + objXSL.xml + '\nResult=' + sss);
                        }
					};
					if (sss != '1')
					{
					    if (isDebug) alert('Transform 2 failed.\n' + sss);
					    strRes += '<a href="http://www.microsoft.com/downloads/details.aspx?familyid=d21c292c-368b-4ce1-9dab-3e9827b70604">XMLHTTP responseText</a> failed.\n';
                    }
     		    }
            }
        }
    }
    return strRes;
}

try
{
    g_baseURI = window.top.location.href;
    g_baseURI = g_baseURI.substring(0, g_baseURI.lastIndexOf('/') + 1);
    g_rootURI = g_baseURI.substring(0, g_baseURI.indexOf('/', g_baseURI.indexOf('//') + 2) + 1);
    g_isHTTP = (g_baseURI.indexOf('file://') == -1);
    var strRes = dfDiagnostics();
    if (strRes == '')
    {
        g_BrowserIsOK = true;
        _DEBUG_ = window.top.g_debug;
        
        document.write('<link rel="shortcut icon" href="app/img/app.ico" type="image/x-icon"/>');
        document.write('<script type="text/javascript" src="egn/scr/yahoo/yahoo.js"></' + 'script>');
        document.write('<script type="text/javascript" src="egn/scr/yahoo/event.js"></' + 'script>');
        document.write('<script type="text/javascript" src="egn/scr/datafloat.js"></' + 'script><!-- namespace -->');
        document.write('<script type="text/javascript" src="egn/scr/dfhelper.js"></' + 'script><!-- global static, in top page only -->');
        document.write('<script type="text/javascript" src="egn/scr/dfxml.js"></' + 'script><!-- global static, in top page only -->');
        document.write('<script type="text/javascript" src="egn/scr/dfhtml.js"></' + 'script><!-- global static, in top page only -->');
        document.write('<script type="text/javascript" src="egn/scr/dfengine.js"></' + 'script><!-- global static, in top page only -->');
        document.write('<script type="text/javascript" src="egn/scr/dfmodel.js"></' + 'script><!-- singleton for each page -->');
        /*
        In dfhelper.js and dfxml.js, we should NOT refer to any window / document object. 
        In dfhtml.js, we always pass the window object as the first parameter. 
        In dfengine.js, we can hard code dGPS3's HTML framework.
        */
        document.write('<script type="text/javascript" src="egn/scr/globalstatic.js"></' + 'script><!-- global static, in top page only -->');
        document.write('<script type="text/javascript" src="app/scr/customized_mandatory.js"></' + 'script>');
        document.write('<script type="text/javascript" src="app/scr/customized_optional.js"></' + 'script>');
    }
    else document.write(strRes);
}
catch(e) 
{ 
	alert('window_top_include.js: ' + e.description); 
}

