/*Copyright Bridgeline Software, Inc. An unpublished work created in 2007. All rights reserved. 
This software contains the confidential and trade secret information of Bridgeline Software, Inc. 
("Bridgeline").  Copying, distribution or disclosure without Bridgeline's express written permission is prohibited.*/

/*************************** Browser Window Size and Position****************/
/*copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005*/
function pageWidth() {
    return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 
   
function pageHeight() {
    return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 

function posLeft() {
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
} 

function posTop() {
    return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
} 

function posRight() {
    return posLeft()+pageWidth();
} 
function posBottom() {
    return posTop()+pageHeight();
}

/*************************** Scroll height & width****************/
function getPageHeightWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight + document.body.offsetTop;
  	}
	return yWithScroll;
}

function getPageWidthWithScroll(){
	if (window.innerWidth && window.scrollMaxX) {// Firefox
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		xWithScroll = document.body.offsetWidth + document.body.offsetLeft;
  	}
	return xWithScroll;
}

/*************************** Image Swap, preload and rollover functions ****************/
function MM_swapImgRestore() 
{ //v3.0
  var i,x,a=document.MM_sr; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() 
{ //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d)
 { //v4.01
  var p,i,x;  if(!d) d=document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) 
  {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() 
{ //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
/*************************** Image Swap, preload and rollover functions ****************/

/*************************** MainMenu ****************/
function toggleState(curElem) {
	var navElem = document.getElementById('menuItems');
	
	if (navElem) {
		var arrNavItems = navElem.getElementsByTagName("A");
		//remove any "selected" class from each menu item
		for(i=0;i<arrNavItems.length;i++) {
			arrNavItems[i].className = arrNavItems[i].className.replace('selected', '');
		}
	}
	//add "selected" to the clicked item
	curElem.className += " selected";
}
/*************************** MainMenu ****************/

/*************************** Start Script for dynamic Accordion Style  ****************/

function addEvent(elm, evType, fn, useCapture) {
	if(elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} 
	else if (elm.attachEvent){
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else{
		elm['on' + evType] = fn;
	}
}

function findAccordions() {
	var bulletLists = document.getElementsByTagName("ul");
	var accordionList;
	var accordionListItems;
	var firstChildElem;
	var DOMwalker;
	for (i=0;i<bulletLists.length;i++) {
		if (bulletLists[i].className.indexOf("accordion")!=-1) {
			accordionList = bulletLists[i];
			accordionListItems = accordionList.getElementsByTagName("li");
			for(j=0;j<accordionListItems.length;j++) {
			    accordionListItems[j].onclick = doExpand;
				if(accordionListItems[j].hasChildNodes()) {
				    if(document.createTreeWalker) {
				        DOMwalker = document.createTreeWalker(accordionListItems[j], NodeFilter.SHOW_ELEMENT, null, false);
				        firstChildElem = DOMwalker.firstChild();
				    }else{
				        firstChildElem = accordionListItems[j].firstChild;
				    }
				    if(firstChildElem && firstChildElem.tagName) {
				        firstChildElem.style.cursor="pointer";
				    }		       
			    }  
			}
		}
	}
}
function doExpand(e) 
{
    if (window.event) window.event.cancelBubble = true;
    else e.stopPropagation();    
    
    var listItems;
    if (this.parentNode.className.indexOf("accordion")!=-1)
    {
        listItems = this.parentNode.getElementsByTagName("li");
        for(m=0;m<listItems.length;m++) {
		    listItems[m].className = "";
		}
    }
    this.className = "expanded";
}

addEvent(window, 'load', findAccordions, false);

/*************************** End Script for dynamic Accordion Style  ****************/


/*
CSS Browser Selector v0.2.7
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
var css_browser_selector = function() {var ua=navigator.userAgent.toLowerCase(),is=function(t){return ua.indexOf(t) != -1;},h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;}();

/*************************** Pop up a modal window  ****************/

function showModal(pageUrl, height, width) {

	var custompagesFolder="";   // <-----edit this
	
	//Added by Avinash for IE
	var browser=navigator.appName;
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
    
    
	var tempIframe = document.createElement('iframe');
	tempIframe.id = "modal";
	tempIframe.name = "modal";
	tempIframe.frameBorder = "0";
	tempIframe.scrolling="no";
	tempIframe.height = "1";
	tempIframe.width = "1";
	tempIframe.style.position="absolute";	
	//tempIframe.style.overflow = "hidden";
	tempIframe.allowTransparency=true;	
	//tempIframe.src = custompagesFolder+ pageUrl;
	tempIframe.src = pageUrl;	
	document.body.appendChild(tempIframe);
	//return false;
}
function CenterModal(width,height){
	var elementId = "modal";
	var visibleHeight = pageHeight();
	var visibleWidth = pageWidth();
	element = document.getElementById(elementId);
	
	//alert("width="+element.offsetWidth);
	if(element)
	{
	element.width = width;
	element.height = height+0;
	if(element.height > visibleHeight){
		element.style.top = "20px";
	}else{
		element.style.top = (visibleHeight-element.height)/2 + "px";
	}
	element.style.left = (visibleWidth-element.width)/2 + "px";
	
	var totalHeight = getPageHeightWithScroll();
	var totalWidth = getPageWidthWithScroll();
	//alert("elementHeight="+element.height+"   total="+totalHeight);
	if(!document.getElementById('coverLayer')){
	    var tempCoverDiv = document.createElement('div');
	    tempCoverDiv.id = "coverLayer";
	    tempCoverDiv.style.height=totalHeight+"px";
	    tempCoverDiv.style.width=totalWidth+"px";
		document.body.appendChild(tempCoverDiv);
		getScrollXY();
	    parent.window.scrollTo(0,0);
	}
	 var wrapper=document.getElementById('wrapper');
	  if(wrapper){
	    setTimeout(function(){     
        element.height=(wrapper.offsetHeight) + "px";},50);
    }
}	
}
function closeModalWindow() {
	var tempIframe = document.getElementById('modal');
	var tempCoverDiv = document.getElementById('coverLayer');
	if(tempIframe) tempIframe.parentNode.removeChild(tempIframe);
	if(tempCoverDiv) tempCoverDiv.parentNode.removeChild(tempCoverDiv);
	parent.window.scrollTo(xPos,yPos);	
}
var xPos = 0, yPos = 0;
function getScrollXY() {
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        xPos = window.pageXOffset;
        yPos = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        xPos = document.body.scrollLeft;
        yPos = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        xPos = document.documentElement.scrollLeft;
        yPos = document.documentElement.scrollTop;
    }
}
/*************************** End pop up a modal window  ****************/

function hideElement(contentId, btnId){
	contentElement = document.getElementById(contentId);
	btnElement = document.getElementById(btnId);		
	if(contentElement.className=="displayNone")
	{
	    if(btnElement != null)btnElement.innerHTML = '<a href="#" onclick="hideElement(\''+contentId+'\',\''+btnId+'\'); return false;"><img src="/images/minusIcon.gif" alt="Collapse" /></a>';
		contentElement.className="displayBlock";	
		if(contentId=="newsgistContent")
		{
		    document.getElementById("pressgistContent").className="displayNone";
		    document.getElementById("hide2").innerHTML = '<a href="#" onclick="hideElement(\'pressgistContent\',\'hide2\'); return false;"><img src=" /images/plusIcon.gif" alt="Expand" /></a>';;
		}
		else if(contentId=="pressgistContent")
		{
		    document.getElementById("newsgistContent").className="displayNone";
		    document.getElementById("hide1").innerHTML = '<a href="#" onclick="hideElement(\'newsgistContent\',\'hide1\'); return false;"><img src=" /images/plusIcon.gif" alt="Expand" /></a>';;
		}
	}
	
	else if(contentElement.className=="displayBlock")
	{
		if(btnElement != null)btnElement.innerHTML = '<a href="#" onclick="hideElement(\''+contentId+'\',\''+btnId+'\'); return false;"><img src=" /images/plusIcon.gif" alt="Expand" /></a>';
		contentElement.className="displayNone";
	}
}

function expandList(listId, btnId){
	contentElement = document.getElementById(listId);
	btnElement = document.getElementById(btnId);
	//alert("style="+contentElement.style.display);
	if(contentElement.style.display == "none" || contentElement.style.display == ""){
		btnElement.setAttribute("src","/images/minus.jpg");
		btnElement.setAttribute("alt","Hide");
		contentElement.style.display = "block";
	}else{
		btnElement.setAttribute("src","/images/plus.jpg");
		btnElement.setAttribute("alt","Show");
		contentElement.style.display = "none";
	}
}
/*********************for radio button alert messages****************/
function openPopup(testRadio)
{
        var n=findnextSiblingHyperLink(testRadio);
        var strHref=n.href;
        if(strHref)
        {
            if((strHref.search("#"))>=0)
                strHref=testRadio.value;
             
            if(strHref.search("javascript:showLink")>=0)
            {
                strHref=strHref.replace("javascript:showLink('","")                
                strHref=strHref.substring(0,strHref.indexOf("','_"));
                window.scrollTo(0,0);               
                showModal(strHref);
            }
        }
}
function findnextSiblingHyperLink(testRadio)
{
 var n = testRadio;
    do n = n.nextSibling;
    while (n && n.nodeType != 1 && n.tagName!='A');
    return n;
}
function findprevSiblingRadio(testRadio)
{
    var p = testRadio;
    do p = p.previousSibling;
    while (p && p.nodeType != 1 && p.tagName!='radio');
    return p;
}

function whichElement(e)
{
    var targ;
    if (!e)
      var e=window.event;
    
    if (e.target)
        targ=e.target;        
    else if (e.srcElement)
     targ=e.srcElement;
        
    if (targ.nodeType==3) // defeat Safari bug
        targ = targ.parentNode;
     
    var tname;
    tname=targ.tagName;

    if(tname=='A')
    {
       var p=findprevSiblingRadio(targ)
       
            if((p.value.search("javascript:showLink"))==-1)
             {   p.value=targ.href;
                targ.href="#";
              }
        
    }

}
/*********************for radio button alert messages****************/

function ShareWithColleague(shareLink)
{
    
    
	var txtFile = false
	
	if (window.XMLHttpRequest) // if Mozilla, IE7, Safari etc
		txtFile = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE6 or below
		try {
		txtFile = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try{
			txtFile = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
    
    txtFile.open("GET", "/File Library/Site Configurations/ProductMailMsg.txt", true);
    txtFile.onreadystatechange = function() 
    {
        if (txtFile.readyState === 4) 
        {  // Makes sure the document is ready to parse.
            if (txtFile.status === 200) 
            {  // Makes sure it's found the file.
                message = txtFile.responseText; 
                message += "%0D%0A%0A";
                message += shareLink;
    
                window.location = "mailto:?subject=SunChemical General Information&body=" + message;              
            }
        }
    }
    
    txtFile.send(null);
    return false;
}

function mailPressRelease(shareLink)
{
    var message;
    
    var txtFile = false
	
	if (window.XMLHttpRequest) // if Mozilla, IE7, Safari etc
		txtFile = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE6 or below
		try {
		txtFile = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try{
			txtFile = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	
    txtFile.open("GET", "/File Library/Site Configurations/mailPressMsg.txt", true);
    txtFile.onreadystatechange = function() 
    {
        if (txtFile.readyState === 4) 
        {  // Makes sure the document is ready to parse.
            if (txtFile.status === 200) 
            {  // Makes sure it's found the file.
                message = txtFile.responseText; 
                message += "%0D%0A%0A";
                message += shareLink;
    
                window.location = "mailto:?subject=SunChemical Press Information&body=" + message;              
            }
        }
    }
    
    txtFile.send(null);
    return false;
}
function mailCaseStudy(shareLink)
{
    var message;
    
    var txtFile = false
	
	if (window.XMLHttpRequest) // if Mozilla, IE7, Safari etc
		txtFile = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE6 or below
		try {
		txtFile = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try{
			txtFile = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	
    txtFile.open("GET", "/File Library/Site Configurations/mailCaseStudyMsg.txt", true);
    txtFile.onreadystatechange = function() 
    {
        if (txtFile.readyState === 4) 
        {  // Makes sure the document is ready to parse.
            if (txtFile.status === 200) 
            {  // Makes sure it's found the file.
                message = txtFile.responseText; 
                message += "%0D%0A%0A";
                message += shareLink;
    
                window.location = "mailto:?subject=SunChemical Case Study Information&body=" + message;              
            }
        }
    }
    
    txtFile.send(null);
    return false;
}

// function resizeText(multiplier) 
// { 
//    var c = document.getElementById("SplitText"); 
//    if (c.style.fontSize == "")
//      { 
//        c.style.fontSize = "1.0em"; 
//      } 
//    c.style.fontSize = parseFloat(c.style.fontSize) + (multiplier * 0.2) + "em";
//} 

function replaceAll( str, from, to ) 
{
    var idx = str.indexOf( from );

    while ( idx > -1 ) 
    {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }

    return str;
}

function reCase(str)
{
	var M= str.match(/(< *\w+)/g);
	var L= M.length;
	var temp='';
	for (var i=0; i<L;i++)
	{
		var tem= M[i].substring(1);
		tem=tem.toLowerCase();
				
		if(temp.search(tem+';')!=-1)continue;
		var rx= new RegExp("(< *\/? *)"+tem,'gi');
		temp+= tem+'; '
		str= str.replace(rx,'$1'+tem);
	}
	str=str.replace(/> *</g,'>\n<');	
	return str;
}
 
/* Javascript Pagination Start*/
var contents = new Array();
var nav1, nav2,splittext;
var contentBlock;

function BuildContent(container)
{
    pageNumber = 0;
    var html = reCase(container.innerHTML);
    try
    {
        if (extraContainer.innerHTML != "")
            html = html + reCase(extraContainer.innerHTML);
    }
    catch(e){}
    html = replaceAll(html, "class=pagebreak", 'class="pagebreak"');
    contents = CreateArray(html);
    DisplayContent(1);
}

function DisplayContent(pageNum)
{
    pageNumber = parseInt(pageNum);    
    if (nav1)
        nav1.innerHTML = GetNumericLinks(pageNumber, contents.length)
    if (nav2)
        nav2.innerHTML = nav1.innerHTML;
    
    contentBlock.innerHTML = contents[pageNumber - 1];
   
    if(contents.length < 2)
    {     
        if (nav1)
            nav1.innerHTML="";
        if (nav2)
            nav2.innerHTML="";
    }
    return false;
}

function GetNumericLinks(currentPage, totalPages)
{
    var result = "";

    if (currentPage == 1)
        result += "< prev <a class='prev inactive' href='#' onclick='return false;'></a>";            
    else
        result += "<a class='prev' href='#' onclick=\"return DisplayContent(" + (currentPage - 1) + ");\">< prev</a>";

    for (count = 1; count <= totalPages; count++)
    {
        if (currentPage == count)
            result += "<span class='current'>" + count + "</span>";
        else
            result += "<a href='#' onclick=\"return DisplayContent(" + count + ");\">" + count + "</a>";                    
    }

    if (currentPage == totalPages)
        result += "<a class='next inactive' href='#' onclick='return false;'></a> next >";
    else
        result += "<a class='next' href='#' onclick=\"return DisplayContent(" + (currentPage + 1) + ");\"> next ></a>";

    return result;
}

function CreateArray(content) 
{
    var content;
    var count = 0;    
    var txtStart = 0;
    var contentArray = new Array();
    var breakPos;
    var breakCharCount = "";
   
    while(Trim(content) != "") 
    {
        breakPos = content.toLowerCase().indexOf('<p class="pagebreak">&nbsp;</p>');
            
        if (breakPos > -1)
            contentArray[count] = Trim(content.substring(0, breakPos));
        else
            contentArray[count] = Trim(content);
        
        txtStart = contentArray[count].length;
        content = Trim(content.substring(txtStart + 33, content.length));
        count = count + 1;            
    }    
    
    return contentArray;
}

function LTrim(str)
{
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...

    var j=0, i = s.length;

    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;


    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }

  return s;
}

function RTrim(str)
{
  // We don't want to strip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...

    var i = s.length - 1;       // Get length of string

    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;


    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }

  return s;
}

function Trim(str)
{
  return RTrim(LTrim(str));
}
/* Javascript Pagination End*/

function hideImageContainer()
{
  var imageid=document.getElementById("imgContainerDiv");   
  if(!document.getElementById("autohide") || !document.getElementById("jsInclude"))
   {  
       if(navigator.appName.indexOf("Microsoft")>=0 && imageid.childNodes[0].childNodes[0]!=null)
        {  
            if(imageid.childNodes[0].childNodes[0].getAttribute("src").indexOf("Image%20Library")<0)
            {
               imageid.style.display="none"
            }  
        } 

        if(navigator.appName.indexOf("Netscape")>=0)
        {
            for( i=1; i<=imageid.childNodes.length; i++)   
            {  
                if(imageid.childNodes[i]!=null && imageid.childNodes[i].nodeName=='DIV')
                {                 
                  for( j=1; j<imageid.childNodes[i].childNodes.length; j++ )   
                    {                                         
                      if(imageid.childNodes[i].childNodes[j].nodeName=="IMG" && 
                      imageid.childNodes[i].childNodes[j].getAttribute("src").indexOf("Image%20Library")<0 ) 
                      {
                        imageid.style.display="none"
                         break;
                      }                      
                    }                   
                }               
            }            
        }
    }
 } 
 
 
 function controlCenter (btnId, evt) 
{   
    btn = document.getElementById(btnId);
    if (btn)
    {
        if (evt.keyCode == 13)
        {
            evt.returnValue = false;
            evt.cancel = true;
            btn.click();
            return false;
        } 
    } 
}

function storeInCookie(URL, key, ParentIndexId)
{
  document.cookie = "key=" + key  + ";path=/";
  document.cookie = "ParentIndexId=" + ParentIndexId + ";path=/";
  window.location.href = URL;
}
