﻿
var reEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
var reTel = /^[\d\s]{10,25}|[+][\d\s]{10,25}$/;
var reNumber = /[0-9]/;
var reYear = /^[0-9]{2,4}$/;

// ------------------------------------- REGEX DATE ------------------------------------------------------//
function CheckDate(datum) {
  //(Schritt 1) Fehlerbehandlung
  if (!datum) return false;
  datum = datum.toString();

  //(Schritt 2) Aufspaltung des Datums
  datum = datum.split(".");
  if (datum.length != 3) return false;

  if (!datum[0].match(reNumber)) return false;
  if (!datum[1].match(reNumber)) return false;
  if (!datum[2].match(reNumber)) return false;

  //(Schritt 3) Entfernung der fuehrenden Nullen und Anpassung des Monats
  datum[0] = parseInt(datum[0], 10);
  datum[1] = parseInt(datum[1], 10) - 1;

  //(Schritt 4) Behandlung Jahr nur zweistellig
  if (datum[2].length == 2) datum[2] = "20" + datum[2];

  // wenn länger als 4
  if (datum[2].length > 4) return false;

  //(Schritt 5) Erzeugung eines neuen Dateobjektes
  var kontrolldatum = new Date(datum[2], datum[1], datum[0]);

  //(Schritt 6) Vergleich, ob das eingegebene Datum gleich dem JS-Datum ist
  if (kontrolldatum.getDate() == datum[0] && kontrolldatum.getMonth() == datum[1] && kontrolldatum.getFullYear() == datum[2])
    return true; else return false;
}
// ------------------------- UPDATE CAPTCHA IMAGE  ------------------------------------- //
function updateImage(img, text) {
  var image = document.getElementById(img);
  image.src = text + '?Random=' + Math.random(1);
}

// ------------------------- LOADER OFF ---------------------------------------------------------- //
function loaderOff()
{   
    Effect.Fade('loader', {duration: 0.2, afterFinish: function()
    {    
        Effect.Appear('layer', {duration: 0.6}); 
    }
    });     
}

// ------------------------- Navigation - Load Pagecontent (with LOADER) ------------------------- //
function loadSite(site)
{
    Effect.Fade('layer', {duration: 0.6, afterFinish: function()
    {
        Effect.Appear('loader', {duration: 0.2, afterFinish: function() 
        { 
            ob_post.UpdatePanelFromPage('panelContent', 'siteContent', site);
        }
        });
    }        
    });
  }

  // -------------------------------- SELECT AGENDA ENTRIES ------------------------------ //
  function JSSelectAgendaEntries() {
    if (document.getElementById("cboMonths").value != "select") {
      loadSite("02.aspx?Date=" + document.getElementById("cboMonths").value);
    }
  }
// -------------------------------- CHECK / SEND FORM NEWSLETTER ------------------------------ //
function CheckFormNewsletter() {
  var bEmail = false;
  var bTel = false;

  if (document.getElementById("tbxName").value == '' || document.getElementById("tbxVorname").value == '' || document.getElementById("tbxEmail").value == '') {
    Effect.Appear('divError', { duration: 0.0 });
    Effect.Fade('divEmail', { duration: 0.0 });
  }
  else {
    Effect.Fade('divError', { duration: 0.0 });

    // check Email
    var sEmail = new String(document.getElementById("tbxEmail").value);
    if (!sEmail.match(reEmail)) {
      Effect.Appear('divEmail', { duration: 0.0 });
      bEmail = false;
    }
    else {
      bEmail = true;
      Effect.Fade('divEmail', { duration: 0.0 });
    }

    // check Tel
    var sTel = new String(document.getElementById("tbxMobile").value);
    if (sTel != "" && sTel != "Mobile") {
      if (!sTel.match(reTel)) {
        Effect.Appear('divTel', { duration: 0.0 });
        bTel = false;
      }
      else {
        bTel = true;
        Effect.Fade('divTel', { duration: 0.0 });
      }
    }
    else {
      bTel = true;
      Effect.Fade('divTel', { duration: 0.0 });
    }

    if (bEmail && bTel) {
      Effect.Appear('divSent', { duration: 0.0 });
      document.getElementById("btnSend").disabled = true;

      SendFormNewsletter();
    }
  }
}


