function validateReview(form) {
  if (form.rating.value == -1) {
    alert("Please rate this establishment");
    form.rating.focus();
    return (false);
  }

  if (form.title.value.length == 0) {
    alert("Please enter a title for this review");
    form.title.focus();
    return (false);
  }

  if ((form.anonymous[1].checked) && (form.name.value.length == 0)) {
    alert("Please enter your name, or select anonymous");
    form.name.focus();
    return (false);
  }

  if (form.countryID.value == -1) {
    alert("Please select your country");
    form.countryID.focus();
    return (false);
  }

  if (form.comments.value.length == 0) {
    alert("Please enter a detailed comment");
    form.comments.focus();
    return (false);
  }

  return (true);
}

function validateLiveGuestAllocations(form) {
  if (!validateNumbers(form)) {
    return (false);
  }
  
  var roomTypeIDs = new Array();
  var adults = new Array();
  var children = new Array();

  totalElements = form.elements.length;
  numRoomTypes = 0;
  lastRoomTypeID = 0;
  lastRoomNumber = 0;
  firstBracketIndex = 0;
  secondBracketIndex = 0;
  thirdBracketIndex = 0;
  roomTypeID = 0;
  roomNumber = 0;
  inputName = "";
  for (count=0; count<totalElements; count++) {
    if(form.elements[count].type == "text") {
      inputName = form.elements[count].name;
      firstBracketIndex = inputName.indexOf('[');
      secondBracketIndex = inputName.indexOf(']');
      thirdBracketIndex = inputName.lastIndexOf('[');
      roomTypeID = parseInt(inputName.substring(firstBracketIndex+1, secondBracketIndex));
      roomNumber = parseInt(inputName.substring(thirdBracketIndex+1, inputName.length -1));
      if ((roomTypeID != lastRoomTypeID) || (roomNumber != lastRoomNumber)) {
        roomTypeIDs[numRoomTypes] = roomTypeID;
        adults[numRoomTypes] = 0;
        children[numRoomTypes] = 0;
        numRoomTypes++;
        lastRoomTypeID = roomTypeID;
        lastRoomNumber = roomNumber;
      }
      if (form.elements[count].value.length != 0) {
        if (inputName.indexOf("numberOfAdultsForRoom") != -1) {
          adults[numRoomTypes-1] = parseInt(form.elements[count].value);
        }
        else if (inputName.indexOf("numberOfChildrenForRoom") != -1) {
          children[numRoomTypes-1] = parseInt(form.elements[count].value);
        }
      }
    }
  }
  
  for (i=0; i<numRoomTypes; i++) {
    if ((adults[i] == 0) && (children[i] == 0)) {
      alert ("Please enter the number of adults and/or children for every room required");
      return (false);
    }
    if (adults[i] > maxAdultsForType[roomTypeIDs[i]]) {
      alert ("Please ensure that the number of adults <= maximum for that type");
      return (false);
    }
    if ((adults[i] + children[i]) > maxGuestsForType[roomTypeIDs[i]]) {
      alert ("Please ensure that the number of adults plus children\nfor every room is <= maximum for that type");
      return (false);
    }
  }
 
  return (true);
}

function validateLiveRoomTypesRequired(form) {
  if (!validateNumbers(form)) {
    return (false);
  }
  
  var rooms = new Array();

  totalElements = form.elements.length;
  numRoomsOfType = 0;
  for (count=0; count<totalElements; count++) {
    if(form.elements[count].type == "text") {
      if (form.elements[count].value.length != 0) {
        numRoomsOfType = parseInt(form.elements[count].value);
        if (numRoomsOfType > 0) {
          return (true);
        }
      }
    }
  }
  
  alert("You have not entered any room requirements");
  return (false);
}

function validateLiveReservationPaymentForm(form) {
  if (validateLiveReservationForm(form)) {
    return validateLivePaymentForm(form);
  } else {
    return false;
  }
}

