﻿
//Javascript for Global Navigation
//Function to set the cookie value
function fnNMGNSetCookie (cookiename, cookievalue)
{
    document.cookie = cookiename + "=" + escape(String(cookievalue)) + '; path=/';
}

//Function to get the cookie value
function fnNMGNReadCookie(cookiename) 
{
    var cookiecontent = new String();
    if(document.cookie.length > 0) {
	    cookiename += '=';
	    var cookiebegin = document.cookie.indexOf(cookiename);
	    var cookieend = 0;
	    if(cookiebegin > -1) {
		    cookiebegin += cookiename.length;
		    cookieend = document.cookie.indexOf(";",cookiebegin);
		    if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
		    cookiecontent = document.cookie.substring(cookiebegin,cookieend);
		    }
	    }
    return unescape(cookiecontent);
}

 var btnFirst;
 var pnlsecondaryNavFirst ;
 var halfCount;
 var arrTags;
 var btnId ;
 var panelId ;
 
 function fnNMGNDrawerPosition()
 { 
 	 
 	 var bIntializePanelPosition = true;
	 if ( pnlsecondaryNavFirst.style.left != null)
	 {
 		var bounds = Sys.UI.DomElement.getBounds(btnFirst);
 		if ((bounds.x + "px") == pnlsecondaryNavFirst.style.left)
 		{ bIntializePanelPosition  = false; } 		
 	 }
 	 if (bIntializePanelPosition  == true)
 	 {	 
 	     //Set the panel position to be just below the top nav
	    var  iCount=0;    
	    var bounds = Sys.UI.DomElement.getBounds(btnFirst);
		var initialleft = bounds.x;
	    var left = 0;
	 
	    var btnLast = document.getElementById(btnId + arrTags[(arrTags.length -1)] );
	    bounds = Sys.UI.DomElement.getBounds(btnLast);
	    var right =  bounds.x + btnLast.offsetWidth - initialleft ;
	
	    for (iCount=0; iCount < arrTags.length; ++iCount)
	    {    
	       var pnlSecondaryNav = document.getElementById(panelId + arrTags[iCount] );
	       var btnMainMenu = document.getElementById(btnId + arrTags[iCount] );
	       var bounds = Sys.UI.DomElement.getBounds(btnMainMenu);
	  
	       //Set the position of the panel
	       if ( pnlSecondaryNav.style.top == "" )
	       {
		       pnlSecondaryNav.style.position = "absolute";
		       var pnlHeight = 0 + btnMainMenu.offsetHeight + 2 ; 
		       pnlSecondaryNav.style.top = pnlHeight + "px" ;
	       }    
	       if (iCount < halfCount)
	       {       
	            pnlSecondaryNav.style.left = left + "px" ;                  
	         
	       }
	       else
	       {
	           pnlSecondaryNav.style.right = right + "px" ;
	           pnlSecondaryNav.style.left = "";
	           
	       }
	      	   
	       //Set the position of the div for the tabs
	       var popupMenu = panelId + arrTags[iCount];
	       popupMenu = popupMenu.replace ("_pnl" + arrTags[iCount], "_ucPopupMenu" + arrTags[iCount]);
		   popupMenu += "_dvPopupMenuInner";      
	       var divPopupMenu = document.getElementById(popupMenu );
	       if ( divPopupMenu != null)
	       {
	            var divPopupMenuPaddingLeft = bounds.x - initialleft ;
	            divPopupMenu.style.paddingLeft = divPopupMenuPaddingLeft + "px";
	       }
	    }  
	 }
 }
 
