jQuery.fn.reverse = [].reverse;

var ladeicon = '<img src="/common/images/ajax.gif" alt="Lade...">';

function checkpass()
{
  var str = 0;
  var imagesrc = 0;
  var staerke = 'keine';
  var farbe = 'cccccc';
  var punkte = 0;

  if ($('#passwort1'))
  {
    if ($('#passwort1').val().length > 0)            {  punkte++; }
    if ($('#passwort1').val().length > 6)            {  punkte++; }
    if ($('#passwort1').val().length > 11)           {  punkte++; }
    if ($('#passwort1').val().match(/[a-z]/))        {  punkte++; }
    if ($('#passwort1').val().match(/[A-Z]/))        {  punkte++; }
    if ($('#passwort1').val().match(/[0-9]/))        {  punkte++; }
    if ($('#passwort1').val().match(/[^0-9a-zA-Z]/)) {  punkte++; }

         if (punkte >= 7) { str = 4; }
    else if (punkte >= 5) { str = 3; }
    else if (punkte >= 3) { str = 2; }
    else if (punkte >= 1) { str = 1; }
  }


  switch (str)
  {
    case 1:
      imagesrc = -20;
      staerke = 'sehr schwach';
      farbe = 'ff0000';
      break;
    case 2:
      imagesrc = -40;
      staerke = 'schwach';
      farbe = 'ff9900';
      break;
    case 3:
      imagesrc = -60;
      staerke = 'gut';
      farbe = '009900';
      break;
    case 4:
      imagesrc = -80;
      staerke = 'sehr gut';
      farbe = '009900';
      break;
  }

  $('#passwordcheck1').css('background-position', '0px ' + imagesrc + 'px');
  $('#passwordcheck2').html('Passwortstärke: ' + staerke);
  $('#passwordcheck2').css('color', '#' + farbe);
}

$( document ).ready(function()
{
  if(document.getElementById("dialog") !== null)
  {
    $('#dialog').dialog({ modal: true, autoOpen: false, minWidth: 400 });
  }
});

function showDialog(titel)
{
  if (!titel)
  {
    titel = 'Benutzerabfrage';
  }
  $('#dialog').dialog({ title: titel });
  $('#dialog').dialog('open');
  return false;
}

function hideDialog()
{
  if ($('#dialog').dialog('isOpen'))
  {
    $('#dialog').dialog('close');
    $('#dialog').dialog('destroy');
  }
  $('#dialog').html(ladeicon);
  $('#dialog').dialog({ modal: true, autoOpen: false, minWidth: 400 });
  return false;
}

function InfoDialog(text, link, titel)
{
  $('#dialog').html(text);
  $('#dialog').dialog({ buttons: [ { text: 'Schliessen',  click: function() { hideDialog(); } } ] });
  showDialog(titel);
  return false;
}

function Dialog(text, link, titel)
{
  $('#dialog').html(text);
  $('#dialog').dialog({ buttons: [ { text: 'Bestätigen', click: function() { $('#dialog').html(ladeicon); window.location.href = link; } },
                                   { text: 'Abbrechen',  click: function() { hideDialog(); } } ] });
  showDialog(titel);
  return false;
}

function InputDialog(text, link, inputfeld, titel)
{
  $('#dialog').html(text + '<br><br><form method="post" action="' + link + '"><div><input type="text" name="' + inputfeld + '"><br><br><input class="button" type="submit" value="Bestätigen"> <input class="button button100" type="button" value="abbrechen" onClick="hideDialog()"></div></form>');
  showDialog(titel);
  return false;
}

function TextboxDialog(text, link, inputfeld, titel)
{
  $('#dialog').html(text + '<br><br><form method="post" action="' + link + '"><div><textarea cols="50" rows="5" name="' + inputfeld + '"></textarea><br><br><input class="button button100" type="submit" value="Bestätigen"> <input type="button" class="button" value="abbrechen" onClick="hideDialog()"></div></form>');
  showDialog(titel);
  return false;
}

