﻿// JScript File

// This ONLY works for IE, but should not muck up any other browsers, just not trap the event
//window.onbeforeunload = stopper;

function TravelInsuranceWarning(theForm)
{
    myTarget = document.getElementById("TILink");
    if (theForm.txtTravelInsAmount.value == "N/A")
    {
        alert("myfont");
        myTarget.style="myfont";
//        alert("Warning: \n\nDue to the combination of cover options,\n" + 
//            "your age details and the constraints of the scheme,\n" +
//            "The Railway Touring Co. is unable to provide the insurance cover\n" + 
//            "you have chosen.\n" +
//            "Please contact RTC ASAP.");
    }
    else
    {
        myTarget.style="myhide";
    }
}

function stopper()
{
    var flag
    var mesg ="You are closing this window. You will lose any Booking information you have provided.";
    return mesg;
}

function OpenTandCWindow()
{
	var wdwTandC = window.open("http://www.railwaytouring.net/terms_and_conditions/eu-world_terms", "TsAndCs", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizeable=no, width=800, height=400, left=10, top=10");
	wdwTandC.focus();
}

function OpenTICsWindow()
{
	var wdwTandC = window.open("http://www.railwaytouring.co.uk/Resources/RTCPre-bookandTI.pdf", "TIConds", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizeable=no, width=800, height=400, left=10, top=10");
	wdwTandC.focus();
}

function ValidateBookingFormINT(theForm)
{

    myTarget = document.getElementById("DontTouch");
    myTarget.innerText="";

  if (theForm.ddlTours.value == "0")
  {
    alert("Please choose a tour to travel on.");
    theForm.ddlTours.focus();
    return (false);
  }

// only check for joining station IF we have some
  if (theForm.hid_JoiningStations.value == "True")
  {
      if (theForm.ddlStations.value == "0")
      {
        alert("Please choose your joining station.");
        theForm.ddlStations.focus();
        return (false);
      }
  }

// uk residency check validation
  var radioSelected = false;
  var intUKResidency = 0;
  for (i = 0;  i < theForm.rblUKResidency.length;  i++)
  {
    if (theForm.rblUKResidency[i].checked)
    {
        intUKResidency=i+1;
        radioSelected = true;
    }
  }
  if (!radioSelected)
  {
    alert("Please indicate wether you are a \"UK Resident\".");
    theForm.btnCheckAvailability.focus();    
    return (false);
  }

/*// travel insurance validation
  var radioSelected = false;
  for (i = 0;  i < theForm.rblTravelInsurance.length;  i++)
  {
    if (theForm.rblTravelInsurance[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please indicate your choice of \"Travel Insurance\".");
    theForm.btnCheckAvailability.focus();    
    return (false);
  }

// OK which travel insurance did we pick?

  //RTC insurance ..
  if (theForm.rblTravelInsurance[0].checked)
  {
    if ( theForm.txtTravelInsAmount.value == "" )
    {
        alert("Please complete our \"Travel Insurance\" Scheme details.");
        theForm.btnCheckAvailability.focus();    
        return (false);
    }
  }
*/

  //Customers own insurance ..
//  if (theForm.rblTravelInsurance[1].checked)
//  {
//    if ( theForm.txtOwnTI.value == "" )
//    {
//        alert("Please provide details of your own \"Travel Insurance\" Scheme.");
//        theForm.txtOwnTI.focus();    
//        return (false);
//    }
//  }

// payment validation starts here - only run this bit if we came from the Booking Admin System
  if ( theForm.hid_UMode.value != "" )
  {
      var radioSelected = false;
      for (i = 0;  i < theForm.rblPaymentMethod.length;  i++)
      {
        if (theForm.rblPaymentMethod[i].checked)
        {
            radioSelected = true;
            break;
        }
      }
      if (!radioSelected)
      {
        alert("Please select one of the \"Payment Methods\" options.");
        theForm.txtPromoCode.focus();    
        return (false);
      }
      else
      {
        // ok did we select a credit card?
        if ( i == 2 )
        {
          var radioSelected = false;
          for (i = 0;  i < theForm.rblCardTypes.length;  i++)
          {
            if (theForm.rblCardTypes[i].checked)
                radioSelected = true;
          }
          if (!radioSelected)
          {
            alert("Please select one of the \"Card Type\" options.");
            theForm.txtPromoCode.focus();    
            return (false);
          }
        }   
      }
   }
else
{   
// card validation starts here ...
  var radioSelected = false;
  for (i = 0;  i < theForm.rblCardTypes.length;  i++)
  {
    if (theForm.rblCardTypes[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Card Type\" options.");
    theForm.txtPromoCode.focus();    
    return (false);
  }
}

// validate passport info.
// 1st check at least one set of details has been provided
  if (thereAnyPassportDetails() == false)
  {
    alert("Please provide Passport details for at least one passenger.");
    theForm.chkPassport1.focus();
    return (false);
  }
  
// now check each set of PAX details for completeness

    var intTotalPassengers = 0;
  
  	for (var i = 1; i <= theForm.ddlNoPax.value ; i++) {
  	
      myTargetchk = document.getElementById("chkPassport" + i);	

      if ( myTargetchk.checked == true )
      {
          if (arePassportDetailsOK(i) == false )
          {
            alert("Passport holders details for passenger " + i + " are incomplete/incorrect, please check and correct them.");
            myTargetddl = document.getElementById("ddlPassportSalutation" + i);	            
            myTargetddl.focus();
            return (false);
          }
          else
          {
            intTotalPassengers += 1;
          }  
      }
      else
      {
            if ( i == 1) 
            {
                alert("The first Passport holders details MUST be completed.");
                myTargetchk.focus();
                return (false);
            }
      }
	}

// ensure Voucher amount specified IF we have chosen Voucher payment method - but only if we are a B/O BKG
if ( theForm.hid_UMode.value != "" )
{
  if ( theForm.rblPaymentMethod[4].checked )
  {
      if ( theForm.txtVoucherAmount.value == "" )
      {
         alert("You have chosen the Voucher Payment Method but not supplied a Voucher Amount.");
         theForm.txtVoucherAmount.focus();
         return (false);
      }
  }

  // ensure Voucher value is numeric, if supplied
  if ( theForm.txtVoucherAmount.value != "" )
  {
   if ( theForm.rblPaymentMethod[4].checked == false)
   {
         alert("You have supplied a Voucher Amount but not selected the Voucher Payment Method.");
         theForm.txtVoucherAmount.focus();
         return (false);
   }
   
   if ( isFP(theForm.txtVoucherAmount.value) == false )
   {
     alert("Please enter only numbers and a decimal point in the \"Voucher Amount\" field and re-apply the Voucher Discount.");
     theForm.txtVoucherAmount.focus();
     return (false);
   }

   if ( parseFloat(theForm.txtVoucherAmount.value) > 0 )
   {
     alert("You have supplied a Voucher Discount amount but NOT Applied it.");
     theForm.txtVoucherAmount.focus();
     return (false);
   }

  }

  // ensure Manual Discount value is numeric, if supplied
  if ( theForm.txtManualDiscount.value != "" )
  {
   
   if ( isFP(theForm.txtManualDiscount.value) == false )
   {
     alert("Please enter only numbers and a decimal point in the \"Manual Discount Amount\" field and re-apply the Manual Discount.");
     theForm.txtManualDiscount.focus();
     return (false);
   }

   if ( parseFloat(theForm.txtManualDiscount.value) > 0 )
   {
     alert("You have supplied a Manual Discount amount but NOT Applied it.");
     theForm.txtManualDiscount.focus();
     return (false);
   }

  }

}
  
// check that no of deposits requested = no of sets of passenger passport details
// V3.01 12/10/07 change test to look at 1st line only as that is where the deposit will be, plus
//       this will not try to include the bedrooms in the total

    var intNoDeposits = 0;
    var tempSeats;

    for ( var i = 1;  i < parseInt(theForm.hid_TotDeposits.value,10) + 1;  i++)
    {
        //alert("i: " + i + " " + eval("theForm.txtSeats" + i + ".value"));
        tempSeats = eval("theForm.txtSeats" + i);
        if ( tempSeats.value != "" )
        {
            intNoDeposits += parseInt(tempSeats.value,10);
        }
    }
    
    //if (theForm.txtSeats1.value != "") intNoDeposits += parseInt(theForm.txtSeats1.value,10);
    //if (theForm.txtSeats2.value != "") intNoDeposits += parseInt(theForm.txtSeats2.value,10);
    //if (theForm.txtSeats3.value != "") intNoDeposits += parseInt(theForm.txtSeats3.value,10);        
    //alert("intNoDeposits=" + intNoDeposits + " intTotalPassengers=" + intTotalPassengers);
    if ( intNoDeposits != intTotalPassengers )
    {
        alert("WARNING:\n\nYou have requested " + intNoDeposits + " deposit(s) but you have provided " + intTotalPassengers + 
            " sets of Passenger(s) details\n\nPlease complete the remaining Passenger details or correct the number of deposits");
        theForm.chkPassport1.focus();
        return (false);
    }
    
// N & A to go here...
// name and address block starts here ...
  if (theForm.ddlSalutation.value == "0")
  {
    alert("Please choose your salutation.");
    theForm.ddlSalutation.focus();
    return (false);
  }

  if (theForm.txtInitials.value == "")
  {
    alert("Please enter a value for the \"Initials\" field.");
    theForm.txtInitials.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz \t\r\n\f";
  var checkStr = theForm.txtInitials.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters and whitespace characters in the \"Initials\" field.");
    theForm.txtInitials.focus();
    return (false);
  }

  if (theForm.txtSurname.value == "")
  {
    alert("Please enter a value for the \"Surname\" field.");
    theForm.txtSurname.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'- \t\r\n\f";
  var checkStr = theForm.txtSurname.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters, whitespace characters or apostrophes in the \"Surname\" field.");
    theForm.txtSurname.focus();
    return (false);
  }

  if (theForm.txtHouse.value == "")
  {
    alert("Please enter a value for the \"House No./Name\" field.");
    theForm.txtHouse.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/'- \t\r\n\f";
  var checkStr = theForm.txtHouse.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter, digit and whitespace characters in the \"House No./Name\" field.");
    theForm.txtHouse.focus();
    return (false);
  }

  if (theForm.txtRoad.value == "")
  {
    alert("Please enter a value for the \"Road Name\" field.");
    theForm.txtRoad.focus();
    return (false);
  }
  
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/,'- \t\r\n\f";
  var checkStr = theForm.txtRoad.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters, digits or whitespace characters in the \"Road\" field.");
    theForm.txtRoad.focus();
    return (false);
  }
  
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/'- \t\r\n\f";
  var checkStr = theForm.txtArea.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters, digits or whitespace characters in the \"Area\" field.");
    theForm.txtArea.focus();
    return (false);
  }

  if (theForm.txtTown.value == "")
  {
    alert("Please enter a value for the \"Town\" field.");
    theForm.txtTown.focus();
    return (false);
  }
  
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/'    - \t\r\n\f";
  var checkStr = theForm.txtTown.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters, digits or whitespace characters in the \"Town\" field.");
    theForm.txtTown.focus();
    return (false);
  }

