<!-- 
/************************************************************************

FILE: scripts.js
DESCRIPTION: Functions and variables used by KURLS to install and/or
initiate delivery into the KDM 4.02.

Copyright (c) 2004-2005 Kontiki Inc.

*************************************************************************/

//////////////////////////////////////////////////////////////////////////
//
// Configurable variable: An absolute URL to the index.html file of the
// implementation.
//
// NOTE: Since this variable may be called from files on various servers,
// this *must* be an absolute URL.
//
//////////////////////////////////////////////////////////////////////////
var gIndexPageUrl =
"http://kdx.kontiki.com/securedelivery/reference/index.html" // production


//////////////////////////////////////////////////////////////////////////
//
// Configurable variable: Domain to a default DMS and a default client
// model MOID, in case one is not passed in.
//
//////////////////////////////////////////////////////////////////////////
gDefaultDMS = "web-g.kontiki.com";
gDefaultModel = "be8b0e04-e87c-21ab-3af3-f422e16ae90f";

//////////////////////////////////////////////////////////////////////////
//
// Configurable variable: Array of known DMS domains. Associate the KID
// namespace to the domain of the DMS's End User Webapp.
//
//////////////////////////////////////////////////////////////////////////
var gDMSMap = new Array();
gDMSMap.kontiki = "web-g.kontiki.com";
gDMSMap.stage = "web-stage.kontiki.com"
gDMSMap.winqa = "web-wqa.kontiki.com";


//////////////////////////////////////////////////////////////////////////
//
// Can dyamically point to different implementations for testing. We find
// the referring domain and redirect accordingly.
//
//////////////////////////////////////////////////////////////////////////
var theDomain = getDomain(window.location.href); // Get the hosting domain.
if (theDomain == "kdx.kontiki.com") // Kontiki Production environment
    gIndexPageUrl = "http://kdx.kontiki.com/securedelivery/reference/index.html";
else if (theDomain == "kdx-stage.kontiki.com") // Kontiki Staging environment
    gIndexPageUrl = "http://kdx-stage.kontiki.com/securedelivery/kdmx_secure/index.html";
else if (theDomain == "kontiki.corp.kontiki.com") // Kontiki Dev environment
    gIndexPageUrl = "http://kontiki.corp.kontiki.com/Departments/engineering/release/clients/maui/kdmx_secure/index.html";
else if (theDomain == "localhost") // Local developer environment
    gIndexPageUrl = "http://localhost/maui/index.html"; 

//alert("index page: " + gIndexPageUrl);

//////////////////////////////////////////////////////////////////////////
//
// FUNCTION: launchKDX(moid)
//
// DESCRIPTION: Pre-4.02 launcher for backward compatibility. Note: The
// "s" param is not used by the 4.02 client, but it is necessary for
// non-interception by KDM 3.x that may be already installed.
//
//////////////////////////////////////////////////////////////////////////
function launchKDX(moid)
{     getIt("moid="+moid+"&model=" + gDefaultModel + "&domain=" + gDefaultDMS + "&s=kdx&alerts=false");
}

//////////////////////////////////////////////////////////////////////////
//
// FUNCTION: launchPlugin(moid, profile)
//
// DESCRIPTION: Pre-4.02 launcher for backward compatibility. Note: The
// "s" param is not used by the 4.02 client, but it is necessary for
// non-interception by KDM 3.x that may be already installed.
//
//////////////////////////////////////////////////////////////////////////
function launchPlugin(moid, profile)
{
getIt("moid="+moid+"&model=" + gDefaultModel + "&domain=" + gDefaultDMS + "&s=kdx&alerts=false");
}