function SendFormNewsletter() {
  ob_post.AddParam("sLastName", document.getElementById("tbxName").value);
  ob_post.AddParam("sFirstName", document.getElementById("tbxVorname").value);
  ob_post.AddParam("sEmail", document.getElementById("tbxEmail").value);
  if (document.getElementById("tbxMobile").value == "Mobile")
  { ob_post.AddParam("sTel", ""); }
  else { ob_post.AddParam("sTel", document.getElementById("tbxMobile").value); }

  ob_post.post("01.aspx", "SaveForm");

}

// -------------------------------- CHECK / SEND FORM CONTACT ------------------------------ //
function CheckFormContact() {
  var bEmail = false;
  var bCaptcha = false;
  var bDate = false;

  if (document.getElementById("tbxNameContact").value != '' && document.getElementById("tbxEmailContact").value != '') {
    Effect.Fade('divErrorContact', { duration: 0.0 });

    // check normal Form
    var sEmail = new String(document.getElementById("tbxEmailContact").value);
    if (!sEmail.match(reEmail)) {
      bEmail = false;
      Effect.Appear('divEmailContact', { duration: 0.0 });
    }
    else {
      bEmail = true;
      Effect.Fade('divEmailContact', { duration: 0.0 });
    }

    // check date
    if (document.getElementById("tbxDateContact").value != "") {
      if (!CheckDate(document.getElementById("tbxDateContact").value)) {
        bDate = false;
        Effect.Appear('divDateContact', { duration: 0.0 });
      }
      else {
        bDate = true;
        Effect.Fade('divDateContact', { duration: 0.0 });
      }
    }
    else {
      bDate = true;
    }
    
    if (bEmail && bDate) {

      // check captcha
      if (ob_post.post("captcha.aspx", "CheckCaptcha", null, { "sCaptchaText": document.getElementById("captchaText").value })) {
        bCaptcha = true;
        Effect.Fade('divCaptchaError', { duration: 0.0 });
      }
      else {
        bCaptcha = false;
        Effect.Appear('divCaptchaError', { duration: 0.0 });

        updateImage('captchaImgPlaceholder', '../cms/PageModules/Captcha/Captcha.aspx');
        return;
      }

      Effect.Fade('divDateContact', { duration: 0.0 });
      Effect.Fade('divEmailContact', { duration: 0.0 });

      Effect.Appear('divSentContact', { duration: 0.0 });
      document.getElementById("btnSendContact").disabled = true;

      SendContactEmail();
    }
  }
  else {
    Effect.Appear('divErrorContact', { duration: 0.0 });
  }
}

function SendContactEmail() {
  ob_post.AddParam("sDate", document.getElementById("tbxDateContact").value);
  ob_post.AddParam("sLocation", document.getElementById("tbxLocationContact").value);
  ob_post.AddParam("sPartyname", document.getElementById("tbxPartynameContact").value);

  ob_post.AddParam("sFirma", document.getElementById("tbxFirmaContact").value);
  ob_post.AddParam("sName", document.getElementById("tbxNameContact").value);
  ob_post.AddParam("sVorname", document.getElementById("tbxVornameContact").value);
  ob_post.AddParam("sEmail", document.getElementById("tbxEmailContact").value);
  ob_post.AddParam("sComment", document.getElementById("tbxCommentContact").value);
  
  ob_post.AddParam("bEntryNewsletter", document.getElementById("chkBoxNewsletterContact").checked);

  document.getElementById("tbxDateContact").value = '';
  document.getElementById("tbxLocationContact").value = '';
  document.getElementById("tbxPartynameContact").value = '';
  document.getElementById("tbxNameContact").value = '';
  document.getElementById("tbxVornameContact").value = '';
  document.getElementById("tbxEmailContact").value = '';
  document.getElementById("tbxCommentContact").value = '';
  
  ob_post.post("06.aspx", "SendEmail");
}

