function goTo(address, timeout)
{
  if(timeout > 0)
  {
    window.setTimeout(function(){document.location.href = address;}, timeout);
  }
  else
  {
    document.location.href = address;
  }
}

function confirmQuestion(question, address)
{
  if(confirm(question))
  {
    goTo(address);
  }
}

function popUp(url, p)
{
  popup = window.open(url, 'popup', 'width='+p.width+',height='+p.height+',status='+p.status+',location='+p.location+',scrollbars='+p.scrollbars+',resizable='+p.resizable+',menubar='+p.menubar+',toolbar='+p.toolbar+',top='+p.top+',left='+p.left);
  popup.focus();
}


/* sitzplan */
var placesTemp = new Array();

function insertPlacesTemp(etable_id)
{
  var placeInArray = $.inArray(etable_id, placesTemp);

  if(placeInArray >= 0)
  {
    placesTemp.splice(placeInArray, 1);
    $('td[name=place_'+etable_id+']').removeClass('marked').addClass('free');
  }
  else
  {
    placesTemp.push(etable_id);
    $('td[name=place_'+etable_id+']').removeClass('free').addClass('marked');
  }
}

function insertBlockTemp(action_mode, eblock_id)
{
  if(action_mode == '-')
  {
    var blockInArray = $.inArray(eblock_id, placesTemp);

    if(blockInArray >= 0)
    {
      placesTemp.splice(blockInArray, 1);
      $('#'+eblock_id).text(parseFloat($('#'+eblock_id).text()) + 1);
      $('#'+eblock_id+'_my').text((parseFloat($('#'+eblock_id+'_all').text()) - parseFloat($('#'+eblock_id).text()))+' '+((parseFloat($('#'+eblock_id+'_all').text()) - parseFloat($('#'+eblock_id).text())) == 1 ? 'Platz' : 'Plätze')+' reserviert');

      var blockInArrayAfterSplice = $.inArray(eblock_id, placesTemp);
      if(blockInArrayAfterSplice < 0)
      {
        $('#'+eblock_id+'_my').fadeOut('slow');
      }
    }
    else
    {
      alert('Sie haben in diesem Block noch keine Plätze reserviert!');
    }
  }
  else
  {
    if(parseFloat($('#'+eblock_id).text()) > 0)
    {
      placesTemp.push(eblock_id);
      $('#'+eblock_id).text(parseFloat($('#'+eblock_id).text()) - 1);
      $('#'+eblock_id+'_my').text((parseFloat($('#'+eblock_id+'_all').text()) - parseFloat($('#'+eblock_id).text()))+' '+((parseFloat($('#'+eblock_id+'_all').text()) - parseFloat($('#'+eblock_id).text())) == 1 ? 'Platz' : 'Plätze')+' reserviert').fadeIn('slow');
    }
    else
    {
      alert('In diesem Block sind leider keine freien Plätze mehr verfügbar!');
    }
  }
}

function ajax_insertPlaces()
{
  $.ajax({
    url: 'ajax/request-insertPlaces.php',
    data: {place_temp: placesTemp.join(',')},
    type: 'POST',
    async: false,
    cache: false,
    success: function(val) {
      if(val == "true") {
        alert('Die Plätze wurden erfolgreich für 30min reserviert! Bitte schließen Sie die Bestellung innerhalb dieser Zeit ab, da die Plätze sonst wieder freigegeben werden!');
        goTo('index.php?modul=lieferadresse');
      } else if(val == "nfalse") {
        alert('Bitte wählen Sie mindestens einen Platz aus!');
        goTo('index.php?modul=sitzplan');
      } else if(val == "bfalse") {
        alert('In diesem Block stehen leider nicht mehr genug freie Plätze zur Verfügung!');
        goTo('index.php?modul=sitzplan');
      } else {
        alert('Diese Plätze sind leider bereits reserviert! Bitte wählen Sie andere Plätze aus!');
        goTo('index.php?modul=sitzplan');
      }
    }
  });
}

function setOrderStatus(status, kunde_id)
{
  if($('#'+status+'_'+kunde_id).attr('checked') == true)
  {
    if(confirm("Bitte bestätigen Sie mit 'OK', um eine E-Mail an den Kunden zu senden und den neuen Status zu setzen!"))
    {
      $.ajax({
        url: '../ajax/request-setOrderStatus.php',
        data: {status: status, kunde_id: kunde_id, setStatus: 'checked'},
        type: 'POST',
        async: false,
        cache: false,
        success: function(val) {
          if(val == "true") {
            alert('Eine E-Mail wurde zur Bestätigung an den Kunden gesendet!');
          } else {
            alert('Ein Fehler ist aufgetreten! Bitte erneut versuchen!');
          }
        }
      });
    }
    else
    {
      $('#'+status+'_'+kunde_id).attr('checked', false);
    }
  }
  else
  {
    if(confirm("Bitte bestätigen Sie mit 'OK', um den neuen Status zu setzen (der Kunde erhält keine E-Mail)!"))
    {
      $.ajax({
        url: '../ajax/request-setOrderStatus.php',
        data: {status: status, kunde_id: kunde_id, setStatus: 'unchecked'},
        type: 'POST',
        async: false,
        cache: false,
        success: function(val) {
          if(val == "false") {
            alert('Ein Fehler ist aufgetreten! Bitte erneut versuchen!');
          }
        }
      });
    }
    else
    {
      $('#'+status+'_'+kunde_id).attr('checked', true);
    }
  }
}
