//some common utility functions and variables used by various pages

var dartPtileRan = parseInt(Math.random() * 1000000000);

var is_searchType = 'nasdaq';
var searchOptionsDisplayed = false;
var currSearchSel = "nasdaqImg";

if(typeof myPage != 'undefined')
{
	document.domain = 'nasdaq.com';
}

function openWin(winName,winURL,winWidth,winHeight)
{
		 newWindow = window.open(winURL,winName,'toolbar=no,menubar=no,width='+winWidth+',height='+winHeight+',scrollbars=no,resizable=no');
}

//search functions

function is_checkSearchType(searchType) 
{

	if (searchType == 0)
	{
		is_searchType = 'nasdaq';
	} 
	else if(searchType == 1)
	{
		is_searchType = 'web';
	}
	else if(searchType == 2)
	{
		is_searchType = 'local';
	}

	return is_searchType;
}

function Cis_clearTB() 
{
	document.getElementById("input-search-text").value ='';
}

function Cis_submitSearch() 
{
	searchTerm = document.getElementById("input-search-text").value;
	if(searchTerm!="" && searchTerm!= "Site Search") 
	{
		if (is_searchType == 'nasdaq' && searchTerm!="Site Search") 
		{
			top.document.cookie='search=1';
			document.location.href = "http://search.nasdaq.com/search?q=\""+searchTerm+"\"&site=dotcom_collection&client=default_frontend&proxystylesheet=default_frontend&output=xml_no_dtd&proxyreload=1";
		} 
		else if(is_searchType == 'web') 
		{
			document.location.href = "http://nasdaq.infospace.com/nasdaq/ws/results/Web/"+searchTerm+"/1/0/0/Relevance/iq=true/zoom=off/_iceUrlFlag=7?_IceUrl=true";
		}
		else
			document.location.href = "http://nasdaq.yellowbook.com/yellow-pages/?what="+searchTerm;
		return false;
	}
	return true;
}

	
function handleSearchDropClick()
{
	if(!searchOptionsDisplayed)
	{
		document.getElementById("dropdown-look").style.display = "block";
		searchOptionsDisplayed = true;
	}
	else
	{
		document.getElementById("dropdown-look").style.display = "none";
		searchOptionsDisplayed = false;
	}
}

function handleSearchKeyPress(event)
{
	if(event.keyCode == 13)
	{
		Cis_submitSearch();
		return false;
	}
	return true;
}

function selectSearchType(type, imgID)
{
	is_checkSearchType(type);
	if(imgID!=currSearchSel)
	{
		document.getElementById(currSearchSel).src = "http://www.nasdaq.com/images/blank-box.jpg";
		document.getElementById(imgID).src = "http://www.nasdaq.com/images/check-box.jpg";
		currSearchSel = imgID;
	}
		
	Cis_submitSearch();
}

function setST(selEle, unSelEle1, unSelEle2)
{
	document.getElementById(selEle).className = "see-highlight";
	document.getElementById(unSelEle1).className = "see-option";
	document.getElementById(unSelEle2).className = "see-option";
}

function handleFooterSearchKeyPress(event)
{
	if(event.keyCode == 13)
	{
		footersubmitSearch();
		return false;
	}
	return true;
}

function footersubmitSearch() 
{
	searchTerm = document.getElementById("footer_search_keywords").value;
	if(searchTerm!="" && searchTerm!="enter keywords") 
	{
		is_searchType = document.getElementById("nasdaq_footer_search").options[document.getElementById("nasdaq_footer_search").selectedIndex].value;
		if (is_searchType == 'nasdaq') 
		{
			top.document.cookie='search=1';
			document.location.href = "http://search.nasdaq.com/search?q=\""+searchTerm+"\"&site=dotcom_collection&client=default_frontend&proxystylesheet=default_frontend&output=xml_no_dtd&proxyreload=1";
		} 
		else if(is_searchType == 'web') 
		{
			document.location.href = "http://nasdaq.infospace.com/nasdaq/ws/results/Web/"+searchTerm+"/1/0/0/Relevance/iq=true/zoom=off/_iceUrlFlag=7?_IceUrl=true";
		}
		else
		{
			document.location.href = "http://nasdaq.yellowbook.com/yellow-pages/?what="+searchTerm;
		}
		return false;
	}
	return true;
}


