var _opener = null; 
var _help = null;
var _orientation = null;
var _simulatorMode = false;
var _isMacMozilla = (navigator.userAgent.indexOf('Firefox/2.0') != -1) && (navigator.userAgent.indexOf('Mac') != -1);
var _isIE = navigator.appName == 'Microsoft Internet Explorer';
var _isSafari = (navigator.userAgent.indexOf('Safari') != -1);
var _ieDecorationWidth = -1;
var _ieDecorationHeight = -1;
var _ieYOffset = -1;
var _ieXOffset = -1;
var _lastWidth;
var _savedWidth;


/**
 * Run when the streamer applet is successfully started
 */
function AppletStarted()
{
    
	//window.moveBy(50, 50);
	
	var appletDiv = document.getElementById("divJSApplet");
	var splashDiv = document.getElementById("_ctlSplash");
	appletDiv.style.margin = "0px";
	appletDiv.style.position = "absolute";
	appletDiv.style.left = "0px";
	appletDiv.style.top = "0px";
	appletDiv.style.width = "100%";
	appletDiv.style.height = "100%";

	splashDiv.style.display = "none";

}

function logoff()
{
    if(document._ctlApplet) {
	    document._ctlApplet.performLogoff();
	}
}

/**
 * Open a SANC
 * @param {key} key used to look up item in _data array
 * @param {symbol} symbol to append to url
 * @param {index} indicates which account user is logged into
 */
function PopWin(key, symbol, index) {

	try {
	    _data;
	} catch (e) {
	    _data = frames[0]._data;
	    _baseURL = frames[0]._baseURL;
	    _mode = frames[0]._mode;
	    
	}
	
	if (_mode == "simulator"){
	    _simulatorMode = true;
	}

    // Set null values to empty string 
	if (symbol == null)
		symbol = "";
	if (index == null)
		index = "";
	if (key == null)
		key = "";
	if (_data == null)
		return;
	var sUrl = _data[key];

    //These two lines of code have been added due to an issue in the Mac where the parameters are not recognized as
    //strings so the replace functions below render them as undefined
	symbol = symbol + '';
	index = index + '';
	key = key + '';
	
	//if the array item cannot be found, check for Simulator mode
	if (_data[key] == null){
		
		var msg = "";
		if (_simulatorMode == true){
			msg = "Streetsmart.com is currently running in Simulator mode.";
			msg += "\nSchwab.com links are not enabled while in simulator mode.";
			msg += "\nTo access these links, please login to schwab.com and";
			msg += "\nlaunch StreetSmart.com in live mode.";
		} else {
			msg = "This link is disabled in " + _mode + " mode.";
		}
		alert(msg);
		return;
	}
	
    // insert parameters needed in URL 
	sUrl = sUrl.replace('{0}', _baseURL);
	sUrl = sUrl.replace('{1}', symbol);			  
	sUrl = sUrl.replace('{2}', index);

	if ((sUrl.indexOf("www.schwab.com") < 0) && (sUrl.indexOf("www.schwabat.com") < 0)){
    	sUrl = sUrl.replace('//','/');
    }

    
	if (sUrl.indexOf("http") == -1 && _baseURL != "/"){
		sUrl = "https://" + sUrl;
	}

	try {
		var popup = requiresPopUp(sUrl);
		if (popup != null) {
			popup.focus();
			return;
		}
		if (!_opener) {
			if (parent.opener) {
			    try {
				if (parent.opener.top){
					_opener = parent.opener.top;
				}
				else {
					_opener = parent.opener;
				}
			    } catch(exc) {
				_opener = parent.opener;
			    }
			} else {
				_opener = window;
			}
		}

		var isOpenerClosed = false;
		try {
			isOpenerClosed = _opener.closed;
		} catch(exc) {
			isOpenerClosed = true;
		}
		
        sUrl = sUrl.replace(/\s/g,"%20");
        
		if (_opener == window || isOpenerClosed ) {

			if (_simulatorMode == true && sUrl.indexOf("www.schwab.com") == -1) {
				_opener = ShowPopWin (sUrl, "",800,600,1,"yes");
			} else {
				_opener = window.open (sUrl, "", "");
				// popup was blocked
				if(_opener == null) {
					document._ctlApplet.reportBlockedPopup(sUrl);
					return;
				}
			}
			_opener.focus();
		} else {
			
			//_opener.name = "PopWin";
			_opener.location = sUrl;
		}
		_opener.focus();
		window.moveTo(0,0);
	} catch (e){
		showPopUpWarning(sUrl,"PopWin");
		throw(e);
	}
}


/**
 * ???
 * @param {url} 
 */
 
function requiresPopUp(url) {
	if (url.toLowerCase().indexOf('kentower') > -1 || url.toLowerCase().indexOf('marketedge') > -1
		|| url.toLowerCase().indexOf('/trading/margin?') > -1) {
		return ShowPopWin(url,"PopUp",800,500,1,1,0,0,0,0,1);
	}
	return null;
}

/**
 * ???
 * @param {page} 
 * @param {target} 
 */
function showPopUpWarning (page, target) {
	var div = document.getElementById('_PopUpWarning');
	var link = document.getElementById('_PopUpLink');
	link.href = page;
	link.target = target;
	div.style.display = "";
}

/**
 * ???
 */
function hidePopUpWarning () {
	var div = document.getElementById('_PopUpWarning');
	div.style.display = 'none';
}

/**
 * close the window or the parent window
 */	
function closeWin() {
	if (window.parent != null) {
		parent.close();
	}
		window.close();
	}


/**
 * ???
 * @param {page} 
 */
function popWin(page) {
	PopWin(page);
}

/**
 * show the Help window
 * @param {url} 
 */
