var windowLocation = true;
if (self != top){windowLocation = false;}
else {windowLocation = true;}
var activeId = "";
var activeIndicatorId = "";
function subMenu(id){
	var subMenuId=id+"Menu";
	if (activeId != ""){
		var prevMenuId = activeId+"Menu";
		var prevMenuObj = document.getElementById(prevMenuId);
		prevMenuObj.style.display="none";
	}
	else{
		var prevMenuId = subMenuId;
		var prevMenuObj = document.getElementById(prevMenuId);
		prevMenuObj.style.display="none";
	}
	var indicatorId = id+"Indicator";
	
	var activeIdObj = document.getElementById(activeId);
	var activeIndicatorIdObj = document.getElementById(activeIndicatorId);
	
	var topCurveUpper = document.getElementById("topCurveUpper");
	var topCurveLower = document.getElementById("topCurveLower");
	
	if (activeIndicatorId != ""){
		activeIdObj.className="menuOff";
	}
	if (activeIndicatorId != ""){
		activeIndicatorIdObj.style.visibility="hidden";
	}
	if (id == "home"){
		topCurveUpper.style.marginLeft = "0px";
		topCurveUpper.style.marginRight = "2px";
		topCurveLower.style.marginLeft = "0px";
		topCurveLower.style.marginRight = "1px";
	}
	else if (id == "client"){
		topCurveUpper.style.marginLeft = "2px";
		topCurveUpper.style.marginRight = "0px";
		topCurveLower.style.marginLeft = "1px";
		topCurveLower.style.marginRight = "0px";
	}
	else{
		topCurveUpper.style.marginLeft = "2px";
		topCurveUpper.style.marginRight = "2px";
		topCurveLower.style.marginLeft = "1px";
		topCurveLower.style.marginRight = "1px";
	}
	document.getElementById(id).className="menuOn";
	document.getElementById(subMenuId).style.display="inline";
	document.getElementById(indicatorId).style.visibility="visible";
	activeId=id;
	activeIndicatorId=indicatorId;
};

function changeBgColor(element){
	if (element.className != "menuOver" && element.className != "menuOn"){
		element.className = "menuOver";
		element = null;
	}
	else{
		if (element.className == "menuOver"){
			element.className = "menuOff";
			element = null;
		}
	}
};

function changeSubMenuBgColor(element){
	if (element.className != "subMenuOver" && element.className != "subMenuOn"){element.className = "subMenuOver";}
	else{ if (element.className == "subMenuOver"){element.className = "subMenuOff";} }
};

function displayNav(mainSec, subSec){
	var subSectionId = mainSec + subSec;
	var subSection = document.getElementById(subSectionId);
	subMenu(mainSec);
	subSection.className = "subMenuOn";
};

var topNavSectionArray = ['home','portfolio','trade','research','tools','goals','client'];

function topNavIndicatorClass(tableID, sectionArray){
	this.id = tableID;
	this.tableContainer = document.getElementById(this.id);
	this.sectionArray = sectionArray;
	var j = this.sectionArray.length;
	for (var i = 0; i < j; i++){
		var oIndicatorCell = document.createElement("td");
		oIndicatorCell.id = this.sectionArray[i] + "Indicator";
		if (i == 0 || i == (j-1)){oIndicatorCell.colSpan = 2;}
		var oIndicatorDiv1 = document.createElement("div");
		oIndicatorDiv1.className = "indicator1";
		var oIndicatorDiv2 = document.createElement("div");
		oIndicatorDiv2.className = "indicator2";
		oIndicatorCell.appendChild(oIndicatorDiv1);
		oIndicatorCell.appendChild(oIndicatorDiv2);
		this.tableContainer.appendChild(oIndicatorCell);
	}
};

function topNavMainTabClass(tableID){
	this.id = tableID;
	this.tableContainer = document.getElementById(this.id);
};

topNavMainTabClass.prototype.addItem = function(id, text, url){
	var oMainTabCell = document.createElement("td");
	oMainTabCell.id = id;
	oMainTabCell.className = "menuOff";
	oMainTabCell.onmouseover = function(){changeBgColor(this)};
	oMainTabCell.onmouseout = function(){changeBgColor(this)};
	if (windowLocation){
		oMainTabCell.onclick = function(){document.location.href = url;};
	}
	else{
		oMainTabCell.onclick = function(){parent.location.href = url;};
	}
	var oMainTabDiv = document.createElement("div");
	var oMainTabAnchor = document.createElement("a");
	oMainTabAnchor.href = url;
	oMainTabAnchor.appendChild(document.createTextNode(text));
	oMainTabDiv.appendChild(oMainTabAnchor);
	oMainTabCell.appendChild(oMainTabDiv);
	this.tableContainer.appendChild(oMainTabCell);
};

