var mainNavOn = "#ffebcc";		//menu text color on state
var mainNavOff = "#ffffff";		//menu text color off state
var subNavOn = "#F0950E"; 		//submenu bgcolor on state
var subNavOff = "#F3EBD2"; 		//submenu bgcolor off state
var fontOn = "#ffffff";
var fontOff = "#003366";

var externalLinkOn = new Image();
externalLinkOn.src="/images/nav_ext_on.gif";
var externalLinkOff = new Image();
externalLinkOff.src="/images/nav_ext_off.gif";

var submenuEnabled = false;	//boolean for whether a submenu is currently active
var myCurrentSub = null;	//var to strre currently active submenu item

var audience = getCookie("audience"); // audience cookie (set by Ameritrade)
// getCookie returns false if the cookie does not exist
if  (! audience) audience = "prospect";

var targeted = getCookie("targeted"); // targeted cookie  (set by Ameritrade)

var default_homepage = "index1.html"; // default page for non-cookied, not targeted user


var new_window_properties = "toolbar=yes, location=yes, status=yes, menubar=yes, scrollbars=yes, scrolling=yes, resizable=yes";		// properties for new browser windows

var popup_window_properties = "toolbar=no, location=no, status=no, menubar=no, resizable=no";		//properties for popup windows


if (targeted) {
	var today = new Date();
	var seven_days_from_now = new Date(today.getTime() + (7 * 24 * 60 * 60 * 1000));
	if (audience == "client") {
		setCookie("targeted", targeted, null, '.ameritrade.com', '/');
	}
	else {
		setCookie("targeted", targeted, seven_days_from_now, '.ameritrade.com', '/');
	}
}

// ------------------------------ menu functions ------------------------------

function setMainMenu (objMainDiv, blnRollActive, idSubnav, blnClickEvent) {
// function to handle all rollover and click events
// params are: the div object for the top level item, boolean active/inactive rollover state, ID of the adjacent drop-down menu, boolean to handle click events
	if (blnClickEvent) {						// handle all click events
		if (submenuEnabled) {						// if a drop-down has been enabled
			if (idSubnav = myCurrentSub) {				// if the item's drop-down is already currently active
				setSubnav(idSubnav, false);
				myCurrentSub = null;
				submenuEnabled = false;
			}
		} else {									// if no drop-down has been enabled
			setSubnav(idSubnav, true);
			myCurrentSub = idSubnav;
			submenuEnabled = true;
		}
	} else {									// handle mouseover/mouseout (non-click) events
		if (submenuEnabled) {
			if ((blnRollActive) && (idSubnav != myCurrentSub)) {	// if a top-level item is moused over, and it does not correspond to the currently active drop-down
				setSubnav(myCurrentSub, false);
				setSubnav(idSubnav, true);
				myCurrentSub = idSubnav;
			}
		}
	}

	// do mouseover/mouseout state for top level text
	objMainDiv.style.color = (blnRollActive) ? mainNavOn : mainNavOff;
	objMainDiv.className = (blnRollActive) ? "mainRollOn" : "mainOff";
}

// function to handle menu reset - called with 'onmouseout' from the nav components container div elements.
function doMenuReset(obj) {
	if (submenuEnabled) {
		setSubnav(myCurrentSub, false);
		myCurrentSub = null;
		submenuEnabled = false;
	}
}

// function that toggles submenu with 'id' on or off according to 'blnItemActive'
function setSubnav(id, blnItemActive) {
	document.getElementById(id).style.display = (blnItemActive) ? "block" : "none"
}

// function to handle mouseover/mouseout events for the submenus.
function setSubmenu(obj, active) {
	obj.style.background = (active) ? subNavOn : subNavOff;
	obj.style.color = (active) ? fontOn : fontOff;
	obj.style.borderLeft = (active) ? "5px solid "+subNavOn : "5px solid #FFD699";
	for (i=0;i<obj.childNodes.length;i++){
		if(obj.childNodes[i].src){
		obj.childNodes[i].src = (active) ? externalLinkOn.src : externalLinkOff.src;
		};
	}		
}

// ------------------------------ link functions ------------------------------

// function to open new window
function newWindow(url) {
	window.open(url, "newwin", new_window_properties);
}

// function to open popup windows
function popWindow(url, width, height) {
	window.open(url, "newwin", "scrollbars=no, scrolling=no,  width=" + width + ", height=" + height + ", " + popup_window_properties);
}

// function to open popup windows
function popWindowWithScrollBars(url, width, height) {
	window.open(url, "newwin", "scrollbars=yes, scrolling=yes, width=" + width + ", height=" + height + ", " + popup_window_properties);
}

