function formatSymbol(sym)
{
	sym = sym.toLowerCase().replace("/","-");//change to lower case and replace / with -
	return sym;
}


function CheckInput(pageId) 
{
	var target = '';
	selected = '';
	arSymbols = '';
	bLookup = false;
	var urlre = /^http/;
	
	
		if (document.getElementById("quotepage").value == 'NasdaqLastsale')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/real-time";
		}
		else if (document.getElementById("quotepage").value == 'nlstrades')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/time-sales";
		}
		else if (document.getElementById("quotepage").value == 'quick') 
		{
			//if(qbUseNewNav)
			//	window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/stock-quotes";
			//else
			window.location.href = wwwURL + 'aspx/flashquotes.aspx?symbol=' + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol;
			return;
		}
		else if (document.getElementById("quotepage").value == 'sentiment') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/stream";
			return;
		}

		else if (document.getElementById("quotepage").value == 'multi') 
		{
			//if(qbUseNewNav)
			//	window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/stock-quotes";
			//else
			window.location.href = wwwURL + 'aspx/infoquotes.aspx?symbol=' + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol;
			return;
		}
		else if (document.getElementById("quotepage").value == 'summaryquote')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol);
		}
		else if (document.getElementById("quotepage").value == 'charting') 
		{
			
			if(myPage != "charting")
			{
				window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/stock-chart";				
			}
			else
			{
				var chartingOptions="";
				//carry over user parameters
				if(document.quotenav.intraday.value!="")
					chartingOptions += "&intraday="+document.quotenav.intraday.value;

				if(document.quotenav.timeframe.value!="")
					chartingOptions += "&timeframe="+document.quotenav.timeframe.value;

				if(document.quotenav.charttype.value!="")
					chartingOptions += "&charttype="+document.quotenav.charttype.value;
	
				if(document.quotenav.splits.value!="")
					chartingOptions += "&splits="+document.quotenav.splits.value;

				if(document.quotenav.movingaverage.value!="")
					chartingOptions += "&movingaverage="+document.quotenav.movingaverage.value;

				if(document.quotenav.lowerstudy.value!="")
					chartingOptions += "&lowerstudy="+document.quotenav.lowerstudy.value

				if(chartingOptions!="")
					chartingOptions = "?"+chartingOptions;
				
				window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/stock-chart" + chartingOptions;
				
			}
		}
		else if(document.getElementById("quotepage").value == 'dynamic_charting')
		{	
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/interactive-chart";		
		}
		else if (document.getElementById("quotepage").value == 'redpage') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/revenue-eps";
		}
		else if (document.getElementById("quotepage").value == 'full')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/financials";
		}
		else if (document.getElementById("quotepage").value == 'competitor')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/competitors";			
		}
		else if (document.getElementById("quotepage").value == 'short') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/short-interest";
		}
		else if (document.getElementById("quotepage").value == 'news')
		{
			window.location.href = wwwURL + 'asp/quotes_news.asp?symbol=' + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol;
			return;
		}
		else if (document.getElementById("quotepage").value == 'companynews')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/news-headlines";
			return;
		}
		else if (document.getElementById("quotepage").value == 'companypressrelease')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/press-releases";
			return;
		}
		else if (document.getElementById("quotepage").value == 'companynewsthestreet')
		{
			window.location.href = wwwURL + 'aspxcontent/newsheadlines.aspx?symbol=' + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol + "&SourceCode=TSTRT";
			return;
		}
		else if (document.getElementById("quotepage").value == 'companynewsthefool')
		{
			window.location.href = wwwURL + 'aspxcontent/newsheadlines.aspx?symbol=' + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol + "&SourceCode=MFOOL";
			return;
		}		
		else if (document.getElementById("quotepage").value == 'analystinfo')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/analyst-research";
		}
		else if (document.getElementById("quotepage").value == 'stockreports')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/stock-report";
		}
		else if (document.getElementById("quotepage").value == 'annualrpt') 
		{
			//if(qbUseNewNav)
			//	window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/annual-report";
			//else				
			window.location.href = wwwURL + 'aspx/annualreport.aspx?symbol=' + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol;
			return;
		}
		// filings
		else if (document.getElementById("quotepage").value == 'holdingssummary')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/ownership-summary";
			return;
		}
		else if (document.getElementById("quotepage").value == 'holdings') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/institutional-holdings";
			return;
		}
		else if (document.getElementById("quotepage").value == 'insiderform4')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/insider-trades";
			return;
		}
		else if (document.getElementById("quotepage").value == 'insiderform144')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/insider-trades";
			return;
		}
		else if (document.getElementById("quotepage").value == 'companyfilings' || document.getElementById("quotepage").value == 'filings')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/sec-filings";
			return;
		}
		else if (document.getElementById("quotepage").value == 'CallTranscripts')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/call-transcripts";
		}
		else if (document.getElementById("quotepage").value == 'options')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/option-chain";
		}
		else if (document.getElementById("quotepage").value == 'stockconsultant') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/stock-consultant";
		}
		else if (document.getElementById("quotepage").value == 'stockcomparison') 
		{
			//if(qbUseNewNav)
			//	window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/stock-comparison";
			//else				
			window.location.href = wwwURL + "aspx/stock-comparison.aspx?symbol=" + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol;
			return;
	    }
		else if (document.getElementById("quotepage").value == 'guruanalysis')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis";
		}
	    else if (document.getElementById("quotepage").value == 'afterhours')
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/after-hours";
		}
		else if (document.getElementById("quotepage").value == 'premarket') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/premarket";
		} 
		else if (document.getElementById("quotepage").value == 'afterhourscharts') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/after-hours-chart";
	    }
	 	else if (document.getElementById("quotepage").value == 'premarketcharts') 
		{
	        window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/premarket-chart";
        }			
		else if (document.getElementById("quotepage").value == 'historical_quotes') 
		{
	        window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/historical";
		}
        else if (document.getElementById("quotepage").value == 'chartingfunds') 
		{
			window.location.href = wwwURL + 'aspx/mutual-fund-chart.aspx?symbol=' + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol;
			return;
        }
		else if (document.getElementById("quotepage").value == 'profilemutual') 
		{
	        window.location.href = wwwURL + 'aspx/fundprofile.aspx?symbol=' + quoteBoxSelectedSymbol + "&selected=" + quoteBoxSelectedSymbol;
			return;
        }
		else if (document.getElementById("quotepage").value == 'dividend_history') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/dividend-history";
		}
	    else if (document.getElementById("quotepage").value == 'guru-lynch') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis/lynch";
		}
		else if (document.getElementById("quotepage").value == 'guru-graham') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis/graham";
		}
		else if (document.getElementById("quotepage").value == 'guru-validea') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis/validea-momentum";
		}
		else if (document.getElementById("quotepage").value == 'guru-fool') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis/fool";
		}
		else if (document.getElementById("quotepage").value == 'guru-dreman') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis/dreman";
		}
		else if (document.getElementById("quotepage").value == 'guru-zweig') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis/zweig";
		}
		else if (document.getElementById("quotepage").value == 'guru-fisher') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis/fisher";
		}
		else if (document.getElementById("quotepage").value == 'guru-oshaughnessy') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/guru-analysis/oshaughnessy";
		}
		else if (document.getElementById("quotepage").value == 'etfdetail') 
		{
			window.location.href = wwwURL + 'symbol/' + formatSymbol(quoteBoxSelectedSymbol) + "/etf-detail";
		}
}


