/*
	Author:	Geoff Hughes
	Date:	16/05/2005
	Version: 2
	Description:
		Tool to support selection of images by page number.
*/
/* Global variables */
var template=""	//image file name template
var map = null //current page map object
/* Append

	Description:
		Method of Map object. Appends a pageSet object
		to the list contained in Map.
*/
function Append(pageSet)
{
  var p = this
  var q = p.next
 
  while (q!=null)
  {
	p = q
	q = p.next
  }
  p.next = pageSet
}
/* Map

	Description:
		Constructor for Map object.
*/
function Map()
{
  this.next = null
  this.Append = Append;
  this.validatePage = validatePage;
  this.showPage = showPage;
}
/* PageSet

	Description:
		Constructor for PageSet object.
	Parameters:
		first	First page number in a range
		last	Last page number in a range
		imgoffs	Image offset - the number which produces
				an equivalent image number when added to
				a page number in the range.
*/				
function PageSet(first,last,imgoffs)
{
  this.next = null
  this.first = first
  this.last = last
  this.imgoffs = imgoffs
}
/* createMap

	Description:
		Creates a simple page map using the informatiom
		specified in the string 'range'
	Parameters:
		range	string variable in the form "first,last,offset",
		where first,last specify the page range and offset specifies
		the image offset to be applied in the range.
*/
function createMap(range)
{
	// Split the input string into three components
	// Note: could have used the split method of
	// the string object but not available in all browsers.
	
	var a = new Array(3)
	var j,k
    for (i = 0; i < a.length; i++)
	{
	  a[i]=0
	}
	if(range.length > 0)
	{
	  for (i = 0, j=0; i < a.length; i++)
	  {
		k = range.indexOf(",",j)
		if(k == -1)
		{
		  a[i]=parseInt(range.substring(j,range.length),10)
		  break
		}
		else
		{
		  a[i]= parseInt(range.substring(j,k),10)
		  j = k+1
		}
	  }
	} 
  // discard memory allocated to a previous map	
  delete map
  // construct the map
  map = new Map()
  // append the page range
  map.Append(new PageSet(a[0],a[1],a[2]))
}
/* selectedValue

	Description:
		Returns the value of the currently selected option in the
		drop-down box named "section". If section doesn't exist,
		raises alert and returns zero.
*/
function selectedValue()
{
  var sect = document.forms[0].section
 
  if (sect==null)
  {
    alert("Drop-down element 'section' not found") 
    return 0
  }
  else return sect.options[sect.selectedIndex].value
}
/* pad

	Description:
		Inserts image number into template
	Inputs:
		x	integer containing required image number
	Return value:
		string containing image number right justified
		in the template
*/
function pad(x)
{
  var s = template.substring(0,template.indexOf("."))
  var y = x.toString(10)
  var p = s.length - Math.min(y.length,s.length)
  if(p < s.length)
  {
    s = s.substring(0,p) + y.substring(0,s.length-p)
  }
  return s
}
/* reset

	Description:
		Programmatic reset of the form assumed to contain the
		page number text box
		
*/
function reset()
{
  document.forms[0].reset()
}
/* validatePage

	Description:
		Method of Map object.
		The page number is assumed to be in an element named "page" on the first
		form in the forms collection for the current page. An alert is raised if
		a text box named 'page' is not found. Otherwise, Checks that the user
		supplied page number is valid and lies within the range specified
		in the initPage call.
		If the number is not valid an alert message is output.
	Return value:
		If page number is valid, returns offset.
		Else returns null.
*/
function validatePage()
{
  var pSet = this.next
  var pageBox = document.forms[0].page
  
  if(pageBox==null)
  {
	alert("A text input box named 'page' was not found")
	return -1
  }
  else
  { 
	var p = pageBox.value
	p = parseInt(p,10)
	while(pSet!=null)
	{
	  if((p >= pSet.first)&&(p <=pSet.last))return pSet.imgoffs
	  pSet = pSet.next
	}
	alert("Page number not found")
	return null
  }
}
/* showPage

	Description:
	    Method of Map object.
		The page number is assumed to be in an element named "page" on the first
		form in the forms collection for the current page. An alert is raised if
		a text box named 'page' is not found. Otherwise, an image reference is
		created by "inserting" the user entered page number
		into the template specified in the initPage call.

		The image so referenced is then loaded into a frame object named "image" in
		the parent frameset.
*/
function showPage()
{
  var imageref = ""
  var pageBox = document.forms[0].page
  
  if(pageBox==null)
  {
	alert("A text input box named 'page' was not found")
  }
  else
  { 
    var offset = this.validatePage()
    // if page number is not valid reset the page box to the
    // first valid page number, and set the offset to the
    // corresponding offset for this page
    if (offset == null)
    {
      document.forms[0].page.value = this.next.first
      offset = this.next.imgoffs
    }
    var pageNum = parseInt(pageBox.value,10)+offset
    imageRef = pad(pageNum)+ template.substring(template.indexOf("."))
	parent.image.location = imageRef
  }
}