var commonAccId = '';
var pageName='';
var commonSiteId='';

/* 	Function:siteCatalyst()
	Aim: To track the page names on user action for Omniture */
function siteCatalyst(pgname)
{	

	s.pageName=s.getPageName()+'/'+pgname; 
    s.cookieDomainPeriods="2"
    s.currencyCode="USD"
    /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
    var s_code=s.t();if(s_code)document.write(s_code)
}

function init(server,root,parent,child,appType,webexHome,modParam,showAssist)
{
    $(document).ready(function()
    {
		if(showAssist=='false')
		{
			     	$("#leftNavigation").load('/customerFirst/support/US/commons/center/pages/leftNavNoAssist.html' , function()
					  { 
					    loadLeftNavTree(server,root,parent,child,appType,webexHome,modParam);
					  });
					 
		}
		else
		{
					$("#leftNavigation").load('/customerFirst/support/US/commons/center/pages/leftNav.html' , function()
					  { 
					    loadLeftNavTree(server,root,parent,child,appType,webexHome,modParam);
					  });
					 
		}
	});
	
  }   

function loadLeftNavTree(server,root,parent,child,appType,webexHome,modParam)
{
	   $("#leftnavbox ul ul").hide();
	    //Expand Root, Parent & Child
	    var text;
	    if(child!=null && child!='')    
	    {
	        text=child;
	    }
	    else if(parent!=null && parent!='')    
	    {
	        text=parent;
	    }
	    else if(root!=null && root!='' && root!='null')
	    {
	        text=root;
	    }
	    
	    syncLeftNavBreadCrumb(root,parent,child,server,text,appType,'false',webexHome,modParam);


	    $("#leftnavbox li a").click(function()
	    {
		  text = $(this).attr('id');
	          
	          //show waiting message for portal pages if moving from NEW -> OLD
	          if(text=='profile' || text=='helpSupport' || text=='dwnload' || text=='searchKb' || text=='hwDoI'  || text=='contactSupp' || text=='document' || text=='support')
		  {
		     $.blockUI({ message:'<h2>Loading.....</h2>'  });
		  }
		  
		  var link=$(this).attr('name');
		  if(link=='child')
		  {
		     child=text;
		     parent=parent;
		     root=root;	
		  }
		  else if(link=='parent')
		  {
		     child='';
		     parent=text;
		     root=root;
		  }
		  else if(link=='root')
		  {
		     child='';
		     parent='';
		     root=text;
		     $("#leftnavbox ul ul").hide();
		  }
		  /* Added for removing the KB article and project name in session - Pradnya */
		   if(text == 'support')
		  {			
		   /* Set the index and project name in session for viewing previous KB article */
			TicketFacade.removeKBProjectInSession(function() {            					
			});	
		  }
		  var leftNavClick='true';
		  syncLeftNavBreadCrumb(root,parent,child,server,text,appType,leftNavClick,webexHome,modParam);
	    });
	    
	    if(parent!='helpSupport')
	     expandGetHelp();	
}

messageObj = new DHTML_modalMessage();	// We only create one object of this class
messageObj.setShadowOffset(5);	// Large shadow