function validateLiveReservationForm(form) {
  if (form.firstName.value.length == 0) {
    alert("Please enter your first name");
    form.firstName.focus();
    return (false);
  }
  if (form.surname.value.length == 0) {
    alert("Please enter your surname");
    form.surname.focus();
    return (false);
  }
  if (form.phoneNumber.value.length == 0) {
    alert("Please enter your full telephone number");
    form.phoneNumber.focus();
    return (false);
  }
  if (form.emailAddress.value.length == 0) {
    alert("Please enter your email address");
    form.emailAddress.focus();
    return (false);
  }
  atIndex = form.emailAddress.value.indexOf('@');
  if (atIndex == -1) {
    alert("Please enter a valid email address");
    form.emailAddress.focus();
    return (false);
  }
  dotIndex = form.emailAddress.value.indexOf('.', atIndex);
  if (dotIndex == -1) {
    alert("Please enter a valid email address");
    form.emailAddress.focus();
    return (false);
  }
  return (true);
}

function validateLivePaymentForm(form) {
  if (form.clientCCType.value == -1) {
    alert("Please enter your credit card type");
    form.clientCCType.focus();
    return (false);
  }
  if (form.clientCCName.value.length <= 4) {
    alert("Please enter the name on your card");
    form.clientCCName.focus();
    return (false);
  }
  if ((form.clientCCNumber.value.length < 13) || (form.clientCCNumber.value.length > 16)) {
    alert("Please enter your full credit card number");
    form.clientCCNumber.focus();
    return (false);
  }
  if (isNaN(form.clientCCNumber.value)) {
    alert("Please enter a valid credit card number, using only numbers");
    form.clientCCNumber.focus();
    return(false);
  }
  if (parseInt(form.clientCCNumber.value) < 0) {
    alert("Please enter a valid credit card number");
    form.clientCCNumber.focus();
    return(false);
  }
  if (form.clientCCExpiryMonth.value == -1) {
    alert("Please enter your credit card's expiry month");
    form.clientCCExpiryMonth.focus();
    return (false);
  }
  if (form.clientCCExpiryYear.value == -1) {
    alert("Please enter your credit card's expiry year");
    form.clientCCExpiryYear.focus();
    return (false);
  }
  today = new Date();
  if (parseInt("20" + form.clientCCExpiryYear.value) == today.getYear()) {
    if (parseInt(form.clientCCExpiryMonth.value) < today.getMonth()) {
      alert("Your card has expired.\n Please enter another credit card's details or correct the error.");
      form.clientCCExpiryMonth.focus();
      return (false);
    }
  }
  if (form.clientCCCVV.value.length != 3) {
    alert("Please enter your credit card's CVV number");
    form.clientCCCVV.focus();
    return (false);
  }
  return (true);
}

function validateEmailEnquiryBookingOfflineForm(form) {
  if (form.clientTelAreaCode.value.length == 0) {
    alert("Please enter your telephone code");
    form.clientTelAreaCode.focus();
    return (false);
  }
  if (form.clientTelNumber.value.length == 0) {
    alert("Please enter your telephone number");
    form.clientTelNumber.focus();
    return (false);
  }

  return (true);
}

function validateEmailEnquiryBookingForm(form) {
  if (form.clientTelAreaCode.value.length == 0) {
    alert("Please enter your telephone code");
    form.clientTelAreaCode.focus();
    return (false);
  }
  if (form.clientTelNumber.value.length == 0) {
    alert("Please enter your telephone number");
    form.clientTelNumber.focus();
    return (false);
  }

  if (form.clientCCName.value.length <= 4) {
    alert("Please enter the name on your card");
    form.clientCCName.focus();
    return (false);
  }
  if (form.clientCCType.value == -1) {
    alert("Please enter your credit card type");
    form.clientCCType.focus();
    return (false);
  }
  if ((form.clientCCNumber.value.length < 13) || (form.clientCCNumber.value.length > 16)) {
    alert("Please enter your full credit card number");
    form.clientCCNumber.focus();
    return (false);
  }
  if (isNaN(form.clientCCNumber.value)) {
    alert("Please enter a valid credit card number, using only numbers");
    form.clientCCNumber.focus();
    return(false);
  }
  if (parseInt(form.clientCCNumber.value) < 0) {
    alert("Please enter a valid credit card number");
    form.clientCCNumber.focus();
    return(false);
  }
  if (form.clientCCExpiryMonth.value == -1) {
    alert("Please enter your credit card's expiry month");
    form.clientCCExpiryMonth.focus();
    return (false);
  }
  if (form.clientCCExpiryYear.value == -1) {
    alert("Please enter your credit card's expiry year");
    form.clientCCExpiryYear.focus();
    return (false);
  }
  today = new Date();
  if (parseInt("20" + form.clientCCExpiryYear.value) == today.getYear()) {
    if (parseInt(form.clientCCExpiryMonth.value) < today.getMonth()) {
      alert("Your card has expired.\n Please enter another credit card's details or correct the error.");
      form.clientCCExpiryMonth.focus();
      return (false);
    }
  }
  if (form.clientCCCVV.value.length != 3) {
    alert("Please enter your credit card's CVV number");
    form.clientCCCVV.focus();
    return (false);
  }
  return (true);
}

