// The following variables are used to control the scrolling news

var currTop;
var topStopPoint;
var refTop;
var clipRef;
var paused;
var scrollId = 0;

// used to allow the subMenus to timeout after a specified period 
// and become invisible again
var timeOutId = 0;;

// The following variables are used to maintain information specific
// to the browser being used
var userAgentString;
var browser;
var layerRef;
var layerRefClose = '")';
var styleSwitch =".style";
var pxSwitch ="px";

// Called from every page each time it is loaded.  Identifies the 
// browser and sets up utility variables correspondingly.
function init() {

  userAgentString=navigator.userAgent.toLowerCase();  
  if (!document.all && document.getElementById) {            
    // Netscape 6
    browser = "ns6";
    layerRef='document.getElementById("';
	// include the appropriate stylesheet
	document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ns.css" type="text/css">');
  } 
  else if (userAgentString.indexOf("mozilla") == 1) {    
      browser = "mozilla";
      layerRef='document.getElementById("';
      document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ns.css" type="text/css">');  
      } else {
      if (userAgentString.indexOf("msie") != -1) {
        if (userAgentString.indexOf("msie 8") == 1) {
        // I.E 7
        browser = "ie8";
        layerRef='document.getElementById("';
        }
        else if (userAgentString.indexOf("msie 7") == 1) {
        // I.E 7
        browser = "ie7";
        layerRef='document.getElementById("';
        } else if (userAgentString.indexOf("msie 6") == 1) {
        // I.E 6
        browser = "ie6";
        layerRef='document.getElementById("';
        } else if (userAgentString.indexOf("msie 4") == -1) {
        // yes IE but not IE4
        browser = "ie5";
        layerRef='document.getElementById("';
        // include the appropriate stylesheet
        // notes..
        // absolute paths begin with a forward slash & entire path from root to file must be defined.
        // document.write('<link rel="stylesheet" href="/Common/Style/chpowell_ie.css" type="text/css">');  	    
        // relative paths up one directory being with "../" defines a relative path up one directory
        // document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ie.css" type="text/css">');  	    
        // relative paths up two directories "../../" defines a relative path up two directories..
        // document.write('<link rel="stylesheet" href="../../Common/Style/chpowell_ie.css" type="text/css">');  	    
        document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ie.css" type="text/css">');  	    
	    } else {
	    // I.E 4
          browser = "ie4";
          layerRef='document.all("';	
	      // include the appropriate stylesheet
	      document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ie.css" type="text/css">');  
          }
         } else {
         document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ie.css" type="text/css">');  
         // Browser is not supported (most likely NS 4.x)
         document.location.href = "../Common/NotSupported.jsp"	    
      }
    }

}

// Used on the rate central page to validate that if the user selects
// "other" as the company type they fill in the corresponding text
// box on the page.
function sendQuoteRequest() {
    document.raterequestform.submit();  
  }

// Used on the rate central page to validate that if the user selects
// "other" as the company type they fill in the corresponding text
// box on the page.
function validateCompanyType() {

  if (document.raterequestform.companyType[5].checked) {       
    if (eval(layerRef+'companyTypeOther'+layerRefClose+'.value == ""')) {  
  	  alert("Please specify a Company Type or select a different option");
    } else {
      document.raterequestform.submit();  
    }
  } else {
    document.raterequestform.submit();  
  }
}

// Used on the rate central page to validate that if the user selects
// "other" as the company type they fill in the corresponding text
// box on the page.
function validateRateRequest() {
    document.raterequestform.submit();  
}


