<?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');
  
  $command=isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'';
  if($command!=''){
  	switch($command){
	case 'preview':
		echo 'mail_preview^^';
		echo mail_preview();
		break;
	case 'mail_cancel':
		echo 'input_send_mail_page^^';
		echo input_send_mail_page();
		break;
	case 'send_mail';
		echo send_mail();
		echo 'send_mail_success^^';
		echo input_send_mail_page();
		break;
	case 'mail_back':
		echo 'mail_back^^';
		echo input_send_mail_page();
		break;
	}
  exit;
  }
  
  tep_get_last_access_file();
?>
	<?php function input_send_mail_page(){ ?>
	<?php echo tep_draw_form('mail_form', FILENAME_AFFILIATE_CONTACT, 'action=preview'); ?>
	<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
	<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
    $affiliate = array();
    $affiliate[] = array('id' => '0', 'text' => TEXT_SELECT_AFFILIATE);
    $affiliate[] = array('id' => '***', 'text' => TEXT_ALL_AFFILIATES);
//    $affiliate[] = array('id' => '**D', 'text' => TEXT_NEWSLETTER_AFFILIATES);
    $mail_query = tep_db_query("select affiliate_email_address, affiliate_firstname, affiliate_lastname from " . TABLE_AFFILIATE . " order by affiliate_lastname");
    while($affiliate_values = tep_db_fetch_array($mail_query)) {
      $affiliate[] = array('id' => $affiliate_values['affiliate_email_address'],
                           'text' => $affiliate_values['affiliate_lastname'] . ', ' . $affiliate_values['affiliate_firstname'] . ' (' . $affiliate_values['affiliate_email_address'] . ')');
    }
	?>
	<tr>
	<td class="main"><?php echo TEXT_AFFILIATE; ?></td>
	<td><?php echo tep_draw_pull_down_menu('affiliate_email_address', $affiliate, $HTTP_GET_VARS['affiliate']);?></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', AFFILIATE_EMAIL_ADDRESS, 'size="60"'); ?></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="60"'); ?></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><?php echo tep_draw_textarea_field('message', 'soft', '60', '15'); ?></td>
	</tr>
	<tr>
	<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
	</tr>
	<tr>
	<td colspan="2" align="right"><?php echo '<a href="javascript: mail_form_submit();">'.tep_image_button('button_send_email.gif', IMAGE_SEND_EMAIL).'</a><span id="span_ajax_load"></span>'; ?></td>
	</tr>
	</table></td>
	</form></tr>
	</table>
	<?php } ?>
	
	<?php function mail_preview(){ 
	global $HTTP_POST_VARS;
    switch ($HTTP_POST_VARS['affiliate_email_address']) {
      case '***':
        $mail_sent_to = TEXT_ALL_AFFILIATES;
        break;
//      case '**D':
//        $mail_sent_to = TEXT_NEWSLETTER_AFFILIATES;
//        break;
      default:
        $mail_sent_to = $HTTP_POST_VARS['affiliate_email_address'];
        break;
    }
	?>
	<?php echo tep_draw_form('mail', FILENAME_AFFILIATE_CONTACT, 'action=send_email_to_user'); ?>
	<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_AFFILIATE; ?></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(stripslashes($HTTP_POST_VARS['from'])); ?></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 echo nl2br(htmlspecialchars(stripslashes($HTTP_POST_VARS['message']))); ?></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)));
      }
    }
	?>
	<table border="0" width="100%" cellpadding="0" cellspacing="2">
	<tr>
	<td><?php echo '<a href="javascript: mail_back();">'.tep_image_button('button_back.gif', IMAGE_BACK, 'name="back"').'</a><span id="back_ajax_load"></span>'; ?></td>
	<td align="right"><?php echo '<a href="javascript: mail_cancel();">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a><span id="cancel_ajax_load"></span>&nbsp;<a href="javascript: send_mail();">' . tep_image_button('button_send_email.gif', IMAGE_SEND_EMAIL).'</a><span id="span_ajax_load"></span>'; ?></td>
	</tr>
	</table></td>
	</tr>
	</table>
	</form>
	<?php }
	
	function send_mail(){
	global $HTTP_POST_VARS;
	switch ($HTTP_POST_VARS['affiliate_email_address']) {
      case '***':
        $mail_query = tep_db_query("select affiliate_firstname, affiliate_lastname, affiliate_email_address from " . TABLE_AFFILIATE . " ");
        $mail_sent_to = TEXT_ALL_AFFILIATES;
        break;
//      case '**D':
//        $mail_query = tep_db_query("select affiliate_firstname, affiliate_lastname, affiliate_email_address from " . TABLE_AFFILIATE . " where affiliate_newsletter = '1'");
//        $mail_sent_to = TEXT_NEWSLETTER_AFFILIATE;
//        break;
      default:
        $affiliate_email_address = tep_db_prepare_input($HTTP_POST_VARS['affiliate_email_address']);

        $mail_query = tep_db_query("select affiliate_firstname, affiliate_lastname, affiliate_email_address from " . TABLE_AFFILIATE . " where affiliate_email_address = '" . tep_db_input($affiliate_email_address) . "'");
        $mail_sent_to = $HTTP_POST_VARS['affiliate_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']);

    // Instantiate a new mail object
    $mimemessage = new email(array('X-Mailer: osC mailer'));

    // Build the text version
    $text = strip_tags($text);
    if (EMAIL_USE_HTML == 'true') {
      $mimemessage->add_html($message);
    } else {
      $mimemessage->add_text($message);
    }

    // Send message
    $mimemessage->build_message();
    while ($mail = tep_db_fetch_array($mail_query)) {
      $mimemessage->send($mail['affiliate_firstname'] . ' ' . $mail['affiliate_lastname'], $mail['affiliate_email_address'], '', $from, $subject);
    }
	}
	?>
	
