//------------------------------------------------------------------------------
// Javascript Globals                                                           |
//------------------------------------------------------------------------------

// this is a collection of objects holding banner rotation data
    var currBanner = new Array();

// number of tries to load the page
var loadGWTryCount = 0;


//------------------------------------------------------------------------------
// bug fix for ie6                                                          |
//------------------------------------------------------------------------------

        try {
        document.execCommand("BackgroundImageCache", false, true);
        } catch(err) {}

//------------------------------------------------------------------------------
//WIDGET SWITCHBOARD                                                           |
//------------------------------------------------------------------------------
  var pageID = getQueryVariable("id");
  if (pageID != "") {
      switch(pageID.toLowerCase()) {
        case "marketsystemstatus":
            setTimeout("GetCurrSystemStatus()", 1000*60);
            break;
        case "marketsystemstatustoday":
            setTimeout("GetTodaySystemStatus()", 1000*60);
            break;
        case "marketsystemstatussearch":
            initSystemStatusSearchPage();
            break;
        case "currentheadlines":
             PageGWLoadCheck();
             break;
        case "microcurrentheadlines":
             PageGWLoadCheck();
             break;
        case "currentheadlinesbyid":
             //alert("Inside currentheadlinesbyid"); 
             PageGWLoadCheck();
             break;
        case "archiveheadlines":
             PageGWLoadCheck();
             break;
        case "microarchiveheadlines":
             PageGWLoadCheck();
             break;
        case "tradehalts":
            setTimeout("GetTradeHalts()", 1000*60);
            break;
        case "clearlyerroneousform":
            InitCEForm();
            break;
        case "moreinfodataproducts":
            PageGWLoadCheck();
            break;
        case "moreinfofinancialproducts":
            PageGWLoadCheck();
            break;
        case "passwordchange":
             PageGWLoadCheck();
             break;                                               
      }
  }
    
function OnPageLoaded()
{
	if (pageID != "") {
        switch(pageID.toLowerCase()) {       
        case "currentheadlines":
             SetCurrentHeadlinesOptions();  
             break;
        case "microcurrentheadlines":
             SetMicroCurrentHeadlinesOptions();  
             break;
        case "currentheadlinesbyid":
             //alert("Inside currentheadlinesbyid"); 
             SetCurrentHeadlinesOptionsByCategory();            
             break;
        case "archiveheadlines":
             SetArchiveHeadlinesOptions();          
             break;
        case "microarchiveheadlines":
             SetArchiveHeadlinesOptions();          
             break;
        case "moreinfodataproducts":
            initMoreInfoData();
            break;
        case "moreinfofinancialproducts":
            initMoreInfoFinancial();
            break;             
        case "passwordchange":
            initPasswordChange();
            break;  
         case "pbotdailymarketstatistics":
             showPBOTSummary();
             break;                                
        }
    }
}

function PageGWLoadCheck() 
{
	/* for Mozilla/Opera9 */
    if(navigator.userAgent.indexOf('Firefox')!=-1) {
        document.addEventListener("DOMContentLoaded", OnPageLoaded, false);
    }
    /* for Internet Explorer */
    else if(navigator.userAgent.indexOf('IE')!=-1) {
      if( typeof (document.readyState)!="undefined" && document.readyState!="complete") {
		if(loadGWTryCount < 2000) {
			loadGWTryCount++;
			setTimeout("PageGWLoadCheck()",500);
        }
        else {
            alert("Unfortunately there is a problem with this page loading.");
        }
     }
     else {
     		OnPageLoaded();
          }
    } else alert("Unknown browser.  This website supports Internet Explorer and Firefox");    
}

//------------------------------------------------------------------------------
//WIDGET FUNCTIONS                                                             |
//------------------------------------------------------------------------------

//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: Mike Lichter
//Creation Date: 10/8/2007
//Widget Name: HaltHistory
    function GetTradeHalts() {
        document.getElementById('divTradeHaltResults').innerHTML = "updating....";
        Server.BL_GenericGrid.GetTableBySP(cb_GetTradeHalts, "nsp_tradehalt_get_halt");
        setTimeout("GetTradeHalts()", 1000*60);
    }

    function cb_GetTradeHalts(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divTradeHaltResults");
        obj.innerHTML = r;
    }