//Function called on load to properly position the global navigation controls
function fnNMGNOnLoad (tagValue, btnInitial, panelInitial,  tab)
{   

    //Set the panel position to be just below the top nav
    var hdTagValue = document.getElementById(tagValue);
    var sTagValue = hdTagValue.value;
    arrTags = sTagValue.split(":");
    halfCount = arrTags.length/2 ;
    btnId = btnInitial;
    panelId = panelInitial;

    btnFirst = document.getElementById(btnInitial + arrTags[0] );
    pnlsecondaryNavFirst = document.getElementById(panelInitial + arrTags[0] );
	
	//Position the secondary navigation drawers properly
    fnNMGNDrawerPosition();
	
	//Get the proper tab selected
    var hdTab =  document.getElementById(tab);

    if (hdTab.value == "")
    {
        hdTab.value = "0";
        try 
        {
            var tabChangedCookieName = "NMFNGlobalNavigationTabSelected";
            var value = fnNMGNReadCookie ( tabChangedCookieName);
            if ( value == "")
            {
                hdTab.value = "0";
                fnNMGNSetCookie(tabChangedCookieName, "0");
            }
            else
            {              
                hdTab.value = value;
            }
        }
        catch(e) { }
    }
}

//Function to display the Iframe on menu popup to hide the controls on the 
//parent page
function fnNMGNShowIframe( divPopup , panel, callingSource)
{

    var nonCallingSource = "NMFNFormLibraryHidden";
    if ( callingSource == "NMFNFormLibraryHidden")
    {
        nonCallingSource = "NMFNGlobalNavigationHidden";
    }
    var bounds = Sys.UI.DomElement.getBounds(panel);
    var px = bounds.x;
    var py = bounds.y;
    var pxend = px + divPopup.offsetWidth;
    var pyend = py + divPopup.offsetHeight;
    
    //Hide Select boxes
    var elements = document.getElementsByTagName("select");  
    
    var ua = navigator.userAgent.toLowerCase();
    if(ua.indexOf("msie 6.0")!= -1)
    {
    for ( var iCount =0; iCount < elements.length; ++iCount)
    {
        bounds = Sys.UI.DomElement.getBounds(elements[iCount]);
        var x = bounds.x;
        var y = bounds.y;
        var xend = x + elements[iCount].offsetWidth;
        var yend = y + elements[iCount].offsetHeight;
        if ( ( x < pxend) && (xend > px))
        {
            if ( ( y < pyend) && (yend > py))
            {
               if  (elements[iCount].style.visibility != "hidden")
               {
                  elements[iCount].style.visibility = "hidden";
                  elements[iCount].setAttribute(callingSource, "true");
                  elements[iCount].setAttribute(nonCallingSource, "false");
               }
            }
        }
    }
    }
        
    if ( callingSource == "NMFNGlobalNavigationHidden")
    {
        //Hide the popup div for Form Library
        elements = document.getElementsByTagName("div"); 
        for ( var iCount =0; iCount < elements.length; ++iCount)
        {
             if (elements[iCount].getAttribute("NMFNFLPopup") != null)
             {
                if (elements[iCount].getAttribute("NMFNFLPopup") == "true")
                {
                
                    bounds = Sys.UI.DomElement.getBounds(elements[iCount]);
                    var x = bounds.x;
                    var y = bounds.y;
                    var xend = x + elements[iCount].offsetWidth;
                    var yend = y + elements[iCount].offsetHeight;
                    if ( ( x < pxend) && (xend > px))
                    {
                        if ( ( y < pyend) && (yend > py))
                        {
                            elements[iCount].style.visibility = "hidden";
                        }
                     }
                }
             }
        }
    }
     HideStreetView( ua);
}
//Function to hide street view in Office Locator Google
function HideStreetView ( ua )
{
	if(ua.indexOf("chrome")!= -1)
    {
    	var streetView = document.getElementById("streetview_street")
		if ( streetView != null )
		{	 
			if (( streetView.className  == "nmgol_visibleDiv_street" )
			|| ( streetView.className  == "nmgol_visibleDiv_street_maximize" ))
			{	
				if ( streetView.innerHTML.toLowerCase().
				indexOf('<param name="wmode" value="opaque">' ) == -1 )
				{
		    		var flshObjs = streetView.getElementsByTagName("object")
					if ( flshObjs.length > 0)
					{
						var flshObj = flshObjs [0];
						if ( flshObj != null)
						{
						 	var elm = document.createElement("param");
						 	elm.setAttribute("name", "wmode");
						 	elm.setAttribute("value", "opaque");
						 	flshObj.appendChild (elm);						 
						} 
					}
				}
			}
		}
	} 
}