// 5.09 run standard post code/zip code checks for us/uk and ww, i.e. everybody
      if (theForm.txtPostCode.value == "")
      {
        alert("Please enter a value for the \"Post Code\" field.");
        theForm.txtPostCode.focus();
        return (false);
      }

      if (theForm.txtPostCode.value.length > 9)
      {
        alert("Please enter at most 9 characters in the \"Post Code\" field.");
        theForm.txtPostCode.focus();
        return (false);
      }

      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789- \t\r\n\f";
      var checkStr = theForm.txtPostCode.value;
      var allValid = true;
      var validGroups = true;
      for (i = 0;  i < checkStr.length;  i++)
      {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
          if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
          allValid = false;
          break;
        }
      }
      if (!allValid)
      {
        alert("Please enter only letter, digit and whitespace characters in the \"Post Code\" field.");
        theForm.txtPostCode.focus();
        return (false);
      }

    // 5.09 only run this if we are a UK PAX
    if ( theForm.ddlCountry.value == "93" )
    {
      if (isPostCodeValid(theForm.txtPostCode.value) == false )
      {
        alert("The Post Code field is NOT in a recognised format, please check and change it.");
        theForm.txtPostCode.focus();
        return (false);
      }
    }
  
  theForm.txtPostCode.value = theForm.txtPostCode.value.toUpperCase();  
    
    // only check for US State IF we have chosen USA
      if (theForm.ddlCountry.value == "89")
      {
          if (theForm.ddlUS_State.selectedIndex == "0")
          {
            alert("Please choose your State.");
            theForm.ddlUS_State.focus();
            return (false);
          }
      }

  // Add validation for Deliver Address IF it has been provided
  if (theForm.chkUseDeliveryAddress.checked == 1)
  {
    if (ValidateDeliveryAddress(theForm,"") == false )
    {
        return (false);    
    }
  }

