if (typeof(Newcorp) == "undefined") var Newcorp = new Object();
if (typeof(Newcorp.InteractiveSales) == "undefined") Newcorp.InteractiveSales = new Object();

// Defines the base URL of the sales tool. Must end with a forward slash: / 
// ============================================================================================
// TODO: Customize this to the sub-domain you will be using to host the Interactive Sales Tool.
// ============================================================================================
Newcorp.InteractiveSales._ClientBaseUrl = "http://protectionplan.shopnbc.com/";

// Launches the N.E.W. Interactive Sales Tool for a specific product
Newcorp.InteractiveSales.ShowForProduct = function(clientCode, languageCode, productSku, productPrice, productName, productCategory, sourceLocation, userToken, visitToken)
{
    var launchUrl = this._CreateBaseUrl(clientCode, languageCode, sourceLocation, userToken, visitToken);
    launchUrl += "&s=" + escape(productSku);                
    launchUrl += "&pp=" + escape(productPrice.toString());

    if (typeof(productName) == "string" && productName.length > 0)
    {
        launchUrl += "&pn=" + productName; //assumes already is URL encoded - KM@shopnbc
    }
    
    if (typeof(productCategory) == "string" && productCategory.length > 0)
    {
        launchUrl += "&pc=" + escape(productCategory);
    }

    this._Show(launchUrl);
};

// Launches the N.E.W. Interactive Sales Tool with generic content
Newcorp.InteractiveSales.ShowGeneric = function(clientCode, languageCode, sourceLocation, userToken, visitToken)
{
    var launchUrl = this._CreateBaseUrl(clientCode, languageCode, sourceLocation, userToken, visitToken);
    this._Show(launchUrl);
};

// Internal code used to define browser window settings
Newcorp.InteractiveSales._WindowTarget = "NewcorpInteractiveSales";
Newcorp.InteractiveSales._WindowWidth = 700;
Newcorp.InteractiveSales._WindowHeight = 384;

// Internal code used to generate url with the base parameters used to launch the Interactive Sales Tool
Newcorp.InteractiveSales._CreateBaseUrl = function(clientCode, languageCode, sourceLocation, userToken, visitToken)
{
    var baseUrl = this._ClientBaseUrl + "Interface.aspx";
    baseUrl += "?c=" + escape(clientCode);
    baseUrl += "&l=" + escape(languageCode);
    
    if (typeof(sourceLocation) == "string" && sourceLocation.length > 0)
    {
        baseUrl += "&rs=" + escape(sourceLocation);
    }
    
    if (typeof(userToken) == "string" && userToken.length > 0)
    {
        baseUrl += "&ru=" + escape(userToken);
    }
    
    if (typeof(visitToken) == "string" && visitToken.length > 0)
    {
        baseUrl += "&rv=" + escape(visitToken);
    }        

    return baseUrl;    
}

// Internal code used to open the Interactive Sales Tool browser window to a specified URL
Newcorp.InteractiveSales._Show = function(launchUrl)
{
    // The window should not have any user interface other than scroll bars and should not be resizable.
    var features = "toolbar=0,resizable=0,location=0,menubar=0,status=0,scrollbars=1,directories=0,top=0,left=0";
    features += ",width=" + this._WindowWidth.toString();
    features += ",height=" + this._WindowHeight.toString();

    // Open the window and detect whether a pop-up blocker was initiated.
    var windowRef = window.open(launchUrl, this._WindowTarget, features);        
    if (typeof(windowRef) != "undefined" && windowRef != null)
    {    
        windowRef.focus();
        return true;
    }
    else
    {
        return false;
    }
}