function validateDetailsForm(form) {
  if (form.firstname.value.length == 0) {
    alert("Please enter your firstname");
    form.firstname.focus();
    return (false);
  }
  if (form.surname.value.length == 0) {
    alert("Please enter your surname");
    form.surname.focus();
    return (false);
  }
  if (form.address1.value.length == 0) {
    alert("Please enter your address");
    form.address1.focus();
    return (false);
  }
  if (form.city.value.length == 0) {
    alert("Please enter your city");
    form.city.focus();
    return (false);
  }
  if (form.postalCode.value.length == 0) {
    alert("Please enter your postal code");
    form.postalCode.focus();
    return (false);
  }
  if (form.country.value.length == 0) {
    alert("Please enter your country");
    form.country.focus();
    return (false);
  }
  if (form.telCode.value.length == 0) {
    alert("Please enter your telephone code");
    form.telCode.focus();
    return (false);
  }
  if (form.telNumber.value.length == 0) {
    alert("Please enter your telephone number");
    form.telNumber.focus();
    return (false);
  }

  if (form.emailAddress.value.length == 0) {
    alert("Please enter your email address");
    form.emailAddress.focus();
    return (false);
  }
  atIndex = form.emailAddress.value.indexOf('@');
  if (atIndex == -1) {
    alert("Please enter a valid email address");
    form.emailAddress.focus();
    return (false);
  }
  dotIndex = form.emailAddress.value.indexOf('.', atIndex);
  if (dotIndex == -1) {
    alert("Please enter a valid email address");
    form.emailAddress.focus();
    return (false);
  }

  if (form.ccType.value == -1) {
    alert("Please enter your credit card type");
    form.ccType.focus();
    return (false);
  }
  if (form.ccNumber.value.length < 16) {
    alert("Please enter your full credit card number");
    form.ccNumber.focus();
    return (false);
  }
  if (form.ccExpiryMonth.value == -1) {
    alert("Please enter your credit card's expiry month");
    form.ccExpiryMonth.focus();
    return (false);
  }
  if (form.ccExpiryYear.value == -1) {
    alert("Please enter your credit card's expiry year");
    form.ccExpiryYear.focus();
    return (false);
  }
  today = new Date();
  if (parseInt("20" + form.ccExpiryYear.value) == today.getYear()) {
    if (parseInt(form.ccExpiryMonth.value) < today.getMonth()) {
      alert("Your card has expired.\n Please enter another credit card's details or correct the error.");
      form.ccExpiryMonth.focus();
      return (false);
    }
  }
  return (true);
}

