//<!--

var GRO_Price = 2;
var Census_Price = 5;
var Parish_Price = 15;

//----------------------------------------------------------
// Valid_Email
// Validate email address
//----------------------------------------------------------
var chkDot = true;
var usEmail = true;
function Valid_Email(eAddr) 
{ 
   var lenSuffix = (usEmail) ? 4 : 3;
   var result = false;
   var ndxAt = ndxDot =  0;
          
   ndxAt  = eAddr.indexOf("@");
   ndxDot = eAddr.indexOf(".") ;
   ndxDot2 = eAddr.lastIndexOf(".") ;
          
   if ((ndxDot < 0) || (ndxAt < 0))
      alert("Your email address is blank or lacks '.' or '@'.\n\nThe format is 'you@domain.com'"); 
   //else if (chkDot && (ndxDot < ndxAt))
   //   chkDot = !(confirm("Your email address has a 'dot' before the '@'\n\nAre you sure that is right?"));
   else if ((ndxDot2 - 3) <= ndxAt)
       alert("Your email address may be missing a domain name.\n\nThe format is 'you@domain.com'");
   //  else if (eAddr.length < ndxDot2 + lenSuffix) 
   //    usEmail = !(confirm("You have fewer than 3 characters as a domain suffix.\nAre you sure that is right?"));
   else 
        result=true; 
          
   return result; 
} 

//----------------------------------------------------------
// validate
// Validate input form
//----------------------------------------------------------
function validate(BMD_Type) 
{ 
   if (BMD_Type == "census")
      input=document.censusform
   else    
   if (BMD_Type == "parish")
      input=document.parishform
   else
      input=document.birthform

   email=input.Client_Email.value
   csurname=input.Client_Surname.value 
   cforename=input.Client_Forename.value 
   bsurname=input.Search_Surname.value 
   bforename=input.Search_Forename.value 

   if (BMD_Type != "census")
   {
      dob=input.Search_DOB.value 
      bfrom=input.Year_From.value
      bto=input.Year_To.value
   }

   submitOK=true 

   bsurnameerror = "";
   csurnameerror = "";
   dateerror = "";
   rangeerror = "";
   yearerror = "";
   boundserror = "";
   tenerror = "";
   censuserror = "";
   censusageerror = "";
   censuscountyerror = "";
   parisherror = "";
   parishtypeerror = "";

   // Validate clients email address
   if (!Valid_Email(email)) 
      return false;
  
   // Check for client forname and surname
   if ((csurname.length < 1) || (cforename.length < 1))
   { 
      csurnameerror = "Your forename and surname\n\n"
      submitOK=false
   } 

   // Check for forname and surname of person to be searched
   if (BMD_Type == "marriage")
   {
      msurname=input.Search_Param2.value 
      mforename=input.Search_Param.value 
      if (((bsurname.length < 1) || (bforename.length < 1)) && ((msurname.length < 1) || (mforename.length < 1)))
      { 
         bsurnameerror = "A forename and surname of the person(s) to be searched\n\n"
         submitOK=false 
      } 
   }
   else
   {
      if ((bsurname.length < 1) || (bforename.length < 1))
      { 
         bsurnameerror = "A forename and surname of the person to be searched\n\n"
         submitOK=false 
      }
   } 

   // Check that a census year is input
   if (BMD_Type == "census")
   {
      bcensus=input.Search_Census.value
      if (bcensus == "")
      {
         censuserror = "A census year\n\n"
         submitOK=false
     }

     bcensus = input.Search_Age.value
     if (bcensus == "") {
         censusageerror = "An age\n\n"
         submitOK = false
     }

     bcensus = input.Search_County.value
     if (bcensus == "") {
         censuscountyerror = "A state/county to search\n\n"
         submitOK = false
     }
   }

   // Check that a parish type is input
   if (BMD_Type == "parish")
   {
      bparishtype=input.Search_Type.value
      if (bparishtype == "")
      {
         parishtypeerror = "A type of register\n\n"
         submitOK=false
      }
   }

   // Check that parish is input
   if ((BMD_Type == "census") || (BMD_Type == "parish"))
   {
      bparish=input.Search_Parish.value
      if (bparish.length < 1)
      {
         parisherror = "A City/Town/Villiage or Parish\n\n"
         submitOK=false
      }
   }

   // Check that date and range have not both been entered
   if (BMD_Type != "census")
   {
      if (((dob.length > 1) && ((bfrom.length > 1) || (bto.lenght > 1))) ||
         ((dob.length < 1) && ((bfrom.length < 1) || (bto.lenght < 1))))
      {  
         if (BMD_Type == "parish")
            dateerror = "Either a date or a year range to search - but not both\n"
         else
            dateerror = "Either a date of " + BMD_Type + " or a year range to search - but not both\n" 
         submitOK=false
      } 

      // Check a range has been entered correctly
      if (((bfrom.length > 1) && (bto.length < 1)) ||
         ((bfrom.length < 1) && (bto.length > 1)))
      {
         rangeerror = "Enter a From year and To year\n\n"
         submitOK=false
      }

      if ((bto > 1) && (bfrom > 1))
      {
         if (BMD_Type == "parish")
         {
            // Check To_Year and From_Year dates in range 1538-2006
            if ((bto < 1538) || (bto > 2006) || (bfrom < 1538) || (bfrom > 2006))
            {
               boundserror = "Years must be between 1538 and present\n";
               submitOK=false
            }
         }
         else
         {
            // Check To_Year and From_Year dates in range 1837-2005
            if ((bto < 1837) || (bto > 2005) || (bfrom < 1837) || (bfrom > 2005))
            {
               boundserror = "Years must be between 1837 and 2005\n";
               submitOK=false
            }
         }
         // Check From_Year is less than To_Year
         if (bto < bfrom)
         {
            yearerror = "From year is less than To year\n"
            submitOK=false
         }
         // Check From_Year and To_Year range is not more than 10 years
         //if ((bto - bfrom + 1) > 10)
         //{
         //  tenerror = "Range must not be more than 10 years\n";
         //  submitOK=false
         //}
      }
   }
 
   if (submitOK==false) 
   {  
      alert("Please enter the following;\n\n" + csurnameerror + bsurnameerror + parishtypeerror + censuserror + censusageerror + censuscountyerror + parisherror + dateerror + rangeerror + yearerror + boundserror + tenerror);
      return false
  } 
}