// --------------------------- Change Reservation  -------------------------------- //
function SelectReservationInfo() {
  var ID = document.getElementById("cboReservations").value;
  loadSite('03.aspx?AgendaID=' + ID);
}
// --------------------------- Change Guestlistinfo  -------------------------------- //
function SelectGuestlistInfo() {
  var ID = document.getElementById("cboGuestlists").value;
  loadSite('07.aspx?AgendaID=' + ID);
}

// -------------------------------- CHECK / SEND FORM RESERVATION ------------------------------ //
function CheckFormReservation(ID) {
  var bEmail = false;
  var bTel = false;
  var bPersons = false;
  
  if (document.getElementById("tbxNameReservation").value == '' || document.getElementById("tbxVornameReservation").value == '' || document.getElementById("tbxEmailReservation").value == '') {   
    Effect.Appear('divErrorReservation', { duration: 0.0 });
    Effect.Fade('divEmailReservation', { duration: 0.0 });
  }
  else {    
    Effect.Fade('divErrorReservation', { duration: 0.0 });

    // check Email
    var sEmail = new String(document.getElementById("tbxEmailReservation").value);
    if (!sEmail.match(reEmail)) {
      Effect.Appear('divEmailReservation', { duration: 0.0 });
      bEmail = false;
    }
    else {
      bEmail = true;
      Effect.Fade('divEmailReservation', { duration: 0.0 });
    }

    // check Tel
    var sTel = new String(document.getElementById("tbxMobileReservation").value);
    if (sTel != "") {
      if (!sTel.match(reTel)) {
        Effect.Appear('divTelReservation', { duration: 0.0 });
        bTel = false;
      }
      else {
        bTel = true;
        Effect.Fade('divTelReservation', { duration: 0.0 });
      }
    }
    else {
      bTel = true;
      Effect.Fade('divTelReservation', { duration: 0.0 });
    }

    // check Persons
    var sPersons = new String(document.getElementById("tbxPersonsReservation").value);
    if (sPersons != "") {
      if (!sPersons.match(reNumber)) {
        Effect.Appear('divPersonsReservation', { duration: 0.0 });
        bPersons = false;
      }
      else {
        bPersons = true;
        Effect.Fade('divPersonsReservation', { duration: 0.0 });
      }
    }
    else {
      bPersons = true;
      Effect.Fade('divPersonsReservation', { duration: 0.0 });
    }

    if (bEmail && bTel && bPersons) {

      // check captcha
      if (ob_post.post("captcha.aspx", "CheckCaptcha", null, { "sCaptchaText": document.getElementById("captchaText").value })) {
        bCaptchaFAQ = true;
        Effect.Fade('divCaptchaError', { duration: 0.0 });
      }
      else {
        bCaptchaFAQ = false;
        Effect.Appear('divCaptchaError', { duration: 0.0 });

        updateImage('captchaImgPlaceholder', '../cms/PageModules/Captcha/Captcha.aspx');
        return;
      }
    
      Effect.Appear('divSentReservation', { duration: 0.0 });
      document.getElementById("btnSendReservation").disabled = true;

      SendFormReservation(ID);
    }
  }
}

function SendFormReservation(ID) {
  ob_post.AddParam("sLounge", document.getElementById("cboLounges").value);
  ob_post.AddParam("sName", document.getElementById("tbxNameReservation").value);
  ob_post.AddParam("sVorname", document.getElementById("tbxVornameReservation").value);
  ob_post.AddParam("sEmail", document.getElementById("tbxEmailReservation").value);
  ob_post.AddParam("sPersons", document.getElementById("tbxPersonsReservation").value);
  ob_post.AddParam("sComment", document.getElementById("tbxCommentReservation").value);
  ob_post.AddParam("sTel", document.getElementById("tbxMobileReservation").value);
  ob_post.AddParam("bNewsletter", document.getElementById("chkBoxNewsletterReservation").checked);

  document.getElementById("tbxNameReservation").value = '';
  document.getElementById("tbxVornameReservation").value = '';
  document.getElementById("tbxEmailReservation").value = '';
  document.getElementById("tbxPersonsReservation").value = '';
  document.getElementById("tbxCommentReservation").value = '';
  document.getElementById("tbxMobileReservation").value = '';
  
  ob_post.post("03.aspx?AgendaID=" + ID, "SaveForm");

}