// cross-check country chosen with UK-REsident Y/N?
  // OK we've chosen UK but we've selected NON-UK resident
  if ( (theForm.ddlCountry.value == "93") && ( intUKResidency == "2" ) )
  {
    alert("You have indicated you are NOT a Resident of the UK,\nbut you have chosen the United Kingdom " +
        "for your Address. \nPlease check your booking.");
    theForm.ddlNoPax.focus();
    return (false);
  }

  // OK we've chosen UK but we've selected NON-UK resident
  if ( (theForm.ddlCountry.value != "93") && ( intUKResidency == "1" ) )
  {
    alert("You have indicated you are Resident in the UK,\nbut you have not chosen the United Kingdom " +
        "for your Address. \nPlease check your booking.");
    theForm.ddlNoPax.focus();
    return (false);
  }

  // Email Address validation ... only applicable IF we are public, non-mandatory for RTC office
  if ((theForm.hid_UMode.value == "") && (theForm.txtEmailAddress1.value == ""))
  {
    alert("Please enter a value for the \"Email Address\" field.");
    theForm.txtEmailAddress1.focus();
    return (false);
  }

  // 30/08/06 MJW Added underscore to list of acceptable characters ...
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@-._";
  var checkStr = theForm.txtEmailAddress1.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter, digit and \"@-.\" characters in the \"Email Address\" field.");
    theForm.txtEmailAddress1.focus();
    return (false);
  }

  if ( theForm.txtEmailAddress1.value != "" )
  {
      if (isEmailValid(theForm.txtEmailAddress1.value) == false )
      {
        alert("The Email Address field is NOT in a recognised format, please check and change it.");
        theForm.txtEmailAddress1.focus();
        return (false);
      }
  }

  if ((theForm.hid_UMode.value == "") && (theForm.txtEmailAddress2.value == ""))
  {
    alert("Please enter a value for the \"Email Address\" field.");
    theForm.txtEmailAddress2.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@-._";
  var checkStr = theForm.txtEmailAddress2.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter, digit and \"@-.\" characters in the \"Email Address\" field.");
    theForm.txtEmailAddress2.focus();
    return (false);
  }

  if ( theForm.txtEmailAddress1.value != "" )
  {
      if (isEmailValid(theForm.txtEmailAddress2.value) == false )
      {
        alert("The Email Address field is NOT in a recognised format, please check and change it.");
        theForm.txtEmailAddress2.focus();
        return (false);
      }
  }

  if (theForm.txtEmailAddress1.value != theForm.txtEmailAddress2.value)
  {
    alert("Your Email Addresses ARE NOT the same, please enter a value for the \"Email Address\" field.");
    theForm.txtEmailAddress2.focus();
    return (false);
  }

  if (theForm.txtPhoneDay.value == "")
  {
    alert("Please enter a value for the \"Daytime Phone Number\".");
    theForm.txtPhoneDay.focus();
    return (false);
  }

  var checkOK = "0123456789- \t\r\n\f";
  var checkStr = theForm.txtPhoneDay.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only digits and whitespace characters in the \"Daytime Telephone Number\" field.");
    theForm.txtPhoneDay.focus();
    return (false);
  }

  var checkOK = "0123456789- \t\r\n\f";
  var checkStr = theForm.txtPhoneEve.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only digit and whitespace characters in the \"Evening Telephone Number\" field.");
    theForm.txtPhoneEve.focus();
    return (false);
  }

  //v5.10 new check to prevent injection attacks on Notes field
  if ( theForm.txtNotes.value != "" )
  {
      if (areNotesValid(theForm.txtNotes.value) == false )
      {
        alert("Your Booking Notes contain special characters, please check and remove them.");
        theForm.txtNotes.focus();
        return (false);
      }
  }
  
