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

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.omn.org/securedelivery/omn/index.html";

//////////////////////////////////////////////////////////////////////////
//
// Configurable variable: Domain to a default DMS and a default client
// model MOID, in case one is not passed in.
//
//////////////////////////////////////////////////////////////////////////
gDefaultFolder = "omn";
gDefaultDMS = "dms-h.omn.org";
gDefaultModel = "e152d04a-85b1-847a-fc41-4cdbd1ccc595";


//////////////////////////////////////////////////////////////////////////
//
// 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.omn = "dms-h.omn.org";
gDMSMap.dev = "web-dev.corp.kontiki.com";
gDMSMap.dl = "web-dl.corp.kontiki.com";
gDMSMap.kontiki = "web-g.kontiki.com";
gDMSMap.stage = "web-stage.kontiki.com"
gDMSMap.winqa = "web-wqa.kontiki.com";
gDMSMap.ey = "web-ey.kontiki.com";
gDMSMap.verizontest = "sacs1lrcdd01.ITUTL1.ITCENT.EBIZ.VERIZON.COM";

var gFolderMap = new Array();
gFolderMap.omn = "omn";
gFolderMap.omn_stage = "omn_stage";
gFolderMap.client_demo = "demo";
//////////////////////////////////////////////////////////////////////////
//
// 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 == "kdx.omn.org" || theDomain == "guide-h.omn.org" || theDomain=="") // Kontiki Omn environment
{
    gIndexPageUrl = "http://kdx.omn.org/securedelivery/";
}
else if (theDomain == "localhost") // Local developer environment
    gIndexPageUrl = "http://localhost/maui/index.html"; 
else if (theDomain == "localhost:8080") // Local developer environment
    gIndexPageUrl = "http://localhost:8080/kdx/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_CINEQUEST(moid)
{
    launchKDX(moid,"CQUEST");
}

//////////////////////////////////////////////////////////////////////////

function launchKDX(moid,company)
{
    var af_URI = getAffCompany(company); 
    moid = (moid==null)? "" : moid;

    var theDomain = getDomain(window.location.href);
    if(theDomain == "kdx.omn.org"
        || theDomain == "guide-h.omn.org"
        || theDomain == "")
    {
        gIndexPageUrl = "http://kdx.omn.org/securedelivery/";
        gDefaultDMS = getDMS(moid);
        gDefaultFolder = getOMNFolder(moid);
        gIndexPageUrl+= gDefaultFolder+"/index.html";
    }
    var moidParam = (moid=="")? "" :"moid="+moid+"&";
    getIt(moidParam+"model=" + gDefaultModel + "&domain=" + gDefaultDMS + "&s=kdx&alerts=false"+af_URI);
}

//////////////////////////////////////////////////////////////////////////

affCompanys = {
        KQED:"http://guide-h.omn.org/clientsetup/kqed/settings.xml",
        WGBH:"http://guide-h.omn.org/clientsetup/wgbh/settings.xml",
        CQUEST:"http://guide-h.omn.org/clientsetup/cinequest/settings.xml" };

function getAffCompany(company)
{
    switch(company)
    {
    case "KQED": 
    case "WGBH": 
    case "CQUEST": 
        return "&url="+encodeURI( affCompanys[company] );
    default:
        return "";
    }
}
//////////////////////////////////////////////////////////////////////////
//
// 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 = 407;
    var height = 120;

    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=no,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 = "";
        if(args["kget"]) moid = args["kget"];
    }
    
    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 );
    }
    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 platform = navigator.platform;

    var osIsOk =  (platform == "Win32" ||  platform =="MacPPC" || platform =="Mac68k" );


/*    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);
    var isFirefox = navigator.userAgent.indexOf("Firefox") >= 0;
    var isSafari = navigator.userAgent.indexOf("Safari") >= 0

    return (isIE5up || isFirefox ||isSafari) && osIsOk;
}
//////////////////////////////////////////////////////////////////////////
// FUNCTION: isBrowserSupported()
//
// DESCRIPTION: Detects browser and returns true
// if browser is supported.
//////////////////////////////////////////////////////////////////////////
function isBrowserSupported()
{
    var agt = navigator.userAgent.toLowerCase();
    var isIE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

    return isIE;
}

//////////////////////////////////////////////////////////////////////////
// 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()
{
    standard.style.display = "none";
    xpsp2.style.display = "inline";
    if (window.resizeTo) 
    {
    if (self.name== "popup")
    self.resizeTo(570,209);
    else
    self.resizeTo(600,400);
    }
}

////////////////////////////////////////////////////////////////////////////////
// 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 domain = 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];
    return domain;
}
function getOMNFolder(i_urn)
{
    var index1;
    var index2;
    var index3;
    var index4;
    var tempString = "";
    // 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) + 1);
        index4 = i_urn.indexOf(":", index3);
        tempString = i_urn.substring(index3, index4);
    }
    if(tempString != "" && gFolderMap[tempString]) 
        gDefaultFolder = gFolderMap[tempString];
    return gDefaultFolder;
}
////////////////////////////////////////////////////////////////////////////////
// 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;
}

////////////////////////////////////////////////////////////////////////////////
// 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;
}

function encodeURI(str)
{
        str = escape(str);
    str = str.replace(/x/g, "xx");
    str = str.replace(/%/g, "xy");
    str = str.replace(/\//g,"xz");
        return str;
}

function uncodeURI(str)
{
        str =str.replace(/xz/g,'/');
        str =str.replace(/xy/g,'%');
        str =str.replace(/xx/g,'x');
        str = unescape(str);
        return str;
}

//-->
