var sndx=new makesoundex();
var inputname=null;
var soundexcode=null;


// Data Initialisation (SH/GCB)
function makesoundex() {
  this.a = -1
  this.b =  1
  this.c =  2
  this.d =  3
  this.e = -1
  this.f =  1
  this.g =  2
  this.h = -1
  this.i = -1
  this.j =  2
  this.k =  2
  this.l =  4
  this.m =  5
  this.n =  5
  this.o = -1
  this.p =  1
  this.q =  2
  this.r =  6
  this.s =  2
  this.t =  3
  this.u = -1
  this.v =  1
  this.w = -1
  this.x =  2
  this.y = -1
  this.z =  2
}


function collapse(surname) {
  if (surname.length==1) {
    return surname
  }
  var right=collapse(surname.substring(1,surname.length))
  if (sndx[surname.charAt(0)]==sndx[right.charAt(0)]) {
    return surname.charAt(0)+right.substring(1,right.length)
  }
  return surname.charAt(0)+right
}



function soundex(inputname) {
  soundexvalue=""
  var stage1=collapse(inputname.toLowerCase())
  soundexvalue+=stage1.charAt(0).toUpperCase() // Retain first letter
  var stage2=stage1.substring(1,stage1.length)
  var count=0
  for (var i=0; i<stage2.length && count<3; i++) {
    if (sndx[stage2.charAt(i)]>0) {
      soundexvalue+=sndx[stage2.charAt(i)]
      count++
    }
  }
  for (; count<3; count++) {
    soundexvalue+="0"
  }
 return soundexvalue
}



function DoSubmit(){
formOK=true;
document.forms[0].sndxcode.value="";

// clear comments
document.getElementById("forecomment").innerHTML="";
document.getElementById("surcomment").innerHTML="";
document.getElementById("earlycomment").innerHTML="";
document.getElementById("latecomment").innerHTML="";

//check surname entered
inputname=document.formA.surname.value;
if (inputname.length<3){
document.getElementById("surcomment").innerHTML="you must include a surname";
formOK= false;
}
else{
//check surname alphabetic
    for (var i=0; i<inputname.length; i++) {
      var letter=inputname.charAt(i)
      if (!(letter>='a' && letter<='z' || letter>='A' && letter<='Z')) {
      document.getElementById("surcomment").innerHTML= "alphabetic characters only";
        formOK= false;
      }
    }
 }

//work out SOUNDEX (even if not needed)
if (formOK){
document.forms[0].sndxcode.value=soundex(inputname);
}

//check forename
inputname=document.forms[0].forename.value;
if (inputname.length>0){
    for (var i=0; i<inputname.length; i++) {
      var letter=inputname.charAt(i)
      if (!(letter>='a' && letter<='z' || letter>='A' && letter<='Z')) {
      document.getElementById("forecomment").innerHTML= "alphabetic characters only";
        formOK= false;
      }
    }


}

//check dates
var early=document.forms[0].early.value;
var earlylen=early.length;
var earlyvalid=true;
switch(earlylen){
case 0:
	latevalid=false;	
	break;
case 4:
	for (var i=0; i<earlylen; i++) {
      	var letter=early.charAt(i);
      	if (!(letter>='0' && letter<='9')) {
      		document.getElementById("earlycomment").innerHTML= "4-digit year value only";
      		formOK= false;
			earlyvalid=false;	
      		}// end if
   		}// end for
	break;
default:
      document.getElementById("earlycomment").innerHTML= "4-digit year value only";
	formOK=false
	earlyvalid=false;
	break;
}// end switch


var late=document.forms[0].late.value;
var latelen=late.length;
var latevalid=true;
switch(latelen){
case 0:
	latevalid=false;	
	break;
case 4:
	for (var i=0; i<latelen; i++) {
      	var letter=late.charAt(i);
      	if (!(letter>='0' && letter<='9')) {
      		document.getElementById("latecomment").innerHTML= "4-digit year value only";
      		formOK= false;
			latevalid=false;
      		}// end if
   		}// end for
		break;
default:
alert (latelen);
      document.getElementById("latecomment").innerHTML= "4-digit year value only";
	formOK=false
	latevalid=false;
	break;
}// end switch


if(earlyvalid && latevalid){
if (early > late) {
document.getElementById("earlycomment").innerHTML="earliest is later";
document.getElementById("latecomment").innerHTML="than latest!";
formOK=false;
}
}


if(formOK==true){
document.getElementById("numhits").innerHTML="";
document.getElementById("searching").innerHTML="searching";
return true;
 }
else {
return false;
}
}


function DoExit(){
this.location.href="http://www.anesfhs.org.uk/databank/";
}



function DoHelp(){
document.getElementById("helpbox").style.visibility="visible";
document.getElementById("helpbox").innerHTML="<div align='right'><a class='exitbutton' href='Javascript:HideHelp();'>&nbsp;exit Help&nbsp;</a></div><div class='helphead'>Help Menu</div<hr><p align='center'>For detailed help and background information click:</p><p class='centre'><a class='boldbutton' href='Javascript:DoHelpPhBG();'>Parishes and Burial Grounds</a><br> <a class='boldbutton' href='Javascript:DoHelpPubUnpub();'>Published and Unpublished</a><br> <a class='boldbutton' href='Javascript:DoHelpSearch();'>Searches and Results</a> <br> <a class='boldbutton' href='Javascript:DoHelpProbs();'> Problems & Feedback</a></p>";
}