function toggleBackgroundColor (option, type, object) {

  var page;
  var component;
  var subMenu;

  component = type;
  subMenu = "SubMenu_" + component;
  page = document.location.href;
  page = page.substring(page.lastIndexOf("/") + 1, page.indexOf(".jsp"));     

  switch (page) {
    case "RateCentral":
    case "RateConfirm":
    case "RatePreview":
    case "PowellDistributionSystems":
    case "Error":
      page = "Tools";
      break;
    case "IndustryNews":
    case "UsefulLinks":
      page = "Resources";
      break;
  }

  if (option == "selected") {
    if (type != "") {
      if (type == "escape") {
        resetAllComponents(page);      
      } else if (type != "Home") {      

        switch (page) {
          case "Home" :
            eval(layerRef+"SubMenu_Services_Top"+layerRefClose+styleSwitch+'.top = 80');            
		    eval(layerRef+"SubMenu_Services"+layerRefClose+styleSwitch+'.top = 96');
		    eval(layerRef+"SubMenu_Company_Top"+layerRefClose+styleSwitch+'.top = 98');
		    eval(layerRef+"SubMenu_Company"+layerRefClose+styleSwitch+'.top = 114');
		    eval(layerRef+"SubMenu_Resources_Top"+layerRefClose+styleSwitch+'.top = 116');
		    eval(layerRef+"SubMenu_Resources"+layerRefClose+styleSwitch+'.top = 132');		    
		    break;
          case "Services" :
		    eval(layerRef+"SubMenu_Company_Top"+layerRefClose+styleSwitch+'.top = 177');
		    eval(layerRef+"SubMenu_Company"+layerRefClose+styleSwitch+'.top = 193');		    		    
		    eval(layerRef+"SubMenu_Resources_Top"+layerRefClose+styleSwitch+'.top = 195');		    
		    eval(layerRef+"SubMenu_Resources"+layerRefClose+styleSwitch+'.top = 211');		    
		    break;
          case "Company" :
            eval(layerRef+"SubMenu_Services_Top"+layerRefClose+styleSwitch+'.top = 80');		    
            eval(layerRef+"SubMenu_Services"+layerRefClose+styleSwitch+'.top = 96');		    
            eval(layerRef+"SubMenu_Resources_Top"+layerRefClose+styleSwitch+'.top = 173');		    
            eval(layerRef+"SubMenu_Resources"+layerRefClose+styleSwitch+'.top = 187');		    
		    break;		  		  		        
          case "Resources" :
            eval(layerRef+"SubMenu_Services_Top"+layerRefClose+styleSwitch+'.top = 80');		    
            eval(layerRef+"SubMenu_Services"+layerRefClose+styleSwitch+'.top = 96');		    
            eval(layerRef+"SubMenu_Company_Top"+layerRefClose+styleSwitch+'.top = 98');		    
            eval(layerRef+"SubMenu_Company"+layerRefClose+styleSwitch+'.top = 114');		    
		    break;		  		  		        		  
          case "Tools" :
            eval(layerRef+"SubMenu_Services_Top"+layerRefClose+styleSwitch+'.top = 80');            
		    eval(layerRef+"SubMenu_Services"+layerRefClose+styleSwitch+'.top = 96');
		    eval(layerRef+"SubMenu_Company_Top"+layerRefClose+styleSwitch+'.top = 98');
		    eval(layerRef+"SubMenu_Company"+layerRefClose+styleSwitch+'.top = 114');
		    eval(layerRef+"SubMenu_Resources_Top"+layerRefClose+styleSwitch+'.top = 116');
		    eval(layerRef+"SubMenu_Resources"+layerRefClose+styleSwitch+'.top = 132');		    
		    break;
        } // switch
        
	    resetAllComponents(page);
        eval(layerRef+'MainPage'+layerRefClose+'.zIndex = 1');
        eval(layerRef+subMenu+layerRefClose+'.zIndex = 2');
        eval(layerRef+subMenu + '_Top'+layerRefClose+'.zIndex = 2');        
	    eval(layerRef+component + '_Left'+layerRefClose+'.className = "navTrailMouseOver"');		        
        eval(layerRef+component+layerRefClose+'.className = "leftNavMouseOver"');       
        eval(layerRef+subMenu + '_Top'+layerRefClose+styleSwitch+'.visibility = "visible"');      
        eval(layerRef+subMenu+layerRefClose+styleSwitch+'.visibility = "visible"');      
		setTimeout('resetMenu("'+page+'", "'+timeOutId+'")', 5000);

      } else {
  	    resetAllComponents(page);
        eval(layerRef+component+ '_Left'+layerRefClose+'.className = "navTrailMouseOver"');		        
      }
    }    
  } else {
    if (type != "") {     
      if (type == "escape") {
        resetAllComponents(page);      
      } else {        
	    resetAllComponents(page);      
        eval(layerRef+"MainPage"+layerRefClose+'.zIndex = 1');     
        eval(layerRef+subMenu+layerRefClose+'.zIndex = 0');     
        eval(layerRef+subMenu + '_Top'+layerRefClose+styleSwitch+'.visibility = "hidden"');      
        eval(layerRef+subMenu+layerRefClose+styleSwitch+'.visibility = "hidden"');  
      }
    }    
  }

}

