/*******************************************************************************
	
	File Name:-		header.js
	Written by:-    Sangeetha Navaratnam
	Date:-			Spe 2007
	Version:-		1.0
	Updates:-		
	Previous Updates:-	
********************************************************************************
	Description:-	Contains the Javascript functions used by header2.html 
	                and header1.html in all the web modules.
	Function List:-	   clearText
			   putText
			   getCookie
			   keepSessionAlive
			   showLogin
			   loadPage
********************************************************************************/


/*******************************************************************************
	Function Name:- clearText
********************************************************************************
	Clears the text.
	Parameters: None
	Return Values: None.
********************************************************************************/
	function clearText(){
	     var i=document.getElementById("q").value;
	     if(i=="Search...")
	       document.getElementById("q").value="";
        }


/*******************************************************************************
	Function Name:- putText
********************************************************************************
	Puts text.
	Parameters: None.
	Return Values: None.
********************************************************************************/
	function putText(){
	    var i=document.getElementById("q").value;
	    if(i=="" || i==" ")
	    document.getElementById("q").value="Search...";
        }


/*******************************************************************************
	Function Name:- getCookie
********************************************************************************
	Gets the cookie value of the site minder session details set by 
	siteminder when authentication.
	Parameters: var cookiename
	Return Values: var.
********************************************************************************/
	function getCookie(cookiename) {
	   
	      var cookiestring=""+document.cookie;
	      var index1=cookiestring.indexOf(cookiename);
	      if (index1==-1 || cookiename=="") return ""; 
	      var index2=cookiestring.indexOf(';',index1);
	      if (index2==-1) index2=cookiestring.length; 
	      
	      return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
        }
        
/*******************************************************************************
	Function Name:- keepSessionAlive
********************************************************************************
	Activates the image from given URL if the expected cookie value is there.
	Parameters: None.
	Return Values: None.
********************************************************************************/
	function keepSessionAlive() {
		   
	      var hasAccessedWLP = getCookie("JSESSIONID_btwportal");
	      if(hasAccessedWLP != null && hasAccessedWLP != ''){
	      	var randomNumber = Math.round(Math.random()*10000);
	      	document.images("renewSession").src = "/btwportal/images/transparent.gif?"+randomNumber;
	      }
        }
        
/*******************************************************************************
	Function Name:- showLogin
********************************************************************************
	Show, hide the Login, Logout link depending on user's authentication status.
	Parameters: None.
	Return Values: None.
********************************************************************************/
	function showLogin() {
	
	    var isLoggedin = getCookie('SMSESSION');
            var cadAuthCookie = getCookie('BTWOSFINTREG');            
            var div1 = document.getElementById('show_login');
            var div2 = document.getElementById('show_logout');

            if (isLoggedin == null || isLoggedin == '' || isLoggedin == "LOGGEDOFF" ) {
            //login
                                    div1.style.display = '';
                                    div2.style.display = 'none';
             }
              else if (cadAuthCookie != null && cadAuthCookie != ''){
                   //login
                   div1.style.display = '';
                   div2.style.display = 'none';
             }
             else{
                  //logout
                  div1.style.display = 'none';
                  div2.style.display = '';
             }
       }    
       
/*******************************************************************************
	Function Name:- loadPage
********************************************************************************
	Checks the SMSESSION cookie value to verify whether user logged in to 
	the app. And if yes, change the target url for Home and Customerzone 
	accordingly.
	Parameters: var page.
	Return Values: None.
********************************************************************************/
	function loadPage(page){
	
	      var smSessionValue = getCookie("SMSESSION");
	
	      if (smSessionValue == null || smSessionValue == '' || smSessionValue == "LOGGEDOFF") {
	        if (page == "home" ) {
	          document.location.href = "/portalzone/login/home.do";
	        }
	        else {
	          document.location.href ="/portalzone/login/customerzone.do";
	        }
	      }
	      else {
	         document.location.href = "/portalzone/portalzone/homeLogin.do?from=" + page;
	      }
        }