function topNavSubNavClass(tableID){
	this.id = tableID;
	this.tableContainer = document.getElementById(this.id);
	this.initSubnav = true;
};

topNavSubNavClass.prototype.addItem = function(mainSecId, padOverride, pageId, url, text, parentId, last){
	if (parentId){
		var parentRow = document.getElementById(parentId + "MenuRow");
		var oSubNavCell = document.createElement("td");
		oSubNavCell.id = mainSecId + pageId;
		oSubNavCell.className = "subMenuOff";
		oSubNavCell.onmouseover = function(){changeSubMenuBgColor(this);};
		oSubNavCell.onmouseout = function(){changeSubMenuBgColor(this);};
		if (windowLocation){
			oSubNavCell.onclick = function(){document.location.href = url;};
		}
		else{
			oSubNavCell.onclick = function(){parent.location.href = url;};
		}
		if (parentRow.className && parseInt(parentRow.className) > 0){
			oSubNavCell.style.paddingLeft = parseInt(parentRow.className) + "px";
			oSubNavCell.style.paddingRight = parseInt(parentRow.className) + "px";
		}
		var oSubNavCellAnchor = document.createElement("a");
		oSubNavCellAnchor.href = url;
		oSubNavCellAnchor.appendChild(document.createTextNode(text));
		oSubNavCell.appendChild(oSubNavCellAnchor);
		parentRow.appendChild(oSubNavCell);
		if (!last){this.createDivider(parentRow);}
	}
	else{
		if (this.initSubnav){
			this.tableContainer.innerHTML = "";
			this.initSubnav = false;
		}
		var oSubNavDiv = document.createElement("div");
		oSubNavDiv.id = mainSecId + "Menu";
		oSubNavDiv.className = "subMenuItem";
		oSubNavDiv.onclick = function(){this.blur();};
		var oSubNavTable = document.createElement("table");
		oSubNavTable.align = "center";
		oSubNavTable.cellPadding = 0;
		oSubNavTable.cellSpacing = 0;
		var oSubNavTBody = document.createElement("tbody");
		var oSubNavRow = document.createElement("tr");
		oSubNavRow.id = mainSecId + "MenuRow";
		if (padOverride){oSubNavRow.className = padOverride.toString();}
		oSubNavTBody.appendChild(oSubNavRow);
		oSubNavTable.appendChild(oSubNavTBody);
		oSubNavDiv.appendChild(oSubNavTable);
		this.tableContainer.appendChild(oSubNavDiv);
	}
};

topNavSubNavClass.prototype.createDivider = function(parent){
	var oSubNavDividerCell = document.createElement("td");
	oSubNavDividerCell.className = "subdivider";
	oSubNavDividerCell.appendChild(document.createTextNode("|"));
	oSubNavDividerCell.style.paddingLeft = 0;
	oSubNavDividerCell.style.paddingRight = 0;
	oSubNavDividerCell.style.width = "1px";
	parent.appendChild(oSubNavDividerCell);
};