//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: Mike Lichter
//Creation Date: 10/8/2007
//Widget Name: HaltHistory
    function GetTradingHaltsforDay(date){
        document.getElementById('divHaltHistoryResults').innerHTML = "";
        document.getElementById('spanHaltHistoryStatus').innerHTML = "...working, please wait";

        Server.BL_TradeHalt.GetHaltsByDate(cb_GetTradingHaltsforDay, date);
    }
    
    function cb_GetTradingHaltsforDay(r, e){
        document.getElementById('spanHaltHistoryStatus').innerHTML = "";
        if (e) return alert(e.message);
        var obj = document.getElementById("divHaltHistoryResults");
        obj.innerHTML = r;
    
    }
        
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: H Nguyen
//Creation Date: 10/2/2007
//Widget Name: SystemStatus + SystemStatusSearchPage
    function GetCurrSystemStatus() {
        document.getElementById('divCurrSystemStatusResults').innerHTML = "updating....";
        document.getElementById('divCurrTradeCorrectionResults').innerHTML = "updating....";
        document.getElementById('divCurrIPOResults').innerHTML = "updating....";
        document.getElementById('divCurrSelfHelpResults').innerHTML = "updating....";
        Server.BL_SysStatus.GetSysStatusPageData(cb_GetCurrSystemStatus, "C", "System Status");
        Server.BL_SysStatus.GetSysStatusPageData(cb_GetCurrTradeCorrection, "C", "Trade Corrections");
        Server.BL_SysStatus.GetSysStatusPageData(cb_GetCurrIPO, "C", "IPO");
        Server.BL_SysStatus.GetSysStatusPageData(cb_GetCurrSelfHelp, "C", "Self-Help");
        setTimeout("GetCurrSystemStatus()", 1000*60);
    }

    function cb_GetCurrSystemStatus(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divCurrSystemStatusResults");
        obj.innerHTML = r;
    }

    function cb_GetCurrTradeCorrection(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divCurrTradeCorrectionResults");
        obj.innerHTML = r;
    }

    function cb_GetCurrIPO(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divCurrIPOResults");
        obj.innerHTML = r;
    }

    function cb_GetCurrSelfHelp(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divCurrSelfHelpResults");
        obj.innerHTML = r;
    }

    function GetTodaySystemStatus() {
        document.getElementById('divTodaySystemStatusResults').innerHTML = "updating....";
        document.getElementById('divTodayTradeCorrectionResults').innerHTML = "updating....";
        document.getElementById('divTodayIPOResults').innerHTML = "updating....";
        document.getElementById('divTodaySelfHelpResults').innerHTML = "updating....";
        Server.BL_SysStatus.GetSysStatusPageData(cb_GetTodaySystemStatus, "T", "System Status");
        Server.BL_SysStatus.GetSysStatusPageData(cb_GetTodayTradeCorrection, "T", "Trade Corrections");
        Server.BL_SysStatus.GetSysStatusPageData(cb_GetTodayIPO, "T", "IPO");
        Server.BL_SysStatus.GetSysStatusPageData(cb_GetTodaySelfHelp, "T", "Self-Help");
        setTimeout("GetTodaySystemStatus()", 1000*60);
    }

    function cb_GetTodaySystemStatus(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divTodaySystemStatusResults");
        obj.innerHTML = r;
    }

    function cb_GetTodayTradeCorrection(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divTodayTradeCorrectionResults");
        obj.innerHTML = r;
    }

    function cb_GetTodayIPO(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divTodayIPOResults");
        obj.innerHTML = r;
    }

    function cb_GetTodaySelfHelp(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("divTodaySelfHelpResults");
        obj.innerHTML = r;
    }

    function initSystemStatusSearchPage() {
    }
    
    function popSystemStatusSearchPage() {
        var start_date = document.getElementById('start_date').value;
        var end_date = document.getElementById('end_date').value;
	    var ssubject = document.getElementById('subject');
	    for (var i = 0; i < ssubject.options.length; i++) {
            if (ssubject.options[i].selected) {
        	    var subject = ssubject.options[i].value;
            }
        }
        //if (validateUSDate(start_date) && validateUSDate(end_date)) {
            document.getElementById('searchStatus').innerHTML = "...working, please wait";
            Server.BL_SysStatus.GetSysStatusSearchPageData(cb_loadSystemStatusResult, subject, start_date, end_date);
        //}
        //else {
        //    alert ("Enter in a valid Date");
        //}
    }

    function cb_loadSystemStatusResult(r,e) {
        document.getElementById('searchStatus').innerHTML = "";
        if (e) return alert(e.message);
        var obj = document.getElementById("SystemStatusSearchResultDiv");
        obj.innerHTML = r;
    }

