/*******************************************************************************
' Function Name		fnLoad				
'
' Description		This function toggles the Show/Hide Summaries link on load
'*******************************************************************************/
function fnLoad(summaryLinkID, hiddenInputID, sShowSummaries, sHideSummaries)
{   
	if (document.getElementById(summaryLinkID)!=null)
   	{
		var hiddenInput = document.getElementById(hiddenInputID);
    	var summaryLink = document.getElementById(summaryLinkID);
    
        if (hiddenInput.value != "show")
        {
	        fnHideAll(summaryLinkID, hiddenInputID, sShowSummaries, sHideSummaries);       
        }
        else
        {
	        fnShowAll(summaryLinkID, hiddenInputID, sShowSummaries, sHideSummaries);
        }
    }
    
    // Function used to maintain the selected value of the scope dropdownlist
    fnMaintainScope();
}

/*******************************************************************************
' Function Name		fnCheck				
'
' Description		This function toggles the Show/Hide Summaries link
'*******************************************************************************/
function fnCheck(summaryLinkID, hiddenInputID, sShowSummaries, sHideSummaries)
{
  	var hiddenInput = document.getElementById(hiddenInputID);
    var summaryLink = document.getElementById(summaryLinkID);
    
    if (hiddenInput.value != "show")
    {
	    fnShowAll(summaryLinkID, hiddenInputID, sShowSummaries, sHideSummaries);       
    }
    else
    {
	    fnHideAll(summaryLinkID, hiddenInputID, sShowSummaries, sHideSummaries);
    }
}

/*******************************************************************************
' Function Name		fnShowAll				
'
' Description		This function shows the search descriptions if hidden
'*******************************************************************************/
function fnShowAll(summaryLinkID, hiddenInputID, sShowSummaries, sHideSummaries)
{
    var hiddenInput = document.getElementById(hiddenInputID);
    var summaryLink = document.getElementById(summaryLinkID);
    var divElements = document.getElementsByName('trSummary');
	var iCellItemCount = divElements.length;   
 
    for (iCount=0; iCount<iCellItemCount; iCount++)
	{
        divElements[iCount].style.display = "block";
    }
    
    summaryLink.innerHTML = "<b>" + sHideSummaries + "</b>";	
	hiddenInput.value = 'show';
}

/*******************************************************************************
' Function Name		fnHideAll				
'
' Description		This function hides the search descriptions
'*******************************************************************************/
function fnHideAll(summaryLinkID, hiddenInputID, sShowSummaries, sHideSummaries)
{
    var hiddenInput = document.getElementById(hiddenInputID);
    var summaryLink = document.getElementById(summaryLinkID);
    var divElements = document.getElementsByName('trSummary');
    var iCellItemCount = divElements.length;   
 
    for (iCount=0; iCount<iCellItemCount; iCount++)
	{
        divElements[iCount].style.display = "none";
    }
    
    summaryLink.innerHTML = "<b>" + sShowSummaries + "</b>";
	hiddenInput.value = 'hide';	
}

/*******************************************************************************
' Function Name		fnMaintainScope				
'
' Description		This function maintains the selected scope
'*******************************************************************************/
function fnMaintainScope()
{
    var Scope =	fnGetQval('s');
    Scope = Scope.replace('+',' ');
    Scope = unescape(Scope);       
    
    var tagname = document.getElementsByTagName('select');    
    
    for (j=0; j<tagname.length; j++)
    {   if(tagname[j].id.indexOf('SBScopes') == -1)
        {   
        	continue;
        }    
        else
        {   
        	for(i=0; i<tagname[j].options.length; i++)
            {   
            	if(tagname[j].options[i].value == Scope)
                {   
                	tagname[j].selectedIndex = i;
                    break;
                }
            }
        }        
    } 
    return;   
}

/*******************************************************************************
' Function Name		fnOpenSearchResults				
'
' Description		This function displays the search results
'*******************************************************************************/
function fnOpenSearchResults(ddlScope, txtSearchKeyword, sSearchPageResultURL, sKeywordError)
{   
    var tagname = document.getElementsByTagName('select');
    var keyword = document.getElementsByTagName('input');
    var sScope,sKeyword;
    
    for(i=0; i<keyword.length; i++)
    {   
    	if(keyword[i].id.indexOf(txtSearchKeyword)!= -1)
        {   
        	sKeyword = keyword[i].value;
            break;
        }        
    }
    
    sKeyword = fnTrim(sKeyword);
    
    if (sKeyword =='' || sKeyword.length==0)
    {   
    	alert (sKeywordError);
        return;
    }
    
    for (j=0; j<tagname.length; j++)
    {   
    	if(tagname[j].id.indexOf(ddlScope)!= -1)
        {   
        	sScope = tagname[j].value;
            break;
        }
    } 
    
    if (sScope.indexOf('Corporate Site') != -1)
    {   
    	window.open('http://www.eastman.com/Search/Pages/Search_Results.aspx' + '?k=' + sKeyword + '&s=All%20Sections','_blank');        
    }
    else
    {   
    	window.open(sSearchPageResultURL + '?k=' + sKeyword + '&s=' + sScope,'_self');        
    }
}

/*******************************************************************************
' Function Name		fnOpenSearchResultsOnEnterKey				
'
' Description		This function displays the search results on click of enter key
'*******************************************************************************/
function fnOpenSearchResultsOnEnterKey(ddlScope, txtSearchKeyword, sSearchPageResultURL, sKeywordError, e)
{
    var characterCode;
    if (e && e.which)
    {   
    	// Firefox specific code
        e = e;
        characterCode = e.which;                            
    }
    else
    {   
    	// IE specific code       
        e = event;      
        characterCode = e.keyCode;        
    }
    
    if (characterCode == 13)
    {   
        if (window.event)
        {   
        	event.returnValue=false;
            event.cancel = true;
        }
        else
        {   
        	// Firefox specific code
            e.preventDefault(); 
        }
        fnOpenSearchResults(ddlScope, txtSearchKeyword, sSearchPageResultURL);
    }   
}

/*******************************************************************************
' Function Name		fnGetQval				
'
' Description		This function returns a particular query string value
'*******************************************************************************/
function fnGetQval(name)
{
    var m, Q = window.location.search.substring(1);
    
    if ('' != Q)
    {
        var re = new RegExp(escape(name) + '=([^&$]+)');        
        if (m = Q.match(re))
        {   
        	return m[1];
        }
        else
        {   
        	return '';
        }
    }    
    return '';
}

/*******************************************************************************
' Function Name		fnTrim				
'
' Description		This function returns a trimmed string removing trailing spaces
'*******************************************************************************/
function fnTrim(str)
{
	return str.replace(/^\s+|\s+$/g, '');
}
