function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ClearOptions() { 
  document.BookingForm.elements['collection_delivery_detail'][0].checked = false;
  document.BookingForm.elements['collection_delivery_detail'][1].checked = false;
  document.BookingForm.elements['collection_delivery_detail'][2].checked = false;
  document.BookingForm.elements['Payment_Details'][0].checked = false;
  document.BookingForm.elements['Payment_Details'][1].checked = false;
  document.BookingForm.elements['Payment_Details'][2].checked = false;
  document.BookingForm.elements['Payment_Details'][3].checked = false;
  document.BookingForm.elements['Booking_include_a_vehicle'][0].checked = false;
  document.BookingForm.elements['Booking_include_a_vehicle'][1].checked = false;
}
function CopyCollection() { 
   document.BookingForm.Collection_Contact_Name.value = document.BookingForm.Full_Name.value;
   document.BookingForm.Collection_Phone.value = document.BookingForm.Phone.value;
   document.BookingForm.Collection_Address.value = document.BookingForm.Postal_Address.value;
   document.BookingForm.Delivery_Contact_Name.value = '';
   document.BookingForm.Delivery_Phone.value = '';
   document.BookingForm.Delivery_Address.value = '';
}
function CopyDelivery() { 
   document.BookingForm.Delivery_Contact_Name.value = document.BookingForm.Full_Name.value;
   document.BookingForm.Delivery_Phone.value = document.BookingForm.Phone.value;
   document.BookingForm.Delivery_Address.value = document.BookingForm.Postal_Address.value;
   document.BookingForm.Collection_Contact_Name.value = '';
   document.BookingForm.Collection_Phone.value = '';
   document.BookingForm.Collection_Address.value = '';
}
function RemoveDetails() { 
   document.BookingForm.Delivery_Contact_Name.value = '';
   document.BookingForm.Delivery_Phone.value = '';
   document.BookingForm.Delivery_Address.value = '';
   document.BookingForm.Collection_Contact_Name.value = '';
   document.BookingForm.Collection_Phone.value = '';
   document.BookingForm.Collection_Address.value = '';
}
function CheckBookingFields() { 
     if (!document.BookingForm.elements['Payment_Details'][1].checked) {
	if ((trim(document.BookingForm.Quote_Ref.value) == '') && (trim(document.BookingForm.Voucher_No.value) == '') ) {
		// something is wrong
		alert('please enter your quote reference number or voucher number');
                            document.BookingForm.Quote_Ref.focus();
		return false;
	}
	if ((trim(document.BookingForm.Quote_Ref.value) != '') && ((document.BookingForm.Quote_Ref.value.length < 2) || (document.BookingForm.Quote_Ref.value.length > 10)) ) {
		// something is wrong

		alert('please enter your quote reference number correctly, it should be alpha-numeric with a minimum of 2 and a maximum of 10 characters.');
                            document.BookingForm.Quote_Ref.focus();
		return false;
	}
	if ((trim(document.BookingForm.Voucher_No.value) != '')  && ((document.BookingForm.Voucher_No.value.length < 2) || (document.BookingForm.Voucher_No.value.length > 10)) ) {
		// something is wrong

		alert('please enter your voucher number correctly, it should be alpha-numeric with a minimum of 2 and a maximum of 10 characters.');
                            document.BookingForm.Voucher_No.focus();
		return false;
	}
  }

	if ((trim(document.BookingForm.Full_Name.value) == '')) {
		// something is wrong
		alert('please enter your full name');
                            document.BookingForm.Full_Name.focus();
		return false;
	}
	if ((trim(document.BookingForm.Postal_Address.value) == '')) {
		// something is wrong
		alert('please enter your address');
                            document.BookingForm.Postal_Address.focus();
		return false;
	}
	if ((trim(document.BookingForm.Phone.value) == '')) {
		// something is wrong
		alert('please enter your phone number');
                            document.BookingForm.Phone.focus();
		return false;
	}
	 if ((trim(document.BookingForm.Email.value) == '')) {
		// something is wrong
		alert('please enter your email');
                            document.BookingForm.Email.focus();
		return false;
	}


	 if ((trim(document.BookingForm.Collection_Contact_Name.value) == '')) {
		// something is wrong
		alert('please enter your collection contact name');
                            document.BookingForm.Collection_Contact_Name.focus();
		return false;
	}
	 if ((trim(document.BookingForm.Collection_Phone.value) == '')) {
		// something is wrong
		alert('please enter your collection phone');
                            document.BookingForm.Collection_Phone.focus();
		return false;
	}
	 if ((trim(document.BookingForm.Collection_Address.value) == '')) {
		// something is wrong
		alert('please enter your collection address');
                            document.BookingForm.Collection_Address.focus();
		return false;
	}
	 if ((trim(document.BookingForm.Delivery_Contact_Name.value) == '')) {
		// something is wrong
		alert('please enter your delivery contact name');
                            document.BookingForm.Delivery_Contact_Name.focus();
		return false;
	}
	 if ((trim(document.BookingForm.Delivery_Phone.value) == '')) {
		// something is wrong
		alert('please enter your delivery phone');
                            document.BookingForm.Delivery_Phone.focus();
		return false;
	}
	 if ((trim(document.BookingForm.Delivery_Address.value) == '')) {
		// something is wrong
		alert('please enter your delivery address');
                            document.BookingForm.Delivery_Address.focus();
		return false;
	}
 if ((!document.BookingForm.elements['Booking_include_a_vehicle'][0].checked) && (!document.BookingForm.elements['Booking_include_a_vehicle'][1].checked)){
     	       alert('Does this booking include a vehicle?');
                            document.BookingForm.elements['Booking_include_a_vehicle'][0].focus();
		return false;
}
 if ((document.BookingForm.elements['Booking_include_a_vehicle'][0].checked) && (eval("typeof(document.BookingForm.Vehicle_registration) != 'undefined'")) && (trim(document.BookingForm.Vehicle_registration.value) == '')  && (!document.BookingForm.Registration_plates.checked) ) {
     		alert('Please enter the vehicle registration/s.  If the vehicle is not registered, then check the No registration plates box');
                            document.BookingForm.Vehicle_registration.focus();
		return false;
}

if ((document.BookingForm.elements['Payment_Details'][1].checked) && (trim(document.BookingForm.Account_Number.value) == ''))
   {
     		alert('please enter your account number');
                            document.BookingForm.Account_Number.focus();
		return false;
   }

 if ((!document.BookingForm.elements['Payment_Details'][0].checked) && (!document.BookingForm.elements['Payment_Details'][1].checked) && (!document.BookingForm.elements['Payment_Details'][2].checked) && (!document.BookingForm.elements['Payment_Details'][3].checked))
   {
     		alert('Please select your payment option');
                           document.BookingForm.elements['Payment_Details'][0].focus();
		return false;
   }
 if ((!document.BookingForm.Agree_Terms_and_Conditions.checked) )
   {
     		alert('Do you agree our terms and conditions?');
                            document.BookingForm.Agree_Terms_and_Conditions.focus();
		return false;
   }
	
  return true; 

}