function open_new_window(url, width, height, statusbar, scrollbar, menubar) {
    window.name = "ParentWin";
    window.open(url, "newWin", "menubar="+(menubar?"yes":"no")+",toolbar=0,location=0,directories=0,copyhistory=0,scrollbars="+(scrollbar?"yes":"no")+",resizable=yes,status="+(statusbar?"yes":"no")+",width="+width+",height="+height);

}


function readResults(){
	readStockResults();
	return;
}

var validationIndex=-1;
var validationSelectedInvalid = false;

//validationIndex is initialized to -1
//readStockResults is called from readResults which is called from an invisible iframe on the page for symbol validation
//there are two times where validation is used:
//add single symbol and edit symbol list
//for add single symbol validation, validationIndex will be -1
//for edit symbol list validation, validationIndex will be the index of the current symbol being validated

function readStockResults()
{
	var result = window.frames["validateSymbolForm"].document.body.innerHTML.split('|');
		// [0] - symbol
		// [1] - is valid symbol?
		// [2] - is new symbol?
		// [3] - new symbol
		// [4] - change date.
	
	if(validationIndex == -1)//doing add single symbol validation
	{
		if(result[1].toLowerCase()=='true')//it validated, load it
		{
				displaySymbolInfo(result[0]);
		} 
		else if(result[2].toLowerCase()=='true')//it changed to a new symbol, show a message and then load it
		{
				alert(result[0] + ' is changed to ' + result[3] + ' on ' + result[4]);
				if(!isDuplicate(result[3]))
				{	
					displaySymbolInfo(result[3]);
				}
				else
				{
					selectSymbol(result[3]);
				}					
		} 
		else //not a valid symbol, just stop
		{
				alert(result[0] + ' is Not a Valid Symbol');
				document.frmAddASymbol.symbol.value = '';
				document.frmAddASymbol.symbol.focus();
				validatingSymbol = false;
		}
	}
	else //doing edit symbol list validation
	{
		if(result[1].toLowerCase()=='true')//it validated, move on to the next one
		{
				alert("getting here?2");
				persistValidation(validationIndex+1);
		} 
		else if(result[2].toLowerCase()=='true')//it changed to a new symbol, show a message, replace the old entry, and move on to the next one
		{
				alert(result[0] + ' is changed to ' + result[3] + ' on ' + result[4]);

					if(!isDuplicate(result[3]))
					{	
						quoteBoxSymbolArray[validationIndex] = result[3];
						persistValidation(validationIndex+1);
					}
					else
					{	
						quoteBoxSymbolArray.splice(validationIndex,1);//remove from the array
						if(result[0] == quoteBoxSelectedSymbol) //is this the selected?
						{
							if(quoteBoxSymbolArray.length>0)
							{
								validationSelectedInvalid = true;
								if(validationIndex < quoteBoxSymbolArray.length-1) 
									quoteBoxSelectedSymbol = quoteBoxSymbolArray[validationIndex]; //set selected to the next symbol if there is one
								else
									quoteBoxSelectedSymbol = quoteBoxSymbolArray[0]; //otherwise set selected to the first valid symbol
							}
						}
						persistValidation(validationIndex); //go on with list
					}
		} 
		else 
		{
			alert(result[0] + ' is Not a Valid Symbol');//not a valid symbol, remove this entry
			quoteBoxSymbolArray.splice(validationIndex,1);//remove from the array
			if(result[0] == quoteBoxSelectedSymbol) //is this the selected?
			{
				if(quoteBoxSymbolArray.length>0)
				{
					validationSelectedInvalid = true;
					if(validationIndex < quoteBoxSymbolArray.length-1) 
						quoteBoxSelectedSymbol = quoteBoxSymbolArray[validationIndex]; //set selected to the next symbol if there is one
					else
						quoteBoxSelectedSymbol = quoteBoxSymbolArray[0]; //otherwise set selected to the first valid symbol
				}
			}
			persistValidation(validationIndex); //go on with list
		}
	}
	return;
}

