String.prototype.trim = function()
{
    var x = this.replace(/(^\s*)|(\s*$)/g,"");
    return x.replace(/^\[ \t]*$/g,"");
}

function getNodeFormat(nodetype)
{
    return nodetype & this.FORMAT_MASK;
}

function getNodeSource(nodetype)
{
    return nodetype & this.SOURCE_MASK;
}

function NodeType()
{
    this.FORMAT_MASK = 0x0f;
    this.PDF_NODE = 0x01;
    this.HTML_NODE = 0x02;
    this.WORD_NODE = 0x03;
    this.OTHER_FORMAT_NODE = 0x04;
    this.FOLDER_NODE = 0x05;
    
    this.SOURCE_MASK = 0xf0;
    this.DOCUMENT_NODE =  0x00;
    this.INTERNAL_NODE =  0x10;
    this.EXTERNAL_NODE =  0x20;
    this.ROCHE_NODE =  0x30;
    this.OTHER_SOURCE_NODE = 0x40;
    this.CLINICAL_NODE =  0x50;
    this.PROMOTIONAL_NODE =  0x60;
    
    this.DOCUMENT_PDF_NODE = this.DOCUMENT_NODE | this.PDF_NODE;
    this.DOCUMENT_WORD_NODE = this.DOCUMENT_NODE | this.WORD_NODE;
    this.DOCUMENT_HTML_NODE = this.DOCUMENT_NODE | this.HTML_NODE;
    this.LINK_INTERNAL_PDF_NODE = this.INTERNAL_NODE | this.PDF_NODE;
    this.LINK_INTERNAL_WORD_NODE = this.INTERNAL_NODE | this.WORD_NODE;
    this.LINK_INTERNAL_HTML_NODE = this.INTERNAL_NODE | this.HTML_NODE;
    this.LINK_INTERNAL_FOLDER_NODE = this.INTERNAL_NODE | this.FOLDER_NODE;
    this.LINK_EXTERNAL_PDF_NODE = this.EXTERNAL_NODE | this.PDF_NODE;
    this.LINK_EXTERNAL_WORD_NODE = this.EXTERNAL_NODE | this.WORD_NODE;
    this.LINK_EXTERNAL_HTML_NODE = this.EXTERNAL_NODE | this.HTML_NODE;
    this.LINK_ROCHE_PDF_NODE = this.ROCHE_NODE | this.PDF_NODE;
    this.LINK_ROCHE_WORD_NODE = this.ROCHE_NODE | this.WORD_NODE;
    this.LINK_EROCHE_HTML_NODE = this.ROCHE_NODE | this.HTML_NODE;
    this.UNKOWN_TYPE_NODE = this.OTHER_SOURCE_NODE | this.OTHER_FORMAT_NODE;
    
    this.getNodeFormat = getNodeFormat;
    this.getNodeSource = getNodeSource;
}

var _nodeType = new NodeType();

function view(url,windowName,type,feature) 
{
    var source = _nodeType.getNodeSource(type);
    var format = _nodeType.getNodeFormat(type);

    if(format == _nodeType.PDF_NODE)
    {
        if(source == _nodeType.DOCUMENT_NODE || source == _nodeType.INTERNAL_NODE)
        {
            openPDFFile(url,true,'_blank');
        }
        else if(source == _nodeType.EXTERNAL_NODE || source == _nodeType.CLINICAL_NODE || source == _nodeType.PROMOTIONAL_NODE)// the url is leave site url
        {
            openPDFFile(url,false,'_self');// it should open in portal main window
        }
        else
        {
            openPDFFile(url,false,'_blank');
        }
    }
    else if(format == _nodeType.WORD_NODE)
    {
        if(source == _nodeType.DOCUMENT_NODE || source == _nodeType.INTERNAL_NODE)
        {
            openWordFile(url,true,'_blank');
        }
        else if(source == _nodeType.EXTERNAL_NODE || source == _nodeType.CLINICAL_NODE || source == _nodeType.PROMOTIONAL_NODE)
        {
            openWordFile(url,false,'_self');
        }
        else
        {
            openWordFile(url,false,'_blank');
        }
    }
    else if(source == _nodeType.EXTERNAL_NODE || source == _nodeType.CLINICAL_NODE || source == _nodeType.PROMOTIONAL_NODE)
    {
        location = encodeExternalUrl(url);
    }
    else
    {
        var normalized = url.toLowerCase();
        if(normalized.indexOf('http://') != 0 && normalized.indexOf('https://') != 0)
        {
            url += 'http://';
        }
        openWindow(url,'_blank',600,450,1,1);
    }
}