function loadRightContent(text,server,root,parent,child,webexHome,modParam)
{
    if(text=='myAcc')
    {			
		ManageAccountFacade.checkUserBillingType(function(userBillingType) {			
			if(userBillingType != null && userBillingType=='APOLLO')
			{				
				$("#rightContent").load('/customerFirst/support/US/accountmanagement/center/pages/apollomanageAccount.html', function(){
					
					siteCatalyst("Step 1 - Manage My Account");
					
					openAddSiteAccount(server);

					//Pass the server variable for further use
					loadServerURL(server);

					setAccountSite();

					var t=setTimeout("setAccountAndSiteValues()",1000)	
				});	
			}else{
				$("#rightContent").load('/customerFirst/support/US/accountmanagement/center/pages/manageAccount.html', function(){
					
					siteCatalyst("Step 1 - Manage My Account");
					
					openAddSiteAccount(server);

					//Pass the server variable for further use
					loadServerURL(server);

					setAccountSite();

					var t=setTimeout("setAccountAndSiteValues()",1000)	
				});	
			}
		});	
	}

    else if(text=='createOrder')
    {
        $("#rightContent").load('/customerFirst/support/US/ticketmanagement/center/pages/CreateTicketSteps.html', function(){ 
		//alert("Inside mainPage.js");
		populateValues();
        	hideRemainingDivs();
        });
    }
    else if(text=='orderHistory')
    {
	$("#rightContent").load('/customerFirst/support/US/ticketmanagement/center/pages/ViewUpdateTicket.html', function(){ 
        		getTicketList();
			disable();
        });
    }
    else if(text=='orderHistoryAcc')
    {
    	 $("#rightContent").load('/customerFirst/support/US/accountmanagement/center/pages/viewOrderSupport.html', function()
	{  
		siteCatalyst(Pg_AccountManagement_TicketHistory);
		
		serverURL=server;
		fetchTicketHistory();
		//openAddSiteAccount(server);
	});
	
    }
    else if(text=='invoice')
    {
        $("#rightContent").load('/customerFirst/support/US/invoices/center/pages/invoices.html', function(){
        	
		   siteCatalyst(Pg_Invoices_Landing);
        	//openAddSiteAccount(server);
        	
        	setAccount(text,server,'');
        	//forward(document.getElementById('select').options[0].value,server);
        });
    }
    else if(text=='invPayHist')
    {
        $("#rightContent").load('/customerFirst/support/US/invoices/center/pages/invoicePayHistory.html', function(){
		 siteCatalyst(Pg_Invoices_InvoiceHistory);
		//openAddSiteAccount(server);
		setAccount(text,server ,'');
		
        	//initInvoices("123816",server);
        });
    }
    else if(text=='autoPay')
    {
    	 InvoiceFacade.getAccountList(function(invoiceBean) {
		var varInvoiceBean = invoiceBean;
	         var selectedAcc = varInvoiceBean.selectedAccount;
	         commonAccId = selectedAcc;
		 var resultobj;
		 InvoiceFacade.getCCByAcctId(commonAccId,function(data) {
		 resultobj=data;
		 if(resultobj!=null)
		 {
		 	if(resultobj.result == 'Success')
		 	{
		 		
		 		//document.getElementById('invoiceHomeDiv').style.display='';
		      		$("#rightContent").load('/customerFirst/support/US/invoices/center/pages/Autopay_Enrolled.html', function(){
					 siteCatalyst(Pg_Invoices_AutoPayEnroled);
				openAddSiteAccount(server);
			     	setAccount(text,server,resultobj);
		      		});
		      	}
		      	else
		      	{
		      		
		      		  $("#rightContent").load('/customerFirst/support/US/invoices/center/pages/Autopay_Enrolled.html', function(){
					   siteCatalyst(Pg_Invoices_AutoPayEnroled);
				   openAddSiteAccount(server);
			     	   setAccount(text,server,resultobj);
			     	   });
		      		   	
		      		   //alert('come');
		      		   document.getElementById('invoiceHomeDiv').style.display='none';
		      	}
		 }
		 else
		 {
		 	
		 		//document.getElementById('invoiceHomeDiv').style.display='';
		     		$("#rightContent").load('/customerFirst/support/US/invoices/center/pages/AutoPay_NotEnroll.html', function(){ 
				siteCatalyst(Pg_Invoices_AutoPayNotEnroled);					
				openAddSiteAccount(server);
			    	setAccount(text+'notenrolled',server,resultobj);  
		      		});
		 }
		  
	    });
	});
    }
    
    else if(text=='billAdd')
    {
        $("#rightContent").load('/customerFirst/support/US/invoices/center/pages/updateBillingAddress.html', function(){ 
			  siteCatalyst(Pg_Invoices_BillingAddress);
			//openAddSiteAccount(server);
        		setAccount(text,server,'' );
        });
    }
	else if(text=='serviceSummary')
    {
        //alert('inside mainpagejs serviceSummaryTest');
		//alert('inside mainpagejs serviceSummaryTest'+text);
		$("#rightContent").load('/customerFirst/support/US/invoices/center/pages/serviceSummaryGpl.html', function(){ 
			  siteCatalyst(Pg_Invoices_BillingAddress);
			//openAddSiteAccount(server);
				//alert('inside mainpagejs setAccount text serviceSummaryTest'+text);
        		setAccount(text,server,'' );
        });
    }
    else if(text=='startWebex')
    {
        $("#rightContent").load('/customerFirst/support/US/commons/center/pages/gettingStartedWebEx.htm', function(){
          	//Call DWR for --
        });
    }
    else if(text=='mngYrAcct')
    {
        $("#rightContent").load('/customerFirst/support/US/commons/center/pages/manageYourAccount.htm', function(){
              
	        $("#manageYourAccountDiv a").click(function()
					{
				  text = $(this).attr('id');
					
					  if(text=='seeMeetings')
				  {
				  	window.location.href=webexHome;
				  }
				   if(text=='technicalSupport')
				  {
				  if(webexHome.match('CustomerCareHome')) {
						window.location.href=webexHome;
				  }
				  else { 
				 window.location.href="/SelfServiceWeb/SelfServicePortal.portal?_nfpb=true&_pageLabel=AssistServicesHome&root=support&parent=asstServ";
				  }
				  }
				  if(text=='changeYourPwd')
				  {
				  if(webexHome.match('CustomerCareHome')) {
					window.location.href=webexHome;
				  }
				  else { 
				   window.location.href="/SelfServiceWeb/SelfServicePortal.portal?_nfpb=true&_pageLabel=ChangePassword&actionOverride=%2Fportlets%2FviewProfile%2FchangePassword";
				  }
				  }
				  });
				  
				          });
    }
    else if(text=='support')
    {
           window.location.href='/SelfServiceWeb/SelfServicePortal.portal?_nfpb=true&_pageLabel=Support&root=support';
    }
    else if(text=='myr')
    {
    	window.location.href=webexHome;
    }
    else if(text=='portalOrder')
    {
    	$("#rightContent").load('/customerFirst/support/US/ticketmanagement/center/pages/ViewUpdateTicket.html', function(){ 
        	viewTicket(modParam);
        });
    }
    else if(text=='accountPay')
    {
    	InvoiceFacade.getSiteIdFromSession(function(siteId) {
              		  commonSiteId = siteId;
			  if(siteId =='' || siteId==null)
			  {
					 commonSiteId ='No Sites';
			  }
			  sleep(500); 
			  payInvoiceBalance(server,modParam);
		 });
    }
	else if(text=='ticketMgmtPage')
    {
		$("#rightContent").load('/customerFirst/support/US/ticketmanagement/center/pages/ViewUpdateTicket.html', function(){
			viewTicket(modParam);
		});
    }
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
    else if(text=='hwDoI')
    {
		$("#rightContent").load('/customerFirst/support/US/howdoi/center/pages/hdiLang.html', function(){
			displayResult();
			displayLanguages(hdiLang);
			
		});
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	else if(text=='accountMgmtPage')
    {
		/* Invoke Account Management module */
		$("#rightContent").load('/customerFirst/support/US/accountmanagement/center/pages/orderRequest.html', function()
		{
				loadServerURL(server);
				loadOrderRequestPage(modParam);
		});
    }
    else if(text=='createHost'||text=='removeHost'||text=='inviteMeeting'||text=='requestTraining'||text=='requestContact'||text=='getContact'||text=='cancelService')
    {
       	$("#rightContent").load('/customerFirst/support/US/accountmanagement/center/pages/manageAccount.html', function(){
				siteCatalyst(Pg_AccountManagement_Landing);
				
            	var aId=document.getElementById(text);
            	loadServerURL(server);
            	//setAccountSite();
            	redirectKBSuggestionsPage(aId.innerHTML,aId.title);
        });
    }
    else if(text=='assistEvent')
    {
        $("#rightContent").load('/customerFirst/support/US/commons/center/pages/assistEventDetails.htm', function(){
	      	//Call DWR for --
        });
    }
}



function sleep(milliseconds)
{
	var now = new Date();
	var exitTime = now.getTime() + milliseconds;
	while (true)
	{
		now = new Date();
		if (now.getTime() > exitTime)
		return;
	}
}

function openModelMsg(url)
{
	messageContent = '<div id="popupInner">' + '<div id="popupControls">' + '<a title="Close Window" onclick="closeMessage();" id="popCloseBox" /> Close window </a>'
	+ '</div>' + '<iframe src="'+url+'" style="width:490px;height:620px;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame"></iframe>'
	+ '</div>';
	messageObj.setHtmlContent(messageContent);
	messageObj.setSize(490,620);
	messageObj.setSource(false);                                                                                                   // no html source since we want to use a static message here.
	messageObj.setShadowDivVisible(false);                             // Disable shadow for these boxes          
	messageObj.display();
	//ShowLayer();
}

function closeMessage()
{
   messageObj.close();	
   parent.location.reload(1);
}

function openAddSiteAccount(server)
{
    $("#rightContent a").click(function()
    {
       var text = $(this).attr('id');
 	   if(text=='addSite')
	   {
	       	var url='/SelfServiceWeb/portlets/addAccountNew/addAccountNewController.jpf?modalmsg=true';
	       	openModelMsg(url);
    	   }
    });	
}

function ShowLayer() {
    // hide all dropdowns on the page
    // looping through all forms on the page
	
    for (f = 0; f < document.forms.length; f++)
    {
        var elements = document.forms[f].elements;
        // looping through all elements on certain form

        for (e = 0; e < elements.length; e++)
        {
            if (elements[e].type == "select-one")
            {
                elements[e].style.display = 'none';
            }
        }
    }
        
    // show layer

  //  var layer = document.getElementById('layer');
   // layer.style.display = 'block';
}

function rightContentClick(root,parent,child,server)
{
    $("#rightContent a").click(function()
    {
    	var text = $(this).attr('id');
    	
    	var link=$(this).attr('name');
	
	if(link=='child')
	  {
	     child=text;
	     parent=parent;
	     root=root;	
	  }
	  else if(link=='parent')
	  {
	     child='';
	     parent=text;
	     root=root;
	  }
	  else if(link=='root')
	  {
	     child='';
	     parent='';
	     root=text;
	  }
	  
	syncLeftNavBreadCrumb(root,parent,child,server,text,appType);
    });
    
}

function createBreadCrumb(root,parent,child,server,appType,webexHome)
{
    
    $("#breadcrumb").empty();
    if(child!=null && child!='' && getBranchName(child)!='' && getBranchName(parent)!='' && getBranchName(root)!='')    
    {
	 $("#breadcrumb").append('<a href="http://www.webex.com/">WebEx</a> &gt; <a href="#" id='+root+' name="root">'+getBranchName(root)+'</a> &gt; <a href="#" id='+parent+' name="parent">'+getBranchName(parent)+'</a> &gt;'+getBranchName(child));
    }
    else if(parent!=null && parent!='' && getBranchName(parent)!='' && getBranchName(root)!='')    
    {
	 $("#breadcrumb").append('<a href="http://www.webex.com/">WebEx</a> &gt; <a href="#" id='+root+' name="root">'+getBranchName(root)+'</a> &gt;'+getBranchName(parent));
    }
    else if(root!=null && root!='' && getBranchName(root)!='')
    {
	 $("#breadcrumb").append('<a href="http://www.webex.com/">WebEx</a> &gt;'+getBranchName(root));
    }
    
    $("#breadcrumb a").click(function()
    {
    	var text = $(this).attr('id');
    	if(text=='support' || text=='myr')
	{
	   $.blockUI({ message:'<h2>Loading.....</h2>'  });
	}
	var link=$(this).attr('name');
     	 
     	 if(link=='child')
	  {
	     child=text;
	     parent=parent;
	     root=root;	
	  }
	  else if(link=='parent')
	  {
	     child='';
	     parent=text;
	     root=root;
	  }
	  else if(link=='root')
	  {
	     child='';
	     parent='';
	     root=text;
	     $("#leftnavbox ul ul").hide();
	  }
	syncLeftNavBreadCrumb(root,parent,child,server,text,appType,'false',webexHome);
    });
}

function syncLeftNavBreadCrumb(root,parent,child,server,text,appType,leftNavClick,webexHome,modParam)
{
	$("#leftnavbox li a").each(function(){
	   $(this).parent('li').removeClass('active');
	});
	
	syncLeftNav(root,parent,child);
	if(appType=='NEW')
	{
	    createBreadCrumb(root,parent,child,server,appType,webexHome); 
	    if(leftNavClick==null || leftNavClick=='' || leftNavClick=='false' || text=='myr') // If on left Nav MYR Home is clicked than also load the content
	      loadRightContent(text,server,root,parent,child,webexHome,modParam);	
	}
	else if(appType=='OLD')
	{
	    if(text=='myr')
	    {
	        window.location.href=webexHome;
    	    }
	}
}

function sendBackToNewApp(root,parent,child,server,text)
{ 
    
    if(text=='myAcc')
    {
       window.location.href='/SelfServiceWeb/AccountManagement.jsp?root='+root+'&parent='+parent+'&child='+child;
    }
    else if(text=='invoice' || text=='invPayHist' || text=='autoPay' || text=='billAdd' || text=='serviceSummary')
    {
        window.location.href='/SelfServiceWeb/Invoices.jsp?root='+root+'&parent='+parent+'&child='+child;
    }
    else if(text=='orderHistory' || text=='createOrder')
    {
        window.location.href='/SelfServiceWeb/TicketManagement.jsp?root='+root+'&parent='+parent+'&child='+child;
    }
}

function syncLeftNav(root,parent,child)
{
	$("#leftnavbox li a").each(function(){
	    var cLink=$(this).attr('id');
	    if(child!=null && child!='' && child==cLink)    
	    {
		var subnavroot = $("#"+root).next(); 
		if (subnavroot.is(":hidden")) { 
		    subnavroot.slideDown("fast"); 
		}
		var subnav = $("#"+parent).next(); 
		if (subnav.is(":hidden")) { 
		    subnav.slideDown("fast"); 
		} 
		$(this).parent('li').addClass('active');
	    }
	    else if((child==null || child=='' || getBranchName(child)=='') && parent!=null && parent!='' && parent==cLink)    
	    {
	    	var subnavroot = $("#"+root).next(); 
		if (subnavroot.is(":hidden")) { 
		    subnavroot.slideDown("fast"); 
		}
		
		var navsibling = $(this).parent().siblings('li').children('ul'); 
		if (navsibling.is(":visible")) { 
		    navsibling.slideUp("fast"); 
                } 
		var subnav = $(this).next(); 
		if (subnav.is(":hidden")) { 
		      subnav.slideDown("fast"); 
		} else { 
		      subnav.slideUp("fast"); 
		}
		$(this).parent('li').addClass('active');
	    }
	    else if((child==null || child=='' || getBranchName(child)=='') && (parent==null || parent=='' || getBranchName(parent)=='') && root!=null && root!='' && root==cLink)    
	    {
		var navsibling = $(this).parent().siblings('li').children('ul'); 
		if (navsibling.is(":visible")) { 
		    navsibling.slideUp("fast"); 
                }
		var subnav = $(this).next(); 
		if (subnav.is(":hidden")) { 
		      subnav.slideDown("fast"); 
		} else { 
		      subnav.slideUp("fast"); 
		}
	    }
	}); 	
}

function getBranchName(level)
{
   var name='';
   $("#leftnavbox li a").each(function(){
   	var label=$(this).attr('label');
   	var id=$(this).attr('id');
   	if(id!=null && id!='' && id==level)
   	{
   	    name=label;
   	    return false;	
   	}
   });
   
   return name;
}

function expandGetHelp()
{
   $("#helpSupport").next().slideDown("fast"); 	
}