function displaySymbolInfo(symbol)
{
	if(quoteBoxNumSymbols==25)
	{
		alert('Can not add more than 25 symbols. Please use Edit Symbol List to edit your symbol list.');
		validatingSymbol = false;
		return false;
	}
	
	//set new symbol as selected and add to symbol array new option at the end
	quoteBoxSelectedSymbol = symbol;
	quoteBoxSymbolArray.unshift(symbol); //add to first element in array

	//save the new cookie
	var preference; //save my list for next time preference
    preference = get_cookie_value("userCookiePref");
	var exdate=new Date();
	if(preference=="true")
		exdate.setDate(exdate.getDate()+365);//expires in 1 year
	else
		exdate.setHours(exdate.getHours()+12);//expires in 12 hours
	quoteBoxSymbolArray[0]=quoteBoxSymbolArray[0]+"+";//add tag to selected
	document.cookie="userSymbolList="+quoteBoxSymbolArray.join('&')+";expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
	quoteBoxSymbolArray[0]=quoteBoxSymbolArray[0].replace("+","");//remove the tag from selected
	
	//quoteBoxSymbolArray.splice(1,quoteBoxSymbolArray.length-1); //remove symbols from the array
	//quoteBoxNumSymbols = 1;
	
	CheckInput(); //reload the page		
}