// Called when a specified period has elapsed after a submenu is activated.  
// The id parameter is unique to each instance of a subMenu being activated
// and is used to validate whether the subMenu in question is still visible.
function resetMenu(activePage, id) {

  if (timeOutId == id) {
    timeOutId = 0;
    resetAllComponents(activePage);
  }
  
}

// Takes an activePage argument that indicates which page
// the user is currently viewing.  All submenus and navigational
// items are reset to the corresponding starting point for that 
// page.
function resetAllComponents(activePage) {
    
    timeOutId += 1;
    
    eval(layerRef+"MainPage"+layerRefClose+'.zIndex = 1');     

    eval(layerRef+"Services"+layerRefClose+'.className = "leftNav"');
    eval(layerRef+"Services_Left"+layerRefClose+'.className = "navTrail"');		                
    eval(layerRef+"SubMenu_Services_Top"+layerRefClose+'.zIndex = 0');     
    eval(layerRef+"SubMenu_Services_Top"+layerRefClose+styleSwitch+'.visibility = "hidden"');  
    eval(layerRef+"SubMenu_Services"+layerRefClose+'.zIndex = 0');     
    eval(layerRef+"SubMenu_Services"+layerRefClose+styleSwitch+'.visibility = "hidden"');  

    eval(layerRef+"Company"+layerRefClose+'.className = "leftNav"');
    eval(layerRef+"Company_Left"+layerRefClose+'.className = "navTrail"');		                  
    eval(layerRef+"SubMenu_Company_Top"+layerRefClose+'.zIndex = 0');     
    eval(layerRef+"SubMenu_Company_Top"+layerRefClose+styleSwitch+'.visibility = "hidden"');  
    eval(layerRef+"SubMenu_Company"+layerRefClose+'.zIndex = 0');     
    eval(layerRef+"SubMenu_Company"+layerRefClose+styleSwitch+'.visibility = "hidden"');  

    eval(layerRef+"Resources"+layerRefClose+'.className = "leftNav"');
    eval(layerRef+"Resources_Left"+layerRefClose+'.className = "navTrail"');		                
    eval(layerRef+"SubMenu_Resources_Top"+layerRefClose+'.zIndex = 0');     
    eval(layerRef+"SubMenu_Resources_Top"+layerRefClose+styleSwitch+'.visibility = "hidden"');  
    eval(layerRef+"SubMenu_Resources"+layerRefClose+'.zIndex = 0');     
    eval(layerRef+"SubMenu_Resources"+layerRefClose+styleSwitch+'.visibility = "hidden"');    

    if ((activePage != "Tools") && (activePage != "Home")) {
      eval(layerRef+activePage + '_Left'+layerRefClose+'.className = "navTrailSelected"');
      eval(layerRef+activePage+layerRefClose+'.className = "leftNavSelected"');
    }
  
}

// Called from the "Useful Links" page.  Ensures that a valid 
// selection was made before opening a new browser with the 
// user's selection.
function activateLink (destinationAddress) {
	
  if (
      (destinationAddress != "- Please select a link from the list below -") &&
      (destinationAddress != "")     
     ) {
    window.open(destinationAddress,'linkFromCHPowell','');
  } else {
    alert("Please select a link from the drop down list");
  }

}

// Called from the onLoad() event on the home page.  Initializes
// the variables used to control the scrolling text and starts
// the scrolling process.
function initializeScrollingText() {     

  if (browser == "ns6"){
    refTop = 470;   // 345;
  } else {
    refTop = 485;  // 360;
  }
  clipRef = 350;  // 145
  currTop = refTop;
  topStopPoint = currTop -75;   
  paused = "false";
  scrollId = 0; 
  setTimeout('scrollText("'+scrollId+'")', 5000); 
   
}
  