// N & A block ends here

// Validation for optional Booking Override Date if it has been specified - B/O BKG Only
if ( theForm.hid_UMode.value != "" )
{
    if (theForm.txtOverrideDate.value != "")
    {
        if (isDate(theForm.txtOverrideDate.value) == false)
        {
         theForm.txtOverrideDate.focus();        
         return (false);
        } 
    }
}
    
    if ( theForm.chkTandCs.checked == false )
  {
    alert("Please Read and Accept the \"Terms and Conditions of Booking\".");
    theForm.chkTandCs.focus();
    return (false);
  }
    var mesg = "Please confirm that you wish to place your Booking with The Railway Touring Company?"
	if ( !window.confirm(mesg) ) return (false);
        myTarget = document.getElementById("DontTouch");
        myTarget.innerHTML="Thank You, your Booking is now being processed, this may take 1-2 minutes. " +
            "Please DO NOT click the Confirm button again or you may be charged twice for the booking!<br><br>";
        theForm.btnValidateBooking.focus();
        document.frmBookingINT.btnConfirmBooking.click();
}

function MMtoMMM(intMM)
{
	if (intMM == 1) return "Jan";
	if (intMM == 2) return "Feb";
	if (intMM == 3) return "Mar";
	if (intMM == 4) return "Apr";
	if (intMM == 5) return "May";
	if (intMM == 6) return "Jun";
	if (intMM == 7) return "Jul";
	if (intMM == 8) return "Aug";
	if (intMM == 9) return "Sep";
	if (intMM == 10) return "Oct";
	if (intMM == 11) return "Nov";
	if (intMM == 12) return "Dec";
}