function validateRoomsRequiredForm(form) {
  if (!validateNumbers(form)) {
    return (false);
  }
  
  var rooms = new Array();
  var adults = new Array();
  var children = new Array();

  totalElements = form.elements.length;
  numRoomTypes = 0;
  lastRoomTypeID = 0;
  firstBracketIndex = 0;
  roomTypeID = 0;
  inputName = "";
  for (count=0; count<totalElements; count++) {
    if(form.elements[count].type == "text") {
      inputName = form.elements[count].name;
      firstBracketIndex = inputName.indexOf('[');
      roomTypeID = inputName.substring(firstBracketIndex+1, inputName.length -1);
      if (parseInt(roomTypeID) != lastRoomTypeID) {
        rooms[numRoomTypes] = 0;
        adults[numRoomTypes] = 0;
        children[numRoomTypes] = 0;
        numRoomTypes++;
        lastRoomTypeID = roomTypeID;
      }
      if (form.elements[count].value.length != 0) {
        if (inputName.indexOf("roomsRequiredForType") != -1) {
          rooms[numRoomTypes-1] = parseInt(form.elements[count].value);
        }
        else if (inputName.indexOf("numberOfAdultsPerRoomForType") != -1) {
          adults[numRoomTypes-1] = parseInt(form.elements[count].value);
        }
        else {
          children[numRoomTypes-1] = parseInt(form.elements[count].value);
        }
      }
    }
  }
  
  roomRequirementsEntered = false;
  for (i=0; i<numRoomTypes; i++) {
    if ( (rooms[i] == 0) && (adults[i] == 0) && (children[i] == 0) ) {
    }
    else if ( (rooms[i] != 0) && ((adults[i] != 0) || (children[i] != 0)) ) {
      roomRequirementsEntered = true;
    }
    else {
      if (rooms[i] == 0) {
        alert ("You have not entered the number of rooms required for type " + (i+1));
      }
      else {
        alert ("You have not entered the number of people for type " + (i+1));
      }
      return (false);
    }
  }
 
  if (!roomRequirementsEntered) {
    alert("You have not entered any room requirements");
    return (false);
  }

  return (true);
}

function validateNumbers(form) {
  totalElements = form.elements.length;
  for (count=0; count<totalElements; count++) {
    if(form.elements[count].type == "text") {
      if (form.elements[count].value.length > 0) {
        if (isNaN(form.elements[count].value)) {
          alert("Error - please only enter numbers");
          return(false);
        }
        if (parseInt(form.elements[count].value) < 0) {
          alert("Error - please only enter positive numbers");
          return(false);
        }
      }
    }
  }
  return(true);
}

function resetPage() {
  history.go(0);
}

function validateQuickSearchForm(form) {
  searchText = form.searchText.value;
  searchText = searchText.replace("\*", ""); 
  searchText = searchText.replace("?", ""); 
  searchText = searchText.replace("%", ""); 
  searchText = searchText.replace("_", ""); 
  if (searchText.length <= 2) {
    alert("Please enter a search string that is longer than 2 characters");
    form.searchText.focus();
    return (false);
  }
  return (true);
}

function validateSearchForm(form, checkForDateWithinAYear) {
  day = parseInt(form.arriveDay.value);
  monthYear = form.arriveMonthYear.value;
  month = parseInt(monthYear.substring(0, monthYear.indexOf('/')));
  year = parseInt(monthYear.substr(monthYear.indexOf('/')+1));
  numNights = parseInt(form.numNights.options[form.numNights.selectedIndex].value);

  if (day == 0) {
    alert("Please select the day of arrival");
    form.arriveDay.focus();
    return(false);
  }

  if (month == 0) {
    alert("Please select the month of arrival");
    form.arriveMonth.focus();
    return(false);
  }

  if (year == 0) {
    alert("Please select the year of arrival");
    form.arriveYear.focus();
    return(false);
  }

  if (numNights == 0) {
    alert("Please select the number of nights you wish to stay");
    form.numNights.focus();
    return(false);
  }

  if (!isValidDate(day, month, year)) {
    alert("Please enter a valid date of arrival");
    form.arriveDay.focus();
    return(false);
  }
  
  if (checkForDateWithinAYear) {
    arrivalDate = new Date(year, month-1, day, 0, 0, 0, 0);
 
    if (isBeforeToday(arrivalDate)) {
      alert("The date of arrival must be today or after");
      form.arriveDay.focus();
      return(false);
    }
  
    if (!isWithinYear(arrivalDate)) {
      alert("The date of arrival must be within a year from today");
      form.arriveDay.focus();
      return(false);
    }
  }
  
  return(true);
}

function isBeforeToday(checkDate) {
  today = new Date();
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);
  today.setMilliseconds(0);
  if (checkDate.getTime() < today.getTime())
    return true;
}

