﻿// This File is the Browser Component's API Functions.
//-----------------------------------------------------

// Instructions:
//--------------
// 	!!!	The users can call the functions below from their scripts   !!!
//
// SelfNavigate(url, newSize)		-  in order to navigate to another page and change the width of the 		//		                  component on the toolbar.
//
// SelfNavigate(url)				-  in order to navigate to another page.
// 
// ChangeWidth(newSize)				-  in order to change the component's width on the toolbar.
//
// ChangeHeight(newSize)			-  in order to change the component's height on the toolbar.
//
// GetCurrentWidth()				-  in order to get current width in pixels.
//
// GetCurrentHeight()				-  in order to get current height in pixels.
//
// GetSearchTerm()					-  in order to get current search term that written inside the search box.
//
// GetMainFrameUrl()				-  in order to get the current url that updated when every navigation is // //                                complete.
//
// GetMainFrameTitle()				-  in order to get the current url that updated when every navigation is // //                                complete.
//
// GetVersion()						-  in order to get the toolbar's version.
//
// OpenFloatingWindow()				-  in order to open new floating window.
//
// CloseFloatingWindow()			-  in order to close current floating window (like window.close())
//
// ChangeSize(newWidth, newHeight)	-  in order to change width and height of the floating window.
//
// NavigateInMainFrame(url)    -  in order to navigate (run scripts) in main frame
//
// StoreKey(key,value)              - stores key with value on users computer (encypted)
//
// RetrieveKey(key)                 - retrieves key from user computer
//
// DeleteKey(key)                   - deleted key on user computer
//
// GetLastError()                  - Gets last error (empty string - none, 1 - unauthorized)