function encodeExternalUrl(url)
{
    if(url)
    {
        var idx = url.indexOf('externalUrl=');
        if(idx != -1)
        {
            var offset = 'externalUrl='.length;
            var exurl = url.substring(idx+offset);
            url = url.substring(0,idx+offset) + encodeURIComponent(exurl);
        }
    }
    return url;
}

    function openWordFile(nodePath, isCMSNode, targetWindow)
    {        
            var winFeature = "height=600,width=800,status=no,toolbar=no,menubar=no,location=no,resizable=yes";
            var url;
            if(isCMSNode)
            {
                url = '/rocheRimPortal/portlets/common/DisplayPDFContent.jsp?isCMSNode='+isCMSNode+'&pdfNodePath='+nodePath;
            }
            else if(nodePath)
            {
                var normalized = nodePath.toLowerCase();
                if(normalized.indexOf('http://') != 0 && normalized.indexOf('https://') != 0)
                {
                    url = "http://" + nodePath;
                }
                else
                {
                    url = nodePath;
                }
            }
            //window.open('/rocheRimPortal/portlets/common/DisplayPDFContent.jsp?isCMSNode='+isCMSNode+'&pdfNodePath='+nodePath,'_blank',winFeature);    
            openWindowNoChrome(url,targetWindow,600,450,1,1);
    }

// search
//
function trapSearchEnter(event)
{
    var e = event ? event : window.event;

    if (e.keyCode == 13)       // Enter key pressed
    {
        e.cancelBubble = true;
        e.returnValue = false; 
        submitSearchForm();
    }
}

function submitSearchForm()
{
    if(validateSearchForm())
    {
        document.getElementById("searchForm").submit();
    }    
}

function validateSearchForm()
{
    var term = document.getElementById("searchTerm");
    term.value = term.value.trim();
    if(term.value == "")
    {
        return false;
    }
    else
    {
        return true;
    }
}

// end search

// for interstitial page

function getRequestParameter(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null)
    {
        return unescape(r[2]);
    }
    else
    { 
        return null;
    }
}

function leave()
{
    history.go(-1);
    
    var pdfPattern = /.+\.pdf/
    var externalUrl = getRequestParameter('externalUrl');
    if(externalUrl)
    {
        var normalized = externalUrl.toLowerCase();
        if(normalized.indexOf('http://') != 0 && normalized.indexOf('https://') != 0)
        {
            externalUrl = "http://" + externalUrl;
        }
        if(externalUrl.match(pdfPattern))
        {
            openPDFFile(externalUrl,false, '_blank');
        }
        else
        {
            openWindow(externalUrl, '_blank',600,450,1,1);
        }
    }
}

// end for interstitial page


function getCookieValue( name ) 
{
    var c = document.cookie; 
    var begin = c.indexOf( name ); 
    if( begin < 0 ) return null; 
    begin += name.length + 1; 
    var end = c.indexOf( ";", begin ); 
    if( end == -1 ) end = c.length; 
    return( c.slice( begin, end ) );
}

function addLatestUpdatesLink(brand, sessionId)
{

    //var updatesDiv = document.getElementById('updates');  changed by rockett 070427
    var updatesDiv = document.getElementById('callout_text');
    
    if(updatesDiv)
    {        
        var link = document.createElement("p");
        updatesDiv.appendChild(link);
        
        var jsessionid = getCookieValue("JSESSIONID");

        if(jsessionid)
        {
            link.innerHTML = '<a href="/'+brand+'/importantupdates" class="more">READ ALL UPDATES</a>';	
        }
        else
        {
            link.innerHTML = '<a href="/'+brand+'/importantupdates;jsessionid=' + sessionId + '" class="more">READ ALL UPDATES</a>';	
        }
    }
}