<?php

/*
osCommerce, Open Source E-Commerce Solutions 
http://www.oscommerce.com 

Copyright (c) 2003 osCommerce 

osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

	require('includes/application_top.php');
	tep_get_last_access_file();                                                                                                   
	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
   // ation send_email_to_user for mail sending
	if ( ($action == 'send_email_to_user') && isset($HTTP_POST_VARS['customers_email_address']) && !isset($HTTP_POST_VARS['back_x']) ) {
		switch ($HTTP_POST_VARS['customers_email_address']) {
			case '***':
				$mail_query = tep_db_query("select customers_firstname,customers_id, customers_lastname, customers_email_address, customers_password from " . TABLE_CUSTOMERS);
				$mail_sent_to = TEXT_ALL_CUSTOMERS;
				break;
			case '**D':
				$mail_query = tep_db_query("select customers_firstname,customers_id, customers_lastname, customers_email_address, customers_password from " . TABLE_CUSTOMERS . " where customers_newsletter = '1'");
				$mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
				break;
			default:
				$customers_email_address = tep_db_prepare_input($HTTP_POST_VARS['customers_email_address']);
				$mail_query = tep_db_query("select customers_firstname, customers_id, customers_lastname, customers_email_address, customers_password from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($customers_email_address) . "'");
				$mail_sent_to = $HTTP_POST_VARS['customers_email_address'];
				break;
		}
		$from = tep_db_prepare_input($HTTP_POST_VARS['from']);
		$subject = tep_db_prepare_input($HTTP_POST_VARS['subject']);
		$message = tep_db_prepare_input($HTTP_POST_VARS['message']);

		//Let's build a message object using the email class
		$mimemessage = new email(array('X-Mailer: osCommerce'));
		// add the message to the object
		
		while ($mail = tep_db_fetch_array($mail_query)) {
			$message_detail=$message;
			$pos=strpos($message,"%%Password%%");
			if($chkpwd==true && $pos)
		{
		$pww=tep_rand(5);
		$password=tep_encrypt_password($pww);
		tep_db_query ("update " . TABLE_CUSTOMERS . " set customers_password = '". $password."' where customers_id = '" . $mail['customers_id']. "'");
		}
		else
		{
		$pww="---- Hidden----";
		}
			$merge_details=array(	TEXT_FN=>$mail['customers_firstname'],
									TEXT_LN=>$mail['customers_lastname'],
									TEXT_SN=>STORE_NAME,
									TEXT_SM=>STORE_OWNER,
									TEXT_SE=>STORE_OWNER_EMAIL_ADDRESS,
									TEXT_LA=>tep_catalog_href_link(FILENAME_AUTOLOGIN,'email=' . $mail['customers_email_address'] . '&id=' . $mail['customers_password']),
									TEXT_EA=>$mail['customers_email_address'],
									TEXT_PWD=>$pww
								);
								
								
			
			while(list($key,$value)=each($merge_details))
			$message_detail=str_replace("%%" . $key  . "%%",$value,$message_detail);
			$message_detail=str_replace('Â', " ", $message_detail);
			
			$message_text=strip_tags($message_detail,'<br>');
			$message_text=str_replace('<br>',chr(13) . chr(10),$message_text);
			$message_text=str_replace('<BR>',chr(13) . chr(10),$message_text);
			   
			if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Disable') {
				$mimemessage->add_text($message_text);
			} else {
				$mimemessage->add_html($message_detail,$message_text);
			}
			$mimemessage->build_message();
		//	echo $mail['customers_firstname'] . ' ' . $mail['customers_lastname'], $mail['customers_email_address'], '', $from, $subject,$message_detail;
			$mimemessage->send($mail['customers_firstname'] . ' ' . $mail['customers_lastname'], $mail['customers_email_address'], '', $from, $subject);
		}
    	tep_redirect(tep_href_link(FILENAME_MAIL, 'mail_sent_to=' . urlencode($mail_sent_to)));
	}

	if ( ($action == 'preview') && !isset($HTTP_POST_VARS['customers_email_address']) ) {
		$messageStack->add(ERROR_NO_CUSTOMER_SELECTED, 'error');
	}

	if (isset($HTTP_GET_VARS['mail_sent_to'])) {
		$messageStack->add(sprintf(NOTICE_EMAIL_SENT_TO, $HTTP_GET_VARS['mail_sent_to']), 'success');
	}

	if (isset($HTTP_POST_VARS['from'])) {
		$from_email=$HTTP_POST_VARS["from"];
	} else {
		$from_email=$HTTP_SESSION_VARS["login_email"];
	}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script type="text/javascript" src="htmlarea/htmlarea.js"></script>
<script type="text/javascript" src="htmlarea/editor.js"></script>
<script language="JavaScript" src="htmlarea/validation.js"></script>
<script language="JavaScript">
<!-- Begin
var pawd="<?php echo TEXT_PWD ?>";
       function init() {
define('customers_email_address', 'string', 'Customer or Newsletter Group');
}
//  End -->
</script>
</head>
<body OnLoad="init()" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">

<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr> 
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
         <!--- <tr>
            <td class="pageHeading"><?php //echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php //echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr> !~-->
        </table></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php
  if ( ($action == 'preview') && isset($HTTP_POST_VARS['customers_email_address']) ) {
    switch ($HTTP_POST_VARS['customers_email_address']) {
      case '***':
        $mail_sent_to = TEXT_ALL_CUSTOMERS;
        break;
      case '**D':
        $mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
        break;
      default:
        $mail_sent_to = $HTTP_POST_VARS['customers_email_address'];
        break;
    }
    $merge_details=array(TEXT_FN=>TEST_MAIL_FN,
                          TEXT_LN=>TEST_MAIL_LN,
			              TEXT_SN=>TEST_MAIL_SN,
    			          TEXT_SM=>TEST_MAIL_SM,
			              TEXT_SE=>TEST_MAIL_SE,
			              TEXT_LA=>TEST_MAIL_LA,
						  TEXT_EA=>TEST_MAIL_EA,
						  TEXT_PWD=>TEST_MAIL_PWD
			             );
   
    $message_text=$HTTP_POST_VARS['message'];
	while(list($key,$value)=each($merge_details))
       $message_text=str_replace("%%" . $key  . "%%",$value,$message_text);	
?>
          <tr><?php
		  //email sending
		   echo tep_draw_form('mail', FILENAME_MAIL, 'action=send_email_to_user'); ?>
            <td><table border="0" width="100%" cellpadding="0" cellspacing="2">
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_CUSTOMER; ?></b><br><?php echo $mail_sent_to; ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_FROM; ?></b><br><?php echo htmlspecialchars(STORE_OWNER_EMAIL_ADDRESS); ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_SUBJECT; ?></b><br><?php echo htmlspecialchars(stripslashes($HTTP_POST_VARS['subject'])); ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_MESSAGE; ?></b><br><?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Enable') { echo (stripslashes($message_text)); } else { echo htmlspecialchars(stripslashes($message_text)); } ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td>
				<?php
				/* Re-Post all POST'ed variables */
					reset($HTTP_POST_VARS);
					while (list($key, $value) = each($HTTP_POST_VARS)) {
					  if (!is_array($HTTP_POST_VARS[$key])) {
						echo tep_draw_hidden_field($key, htmlspecialchars(stripslashes($value)));
					  }
					}
				?>
                    <tr>
	                    <td align="right">
						<?php 
							echo tep_image_submit('button_back.gif', IMAGE_BACK, 'name="back"')  . '&nbsp;' . tep_image_submit('button_send_email.gif', IMAGE_SEND_EMAIL); 
						?>
						</td>
                    </tr>
                </table></td>
             </tr>
            </table></td>
          </form></tr>
          
          