//dynamic market status quote links

function setQuickFindQuoteLink()
{
	//n_market_status could be either P O A C

	if(typeof n_market_status != 'undefined')
	{
		switch(n_market_status)
		{
			case "P":   //only show premarket link
						document.write("<style type='text/css'>#qfRTlink{display:none;}</style>");
						document.write("<style type='text/css'>#qfAHlink{display:none;}</style>");
						break;

			case "O":   //only show real-time link
						document.write("<style type='text/css'>#qfPMlink{display:none;}</style>");
						document.write("<style type='text/css'>#qfAHlink{display:none;}</style>");
						break;

			case "A":   //only show after hours link
						document.write("<style type='text/css'>#qfPMlink{display:none;}</style>");
						document.write("<style type='text/css'>#qfRTlink{display:none;}</style>");
						break;

			case "C":   //only show after hours link
						document.write("<style type='text/css'>#qfPMlink{display:none;}</style>");
						document.write("<style type='text/css'>#qfRTlink{display:none;}</style>");
						break;
		}		

	}
	else
	{   //only show real-time link
		document.write("<style type='text/css'>#qfPMlink{display:none;}</style>");
		document.write("<style type='text/css'>#qfAHlink{display:none;}</style>");
	}						
		
}


function setMenuQuoteLink()
{
	//n_market_status could be either P O A C

	if(typeof n_market_status != 'undefined')
	{
		switch(n_market_status)
		{
			case "P":   //only show premarket link
						document.write("<style type='text/css'>#mnRTlink{display:none;}</style>");
						document.write("<style type='text/css'>#mnAHlink{display:none;}</style>");
						break;

			case "O":   //only show real-time link
						document.write("<style type='text/css'>#mnPMlink{display:none;}</style>");
						document.write("<style type='text/css'>#mnAHlink{display:none;}</style>");
						break;

			case "A":   //only show after hours link
						document.write("<style type='text/css'>#mnPMlink{display:none;}</style>");
						document.write("<style type='text/css'>#mnRTlink{display:none;}</style>");
						break;

			case "C":   //only show after hours link
						document.write("<style type='text/css'>#mnPMlink{display:none;}</style>");
						document.write("<style type='text/css'>#mnRTlink{display:none;}</style>");
						break;
		}		

	}
	else
	{   //only show real-time link
		document.write("<style type='text/css'>#mnPMlink{display:none;}</style>");
		document.write("<style type='text/css'>#mnAHlink{display:none;}</style>");
	}


}



//user community profile functions

function showLoginStatus()
{
	var passurl = document.location.href;
	urlFilt = /[;)(]|%27|%22|%3C|%3E|%uFF1C|%uFF1E|%u003C|%u003E|%u0028|%u0029|%u0027|%u0022|%u003B/g;
	if(!urlFilt.test(passurl))
	{
		var ltsp = new Date();
		document.write("<script src='http://www.nasdaq.com/aspx/communitylogin.ashx?turl=" + encodeURIComponent(passurl) + "&" + ltsp.getTime() + "'><\/script>");
  	}
}

function displayLoginStatus()
{
	if(typeof displayingCommUserName != "undefined")
	{
		if(displayingCommUserName)
		{
			document.write("<style type='text/css'>#communityNotLoggedIn{display:none;}</style>");
			document.write("<style type='text/css'>#communityLoggedIn{display:block;}</style>");
		}
		else
		{
			document.write("<style type='text/css'>#communityLoggedIn{display:none;}</style>");
			document.write("<style type='text/css'>#communityNotLoggedIn{display:block;}</style>");
		}
	}
	else
	{
		document.write("<style type='text/css'>#communityLoggedIn{display:none;}</style>");
		document.write("<style type='text/css'>#communityNotLoggedIn{display:block;}</style>");
	}

}

function displayName()
{
	if(typeof commHandle != "undefined")
	{
		document.write(commHandle);
	}
}

function displayAvatar()
{
	if(typeof avatar != "undefined")
	{
		document.write('<img src="'+avatar+'"/>');
	}

}

//auxilary functions