//----------------------------------------------------------
// Get_Parm
// Read parameters passed from URL string to page
//----------------------------------------------------------
function Get_Parm(string,parm) 
{
   // returns value of parm from string
   var startPos = string.indexOf(parm + "=");
   if (startPos > -1) 
   {
      startPos = startPos + parm.length + 1;
      var endPos = string.indexOf("&",startPos);
      if (endPos == -1)
         endPos = string.length;
      return unescape(string.substring(startPos,endPos));
   }
   return '';
}


//----------------------------------------------------------
// Calc_Price
// Calulate and display the total price
//----------------------------------------------------------
function Calc_Price(Base_Price, Single, Second_Price)
{
   if (Search_DOB.length < 1)  
   {
      Total_Years = Year_To - Year_From + 1;
      s ="s";
      if (Year_To == Year_From)
      {
        Total_Years = 1;
        s = "";
      }
     
      document.write("<b>Search</b> : " + Total_Years + " year" + s + " from " + Year_From + " to " + Year_To + "<hr>");
       
      if (Single)
         document.write("<b>Total Amount To Pay = £" + (Base_Price) + ".00</b>");
      else if (Second_Price > 0)
      {
         document.write("<b>Total Amount To Pay = £" + (Base_Price + (Second_Price * (Total_Years - 1))) + ".00</b>");
      }
      else
         document.write("<b>Total Amount To Pay = £" + (Base_Price * Total_Years) + ".00</b>");
   }
   else 
   {
      document.write("<b>Search</b> : " + Search_DOB + " - 1 year<hr>");
      document.write("<b>Total Amount To Pay = £" + Base_Price +"</b>");
      Total_Years = 1;
   }
}

//-->