function StoreKey(key,value)
{
    SetLastError("");
    if (isFF && BCAPI.storeKey)
        BCAPI.storeKey(key, value);
    else {
        var strApiFunc = 'EBStoreKey(' + key + ',' + value + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}

function RetrieveKey(key)
{
    SetLastError("");
    if (isFF && BCAPI.retrieveKey)
        return BCAPI.retrieveKey(key);
    else {
        myKey = '';
        var strApiFunc = 'EBRetrieveKey(' + key + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
        return myKey;
    }
}

function DeleteKey(key)
{
    SetLastError("");
    if (isFF && BCAPI.deleteKey)
        BCAPI.deleteKey(key);
    else {
        myKey = '';
        var strApiFunc = 'EBDeleteKey(' + key + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}
    
function SelfNavigate(url, newSize)
{
	if(typeof(newSize) == 'undefined')
	    newSize = '-1';

	if (isFF && BCAPI.selfNavigate)
	    BCAPI.selfNavigate(url, newSize);
	else {
	    var strApiFunc = 'EBNavigate(' + url + ',' + newSize + ');';
	    var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
	    location.assign(unescape(strAssignUrl));
	}
}

function ChangeWidth(newSize)
{
    SetLastError("");
    if (isFF && BCAPI.changeWidth)
        BCAPI.changeWidth(newSize);
    else {
        var strApiFunc = 'EBChangeSize(' + newSize + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}

function ChangeHeight(newSize)
{
    SetLastError("");
    if (isFF && BCAPI.changeHeight)
        BCAPI.changeHeight(newSize);
    else {
        var strApiFunc = 'EBChangeSize(' + myWidth + ',' + newSize + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}

function RefreshToolbar()
{
    SetLastError("");
    if (isFF && BCAPI.refreshToolbar)
        BCAPI.refreshToolbar();
    else {
        var strApiFunc = 'EBRefreshToolbar();';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
} 

function EBOpenPopHtml(url,newWidth,newHeight)
{
	OpenFloatingWindow(url,newWidth,newHeight);
}

function OpenFloatingWindow(url,newWidth,newHeight)
{
    SetLastError("");
    if (isFF && BCAPI.openGadget)
        BCAPI.openGadget(url, newWidth, newHeight);
    else {
        var strApiFunc = 'EBOpenPopHtml(' + url + ',' + newWidth + ',' + newHeight + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}

function OpenGadget(url, width, height, features) {
    if (!features) features = "";
    SetLastError("");
    if (isFF && BCAPI.openGadget2)
        BCAPI.openGadget2(url, width, height, features);
    else {
        var strApiFunc = 'EBOpenGadget(\'' + url + '\',' + width + ',' + height + ',' + features + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}

function CloseFloatingWindow()
{
    SetLastError("");
    if (isFF && BCAPI.closeWindow)
        BCAPI.closeWindow();
    else {
        var strApiFunc = 'EBCloseFloatingWindow();';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}

function ChangeSize(newWidth,newHeight)
{
    SetLastError("");
    if (isFF && BCAPI.changeSize)
        BCAPI.changeSize(newWidth, newHeight);
    else {
        var strApiFunc = 'EBChangeSize(' + newWidth + ',' + newHeight + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}

function GetToolbarUnknownCommandApiFuncUrl(strApiFunc)
{
	var strToolbarDomain = GetToolbarDomain();
	
	return strToolbarDomain + "customtemplmngr.aspx?ctl=UnknownCommand&ToolbarId=EB_TOOLBAR_ID&apifunc=" + strApiFunc;
}

function NavigateInMainFrame(strUrl)
{
    SetLastError("");
    if (isFF && BCAPI.navigateInMainFrame)
        BCAPI.navigateInMainFrame(strUrl);
    else {
        var strApiFunc = 'EBMainFrameNavigation(' + strUrl + ');';
        var strAssignUrl = GetToolbarUnknownCommandApiFuncUrl(strApiFunc);
        location.assign(unescape(strAssignUrl));
    }
}

function GetToolbarDomain()
{
	SetLastError("");
	
	return "http://www.conduit.com/";
}

function GetMainFrameUrl()
{
    SetLastError("");
    if (isFF && BCAPI.getMainFrameUrl)
        return BCAPI.getMainFrameUrl();
    else {
        if (!myIsAllowNonPrivacy)
            SetLastError(1);
        return mainFrameUrl;
    }
}

function GetMainFrameTitle()
{
    SetLastError("");
    if (isFF && BCAPI.getMainFrameTitle)
        return BCAPI.getMainFrameTitle();
    else {
        if (!myIsAllowNonPrivacy)
            SetLastError(1);
        return mainFrameTitle;
    }
}

function GetCurrentHeight()
{
    SetLastError("");
    if (isFF && BCAPI.getHeight)
        return BCAPI.getHeight();
    else
        return myHeight;
}

function GetSearchTerm()
{
    SetLastError("");
    if (isFF && BCAPI.getSearchTerm)
        return BCAPI.getSearchTerm();
    else {
        if (!myIsAllowNonPrivacy)
            SetLastError(1);
        return recentSearchTerm;
    }
}

function GetVersion()
{
    SetLastError("");
    if (isFF && BCAPI.getVersion)
        return BCAPI.getVersion();
    else
        return myVersion;
}

function GetLastError()
{
    if (isFF && typeof BCAPI.lastError != "undefined")
        return BCAPI.lastError;
    else
        return myLastError;
}

function GetCurrentWidth()
{
    SetLastError("");
    if (isFF && BCAPI.getWidth)
        return BCAPI.getWidth();
    else
        return myWidth;
}

var recentSearchTerm = "";
var mainFrameUrl = "";
var mainFrameTitle = "";
var myWidth = "";
var myHeight = "";
var myVersion = "";
var myKey   = "";
var myIsAllowNonPrivacy = true;
var myLastError = "";

function SetLastError(strError)
{
    if (isFF && typeof BCAPI.lastError != "undefined")
        BCAPI.lastError = strError;
    else
        myLastError = strError;
}

function EBSetIsAllowNonPrivacyFunctions(strBool)
{
    //for IE
    if(typeof strBool == "string")
        myIsAllowNonPrivacy = (strBool.toLowerCase() == "true") ? true : false;
    // for FF
    else
        myIsAllowNonPrivacy = strBool;
}

function EBSetVersion(version)
{
	myVersion = version;
}

function EBSetCurrentWidth(newWidth)
{
	myWidth = newWidth;
}

function EBSetCurrentHeight(newHeight)
{
	myHeight = newHeight;
}

function EBSetSearchTerm(newSearchTerm)
{
	recentSearchTerm = newSearchTerm;
}

function EBSetMainFrameUrl(newMainFrameUrl)
{
	mainFrameUrl = newMainFrameUrl;
}

function EBSetMainFrameTitle(newMainFrameTitle)
{
	mainFrameTitle = newMainFrameTitle;
}

function EBSetKey(strValue)
{
    myKey = strValue;
}

//BCAPIObject used for Safari MAC
var BCAPI = (typeof BCAPIObject != "undefined") ?  BCAPIObject : new Object();

var isFF = !window.ActiveXObject;