function get_querystring_value(parameter)
{
    string = unescape(String(top.location.search));//grab parameter string

	if(string.indexOf("#") != -1)
	{
		string = string.substr(0,string.indexOf("#"));//remove the anchor
	}
    if(string!="")
    {
        parameter = parameter.toLowerCase();
        string=string.substring(1).split("&"); //separate out into a list
        
        for(var i=0; i<string.length; i++)
        {
            params = string[i].split("=");
            
            if(params[0].toLowerCase() == parameter)
            {   
                
                if(params[1] != "")
                    return params[1]; //parameter found -- return value
                else
                    return false;  //parameter found but no value set
            }
        }
        return false; //querystring parameter not found
         
    }
    else
        return false;//no querystring parameters
 }

function get_cookie_value(parameter)
{
	if(cookies_enabled() && parameter!="")
	{
		index = document.cookie.indexOf(parameter+"=");
		if(index!=-1)
		{
			//i = document.cookie.indexOf("flash=")+6;
			index = index + parameter.length + 1;
			last_index = document.cookie.indexOf(";",index);
			if(last_index==-1)  //symbol list string is at the end of the cookie for some reason
				last_index = document.cookie.length;
			var value = unescape(document.cookie.substring(index,last_index));
			return value;

		}
		else
			return false; //cookie value not found or parameter variable is empty

	}
	else
		return false;//cookies not enabled
}


function set_cookie_value(parameter, value, days)
{
	if(cookies_enabled() && parameter!="")
	{
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+days);//expiration duration
		document.cookie=parameter+"="+value+";expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
	}
	else
		return false; //cookies not enabled
}

function delete_cookie(parameter)
{
	 document.cookie = parameter + "=; expires=Thu, 01-Jan-70 00:00:01 GMT;path=/;domain=nasdaq.com";
} 

function cookies_enabled()
{
	var index;
	index = document.cookie.indexOf("c_enabled$");
	if(index==-1)
	{
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+1);//expires tomorrow
		document.cookie="c_enabled$=true;expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
		index = document.cookie.indexOf("c_enabled$");
		if(index==-1)
			return false;
		else
			return true;
	}
	else
		return true;
}

function adRotatorRandom(pct)
{
	if(pct>0)
	{
		if(Math.floor(Math.random()*100/pct)>0)
			return false;
		else
			return true;
	}
	else
	{
		return false;
	}
}

//google functions 

var c_random_hints = '';//current hints used in previous google calls, if this is set, reuse it

//getRandomGoogleHintSet will return either 1-3 random hints based on the hint set or
//3 random hints based on the default hint set
//or the current hint set already used by previous google ad calls

function getRandomGoogleHintSet()
{
	var result;
	if(typeof googlehints == 'undefined')//set to a default list of none exists
	{
		var googlehints = "stock market, stock investment, trading account, real time stock quotes, investment advice, best investments, mutual funds, portfolio tracking, Stock trading, Personal Finance, Nasdaq stocks, interest rates, Forex trading, equity trading, Pre-market quotes, after hours trading, Business news, company profiles, technology news, Nasdaq companies, online stock";
	}

	if(googlehints == '')//set to a default list if its empty
	{
		googlehints = "stock market, stock investment, trading account, real time stock quotes, investment advice, best investments, mutual funds, portfolio tracking, Stock trading, Personal Finance, Nasdaq stocks, interest rates, Forex trading, equity trading, Pre-market quotes, after hours trading, Business news, company profiles, technology news, Nasdaq companies, online stock";
	}

	if(c_random_hints=='')
	{
		var hints = googlehints.split(',');
		
		//get 3 random indexes between 0 and googlehints array size
		hintmax = hints.length;
		var index1 = Math.floor(Math.random() * hintmax);
		if(hints.length>1)
		{
			do
			{
				var index2 = Math.floor(Math.random() * hintmax);
			}
			while(index2==index1)
			
			if(hints.length>2)
			{	
				do
				{
					var index3 = Math.floor(Math.random() * hintmax);
				}
				while(index3==index2 || index3==index1)
				result = hints[index1].replace(/^\s*/, '').replace(/\s*$/, '')+" "+hints[index2].replace(/^\s*/, '').replace(/\s*$/, '')+" "+hints[index3].replace(/^\s*/, '').replace(/\s*$/, '');
			}
			else
				result = hints[index1].replace(/^\s*/, '').replace(/\s*$/, '')+" "+hints[index2].replace(/^\s*/, '').replace(/\s*$/, '');
		}
		else
			result = hints[index1].replace(/^\s*/, '').replace(/\s*$/, '');

		c_random_hints = result;
		
		return c_random_hints;	
		
	}
	else
		return c_random_hints;	
}

