function validateSymbol(symbol) {

    if (/<[A-Za-z]/.test(document.getElementById(symbol).value))
    {
        alert('You have entered an invalid symbol.  Please try again.');
        return false;
    }
    return true;
}
function SubmitRagingBullComment(clientID, prediction, board, predictorID, targetDate, originID)
{
   var comment = document.getElementById(clientID).value;
   if(comment==''||comment.indexOf('your opinions and knowledge on')>-1)
   {
	    alert('Please enter a comment.');
	    return false;
   }
   else
   {	
        //PredictWallStreet.Web.Ajax.RagingBullCommentSave(predictorID, board, targetDate, directionForecast, comment, originID, ragingBullCommentCallback);
        PredictWallStreet.Web.Ajax.RagingBullCommentSave(predictorID, board, comment, prediction, originID, targetDate);
        document.getElementById('ragingBullBoard').value = board;
        document.getElementById('ragingBullPrediction').value = prediction;
        document.getElementById('ragingBullMessage').value = comment;
        document.ragingbullcomment.submit();
        return false;
   }
}

function ragingBullCommentCallback(res)
	{
	    var xmlDoc = res.value
		if(res!=null&&res.value!="")
		{	
			if(xmlDoc.getElementsByTagName("error")[0].firstChild!=null)
			{
			    alert(xmlDoc.getElementsByTagName("error")[0].firstChild.data);
			}
		}    
	}
	

function changeOpenersParent(url, nameValuePairs)
{
    changeParent(getMyOpener(), url, nameValuePairs);
}

// Changes the windows parent to the passed in url and name value pairs.
// If no window is passed in , window is set to current window.
// If no URL is passed in, the url is set to the parent's current url
// Name value pairs must be prefixed with '?'.
// If window has no parent function degrades gracefully by doing nothing.
function changeParent(myWindow, url, nameValuePairs)
{
    if (myWindow == null){
        myWindow = window;
    }
    if (myWindow.parent != null){
        if (url == null){
            url = myWindow.parent.location.pathname;
        }
        myWindow.parent.location.href = url + nameValuePairs;
    }
}

function show_sentiment(linkId, symbol,timeFrame)
{
    var sentiment_image = document.getElementById("sentiment_trend");
     var i, link, oldUrl, newUrl;
      for (i = 1; link = document.getElementById("sentiment_link_" + i); i++) {
        link.style.fontWeight = "normal";
      }
      link = document.getElementById("sentiment_link_" + linkId); 
      link.style.fontWeight = "bold";
      
      oldUrl = sentiment_image.getAttribute('src');
      newUrl = changeParam(oldUrl, 'p', timeFrame);
      sentiment_image.setAttribute('src', newUrl);
}

var isDefaultComment=true;
function clearDefaultComment(comment_box)
{
    if (comment_box && isDefaultComment)
    {
	    comment_box.value = "";
	    isDefaultComment = false;
    }
}
function validateComment(controlID)
{        
    if(document.getElementById(controlID).value == 'undefined')
    {
        alert('Please enter a comment.');
        return false;
    }
    var comment = document.getElementById(controlID).value;
    //var comment = document.getElementById('ucPredictionWidget_ctl01_tbComment').value;
    if(comment==''||comment.indexOf('your opinions and knowledge on')>-1)
    {
	    alert('Please enter a comment.');
	    return false;
	}
}
function getMyOpener()
{
    var foundIt="false";
    if(window.opener!=null) 
    { 
        var x_win = window.self;
        while((x_win!=null) && (foundIt=="false")) 
        {  
            if (x_win.opener==null)
            {
                foundIt="true";
            }
            else
            {
                x_win = x_win.opener;
            }
        }
        return x_win; 
    } 
    else 
    { 
        return window; 
    }
}

function simplifyDomain()
{
	var baseDomain = document.domain;
	var domainParts = baseDomain.split('.');
	var simpleDomain = baseDomain;
	if (domainParts.length >= 2)
	{
		simpleDomain = domainParts[domainParts.length-2] + '.' + domainParts[domainParts.length-1];
	}
	return simpleDomain;
}

function changeParam(url, param, newValue)
{
    var expression;
    var newUrl = url;
    
    if (newValue == "")
    {
        //remove param
        expression = eval('/([?&])' + param + '=[^&]*&?/i');
        newUrl = url.replace(expression, '$1');
    }
    else
    {
        //change or append param
        expression = eval('/([?&]' + param + '=)[^&]*/i');
        
        if ( expression.test(url) ) {
        
            // replace symbol param
            newUrl = url.replace(expression, '$1' + newValue);
            
        } else {
        
            // add symbol param
            var appendCharacter = '?';
            if (url.indexOf('?') >= 0) {
                appendCharacter = '&';
            }
            newUrl = url + appendCharacter + param + '=' + newValue;
        }
    }
    
    return newUrl;
}

function updateUrl(oldUrl, newSymbol)
{
    var navigateUrl = oldUrl;
    
    // remove pd param
    navigateUrl = changeParam(navigateUrl, 'pd', '');
    
    // remove t param
    navigateUrl = changeParam(navigateUrl, 't', '');

    // remove prediction param
    navigateUrl = changeParam(navigateUrl, 'prediction', '');
    
    // set pv param
    navigateUrl = changeParam(navigateUrl, 'pv', '1');
    
    // update symbol param
    navigateUrl = changeParam(navigateUrl, 'symbol', escape(newSymbol));
    
    return navigateUrl;
}