function showHelp(url) {

	if (_help == null || _help.closed) {
		_help = ShowPopWin(url,"HelpWin",850,500,1,1,0,0,0,0,1);
	} else {
		_help.close();
		_help = ShowPopWin(url,"HelpWin",850,500,1,1,0,0,0,0,1);
	}
	try {
		if(_help != null) {
			_help.focus();
		}
	} catch (e){
		showPopUpWarning(url,"HelpWin");
	}
}

/**
 * launches the Orientation
 * @param {} 
 */
function launchOrientation() {
	var key = "Orientation";
	var url = _data[key];

	if (_orientation == null || _orientation.closed) {
		_orientation =
			ShowPopWin(url,"QuickTour",840,630,1,1,0,0,0,0,1);
	} else {
		_orientation.close();
		_orientation = ShowPopWin(url,"QuickTour",840,630,1,1,0,0,0,0,1);
	}
	try {
		if(_orientation != null) {
			_orientation.focus();
		}
	} catch (e){
		showPopUpWarning(url,"QuickTour");
	}
}

/**
 * opens a popup window
 * @param {page} url
 * @param {WindowName} window name parameter
 * @param {Width} width parameter
 * @param {Height} height parameter
 * @param {scrollbars} scrollbars parameter
 * @param {resizable} resizable parameter
 * @param {location} location parameter
 * @param {buttons} buttons parameter
 * @param {toolbar} toolbar parameter
 * @param {menubar} menubar parameter
 * @param {status} status parameter
  
 */
function ShowPopWin (page, WindowName, Width, Height, scrollbars, resizable, location, buttons, toolbar, menubar, status) {	
	//set default settings if parameters not passed
		if (location=='undefined')
			{location=1}
		if (buttons=='undefined')
			{buttons=1}
		if (toolbar=='undefined')
			{ toolbar=1}
		if (menubar=='undefined')
			{ menubar=1}
		if (status=='undefined')
			{ status=1}
		var sAttribs="location=" + location + ",buttons=" + buttons + ",toolbar=" + toolbar + ",scrollbars=" + scrollbars + ",width=" + Width + ",height=" + Height ;
		sAttribs += ",resizable=" + resizable + ",menubar=" + menubar + ",status=" + status ;
		
		var popup = window.open(page, WindowName, sAttribs);
		if(popup == null) {
			// popup was blocked
			document._ctlApplet.reportBlockedPopup(page);
		}
		return popup;
}

/**
 * ???
 * @param {width} 
 * @param {shrink} 
 * @param {anchorRight} 
 */
function resizer(width, shrink, anchorRight) {
	scroll(0,0);
	
	var resizeWidthBy = 0;
	var applet;
	if ( window.document.all )
	{
		applet = window.document.all("_ctlApplet");
	}
	else if ( window.document.getElementById )
	{
		applet = window.document.getElementById("_ctlApplet");
	}
	
	
	var currentWidth = applet.clientWidth;
	resizeWidthBy = width - currentWidth;

	parent.resizeBy(resizeWidthBy, 0);
	if(anchorRight==true) {
		parent.moveBy(-resizeWidthBy,0);
	}
	// double-call seems necessary for Safari
	if(_isSafari && !shrink) {
		parent.resizeBy(resizeWidthBy, 0);
	}

}

/**
 * ???
 */
function showLiveHelp() {
	document._ctlApplet.toggleLiveHelp();
	}

/**
 * ???
 */	
function getBounds() {
    return "" + getX() + "," + getY() + "," + getWidth() + "," + getHeight();
	}

/**
 * ???
 * @param {x} 
 * @param {y}
 * @param {width}
 * @param {height}
 */
function restoreBounds(x, y, width, height) {

    if (!_isMacMozilla) {

	parent.moveTo(x, y);
	parent.resizeTo(width, height);

    }
	    
    if (_isIE) {

        _ieXOffset = x - parent.screenLeft;
       	_ieYOffset = y - parent.screenTop;
       	_ieDecorationWidth = width - parent.document.body.clientWidth;
       	_ieDecorationHeight = height - parent.document.body.clientHeight;

    }

}


/**
 * ???
 */
function getWidth() {
    if (_isIE) {
        maybeComputeIEDecorations();
        return parent.document.body.clientWidth + _ieDecorationWidth;
    } else {
        return parent.outerWidth;
}
}


/**
 * ???
 */
function getHeight() {
    if (_isIE) {
        maybeComputeIEDecorations();
        return parent.document.body.clientHeight + _ieDecorationHeight;
    } else {
        return parent.outerWidth;
}
}

/**
 * ???
 */
function getX() {
    if (_isIE) {
        maybeComputeIEDecorations();
        return parent.screenLeft + _ieXOffset;
    } else {
        return parent.screenX;
	}
}

/**
 * ???
 */
function getY() {
    if (_isIE) {
        maybeComputeIEDecorations();
        return parent.screenTop + _ieYOffset;
    } else {
        return parent.screenY;
		}
		}
/**
 * ???
 */
function maybeComputeIEDecorations() {
    if (_isIE && _ieDecorationWidth == -1) {
        var width = parent.document.body.clientWidth;
        var height = parent.document.body.clientHeight;
        parent.resizeTo(width, height);
        _ieDecorationWidth = width - parent.document.body.clientWidth;
        _ieDecorationHeight = height - parent.document.body.clientHeight;
        parent.resizeTo(width + _ieDecorationWidth, height + _ieDecorationHeight);
        var x = parent.screenLeft;
        var y = parent.screenTop;
        parent.moveTo(x, y);
        _ieXOffset = x - parent.screenLeft;
        _ieYOffset = y - parent.screenTop;
        parent.moveTo(x + _ieXOffset, y + _ieYOffset);
	}
}