function FormatCurrencyValue(Num)
{
	if ( Num.indexOf(".") == -1 )
	{
		return Num + ".00"
	}
	else
	{
		if ( Num.length - Num.indexOf(".") == 2)
			return Num + "0";
		else
			return Num;
	}
}

function areNotesValid(notes) 
{ 
    return /^[a-zA-Z0-9\s.\-\',;~!£?/&+*=:@()]*$/.test(notes); 
}

function isEmailValid(email) 
{ 
    return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,}$/.test(email); 
}

function arePassportDetailsOK(intNo) 
{ 
    myTarget = document.getElementById("ddlPassportSalutation" + intNo);
    if ( myTarget.value == "0") return false;

    myTarget = document.getElementById("txtPassportForename" + intNo);
    if ( myTarget.value == "") return false;

    myTarget = document.getElementById("txtPassportSurname" + intNo);
    if ( myTarget.value == "") return false;

    myTarget = document.getElementById("txtPassportDOB" + intNo);
    if ( myTarget.value == "") return false;
    
    if (isDate(myTarget.value) == false) return false;
    
    return true;
}

function thereAnyPassportDetails()
{
	for (var i = 1; i <= theForm.ddlNoPax.value ; i++) {
        myTarget = document.getElementById("chkPassport" + i);	
        if ( myTarget.checked == true ) return true;
	}
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(0,pos1)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : DD/MM/YYYY")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
    return true
}

function isPostCodeValid(postcode) 
{ 
    return /^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1}[ ]{1}[0-9][A-Za-z]{2}$/.test(postcode); 
}

function isFP(voucheramount)
{
    return regExp = /[-\d\.-]/.test(voucheramount);
}

