<!-- // Data declarations and function definitions for Parish Data Map
	 // ersion 4.4 - 2008-10-24  Adds support for details and also refresh function
	 // Version 4.3 - 2008-08-07
	 // Rev .3 corrects a typing error at line 433 causing many of 1841 to be treated as 1851
	 // Adds 1851 census and improved parsiong for all censuses

var transcriptRoot = ""		// Default values if no parconf.js file exists
var offerDetails = false

var d = new Array()			// Parishes in original (alphabetic) order
var m = new Array()			// Parishes in map index order

// Dummy first data entry.
d[0]=" Click here for drop-down list  ---->_0XX999_0_0_0_0"

// Remaining data elements are read in from a separate data file pardata.js
// following the end of the function definitions

// Decimal to Hex Array
var h = new Array("f","e","d","c","b","a","9","8","7","6","5","4","3","2","1","0")

// County Codes
var c = new Array()
c[0] = new Array("OXF","f","f","9")
c[1] = new Array("BRK","f","c","c")	// Berkshire moved to Oxford in 1974
c[2] = new Array("BRK","f","9","9")	// Berkshire post-1974
c[3] = new Array("BKM","9","9","f")
c[4] = new Array("NTH","9","f","9")
c[5] = new Array("WAR","f","9","f")
c[6] = new Array("WOR","0","9","0")
c[7] = new Array("GLS","9","f","f")
c[8] = new Array("WIL","f","9","6")
c[9] = new Array("UKN","c","c","c")


//Identify browser
brow = navigator.appName + parseInt(navigator.appVersion);
IE4 = (brow == "Microsoft Internet Explorer4")
Pre4 = (parseInt(navigator.appVersion) < 4)

// Set some defaults
transViewAvailable = false
misViewAvailable = false
inCensus = false
keySequence=""
timer=setTimeout("",1)

// Define functions
function BuildOptions() {
	// Populate map order array, with list index, and create <OPTION> elements.
	for(i=0; i<d.length; i++) {
		e=d[i].split("_")
		if(e[1].length > 6 && e[1].slice(3,6) != "999") {
			n = notOctal(e[1].slice(3,6))
			m[n]=i
		}
		document.writeln("<OPTION value=" + i + ">"+e[0])
		
	}
	// Ensure map order array has no gaps by filling these with dummy elements
	for(i=0; i<m.length; i++) {
		if (m[i]==null) m[i]=0;
	}
}


function BuildAreas() {
	// Create <AREA> tag elements
	for(i=0; i<m.length; i++) {
		e=d[m[i]].split("_")
		if(e[1].length > 6) {
			t = '<AREA shape="polygon" coords="'
			for(j=6; j<e[1].length-3; j=j+3) {
				t = t + e[1].slice(j, j+3) + ','
			}
			t = t + e[1].slice(j, j+3) + '" '
			t = t + 'href="JavaScript:ClickMap('
			t = t + i
			t = t + ')" title="'
			t = t + e[0] +'">'
			document.writeln(t)
		}
	}
	document.pdata.ParishList.focus()
}


function ClickMap(x) {
	SetTrans(x)
	document.pdata.ParishList.selectedIndex = m[x]
	ShowResults()
}


function KeyDown(eventObject) {
    if(!inCensus) {
	if(IE4) key = eventObject.keyCode
	else key = eventObject.which;

	if((key==38 || key==40) && !IE4) { // Handle up and down arrow keys for non-IE4
		document.pdata.ParishList.selectedIndex += (key - 39)
		keySequence = ""
	}
	else if((key>64 && key<91) || key==32 || key==189) { // Letters, space & hyphen
		keySequence += String.fromCharCode(key)
		clearTimeout(timer)
		timer=setTimeout("timeUp()",2000)
	}
	SelectParish()
    }
    else {
	if(IE4) key = eventObject.keyCode
	else key = eventObject.which;
	if(key==13) {
		parseCensus()
	}	
    }





}

function timeUp() {
	keySequence=""
	clearTimeout(timer)
}


function SelectParish() {

	ks=keySequence.toLowerCase()	// Must freeze value in case timeout occurs during this.
	if(ks!="") {
		for(i=0; i<d.length; i++) {
			e = d[i].split("_")
			if(ks <= e[0].toLowerCase()) break
		}
	}
	else {
		i = document.pdata.ParishList.selectedIndex
	}
	if(i>0 && i<d.length) { 
		document.pdata.ParishList.selectedIndex = i
		e = d[i].split("_")
		n = notOctal(e[1].slice(3,6))
		SetTrans(n)
		ShowResults()
	}
}