function Validate_String(string, return_invalid_chars)
         {
         valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         invalid_chars = '';
         
         if(string == null || string == '')
            return(true);
         
         //For every character on the string.   
         for(index = 0; index < string.length; index++)
            {
            char = string.substr(index, 1);                        
            
            //Is it a valid character?
            if(valid_chars.indexOf(char) == -1)
              {
              //If not, is it already on the list of invalid characters?
              if(invalid_chars.indexOf(char) == -1)
                {
                //If it's not, add it.
                if(invalid_chars == '')
                   invalid_chars += char;
                else
                   invalid_chars += ', ' + char;
                }
              }
            }                     

         //If the string does not contain invalid characters, the function will return true.
         //If it does, it will either return false or a list of the invalid characters used
         //in the string, depending on the value of the second parameter.
         if(return_invalid_chars == true && invalid_chars != '')
           {
           last_comma = invalid_chars.lastIndexOf(',');
           
           if(last_comma != -1)
              invalid_chars = invalid_chars.substr(0, $last_comma) + 
              ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
                      
           return(invalid_chars);
           }
         else
           return(invalid_chars == ''); 
         }


function Validate_Email_Address(email_address)
         {
   
         //Assumes that valid email addresses consist of user_name@domain.tld
         at = email_address.indexOf('@');
        domain_name = email_address.substr(at + 1, email_address.length);   
         dot = domain_name.indexOf('.');
  r = 0;
  for (i=0; i < domain_name.length; i++) {
    if (domain_name.charAt(i) == '.' ) {
      r = r+1;
      }
  }

         if(at == -1 ||   dot == -1 ||  dot == 0 || r >= 3 || dot == email_address.length - 1)
            return(false);

         user_name = email_address.substr(0, at);
         domain_name = email_address.substr(at + 1, email_address.length);                  
       
         if(Validate_String(user_name) === false || 
            Validate_String(domain_name) === false)
            return(false);                     
         
         return(true);
         }
