/*document.observe("dom:loaded", function() {*/

Event.observe(window, "load", function() {
  if (window.location.hash.substr(1) != parseInt(window.location.hash.substr(1))){
  
  	if(window.location.hash.length > 1 && !IsNumeric(window.location.hash)){
			var theHash = window.location.hash.substr(2);
			if(theHash.indexOf("/")==-1){
				var newSection = theHash;
				var newPage = theHash;
			}else{
				var newSectionPage = theHash.split("/");
				var newSection = newSectionPage[0];
				var newPage = newSectionPage[1];
			}
		
			if(document.getElementById("sessionPage").innerHTML != newPage)	{
				browse(newSection, newPage);
			}
		}
	
	}

});

var current_hash = window.location.hash;

function check_hash() {
    if ( window.location.hash != current_hash) {
       
        if(window.location.hash.length==0){
        	var newSection = "home";
			var newPage = "home";
        }else{
       		
       		
       		
       			current_hash = window.location.hash.substr(2);
               
				if(current_hash.indexOf("/")==-1){
					var newSection = current_hash;
					var newPage = current_hash;
				}else{
					var newSectionPage = current_hash.split("/");
					var newSection = newSectionPage[0];
					var newPage = newSectionPage[1];
				}
		
		}
		if(document.getElementById("sessionPage").innerHTML != newPage)	{
			if(!IsNumeric(newSection)) browse(newSection, newPage);
		}
	}
}

hashCheck = setInterval( "check_hash()", 200 );

function IsNumeric(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

function beginNewScroll(){
	var elements = $('feature1','feature2');
var i;

	for(i=0;i<2;i++){
		if($(elements[i]) && $(elements[i]).style.display == "none") {
			var num = i; 
			break;
		}	
	}
	new Effect.BlindDown(elements[num], {duration: 2.0});
	setTimeout(beginNewScroll,5000);	



}

function showURL(msg,e){

	$('msg').innerHTML = msg;
	
	var unicode=e.keyCode? e.keyCode : e.charCode
	if($('msg').style.display == 'none' && unicode == 100){//if 'd' key is pressed
		new Effect.MoveBy('container', 24, 0, {duration: 0.5, beforeStart: function(){
			new Effect.Appear('msg', {duration: 0.8});
		}});
	}
}

function changeLinks(id){

	/*if(window.location.hash.length > 1){
		var theHash = window.location.hash.substr(2);
		if(theHash.indexOf("/")==-1){
			var newSection = theHash;
			var newPage = theHash;
		}else{
			var newSectionPage = theHash.split("/");
			var newSection = newSectionPage[0];
			var newPage = newSectionPage[1];
		}
		
		if(document.getElementById("sessionPage").innerHTML != newPage)	{
			browse(newSection, newPage);
		}
	}*/
	
		if(document.getElementById("pngType").value == 'pngAlpha'){
		
			/*
 
			Correctly handle PNG transparency in Win IE 5.5 & 6.
			http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.

			*/

			for(var i=0; i<document.images.length; i++)
   			{
      			var img = document.images[i]
      			var imgName = img.src.toUpperCase()
      			if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
         			var imgID = (img.id) ? "id='" + img.id + "' " : ""
         			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
         			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
         			var imgStyle = "display:inline-block;" + img.style.cssText 
         			if (img.align == "left") imgStyle = "float:left;" + imgStyle
         			if (img.align == "right") imgStyle = "float:right;" + imgStyle
         			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
         			var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
         			img.outerHTML = strNewHTML
         			i = i-1
      			}
   			}
		
		}
		
		var divobj=$(id);
		
		var alist=divobj.getElementsByTagName("a"); //array containing the A elements within DIV
	
		for (var x=0; x<alist.length; x++){ //loop through each A element

				if(alist[x].getAttribute("rel") && alist[x].getAttribute("rel").substring(0,5) != "light" && alist[x].getAttribute("rel") != "" && alist[x].className != 'mailto'){
				
					alist[x].setAttribute("href", "#/" + alist[x].getAttribute("rel")); //replace URL with #
				
					alist[x].onclick=function(){
					
						browse(this.getAttribute("rel"), false);
						
					}//end onclick
				
					if(alist[x].getAttribute("rev")){
					
						alist[x].setAttribute("href", "#/" + alist[x].getAttribute("rel") + '/' + alist[x].getAttribute("rev"));
						alist[x].onclick=function(){browse(this.getAttribute("rel"), this.getAttribute("rev"));}//end onclick
					
					}//end if has rev
				
				}//end if has rel attr
				
			/*if(alist[x].className == 'pulse')
				{
					(alist[x]).onmouseover=function(){

						new Effect.Pulsate(this,{duration:2, from:.4, to:1, pulses:3});									
					
					}
				}
				*/
				if(alist[x].className == 'backbutton')
				//can use Builder to add a node here?? 
				{
				
					alist[x].onmouseover=function(){
				
						imgOn('button_back','.jpg');
					
					}
				
					alist[x].onmouseout=function(){
				
						imgOff('button_back','.jpg');
					
					}
				
				}
				
				if(alist[x].className == 'nextbutton')
				{
				
					alist[x].onmouseover=function(){
				
						imgOn('button_next','.jpg');
					
					}
					
					alist[x].onmouseout=function(){
				
						imgOff('button_next','.jpg');
						
					}
				
				}
				
				if(alist[x].className == 'mailto')
				{
				
					//alist[x].href = "mailto: " + alist[x].getAttribute("rel") + '.' + alist[x].getAttribute("rev") + "@utsa.edu";
					alist[x].href = "#";
					alist[x].alt = "email " + alist[x].getAttribute("rel");
					alist[x].title = "email " + alist[x].getAttribute("rel");
					alist[x].onclick = function(){OpenEmail(this.getAttribute("rel"), this.getAttribute("rev"));}
					
					
				}
				
		}//end inner for loop
		
	//}//end if browse function supported
	
}//end function