<!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 language="javascript" src="includes/http.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript">
var ajax_image_load='<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>';
	function mail_form_submit(){
	var error_message='';	
	subject=document.mail_form.subject.value;
		if(document.mail_form.affiliate_email_address.value==0)
			error_message='<?php echo ERROR_NO_AFFILIATE_SELECTED; ?>';
		if(subject=='' || str_trim(subject)=='')
			error_message+='\n<?php echo ERROR_SUBJECT; ?>';
		if(error_message!='')
			alert(error_message);
		else{
		do_post_command('mail_form','<?php echo tep_href_link(FILENAME_AFFILIATE_CONTACT,'command=preview'); ?>');
		document.getElementById('span_ajax_load').innerHTML=ajax_image_load;
		}
	}
	function mail_cancel(){
	document.getElementById('cancel_ajax_load').innerHTML=ajax_image_load;
	do_get_command('<?php echo tep_href_link(FILENAME_AFFILIATE_CONTACT,'command=mail_cancel'); ?>');
	}
	function mail_back(){
	document.getElementById('back_ajax_load').innerHTML=ajax_image_load;
	do_post_command('mail','<?php echo tep_href_link(FILENAME_AFFILIATE_CONTACT,'command=mail_back'); ?>');
	}
	function send_mail(){
	do_post_command('mail','<?php echo tep_href_link(FILENAME_AFFILIATE_CONTACT,'command=send_mail'); ?>');
	document.getElementById('span_ajax_load').innerHTML=ajax_image_load;
	}
	function do_result(result){
	var token=result.split('^^');
		switch(token[0]){
		 case 'mail_preview':
		 	document.getElementById('ajax_load').innerHTML=token[1];
		 	break;
		 case 'input_send_mail_page':
		 	document.getElementById('ajax_load').innerHTML=token[1];
		 	break;
		case 'send_mail_success':
			document.getElementById('ajax_load').innerHTML=token[1];
			setInterval('success_function()',3000);
			document.getElementById('subject').value='';
			document.getElementById('message').value='';
			document.getElementById('display_tag').style.display='';
			break;
		case 'mail_back':
			document.getElementById('ajax_load').innerHTML=token[1];
			break;
		}
	}
	function success_function(){
	document.getElementById('display_tag').style.display='none';
	clearInterval();
	}
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- Ajax Work Starts -->
<div id="display_tag" class="smallText" style="padding:10px;display:none;color:#0000FF"><?php echo tep_image(DIR_WS_IMAGES.'icons/success.gif'); ?>&nbsp;Mail Successfully Send</div>
<table width="100%" cellpadding="2" cellspacing="2">
<tr>
	<td width="100%" valign="top"><table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
	 <td width="100%"><table border="0" width="1000%" cellpadding="0" cellspacing="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 id="ajax_load"><?php echo input_send_mail_page(); ?></td>
		</tr>
	</table></td>
</tr>
</table> 

<!-- Ajax Work Ends -->


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