// Calculating delivery.

function UpdateDelivery()
 {
  var deliveryType
  deliveryType = document.deliveryForm.deliveryType[document.deliveryForm.deliveryType.selectedIndex].value
  
  window.location.href = "basket.asp?action=deliver&dType=" + deliveryType
 }

// Opening the window.

function popUp(URL)
{

	var window_name = "lledomodels_large"
	var window_width = 665
	var window_height = 470
	var window_left = (screen.availWidth / 2) - (window_width / 2)
	var window_top = (screen.availHeight / 2) - (window_height / 2)
	var window_dimensions = "height=" + window_height + ",width=" + window_width + ",left=" + window_left + ",top=" + window_top

	window.open(URL, window_name, window_dimensions)

}

// Closing the window.

function closeWin()
{
	self.close();
}

// This function finds out whether a character is allowed, uppercase, lowercase or a space.

function is_letter(character)
{
	var lowercase_letters = "abcdefghijklmnopqrstuvwxyz ";
	var uppercase_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";

	if (lowercase_letters.indexOf(character) == -1 &&
		uppercase_letters.indexOf(character) == -1)
			{
			return false;
			}
	return true;
}

// This function uses the previous to find out if a string is alphabetical only.

function is_alphabetic(string_value)
{
	for (var counter = 0; counter < string_value.length; counter++)
		{
		current_char = string_value.charAt(counter)

		if (!is_letter(current_char))
			{
			return false;
			}
		}
	return true;
}

// This function checks to see if an email address is valid or not - minimum email: a@abc.uk

function valid_email(email_address)
{
	if (email_address.length < 5)
		{
		return false;
		}

	at_location = email_address.indexOf("@");
	dot_location = email_address.lastIndexOf(".");

	if (at_location == -1 || dot_location == -1 || at_location > dot_location)
		{
		return false;
		}

	if (at_location == 0)
		{
		return false;
		}

	if (dot_location - at_location <= 3)
		{
		return false;
		}

	if (email_address.length - dot_location <= 2)
		{
		return false;
		}

	return true;
}

// Begin checkout form code.

// This function sets the focus on the form to prompt for the users name.

function initialise_checkout_form()
{
	document.checkout.FirstName.focus();
}

// This function checks the fields against the various parameters to see if they are valid of not.

function validate_checkout_form()
{
	if ((document.checkout.FirstName.value.length < 3) || (is_alphabetic(document.checkout.FirstName.value)) == false)
		{
		document.checkout.FirstName.focus();
		alert("Please enter your first name.");
		return false;
		}

	if ((document.checkout.Surname.value.length < 3) || (is_alphabetic(document.checkout.Surname.value)) == false)
		{
		document.checkout.Surname.focus();
		alert("Please enter your surname.");
		return false;
		}

	if (document.checkout.Address1.value.length < 5)
		{
		document.checkout.Address1.focus();
		alert("Please enter your address line 1.");
		return false;
		}

	if (document.checkout.Address2.value.length < 5)
		{
		document.checkout.Address2.focus();
		alert("Please enter your address line 2.");
		return false;
		}

	if (document.checkout.Postcode.value.length < 6)
		{
		document.checkout.Postcode.focus();
		alert("Please enter your postcode.");
		return false;
		}

	if (document.checkout.Telephone.value.length < 11)
		{
		document.checkout.Telephone.focus();
		alert("Please enter your telephone number.");
		return false;
		}

	if (valid_email(document.checkout.Email.value) == false)
		{
		document.checkout.Email.focus();
		alert("Please enter a valid Email address.");
		return false;
		}

	return true;
}

// Begin search form code

// This function sets the focus on the form to prompt for the users search term.

function initialise_search_form()
 {
  document.LledoSearch.searchTerm.focus();
 }

// This function checks that the users search term meets the minimum length requirement.

function check_search_length()
 {
  if (document.LledoSearch.searchTerm.value.length < 3)
   {
    document.LledoSearch.searchTerm.focus();
    window.alert("Error: Please enter at least 3 characters as your search term.");
    return false;
   }

  return true;
 }