function IsNumeric(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }



function ql_browse(href){
	if(href.substring(0,7) == "http://"){
		window.location = href;
	}else{
		var splitResult = href.split("/");
		window.location.hash = "/" + splitResult[0] + "/" + splitResult[1];
		browse(splitResult[0],splitResult[1]);
	}
}

function browse(section,page,pagebgcolor,textcolor){

	if(section != document.getElementById('sessionSection').innerHTML){
		new Effect.Morph('container',{ style:'background-color:#' + pagebgcolor + '; color:#' + textcolor + ';'}); 
		new Effect.Morph('fader',{ style:'background-color:#' + pagebgcolor + ';'}); 
		new Effect.Morph('lnav',{ style:'border-color:#' + pagebgcolor + ';'}); 
		var prev = document.getElementById('section').innerHTML; 
		
		
		
		if(Element.hasClassName($(section), 'invisible')) { 
			Element.removeClassName($(section), 'invisible'); 
			Element.addClassName($(section), 'visible'); 
			Element.removeClassName(prev, 'visible'); 
			Element.addClassName(prev, 'invisible'); 
			
			if($('div_' + section + '_body')){		

				document.getElementById('section').innerHTML = section; if(document.getElementById('div_' + prev + '_body')){
					new Effect.Parallel( [ 
						new Effect.BlindDown('div_' + section + '_body', {sync: true}), 
						new Effect.BlindUp('div_' + prev + '_body', {sync: true}) ],
						{duration: 0.8} );
				}else{
					new Effect.BlindDown('div_' + section + '_body');
				}
			}
		}
	}

		document.getElementById("sessionSection").innerHTML = section;
		document.getElementById("sessionPage").innerHTML = page;								
	
		if(page == "biological" || page == "chemical"){		
			loadjscssfile("/safety/js/forms.js", "js");
		}
	
		if(IsNumeric(page)){//gallery
		
			var url = '/safety/includes/pages/content/' + section + '/' + 'page.cfm';
		
		}else{

			if($('div_' + document.getElementById("section").innerHTML + '_body')){
			
				var li_list=$('div_' + document.getElementById("section").innerHTML + '_body').getElementsByTagName("li") //array containing the LI elements within the previous section UL
	
				for (var x=0; x<li_list.length; x++){ //loop through each LI element

					Element.removeClassName(li_list[x].getAttributeNode("id").value, 'selected');
								
					Element.addClassName(li_list[x].getAttributeNode("id").value, 'unselected');
						
				}//end turning off previous 
			
			}//end if 
			
			Element.removeClassName($('li_' + page), 'unselected');
								
					Element.addClassName($('li_' + page), 'selected');
			
			var url = '/safety/includes/pages/content/' + section + '/' + page + '.cfm?newpage=' + page;
			

		}

	if(url != document.getElementById('status').innerHTML){
							
			document.getElementById('status').innerHTML = url;
			
			/*need code to handle nav tracking*/
			new Ajax.Updater('standby', url, {method:'get', asynchronous: true, evalScripts: false, onSuccess: fadeOutIn(section, page, 'innercontent')});
    	
		}//end if the clicked link doesn't lead to this same page
}//end function: browse