//////////////////////////////////////////////////////////////////////////
//
// FUNCTION: getIt(getitAttributes)
//
// DESCRIPTION: 4.02 launcher. Initiates installation and/or delivery
// into the KDM 4.02 client and passes along values via the "getitAttributes"
// string. Note: The "s" param is not used by the 4.02 client, but it is 
// necessary for non-interception by KDM 3.x that may be already
// installed.
//
//////////////////////////////////////////////////////////////////////////
function getIt(getitAttributes)
{

    //popup vars
    var x = 0;
    var y = 0;
    var width = 391;
    var height = 106;

    if( isPlatformSupported() )
    {
        if (screen)
        {
            x = (screen.availWidth - width)/2;
            y = (screen.availHeight - height)/2;
        }
        var winAttributes = "width=" +width+ ",height=" +height
                   + ",screenX=" +x+ ",screenY=" +y+ ",top=" +y+ ",left=" +x
                   + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizeable=no";
        var startPage = gIndexPageUrl + "?" +getitAttributes;
        var popupWin = window.open(startPage,"popup",winAttributes);
        popupWin.focus();
    }
    else
    {
        doFallback(getitAttributes);
    }
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: doFallback(attributes)
//
// DESCRIPTION: Redirects to fallback page. Uses values passed in via
// "attributes" to determine fallback server. Note: This function should be
// customized for implementations that have a different fallback flow.
////////////////////////////////////////////////////////////////////////////////
function doFallback(attributes)
{
    var moid = "";
    var urn = "";
    var domain = gDefaultDMS;

    
    if(attributes != "")
    {
        args = parseArgs(attributes);
        if(args["moid"]) moid = args["moid"]; else moid = "null";
        if(args["kget"]) moid = args["kget"];
    }

    if(moid == "null")
    {
        alert(gUpgradeFailedMessage);
        if(self.name == "popup") setTimeout("window.close()",5000); // Close if popup window.
        return;
    }

    
    if (isUrn(moid))
    {
        urn = moid;
        moid = normalizeMoid(moid);
        domain = getDMS(urn);
    }
    
    var fallbackURL = "http://" + domain + "/zodiac/servlet/zodiac/template/pub,install,GetIt.vm/moid/" + moid;
    if (urn.length > 0)
        fallbackURL += "/kget/" + urn;
    fallbackURL += "/s/kdx/fallback/kdx";

    if (self.name == "popup")
    {
        self.resizeTo( 600, 500 );
    }
    //alert(fallbackURL);
    doRedirect(fallbackURL);
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: doRedirect(newURL)
//
// DESCRIPTION: If user is in a popup window, calls location.replace(),
// otherwise calls location.href() for optimized user experience.
////////////////////////////////////////////////////////////////////////////////
function doRedirect(newURL)
{
    if((document.images) && (isPlatformSupported()) && (self.name == "popup"))
        location.replace(newURL);
    else location.href = newURL;
}

//////////////////////////////////////////////////////////////////////////
// FUNCTION: isPlatformSupport()
//
// DESCRIPTION: Detects browser/platform and returns true
// if user is supported for KDM 4.02.
//////////////////////////////////////////////////////////////////////////
function isPlatformSupported()
{
    var agt = navigator.userAgent.toLowerCase();
    var major = parseInt(navigator.appVersion); 

    var osIsOk = ( (agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1) // Windows 98
    || (agt.indexOf("win 9x 4.90")!=-1) || (agt.indexOf("windows me")!=-1)// Windows ME
    || (agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1) // Windows NT
    || (agt.indexOf("windows 2000")!=-1) // Windows 2000
    || (agt.indexOf("xp")!=-1) ); // Windows XP

    var isXPSP2 = window.navigator.userAgent.indexOf("SV1") != -1;
    var isIE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var isIE3 = (isIE && (major < 4));
    var isIE4 = (isIE && (major == 4) && (agt.indexOf("msie 4") !=-1));
    var isIE5up = (isIE && !isIE3 && !isIE4);

    return isIE5up && osIsOk;
}

//////////////////////////////////////////////////////////////////////////
// FUNCTION: isXPSP2()
//
// DESCRIPTION: Returns true for XPSP2 platforms.
//////////////////////////////////////////////////////////////////////////
function isXPSP2()
{
    return window.navigator.userAgent.indexOf("SV1") != -1;
}
                
//////////////////////////////////////////////////////////////////////////
// FUNCTION: doXPSP2()
//
// DESCRIPTION: On Install and Upgrade Pages, display hidden XPSP2 UI
//////////////////////////////////////////////////////////////////////////
function doXPSP2()
{
    standardDiv.style.display = "none";
    popupDiv.style.display = "none";
    xpsp2.style.display = "inline";
    if (window.resizeTo) self.resizeTo(610,374);
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: isUrn(i_urn)
//
// DESCRIPTION: Returns true if "i_urn" is of urn syntax
// (i.e., urn:kid:kontiki:somenetwork:somemoid)
////////////////////////////////////////////////////////////////////////////////
function isUrn(i_urn)
{
    if (i_urn.indexOf("urn") != -1)
        return true;
    else
        return false;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: normalizeMoid(i_urn)
//
// DESCRIPTION: Converts string "i_urn" of urn syntax to a moid.
////////////////////////////////////////////////////////////////////////////////
function normalizeMoid(i_urn)
{
    var index = 0;
    var tempString;
    if (i_urn.indexOf("urn") != -1)
    {
        while(true) 
        {
            index = i_urn.indexOf(":", index);
            if (index == -1 )
                break;
                
            index++;
            tempString = i_urn.substring(index, i_urn.length);
        }    
        
        return tempString;
    }
    else 
        return i_urn;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: getDMS(i_urn)
//
// DESCRIPTION: Parses out the DMS domain of the URN "i_urn". The map "m" needs
// to be customized for implementations used by externally deployed DMS's.
////////////////////////////////////////////////////////////////////////////////
function getDMS(i_urn)
{
    var index1;
    var index2;
    var index3;
    var tempString = "";
    var defaultDomain = gDefaultDMS;
    
    // parse out the kid from the urn
    if (i_urn.indexOf("urn") != -1)
    {
        index1 = (i_urn.indexOf(":") + 1);
        index2 = (i_urn.indexOf(":", index1) + 1);
        index3 = i_urn.indexOf(":", index2);
        tempString = i_urn.substring(index2, index3);
    }
        
    if(tempString != "") domain = gDMSMap[tempString];
    if(domain) return domain;
    else return defaultDomain;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: parseHTMLArgs()
//
// DESCRIPTION: Returns a string of param/value pairs from the 
// location.search query string
////////////////////////////////////////////////////////////////////////////////
function parseHTMLArgs()
{
    if( document.location.search.length > 0 )
    {
	    //skip the '?'
        args = parseArgs(document.location.search.substring(1));
        return args;
    }
    else return "";
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: parseArgs(string)
//
// DESCRIPTION: Returns JS object of param/value pairs from "string" of syntax
// "foo=bar&fee=bat&fie=bas"
////////////////////////////////////////////////////////////////////////////////
function parseArgs(string)
{
    var args = new Object();
    var pairs = string.split("&");
        
    for(var i = 0; i < pairs.length; i++)
    {
        // Look for "name=value"
        var pos = pairs[i].indexOf('='); 
        // if not found, skip to next
        if (pos == -1) continue; 
        // Extract the name
        var argname = pairs[i].substring(0,pos); 
    
        // Extract the value
        var value = unescape( pairs[i].substring(pos+1) );
        // Store as a property
        args[argname] = value; 
    
        //alert(argname + " = " + value);
    }
    return args;
}

////////////////////////////////////////////////////////////////////////////////
// FUNCTION: getDomain(i_url)
//
// DESCRIPTION: Returns "server.domain.ext" of "i_url" of syntax
// "http://server.domain.ext/foobar/file.foo"
////////////////////////////////////////////////////////////////////////////////
    
function getDomain(i_url)
{
    var index = i_url.indexOf("://");
    var domain = i_url.substring(index + 3);
    domain = domain.substring(0, domain.indexOf("/"));
    return domain;
}
//-->
