<?php

include_once "../admin.inc.php";

$insert_fields = array();

$insert_fields["email"] = $_POST["email"];

if (!filter_var($insert_fields["email"], FILTER_VALIDATE_EMAIL)) {
  $result = "Указан неправильный эл. адрес.";
}else {

  $insert_fields["base_id"] = $_POST["base_id"];

  for ($counter = 1; $counter <= 5; $counter++) {
    if (isset($_POST["merge_" . $counter]))
      $insert_fields["merge_" . $counter] = $_POST["merge_" . $counter];
  }

  $db->query("DELETE FROM Module_Dashamail_Subscribers WHERE email = '" . $insert_fields["email"] . "'");

  $affected = $db->rows_affected;

  $insert_fields_query = "";

  if (count($insert_fields))
    foreach ($insert_fields as $insert_fields_key => $insert_fields_value) {
      if ($insert_fields_query)
        $insert_fields_query .= ", ";

      $insert_fields_query .= "`" . $insert_fields_key . "` = '" . $insert_fields_value . "'";
    }

  $db->query("INSERT INTO Module_Dashamail_Subscribers SET " . $insert_fields_query . "");

  if ($affected) {
    $result = "Вы уже подписаны на эту рассылку. Ваши данные обновлены.";
  }else {
    $result = "Вы успешно подписались на рассылку.";
  }
}


BeginHtml();
?>
  <body>

  <style type="text/css">
    .subscribeResultForm {
      width:300px;
      height:300px;
      border-radius:5px;
      border:3px #000 solid;
      background:#fff;
      position:fixed;
      top:45%;
      left:50%;
      margin-top:-150px;
      margin-left:-150px;
      display:none;
      opacity:0;
      z-index:5;
      padding:20px 10px;
      text-align:center;
    }

    .subscribeResultForm .subscribeResultForm-text {
      width:90%;
      height:240px;
      margin:0 auto;
      vertical-align:middle;
      display:table-cell;
      text-align:center;
      font-size:36px;
      line-height:1.3em;
    }

    .subscribeResultOverlay {
      z-index:3;
      position:fixed;
      background-color:#000;
      opacity:0.8;
      width:100%;
      height:100%;
      top:0;
      left:0;
      cursor:pointer;
      display:none;
    }

    a.subscribeResultForm-backButton {
      margin-top:50px;
      width:150px;
      height:40px;
      line-height:38px;
      margin:0 auto;
      border:2px solid #505050;
      font-size:24px;
      color:#505050;
      text-align:center;
      margin:5px;
      font-weight:bold;
      display:inline-block;
    }

    a.subscribeResultForm-backButton:hover {
      color:#505050;
    }
  </style>
  <script>
    $(document).ready(function () {

      $('.subscribeResultOverlay').fadeIn(400, function () {
        $('.subscribeResultForm')
          .css('display', 'block')
          .animate({opacity: 1, top: '50%'}, 200);
      });

    });
  </script>

  <div class="subscribeResultForm">
    <div class='subscribeResultForm-text'>
      <? echo $result; ?>
    </div>
    <br/>
    <a class="subscribeResultForm-backButton" href='javascript:history.go(-1)'>Назад</a>
  </div>
  <div class="subscribeResultOverlay"></div>
  </body>
<?
EndHtml();