//Function to return back the frame into its previous state
function fnNMGNHideIframe(callingSource)
{
    var elements = document.getElementsByTagName("select");  
    for ( var iCount =0; iCount < elements.length; ++iCount)
    {
      if (elements[iCount].getAttribute(callingSource) != null)
      {
        if (elements[iCount].getAttribute(callingSource) == "true")
        {
             elements[iCount].style.visibility = "visible";
             elements[iCount].setAttribute(callingSource, "false");
        }
      }
    }
    
    
    if ( callingSource == "NMFNGlobalNavigationHidden")
    {
         //unhide the popup div for Form Library
        elements = document.getElementsByTagName("div"); 
        for ( var iCount =0; iCount < elements.length; ++iCount)
        {
             if (elements[iCount].getAttribute("NMFNFLPopup") != null)
             {
                if (elements[iCount].getAttribute("NMFNFLPopup") == "true")
                {
                    elements[iCount].style.visibility = "visible";
                }
             }
        }
    }
}
 

//Function to show the popup on hover over the main navigation
function fnNMGNMenuHover(sChildNavigation, sTag, tagValue, hasTabs, tabChanged, selectedTab, personal, cellPersonal)
{   
	//Position the secondary navigation drawers properly
    fnNMGNDrawerPosition();

    //Get the panel for the menu item hovered over
    var pnlSecondaryNavigation = document.getElementById(sChildNavigation);
   
    var hdTagValue = document.getElementById(tagValue);
    var sTagValue = hdTagValue.value;
    var arrTags = sTagValue.split(":");
  
   //Hide all other divs 
   var  iCount=0;
   for (iCount=0; iCount < arrTags.length; ++iCount)
   {    
       var navID = sChildNavigation.replace(sTag,arrTags[iCount]);
       if ( navID != sChildNavigation)
       {
           var pnlSecondaryNav = document.getElementById(navID);
           pnlSecondaryNav.style.visibility = "hidden";              
       }     
   }
    //Make the selected secondary navigation visible
    pnlSecondaryNavigation.style.visibility = "visible";       
    
    //Select the proper tab on the panel
    if ( hasTabs.toLowerCase() == "true")
    {   
       var business = personal.replace("dvGNPersonal", "dvGNBusiness");
       var hdTabChanged = document.getElementById(tabChanged); 
 
       fnNMGNTabChanged(tabChanged, hdTabChanged.value, personal, business, cellPersonal,  sChildNavigation );
    }
    else
    { 
        var divPersonal = document.getElementById(personal);
        divPersonal.style.visibility = "visible";
        var divPopup = document.getElementById(personal.replace("dvGNPersonal", "dvPopupMenu"));

        var divPopupHeight = divPersonal.offsetHeight  + 7;
        var divPopupWidth = divPersonal.offsetWidth  + 8;  
        divPopup.style.height = divPopupHeight + "px";
        divPopup.style.width = divPopupWidth + "px";  
  		//For Background Shadow
        var divPopupMenuMain= document.getElementById(personal.replace("dvGNPersonal", "dvPopupMenuMain"));
        divPopupHeight  = divPopupHeight + 5 ;
        divPopupMenuMain.style.height = divPopupHeight + "px";
        divPopupMenuMain.style.width = divPopupWidth + "px";       
               
        //Set the panel position 
        if ( pnlSecondaryNavigation.style.left == "")
        {
            var pnlPositionLeft = ( parseInt ( pnlSecondaryNavigation.style.right , 10)) -  (parseInt( divPopup.style.width, 10));
            if ( pnlPositionLeft != NaN)
            {
                pnlSecondaryNavigation.style.left = pnlPositionLeft + "px";
            }
        }
        fnNMGNShowIframe (divPopup, pnlSecondaryNavigation, "NMFNGlobalNavigationHidden" );
    }
}

