<?php

include "checklog.php";

if ($HTTP_POST_VARS['action'] == "upload") {
	if (is_uploaded_file($HTTP_POST_FILES['msgfile']['tmp_name'])) {
		$FileName = $HTTP_POST_FILES['msgfile']['tmp_name'];
		$file = fopen($FileName, "rb");
		if ($file) {
			$msg = fread($file, filesize($FileName));
			fclose($file);
			$msg = rawurlencode($msg);
		} else {
			$msg = "";
		}
	} else {
		$msg = "";
	}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<base target="_self">
<script language="JavaScript">
<!--
function LoadMsg() {
	top.frames["intestazione"].document.tmpform.tmpfield1.value = "<?php echo $msg; ?>";
	self.location = "bonews1.php";
}
//-->
</script>
</head>
<body onLoad="LoadMsg();">
</body>
</html>
<?php
} elseif ($HTTP_POST_VARS['action'] == "send") {
	$mitt = $HTTP_POST_VARS["mittente"];
	$ogg = $HTTP_POST_VARS["oggetto"];
	$dest = $HTTP_POST_VARS["destinatari"];
	$enc = $HTTP_POST_VARS["tipo"];
	$test = $HTTP_POST_VARS["prova"];
	$msg = $HTTP_POST_VARS["messaggio"];
	
	if ($test == 1) {
		$elenco = $mitt;
	} else {
		if ($dest == 0) {
			$sql = "SELECT DISTINCT email FROM clienti WHERE flag1 = 1 AND (%1)";
		} elseif ($dest == 1) {
			$sql = "SELECT DISTINCT email FROM clienti WHERE news = 1 AND (%1)";
		} elseif ($dest == 2) {
			$sql = "SELECT DISTINCT email FROM clienti WHERE (%1)";
		}
		$listsql = "1 = 0";
		if ($HTTP_POST_VARS["listino1"] == "y") { $listsql .= " OR list = 0"; }
		if ($HTTP_POST_VARS["listino2"] == "y") { $listsql .= " OR list = 1"; }
		if ($HTTP_POST_VARS["listino3"] == "y") { $listsql .= " OR list = 2"; }
		if ($HTTP_POST_VARS["listino4"] == "y") { $listsql .= " OR list = 3"; }
		if ($HTTP_POST_VARS["listino5"] == "y") { $listsql .= " OR list = 4"; }
		if ($HTTP_POST_VARS["listino6"] == "y") { $listsql .= " OR list = 5"; }
		if ($HTTP_POST_VARS["listino7"] == "y") { $listsql .= " OR list = 6"; }
		if ($HTTP_POST_VARS["listino8"] == "y") { $listsql .= " OR list = 7"; }
		if ($HTTP_POST_VARS["listino9"] == "y") { $listsql .= " OR list = 8"; }
		if ($HTTP_POST_VARS["listino10"] == "y") { $listsql .= " OR list = 9"; }
		$sql = str_replace('%1',$listsql,$sql);
		$result = mysql_query($sql);
		while ($row = mysql_fetch_array($result)) {
			$elenco .= $row['email'] . "\n";
		}
		$elenco = trim($elenco);
	}
?>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<base target="_self">
<link rel="stylesheet" href="backoffice.css" type="text/css">

<script language="JavaScript">
<!--

function CheckForm() {
	ammessi = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ1234567890";
	testo = document.view.elenco.value;
	lung=testo.length;
	n = 0;
	newtesto = "";
	while (n < lung) {
		n = n + 1;
		carattere=testo.charAt(n);
		if (ammessi.indexOf(carattere) > -1) {
			newtesto = newtesto + carattere;
		}
	}
	if (newtesto == "") {
		alert("Non è presente alcun destinatario");
		return false;
	} else {
		return true;
	}
}

//-->
</script>
</head>

<body topmargin="0" leftmargin="4" marginwidth="4" marginheight="0" class="tooltip">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td width="50%"><span class="titolo">Newsletter - </span><span class="sottotitolo">Conferma 
      invio</span></td>
    <td width="25%">&nbsp;</td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>
<form  name="view" action="bonews1send.php" method="post" onSubmit="return CheckForm();">
<input type="hidden" name="mittente" value="<?php echo MakeClean($mitt); ?>">
<input type="hidden" name="oggetto" value="<?php echo MakeClean($ogg); ?>">
<input type="hidden" name="tipo" value="<?php echo MakeClean($enc); ?>">
<input type="hidden" name="messaggio" value="<?php echo rawurlencode($msg); ?>">
  <table border="1" bordercolor="#333399" cellpadding="4" cellspacing="0" width="600">
    <tr><td>
        <table border="0" class="form">
          <tr> 
            <td colspan="4" class="intestazione">Destinatari</td>
          </tr>
          <tr> 
            <td valign="top"><b>Elenco destinatari</b>:<br>
              (&egrave; possibile aggiungere manualmente nuovi destinatari o rimuoverne 
              alcuni) </td>
            <td colspan="3"> 
              <textarea name="elenco" cols="40" rows="20" class="pulsante"><?php echo $elenco; ?></textarea>
            </td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td> 
              <input type="submit" name="Submit" value="Invia" class="pulsante">
            </td>
            <td colspan="2">&nbsp; </td>
          </tr>
        </table>
  </td></tr></table>
</form>
</body>
</html>
<?php
}
?>