<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/

require('includes/application_top.php');
require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_NEWSLETTERS);

$error = false;

$newsletter_email = tep_db_prepare_input($HTTP_POST_VARS['Email']);
$newsletter_lastname = tep_db_prepare_input($HTTP_POST_VARS['lastname']);
$email_type = tep_db_prepare_input($HTTP_POST_VARS['email_type']);

if (strlen($newsletter_email) < ENTRY_EMAIL_ADDRESS_MIN_LENGTH) {
  $error = true;
} elseif (tep_validate_email($newsletter_email) == false) {
  $error = true;
}

if ($error == false) {
  $subscribers_info = tep_db_query("select subscribers_id from " . TABLE_SUBSCRIBERS . " where subscribers_email_address = '" . $newsletter_email . "' ");
  $date_now = date('Ymd');

  if (!tep_db_num_rows($subscribers_info)) {
    $gender = '' ;
    tep_db_query("insert into " . TABLE_SUBSCRIBERS . " (subscribers_email_address, subscribers_lastname, language, subscribers_email_type, date_account_created, customers_newsletter,  subscribers_blacklist, hardiness_zone, status_sent1,  source_import) values ('" . strtolower($newsletter_email) . "',  '" . ucwords(strtolower($newsletter_lastname)) . "',  'English',  '" . $HTTP_POST_VARS['email_type'] . "',  now() ,  '1',  '0', '" . $domain4 . "', '1', 'subscribe_newsletter')");
  } else {
    tep_db_query("update " . TABLE_SUBSCRIBERS . " set customers_newsletter = '" . '1' . "', subscribers_email_type = '" . $HTTP_POST_VARS['email_type'] . "'  where subscribers_email_address  = '" . $newsletter_email . "' ");
  }

  mail_newsletter_subscribe($newsletter_email);
  
  tep_redirect(tep_href_link(FILENAME_NEWSLETTERS_SUBSCRIBE_SUCCESS, '', 'NONSSL'));
} else {
  tep_redirect(tep_href_link(FILENAME_NEWSLETTERS_SUBSCRIBE_ERROR, '', 'NONSSL'));
}
?>