function queryString(searchKey)
{
    var query = window.location.search.substring(1);
    var parms = query.split('&');

    for (var i=0; i<parms.length; i++) 
    {
        var pos = parms[i].indexOf('=');
        if (pos > 0) 
        {
            var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);
            if (key == searchKey)
            {
                return val;
            }
        }
    }
    return "";
}

 

function doGatewayNavigation(gatewayID)
{  
    if (!validateSymbol(gatewayID))
    {
        return false;
    }
    
    var newSymbol = document.getElementById(gatewayID).value.toUpperCase();
    if (window.parent != null) 
    {
        var hfPid = document.getElementById('ucPredictionWidget_hfPID');
        var domain = queryString('domain');
     
        if (hfPid != null && hfPid.value == "10" && domain != "")
        {
            // special redirect URL
            location.href = 'http://' + domain + '/quotes/IframeRefresh.aspx?selectedTab=Quotes&symbol=' + escape(newSymbol);
            return false;
        } 
        else 
        {
            try
            {
                window.parent.location = updateUrl(window.parent.location.href, newSymbol);
                return false;
            }
            catch (Err)
            {
                // could not update the parent window. 
                // Could be a permissions issue (cross-domain)
                // Let this fall through, since we'll take the appropriate action later.
            }
        
        }
    
    }
    
    // if we haven't returned by now, just refresh ourselves
    location.href = updateUrl(location.href, newSymbol);
    
    return false; 

}
    
function doPopUpGatewayNavigation(gatewayID)
{
    if (!validateSymbol(gatewayID))
    {
        return false;
    }
    
    var newSymbol = document.getElementById(gatewayID).value.toUpperCase();
    
    //alert(updateUrl(location.href, newSymbol));
    location.href = updateUrl(location.href, newSymbol);
    
    return false;
} 

// Task 2128:  remove to make labels persistent, this function is no longer used.    
//function navMouseEvent(navIconId, navIconNewImage, navLabelDisplay)
//{
//    changeSource(navIconId, navIconNewImage);
//    changeDisplay(navIconId + "_label", navLabelDisplay);
//    //show/hide triangle
//    if (navLabelDisplay == "inline")
//    {
//        changeDisplay("activeTabMarker", "none");
//    }
//    else if (navLabelDisplay == "none")
//    {
//        changeDisplay("activeTabMarker", "block");
//    }
//}

function changeSource(anObjectID, newHTML) {document.getElementById(anObjectID).setAttribute('src', newHTML);}
function changeChildSource(anObjectID, newHTML){document.getElementById(anObjectID).firstChild.setAttribute('src', newHTML);}
function changeInnerHTML(anObjectID, newHTML){document.getElementById(anObjectID).innerHTML = newHTML;}
function changeDisplay(anObjectID, strDisplay){document.getElementById(anObjectID).style.display = strDisplay;}
// function to show alert box
function showAlert(anObjectID, imgID, newSrcImg)
{   
    //changeBackground('divAlertType',alertType);
    //changeInnerHTML(contentID, message);
    changeSource(imgID, newSrcImg);
    changeDisplay(anObjectID, 'inline');
}
// function to show definition box
function showDef(anObjectID, textDivID, defText)
{   
    changeInnerHTML(textDivID, defText);
    fade(anObjectID, true);
}
function changeBackground(anObjectID, background){document.getElementById(anObjectID).style.backgroundImage="url(" + background + ")";}
// Fade a div element (using scriptaculous). Set boolAppear true to make an element appear, false to make it disappear.
function fade(divId, boolAppear)
{
    var start = 0.0;
    var end = 1.0;
    var duration = 0.5;
    if (!boolAppear)
    {
        start = 1.0;
        end = 0.0;
    }
    Effect.Appear(divId, { duration: duration, from: start, to: end });
}

//document.cookie = 'PredictWallStreet'
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Use this in cross domain settings.
// Both the parent and the child must perform this action.
//document.domain = simplifyDomain();

createCookie("CookieCheck",":)",1);

if (readCookie("CookieCheck") == null) 
{
   window.location="/UC/Cookies.aspx";
}     

// navInitialize
// Pass in a list of one or more nav IDs (the IDs of the nav icon images) -- order does not matter.
// Example: navInitialize('hlTabs1024','hlTabs128','hlTabs16');
function navInitialize()
{    
    for (var i = 0; i < arguments.length; i++) 
    {
        var strNavId = arguments[i];
        var strNavLabel = strNavId + "_label";
        positionNavLabel(strNavId, strNavLabel);       
    } 
    
}

function positionActiveTabMarker(strActiveNavId, strMarkerId)
{
    positionNavLabel(strActiveNavId, strMarkerId);
}

function positionNavLabel(strNavId, strNavLabel)
{
    var intNavIconHeight = 27;
    var intNavIconWidth = 25;               
    var labelOffsetLeft = Math.round( (intNavIconWidth - Element.getWidth(strNavLabel)) / 2.0);
    Position.clone(strNavId, strNavLabel, {offsetTop: intNavIconHeight, offsetLeft: labelOffsetLeft, setWidth: false, setHeight: false });
}