function qbgetIndex(symbol)
{
	for (var t=0;t<quoteBoxSymbolArray.length;t++)
	{
		if (quoteBoxSymbolArray[t] == symbol)
			return t;
	
	}
	return -1;
}


function isDuplicate(newSymbol)
{
	newSymbol = newSymbol.toUpperCase();
	for (var t=0;t<quoteBoxSymbolArray.length;t++)
	{
		if (quoteBoxSymbolArray[t] == newSymbol)
			return true;
	
	}
	return false;
}


function selectSymbol(sym)
{
    quoteBoxSelectedSymbol = sym;

	//update the cookie unconditionally
	index = qbgetIndex(sym);
	if(index==-1)
		return;
	//save the new cookie
	var preference; //save my list for next time preference
    preference = get_cookie_value("userCookiePref");
	var exdate=new Date();
	if(preference=="true")
		exdate.setDate(exdate.getDate()+365);//expires in 1 year
	else
		exdate.setHours(exdate.getHours()+12);//expires in 12 hours
	quoteBoxSymbolArray[index]=quoteBoxSymbolArray[index]+"+";//add tag to selected
	document.cookie="userSymbolList="+quoteBoxSymbolArray.join('&')+";expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
	quoteBoxSymbolArray[index]=quoteBoxSymbolArray[index].replace("+","");//remove the tag from selected
	//quoteBoxSymbolArray.unshift(sym);  //put at the beginning of array
	//quoteBoxSymbolArray.splice(1,quoteBoxSymbolArray.length-1); //remove symbols from the array
	//quoteBoxNumSymbols = 1;
	
	CheckInput(); //reload the page		
}


function trimSymbol(str)
{
    return str.replace(/^\s*/, '').replace(/\s*$/, '');
}

var xmlhttp = null;
var xmlhttpsym;
var validatingSymbol = false;
function validateSymbol()
{
	var url;
	var newSymbol = document.frmAddASymbol.symbol.value.toUpperCase();
	var str;
	
	if(validatingSymbol)
		return false;

	newSymbol = trimSymbol(newSymbol);

	if (newSymbol=='') 
	{
        alert('Please enter a symbol and reselect the add button');
		document.frmAddASymbol.symbol.value = '';
		document.frmAddASymbol.symbol.focus();
   	    return false;
    }
	if(isDuplicate(newSymbol)) 
	{
		selectSymbol(newSymbol);
	}
	else//validate the symbol
	{
		if(window.XMLHttpRequest)
		{
			xmlhttp = new XMLHttpRequest();
		}
		else if(window.ActiveXObject)
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}

		if(xmlhttp==null)
		{
			//use the old iframe method instead
			url = 'http://www.nasdaq.com/aspx/ValidateSymbol.aspx?mode=stock&selected=' + encodeURIComponent(newSymbol);
			document.getElementById("validateSymbolForm").src = url;//calls readResults()
		}
		else
		{
			xmlhttpsym = newSymbol;
			url = '/aspx/ValidateSymbol.aspx?mode=stock';
			url += '&selected=' + encodeURIComponent(newSymbol);

			xmlhttp.onreadystatechange = readResults2;
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
		}
		validatingSymbol = true;
	}
	
	return false;	
}