//Function to hide the popup in case of hover out of the cursor
function fnNMGNMenuHoverOut(sChildNavigation, sTag, tagValue, hasTabs, tabChanged, selectedTab, personal)
{
    //Get the panel for the menu item hovered out of
    var hdTabChanged = document.getElementById(tabChanged);    
    var hdTagValue = document.getElementById(tagValue);
    var sTagValue = hdTagValue.value;
    var arrTags = sTagValue.split(":");       
   //Hide all divs and content within
   var  iCount=0;
   for (iCount=0; iCount < arrTags.length; ++iCount)
   {  
        //Hide Divs
       fnHideDivs (personal,hasTabs );
       var navTopID = sChildNavigation.replace(sTag,arrTags[iCount])
       var pnlSecondaryNav = document.getElementById(navTopID );
       pnlSecondaryNav.style.visibility = "hidden";
   }   
   //Hide the frame
   fnNMGNHideIframe ("NMFNGlobalNavigationHidden");    
}


//Function to redirect to the proper page ( business or personal)
// when the user clicks on the main menu with tabs
function fnNMGNMenuClick (personalurl, businessurl, tabChanged)
{ 
    var hdTabChanged = document.getElementById(tabChanged);
    var url;
     if ( hdTabChanged.value == "0")
    {
        url = personalurl;
    }
    else
    {
        url = businessurl;
    }
    window.location = url;
}

//Function to change the popup data in case of tab changed
// This function is also called to load the proper tab on page load
function fnNMGNTabChanged ( tabChanged , tabSelected, personal, business, cellPersonal,  panel)
{
 
    var divPersonal = document.getElementById(personal);
    
    var divBusiness = document.getElementById(business);
    var divPopup = document.getElementById(personal.replace("dvGNPersonal", "dvPopupMenu"));
    var dvPopupMenuInner = document.getElementById(personal.replace("dvGNPersonal", "dvPopupMenuInner"));
    var hdTabChanged = document.getElementById(tabChanged);
       
	//For Background Shadow
    var divPopupMenuMain= document.getElementById(personal.replace("dvGNPersonal", "dvPopupMenuMain"));
		
   //Get the cells to change the css of the tabs
    var btnPersonal = document.getElementById(cellPersonal);
    var btnBusiness = document.getElementById(cellPersonal.replace("btnPersonal", "btnBusiness"));

    hdTabChanged.value = tabSelected;
    
    var tabChangedCookieName = "NMFNGlobalNavigationTabSelected";
    try 
    {    
       fnNMGNSetCookie (tabChangedCookieName, tabSelected);       
    }
    catch(e)
    {       
    }
    var tabWidth = btnBusiness.offsetWidth + btnPersonal.offsetWidth + parseInt ( dvPopupMenuInner.style.paddingLeft , 10);
    var divPopupWidth;
  
    if ( tabSelected == "0")
    {
        divPersonal.style.visibility = "visible";
        divBusiness.style.visibility = "hidden";
        
        btnPersonal.className = "gnTabPersonalSelected";
        btnBusiness.className = "gnTabBusinessNotSelected";
        
        var divPopupHeight = divPersonal.offsetHeight + btnPersonal.offsetHeight ; 
        divPopupWidth = divPersonal.offsetWidth  + 8;
        
        divPopup.style.height = divPopupHeight + "px" ;       
        divPopup.style.width = divPopupWidth + "px" ;
        divPopupHeight = divPopupHeight  + 5;
        divPopupMenuMain.style.height = divPopupHeight + "px";
    	divPopupMenuMain.style.width = divPopupWidth + "px";  
                
    }
    else
    {
        divPersonal.style.visibility = "hidden";
        divBusiness.style.visibility = "visible";
        
        btnPersonal.className = "gnTabPersonalNotSelected";
        btnBusiness.className = "gnTabBusinessSelected";
        
        var divPopupHeight = divBusiness.offsetHeight + btnBusiness.offsetHeight; 
        divPopupWidth = divBusiness.offsetWidth  + 8;
        
        divPopup.style.height = divPopupHeight + "px" ;
        divPopup.style.width = divPopupWidth + "px" ;
        divPopupHeight = divPopupHeight  + 5;
        divPopupMenuMain.style.height = divPopupHeight + "px";
    	divPopupMenuMain.style.width = divPopupWidth + "px"; 
        
        
    }
    //Check if the tabs are shifted
    if (  tabWidth > divPopupWidth)
    {
        var paddingLeftInnerPopuup = tabWidth - divPopupWidth + 10;
        paddingLeftInnerPopuup =  parseInt ( dvPopupMenuInner.style.paddingLeft , 10) - paddingLeftInnerPopuup;
        dvPopupMenuInner.style.paddingLeft = paddingLeftInnerPopuup + "px";
    }
        
    var secondaryPanel = document.getElementById(panel);
    fnNMGNShowIframe ( divPopup, secondaryPanel, "NMFNGlobalNavigationHidden" ); 
     
}