function isWithinYear(checkDate) {
  today = new Date();
  if (checkDate.getYear() == today.getYear())
    return true;
  if (checkDate.getMonth() > today.getMonth())
    return false;
  if ((checkDate.getMonth() == today.getMonth()) && (checkDate.getDate() >= today.getDate()))
    return false;
  return true;
}

function isValidDate(day, month, year) {
  if((month == 1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)) {
      if ( day > 31 || day < 1 ) return false;
  } else
  if((month == 4)||(month==6)||(month==9)||(month==11)) {
      if ( day > 30 || day < 1 ) return false;
  } else
  if((year - 1960) % 4 != 0) {
      if ( day > 28 || day < 1 ) return false;
  } else
      if ( day > 29 || day < 1 ) return false;

  return true;
}

function removeSpaces(e) {
  if (e.keyCode)
  {
    code = e.keyCode;
  }
  else 
  {
    if (e.which)
    { 
      code = e.which;  
    }
  }
  if (code == 32)
  {
    return false;
  }
  return true;
}

function validateCCNumber(ccNumber) {
  alert("Checking: " + ccNumber);
  for (a=ccNumber.length-1; a>=0; a--) {
    if (ccNumber.charAt(a) == ' ') {
      if (ccNumber.length == 1) {
        ccNumber = "";
      } else if (a == 0) {
        ccNumber = ccNumber.substring(1, ccNumber.length);
      } else if (a == (ccNumber.length-1)) {
        ccNumber = ccNumber.substring(0, a-1);
      } else {
        ccNumber = ccNumber.substring(0, a) + ccNumber.substring(a+1, ccNumber.length);
      }
    }
  }
  alert("Removed spaces: " + ccNumber);

  if (isNaN(ccNumber)) {
    alert("Please enter a valid credit card number");
    return(false);
  }
  if (parseInt(ccNumber) < 0) {
    alert("Please enter a credit card number");
    return(false);
  }
  
  if (ccNumber.length < 13 || ccNumber.length > 16) {
    alert("Please enter a full, valid credit card number");
    return (false);
  }
  
  if (ccNumber.substring(0, 2) == "51" ||  ccNumber.substring(0, 2) == "52" || 
    ccNumber.substring(0, 2) == "53" || ccNumber.substring(0, 2) == "54" ||
    ccNumber.substring(0, 2) == "55") {
    if (ccNumber.length != 16) {
      alert("Please enter a valid MasterCard credit card number");
      return (false);
    }
  }
  if (ccNumber.substring(0, 1) == "4") {
    if ((ccNumber.length != 13) && (ccNumber.length != 16)) {
      alert("Please enter a valid Visa credit card number");
      return (false);
    }
  }
  if (ccNumber.substring(0, 2) == "34" ||  ccNumber.substring(0, 2) == "37") {
    if (ccNumber.length != 15) {
      alert("Please enter a valid Amex credit card number");
      return (false);
    }
  }
  if (ccNumber.substring(0, 3) == "300" ||  ccNumber.substring(0, 3) == "301" ||
    ccNumber.substring(0, 3) == "302" ||  ccNumber.substring(0, 3) == "303" || 
    ccNumber.substring(0, 3) == "304" ||  ccNumber.substring(0, 3) == "305" ||
    ccNumber.substring(0, 2) == "36" || ccNumber.substring(0, 2) == "38") {
    if (ccNumber.length != 14) {
      alert("Please enter a valid Diners Club credit card number");
      return (false);
    }
  }
  
  sum = 0;
  num = 0;
  doubling = false;
  for (i=(ccNumber.length-1); i>=0; i--) {
    if (doubling) {
      num = parseInt(ccNumber.charAt(i))*2;
      if (num > 9) {
        alert("Adding: 1 and: " + (num%10) + " to: " + sum);
        sum = sum + 1 + (num%10);
      } else {
        alert("Adding: " + num + " to: " + sum);
        sum = sum + num;
      }
      doubling = false;
    } else {
      num = parseInt(ccNumber.charAt(i));
      alert("Adding: " + num + " to: " + sum);
      sum = sum + num;
      doubling = true;
    }
    alert("Sum is: " + sum);
  }
  if (sum % 10 == 0) {
    alert("Validated");
    return (true);
  } else {
    alert("Not validated");
    return (false);
  }
}