function readResults2()
{
	if(xmlhttp.readyState == 4 || xmlhttp.readyState == "complete")
	{
		if(xmlhttp.responseText != "")
		{
			var validated = false;
			
			var result = xmlhttp.responseText.split('|');//can't use result[0] here, use xmlhttpsym instead

			result[4] = result[4].replace(/<\/body>/i,"").replace(/<\/html>/i,"");
	
			if(validationIndex == -1)//doing add single symbol validation
			{
				if(result[1].toLowerCase()=='true')//it validated, load it
				{
					displaySymbolInfo(xmlhttpsym);
				}
				else if(result[2].toLowerCase()=='true')//it changed to a new symbol, show a message and then load it
				{
					alert(xmlhttpsym + ' changed to ' + result[3] + ' on ' + result[4]);
					if(!isDuplicate(result[3]))
					{	
						displaySymbolInfo(result[3]);
					}
					else
					{
						selectSymbol(result[3]);
					}
				} 
				else //not a valid symbol, just stop
				{
					alert(xmlhttpsym + ' is not a Valid Symbol.');
					document.frmAddASymbol.symbol.value = '';
					document.frmAddASymbol.symbol.focus();
					validatingSymbol = false;
				}
			}
			else //doing edit symbol list validation
			{
				if(result[1].toLowerCase()=='true')//it validated, move on to the next one
				{
					if(!isDuplicate(result[3]))
						persistValidation(validationIndex+1);
					else
					{
						//alert("getting here?");
						quoteBoxSymbolArray.splice(validationIndex,1);//remove from the array
						if(xmlhttpsym == quoteBoxSelectedSymbol) //is this the selected?
						{
							if(quoteBoxSymbolArray.length>0)
							{
								validationSelectedInvalid = true;
								if(validationIndex < quoteBoxSymbolArray.length-1) 
									quoteBoxSelectedSymbol = quoteBoxSymbolArray[validationIndex]; //set selected to the next symbol if there is one
								else
									quoteBoxSelectedSymbol = quoteBoxSymbolArray[0]; //otherwise set selected to the first valid symbol
							}
						}
						persistValidation(validationIndex); //go on with list
					}
				} 
				else if(result[2].toLowerCase()=='true')//it changed to a new symbol, show a message, replace the old entry, and move on to the next one
				{
					alert(xmlhttpsym + ' changed to ' + result[3] + ' on ' + result[4]);

					if(!isDuplicate(result[3]))
					{	
						quoteBoxSymbolArray[validationIndex] = result[3];
						persistValidation(validationIndex+1);
					}
					else
					{	
						quoteBoxSymbolArray.splice(validationIndex,1);//remove from the array
						if(xmlhttpsym == quoteBoxSelectedSymbol) //is this the selected?
						{
							if(quoteBoxSymbolArray.length>0)
							{
								validationSelectedInvalid = true;
								if(validationIndex < quoteBoxSymbolArray.length-1) 
									quoteBoxSelectedSymbol = quoteBoxSymbolArray[validationIndex]; //set selected to the next symbol if there is one
								else
									quoteBoxSelectedSymbol = quoteBoxSymbolArray[0]; //otherwise set selected to the first valid symbol
							}
						}
						persistValidation(validationIndex); //go on with list
					}
				} 
				else 
				{
					alert(xmlhttpsym + ' is not a Valid Symbol.');//not a valid symbol, remove this entry
					quoteBoxSymbolArray.splice(validationIndex,1);//remove from the array
					if(xmlhttpsym == quoteBoxSelectedSymbol) //is this the selected?
					{
						if(quoteBoxSymbolArray.length>0)
						{
							validationSelectedInvalid = true;
							if(validationIndex < quoteBoxSymbolArray.length-1) 
								quoteBoxSelectedSymbol = quoteBoxSymbolArray[validationIndex]; //set selected to the next symbol if there is one
							else
								quoteBoxSelectedSymbol = quoteBoxSymbolArray[0]; //otherwise set selected to the first valid symbol
						}
					}
					persistValidation(validationIndex); //go on with list
				}
			}
		}
		
	}
}


//called from NasdaqSymLookupNoIndex.aspx
function AddSymbol(symbol) 
{
	var editsymbolactive = false;
	if(typeof modalexists != 'undefined')
	{
		if(modalexists == true)
		{
			if(currentWindowHandle == "symbol_lookup_modal_box")
			{
				if(typeof doingComparisonLookup != 'undefined')//on comparison page
				{
					if(doingComparisonLookup == true)//looking up comparison symbol?
					{
						AddComparisonSymbol(symbol);//add the comparison symbol and bailout
						return;
					}
				}
				popmodal();
			}
			if(currentWindowHandle == "edit_symbol_modal_box")
				editsymbolactive = true;
		}				
	}
	if(!editsymbolactive)
	{	
		if(isDuplicate(symbol))
		{
			selectSymbol(symbol);
		}
		else
		{
			displaySymbolInfo(symbol);
		}
	}
	else
	{
		document.getElementById("modalinput").focus();
		document.getElementById("modalinput").value = symbol+" "+document.getElementById("modalinput").value;
		//alert(symbol + " has been added to your symbol list");
	}
}