// Moves the scrollingText layer up by 1 pixel.  If the scrollingText
// layer has reached its end point a call to fadeIn() is made
// which restarts the process.
function scrollText(id) {     

  if ((paused == "false") && (id == scrollId)) {
    if (eval(layerRef+'scrollingNewsArticle'+layerRefClose+styleSwitch+'.top != topStopPoint+pxSwitch')) {     
      eval(layerRef+'scrollingNewsArticle'+layerRefClose+styleSwitch+'.top = currTop');
      currTop -= 1;
      eval(layerRef+'scrollingNewsArticle'+layerRefClose+styleSwitch+'.clip = "rect(" + "auto" + " " + "auto" + " " + clipRef + pxSwitch + " " + "auto" + ")"');
      clipRef += 1;
      scrollId = 0;
      setTimeout('scrollText("'+scrollId+'")',150);
    } else {    
      clipRef = 0;
      fadeIn(refTop + 200);
    }     
  }
 
}  

function pauseScrollingText() {

  if (paused == "false") {  
    paused = "true";
  } 

}

function resumeScrollingText() {

  if (paused == "true") {  
    paused = "false";
    scrollId +=1;
    setTimeout('scrollText("'+scrollId+'")', 1000);    
  }

}

// Moves the scrollingText layer from the bottom of the news box
// to the top at the end of an iteration.
function fadeIn(topValue) {

  paused = "true";
  if (topValue > refTop) {
    eval(layerRef+'scrollingNewsArticle'+layerRefClose+styleSwitch+'.top = topValue');
    eval(layerRef+'scrollingNewsArticle'+layerRefClose+styleSwitch+'.clip = "rect(" + "auto" + " " + "auto" + " " + clipRef + pxSwitch + " " + "auto" + ")"');    
    topValue-= 5;
    clipRef += 5;
    setTimeout('fadeIn("'+topValue+'")', 1);
  } else {
    initializeScrollingText();
  }

}

// Called from the MouseOver() events on the "tools" bar along the top
// of every page.  Simply changes the style of the appropriate tool.
function switchTool (Tool, Action) {

  if(Tool == "Services" || Tool == "Resources" || Tool == "AboutUs")
  {     
      if (Action == "In") {
        eval(layerRef+Tool+layerRefClose+'.className = "toolSelected"');
      }
  }
  else 
    if(Tool == "Home" || Tool == "WarehouseTracking" || Tool == "Rates" || Tool == "AMS" || Tool == "ISF")
  {
      if (Action == "In") {
        eval(layerRef+Tool+layerRefClose+'.className = "toolSelected"');
      } 
      else {
        eval(layerRef+Tool+layerRefClose+'.className = "toolUnselected"');    
      }  
  }
   else
     if(Tool == "Manifest")
  {
      if (Action == "In") {
        eval(layerRef+Tool+layerRefClose+'.className = "toolSelected"');
      } 
      else {
        eval(layerRef+Tool+layerRefClose+'.className = "toolUnselected"');    
      }  
  }

  else if (Tool == "GlobalNetwork")
  {
      if (Action == "In") {
        eval(layerRef+Tool+layerRefClose+'.className = "toolSelectedRight"');
      } 
      else {
        eval(layerRef+Tool+layerRefClose+'.className = "toolUnselectedRight"');    
      }
  } 
  toggleHeaderMenu(Action, Tool)	

} // Switch tool

