﻿// JScript File

// This ONLY works for IE, but should not muck up any other browsers, just not trap the event
//window.onbeforeunload = stopper;

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/uk_terms", "TsAndCs", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizeable=no, width=800, height=400, left=10, top=10");
	//var wdwTandC = window.open("http://www.hempsted-it.co.uk/RTC/terms-and-conditions.html", "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 runMe(theForm)
{
    if (theForm.txtSurname.value == "OK")
    {
        alert("Running it");
        document.frmBookingUK.btnConfirmBooking.click();
    }
    else
    {   
        alert("Nope!!!!!!!!!!!!!!");
        return false;        
    }
}    

function SeatsSelector(theForm)
{
    var gash = "";
    gash = "Seats chosen were:" + document.activeElement.id + " Value: " + document.activeElement.value; 
    gash += "srcename: " + window.event.srcElement.id + " srceval:" + window.event.srcElement.value;
    alert(gash);
    theForm.txtTotalCost.value = (parseInt(window.event.srcElement.value,10) * 49)
}

function ValidateBookingForm(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);
  }

  if (theForm.ddlStations.value == "0")
  {
    alert("Please choose your joining station.");
    theForm.ddlStations.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);
  }
}

/* 5.20 add validation for TotalPaid - but only for a Back Office Booking */
  if ( theForm.hid_UMode.value != "" )
  {
  
      // 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 ( theForm.txtManualDiscountDetails.value == "" )
       {
         alert("You have supplied a Manual Discount amount but NOT supplied a description.");
         theForm.txtManualDiscountDetails.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);
       }
      }
      
      if (theForm.txtTotalPaid.value != "")
      {
        if (isNumeric(theForm.txtTotalPaid.value) == false )
        {
            alert("Please enter proper amount for \"Total Paid\".");
            theForm.txtTotalPaid.focus();
            return (false);
        }
      }
  }
/*  if (theForm.txtCardNo.value == "")
  {
    alert("Please enter a value for the \"Credit Card Number\" field.");
    theForm.txtCardNo.focus();
    return (false);
  }

  if (theForm.txtCardNo.value.length < 16)
  {
    alert("Please enter all 16 characters in the \"Credit Card Number\" field.");
    theForm.txtCardNo.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.txtCardNo.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  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;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Credit Card Number\" field.");
    theForm.txtCardNo.focus();
    return (false);
  }

  // 24/08/06 MJW - 5th Part of Credit/Debit Card - max. 3 digits - ONLY mandatory for Maestro/Switch cards
  // 25/08/06 MJW - 5th Part of Credit/Debit Card - max. 3 digits - ONLY all other validation removed by agreement with DL 
  var checkOK = "0123456789-";
  var checkStr = theForm.txtCardExtraNo.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  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;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Last Part of Credit Card Number\" field.");
    theForm.txtCardExtraNo.focus();
    return (false);
  }
  
  if (theForm.StartDateMM.selectedIndex < 0)
  {
    alert("Please select one of the \"Start Date Month\" options.");
    theForm.StartDateMM.focus();
    return (false);
  }

  if (theForm.ExpiryDateMM.selectedIndex < 0)
  {
    alert("Please select one of the \"Expiry Date Month\" options.");
    theForm.ExpiryDateMM.focus();
    return (false);
  }

	// 12/04/06 mjw - extra CR card validation
	// check expiry year is > start year
	if ( parseInt(theForm.StartDateYY.value) >= parseInt(theForm.ExpiryDateYY.value) )
	{
	    alert("Expiry Date MUST be later than Start Date");
	    theForm.ExpiryDateYY.focus();
	    return (false);
	}

	// check card hasn't expired
	// 15/09/06 MJW - add Radix of 10 as 2nd Parm. to parseInt as it is incorrectly evaluating 8 and 9 ...
	// ... and correct December ExpiryDate logic to prevent years like 20061 !!
	var ExpiryDateTest;
	if ( parseInt(theForm.ExpiryDateMM.value) == 12 )
		ExpiryDateTest = "01 Jan " + (parseInt(theForm.ExpiryDateYY.value,10) + 1);
	else
		ExpiryDateTest = "01 " + MMtoMMM((parseInt(theForm.ExpiryDateMM.value,10) + 1)) + " " + theForm.ExpiryDateYY.value
	
	var checkDate = new Date(ExpiryDateTest);
	var now = new Date();
	var diff = checkDate.getTime() - now.getTime();
	var days = Math.floor(diff / (1000 * 60 * 60 * 24));
	if ( days <= 0 )
	{
	    alert("CREDIT CARD HAS EXPIRED - PLEASE USE A DIFFERENT CARD OR AMEND THE EXPIRY DATE");
	    theForm.ExpiryDateYY.focus();
	    return (false);
	}

  var checkOK = "0123456789-";
  var checkStr = theForm.intIssueNo.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  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;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Card Issue Number\" field.");
    theForm.intIssueNo.focus();
    return (false);
  }

  if (theForm.intCardSecurityNumber.value == "")
  {
    alert("Please enter a value for the \"Card Security Number\" field.");
    theForm.intCardSecurityNumber.focus();
    return (false);
  }

  if (theForm.intCardSecurityNumber.value.length < 3)
  {
    alert("Please enter at least 3 characters in the \"Card Security Number\" field.");
    theForm.intCardSecurityNumber.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.intCardSecurityNumber.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  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;
    }
    allNum += ch;
  }

  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Card Security Number\" field.");
    theForm.intCardSecurityNumber.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);
  }

  theForm.txtInitials.value = trimAll(theForm.txtInitials.value);
  theForm.txtInitials.value = theForm.txtInitials.value.toUpperCase();
  
  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);
  }

  theForm.txtSurname.value = trimAll(theForm.txtSurname.value);
  theForm.txtSurname.value = toProperCase(theForm.txtSurname.value);  
  
  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);
  }

  theForm.txtHouse.value = trimAll(theForm.txtHouse.value);
  theForm.txtHouse.value = toProperCase(theForm.txtHouse.value);    

  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);
  }

  theForm.txtRoad.value = trimAll(theForm.txtRoad.value);
  theForm.txtRoad.value = toProperCase(theForm.txtRoad.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.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);
  }

  theForm.txtArea.value = trimAll(theForm.txtArea.value);
  theForm.txtArea.value = toProperCase(theForm.txtArea.value);        

  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);
  }

  theForm.txtTown.value = trimAll(theForm.txtTown.value);
  theForm.txtTown.value = theForm.txtTown.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.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();  
  
  // Add validation for Deliver Address IF it has been provided
  if (theForm.chkUseDeliveryAddress.checked == 1)
  {
    if (ValidateDeliveryAddress(theForm,"") == false )
    {
        return (false);    
    }
  }
        
    // 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);
          }
      }

  // 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);
  }

  theForm.txtPhoneDay.value = trimAll(theForm.txtPhoneDay.value);

  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);
  }
  
  theForm.txtPhoneEve.value = trimAll(theForm.txtPhoneEve.value);

  //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

    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);
        //	theForm.txtHid_EmailText.value = BuildBookingEmailBody(theForm,"CustomerCopy");
        //	theForm.txtHid_PFEmailText.value = BuildBookingEmailBody(theForm,"PFCopy");
        myTarget = document.getElementById("DontTouch");
        myTarget.innerText="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!";
        theForm.btnValidateBooking.focus();
        document.frmBookingUK.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 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 isNumeric(value) 
{
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

function leftTrim(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}

function rightTrim(sString) 
{
        while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

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(); });
}

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();    
  }
}