function ShowELSSymbol(symbol)//this is for euro symbols being added
{
	document.location.href = "http://www.nasdaq.com/aspx/els.aspx?selected="+symbol;
}

function initWidget()
{
	selectPage();
	loadSymbols();
	//validateSymbols();
}

function initCookie()
{
	var preference; //save my list for next time preference
    preference = get_cookie_value("userCookiePref");
	if(preference=="true" || preference==false)
	{
		if(cookies_enabled())
			document.getElementById("cookiepref").checked = true;
		updateCookieList(quoteBoxSymbolArray,true,quoteBoxSelectedSymbol);
	}
	else
		updateCookieList(quoteBoxSymbolArray,false,quoteBoxSelectedSymbol);
}

function selectPage()
{
	var currentPage;
	var newsIndex;
	var CallTransIndex;
	for(var i=0;i<document.getElementById("quotepage").length;i++)
	{
		currentPage = document.getElementById("quotepage").options[i].value; 
		if(currentPage == myPage)
		{
			document.getElementById("quotepage").options[i].selected = true;
			if(myPage!='nlstrades' && document.getElementById(currentPage+"link")!=null)//nls trades has no menu entry right now
				document.getElementById(currentPage+"link").className = "qn_on";
			return;
		}
		if(currentPage == "companynews")
			newsIndex = i;
		else if(currentPage == "CallTranscripts")
			CallTransIndex = i;
			
	}
	//misc. catch pages
	if(myPage == 'companynewsthestreet' || myPage == 'companynewsthefool' || myPage == 'companynewsstory')
	{
		document.getElementById("quotepage").options[newsIndex].selected = true;
		//if(myPage == 'companynewsthestreet')
		//	document.getElementById("companynewsthestreetlink").className = "qn_on";
		//else if(myPage == 'companynewsthefool')
		//	document.getElementById("companynewsthefoollink").className = "qn_on";
		
			document.getElementById("companynewslink").className = "qn_on";
	}
	else if(myPage == 'CallTranscript')
	{
		document.getElementById("quotepage").options[CallTransIndex].selected = true;
		document.getElementById("CallTranscriptslink").className = "qn_on";
	}
		
	return;
}

function loadSymbols()
{
	var query = window.location.search.substring(1);
	var params = query.split('&');
	
	for (var i=0;i<params.length;i++)
	{
		symbol = params[i].split('=');
		if (symbol[0].toLowerCase() =='mkttype' && myPage =='afterhours')
		{
			if (symbol[1].toLowerCase() == 'after')
			{
				document.quotenav.mkttype.value='after';
			}
			else
			{
				document.quotenav.mkttype.value='pre'
			}
		}
		else if (symbol[0].toLowerCase() == 'intraday' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.intraday.value=symbol[1];
		}
		else if (symbol[0].toLowerCase() == 'timeframe' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.timeframe.value=symbol[1];
		}
		else if (symbol[0].toLowerCase() == 'charttype' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.charttype.value=symbol[1];
		}
		else if (symbol[0].toLowerCase() == 'splits' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.splits.value=symbol[1];
		}
		else if (symbol[0].toLowerCase() == 'earnings' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.earnings.value=symbol[1];
		}
		else if (symbol[0].toLowerCase() == 'movingaverage' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.movingaverage.value=symbol[1];
		}
		else if (symbol[0].toLowerCase() == 'lowerstudy' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.lowerstudy.value=symbol[1];
		}
		else if (symbol[0].toLowerCase() == 'comparison' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.comparison.value=symbol[1];
		}
		else if (symbol[0].toLowerCase() == 'index' && symbol[1] != null && symbol[1].length > 0)
		{
			document.quotenav.index.value=symbol[1];
		}
				
	}
			
	return;
}

var validationTimerHandle;

function validateSymbols()
{
	persistValidation(0);
}

function persistValidation(index)
{
	if(quoteBoxSymbolArray.length>0)
	{
		if(index<quoteBoxSymbolArray.length)//are we still validating?
		{
			validationIndex = index;
			validateInputSymbol(quoteBoxSymbolArray[index]);
		}
		else //done
		{
			quoteBoxNumSymbols = quoteBoxSymbolArray.length; //number of valid symbols
			if(validationSelectedInvalid) //selected symbol was invalid
				validationIndex = -2
			else
				validationIndex = -1;//everything went smoothly

			validatingSymbol = false;
		}
	}
	else //no valid symbols were found
	{
		validationIndex = -3;
		validatingSymbol = false;
	}
}

