<?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_WISHLIST);

  /*******************************************************************
  ******* ADD PRODUCT TO WISHLIST IF PRODUCT ID IS REGISTERED ********
  *******************************************************************/
  if(tep_session_is_registered('wishlist_id')) {
    $wishlist->add_wishlist($wishlist_id, $attributes_id);

    if(WISHLIST_REDIRECT == 'Tak') {
      tep_redirect(tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $wishlist_id));
    } else {
      tep_session_unregister('wishlist_id');
    }
  }

  /*******************************************************************
  ****************** ADD PRODUCT TO SHOPPING CART ********************
  *******************************************************************/
  if (isset($HTTP_POST_VARS['add_wishprod'])) {
     foreach ($HTTP_POST_VARS['add_wishprod'] as $value) {
        $product_id = tep_get_prid($value);
        if ( $HTTP_POST_VARS['przycisk'] == $value ) {
          $cart->add_cart($product_id, $cart->get_quantity(tep_get_uprid($product_id, $HTTP_POST_VARS['id'][$value]))+1, $HTTP_POST_VARS['id'][$value]);
          $messageStack->add('wishlist', WISHLIST_ADD_TO_CART, 'success');
        }
      }
  }


  /*******************************************************************
  ****************** DELETE PRODUCT FROM WISHLIST ********************
  *******************************************************************/
  if (isset($HTTP_GET_VARS['action'])) {
    if ($HTTP_GET_VARS['action'] == 'remove_wishlist') {
        $value = $HTTP_GET_VARS['products_id'];
        $wishlist->remove($value);
    }
    $messageStack->add('wishlist', WISHLIST_DELETE_PRODUCT, 'success');
  }

  /*******************************************************************
  ************* EMAIL THE WISHLIST TO MULTIPLE FRIENDS ***************
  *******************************************************************/
  if (isset($_POST['email_prod_x'])) {

    $errors = false;
    $ciag_bledu = '';
    $guest_errors = "";
    $email_errors = "";
    $message_error = "";

    if(strlen($_POST['message']) < '1') {
      $error = true;
      $ciag_bledu .= '* '.ERROR_MESSAGE.'<br>';
    }

    if(tep_session_is_registered('customer_id')) {
      $customer_query = tep_db_query("select customers_firstname, customers_lastname, customers_email_address from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
      $customer = tep_db_fetch_array($customer_query);

      $from_name = $customer['customers_firstname'] . ' ' . $customer['customers_lastname'];
      $from_email = $customer['customers_email_address'];
      $subject = $customer['customers_firstname'] . ' ' . WISHLIST_EMAIL_SUBJECT;
      $link = HTTP_SERVER . DIR_WS_CATALOG . FILENAME_WISHLIST_PUBLIC . "?public_id=" . $customer_id;

      //REPLACE VARIABLES FROM DEFINE
      $arr1 = array('$from_name', '$link');
      $arr2 = array($from_name, $link);
      $replace = str_replace($arr1, $arr2, WISHLIST_EMAIL_LINK);
      $message = tep_db_prepare_input($HTTP_POST_VARS['message']);
      $body = $message . $replace;
    } else {
      if(strlen($_POST['your_name']) < '1') {
        $error = true;
        $ciag_bledu .= '* '.ERROR_YOUR_NAME.'<br>';        
      }
      if(strlen($_POST['your_email']) < '1') {
        $error = true;
        $ciag_bledu .= '* '.ERROR_YOUR_EMAIL.'<br>';                
      } elseif(!tep_validate_email($_POST['your_email'])) {
        $error = true;
        $ciag_bledu .= '* '.ERROR_VALID_EMAIL.'<br>'; 
      }

      $from_name = stripslashes($_POST['your_name']);
      $from_email = $_POST['your_email'];
      $subject = $from_name . ' ' . WISHLIST_EMAIL_SUBJECT;
      $message = stripslashes($HTTP_POST_VARS['message']);

      $z = 0;
      $prods = "";
      foreach($HTTP_POST_VARS['prod_name'] as $name) {
        $prods .= stripslashes($name) . "  " . stripslashes($HTTP_POST_VARS['prod_att'][$z]) . "\n" . $HTTP_POST_VARS['prod_link'][$z] . "\n\n";
        $z++;
      }
      $body = $message . $prods;
    }

    //Check each posted name => email for errors.
    $j = 0;
    $email = $HTTP_POST_VARS['email'];
    foreach($HTTP_POST_VARS['friend'] as $friendx) {
      if($j == 0) {
        if($friend[0] == '' && $email[0] == '') {
          $error = true;
          $ciag_bledu .= '* '.ERROR_ONE_EMAIL.'<br>'; 
        }
      }

      if(isset($friendx) && $friendx != '') {
        if(strlen($email[$j]) < '1') {
          $error = true;
          $ciag_bledu .= '* '.ERROR_ENTER_EMAIL.'<br>'; 
        } elseif(!tep_validate_email($email[$j])) {
          $error = true;
          $ciag_bledu .= '* '.ERROR_VALID_EMAIL.'<br>';
        }
      }

      if(isset($email[$j]) && $email[$j] != '') {
        if(strlen($friendx) < '1') {
          $error = true;
          $ciag_bledu .= '* '.ERROR_ENTER_NAME.'<br>';
        }
      }
      $j++;
    }
    if($error == false) {
      $j = 0;
      foreach($HTTP_POST_VARS['friend'] as $friendx) {
        if($friendx != '') {
          mail_wishlist($friendx,$email[$j],$subject,$message,$prods,$replace,$from_name,$from_email);
        }

        //Clear Values
        $friend[$j] = "";
        $email[$j] = "";
        $message = "";

        $j++;
      }

      $messageStack->add('wishlist', WISHLIST_SENT, 'success');
    } else {
      $messageStack->add('wishlist',$ciag_bledu, 'error');
    }
  }


  $breadcrumb->add(NAVBAR_TITLE_WISHLIST, tep_href_link(FILENAME_WISHLIST, '', 'SSL'));

  $content = 'wishlist';

  include (bts_select('main', $content)); // BTSv1.5

  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>