function SetTrans(x) {
	if(document.images && document.images[0].style && document.images[0].style.filter) {
		var r, g, b, a, y, z
		if(x != 999) {
			g = parseInt(x/64)
			r = parseInt(x/8)%8
			b = x%8
			if(r%2) b=7-b
			if(g%2==1) r=7-r
			e = d[m[x]].split("_")
			a = e[1].slice(0,1)
			y = c[a][1] + h[r] + c[a][2] + h[g] + c[a][3] + h[b]
		}
		else y = 010101;		// Unused colour
		z = "Chroma(color=" + y + ")"
		document.images[0].style.filter=z
	}
	else {
		SetRedDot(x)
	}
}


function SetRedDot(x) {
	var xMean, yMean
	for(i=0; i<m.length; i++) {
		e=d[m[i]].split("_")
		if(e[1].length > 6 & e[1].slice(3,6) == x ) {
			xMean = 0
			yMean = 0
			k=0
		
			for(j=6; j<e[1].length-3; j=j+6) {
				xMean = xMean + notOctal(e[1].slice(j, j+3))
				yMean = yMean + notOctal(e[1].slice(j+3, j+6))
				k=k+1
			}
			xMean = xMean * 6 / (e[1].length - 6)
			yMean = yMean * 6 / (e[1].length - 6)
		}
	}

	if(document.redDot) {
		dom=eval(document.redDot)		// Netscape DOM
		dom.left = xMean + 6			// Correct Netscape's map displacement
	  	dom.top = yMean + 6
		}
	else if(document.all.redDot.style) {
		dom=eval(document.all.redDot.style)	// IE & Mozilla & Opera DOM
		dom.left = xMean
		dom.top = yMean
		}
	else 	{
		alert("Sorry, your browser cannot display red marker dots.")
		}
	
}


function ShowResults() {
	i = document.pdata.ParishList.selectedIndex
	e = d[i].split("_")
	document.pdata.par.value = e[0]
	n = parseInt(e[1].slice(0,1))
	document.pdata.co.value = c[n][0]
	n = e[1].slice(1,3)
	document.pdata.ref.value = n
	for(i=2; i<6; i++) {
		if(e[i] == -1) {
			e[i] = "Not yet available"
		}
		else {
			e[i] = "Has" + e[i] + " entries"
		}
	}
	// Mask any parishes with <20 marriages as due to strays
	if(e[3] < 20) e[3] = "n/a";
	
	// Expand Transcript codes
	transViewAvailable = false
	misViewAvailable = false
	if(e[6] == null) e[6] = "Status Unknown";
	else if(e[6] == "#") e[6] = "Only on Microfiche";
	else if(e[6] == "-") e[6] = "Not transcribed";
	else {
		e[6] = e[6] + " & Microfiche"
		if (transcriptRoot != "") transViewAvailable = true;
	}
	document.pdata.bapts.value = e[2]
	document.pdata.mars.value = e[3]
	document.pdata.burs.value = e[4]
	document.pdata.mis.value = e[5]
	document.pdata.trans.value = e[6]
	document.pdata.ParishList.focus()
}	

function refresh() {
	if(document.pdata.ParishList.selectedIndex > 0) {
		i =	document.pdata.ParishList.selectedIndex
		e = d[i].split("_")
		n = notOctal(e[1].slice(3,6))
		SetTrans(n)
		ShowResults()
	}
	document.pdata.ParishList.focus()
	return false
}

function notOctal(s) {
	// Same as parseInt, but prevents leading zeros implying an Octal number!
	while (s.slice(0,1) == "0" && s.length > 1) s = s.slice(1,s.length);
	return parseInt(s)
}


function viewTranscript() {
	if (transViewAvailable) {
		page = document.pdata.trans.value
		e = page.split(" ")			// If transcript on 2 CDs, chose first
		page = transcriptRoot + e[0] + "\\HeadPage.pdf"
		window.location.href=page
	}
	document.pdata.ParishList.focus()
	return false
}

function viewDetails() {
	var parcode = (document.pdata.par.value)
	parcode = parcode.toLowerCase()
	if (parcode.length < 1) return false;
	parcode = parcode.replace(/[^a-z]/g,"_")
	window.location.href="detail.html?" + parcode
	document.pdata.ParishList.focus()
	return false
}



function nullClick() {
	// Forces focus back to Parish List after spurious clicks, to allow immediate key entry.
	document.pdata.ParishList.focus()
	inCensus = false
	return false
}
	

function focusToCensus() {
	// Allows census box to take focus for data entry
	document.pdata.census.focus()
	inCensus = true
	event.cancelBubble = true	
	return false
}

