function nextPage() {
  pageIndex ++;
  if (pageIndex >= pages.length) {
    pageIndex = 0;
  }
  document.getElementById("guide").src = prefix + pages[pageIndex];
}

function previousPage() {
  pageIndex --;
  if (pageIndex < 0) {
    pageIndex = pages.length - 1;
  }
  document.getElementById("guide").src = prefix + pages[pageIndex];
}

function validateForm() {
  var theForm = document.orderForm;
  
  if (theForm.firstName.value == "") {
  	alert("Please enter a value for the \"First Name\" field.");
  	return false;
  }
  if (theForm.lastName.value == "") {
  	alert("Please enter a value for the \"Last Name\" field.");
  	return false;
  }
  if (theForm.email.value == "") {
  	alert("Please enter a value for the \"Email\" field.");
  	return false;
  }
  if (validateEmail(theForm.email.value) == false) {
  	alert("Please enter a valid \"Email\" address.");
  	return false;
  }
  if (theForm.streetAddress1.value == "") {
  	alert("Please enter a value for the \"Address\" field.");
  	return false;
  }
  if (theForm.city.value == "") {
  	alert("Please enter a value for the \"City\" field.");
  	return false;
  }
  if (theForm.state.value == "") {
  	alert("Please enter a value for the \"State\" field.");
  	return false;
  }
  if (theForm.zip.value == "") {
  	alert("Please enter a value for the \"Zip\" field.");
  	return false;
  }
	
	calculateTotal();
  submitForm();
}

function validateEmail(email) {
  return (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
}

function validatePhoneNumber(phoneNumber) {
  var stripped = phoneNumber.replace(/[\(\)\.\-\ ]/g, '');
  stripped = parseInt(stripped);
  if (isNaN(stripped)) {
  	return false;
  }
  stripped = stripped + "";
  if (!(stripped.length == 10)) {
  	return false;
  }
  return true;
}

function submitForm() {
  var theForm = document.orderForm;
  theForm.submit();
}

function calculateTotal() {
  var theForm = document.orderForm;
  var total = 0;
  var paypalMessage = "";
  var numGuides = 1;
  
  if (theForm.delivery.value == "mail") {
    numGuides = theForm.numGuides.value;
    total = theForm.numGuides.value * priceGuide;
  }
  else {
    total = priceGuide;
  }

  if (theForm.delivery.value == "mail") {
    total = parseFloat(total) + parseFloat(priceShipping);
    if (total.toFixed) total = total.toFixed(2);
    paypalMessage = "Online order for " + numGuides + " Access in San Diego Guide(s): $" + total;
    paypalMessage += " (incl. $" + priceShipping + " for shipping)";
  }
  else {
    if (total.toFixed) total = total.toFixed(2);
    paypalMessage = "Online order for 1 downloadable Access in San Diego Guide: $" + total;
    
  }

  document.getElementById("totalValue").value = total;
  document.getElementById("paypalMessage").value = paypalMessage;
}

function setDelivery(delivery) {
  if (delivery.value == "mail") {
    document.getElementById("deliveryOptions").style.display="block";
    document.getElementById("deliveryCharge").style.display="block";
  }
  else {
    document.getElementById("deliveryOptions").style.display="none";
    document.getElementById("deliveryCharge").style.display="none";
  }
}

