
var OAS_version = 11;
if ((navigator.userAgent.indexOf('Mozilla/3') != -1) || (navigator.userAgent.indexOf('Mozilla/4.0 WebTV') != -1))
    OAS_version = 10;
    
function srchTerm(sSearchType)
{
    if (sSearchType == "search") {
	    document.searchForm.SrchTerm.value = document.searchForm.sSrchTerm.value;
    document.searchForm.action = "http://search.morningstar.com/MstarSrch/MstarSrch2.aspx";
    document.searchForm.submit();
    } else {
	    if (document.quoteForm.sSrchTerm.value == "") {
		    document.quoteForm.action = "http://quote.morningstar.com/TickerNotFound.html";			
		    document.quoteForm.submit();
	    }
	    else {
		    document.quoteForm.action = "http://quote.morningstar.com/switch.html?ticker="+document.quoteForm.sSrchTerm.value;
		    document.quoteForm.submit();
	    }
  }
}

function ClearSearchBox(sSearchType)
{
    if (sSearchType == "quote")
    {
	    var text = document.quoteForm.sSrchTerm.value;
	    if (text == "Quotes")
		    document.quoteForm.sSrchTerm.value = "";
    }
    else
    {
	    var text = document.searchForm.sSrchTerm.value;
	    if (text == "Search")
	        document.searchForm.sSrchTerm.color="#FFFFFF";
		document.searchForm.sSrchTerm.value = "";
    }
}

function OAS_NORMAL(pos) { 
    document.write('<A HREF="' + linkAdUrl + '" TARGET=_top>');
    document.write('<IMG SRC="' + imageAdUrl + '" BORDER=0></A>');
}

function OAS_AD(pos) {
    if (OAS_version >= 11)
      OAS_RICH(pos);
    else
      OAS_NORMAL(pos);
}

function footeropenwin(windowName,width,height) {
  newWindow = window.open(windowName, 'newWindow','toolbar=no,location=no,menubar=no,width='+width+',height='+height+',scrollbars=yes,resizable=no');
}

function openPlayerSelection(windowURL) {  
	playerSelectWin = window.open(windowURL,'playerSelectWin','toolbar=no,titlebar=no,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=312,height=480,top=20,left=10');   
}

function openEmailwin(windowName) {
	if (browser_type=="Microsoft Internet Explorer") {
		newWindow = window.open(windowName, 'myWindow','toolbar=no,location=no,menubar=no,width=312,height=320,scrollbars=no,resizable=no');
	} else {
		newWindow = window.open(windowName, 'myWindow','toolbar=no,location=no,menubar=no,width=312,height=355,scrollbars=no,resizable=no');
	}
}

function AdManager()
{
    this.loadTimer = null;
    this.showTimer = null;
    
    //auto fit IFrame size based on the frame content 
    this.resize =function(frameId,x,y)
    {
        var frame1 = document.getElementById(frameId);
        frame1.width = x+0;
        frame1.height = y+0;
        frame1.style.visibility = "visible";
        
        if(frameId == "RightFrame" && x>200)
        {
            var pmwrapper = document.getElementById("pmwrapper");
            if(pmwrapper == null) return;
            document.getElementById("pmbody").style.width = pmwrapper.clientWidth + x + "px";
        }
        if(frameId == "BottomRightFrame" && y>240)
        {
            var adContainer = frame1.parentNode;            
            if(adContainer!=null) adContainer.style.height = y + 36 + "px";
        }
    }
    
    //show advertisements in IFrames
    this.loadAdvertisement = function()
    {
        if(OAS_RICH.toString() == "function(){}") return;
        if(this.loadTimer && this.showTimer == null) return;
        if(this.showTimer)
        {
            window.clearInterval(this.showTimer);
            this.showTimer = null;
        }
        
        var ads = OAS_listpos.split(',');     	
        for (var i = 0; i < ads.length; i++) {
            var frame = getFrame(ads[i]);
            if(frame == "") continue;
            frameName = frame + 'Frame';
            if(window.frames[frameName] == null) continue;
            document.getElementById(frameName).setAttribute("advertise",ads[i]);
            //window.frames[frameName]["advertise"] = ads[i];
            window.frames[frameName].document.location.href = "/RtPort/include/Advertise.htm";
        }
    }
    
    function getFrame(pAd)
    {
        var frames=[['TopLeft','Top'],['Top','Top'],['Top1','Top1'],['Top3','Top3'],['x43','x43'],['x30','x43'],['Bottom','Bottom'],['Right','Right'],['x23','x23'],['Top3','Top3'],['x24','x24']];
        for(var j=0;j<frames.length;j++)
        {
            if(frames[j][0] == pAd) return frames[j][1];
        }
        return "";
    }

    //Dynamically load script file.
    function loadScript(pSrc, pCallback, pId, pCharset)
    {
        var head = document.getElementsByTagName("head")[0];
        var script = document.createElement('script');
        script.setAttribute('language', 'javascript');
        if(pId){
            var oldScript = document.getElementById(pId);
            try{
            delete oldScript;
            if(oldScript) oldScript.parentNode.removeChild(oldScript);
            }catch(e){}
            script.setAttribute('id', pId);
        }
        if(pCharset){
            script.setAttribute('charset', pCharset);
        }   
        script.setAttribute('src', pSrc);
        head.appendChild(script);
        script.onreadystatechange = function() {
            if(this.readyState == 4 || this.readyState == 'complete' || this.readyState == 'loaded') {
                pCallback();
            }
        };
        script.onload = pCallback;
    }

    //Load advertisements every 5 minutes.
    this.loadAdScript = function(pScriptUrl,pInterval)
    {
        if(!pInterval) pInterval = 300;
        this.loadAdvertisement();
        var self = this;
        var func = function()
        {
            OAS_RICH = function(){};
            loadScript(pScriptUrl,self.loadAdvertisement,"portfolioAds");
            this.showTimer = window.setInterval(self.loadAdvertisement,2*1000);
        };
        this.loadTimer = window.setInterval(func,pInterval*1000);
    }
}