function prepareRollovers() {

   if ( !document.getElementsByTagName ) return false;  
   
   // check all the <img> elements
   var images = document.getElementsByTagName("img");
   var imgOFF = new Array();
   var imgON  = new Array();
   
   for ( var i=0; i < images.length; i++ ) {
         
      // for all <img> elements where class="rollover"...
      // NOTE: IE does not suppport getAttribute("class"), for reasons unknown
      if ( images[i].className == "rollover" ) {
			
         // and the default image is the 'OFF' version
         if ( images[i].src.indexOf('_OFF') != -1 ) {
			
            imgOFF[i] = new Image();
            imgON[i]  = new Image();
		 
            var srcOFF = images[i].getAttribute('src');
            // this is a bit indirect, but it forces the browser to pre-load the images
			imgOFF[i].src = srcOFF;
            // assume that for each image with '_OFF' in the filename
            // there is a corresponding image with '_ON' in the filename
			imgON[i].src  = srcOFF.replace( /_OFF/, '_ON');
						
            // set non-standard attributes that can be accessed
            // in the anonymous functions below
            images[i].setAttribute('offsrc', imgOFF[i].src);
            images[i].setAttribute('onsrc', imgON[i].src);
		   
            images[i].onmouseover = function() {
		       this.setAttribute('src', this.getAttribute('onsrc'));
            }

            images[i].onmouseout = function() {
               this.setAttribute('src', this.getAttribute('offsrc'));
            }
		   
		 } else {
		   // ignore images in ON state
		 }
	  }
   }
}

function goToPar(theForm) {

   var paragraphs = Array(18);
   paragraphs['1'] = 191;
   paragraphs['2'] = 103;
   paragraphs['3'] = 43;
   paragraphs['4'] = 98;
   paragraphs['5'] = 209;
   paragraphs['6'] = 130;
   paragraphs['7'] = 90;
   paragraphs['8'] = 210;
   paragraphs['9'] = 71;
   paragraphs['10'] = 154;
   paragraphs['11'] = 93;
   paragraphs['12'] = 63;
   paragraphs['13'] = 78;
   paragraphs['14'] = 66;
   paragraphs['15'] = 76;
   paragraphs['16'] = 120;
   paragraphs['17'] = 359;
   paragraphs['18'] = 149;

   var target = theForm.paragraph.value.replace(/\s+/g, '');

   var pattern = /^\d+\.\d+$/;

   if ( pattern.test(target) ) {
   
	  var part = target.split(".");
	  
	  var chapter = part[0];

          if ( chapter < 1 || chapter > 18 ) {

             alert("Oops. The Manual does not have a Chapter " + chapter + ". Try a chapter number between 1 and 18 (e.g., 3.14).");
             return false;
          }
	  
	  var paragraph = part[1];

	  if ( paragraph > paragraphs[chapter] ) {
	  
	     alert("Oops. Chapter " + chapter + " does not have a paragraph " + paragraph + ". Try again.");
         theForm.paragraph.value = "";
		 theForm.paragraph.focus();
		 return false;
	  
	  } else {
	  
	     var ch_pad = "";
		 var para_pad = "";
	  
	     if ( chapter < 10 ) {
		    ch_pad = "0";
		 }
		 
		 if ( paragraph < 10 ) {
		    para_pad = "00";
		 } else if ( paragraph < 100 ) {
		    para_pad = "0";
		 }
	  
		 window.location.href = "http://podss-dev.uchicago.edu:2001/CMS_ERG/html_files_new/ch" + ch_pad + chapter + "/" + 
		                        "ch" + ch_pad + chapter + "_sec" + para_pad + paragraph + ".html";
		 return false;
	  }
	  
   } else {
      if ( target ) {
         alert("Oops. The value '" + target + "' doesn't appear to be a Manual paragraph number (e.g., 3.14). Try again.");
	     return false;	  
	  } else {
	     // do nothing if the user submits nothing (or spaces)
	     return false;
	  }
   }
}

function prepareForms() {

   // based on scripts in "DOM Scripting" by Jeremy Keith
   // see "showpic.js" script, function "prepareGallery", p. 139

   if ( !document.getElementById ) return false;
   
   // GO TO PARAGRAPH form
   var paraSearchForm = document.getElementById("para_search");
   if ( paraSearchForm ) {
      paraSearchForm.onsubmit = function() {
         return goToPar(this);
      }   
   }
}

window.onload = function() {

   prepareRollovers();
   prepareForms();
}