//Function to hide unhide the popup menu when user hovers over the 
//secondary and tertiary menu links 
function fnNMGNHoverSecondaryMenu( mainNav, personal, tabChanged, hasTabs, cellPersonal, selectedMainButton,  panel, secondaryNavigationTag, isMouseIn )
{
	
    var pnlSecondaryNavigation = document.getElementById(panel);
    var hdTabChanged = document.getElementById(tabChanged);    
    var business = personal.replace("dvGNPersonal", "dvGNBusiness");
    var divPersonal = document.getElementById(personal);  
    var btnTopNav = document.getElementById(mainNav);   
 
    if ( isMouseIn == '1' )
    {
        //Change the css class of the selected top nav        	   	
	    btnTopNav.className = "gntabSelected" + secondaryNavigationTag; 
        
        pnlSecondaryNavigation.style.visibility = "visible";
        if ( hasTabs.toLowerCase() == "true" )
        {
           fnNMGNTabChanged(tabChanged, hdTabChanged.value, personal, business, cellPersonal, panel);
         }
         else
         {
            divPersonal.style.visibility = "visible";
            fnNMGNShowIframe ( divPersonal, pnlSecondaryNavigation, "NMFNGlobalNavigationHidden" ); 
         }
    }
    else
    {   
        //Change the css class for deselecting the top nav
        var hdSelectedNav = document.getElementById(selectedMainButton); 
		if ( btnTopNav.id == hdSelectedNav.value)
		{
			btnTopNav.className = "gntabPage" + secondaryNavigationTag;   
		}
		else
		{
			btnTopNav.className = "gntab" + secondaryNavigationTag; 
		} 	 

        //Hide the tabs whithin
        fnHideDivs (personal,hasTabs );

        pnlSecondaryNavigation.style.visibility = "hidden";
        fnNMGNHideIframe("NMFNGlobalNavigationHidden");
    }    
}

//Function to hide all the tabs
function fnHideDivs( personal, hasTabs)
{
    var divPersonal = document.getElementById(personal);
    divPersonal.style.visibility = "hidden";
    if ( hasTabs.toLowerCase() == "true")
    {
        var business = personal.replace("dvGNPersonal", "dvGNBusiness");
        var divBusiness = document.getElementById(business);
        divBusiness.style.visibility = "hidden";
    }
   
}

//End of Javascript for Global Navigation