//subnav items always have padOverride set to 0, mainSection items can set a custom value if need be
function initNav(){
	var topNavIndicator = new topNavIndicatorClass("navIndicator", topNavSectionArray);
	var topNavMainNav = new topNavMainTabClass("navBg");
	topNavMainNav.addItem("home","Home","/welcome1.html");
	topNavMainNav.addItem("portfolio","Portfolio & Accounts","/portfolioaccounts/overview.html");
	topNavMainNav.addItem("trade","Trade","/trade/overview.html");
	topNavMainNav.addItem("research","Research & Ideas","http://research.tdameritrade.com/public/markets/overview/overview.asp");
	topNavMainNav.addItem("tools","Trading Tools","/tradingtools/overview.html");
	topNavMainNav.addItem("goals","Planning & Retirement","/planningretirement/overview.html");
	topNavMainNav.addItem("client","Client Services","/contact.html");
	var topNavSubNav = new topNavSubNavClass("subMenu");
	topNavSubNav.addItem("home");
		topNavSubNav.addItem("home", 0, "Home", "/welcome1.html", "Home Page", "home");
		topNavSubNav.addItem("home", 0, "WhyChoose", "/whychoose.html", "Why Choose TD AMERITRADE", "home");
		topNavSubNav.addItem("home", 0, "OpenAccount", "/openaccount.html", "Open an Account", "home");
		topNavSubNav.addItem("home", 0, "Pricing", "/pricing.html", "Pricing", "home");
		topNavSubNav.addItem("home", 0, "AccountTypes", "/standardaccounts.html", "Account Types", "home");
		topNavSubNav.addItem("home", 0, "WaysToFund", "/waystofund.html", "Ways To Fund", "home");
		topNavSubNav.addItem("home", 0, "FAQ", "/faq/newaccount.html", "FAQs", "home");
		topNavSubNav.addItem("home", 0, "Apex", "/apex.html", "Apex", "home", true);
	topNavSubNav.addItem("portfolio");
		topNavSubNav.addItem("portfolio", 0, "Overview", "/portfolioaccounts/overview.html", "Overview", "portfolio");
		topNavSubNav.addItem("portfolio", 0, "Balances", "/portfolioaccounts/balancespositions.html", "Balances & Positions", "portfolio");
		topNavSubNav.addItem("portfolio", 0, "Portfolio", "/portfolioaccounts/portfolio.html", "Portfolio", "portfolio");
		topNavSubNav.addItem("portfolio", 0, "Gain", "/portfolioaccounts/gainloss.html", "Gain/Loss", "portfolio");
		topNavSubNav.addItem("portfolio", 0, "WatchLists", "/portfolioaccounts/watchlists.html", "Watch Lists", "portfolio");
		topNavSubNav.addItem("portfolio", 0, "History", "/portfolioaccounts/history.html", "History & Statements", "portfolio");
		topNavSubNav.addItem("portfolio", 0, "Cash", "/portfolioaccounts/cashmanagement.html", "Cash Mgmt", "portfolio");
		topNavSubNav.addItem("portfolio", 0, "Profile", "/portfolioaccounts/preferences.html", "Profile", "portfolio", true);
	topNavSubNav.addItem("trade");
		topNavSubNav.addItem("trade", 0, "Overview", "/trade/overview.html", "Overview", "trade");
		topNavSubNav.addItem("trade", 0, "SnapTicket", "/trade/snapticket.html", "SnapTicket", "trade");
		topNavSubNav.addItem("trade", 0, "Order", "/trade/orderstatus.html", "Order Status", "trade");
		topNavSubNav.addItem("trade", 0, "Stocks", "/trade/stocks.html", "Stocks", "trade");
		topNavSubNav.addItem("trade", 0, "Options", "/trade/options.html", "Options", "trade");
		topNavSubNav.addItem("trade", 0, "MutualFunds", "/trade/mutualfunds.html", "Mutual Funds", "trade");
		topNavSubNav.addItem("trade", 0, "BondsCDs", "/trade/bonds&cds.html", "Bonds & CDs", "trade");
		topNavSubNav.addItem("trade", 0, "QualityEx", "/trade/orderexecution/index.html", "Order Execution", "trade");
		topNavSubNav.addItem("trade", 0, "Triggers", "/trade/tradetriggers.html", "Trade Triggers", "trade", true);
	topNavSubNav.addItem("research", 20);
		topNavSubNav.addItem("research", 0, "Markets", "http://research.tdameritrade.com/public/markets/overview/overview.asp", "Markets", "research");
		topNavSubNav.addItem("research", 0, "Stocks", "http://research.tdameritrade.com/public/stocks/overview/overview.asp", "Stocks", "research");
		topNavSubNav.addItem("research", 0, "Options", "/researchideas/options.html", "Options", "research");
		topNavSubNav.addItem("research", 0, "MutualFunds", "http://research.tdameritrade.com/public/mutualfunds/overview/overview.asp", "Mutual Funds", "research");
		topNavSubNav.addItem("research", 0, "ETFs", "http://research.tdameritrade.com/public/etfs/overview/overview.asp", "ETFs", "research");
		topNavSubNav.addItem("research", 0, "BondsCDs", "http://valubond.tdameritrade.com/wsi/data/company/tdat/PublicOverview.asp", "Bonds & CDs", "research", true);
		
		//http://fixedincome.tdameritrade.com/wsi/data/company/tdat/PublicOverview.asp
		
		//topNavSubNav.addItem("research", 0, "Alerts", "http://research.tdameritrade.com/public/alerts/overview/overview.asp","Alerts", "research");
		//topNavSubNav.addItem("research", 0, "Charts", "/researchideas/charts.html", "Charts", "research", true);
	topNavSubNav.addItem("tools");
		topNavSubNav.addItem("tools", 0, "Overview", "/tradingtools/overview.html", "Overview", "tools");
		topNavSubNav.addItem("tools", 0, "Streamer", "/tradingtools/streamersuite.html", "Streamer Suite", "tools");
		topNavSubNav.addItem("tools", 0, "Command", "/tradingtools/commandcenter.html", "Command Center", "tools");
		//topNavSubNav.addItem("tools", 0, "Console", "#", "Traders Console", "tools");
		topNavSubNav.addItem("tools", 0, "QuoteScope", "/tradingtools/quotescope.html", "QuoteScope", "tools");
		topNavSubNav.addItem("tools", 0, "Analyzer", "/tradingtools/advancedanalyzer.html", "Advanced Analyzer", "tools");
		topNavSubNav.addItem("tools", 0, "MarketMotion", "/tradingtools/marketmotiondetector.html", "Market Motion Detector", "tools", true);
	topNavSubNav.addItem("goals");
		topNavSubNav.addItem("goals", 0, "Overview", "/planningretirement/overview.html", "Overview", "goals");
		topNavSubNav.addItem("goals", 0, "Portfolio", "/planningretirement/portfolioguidance/overview.html", "Portfolio Guidance", "goals");
		topNavSubNav.addItem("goals", 0, "IRA", "/planningretirement/rollover/overview.html", "IRA Rollover", "goals");
		topNavSubNav.addItem("goals", 0, "Retirement", "/planningretirement/retirement/overview.html", "Retirement", "goals");
		topNavSubNav.addItem("goals", 0, "College", "/planningretirement/college/overview.html", "College", "goals");
		topNavSubNav.addItem("goals", 0, "Taxes", "/planningretirement/taxes/overview.html", "Taxes", "goals");
		topNavSubNav.addItem("goals", 0, "SmallBusiness", "/planningretirement/smallbusiness/overview.html", "Small Business", "goals");
		topNavSubNav.addItem("goals", 0, "Advisor", "/planningretirement/advisor.html", "Advisor Referral", "goals", true);
	topNavSubNav.addItem("client", 30);
		topNavSubNav.addItem("client", 0, "Contact", "/contact.html", "Contact Us", "client");
		topNavSubNav.addItem("client", 0, "Forms", "/forms.html", "Find a Form", "client");
		topNavSubNav.addItem("client", 0, "Security", "/security/index.html", "Security Center", "client", true);
};

