/**************************************************************
file: print.js
developer: Bob Lewis
date: 01Jun06
description: inserts header and footer into page for printing;
             Restoration Review (Leroy Garrett);
             part of the logic involves setting the right path 
             for images and URLs, due to the fact that the 
             page being displayed is being accessed directly, 
             where as pages on the Web site are loaded into 
             the 'article.htm' page by way of a call to 
             the clientSideInclude function found in utils.js 
             in the same folder as this file;
update: 03Jun06: modified logic to display the introduction
                 pages to each printed volume of Restoration 
                 Review (Leroy Garrett);
                 the intro page will also display a footer 
                 containing a url for direct access to the page;
                 if the intro page is accessed directly, 
                 without going through the site menus, the 
                 code will not display header or footer 
                 information;
      : 19Jun06: added logic for redirecting files that are
                 accessed directly via an absolute URL such as
                 Google might display in its hit list;
      : 20Jun06: modified code for managing intro files that 
                 are accessed directly via an absolute URL;                 
***************************************************************/
//alert("print.js");

document.write("<script src='../scripts/updates.js'></script>");

function init()
{
	var str = window.location.href;
	//check for query string
	var pos = str.indexOf("?");
	//if there is no query string, simply display the page
	//otherwise, insert header and footer text
	if(pos > -1)
	{
		//create URL, insert into 'footer' <p> 
		//at bottom of page as link and format
		var theURL = str.substr(pos+1, str.length);
		var theFooter = document.getElementById("footer");
		theFooter.innerHTML = "<a href='"+theURL+"'>"+theURL+"</a>";
		
		//start: get volume from intro file  20Jun06
		var test = str.indexOf("intro_"); 
		var isIntro = 0;
		if(test > -1)
		{		
			var thisFile = str;
			var tmpPos = thisFile.indexOf("intros/")    
			var tmpStr = thisFile.substr(tmpPos+7, thisFile.length);
			tmpPos = tmpStr.indexOf(".htm");
			var tmpStr2 = tmpStr.substr(0, tmpPos+4);
			tmpPos = tmpStr2.indexOf("_");
			var introVol = tmpStr2.substr(tmpPos+1,2);
			isIntro = 1;
		}
		//end: get volume from intro file

		theFooter.style.marginTop = "80";
		theFooter.style.fontSize = "10pt";
		theFooter.style.color = "blue";
		theFooter.setAttribute("align","left");

		var thisURL = str.split("?");

		if(thisURL[0].indexOf("intro") > -1)
		  {
		    //insert new span (this_page) element below footer and insert tmpURLs[0] into span
		    //beforeBegin, afterBegin, beforeEnd, afterEnd
		    var spanHTML = "<span id='thispage'><a href='"+thisURL[0]+"'>"+thisURL[0]+"</a></span>"
		    theFooter.insertAdjacentHTML('afterEnd', spanHTML);
		    document.all.footer.style.marginBottom = "10";
		    document.all.thispage.style.fontSize = "10pt";
		  }

		//create citation string, insert into 'header' span,
		//locate at position top:0; left:20
		pos = str.lastIndexOf("htm");
		var myStr = str.substr(pos+3, str.length);
		var myArray = myStr.split("&");
		var vol = myArray[1];
		var num = myArray[2];
		var theYear = myArray[3];

		//start: set vol, num, theYear for intro file 20Jun06
		if(isIntro)
		{
			if(vol == null)
			{
				vol = introVol;
			}
			if(num == null)
			{
				num = "1"; //set to first issue
			}
			if(theYear == null);
			{
				theYear = get_year(vol);
			}
		}
		//end: set vol, num, theYear for intro file

		var theMonth = get_month(num);
		theYear = get_year(vol); //20Jun06
		header_text = "Volume "+vol+", Number "+num+", "+theMonth+" "+theYear;
		var theElem = document.getElementById("header");
		theElem.innerHTML = header_text;

		headerWidth = theElem.offsetWidth;
		var cWide = document.documentElement.offsetWidth;
		theElem.style.position = "absolute";
		theElem.style.top = "0";
		theElem.style.left = 20;
		theElem.style.fontSize = "10pt";
		theElem.style.textAlign = "center";
		document.body.style.marginLeft = "20";
		document.title = header_text;
        theElem.style.left = eval(cWide - headerWidth - header_text.length);// - 10;

        //insert new span for RR title; left justify
        header.insertAdjacentHTML('beforeBegin', "<span id='logo'>Restoration Review</span>");
        var logo = document.getElementById("logo");
        logo.style.color = "black";
        logo.style.fontSize = "10pt";
        logo.style.position = "absolute";
        logo.style.top = "0";
        logo.style.left = document.body.style.marginLeft;

		//if there are images, correct the path string
		var images = document.getElementsByTagName("img");
		if(images.length > 0)
		{
			for (i=0; i<images.length; i++)
			{
                //if this is an 'intro' page, skip this step
                //since the path is different
				if(thisURL[0].indexOf("intro") < 0)
				  {
				    //http://localhost/leroygarrett/rreview/rr22_01/images/coloform.gif"
				    var path = images[i].src;
				    var tmpArray = path.split("/rr"+vol+"_");
				    path = tmpArray[0] + tmpArray[1].substr(2, tmpArray[1].length);
				    images[i].src = path;
				  }
			}
		}
		//insert space before title of document
		var myDivs = document.getElementsByTagName("div");
		myDivs[0].style.position = "relative";
		myDivs[0].style.top = "38";
	}
  else
    {
      //if the href has no has no query string attached, the page being accessed directly;
      //use the href, and title (which contains vol & num)  to format a URL string identical 
      //to that inserted at the bottom of the print page, with the following format:
      //http://localhost/leroygarrett/rreview/article.htm?rr19_01/rr19_01f.htm&amp;19&amp;1&amp;1977

      /* was using the title to retrieve volume and number //delete any time
      var title = document.title;
      var tmpstr = title.split("&");
      var vol = tmpstr[1];
      var yr = get_year(vol)
      var pos = str.indexOf("/rr"+vol);
      var str01 = str.substr(0,pos+1); //http://localhost/leroygarrett/rreview/
      var str02 = str.substr(pos+1,str.length); //rr30_01/rr30_01a.htm
      */
      
      var pos = str.indexOf("_");
      var str01 = str.substr(0,pos-4);
      var str02 = str.substr(pos-4,str.length);
      
      //str02: http://localhost/leroygarrett/rreview/rr30_01/rr30_01a.htm
      pos0 = str02.lastIndexOf("/");
      var vol = str02.substr(pos0+3,2);
      var num = eval(str02.substr(pos0+6,2));
      var yr = get_year(vol);
      var title = "&"+vol+"&"+num;
      
 	  //20Jun06
 	  //if the page being acessed directly, from the absolute URL, is an intro page,
 	  //create a url for the appropriate index file and attach the file name;
 	  //when the index file loads, the intro file will be displayed in a pop-up window;
 	  var thisURL = str.split("?"); //new
      var start = eval(str.length-7);
      if(thisURL[0].indexOf("intro") > -1) //new
	    {
          //str http://localhost/leroygarrett/rreview/intros/intro_29_30.htm
          str02 = str; //assignment needed due to change in value of str02 in previous code segment
          pos = str.indexOf("intros");
          var seg01 = str02.substr(0, pos); //http://localhost/leroygarrett/rreview/
          var thePage = str02.substr(pos+7, str02.length);
          var thisVol = str02.substr(pos+13, 2);
          var thisYear = get_year(thisVol);
          var seg02 = seg01+"display_index.htm?rr"+thisVol+"_01/rr"+thisVol+"_01.htm&"+thisVol+"&1&"+thisYear+"&intros/"+thePage; //intro_"+thisVol+".htm";
          var goURL = seg02;
          window.location = goURL;
	    }
      else if(str.substr(start, 1) == "_") //if an index, not an article
        {
		  var goURL = str01+"display_index.htm?"+str02+title+"&"+yr;
		  window.location = goURL;
        }
      else //if an article, not an index
        {
          var goURL = str01+"article.htm?"+str02+title+"&"+yr;
		  window.location = goURL;
        }
    }
get_updates();
}


function get_month(num)
{
var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December");
return months[num];
} 

function get_year(year)
{
  var BASE = 1958;
  var year = eval(BASE + parseInt(year)); 
  return year;
}