function fadeOutIn(section, page, div_id) {
var header_img_id = 'hd_' + page;
	$('fader').style.backgroundColor = $('container').getStyle('background-color');
	$('fader').style.zIndex = 1000;

	var dimensions = $('innercontent').getDimensions();
	var position = $('innercontent').viewportOffset();
	var pageScrollPosition = document.viewport.getScrollOffsets();

	$('fader').style.width = dimensions.width + "px";
	$('fader').style.height = dimensions.height + "px";
	$('fader').style.left = position.left + "px";
	$('fader').style.top = (position.top + pageScrollPosition.top) + "px";
	
	new Effect.Appear('fader', {duration: 0.4, afterFinish: function(){		
	
		if(!$(header_img_id) || page == "biological" || page == "chemical"){ 
							
							window.location = "?section=" + section + "&page=" + page;
						
						}else{
		var newdimensions = $('standby').getDimensions();
		$('fader').style.height = (newdimensions.height + 21) + "px";
		$(div_id).innerHTML = $('standby').innerHTML; 
		pageTracker._trackPageview("/"+section+"/"+page+".cfm");
		changeLinks(div_id);
		new Effect.Fade('fader', {duration: 0.4, afterFinish: function(){scroll(0,0);}
			});
		
		}
		
	}});
					
	
}

function imgOn(id, extension) {
	if(!pngAlpha){
	document.getElementById(id).src = '/safety/graphics/' + id + '_over' + extension;}
}

function imgOff(id, extension) {
if(!pngAlpha){	document.getElementById(id).src = '/safety/graphics/' + id + extension;}
}
   
      function setCookie(name,value,days) {
   
       if (days) {
   
         var date = new Date();
   
         date.setTime(date.getTime()+(days*24*60*60*1000));
   
         var expires = ";expires="+date.toGMTString();
   
       } else {
   
         expires = "";
   
       }
   
       document.cookie = name+"="+value+expires+";path=/";
  
      }
  
       
  
      function readCookie(name) {
  
       var needle = name + "=";
  
       var cookieArray = document.cookie.split(';');
  
       for(var i=0;i <cookieArray.length;i++) {
  
         var pair = cookieArray[i];
  
         while (pair.charAt(0)==' ') {
  
           pair = pair.substring(1, pair.length);
  
         }
  
         if (pair.indexOf(needle) == 0) {
  
           return pair.substring(needle.length, pair.length);
  
         }
  
       }
  
       return null;
  
      }
      
function returnVisit(){

	alert(readCookie('section'));

}

// Browser Detect  v2.1.6
// documentation: http://www.dithered.com/javascript/browser_detect/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)


function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase(); 

   // browser engine name
   this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

   // browser name
   this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
   this.isSafari      = (ua.indexOf('safari') != - 1);
   this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
   this.isOpera       = (ua.indexOf('opera') != -1); 
   this.isIcab        = (ua.indexOf('icab') != -1); 
   this.isAol         = (ua.indexOf('aol') != -1); 
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
   this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isFirefox     = (ua.indexOf('firefox/') != -1 || ua.indexOf('firebird/') != -1);
   this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
   
   // spoofing and compatible browsers
   this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
   this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
   
   // rendering engine versions
   this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
   this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
   this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
   
   // browser version
   this.versionMinor = parseFloat(navigator.appVersion); 
   
   // correct version number
   if (this.isGecko && !this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
   }
   else if (this.isOmniweb) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
   }
   else if (this.isOpera) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
   }
   else if (this.isIcab) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
   }
   
   this.versionMajor = parseInt(this.versionMinor); 
   
   // dom support
   this.isDOM1 = (document.getElementById);
   this.isDOM2Event = (document.addEventListener && document.removeEventListener);
   
   // css compatibility mode
   this.mode = document.compatMode ? document.compatMode : 'BackCompat';

   // platform
   this.isWin    = (ua.indexOf('win') != -1);
   this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
   this.isMac    = (ua.indexOf('mac') != -1);
   this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
   this.isLinux  = (ua.indexOf('linux') != -1);
   
   // specific browser shortcuts
   this.isNS4x = (this.isNS && this.versionMajor == 4);
   this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
   this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
   this.isNS4up = (this.isNS && this.versionMinor >= 4);
   this.isNS6x = (this.isNS && this.versionMajor == 6);
   this.isNS6up = (this.isNS && this.versionMajor >= 6);
   this.isNS7x = (this.isNS && this.versionMajor == 7);
   this.isNS7up = (this.isNS && this.versionMajor >= 7);
   
   this.isIE4x = (this.isIE && this.versionMajor == 4);
   this.isIE4up = (this.isIE && this.versionMajor >= 4);
   this.isIE5x = (this.isIE && this.versionMajor == 5);
   this.isIE55 = (this.isIE && this.versionMinor == 5.5);
   this.isIE5up = (this.isIE && this.versionMajor >= 5);
   this.isIE6x = (this.isIE && this.versionMajor == 6);
   this.isIE6up = (this.isIE && this.versionMajor >= 6);
   
   this.isIE4xMac = (this.isIE4x && this.isMac);
}
var browser = new BrowserDetect();

