<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

if (isset($_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"] != "") {
    $adminname = $_SESSION["sess_adminname"];
} else {
    header("Location:login.php");
    exit;
}

$txtSubject = trim(stripslashes($_POST["txtSubject"]));
$txtMailContent = trim(stripslashes($_POST["txtMailContent"]));
$ddlArtist = $_POST["ddlArtist"];

if (isset($_POST["btnSendMail"])) {
    $message = "";
    if (!isNotNull($txtSubject)) {
        $message .= "* ".SUB_EMPTY."!<br>";
    }
    if (!isNotNull($txtMailContent)) {
        $message .= "* ".CONTENT_EMPTY."!<br>";
    }
    if (!isNotNull($ddlArtist)) {
        $message .= "* ".SEL_AFF."!<br>";
    }

    if ($message == "") {


        /* ---------Multicart 2.0 Upgradation----------- */
        /* for($count_start = 1;$count_start < count($ddlArtist);$count_start ++)
          {
          $artistlist .= $ddlArtist[$count_start] . ",";

          }
          $artistlist = substr($artistlist,0,strlen($artistlist)-1); */

        //$sellerlist = $_POST['ddlArtist'];

        foreach ($_POST['ddlArtist'] as $sellerlist) {

            $artistlist[] = $sellerlist;

            //$sellerlist[]= $ddlArtist[$count_start] . ",";
        }


        $artistlist = join(",", $artistlist);

        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
        $headers .= "From: " . SITE_EMAIL . "<" . SITE_EMAIL . ">" . "\r\n";
        $headers .= 'Bcc: ';

        /* ---------Multicart 2.0 Upgradation----------- */

        /* $sqlemails_to = "SELECT email,first_name,last_name FROM ".$tableprefix."affiliates WHERE affiliate_id= ".$ddlArtist[0];

          $res_to = mysql_query($sqlemails_to) or die(mysql_error());

          if(mysql_num_rows($res_to) > 0)
          {
          $res_row = mysql_fetch_assoc($res_to);
          $to = $res_row['email'];
          $name = $res_row['first_name']." ".$res_row['last_name'];
          } */

        $subject = $txtSubject;
        $mailcontent = nl2br($txtMailContent);

        if ($artistlist != "") {
            $sqlemails = "SELECT email,first_name,last_name FROM " . $tableprefix . "affiliates WHERE affiliate_id IN ($artistlist) ";

            $res = mysql_query($sqlemails);
            if (mysql_num_rows($res) != 0) {
                while ($row = mysql_fetch_array($res)) {
                    $to = $row['email'];

                    $name = $row['first_name'] . " " . $row['last_name'];


                    $mail_send_details = "<html>
                                        <head>
                <link href='" . SITE_URL . "/style/template1-admin.css' rel='stylesheet' type='text/css'>
                </head><body>
                <center>
                <div align='left' id='layout'>
                <div align='left' class='header'>
                <table width='80%' cellpadding='5' class='header'>
                <tr><td><div align='left' class='logo'><img src=" . $logofolder . "/" . $logoimage . " width='125' height='39'/></div></td></tr>
                <tr><td align='left'><p>".MAIL_ORDER_TEXT_DEAR." " . $name . "</p></td>
                <tr><td><div>" . $mailcontent . "</div></td></tr>
                <tr>
                <td align='left'><p>".MAIL_SUBSCRIPTION_REMINDER_END.",<br>".MAIL_SUBSCRIPTION_SIGNATURE_ADMIN." <br>" . stripslashes(SITE_NAME) . "</p>
                </td>
                </tr>
                <div align='left' class='clear_float'><img src=" . $imagefolder . "/clear.gif width=1 height=1/></div>
                </table>
                </div></div>
                </center></body></html>";

                    // echo "<br>".$mail_send_details;

                    $mailsent = @mail($to, $subject, $mail_send_details, $headers);
                }
            }

            //$to = substr($to,1);
            //$bcc_copies = substr($bcc_copies,1);
            //$headers .= $bcc_copies;
        }




        /* ---------Multicart 2.0 Upgradation----------- */



        /* ---------Multicart 2.0 Upgradation----------- */

        if ($mailsent) {
            $success_message = AFF_MAIL_SUCCESS."<br>";
            $success = 1;
        } else {
            $message = MAIL_ERROR."<br>";
            $success = 0;
        }
    } else {
        $message = "<br>".MSG_CORRECT_THE_ERRORS."<br>" . $message;
    }
}

if (isset($_GET["msg"]) and $_GET["msg"] != "") {
    $msg = $_GET["msg"];
    switch ($msg) {
        case "deleted": $success_message = MSG_USER_DELETED;
            break;
        default:
            ;
    } // switch
}

if ($success == 1) {
    $txtSubject = "";
    $txtMailContent = "";
    $ddlArtist = "";
}

$artistlist = makeAffiliateNameList();

include "../includes/htmltop.php";
?>


<body>

<center>

    <div align="left" id="layout">




        <!--header area start -->
        <?php
        include_once("../includes/topheader_admin.php")
        ?>

        <!--header area end -->
        <div align="left" class="main_body">
            <!--main body start -->
            <?php
            include_once("../includes/admintopmenu.php")
            ?>		
            <div align="left" class="content_area">

                <!--content area start -->

                <div align="left" class="page_title"><?php echo MAIL_AFF;?></div>
                <div align="center" class="text_information grey_tr"><?php echo $message ?></div>
                <div align="center" class="text_information_success grey_tr"><?php echo $success_message; ?></div>
                <br>

                <div align="left">			

                    <form name="frmFeedback" action="<?php echo $_SERVER["PHP_SELF"] ?>" method="POST">
                        <table cellpadding="0" cellspacing="0" width="100%" class="manage_table">
                            <tr><td colspan="3">&nbsp;</td></tr>
                            <tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo FIELDS_ARE_MANDATORY;?></td></tr>

                            <tr><td width="40%" align="right" valign=top><?php echo TO;?>&nbsp;<span class="required">*</span></td>
                                <td width="3%" valign="top" ></td>
                                <td width="57%" align="left" valign=top><?php echo makeDropDownList("ddlArtist[]", $artistlist, $ddlArtist, false, 'textarea2', ' multiple size=10 cols=40 ', $behaviors) ?></td>
                            </tr>
                            <tr><td align="right" valign=top><?php echo SUBJECT;?>&nbsp;<span class="required">*</span></td>
                                <td valign="top" ></td>
                                <td valign=top align="left"><input class="textarea2" type="text" name="txtSubject" value="<?php echo htmlentities(stripslashes($txtSubject)); ?>" size="60" maxlength="255"></td>
                            </tr>
                            <tr><td align="right" valign=top><?php echo MAIL_CONTENT;?>&nbsp;<span class="required">*</span></td>
                                <td valign="top" ></td>
                                <td valign=top align="left"><textarea name='txtMailContent' rows='10' class="textarea2" cols='50'><?php echo htmlentities(stripslashes($txtMailContent)); ?></textarea></td>
                            </tr>
                            <tr><td colspan="3">&nbsp;</td></tr>
                            <tr><td colspan="3" align="center">
                                    <input type="submit" value="<?php echo SEND_MAIL;?>" name="btnSendMail" class="button">

                                </td>
                            </tr>
                        </table>
                    </form>

                </div>


                <!--content area end-->

            </div>


            <div align="left" class="clear_float"><img src="<?php echo $imagefolder; ?>/clear.gif" alt="" width="1" height="1" /></div>
            <!--main body end -->
        </div>


        <?php
        include "../includes/footer.php";
        ?>