function AjaxDialog(text, link, titel, returnhtml)
{
  $('#dialog').html(text + '<br><br><input type="button" class="button" value="Bestätigen" onClick="$(\'#dialog\').html(\'' + ladeicon + ' Bitte warten\'); sendAjaxDialog(\'' + link + '\', \'' + returnhtml + '\'); if (!returnhtml) { hideDialog(); }"> <input type="button" class="button button100" value="abbrechen" onClick="hideDialog()">');
  showDialog(titel);
  return false;
}

function sendAjaxDialog(link, returnhtml)
{
  var url = link;
  $.get(url, function(text)
  {
    if (returnhtml)
    {
      $('#dialog').html(text);
    }
  });
  return false;
}

function AjaxLoadDialog(linkget, titel)
{
  $('#dialog').html(ladeicon);
  showDialog(titel);
  $.get(linkget, function(text) { $('#dialog').html(text); });
  return false;
}

function ShowStatusBalken(instanz)
{
  $.getJSON('/ajax/statusbalken.php',
  {
    instanz: instanz,
    format: 'json'
  })
  .done(function(data)
  {
    $.each(data.bars, function( i, item )
    {
      if ($('#progressbar' + item.UID).length == 0)
      {
        $('#statusbalken').append('<label class="progressbar-caption">' + item.Aktion + '</label>');
        $('#statusbalken').append('<div id="progressbar' + item.UID + '"><div class="progressbar-label" id="progressbar-label' + item.UID + '">0 %</div></div>');
        $('#statusbalken').append('<div style="clear: left; margin-bottom: 4px;"></div>');

        var progressbar   = $('#progressbar' + item.UID),
            progressLabel = $('#progressbar-label' + item.UID);

        progressbar.progressbar({ value: false,
                                  change: function() {
                                    progressLabel.text(progressbar.progressbar('value') + ' %');
                                  }
                                });
      }
      else
      {
        var progressbar   = $('#progressbar' + item.UID),
            progressLabel = $('#progressbar-label' + item.UID);
      }

      var value = Math.round(item.Ist * 100 / item.Soll);
      progressbar.progressbar('value', value);

    });

    if (data.Erfolg)
    {
      $('#Erfolg li:eq(1)').html(data.Erfolg);
      $('#Erfolg').show();
    }
    else if (data.Fehler)
    {
      $('#Fehler li:eq(1)').html(data.Fehler);
      $('#Fehler').show();
    }
    else
    {
      setTimeout(function() { ShowStatusBalken(instanz); }, 500 );
    }
  });
}

$.datepicker.regional['de'] =
{
  closeText: 'Schließen',
  prevText: '< zurück',
  nextText: 'vor >',
  currentText: 'Jetzt',
  monthNames: ['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
  monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'],
  dayNames: ['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag'],
  dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
  dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
  weekHeader: 'KW',
  dateFormat: 'dd.mm.yy',
  timeText: 'Uhrzeit',
  firstDay: 1,
  isRTL: false,
  showMonthAfterYear: false,
  yearSuffix: ''
};
$.datepicker.setDefaults($.datepicker.regional['de']);

if ($.timepicker)
{
  $.timepicker.regional['de'] = {
   timeOnlyTitle: 'Uhrzeit wählen',
   timeText: 'Uhrzeit',
   hourText: 'Stunde',
   minuteText: 'Minute',
   secondText: 'Sekunde',
   millisecText: 'Millisekunde',
   timezoneText: 'Zeitzone',
   currentText: 'Jetzt',
   closeText: 'Schließen',
   timeFormat: 'HH:mm',
   isRTL: false
  };
  $.timepicker.setDefaults($.timepicker.regional['de']);
}

function saveCookies()
{
  var cookies = 0;

  if ($('#cookie1').prop("checked") == true) { cookies += 16; }
  if ($('#cookie2').prop("checked") == true) { cookies += 2; }
  if ($('#cookie4').prop("checked") == true) { cookies += 4; }
  if ($('#cookie8').prop("checked") == true) { cookies += 8; }

  var d = new Date();
  d.setTime(d.getTime() + (365*24*60*60*1000));
  var expires = "expires="+ d.toUTCString();
  document.cookie = "cookieaccept=" + cookies + ";" + expires + "; path=/";

  alert('Ihre Einstellungen zum Datenschutz wurden gespeichert.');

  location.reload();
}