//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: bronsonj
//Creation Date: 10/12/2007
//Widget Name: Dividends
    function GetDividends(date){
        document.getElementById('divDividends').innerHTML = "";
        document.getElementById('spanDividendsStatus').innerHTML = "...working, please wait";

        Server.BL_DailyListDividends.GetDividendsForDate(GetDividendsHandler, date);
    }

    function GetDividendsHandler(r, e){
        document.getElementById('spanDividendsStatus').innerHTML = "done";
        if (e) return alert(e.message);
        var obj = document.getElementById("divDividends");
        obj.innerHTML = r;
    
    }
    
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: Roman Shapiro
//Creation Date: 10/15/2007
//Widget Name: News Pages and widgets
    // Current headlines by category
    function SetCurrentHeadlinesOptionsByCategory()
    {
        var cat_id = getQueryVariable("cat_id"); 
        document.getElementById("curHeadlinesDiv").innerHTML = "...working, please wait";  
        Server.BL_NewsListing.GetCurrentHeadlinesPageDataByCategory(cb_loadCurrentHeadlinesData,cat_id);
    }            
    
    //ML - 05/14/2007 (pull 11,12,and 13 for micro
    function SetMicroCurrentHeadlinesOptions() {
        document.getElementById("curHeadlinesDiv").innerHTML = "...working, please wait";  
        Server.BL_NewsListing.GetCurrentHeadlinesPageDataByCategory(cb_loadCurrentHeadlinesData,"11,12,13");
    }
    
    // Current headlines
    function SetCurrentHeadlinesOptions()
    {
        //alert('Inside CurrentHeadlinesOptions');
        document.getElementById("curHeadlinesDiv").innerHTML = "...working, please wait";
        Server.BL_NewsListing.GetCurrentHeadlinesPageData(cb_loadCurrentHeadlinesData);
    }  
              
    function cb_loadCurrentHeadlinesData(r,e)
    {
       if (e) return alert(e.message);

       var obj = document.getElementById("curHeadlinesDiv");
       //alert(r);
       if(obj!=null) obj.innerHTML = r;    
    }

    // Archive pages
    function SetArchiveHeadlinesOptions()
    {
        var cat_id = getQueryVariable("cat_id"); 
        var publish_year = getQueryVariable("publish_year");  
        var mkt_id = getQueryVariable("mkt_id");
        if(isNaN(parseInt(publish_year))) //if year is invalid, use current year
        {
            var d = new Date();
            publish_year = d.getFullYear();
        }
        if(isNaN(parseInt(mkt_id))) //if no market we use all markets
        {
            mkt_id = 0;
        }
        SetArchiveHeadlineOptions(cat_id, publish_year, mkt_id);    
    }
    
    function PublishYearsSelectionChanged()
    {
        var lstElt = document.getElementById("lstPublishYears");    
        var lstMkt = document.getElementById("lstMarkets");
        if(lstElt.selectedIndex > -1) {
            var cat_id = document.getElementById("hidCategoryId").value;
            var publish_year = lstElt.options[lstElt.selectedIndex].value;        
            var mkt_id = 0;
            if(lstMkt.selectedIndex > - 1)            
                mkt_id = lstMkt.options[lstMkt.selectedIndex].value;
             
            SetArchiveHeadlineOptions(cat_id, publish_year, mkt_id); 
        }                  
    }
    
    function MarketsSelectionChanged()
    {
        var lstElt = document.getElementById("lstPublishYears");    
        var lstMkt = document.getElementById("lstMarkets");
        if(lstMkt.selectedIndex > -1) {
            var cat_id = document.getElementById("hidCategoryId").value;
            var publish_year = (new Date()).getFullYear();
            var mkt_id = lstMkt.options[lstMkt.selectedIndex].value;
            if(lstElt.selectedIndex > - 1)
                publish_year = lstElt.options[lstElt.selectedIndex].value;        

            SetArchiveHeadlineOptions(cat_id, publish_year, mkt_id); 
        }                  
    }

    function SetArchiveHeadlineOptions(cat_id, publish_year, mkt_id)
    {
        document.getElementById("archHeadlinesDiv").innerHTML = "...working, please wait";
        Server.BL_NewsListing.GetArchiveHeadlinesPageData(cb_loadArchiveHeadlinesData, cat_id, publish_year, mkt_id);    
    }            
    function cb_loadArchiveHeadlinesData(r,e)
    {
       if (e) return alert(e.message);

       var obj = document.getElementById("archHeadlinesDiv");
       //alert(r);
       if(obj!=null) obj.innerHTML = r;       
    }
    
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: Mike Lichter
//Creation Date: 10/19/2007
//Widget Name: Exchange Traded Funds (ETF)
    function GetETFbySymbol(symbol, month, year) {
        showETFdescription(symbol);
        document.getElementById("divETFresults").innerHTML = "...working, please wait";
        Server.BL_ETF.CreateETFBySymbolTable(cb_GetETFbySymbol, symbol, month, year);
    }    
    
    function cb_GetETFbySymbol(r,e) {
        if (e) { 
            document.getElementById("divETFresults").innerHTML = "";
            alert(e.message);
            return;
        }
        var obj = document.getElementById("divETFresults");
        obj.innerHTML = r;
    }
    
    function GetETFLanding() {
        hideETFdescriptions();
        document.getElementById("divETFresults").innerHTML = "...working, please wait";
        Server.BL_ETF.CreateETFLandingTable(cb_GetETFLanding);    
    }
    
    function cb_GetETFLanding(r,e) {
        if (e) { 
            document.getElementById("divETFresults").innerHTML = "";
            alert(e.message);
            return;
        }
        var obj = document.getElementById("divETFresults");
        obj.innerHTML = r;    
    }
    
    function hideETFdescriptions() {
        for(i=0; i < descriptionDivs.length; i++) {
            document.getElementById(descriptionDivs[i]).style.display = "none";
        }    
    }
    
    function showETFdescription(symbol) {
        hideETFdescriptions();
        document.getElementById("div" + symbol.toUpperCase()).style.display = "block";
    }   
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: Mike Lichter
//Creation Date: 10/25/2007
//Widget Name: NasdaqIndexes

    
    function getIndexes() {
        document.getElementById('IXresults').innerHTML = "...working, please wait";
        Server.BL_NasdaqIndexes.GetIndexes(cb_Index);
    }

    function getIndexByIX(IX) {
        document.getElementById('IXresults').innerHTML = "...working, please wait";
        Server.BL_NasdaqIndexes.GetIndexByIX(cb_Index, IX);
    }

    function cb_Index(r, e){
        if (e) return alert(e.message);
        var obj = document.getElementById("IXresults");
        obj.innerHTML = r;
    }    
    
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: Mike Lichter
//Creation Date: 11/6/2007
//Page Name: Misappropriation
    function sendMisEmail() {
        var vendor = document.getElementById('txtVenderName').value;
        var desc = document.getElementById('txtDesc').value;
        Server.BL_Misappropriation.SendEmail(cb_sendMisEmail, vendor, desc);
    }
    
    function cb_sendMisEmail(r,e) {
        if (e) return alert(e.message);
        document.getElementById('misappropriationForm').style.display="none";
        document.getElementById('results').style.display="block";
    }
 