// if IE5.5+, IE6 on Win32, then display PNGs with AlphaImageLoader
if ((browser.isIE55 || browser.isIE6x) && browser.isWin32) {
	var pngAlpha = true;
// else, if the browser can display PNGs normally, then do that
} else if ((browser.isGecko) || (browser.isIE5up && browser.isMac) || (browser.isOpera && browser.isWin	&& browser.versionMajor >= 6) || (browser.isOpera && browser.isUnix && browser.versionMajor >= 6) || (browser.isOpera && browser.isMac	&& browser.versionMajor >= 5) || (browser.isOmniweb && browser.versionMinor >= 3.1) || (browser.isIE6up) || (browser.isIcab && browser.versionMinor >= 1.9) || (browser.isWebtv) || (browser.isSafari) || (browser.isDreamcast)) {
	var pngNormal = true;
}

/* functions to synthesize and e-mail address and load a form (or a mailto:
   url on the fly when a USER clicks a form button
   created by Jim Lewis with form window programmed by Adam Critchley
   10/2006
   Modified 10/30/06 by Lewis to accept specific form label message and to specify specific box width, if none given, defaults to width in SendMail class style
   Modified 01/10/07 by Adam to dynamically build string used for input tag based on whether fwidth or addstyle have been defined
   Modified by Mike Sullivan for new EHSRM website Jan '09
*/
function OpenEmail(nam1,nam2) 
{
  var prefix;
  //nam2 for a work study is "" so following IF is O.K.
  if (nam2 == ""){
	 prefix = nam1;
     lastpart="utsa.edu";
  }
  else {
 	 prefix = nam1+'.'+nam2;
     lastpart="utsa.edu";
  }
  
  var site='http://www.utsa.edu/safety/includes/pages/mailto.htm?mail='+prefix+'%40'+lastpart+'&name='+nam1+'%20'+nam2;
  window.open( site , 'SendEmail','resizable=0,toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,width=500,height=300');	
}

function myPadDate( num )
	{
		if( num < 10 )
		{
			num = "0" + num;
		}
		
		return num;
	}

	function myGetDate()
	{
		var currentTime = new Date();
		var day = myPadDate( currentTime.getDate() );
		var month = myPadDate( currentTime.getMonth() + 1 );
		var year = currentTime.getFullYear();
        var hour = myPadDate( currentTime.getHours() );
        var minute = myPadDate( currentTime.getMinutes() );
        var second = myPadDate( currentTime.getSeconds() );
        return (month+"/"+day+"/"+year+"_"+hour+":"+minute+":"+second);
	}

	function performSubmit()
	{
		var myBuilding = document.bioform.building.value;
		
		if( document.bioform.generator.value.length < 1 )
		{
			alert( "Please provide the Name of the Generator.");
			document.bioform.generator.focus();
		}
		else if( document.bioform.phone.value.length < 1 )
		{
			alert( "Please provide a way for Contacting the Generator.");
			document.bioform.phone.focus();
		}
		else if( document.bioform.room.value.length < 1 )
		{
			alert( "Please provide the Room where the Biohazard is to be picked up.");
			document.bioform.room.focus();
		}
		else if( myBuilding.length < 1 )
		{
			alert( "Please provide the Building where the Biohazard is to be picked up.");
			document.bioform.building.focus();
		}
		else
		{
			document.bioform.submitButton.disabled = true;
			document.bioform.submitButton.value = "Submitting...";
			document.bioform.building.value = myBuilding.toUpperCase();
            document.bioform.date.value = myGetDate();
            document.bioform.submit();
        }
	}
function mySubmit()
	{
		var myBuilding = document.hazform.building.value;
		
		if( document.hazform.generator.value.length < 1 )
		{
			alert( "Please provide the Name of the Generator.");
			document.hazform.generator.focus();
		}
		else if( document.hazform.phone.value.length < 1 )
		{
			alert( "Please provide a way for Contacting the Generator.");
			document.hazform.phone.focus();
		}
		else if( document.hazform.room.value.length < 1 )
		{
			alert( "Please provide the Room where the Chemical is to be picked up.");
			document.hazform.room.focus();
		}
		else if( document.hazform.chemicals.value.length < 1 )
		{
			alert( "Please provide the Name and Concentrations of the Chemicals.");
			document.hazform.chemicals.focus();
		}
		else if( myBuilding.length < 1 )
		{
			alert( "Please provide the Building where the Chemical is to be picked up.");
			document.hazform.building.focus();
		}
		else
		{
			document.hazform.submitButton.disabled = true;
			document.hazform.submitButton.value = "Submitting...";
			document.hazform.building.value = myBuilding.toUpperCase();
            document.hazform.date.value = myGetDate();
            document.hazform.submit();
        }
	}