//returns a google ad ID based on the channel passed in
//returns slot ID for channel='footer,marketactivity' if a match isnt found

function googleGetFooterSlotForChannel(channel)
{
	var result="";

	channel = channel.toLowerCase();
	
	if(channel.search("nls")!=-1)
		result = "0838358092";
	else if(channel.search("flashquote")!=-1)
		result = "9060882363";
	else if(channel.search("summaryquote")!=-1)
		result = "7453545256";
	else if(channel.search("infoquotes")!=-1)
		result = "3288414914";
	else if(channel.search("personalf")!=-1)
		result = "1019200000";
	else if(channel.search("extended")!=-1)
		result = "1855108929";
	else if(channel.search("holdings")!=-1)
		result = "1547108559";
	else if(channel.search("listedcos")!=-1)
		result = "2418575424";
	else if(channel.search("tools")!=-1)
		result = "2451690802";
	else if(channel.search("news")!=-1)
		result = "3016900637";
	else if(channel.search("timesale")!=-1)
		result = "4017299831";
	else if(channel.search("marketactivity")!=-1)
		result = "6842037220";
	else if(channel.search("quotes")!=-1)
		result = "6899712972";
	else if(channel.search("fundamentals")!=-1)
		result = "7212344781";
	else if(channel.search("charts")!=-1)
		result = "9701580833";
	else if(channel.search("options")!=-1)
		result = "9757552146";
	else if(channel.search("commodities")!=-1)
		result = "3695868322";
	else
		result = "6842037220";

	return result;
}

function googleSetFooterAdSlotAndHints()
{
	//programmatically set the google slot and hints
	
    if(typeof sectionId != 'undefined')
	{
		if(sectionId == 'liExtended')
		{
			if(typeof googlechannel== 'undefined')//use navigation default
				google_ad_slot = '1855108929';
			else
				google_ad_slot = googleGetFooterSlotForChannel(googlechannel);
			if(typeof googlehints == 'undefined')//use navigation default
				google_hints = 'day trading internet stock trading stock market software';
		}
		else if(sectionId == "liActivity")
		{
			if(typeof googlechannel== 'undefined')//use navigation default
				google_ad_slot = '6842037220';
			else
				google_ad_slot = googleGetFooterSlotForChannel(googlechannel);
			if(typeof googlehints == 'undefined')//use navigation default
				google_hints = 'market trends market research analysis best stocks';
		}				
		else if(sectionId == "liMarket")
		{
			if(typeof googlechannel== 'undefined')//use navigation default
				google_ad_slot = '3016900637'
			else
				google_ad_slot = googleGetFooterSlotForChannel(googlechannel);
			if(typeof googlehints == 'undefined')//use navigation default
				google_hints = 'business news financial news breaking news';
		}				
		else if(sectionId == "liResearch")
		{
			if(typeof googlechannel== 'undefined')//use navigation default
				google_ad_slot = '2451690802'
			else
				google_ad_slot = googleGetFooterSlotForChannel(googlechannel);
			if(typeof googlehints == 'undefined')//use navigation default
				google_hints = 'stock screener stock analysis trading software';
		}				
		else if(sectionId == "liPersonal")
		{
			if(typeof googlechannel== 'undefined')//use navigation default
				google_ad_slot = '4073863567'
			else
				google_ad_slot = googleGetFooterSlotForChannel(googlechannel);
			if(typeof googlehints == 'undefined')//use navigation default
				google_hints = 'retirement planning financial planning credit reporting';
		}				
		else//use channel = marketactivity,footer by default and set a default hint list if none exists
		{
			if(typeof googlechannel== 'undefined')//use navigation default
				google_ad_slot = '6842037220';//use marketactivity,footer channel by default
			else
				google_ad_slot = googleGetFooterSlotForChannel(googlechannel);
			if(typeof googlehints == 'undefined')//set up a default random list if none exists
		    	googlehints = "stock market, stock investment, trading account, real time stock quotes, investment advice, best investments, mutual funds, portfolio tracking, Stock trading, Personal Finance, Nasdaq stocks, interest rates, Forex trading, equity trading, Pre-market quotes, after hours trading, Business news, company profiles, technology news, Nasdaq companies, online stock";
		}				
	}
	else//no section defined, //use channel = marketactivity,footer by default and set a default hint list if none exists
    {
   		if(typeof googlechannel== 'undefined')//use navigation default
			google_ad_slot = '6842037220';//use marketactivity,footer channel by default
	    else
			google_ad_slot = googleGetFooterSlotForChannel(googlechannel);
		if(typeof googlehints == 'undefined')//set up a default random list if none exists
	    	googlehints = "stock market, stock investment, trading account, real time stock quotes, investment advice, best investments, mutual funds, portfolio tracking, Stock trading, Personal Finance, Nasdaq stocks, interest rates, Forex trading, equity trading, Pre-market quotes, after hours trading, Business news, company profiles, technology news, Nasdaq companies, online stock";
    }
 
     
    if(typeof googlehints != 'undefined')
    {
		//if a hint list exists, there's either a page-defined hint list 
		//or a default list was set either because of a previous google ad call 
		//or because no hints could be set based on navigation in the code above 
		google_hints = getRandomGoogleHintSet();
	}
}

