//*************************************************************************************
// functions page                                                                     *
// Section 1 : Backend                                                                *
//           :a) Restaurant Validation - form validation for add/modifying restaurants*
//           :b) Section Validation - form validation for menu sections               *
//           :c) Content Validation - form validation for menu content                *
//           :d) Offer Validation - form validation for sending email offer           *
//------------------------------------------------------------------------------------*
// Section 2 : Frontend                                                               *
//           :a) Numeric check - checks whether string is numeric                     *
//           :b) Delitogo Validation - form validation for delitogo submission        *
//           :c) Offer Registration Validation - form validation for offers           *
//           :d) Offer unsubscribe - form validation for unsubscribing from offers    *
//           :e) Offer Update Validation - form validation offer update               *
//           :f) Contacts Validation - form validation for contacts                   *
//           :g) Reservation Validation 1 - form validation for reservation pg1       *
//*************************************************************************************
//************************************************************
// Section 1 : Backend                                       *
//************************************************************
//******************************************
// Section 1 :a) Restaurant Validation     *
//******************************************
function AddModifyRestaurant()
{
  //restaurant name
  if (document.theform.name.value == "")
   {
    alert("Please add a Restaurant Name");
    document.theform.name.focus();
    return false;
   }
  //street name
  else if (document.theform.strnum.value == "")
   {
    alert("Please add a Street Number");
    document.theform.strnum.focus();
    return false;
   }
  //street address
  else if (document.theform.straddress.value == "")
   {
    alert("Please add a Street Address");
    document.theform.straddress.focus();
    return false;
   }
  //telephone
  else if (document.theform.tel.value == "")
   {
    alert("Please add a Telephone number");
    document.theform.tel.focus();
    return false;
   }
  //postcode
  else if (document.theform.postcode.value == "")
   {
    alert("Please add a PostCode");
    document.theform.postcode.focus();
    return false;
   }
  //opening times
  else if (document.theform.openmon.value == "")
   {
    alert("Please add Monday opening times");
    document.theform.openmon.focus();
    return false;
   }
  else if (document.theform.opentue.value == "")
   {
    alert("Please add Tuesdays opening times");
    document.theform.opentue.focus();
    return false;
   }
  else if (document.theform.openwed.value == "")
   {
    alert("Please add Wednesday opening times");
    document.theform.openwed.focus();
    return false;
   }
  else if (document.theform.openthu.value == "")
   {
    alert("Please add Thursday opening times");
    document.theform.openthu.focus();
    return false;
   }
  else if (document.theform.openfri.value == "")
   {
    alert("Please add Friday opening times");
    document.theform.openfri.focus();
    return false;
   }
  else if (document.theform.opensat.value == "")
   {
    alert("Please add Saturday opening times");
    document.theform.opensat.focus();
    return false;
   }
  else if (document.theform.opensun.value == "")
   {
    alert("Please add Sunday opening times");
    document.theform.opensun.focus();
    return false;
   }
  // all is well
  else
   {
    return true;
   }
}
//******************************************
// Section 1 :b) Section Validation        *
//******************************************
function AddModifySection()
{
 // validate menutype
  if (document.theform.menutype.value == "")
   {
    alert("Please write the Menu Section you want to add");
    document.theform.menutype.focus();
    return false;
   }
  // all is well
  else
   {
    return true;
   }
}
//******************************************
// Section 1 :c) Content Validation        *
//******************************************
function AddModifyContent()
{
  // validate menutype
  if (document.theform.menutype.value == "" || document.theform.menutype.value == "Please Choose One")
   {
    alert("Please choose the Menu Section you want to add to");
    document.theform.menutype.focus();
    return false;
   }
  // validate restaurant
  else if (document.theform.restaurantid.value == "" || document.theform.restaurantid.value == "Please Choose One")
   {
    alert("Please choose the restaurant this menu is for");
    document.theform.restaurantid.focus();
    return false;
   }
  // all is well
  else
   {
    return true;
   }
}
//******************************************
// Section 1 :d) Offer Validation          *
//******************************************
function SendOffer()
{
  // validate sendto
  if (document.theform.sendto.value == "")
   {
    alert("Please choose who you would like to send to");
    document.theform.sendto.focus();
    return false;
   }
  // validate restaurant
  else if (document.theform.sendto.value == "birth" && document.theform.birthmonth.value == "")
   {
    alert("Please choose a birth month for the birthday email");
    document.theform.birthmonth.focus();
    return false;
   }
 else if (document.theform.offer.value == "")
   {
    alert("Please choose a offer to send");
    document.theform.offer.focus();
    return false;
   }
  // all is well
  else
   {
    return true;
   }
}
//------------------------------------------------------------------------------------------------------------------
//************************************************************
// Section 2 : Frontend                                      *
//************************************************************
//******************************************
// Section 2 :a) Numeric Check             *
//******************************************
function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;
  for (i = 0; i < sText.length && IsNumber == true; i++)
   {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
     {
       IsNumber = false;
     }
   }
  return IsNumber;
}
//******************************************
// Section 2 :b) Delitogo Validation       *
//******************************************
function DeliCheck2()
{
 //validate username
 if(document.delitogo.uname.value == "")
  {
   alert("Please enter your name !");
   document.delitogo.uname.focus();
   return false;
  }
 // validate email
  else if (document.delitogo.uemail.value == "")
   {
    alert("Please enter an email address!");
    document.delitogo.uemail.focus();
    return false;
   }
  else if (! document.delitogo.uemail.value.match("^.+@.+\..+$"))
   {
    alert("Please enter a valid email address!");
    document.delitogo.uemail.focus();
    return false;
   }
  // validate telephone
  else if (document.delitogo.utel.value == "" || !IsNumeric(document.delitogo.utel.value))
   {
    alert("Please enter a valid Telehone number !");
    document.delitogo.utel.focus();
    return false;
   }
 //all is well
 else
  {
   return true;
  }
}
//******************************************
// Section 2 :c) Offer validation          *
//******************************************
function OffersCheck()
{
 //validate username
 if(document.theform.firstname.value == "")
  {
   alert("Please enter your First name !");
   document.theform.firstname.focus();
   return false;
  }
  else if (document.theform.surname.value == "")
  {
   alert("Please enter your Surname !");
   document.theform.surname.focus();
   return false;
  }
 // validate email
  else if (document.theform.email.value == "")
   {
    alert("Please enter an email address!");
    document.theform.email.focus();
    return false;
   }
  else if (! document.theform.email.value.match("^.+@.+\..+$"))
   {
    alert("Please enter a valid email address!");
    document.theform.email.focus();
    return false;
   }
  // validate password
  else if (document.theform.password.value == "")
   {
    alert("Please enter a password!");
    document.theform.password.focus();
    return false;
   }
  // verify password
  else if (document.theform.password.value != document.theform.password1.value)
   {
    alert("Your Passwords do not match!");
    document.theform.password1.focus();
    return false;
   }
  //validate tel numerals if filled
  else if (document.theform.tel.value > "" && !IsNumeric(document.theform.tel.value))
   {
    alert("Please enter a valid Telehone number !");
    document.theform.tel.focus();
    return false;
   }
  //validate mobile numerals if filled
  else if (document.theform.mobile.value > "" && !IsNumeric(document.theform.mobile.value))
   {
    alert("Please enter a valid Mobile number !");
    document.theform.mobile.focus();
    return false;
   }
  //validate fax numerals if filled
  else if (document.theform.fax.value > "" && !IsNumeric(document.theform.fax.value))
   {
    alert("Please enter a valid Fax number !");
    document.theform.fax.focus();
    return false;
   }

 //all is well
 else
  {
   return true;
  }
}
//******************************************
// Section 2 :d) Offer unsubscribe         *
//******************************************
function OffersDel()
{
 if (!document.theform1.unsubscribe.checked)
  {
   alert("Please agree to Un-subscribe by ticking the box !");
   document.theform1.unsubscribe.focus();
   return false;
  }
 //all is well
 else
  {
   return true;
  }
}
//******************************************
// Section 2 :e) Offer update validation   *
//******************************************
function OffersUpdate()
{
 //validate username
 if(document.theform2.firstname.value == "")
  {
   alert("Please enter your First name !");
   document.theform2.firstname.focus();
   return false;
  }
  else if (document.theform2.surname.value == "")
  {
   alert("Please enter your Surname !");
   document.theform2.surname.focus();
   return false;
  }
 // validate email
  else if (document.theform2.email.value == "")
   {
    alert("Please enter an email address!");
    document.theform2.email.focus();
    return false;
   }
  else if (! document.theform2.email.value.match("^.+@.+\..+$"))
   {
    alert("Please enter a valid email address!");
    document.theform2.email.focus();
    return false;
   }
  // validate password
  else if (document.theform2.password.value > "")
   {
    if (document.theform2.password.value != document.theform2.password1.value)
     {
      alert("Your Passwords do not match!");
      document.theform2.password1.focus();
      return false;
     }
   }
  //validate tel numerals if filled
  else if (document.theform2.tel.value > "" && !IsNumeric(document.theform2.tel.value))
   {
    alert("Please enter a valid Telehone number !");
    document.theform2.tel.focus();
    return false;
   }
  //validate mobile numerals if filled
  else if (document.theform2.mobile.value > "" && !IsNumeric(document.theform2.mobile.value))
   {
    alert("Please enter a valid Mobile number !");
    document.theform2.mobile.focus();
    return false;
   }
  //validate fax numerals if filled
  else if (document.theform2.fax.value > "" && !IsNumeric(document.theform2.fax.value))
   {
    alert("Please enter a valid Fax number !");
    document.theform2.fax.focus();
    return false;
   }
 //all is well
 else
  {
   return true;
  }
}
//******************************************
// Section 2 :f) Contacts update validation*
//******************************************
function ContactsValidate()
{
 //validate name
 if(document.contactus.name.value == "")
  {
   alert("Please enter your name !");
   document.contactus.name.focus();
   return false;
  }
 // validate email
  else if (document.contactus.email.value == "")
   {
    alert("Please enter an email address!");
    document.contactus.email.focus();
    return false;
   }
  else if (! document.contactus.email.value.match("^.+@.+\..+$"))
   {
    alert("Please enter a valid email address!");
    document.contactus.email.focus();
    return false;
   }
  // validate enquiry
  else if (document.contactus.enquiry.value == "")
   {
    alert("Please enter an Enquiry!");
    document.contactus.enquiry.focus();
    return false;
   }
 //all is well
 else
  {
   return true;
  }

}
//******************************************
// Section 2 :g) Reservation 1 validation  *
//******************************************
function ReservationCheck1()
{
 var d = new Date();
 var td = d.getDate();
 var tm = (d.getMonth() + 1);
 
 //validate restarant
 if(document.onlinereservations.restaurantid.value == "")
  {
   alert("Please choose a Restaurant !");
   document.onlinereservations.restaurantid.focus();
   return false;
  }
  // validate reservation date
  else if (document.onlinereservations.day.value == "" || document.onlinereservations.month.value == "")
   {
    alert("Please choose a Reservation Date!");
    document.onlinereservations.day.focus();
    return false;
   }
   // validate if date is past
  else if (document.onlinereservations.month.value < tm || (document.onlinereservations.month.value == tm && document.onlinereservations.day.value < td))
   {
    alert("Your Reservation Date is in the past!");
    document.onlinereservations.day.focus();
    return false;
   }
   // validate if date is today
  else if (document.onlinereservations.month.value == tm && document.onlinereservations.day.value == td)
   {
    alert("We do not except Online Bookings for the current day, please call our restaurant directly for a booking for today!");
    document.onlinereservations.day.focus();
    return false;
   }

 //all is well
 else
  {
   return true;
  }

}
//******************************************
// Section 2 :h) Reservation 2 validation  *
//******************************************
function ReservationCheck2()
{

 //validate table
 var bookedcheck = -1;

 if(document.onlinereservations.booked.checked == true)
  {
   bookedcheck = 1;
  }
 else
  {
   for (counter = 0; counter < document.onlinereservations.booked.length; counter++)
    {
     if (document.onlinereservations.booked[counter].checked)
      {
       bookedcheck = counter;
      }
    }
  }
 if(bookedcheck == -1)
  {
   alert("Please choose a Table !");
   return false;
  }
 //validate username
 else if(document.onlinereservations.uname.value == "")
  {
   alert("Please enter your name !");
   document.onlinereservations.uname.focus();
   return false;
  }
 // validate email
  else if (document.onlinereservations.uemail.value == "")
   {
    alert("Please enter an email address!");
    document.onlinereservations.uemail.focus();
    return false;
   }
  else if (! document.onlinereservations.uemail.value.match("^.+@.+\..+$"))
   {
    alert("Please enter a valid email address!");
    document.onlinereservations.uemail.focus();
    return false;
   }
  // validate telephone
  else if (document.onlinereservations.utel.value == "" || !IsNumeric(document.onlinereservations.utel.value))
   {
    alert("Please enter a valid Telehone number !");
    document.onlinereservations.utel.focus();
    return false;
   }
 //validate rules
 if(!document.onlinereservations.agree.checked)
  {
   alert("If you wish to carry on with this booking you need to read and agree to the table rules !");
   document.onlinereservations.agree.focus();
   return false;
  }

 //all is well
 else
  {
   return true;
  }

}