// ------------------------- SendForm GUESTLIST ------------------------------------- //
function CheckGuestlistEntry(ID) {
  var bEmailGL = false;
  var bAgeGL = false;
  var bTelGL = false;

  if (document.getElementById("tbxNameGL").value == '' || document.getElementById("tbxEmailGL").value == '' || document.getElementById("tbxVornameGL").value == '') {
    Effect.Appear('divErrorGL', { duration: 0.0 });
  }
  else {
    var sEmail = new String(document.getElementById("tbxEmailGL").value);
    if (!sEmail.match(reEmail)) {
      bEmailGL = false;

      Effect.Fade('divErrorGL', { duration: 0.0 });
      Effect.Appear('divEmailGL', { duration: 0.0 });
    }
    else {
      bEmailGL = true;
      Effect.Fade('divEmailGL', { duration: 0.0 });
    }

    var sAge = document.getElementById("tbxAgeGL").value;
    if (sAge != "" && !sAge.match(reYear)) {
      bAgeGL = false;
      Effect.Appear('divAgeGL', { duration: 0.0 });
    }
    else {
      bAgeGL = true;
      Effect.Fade('divAgeGL', { duration: 0.0 });
    }

    if (document.getElementById("tbxMobileGL").value != "") {
      bTel = false;

      var sTel = document.getElementById("tbxMobileGL").value;
      if (!sTel.match(reTel)) {
        bTelGL = false;
        Effect.Appear('divTelGL', { duration: 0.0 });
      }
      else {
        bTelGL = true;
        Effect.Fade('divTelGL', { duration: 0.0 });
      }
    }
    else {
      bTelGL = true;
      Effect.Fade('divTelGL', { duration: 0.0 });
    }

    if (bAgeGL && bTelGL && bEmailGL) {

      // check captcha
      if (ob_post.post("captcha.aspx", "CheckCaptcha", null, { "sCaptchaText": document.getElementById("captchaText").value })) {
        bCaptchaFAQ = true;
        Effect.Fade('divCaptchaError', { duration: 0.0 });
      }
      else {
        bCaptchaFAQ = false;
        Effect.Appear('divCaptchaError', { duration: 0.0 });

        updateImage('captchaImgPlaceholder', '../cms/PageModules/Captcha/Captcha.aspx');
        return;
      }
      
      Effect.Fade('divErrorGL', { duration: 0.0 });
      Effect.Appear('divSentGL', { duration: 0.0 });

      document.getElementById("sendButtonGL").disabled = true;
      SaveGuestlistEntry(ID);

    }
  }
}

function SaveGuestlistEntry(ID) {
  ob_post.AddParam("bInsertNewsletter", document.getElementById("chkBoxNewsletterGL").checked);
  ob_post.AddParam("sName", document.getElementById("tbxNameGL").value);
  ob_post.AddParam("sVorname", document.getElementById("tbxVornameGL").value);
  if (document.getElementById("cboCompanionsGL") != null)
  { ob_post.AddParam("sCompanions", document.getElementById("cboCompanionsGL").value); }
  else { ob_post.AddParam("sCompanions", ""); }
  ob_post.AddParam("sGender", document.getElementById("cboGenderGL").value);
  ob_post.AddParam("sAge", document.getElementById("tbxAgeGL").value);
  ob_post.AddParam("sMobile", document.getElementById("tbxMobileGL").value);
  ob_post.AddParam("sEmail", document.getElementById("tbxEmailGL").value);

  document.getElementById("tbxNameGL").value = '';
  document.getElementById("tbxVornameGL").value = '';
  document.getElementById("tbxAgeGL").value = '';
  document.getElementById("tbxMobileGL").value = '';
  document.getElementById("tbxEmailGL").value = '';

  ob_post.post("07.aspx?AgendaID=" + ID, "SaveForm");
}