function toggleHeaderMenu (action, tool) {

  var page;
  var subMenu;

  subMenu = "SubMenuHeader_" + tool;


  if (action == "In")
  {
      if(tool == "AboutUs")
      {
          eval(layerRef+"SubMenuHeader_AboutUs"+layerRefClose+styleSwitch+'.top = 27');
          eval(layerRef+"SubMenuHeader_AboutUs"+layerRefClose+styleSwitch+'.left = 0');
          eval(layerRef+"AboutUs"+layerRefClose+'.className = "toolSelected"');
          eval(layerRef+"SubMenuHeader_AboutUs"+layerRefClose+styleSwitch+'.visibility = "visible"');      
      }
      else if(tool == "Services")
      {
	    eval(layerRef+"SubMenuHeader_Services"+layerRefClose+styleSwitch+'.top = 34');
	    eval(layerRef+"SubMenuHeader_Services"+layerRefClose+styleSwitch+'.left = 0');
        eval(layerRef+"Services"+layerRefClose+'.className = "toolSelected"');
	    eval(layerRef+"SubMenuHeader_Services"+layerRefClose+styleSwitch+'.visibility = "visible"');      
	  }
      else if(tool == "Manifest")
	  {
	    eval(layerRef+"SubMenuHeader_Manifest"+layerRefClose+styleSwitch+'.top = 34');
	    eval(layerRef+"SubMenuHeader_Manifest"+layerRefClose+styleSwitch+'.left = 0');
        eval(layerRef+"Manifest"+layerRefClose+'.className = "toolSelected"');
	    eval(layerRef+"SubMenuHeader_Manifest"+layerRefClose+styleSwitch+'.visibility = "visible"');      
      }
      else if(tool == "Resources")
      {
        eval(layerRef+"SubMenuHeader_Resources"+layerRefClose+styleSwitch+'.top = 34');
        eval(layerRef+"SubMenuHeader_Resources"+layerRefClose+styleSwitch+'.left = 0');
        eval(layerRef+"Resources"+layerRefClose+'.className = "toolSelected"');
        eval(layerRef+"SubMenuHeader_Resources"+layerRefClose+styleSwitch+'.visibility = "visible"');      
      }
  else
  {
            eval(layerRef+"AboutUs"+layerRefClose+'.className = "toolUnSelected"');
            eval(layerRef+"Services"+layerRefClose+'.className = "toolUnSelected"');
            eval(layerRef+"Manifest"+layerRefClose+'.className = "toolUnSelected"');
            eval(layerRef+"Resources"+layerRefClose+'.className = "toolUnSelected"');
            eval(layerRef+"SubMenuHeader_AboutUs"+layerRefClose+styleSwitch+'.visibility = "hidden"');      
            eval(layerRef+"SubMenuHeader_Services"+layerRefClose+styleSwitch+'.visibility = "hidden"');      
            eval(layerRef+"SubMenuHeader_Manifest"+layerRefClose+styleSwitch+'.visibility = "hidden"');      
            eval(layerRef+"SubMenuHeader_Resources"+layerRefClose+styleSwitch+'.visibility = "hidden"');      
  }
  }  // ACTION = IN
  else
  {
	var e = window.event;
	//cross browser compatability
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.nodeName != 'DIV') return;
	//cross browser compatability
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode;
	if (reltg == tg) {
		return;
	}
    else {
        if (tool == "AboutUs") {
            eval(layerRef+"SubMenuHeader_AboutUs"+layerRefClose+styleSwitch+'.visibility = "hidden"');
     	    eval(layerRef+"AboutUs"+layerRefClose+'.className = "toolUnSelected"');
        }
		if (tool == "ServicesMenu") {
            eval(layerRef+"SubMenuHeader_Services"+layerRefClose+styleSwitch+'.visibility = "hidden"');
     	    eval(layerRef+"Services"+layerRefClose+'.className = "toolUnSelected"');
		    }
        else if(tool == "ManifestMenu") {
                 eval(layerRef+"SubMenuHeader_Manifest"+layerRefClose+styleSwitch+'.visibility = "hidden"');
     		     eval(layerRef+"Manifest"+layerRefClose+'.className = "toolUnSelected"');
		     }
        else if(tool == "ResourcesMenu") {
                 eval(layerRef+"SubMenuHeader_Resources"+layerRefClose+styleSwitch+'.visibility = "hidden"');
                 eval(layerRef+"Resources"+layerRefClose+'.className = "toolUnSelected"');
             }
    }
  }

} 
//
// Includes the correct stylesheet for the news articles.
function newsArticlesIncludeCSS () {

  if (browser == "ns6") {            
	document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ns.css" type="text/css">');	
  } else if (browser == "ie5") {    
    document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ie.css" type="text/css">');  	    
  } else  if (browser == "ie4") {
	document.write('<link rel="stylesheet" href="../Common/Style/chpowell_ie.css" type="text/css">');
  }	else {
    // Not Supported
    document.location.href = "../Common/NotSupported.jsp";    
  }

}