function ValidateDeliveryAddress(theForm, strMode)
{
// delivery address block starts here ...
  if (theForm.ddlDA_Salutation.value == "0")
  {
    alert("Please choose your salutation.");
    theForm.ddlDA_Salutation.focus();
    return (false);
  }

  if (theForm.txtDA_Initials.value == "")
  {
    alert("Please enter a value for the \"Initials\" field.");
    theForm.txtDA_Initials.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz \t\r\n\f";
  var checkStr = theForm.txtDA_Initials.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters and whitespace characters in the \"Initials\" field.");
    theForm.txtDA_Initials.focus();
    return (false);
  }

  theForm.txtDA_Initials.value = trimAll(theForm.txtDA_Initials.value);
  theForm.txtDA_Initials.value = theForm.txtDA_Initials.value.toUpperCase();
  
  if (theForm.txtDA_Surname.value == "")
  {
    alert("Please enter a value for the \"Surname\" field.");
    theForm.txtDA_Surname.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'- \t\r\n\f";
  var checkStr = theForm.txtDA_Surname.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters, whitespace characters or apostrophes in the \"Surname\" field.");
    theForm.txtDA_Surname.focus();
    return (false);
  }

  theForm.txtDA_Surname.value = trimAll(theForm.txtDA_Surname.value);
  theForm.txtDA_Surname.value = toProperCase(theForm.txtDA_Surname.value);  
  
  if (theForm.txtDA_House.value == "")
  {
    alert("Please enter a value for the \"House No./Name\" field.");
    theForm.txtDA_House.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'- \t\r\n\f";
  var checkStr = theForm.txtDA_House.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter, digit and whitespace characters in the \"House No./Name\" field.");
    theForm.txtDA_House.focus();
    return (false);
  }

  theForm.txtDA_House.value = trimAll(theForm.txtDA_House.value);
  theForm.txtDA_House.value = toProperCase(theForm.txtDA_House.value);    

  if (theForm.txtDA_Road.value == "")
  {
    alert("Please enter a value for the \"Road Name\" field.");
    theForm.txtDA_Road.focus();
    return (false);
  }
  
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'- \t\r\n\f";
  var checkStr = theForm.txtDA_Road.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters, digits or whitespace characters in the \"Road\" field.");
    theForm.txtDA_Road.focus();
    return (false);
  }

  theForm.txtDA_Road.value = trimAll(theForm.txtDA_Road.value);
  theForm.txtDA_Road.value = toProperCase(theForm.txtDA_Road.value);      

/*  if (theForm.txtArea.value == "")
  {
    alert("Please enter a value for the \"Area\" field.");
    theForm.txtArea.focus();
    return (false);
  } */
  
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'- \t\r\n\f";
  var checkStr = theForm.txtDA_Area.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters, digits or whitespace characters in the \"Area\" field.");
    theForm.txtDA_Area.focus();
    return (false);
  }

  theForm.txtDA_Area.value = trimAll(theForm.txtDA_Area.value);
  theForm.txtDA_Area.value = toProperCase(theForm.txtDA_Area.value);        

  if (theForm.txtDA_Town.value == "")
  {
    alert("Please enter a value for the \"Town\" field.");
    theForm.txtDA_Town.focus();
    return (false);
  }
  
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'    - \t\r\n\f";
  var checkStr = theForm.txtDA_Town.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letters, digits or whitespace characters in the \"Town\" field.");
    theForm.txtDA_Town.focus();
    return (false);
  }

  theForm.txtDA_Town.value = trimAll(theForm.txtDA_Town.value);
  theForm.txtDA_Town.value = theForm.txtDA_Town.value.toUpperCase();  

// v5.05 added Country and US State validation
// 5.09 run standard post code/zip code checks for us/uk and ww, i.e. everybody
      if (theForm.txtDA_PostCode.value == "")
      {
        alert("Please enter a value for the \"Post Code\" field.");
        theForm.txtDA_PostCode.focus();
        return (false);
      }

      if (theForm.txtDA_PostCode.value.length > 9)
      {
        alert("Please enter at most 9 characters in the \"Post Code\" field.");
        theForm.txtDA_PostCode.focus();
        return (false);
      }

      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789- \t\r\n\f";
      var checkStr = theForm.txtDA_PostCode.value;
      var allValid = true;
      var validGroups = true;
      for (i = 0;  i < checkStr.length;  i++)
      {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
          if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
          allValid = false;
          break;
        }
      }
      if (!allValid)
      {
        alert("Please enter only letter, digit and whitespace characters in the \"Post Code\" field.");
        theForm.txtDA_PostCode.focus();
        return (false);
      }

      if (isPostCodeValid(theForm.txtDA_PostCode.value) == false )
      {
        alert("The Post Code field is NOT in a recognised format, please check and change it.");
        theForm.txtDA_PostCode.focus();
        return (false);
      }
     
  theForm.txtDA_PostCode.value = theForm.txtDA_PostCode.value.toUpperCase();  
  
  // All OK, so submit Delivery Address details to ASP.NET Process routines
  if (strMode == "SA" )
  {
    document.frmDADetails.btnProcessDADetails.click();    
  }
}

function trimAll(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
}