// function to display external pages
function showDemo(code) {
	switch(code) {
		case "privacy_monitor":
			popWindowWithScrollBars("/html/privacymonitor.html", 650, 473);
			break;
			
		case "client_login":
			document.location.href = "https://wwws.ameritrade.com/apps/LogIn/";
			break;
			
		case "amerivest":
			popWindow("http://www.amerivest.com/Ameri.htm", 792, 507);
			break;
			
		case "apex":
			document.location.href = "apex.html";
			break;
		
		case "streamer":
			popWindow("/demo/streamer/481_ameritrade_streamer.htm", 905, 694);
			break;
			
		case "advanced analyzer":
			popWindow("/demo/advancedanalyzer/506_ameritrade_advanced.htm", 995, 684);
			break;
			
		case "quotescope":
			popWindow("/demo/quotescope/482_ameritrade_quotescope.htm", 792, 555);
			break;
			
		case "command center":
			popWindow("/demo/command/452_ameritrade_command.htm", 902, 667);
			break;
			
		case "snapticket":
			popWindow("/demo/snapticket/480_snap_ticket.htm", 792, 464);
			break;
			
		case "tradetrigger":
		case "trade triggers":
			popWindow("/demo/tradetriggers/505_ameritrade_tradetriggers.htm", 772, 478);
			break;
			
		//case "order tickets":
		//	newWindow("/demo/options_order/options_order.htm");
		//	break;
			
		case "best execution":
			newWindow("/o.cgi?a=jgh&p=/html/best.fhtml");
			break;
			
		case "commission_calculator":
			popWindowWithScrollBars("/calculator/CommissionCalc.cgi", 788, 547);
			break;
			
		case "online_application":
			newWindow("https://wwwna.ameritrade.com/cgi-bin/apps/AccountApServlet?segment=ameritrade");
			break;
			
		case "account_status":
			popWindowWithScrollBars("https://wwwna.ameritrade.com/cgi-bin/apps/AccountStatusServlet?segment=ameritrade", 770, 390);
			break;
			
		case "eft":
			newWindow("/o.cgi?a=mkz&p=/offer/etf.html");
			break;
		
		case "margin_disclosures":
			newWindow("/forms/ATI845.pdf");
			break;
		
		case "account_email_status":
			popWindowWithScrollBars("/csforms/Email_Account_Status.html", 670, 510);
			break;	
			
		case "deposit_status":
			popWindowWithScrollBars("/csforms/Email_Deposit_Status.html", 670, 510);
			break;

		case "forms_email_request":
			popWindowWithScrollBars("/csforms/Email_Forms_Request.html", 670, 510);
			break;			

		case "new_accounts_email":
			popWindowWithScrollBars("/csforms/Email_New_Accounts.html", 670, 510);
			break;
			
		case "promotions":
			popWindowWithScrollBars("/csforms/Email_Promotions.html", 670, 510);
			break;
			
		case "transfer_status":
			popWindowWithScrollBars("/csforms/Email_Transfer_Status.html", 670, 510);
			break;
			
		case "education_center":
			popWindow("/educationv2/ameritrade_framed.html", 680, 500);
			break;

		case "education_center_stockfundoption":
			popWindow("/educationv2/stocksfunds_framed.html", 680, 500);
			break;
	}
}