<?php
  } else {
?>
<script language="javascript">
//var pwd ="<?php //echo TEXT_PWD?>";
	function AddField(){
	 var select_field;
	 var select_id;
	
	 if (document.mail.fields.selectedIndex<=-1) return;
	 select_id=document.mail.fields.options[document.mail.fields.selectedIndex].value;
	
	 
	 if (select_id.length>3) return;
	
	 select_field="%%" + document.mail.fields.options[document.mail.fields.selectedIndex].innerHTML + "%%";
	 select_field=select_field.replace("&nbsp;&nbsp;","");
	
	
	 if(document.mail.chkpwd.checked==false && select_id=='PWD' ){
	 select_field="--"+"Hidden"+"--";
	
	
	 }
     editor.insertHTML(select_field);
	}
	
function pwd(obj){

if (obj)
	{
		if(confirm("Do you want to create a New Password?")){
		//alert ("ok");
		}
	//	else
	//	{
		//alert ("cancel");
	//	}
		}
		

}
	
/*	function pwd(obj){
	var select_field1
	if (obj)
	{
	// select_id=document.mail.fields.options[document.mail.fields.selectedIndex].value;
	  
		if(confirm("Do you want to create a password")){
			var oOption = document.createElement("OPTION");
							oOption.text=pawd;
							oOption.value='PW';
							
	 		 document.mail.fields.add(oOption,document.mail.fields.length+1)
			  
			 for (icnt=0;icnt<document.mail.fields.length;icnt++){
				pp=document.mail.fields.options[icnt].text;
				
				if(pp == "<?php //echo TEXT_PWD;?>"){
					return true;
				}
			}
			 
			
		}
		else
		{
		alert ("cancel")
		}
		
		
	}
	
	}*/
</script>
          <tr><?php echo tep_draw_form('mail', FILENAME_MAIL, 'action=preview'); ?>
            <td><table border="0" cellpadding="0" cellspacing="2">
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
<?php
    $customers = array();
    $customers[] = array('id' => '', 'text' => TEXT_SELECT_CUSTOMER);
    $customers[] = array('id' => '***', 'text' => TEXT_ALL_CUSTOMERS);
    $customers[] = array('id' => '**D', 'text' => TEXT_NEWSLETTER_CUSTOMERS);
    $mail_query = tep_db_query("select customers_email_address, customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " order by customers_lastname");
    while($customers_values = tep_db_fetch_array($mail_query)) {
    $customers[] = array('id' => $customers_values['customers_email_address'],
                           'text' => $customers_values['customers_lastname'] . ', ' . $customers_values['customers_firstname'] . ' (' . $customers_values['customers_email_address'] . ')');
    }
    $fields_details=array(array('id'=>'TITLE_C','text'=>TEXT_TITLE_C),
                               array('id'=>'FN','text'=>'&nbsp;&nbsp;' . TEXT_FN),
                               array('id'=>'LN','text'=>'&nbsp;&nbsp;' . TEXT_LN),
			    			   array('id'=>'SN','text'=>'&nbsp;&nbsp;' . TEXT_SN),
    						   array('id'=>'SM','text'=>'&nbsp;&nbsp;' . TEXT_SM),
			   			       array('id'=>'SE','text'=>'&nbsp;&nbsp;' . TEXT_SE),
			      			   array('id'=>'LA','text'=>'&nbsp;&nbsp;' . TEXT_LA),
							   array('id'=>'EA','text'=>'&nbsp;&nbsp;' . TEXT_EA),
							   array('id'=>'PWD','text'=>'&nbsp;&nbsp;' . TEXT_PWD)
			       );
?>
              <tr>
                <td class="main"><?php echo TEXT_CUSTOMER; ?></td>
                <td><?php echo tep_draw_pull_down_menu('customers_email_address', $customers, (isset($HTTP_GET_VARS['customer']) ? $HTTP_GET_VARS['customer'] : ''));?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="main"><?php echo TEXT_FROM; ?></td>
                <td><?php echo tep_draw_input_field('from', STORE_OWNER_EMAIL_ADDRESS,'size=40'); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="main"><?php echo TEXT_SUBJECT; ?></td>
                <td><?php echo tep_draw_input_field('subject','','size=40'); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td valign="top" class="main"><?php echo TEXT_MESSAGE; ?></td>
                <td><table border=0 cellspacing="0" cellpadding="0" style="background:ButtonFace;"><tr><td><?php echo tep_draw_textarea_field('message', 'soft', '80', '24','','id="message"'); ?></td>
				     <td  style="background:ButtonFace;" valign=bottom align=center>
          <?php 
		       echo "<div class='main'><b>" . TEXT_MERGE_FIELDS . '</b></div><br>';
		       echo tep_draw_pull_down_menu('fields',$fields_details,'','style="height:' .((strpos($_SERVER['HTTP_USER_AGENT'],"MSIE")>0)?'224':'241') .'" size=15 ondblClick="AddField()"');
		  ?></td></tr></table></td>

<?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Enable') { ?>
           <script>initEditor('message');</script>
<?php }
   ?>
          </script>
                
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
			  <tr>
			  <td align="center"><?php echo tep_draw_checkbox_field('chkpwd','','','','onClick="javascript:pwd(this.checked);"') . '&nbsp;' . TEXT_CREATE_PWD;?></td>
			  </tr>
              <tr>
              
              
                <td colspan="2" align="right">
                 <?php 
				 		//echo '<a href="' . tep_href_link(FILENAME_CUSTOMERS,'selected_box=customers') . '">' . tep_image_button('button_cancel.gif',IMAGE_CANCEL) . '</a>&nbsp;';
				 	   if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Enable'){ 
				 			echo tep_image_submit('button_send_email.gif', IMAGE_SEND_EMAIL, 'onClick="validate();return returnVal;"');
	                   } else {
    			            echo tep_image_submit('button_send_email.gif', IMAGE_SEND_EMAIL); 
					   }
				?>
                </td>
                
                
              </tr>
            </table></td>
          </form></tr>
<?php
  }
?>
<!-- body_text_eof //-->
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