function DoHelpPhBG(){
document.getElementById("helpbox").style.visibility="visible";
document.getElementById("helpbox").innerHTML="<div align='right'><a class='exitbutton' href='Javascript:DoHelp();'>&nbsp;return to Help Menu&nbsp;</a></div><div class='helphead'>Parishes and Burial Grounds</div><hr><iframe src='miphbg.html' width='600' height=90% frameborder='0' scrolling='yes'><a href='miphbg.html'>Hmm, you are using a very old browser.Click here to go directly to included content.</a></iframe>";
}


function DoHelpPubUnpub(){
document.getElementById("helpbox").style.visibility="visible";
document.getElementById("helpbox").innerHTML="<div align='right'><a class='exitbutton' href='Javascript:DoHelp();'>&nbsp;return to Help Menu&nbsp;</a></div><div class='helphead'>Published and Unpublished</div><hr><iframe src='mipubunpub.html' width='600' height=90% frameborder='0' scrolling='yes'><a href='mipubunpub.html'>Hmm, you are using a very old browser.Click here to go directly to included content.</a></iframe>";
}


function DoHelpSearch(){
document.getElementById("helpbox").style.visibility="visible";
document.getElementById("helpbox").innerHTML="<div align='right'><a class='exitbutton' href='Javascript:DoHelp();'>&nbsp;return to Help Menu&nbsp;</a></div><div class='helphead'>Searches and Results</div><hr><iframe src='misrch.html' width='600' height=90% frameborder='0' scrolling='auto'><a href='misrch.html'>Hmm, you are using a very old browser.Click here to go directly to included content.</a></iframe>";
}



function DoHelpProbs(){
document.getElementById("helpbox").style.visibility="visible";
document.getElementById("helpbox").innerHTML="<div align='right'><a class='exitbutton' href='Javascript:DoHelp();'>&nbsp;return to Help Menu&nbsp;</a></div><div class='helphead'>Problems and Feedback</div><hr><p>The MI booklets, and the indexes to them have been produced over a lengthy period of time, by a considerable number of people.  In such a situation, Quality Control is something of a problem.  Many errors have been detected and corrected since the MI Index first went online, but we do depend in part on users reporting errors.</p><p>That said, the assembling of the data and the design of the search interface is entirely down to Gavin Bell (ANESFHS member 4085).<p>The HTML and CSS used in this application have been validated as below, and the website tested with Firefox 3.0.5,   IE7 and Safari 3.2.1.</p><p>If your Browser's default font size is set to anything greater than 16 the pages may not render correctly.</p><p>Significant aspects of the application require Javascript to be enabled in your browser (although if Javascript is turned off, you won't be able to read this message ...  )</p><p></p><p>Any technical problems, errors in the data or suggestions should be made via <a href='" +  unescape("%4d%41%49%4c%54%4f:") + "g.bell" + unescape("%40%77%68%69%63%68%2e%6e%65%74")+ "'>this link" + unescape("%3c%2f%61%3e") + "</p><p></p><p></p><p></p><p></p><p><a href='http://validator.w3.org/check?uri=referer'><img src='http://www.w3.org/Icons/valid-html401' alt='Valid HTML 4.01 Strict' style='border:0;width:88px;height:31px'></a></p><a href='http://jigsaw.w3.org/css-validator/'><img style='border:0;width:88px;height:31px'src='http://jigsaw.w3.org/css-validator/images/vcss' alt='Valid CSS!' /></a>"
}


//-----------------------------------------------------------------------------------------------------------------------------
function helpprobs(){
document.getElementById("helpbox").style.visibility="visible";
document.getElementById("helpbox").innerHTML ="<table width='600' border='0'><tr><td width='400' align='center'><h3>Problems and Feedback</h3></td><td><a class='boldbutton' href='Javascript:DoHelp();'>&nbsp;Back to Help Menu&nbsp;</a></td></tr></table><hr><blockquote><p>The MI booklets, and the indexes to them have been produced over a lengthy period of time, by a considerable number of people. In such a situation, Quality Control is something of a problem. Many errors have been detected and corrected since the MI Index first went online, but we do depend in part on users reporting errors.</p><p>The HTML and CSS used in this application have been validated as below, and the website tested with Firefox 3.0.5,   IE7 and Safari 3.2.1.</p><p>Significant aspects of the application require Javascript to be enabled in your browser (although if Javascript is turned off, you won't be able to read this message ...  )</p><p></p><p>Any technical problems, errors in the data or suggestions should be made via <a href='" +  unescape("%4d%41%49%4c%54%4f:") + "g.bell" + unescape("%40%77%68%69%63%68%2e%6e%65%74")+ "'>this link" + unescape("%3c%2f%61%3e") + "</p><p></p><p></p><p></p><p></p><p><a href='http://validator.w3.org/check?uri=referer'><img src='http://www.w3.org/Icons/valid-html401' alt='Valid HTML 4.01 Strict' style='border:0;width:88px;height:31px'></a></p><a href='http://jigsaw.w3.org/css-validator/'><img style='border:0;width:88px;height:31px'src='http://jigsaw.w3.org/css-validator/images/vcss' alt='Valid CSS!' /></a></blockquote>"
}
//-----------------------------------------------------------------------------------------------------------------------------



function HideHelp(){
document.getElementById('helpbox').style.visibility='hidden';

}