// function for related links 
function setLinks() {
	var id;
	var argv = setLinks.arguments;
  	var argc = argv.length;
  	for (var i = 0; i < argc; i++) {
   		id = argv[i];
		
		switch(id.toLowerCase()) {
			// eft site
			case "eft_site":
				document.write('<a href="/o.cgi?a=mkz&p=/offer/etf.html" target="_blank" class="module-text">ETF Center</a>.  Learn about Exchange Traded Funds.<br /><br />');
				break;
				
			// Commissions
			case "commissions":
				document.write('<a href="details.html" class="module-text">Commissions</a>.  Flat rate, $10.99 Internet equity trades. <br /><br />');
				break;
			
			// Ways to trade
			case "waystotrade":
			case "ways_to_trade":
				document.write('<a href="waystotrade.html" class="module-text">Ways to Trade</a>.  Manage your investments on your terms.<br /><br />');
				break;
			
			// How to Fund
			case "how_to_fund":
			case "how to fund":
				document.write('<a href="howtofund.html" class="module-text">How To Fund</a>.  Convenient account funding methods. <br /><br />');
				break;
				
			// Opening a New Account FAQ
			case "newaccnt_faq":
				document.write('<a href="faq.html" class="module-text">Opening a New Account FAQs</a>. Your questions answered. <br /><br />');
				break;
				
			// IRA FAQ
			case "ira_faq":
				document.write('<a href="faqira.html" class="module-text">IRA FAQs</a>.  Individual Retirement Account questions and answers<br /><br />');
				break;
				
			// Rates & Fees
			case "rates_fees":
				document.write('<a href="ratesfees.html" class="module-text">Rates &amp; Fees</a>.  Find out more. <br /><br />');
				break;
				
			// Account T's and C's
			case "account_tc":
				document.write('<a href="termsconditions.html" class="module-text">Account Terms &amp; Conditions</a>. Read these important terms and conditions.<br /><br />');
				break;
			
			// Best Execution
			case "best_execution":
				document.write('<a href="/o.cgi?a=jgh&p=/html/best.fhtml" target="_blank" class="module-text">Best Execution</a>.  Learn more. <br /><br />');
				break;
				
			// Compare Commissions
			case "compare_commissions":
				document.write('<a href="compare.html" class="module-text">Compare Commissions</a> vs. other brokerages.<br /><br />');
				break;
				
			// Quotescope
			case "quotescope":
				document.write('<a href="quotescope.html" class="module-text">QuoteScope</a>&trade; helps you visualize market trends.<br /><br />');
				break;
				
			// Streamer
			case "streamer":
				document.write('<a href="streamer.html" class="module-text">Ameritrade Streamer</a>&trade; provides up-to-the-second market information.<br /><br />');
				break;
				
			// Advanced Analyzer
			case "advanced_analyzer":
				document.write('<a href="advancedanalyzer.html" class="module-text">Advanced Analyzer</a>&trade;, charting, screening, and more. <br /><br />');
				break;
			
			// Command Center
			case "command_center":
			case "command center":
				document.write('<a href="commandcenter.html" class="module-text">Command Center Screen</a> puts your data and trading tools on one screen.<br /><br />');
				break;
				
			//SnapTicket
			case "snapticket":
				document.write('<a href="snapticket.html" class="module-text">SnapTicket</a>&trade; lets you trade from anywhere online. <br /><br />');
				break;
				
			//SnapTicket
			//case "order_tickets":
			//	document.write('<a href="ordertickets.html" class="module-text">Order Tickets</a> FPO Copy. <br /><br />');
			//	break;
			
			//Trade Triggers
			case "trade_triggers":
				document.write('<a href="tradetriggers.html" class="module-text">Trade Triggers</a>&trade; set orders to be sent automatically. <br /><br />');
				break;
				
			// Trading Solutions
			case "trading_solutions":
			case "trading solutions":
				document.write('<a href="tradingstrategy.html" class="module-text">Trading Solutions</a> help you trade efficiently. <br /><br />');
				break;
				
			// Information Solutions
			case "information_solutions":
				document.write('<a href="information.html" class="module-text">Information Solutions</a> put data at your fingertips. <br /><br />');
				break;
				
			// Order Execution
			case "order_execution":
				document.write('<a href="execution.html" class="module-text">Order Execution</a> lets you trade quickly and confidently. <br /><br />');
				break;
				
			// Accoloades
			case "accolades":
				document.write('<a href="accolades.html" class="module-text">Accolades</a>, awards, and honors. <br /><br />');
				break;
				
			//Our History
			case "our_history":
				document.write('<a href="history.html" class="module-text">Our History</a>.  Find out more.<br /><br />');
				break;
				
			// Account Types
			case "account_types":
				document.write('<a href="types.html" class="module-text">Account Types</a> for a variety of investment needs.<br /><br />');
				break;
				
			// Investment Options
			case "investment_options":
			case "investment_choices":
				document.write('<a href="choices.html" class="module-text">Investment Choices</a> to suit your investment style.<br /><br />');
				break;
		}
	}
}


// ------------------------------ cookie functions ------------------------------

// function to get a cookie value
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

// function to set a cookie
function setCookie(name, value, expires, domain, path){
	var theCookie = name + "=" + escape(value) + "; ";
	if(expires) {
	  theCookie += "expires=" + expires.toGMTString() + "; ";
        }
	if(domain){
		theCookie += "domain=" + domain + "; ";
	}
	if(path){
		theCookie +="path=" + path + "; ";
	}
	document.cookie = theCookie;
}

function deleteCookie(name){
	var now = new Date();
	document.cookie = name + "=0; expires=" + now.toGMTString();
}

// function to redirect to appropriate homepage based on cookies
function goHome() {	
	switch(targeted) {
		case "apex":
			default_homepage = "index_apex.html";
			break;
				
		case "tools":
			default_homepage = "index_prod.html";
			break;
				
		case "21st":
			default_homepage = "index_lterm.html";
			break;
			
		default:
			if (audience == "client") {
				default_homepage = "index2.html";
			} 
			break;
	}
		
	document.location.href = default_homepage;
}