function parseCensus() {
	cen = document.pdata.census.value
	e = cen.toUpperCase().split("/")
	pn = notOctal(e[1])
	fo = notOctal(e[2])

	switch(e[0]) {

	case "RG9": case "1861": case "61": case "6":
		page = transcriptRoot + "1861/RG9-" + pn + ".pdf"
		window.location.href=page
		break;

	case "RG10": case "1871": case "71": case "7":
		page = transcriptRoot + "1871/" + pn + ".pdf"
		window.location.href=page
		break;

	case "RG11": case "1881": case "81": case "8":
		page = transcriptRoot + "1881/RG11-" + pn + ".pdf"
		window.location.href=page
		break;

	case "RG12": case "1891": case "91": case "9":
		if(isNaN(fo) || fo < 1) {
			alert ("Some census pieces in 1891 are split over two files:\nyou must specify RG12/piece/folio")
			break;
		}
		if (pn == 986) {
			if (fo < 109) {
				pn = pn + "-1"
			}
			else {
				pn = pn + "-2"
			}
		}
		if (pn == 1178) {
			if (fo < 116) {
				pn = pn + "-1"
			}
			else {
				pn = pn + "-2"
			}
		}
		if (pn == 1179) {
			if (fo < 122) {
				pn = pn + "-1"
			}
			else {
				pn = pn + "-2"
			}
		}
		page = transcriptRoot + "1891/RG12-" + pn + ".pdf"
		window.location.href=page
		break;

	case "RG13": case "1901": case "01": case "0":
		page = transcriptRoot + "1901/RG13-" + pn + ".pdf"
		window.location.href=page
		break;

	case "HO107": case "1841": case "41": case "4": case "1851": case "51": case "5":
		if(e.length < 3) {
			alert ("Census pieces in 1841 and 1851 are split over several files:\n    For 1841 you must specify HO107/piece/book\n    For 1851 you must specify HO107/piece/folio")
			break;
		}
			
		if(isNaN(fo) || fo < 1) {
			alert ("Census pieces in 1841 and 1851 are split over several files:\n    For 1841 you must specify HO107/piece/book\n    For 1851 you must specify HO107/piece/folio")
			break;
		}
		
		// First we test for various 1841 Census options
		if (pn == 23) {
			if (fo < 20) {
				page = "0023M.pdf"
			}
			else {
				page = "0023W.pdf"
			}
		}
		else if (pn == 872) {
			if (fo < 19) {
				page = "0872a.pdf"
			}
			else {
				page = "0872b.pdf"
			}
		}
		else if (pn == 891) {
			if (fo < 10 ) {
				page = "0891a.pdf"
			}
			else {
				page = "0891b.pdf"
			}
		}
		else if (pn < 100) {
			page = "00" + pn + ".pdf"
		}
		else if (pn < 1000) {
			page = "0" + pn + ".pdf"
		}
		
		
		else {
			// If we get here, we have an 1851 Census Reference
			// Define mapping of folio to file name of each piece
			var p = new Array()
			p[0] = [1687,196,402,999]
			p[1] = [1688,211,406,609,999]
			p[2] = [1689,195,398,999]
			p[3] = [1690,193,386,999]
			p[4] = [1691,209,392,999]
			p[5] = [1720,215,426,638,999]
			p[6] = [1725,186,383,999]
			p[7] = [1726,207,414,999]
			p[8] = [1727,210,415,999]
			p[9] = [1728,218,445,999]
			p[10] = [1729,168,345,999]
			p[11] = [1730,169,321,999]
			p[12] = [1731,191,386,612,999]
			p[13] = [1732,172,400,999]
			p[14] = [1733,161,326,999]
			p[15] = [1734,213,436,999]
			p[16] = [1735,999]
			
			for (i=0; i<p.length; i++) {
				if (p[i][0] == pn) {
					for (j=1; j<p[i].length; j++) {
						if (fo < p[i][j]) {
							// alert ("i="+i+"  j="+j+"  fo="+fo+"  p[i][j]="+p[i][j]+"  pn="+pn+"  p[i][0]="+p[i][0])
							break
						}
					}
					break
				}
			}
			page = p[i][0] + "-" + j + ".pdf"
		}

		if (pn < 1000) {
			page = transcriptRoot + "1841/HO107-" + page
		}
		else {
			page = transcriptRoot + "1851/" + page
		}
		// alert(page)
		window.location.href=page
		break;

	default:
		alert("Piece must start with HO107/, RG9/, RG10/, RG11/, RG12/ or RG13/\nAlternatively, census year (e.g. 1851/  or 51/  or just 5/ ) may be used.\n(N.B. There must be no space before the first / )")

	}	
}

// -->