//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: Mike Lichter
//Creation Date: 11/19/2007
//Page Name: MonthlyMarketActivity
function getActivity() {
    document.getElementById("results").innerHTML = "";
    if(validateMonth()) {
          var activityDate = document.getElementById('txtMonth').value;
		  document.getElementById("results").innerHTML = "...working, please wait";
		  Server.BL_MonthlyMarketActivity.GetMarketActivity(cb_GetMarketActivity, activityDate);
	}
}

function cb_GetMarketActivity(r,e)
{
	if (e) return alert(e.message);
	var obj = document.getElementById("results");
	obj.innerHTML = r;
}

function validateMonth() {
	var activityDate = document.getElementById('txtMonth').value;
	var error = document.getElementById('spanError');
	var month;
	var year;
	var d = new Date();
	var curr_month = d.getMonth() + 1;
	var curr_year = d.getFullYear();
	
	if (activityDate.indexOf('/') > 0) 
	{
		error.innerHTML = "";
				
		//make sure month and year are numeric
		if (IsNumber(activityDate.substring(0,activityDate.indexOf('/'))))
		    month = parseInt(parseVal(activityDate.substring(0,activityDate.indexOf('/'))));
		else 
		{
		    error.innerHTML = "Month must be 1 - 12";
		    return false;
		}
		
		if (IsNumber(activityDate.substring(activityDate.indexOf('/') + 1, activityDate.length)))
    		year = parseInt(parseVal(activityDate.substring(activityDate.indexOf('/') + 1, activityDate.length)));
		else 
		{
			error.innerHTML = "Year must be between 1997 and " + curr_year;
			return false;
		}
		    
		//validate month
		if (month < 1 || month > 12)
		{
			error.innerHTML = "Month must be 1 - 12";
			return false;
		}
		
		//validate year
			if (year < 1997 || year > curr_year)
		{
			error.innerHTML = "Year must be between 1997 and " + curr_year;
			return false;
		}
		
		//make sure date is < today
		if(year >= curr_year && month >= curr_month)
		{
			error.innerHTML = "Date must be earlier then current month/year";
			return false;
		}
		
		return true;
		
	}
	else
	{
		error.innerHTML = "Please enter date in MM/YYYY format";
		return false;
	}
	
}
    