//WINDOW RESIZE
function onWindowResize(){
	if(document.getElementById('frame') == null){
		return;
	}
	var container = document.getElementById('frame');
	if (document.body.clientWidth < 800){
		container.style.width = "760px";
	}
	else if (document.body.clientWidth >= 1024){
		container.style.width = "984px";
	}
	else{
		container.style.width = "auto";
	}
};

function inputFillCheck (item, initValue) {
	if (item.value==""){
		item.className="inputFillOff";
		item.value=initValue;
	}		
	else if (item.value==initValue){
		item.value="";
		item.className="inputFillOn";
	}	
};

function loadIframeNav(){
	var pageURL = document.location.href;
	var reNav = /nav1=(\w+)&nav2=(\w+)/;
	reNav.test(pageURL);
	var mainSec = RegExp.$1;
	var subSec = RegExp.$2;
	if (mainSec != "" && subSec != ""){
		displayNav(mainSec,subSec);
	}
};

var agt=navigator.userAgent.toLowerCase();
var is_ie=(agt.indexOf("msie")!=-1);
/**********
 * Changes the css style and events for buttons
 **************************/
//button class name constants
var OUTER = "Outer";
var MIDDLE = "Middle";
var INNER = "Inner";
var PRIORITY = "Priority";
function btnOn(id){
	var linkRef = document.getElementById(id);
	if (linkRef){
		var pClass = (linkRef.getAttribute("priority")=="true") ? PRIORITY : ""; //if priority, change class
		changeBtnClass(id,pClass+"On");
		linkRef.onfocus = function(){ changeBtnClass(id,pClass+"Over");} ;
		linkRef.onblur = function(){ changeBtnClass(id,pClass+"On");} ;
		linkRef.onmouseover = function(){ changeBtnClass(id,pClass+"Over");};
		linkRef.onmousedown = function(){//need to also clear onfocus and blur on mousedown for mozilla/firefox conflicts
			if (!is_ie){
				document.getElementById(id).onfocus=null;
				document.getElementById(id).onblur=null;
			}
			changeBtnClass(id,pClass+"Down");
		};
		linkRef.onmouseup = function(){ 
			changeBtnClass(id,pClass+"On") 
		};
		linkRef.onmouseout = function(){ //need to also reset onfocus and blur on mousedown for mozilla/firefox conflicts
			changeBtnClass(id,pClass+"On");
			if (!is_ie){
				document.getElementById(id).onfocus = function(){changeBtnClass(id,pClass+"Over");};
				document.getElementById(id).onblur= function(){changeBtnClass(id,pClass+"On");};	
			}
		};
		linkRef.onclick = linkRef.inlineonclick; 
	}
 };
 
 function btnOff(id) {
	var linkRef = document.getElementById(id);
	if (linkRef){
		var pClass = (linkRef.getAttribute("priority")=="true") ? PRIORITY : ""; //if priority, change class
		changeBtnClass(id,pClass+"Off");
		linkRef.onfocus = null;
		linkRef.onblur = null;
		linkRef.onclick = function(){return false;};
		linkRef.onmouseover = null;
		linkRef.onmousedown = null ;
		linkRef.onmouseup = null;
		linkRef.onmouseout = null;	
	}
 };
 