//omniture functions

var eventTracking = "";

function setPageEvent(eventName)
{
	eventTracking = eventName;
}

function addPageEvent(eventName)
{
	eventTracking += eventName;
}

function DisplayingUserName()
{
	if(typeof displayingCommUserName!="undefined")
		return displayingCommUserName;
	else
		return false;
}

function setSectionName(strnavArea) 
{
if(typeof myPage != 'undefined')
{
 s.prop20 = myPage; <!-- added to view pages by mypage variable in Custom Insight -->
}

s.pageName= document.title;

if(typeof propVar!="undefined")
	s.prop1 = propVar;
else
	if(strnavArea=="community")//most community pages will default to this
		s.prop1 = "NASDAQ Community";

if(typeof propVar2!="undefined")
	s.prop2 = propVar2;
else
	if(strnavArea=="community")//community pages will default to this if nothing else is set
		s.prop2 = "Uncategorized";

if(typeof propVar3!="undefined")
	s.prop3 = propVar3;

if(typeof propVar4!="undefined")
	s.prop4 = propVar4;

if(typeof propVar5!="undefined")
	s.prop5 = propVar5;

if(typeof propVar15!="undefined")
	s.prop15 = propVar15;

if(typeof omniture_symbol != "undefined")
{	s.prop10 = omniture_symbol;}

//track investor type
if(typeof track_education != 'undefined')//should only be true after initial login
{
	s.setInvestorInformation(track_education,track_occupation,track_industry,track_investortype,track_risktolerance,track_yearsinvesting,track_tradespermonth,track_portfoliosize);
}

if(typeof propVar15!="undefined")
	s.channel=propVar15;
else
    s.channel = "";

s.server="";
s.pageType="";
/* E-commerce Variables */
s.campaign="";
s.state="";
s.zip="";
s.events=eventTracking;
s.products="";
s.purchaseID="";
s.eVar1="";
s.eVar2="";
s.eVar3="";
s.eVar4="";
s.eVar5="";

if (typeof quoteBoxSymbolArray!="undefined")
{ 
	s.eVar4=quoteBoxSymbolArray[0]; 

	for (x=1;x<quoteBoxSymbolArray.length;x++)
	{ 
		s.eVar4+=":"+quoteBoxSymbolArray[x]; 
	} 
	s.eVar5=(typeof quoteBoxSelectedSymbol!="undefined")?quoteBoxSelectedSymbol:""; 
	s.prop12=s.eVar4; 
	s.prop13=s.eVar5; 
	s.events="event3"; 
}
/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
var s_code=s.t();if(s_code)document.write(s_code);
}