function validateInputSymbol(symbol)
{
	var url;
	var str;
	newSymbol = trimSymbol(symbol);	
	validatingSymbol = true;
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if(xmlhttp==null)
	{
		//use the old iframe method instead
		url = 'http://www.nasdaq.com/aspx/ValidateSymbol.aspx?mode=stock';
		url += '&selected=' + newSymbol;
		document.getElementById("validateSymbolForm").src = url;//calls readResults()
	}
	else
	{
		xmlhttpsym = newSymbol;
		url = '/aspx/ValidateSymbol.aspx?mode=stock';
		url += '&selected=' + newSymbol;

		xmlhttp.onreadystatechange = readResults2;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}


function trimTick(tmpSym)
{
	var	myLen = tmpSym.length;
	if (tmpSym.indexOf('`') > 0)
		tmpSym = tmpSym.substr(0, eval(myLen - 1));

	if (tmpSym.indexOf('%60') > 0)
		tmpSym = tmpSym.substr(0, eval(myLen - 3));
	return tmpSym;
}



//*********edit symbol list modal functions
function editSymbolListModalBoxInit()
{
	var input =	document.getElementById("modalinput");
	input.focus();
	input.value = quoteBoxSymbolArray.join(" ")+" ";
}

function clearlist()
{
	var input =	document.getElementById("modalinput");
	input.focus();
	input.value = "";
	
}

function replaceComma(temp) 
{
	//return temp.replace(/,/g, ' ');

	var out = ','; // replace this
	var add = ' '; // with this
	temp = '' + temp + ' '; // temporary holder

	while (temp.indexOf(out)>-1) 
	{
		pos= temp.indexOf(out);
		temp = '' + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function trim(str)
{
    return str.replace(/^\s*/, '').replace(/\s*$/, '');
}

function gomodal()
{
	var re = /\s */;
	var sInput= document.getElementById("modalinput").value.toUpperCase();

	if(validatingSymbol)
		return false;

	sInput = replaceComma(sInput);
	sInput = trim(sInput);
	if(sInput!="")
	{
		var arStrings = sInput.split(re);

		quoteBoxSelectedSymbol = arStrings[0];

		if(document.getElementById("orderlistcontrol").checked==true)
			arStrings = arStrings.sort();

		if (arStrings.length>25) 
		{
				alert('You may only choose up to 25 symbols. Please edit your list in the data entry box.');
				document.getElementById("modalinput").focus();
				return true;
		}
		
		quoteBoxSymbolArray.splice(0,quoteBoxSymbolArray.length); //remove symbols from array

		for(i=0; i<arStrings.length;i++)
		{
			quoteBoxSymbolArray[i] = arStrings[i]; //add new symbols to the array
		}
		validateSymbols();
        goModalNext();				
	}
	else
		document.getElementById("modalinput").focus();
}

function goModalNext()
{
	if(validationIndex==-1 || validationIndex==-2)
	{
		
		//save the new cookie
		var preference; //save my list for next time preference
	    preference = get_cookie_value("userCookiePref");
		var exdate=new Date();
		if(preference=="true")
			exdate.setDate(exdate.getDate()+365);//expires in 1 year
		else
			exdate.setHours(exdate.getHours()+12);//expires in 12 hours

		quoteBoxSymbolArray[0]=quoteBoxSymbolArray[0]+"+";//add tag to selected
		document.cookie="userSymbolList="+quoteBoxSymbolArray.join('&')+";expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
		quoteBoxSymbolArray[0]=quoteBoxSymbolArray[0].replace("+","");//remove the tag from selected
			
		if(quoteBoxSymbolArray.length>1)
			quoteBoxSymbolArray.splice(1,quoteBoxSymbolArray.length-1); //remove symbols from the array
		quoteBoxNumSymbols = 1;
			
		CheckInput(); //reload the page
		
	}
	else if(validationIndex==-3)
	{
		alert("No valid symbols entered. Please edit your list in the data entry box."); 
		document.getElementById("modalinput").focus();
	}
	else //still validating symbols retry in a little while
		validationTimerHandle = setTimeout("goModalNext()",500);
//if the first symbol is invalid, the page will automatically reload once validation is finished
}

//*************symbol lookup modal functions
function symbolLookupModalBoxinit(type)
{
	var frame = document.getElementById("msymbollookupframe");
	frame.style.visibility = "visible";
	frame.src = quoteURL + 'aspx/nasdaqsymlookupNoIndex.aspx?mode='+type;
}
	
var removedCookieSyms = new Array();

function updateCookieList(symbolList,save, selected)
{
	if(cookies_enabled())
	{

		var total = symbolList.length;
	
		var list="";
		var j,count,sym;
		count=symbolList.length;
		if(count>0)
		{
			for(j=count-1;j>=0;j--)
			{
				if ( (symbolList[j]) && (symbolList[j] != ''))
				{
					sym = symbolList[j].toUpperCase();
					if(sym==selected)
						sym+="+";	//add a tag for selected symbol
					if(list!="")
						list = sym.concat("&"+list);
					else
						list = sym;
				}
			}
		
			var exdate=new Date();
			if(save)
			{
				exdate.setDate(exdate.getDate()+365);//expires in 1 year
			}
			else
			{
				exdate.setHours(exdate.getHours()+12);//expires in 12 hours
			}
			
			document.cookie="userSymbolList="+list+";expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
						
			exdate.setDate(exdate.getDate()+365);//expires in 1 year
			if(save)
				document.cookie="userCookiePref=true;expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
			else
				document.cookie="userCookiePref=false;expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
		}
					
	}//cookies not enabled
}

function toggleCookieNav()
{
	if(cookies_enabled())
	{
		if(get_cookie_value("userCookiePref")=="true")//disabling cookie nav
		{
			var confirmation=confirm("Are you sure? Choosing the OK button will clear your current symbol list for next time");
			if(confirmation==true)
			{
				var exdate=new Date();
				exdate.setDate(exdate.getDate()+365);//expires in 1 year
				document.cookie="userCookiePref=false;expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
			}			
			else
			{
				document.getElementById("cookiepref").checked = true;
			}
		}
		else//enabling cookie nav
		{
			var exdate=new Date();
			exdate.setDate(exdate.getDate()+365);//expires in 1 year
			document.cookie="userCookiePref=true;expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";			
		}
	}
	else
	{
		alert("Browser cookies must be enabled to view multiple symbols during your session");
		document.getElementById("cookiepref").checked = false;
	}
}

//input box cursor, called after ads are loaded
function hpPlaceCursor()
{
	document.getElementById("AddASymbol").focus();//place the cursor in the input box after ads
}

function deleteSym(ele,symbol)
{
	var removeIndex, selectedIndex;
	var preference; //save my list for next time preference

	//if(confirm("Delete "+symbol+" from your list?"))
	//{
		removeIndex = qbgetIndex(symbol);
		selectedIndex = qbgetIndex(quoteBoxSelectedSymbol);
		if(removeIndex!=-1 && selectedIndex!=-1 && removeIndex!=selectedIndex)
		{
			quoteBoxSymbolArray.splice(removeIndex,1);
	
			preference = get_cookie_value("userCookiePref");
			var exdate=new Date();
			if(preference=="true")
				exdate.setDate(exdate.getDate()+365);//expires in 1 year
			else
				exdate.setHours(exdate.getHours()+12);//expires in 12 hours

			quoteBoxSymbolArray[selectedIndex]=quoteBoxSymbolArray[selectedIndex]+"+";//add tag to selected
			document.cookie="userSymbolList="+quoteBoxSymbolArray.join('&')+";expires="+exdate.toGMTString()+";path=/;domain=nasdaq.com";
			quoteBoxSymbolArray[selectedIndex]=quoteBoxSymbolArray[selectedIndex].replace("+","");//remove the tag from selected
	
			ele.parentNode.parentNode.removeChild(ele.parentNode);
		}
	//}
	
}

$(function()
{
	$("#AddASymbol").autocomplete("/aspx/autocompletesearch.aspx",{minChars:2,max:9,cacheLength:1,delay:50,selectFirst:false,extraParams:{sortBy:"symbol"}}).result(function(event, item) 
	{
		validateSymbol(item);
	});
});

initWidget();