function changeBtnClass(id,state){
	var btn = document.getElementById(id + OUTER);
	if (btn){
		 btn.className = "btn" + state + OUTER;
		 var middleBtn = document.getElementById(id+MIDDLE);
		 if (middleBtn) middleBtn.className = "btn" + state + MIDDLE;
		 var innerBtn = document.getElementById(id+INNER);
		 if (innerBtn) innerBtn.className = "btn" + state + INNER;
		 innerBtn.style.padding="0";
	}
};

function btnInit(id,priority,on,setwidth,setheight,iconPre,iconPost,smallsize,mini){
	var linkRef = document.getElementById(id); //get a tag link
	var iconImg = "";//for icons
	if (iconPre || iconPost) {
		iconImg = document.createElement("img");
		iconImg.src = (iconPre) ? iconPre: iconPost;
		iconImg.border = "0";
		iconImg.hspace = "4";
		iconImg.style.paddingTop = "2px"
		//iconImg.width = 10;
		//iconImg.height = 10;
	}
	
	if (linkRef){
		var txt = linkRef.firstChild.nodeValue; //get text in link
		var oswidth = linkRef.offsetWidth; //buttons may need to be same width for design, if so, pass width size
		var padding = (smallsize) ?  16 : 21; //add extra width padding
		padding = (mini) ? 0 : padding;
		var width = (setwidth) ? setwidth : oswidth + padding; //get width of link, and add padding
		var height = (setheight) ? setheight : false; //get width of link, and add padding
		linkRef.firstChild.nodeValue = " "; //erase text link
		linkRef.className = "btnLink"; 
		linkRef.style.width = width; //reset link width
		if(height) linkRef.style.height = height;

		if (iconPre || iconPost) {
			width += 11; //size of icon image + buffer
		}
		
		//writing out this structure <a href="#"><div><div></div><div></div></div></a>
		//triple divs for additional borders
		var outerDiv = document.createElement("div"); 
		outerDiv.id = id + OUTER;
		outerDiv.style.width = width + "px";
		if(height) outerDiv.style.height = height + "px";
		
		var middleDiv = document.createElement("div"); 
		middleDiv.id = id + MIDDLE;
		middleDiv.style.width = (width - 2) + "px";
		if(height) middleDiv.style.height = (width - 2) + "px";
		
		var innerDiv = document.createElement("div");
		innerDiv.id = id + INNER;
		innerDiv.style.width = (width - 4) + "px";
		if(height) innerDiv.style.height = (width - 4) + "px";
		if (smallsize) {innerDiv.style.fontSize = "11px";innerDiv.style.lineHeight = "13px";}
		if(mini){innerDiv.style.fontSize = "9px"; innerDiv.style.lineHeight = "9px";}
		
		outerDiv.appendChild(middleDiv);
		middleDiv.appendChild(innerDiv);
		if (iconPre) {
			innerDiv.appendChild(iconImg);
		}
		innerDiv.appendChild(document.createTextNode(txt));
		if (iconPost) {
			innerDiv.appendChild(iconImg);
		}		
		linkRef.appendChild(outerDiv);
						
		if (priority) { //priority setting
		linkRef.setAttribute("priority","true");
		}
		
		//hold on to inline onclick on the original link
		if (linkRef.inlineset!=true) {
			linkRef.inlineonclick = linkRef.onclick; 
			linkRef.inlineset = true;
		}
		on ? btnOn(id) : btnOff(id); //set button states
	}
	return linkRef;
};

function vendorLink(url){
	top.location.href = url;
};

function submitSymbolQuote(form){
	var initQuote = "ex. ";
	var formValue = form.symbol.value;
	if (formValue.indexOf(initQuote) > -1){
		 var newFormValue = formValue.replace(initQuote, "");
		form.symbol.value = newFormValue;
	}
	form.submit();
};