//------------------------------------------------------------------------------
//PAGE FUNCTIONS                                                             |
//------------------------------------------------------------------------------


//Page Name: PasswordChange
    function initPasswordChange()
    {
        var reason = getQueryVariable("reason");
        var userid = getQueryVariable("userid");
        if(reason!=false)
        {   
            if(reason=="1")
                document.getElementById("msgCause").innerHTML = "Your password will expire soon, please take a moment to change it now";
            else if(reason=="-1")
                document.getElementById("msgCause").innerHTML = "Your password has expired, please change it now.";                
        }
        if(userid!=false) {
            document.getElementById("UserID").value = userid;
            if(userid!="") Server.BL_PasswordChange.GetPasswordHint(cb_getPasswordHint,userid);
        }
    }
    function cb_getPasswordHint(r,e)
    {
       if (e) {           
            return alert(e.message);        
        }
        document.getElementById('Hint').value = r;
    }
    function OnPasswordChangeReset()
    {
        document.getElementById("UserID").value = "";
        document.getElementById("OldPassword").value = "";
        document.getElementById("NewPassword").value = "";
        document.getElementById("NewPassword2").value = "";
        document.getElementById("Hint").value = "";        
    }
    function OnPasswordChangeSubmit()
    {               
        var newpwd = document.getElementById('NewPassword');
        var newpwd2 = document.getElementById('NewPassword2');
        var pwdhint = document.getElementById('Hint');
	    if(newpwd.value != newpwd.value) {
		    alert("The values you have entered for new password do not match, please enter the same value");
		    return;
	    }
	    if(newpwd.value.length < 8) {
		    alert("Your new password has to be at least 8 characters long");
		    return;
	    }
	    if(pwdhint.value == newpwd.value) {
		    alert("The Password Hint cannot be identical to the Password.");
		    return;
	    }	 
        document.getElementById('msgProgress').innerHTML = "...working, please wait";
        Server.BL_PasswordChange.ChangePassword(cb_changePassword,
            document.getElementById("UserID").value,
            document.getElementById("OldPassword").value,
            newpwd.value,
            pwdhint.value);                                    
    }
    function cb_changePassword(r,e)
    {
        document.getElementById('msgProgress').innerHTML = "";
        if (e) {           
            return alert(e.message);        
        }
        document.getElementById('msgProgress').innerHTML = "<a href='javascript:history.back(1)'>Select this link to log back into the previous page</a>";
        return alert(r);

    }
    
    function openEmailWin() {
        var subject = escape(document.title);
        var link = escape(window.location);
        var url = window.location.href;
        if (url.indexOf('www') > -1)
            url = "https://www.NasdaqTrader.com/EASP/TraderBEASP.aspx?id=EmailFriend";
        else if (url.indexOf('cms') > -1)
            url = "https://cms.NasdaqTrader.com/EASP/TraderBEASP.aspx?id=EmailFriend";
        else if (url.indexOf('beta-dev') > -1)
            url = "https://beta-dev.NasdaqTrader.com/EASP/TraderBEASP.aspx?id=EmailFriend";
        else if (url.indexOf('localhost') > -1)
            url = "https://beta-dev.NasdaqTrader.com/EASP/TraderBEASP.aspx?id=EmailFriend";

        else
            url = "https://www.NasdaqTrader.com/EASP/TraderBEASP.aspx?id=EmailFriend";
            
        window.open(url + '&link=' + link + '&subject=' + subject,'','width=355,height=585,toolbar=0,scrollbars=0,menubar=0,resizable=0','false');
    }



//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Author: Steven Leathem
//Creation Date: 11/3/2008
//Page Name: PBOTDailyMarketStatistics

    function showPBOTSummary(reportDate) {
        Server.BL_PBOTDailyMarketStatistics.ShowSummary(cb_showPBOTSummary,reportDate);
    }
    
    function cb_showPBOTSummary(r,e) {
        if (e) return alert(e.message);

        var obj = document.getElementById("ShowPBOTSummary");
        obj.innerHTML = r;
        
        //alert(r);
    }
    
    
