<?php

#############################################################################
#                              Site@School Newsletter module
# Site@School is a website management system designed for schools.
# This is a newsletter module which can be used in Site@School.
#
# coded by Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# newsletter.php: last changed 13/04/2006   SAS version 2.4
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: newsletter.php,v 1.1 2006/10/03 10:33:56 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2005 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}
if (IsSet ($_GET['option']))
{
	$option = $_GET['option'];
}
else
{
	$option = "list";
}
if (IsSet ($_GET['code']))
{
	$code = $_GET['code'];
}
if (IsSet ($_GET['newsletter_id']))
{
	$newsletter_id = $_GET['newsletter_id'];
}
if (IsSet ($_POST['fullname']))
{
	$fullname = $_POST['fullname'];
}
if (IsSet ($_POST['surename']))
{
	$surename = $_POST['surename'];
}
if (IsSet ($_POST['remark']))
{
	$remark = $_POST['remark'];
}
if (IsSet ($_POST['user_email']))
{
	$user_email = $_POST['user_email'];
}
if (IsSet ($_POST['code']))
{
	$code = $_POST['code'];
}

if (IsSet ($_POST['contribute_text']))
{
	if (!get_magic_quotes_gpc())
	{
		$contribute_text = addslashes($_POST['contribute_text']);
	}
	else
	{
		$contribute_text = $_POST['contribute_text'];
	}
}

function check_email($str)
{
	//returns 1 if valid email, 0 if not
	if (ereg("^.+@.+\\..+$", $str))
		return 1;
	else
		return 0;
}

function testipaddress($nametotest)
{
	$ipaddress = $nametotest;
	$ipaddress = gethostbyname($nametotest);
	if ($ipaddress == $nametotest)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

load_language($cmsdir . '/', 'sn_newsletter'); //load language file

$query = "SELECT dbtables FROM $table_modules WHERE location='sn_newsletter'"; // getting the table names
$result = mysql_query($query) or die_script($query, mysql_error());
$moduletables = mysql_result($result, 0);
unset ($query);
unset ($result);
$moduletables = explode(",", $moduletables);
$table_newsletter_content = "$moduletables[0]";
$table_newsletters = "$moduletables[1]";
$table_email_adresses = "$moduletables[2]";

//used for sending emails
$query = "SELECT email FROM $table_newsletters WHERE name='$pagetitle'"; // get email for newsletter
$result = mysql_query($query) or die_script($query, mysql_error());
$RecordCount = mysql_num_rows($result);
if ($RecordCount > 0)
{
	$email_owner = mysql_result($result, 0, 0);
}

$query1 = "SELECT title, register_text, extra_info FROM $table_newsletters WHERE name='$pagetitle'"; // do we have a newsletter with this pagename
$result1 = mysql_query($query1) or die_script($query1, mysql_error());
$title = mysql_result($result1, 0, 0);
$welcome_text = mysql_result($result1, 0, 1);
$extra_info = mysql_result($result1, 0, 2);

switch ($option) // start of switch
{
	case "delete_form" :
		print $title . "<p><b>" . $sas_lang['newsletter_module_unsubscribe'] . " " . $sas_lang['newsletter_module_newsletter'] . "</b><p>" . $sas_lang['newsletter_module_confirm_form'];
		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?section=$section&page=$page&option=confirm_delete&newsletter_id=$newsletter_id\">
			 <table border=\"0\" cellpadding=\"5\">";
		print "<tr><td><span class=text>" . $sas_lang['newsletter_confirmation_code'] . ": </span></td>
			 <td><input type=\"text\" name=\"code\" size=\"15\" class=\"textfield\"></td></tr>";
		print "</table>";
		print "<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"$sas_lang[send]\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";
		break;

	case "confirm_form" :
		print $title . "<p><b>" . $sas_lang['newsletter_module_subscribe'] . " " . $sas_lang['newsletter_module_newsletter'] . "</b><p>" . $sas_lang['newsletter_module_confirm_form'];
		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?section=$section&page=$page&option=confirm_registration&newsletter_id=$newsletter_id\">
			 <table border=\"0\" cellpadding=\"5\">";
		print "<tr><td><span class=text>" . $sas_lang['newsletter_confirmation_code'] . ": </span></td>
			 <td><input type=\"text\" name=\"code\" size=\"15\" class=\"textfield\"></td></tr>";
		print "</table>";
		print "<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"$sas_lang[send]\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";
		break;

	case "confirm_registration" :
		$query = "SELECT email, newsletter_id, enable, name, surename, remarks FROM $table_email_adresses WHERE code = '$code'"; // get the email for the confirmation
		$result = mysql_query($query) or die_script($query, mysql_error());
		$RecordCount = mysql_num_rows($result);
		if ($RecordCount > 0)
		{ //code is found
			$user_email = mysql_result($result, 0, 0);
			$newsletter_id = mysql_result($result, 0, 1);
			$enable = mysql_result($result, 0, 2);
			$user_name = mysql_result($result, 0, 3);
			$user_surename = mysql_result($result, 0, 4);
			$user_remark = mysql_result($result, 0, 5);
			unset ($query);
			unset ($result);
			$query = "SELECT name, confirm, email FROM $table_newsletters WHERE id='$newsletter_id'"; // get the newsletter name
			$result = mysql_query($query) or die_script($query, mysql_error());
			$newsletter_name = mysql_result($result, 0, 0);
			$confirm = mysql_result($result, 0, 1);
			$email_owner = mysql_result($result, 0, 2);
			unset ($query);
			unset ($result);
			if ($enable == 0)
			{
				mysql_query("UPDATE $table_email_adresses SET enable='1' WHERE code = '$code'");
			}
			if ($confirm == 0) //admin doen't have to confirm
			{ //set code to 2.
				mysql_query("UPDATE $table_email_adresses SET enable='2' WHERE code = '$code'");
			}
			if ($email_owner != "")
			{
				$msg = str_replace("%user_name%", $user_name, $sas_lang['newsletter_module_subscribe_msg3']);
				$msg = str_replace("%newsletter_name%", $pagetitle, $msg);
				$msg = str_replace("%user_surename%", $user_surename, $msg);
				$msg = str_replace("%user_email%", $user_email, $msg);
				$msg = str_replace("%user_remark%", $user_remark, $msg);
				require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
				include ("$cmsdir/modules/sn_newsletter/mail_function2.php"); //call the mail function
				$mail_list[0] = $email_owner;
				newsletter_mail($mail_list, $msg, $email_owner, $sas_lang['newsletter_module_email_subject2'] . $sitename);
				//mail("$email_owner", $sas_lang['newsletter_module_email_subject2'].$sitename, "$msg" , "$headers") ;
			}
			$sas_lang['newsletter_module_subscribe_msg2'] = str_replace("%user_email%", $user_email, $sas_lang['newsletter_module_subscribe_msg2']);
			print "<br>" . $msg;
		}
		else
		{ //invalid code
			print $sas_lang['newsletter_module_bad_code_msg'];
		}
		print "<p><form><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"parent.location='index.php?section=$section&page=$page'\" class=\"backbutton\"> 
		         </form>";
		break;

	case "confirm_delete" : // delete the email address from the table
		$newsletter_id_old = $newsletter_id; // assign old id          
		$query = "SELECT email, newsletter_id, name, surename, remarks FROM $table_email_adresses WHERE code='$code'"; // get the email for the delete
		$result = mysql_query($query) or die_script($query, mysql_error());
		$RecordCount = mysql_num_rows($result);
		if ($RecordCount > 0)
		{
			$user_email = mysql_result($result, 0, 0);
			$newsletter_id = mysql_result($result, 0, 1);
			$user_name = mysql_result($result, 0, 2);
			$user_surename = mysql_result($result, 0, 3);
			$user_remark = mysql_result($result, 0, 4);
			unset ($query);
			unset ($result); // delete the email address from the table

			if ($newsletter_id_old == $newsletter_id)
			{
				mysql_query("DELETE FROM $table_email_adresses WHERE code = '$code'");
			}
			else
			{
				$newsletter_id_old = explode(",", $newsletter_id_old);
				$result = -1;
				$result = array_search($newsletter_id, $newsletter_id_old); //does id exists
				unset ($newsletter_id_old[$result]); //delete this entry
				$newsletter_id_new = implode(",", $newsletter_id_old); //create comma delimited string
				mysql_query("UPDATE $table_email_adresses SET newsletter_id='$newsletter_id_new' WHERE email = '$user_email'");
			}
			$sas_lang['newsletter_module_unsubscribe_msg2'] = str_replace("%user_email%", $user_email, $sas_lang['newsletter_module_unsubscribe_msg2']);
			$sas_lang['newsletter_module_unsubscribe_msg2'] = str_replace("%newsletter_name%", $pagetitle, $sas_lang['newsletter_module_unsubscribe_msg2']);

			$query = "SELECT name, email FROM $table_newsletters WHERE id='$newsletter_id'"; // get the newsletter name, email owner
			$result = mysql_query($query) or die_script($query, mysql_error());
			$newsletter_name = mysql_result($result, 0, 0);
			$email_owner = mysql_result($result, 0, 1);
			unset ($query);
			unset ($result);

			if ($email_owner != "")
			{
				$msg = str_replace("%user_name%", $user_name, $sas_lang['newsletter_module_unsubscribe_msg3']);
				$msg = str_replace("%newsletter_name%", $pagetitle, $msg);
				$msg = str_replace("%user_surename%", $user_surename, $msg);
				$msg = str_replace("%user_email%", $user_email, $msg);
				$msg = str_replace("%user_remark%", $user_remark, $msg);
				require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
				include ("$cmsdir/modules/sn_newsletter/mail_function2.php"); //call the mail function
				$mail_list[0] = $email_owner;
				newsletter_mail($mail_list, $msg, $email_owner, $sas_lang['newsletter_module_email_subject1'] . $sitename);
			}

			print "<br>" . $sas_lang['newsletter_module_unsubscribe_msg2'];
		}
		else
		{ //invalid code
			print $sas_lang['newsletter_module_bad_code_msg'];
		}
		print "<p><form><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"parent.location='index.php?section=$section&page=$page';\" class=\"backbutton\"> 
		         </form>";
		break;

	case "send_registration" :
		$error = true;
		if ($fullname == "")
		{
			$msg = $sas_lang['newsletter_module_no_name'];
		}
		elseif ($user_email == "")
	{
			$msg = $sas_lang['newsletter_module_no_email'];
		}
		elseif (check_email($user_email) == 0)
	{
			$msg = str_replace("%user_email%", $user_email, $sas_lang['newsletter_module_novalid_email']);
		}
		else
		{
			$query = "SELECT newsletter_id FROM $table_email_adresses WHERE email = '$user_email'"; // check email address
			$result = mysql_query($query) or die_script($query, mysql_error());
			$RecordCount = mysql_num_rows($result);
			if ($RecordCount == 0)
			{
				$date1 = date('Y-m-d');
				$code = random_code(12); //get random delete code
				mysql_query("INSERT INTO $table_email_adresses (id, surename, name, email, remarks, newsletter_id, enable, date , code)
					VALUES (NULL, '$surename','$fullname', '$user_email', '$remark', '$newsletter_id', '0', '$date1', '$code')");
				$link = "<a href=$site_url/index.php?section=$section&page=$page&newsletter_id=$newsletter_id&option=confirm_registration&code=$code>" . $sas_lang['newsletter_module_click_here'] . "</a>";
				$link2 = "<b>$site_url/index.php?section=$section&page=$page&newsletter_id=$newsletter_id&option=confirm_form</b> <br>";
				$sas_lang['newsletter_module_subscribe_msg'] = str_replace("%user_email%", $user_email, $sas_lang['newsletter_module_subscribe_msg']);
				$msg = str_replace("%newsletter_name%", $pagetitle, $sas_lang['newsletter_module_confirm_msg2']);
				$msg = str_replace("%user_email%", $user_email, $msg) . $link . "<p>";
				$msg .= str_replace("%link%", $link2, $sas_lang['newsletter_module_confirm_msg1']); //add the second part of the message.
				$msg = str_replace("%code%", $code, $msg);
				$msg2 = $sas_lang['newsletter_module_subscribe_msg'];
				require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
				include ("$cmsdir/modules/sn_newsletter/mail_function2.php"); //call the mail function
				$mail_list[0] = $user_email;

				newsletter_mail($mail_list, $msg, $email_owner, $sas_lang['newsletter_module_email_subject2'] . $sitename);
			}
			else
			{ //email already exists
				$newsletter_id_old = mysql_result($result, 0); //get existing ids
				unset ($query);
				unset ($result);
				$newsletter_id_old = explode(",", $newsletter_id_old);

				if (in_array($newsletter_id, $newsletter_id_old)) //do we have the newsletter id already?
				{
					print $sas_lang['newsletter_module_subscriber_exists'];
				}
				else
				{
					array_push($newsletter_id_old, $newsletter_id); //if not add it
					sort($newsletter_id_old);
					$newsletter_id_new = implode(",", $newsletter_id_old); //create comma delimited string 
					mysql_query("UPDATE $table_email_adresses SET newsletter_id='$newsletter_id_new' WHERE email = '$user_email'");
					$msg2 = $sas_lang['newsletter_module_subscription_thank_you'];
				}
			}
			$error = FALSE;
		}
		if ($error)
		{
			print "<p><font color='red' face='Arial' size='3'><b>$msg</b></font>";
			print "<p><input type=\"button\" value=\"" . $sas_lang['newsletter_module_back_to_form'] . "\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" >";
		}
		else
		{
			print "<p>" . $msg2 . "<br>";
			print "<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"parent.location='index.php?section=$section&page=$page'\" class=\"backbutton\">";
		}
		break;

	case "send_decline" :
		$error = true;
		if ($user_email == "")
		{
			$msg = $sas_lang['newsletter_module_no_email'];
		}
		else
		{
			$query = "SELECT newsletter_id FROM $table_email_adresses WHERE email = '$user_email' AND newsletter_id LIKE '%$newsletter_id%'"; // check email address
			$result = mysql_query($query) or die_script($query, mysql_error());
			$RecordCount = mysql_num_rows($result);
			if ($RecordCount == 0)
			{
				$msg = $sas_lang['newsletter_module_no_email_intable'];
				//break;
			}
			else
			{
				$query1 = "SELECT id FROM $table_newsletters WHERE name='$pagetitle'"; // get newsletter id for this pagename
				$result1 = mysql_query($query1) or die_script($query1, mysql_error());
				$newsletter_id = mysql_result($result1, 0, 0);
				$code = random_code(12); //get random delete code
				mysql_query("UPDATE $table_email_adresses SET code='$code' WHERE email = '$user_email' "); //put code in table
				$link = "<a href=$site_url/index.php?section=$section&page=$page&newsletter_id=$newsletter_id&option=confirm_delete&code=$code>" . $sas_lang['newsletter_module_click_here'] . "</a>";
				$link2 = "<b>$site_url/index.php?section=$section&page=$page&newsletter_id=$newsletter_id&option=delete_form</b> <br>";
				$msg = str_replace("%user_email%", $user_email, $sas_lang['newsletter_module_confirm_msg']) . $link . "<p>";
				$msg .= str_replace("%link%", $link2, $sas_lang['newsletter_module_confirm_msg1']); //add the second part of the message.
				$msg = str_replace("%code%", $code, $msg);
				require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
				$msg = str_replace("%newsletter_name%", $pagetitle, $msg); //replace newsletter name
				include ("$cmsdir/modules/sn_newsletter/mail_function2.php"); //call the mail function
				$mail_list[0] = $user_email;
				newsletter_mail($mail_list, $msg, $email_owner, $sas_lang['newsletter_module_email_subject1'] . $sitename);
				//mail("$user_email", $sas_lang['newsletter_module_email_subject1'].$sitename, "$msg" , "$headers") ;
				$error = false;
			}
		}
		if ($error)
		{
			print "<p><b>$msg</b>";
			print "<p><input type=\"button\" value=\"" . $sas_lang['newsletter_module_back_to_form'] . "\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" >";
		}
		else
		{
			$sas_lang['newsletter_module_unsubscribe_msg'] = str_replace("%user_email%", $user_email, $sas_lang['newsletter_module_unsubscribe_msg']);
			print "<p>" . $sas_lang['newsletter_module_unsubscribe_msg'] . "<br>";
		}
		print "<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"parent.location='index.php?section=$section&page=$page'\" class=\"backbutton\">";

		break;

	case "send_contribution" :
		$error = true;
		if ($contribute_text == "")
		{
			$msg = $sas_lang['newsletter_module_no_text'];
		}
		elseif ($user_email == "")
	{
			$msg = $sas_lang['newsletter_module_no_email'];
		}
		else
		{
			$query = "SELECT newsletter_id, surename, name FROM $table_email_adresses WHERE email = '$user_email' AND enable = 2 AND newsletter_id LIKE '%$newsletter_id%'"; // check email address
			$result = mysql_query($query) or die_script($query, mysql_error());
			$RecordCount = mysql_num_rows($result);
			if ($RecordCount == 0)
			{
				$msg = $sas_lang['newsletter_module_no_email_intable'];
				//break;
			}
			else
			{
				$surname = mysql_result($result, 0, 1);
				$name = mysql_result($result, 0, 2); //email exists
				$query1 = "SELECT email, contribute_thanks, name FROM $table_newsletters WHERE name='$pagetitle'"; // get email for newsletter
				$result1 = mysql_query($query1) or die_script($query1, mysql_error());
				$email = mysql_result($result1, 0, 0);
				$contribute_thankyou = mysql_result($result1, 0, 1);
				$newsletter_name = mysql_result($result1, 0, 2);
				$message = $sas_lang['newsletter_module_contribution_msg'] . $surname . " " . $name . " :" . $user_email . ":<p>";
				$message .= stripslashes($contribute_text);
				$query = "SELECT id, content FROM $table_newsletter_content WHERE published = 0 AND archive = 0 AND newsletter_id = $newsletter_id ORDER BY date DESC"; // get the newsletter name
				$result = mysql_query($query) or die_script($query, mysql_error());
				$RecordCount = mysql_num_rows($result);
				$sas_lang['newsletter_module_contribution_sep'] = str_replace("%user_email%", $surname . " " . $name . " : " . $user_email, $sas_lang['newsletter_module_contribution_sep']);
				if ($RecordCount > 0) //we have an unpublished newsletter
				{
					$id = mysql_result($result, 0, 0);
					$content_old = mysql_result($result, 0, 1);
					$content_new = $content_old . $sas_lang['newsletter_module_contribution_sep'] . $contribute_text . $sas_lang['newsletter_module_contribution_sep'];
					mysql_query("UPDATE $table_newsletter_content SET content='$content_new' WHERE id='$id'"); //update the content

				}
				else //we have to create one.
					{
					$query = "SELECT header, footer, title FROM $table_newsletter_content WHERE newsletter_id='$newsletter_id' ORDER BY date DESC";
					$result = mysql_query($query) or die_script($query, mysql_error()); //get header and footer from previous one
					$RecordCount = mysql_num_rows($result);
					if ($RecordCount > 0)
					{
						$header = mysql_result($result, 0, 0);
						$footer = mysql_result($result, 0, 1);
						$title = mysql_result($result, 0, 2);
					}
					$content_new = addslashes($sas_lang['newsletter_module_contribution_sep'] . $contribute_text . $sas_lang['newsletter_module_contribution_sep']);
					$date1 = date('Y-m-d');
					mysql_query("INSERT INTO $table_newsletter_content (id, title, archive, content, published, newsletter_id, date, header, footer)
							VALUES (NULL, '$title', '0', '$content_new' , '0' ,'$newsletter_id','$date1', '$header', '$footer')");
				}

				if ($email != "")
				{
					include ("$cmsdir/modules/sn_newsletter/mail_function2.php"); //call the mail function
					$mail_list[0] = $email;
					// mail("$email","Bijdrage nieuwsbrief: ".$newsletter_name,"$message","$headers");   
				}
				$error = False;
			}
		}
		if ($error)
		{
			print "<p class=\"sas_error\"><b>$msg</b>
							<form  METHOD=\"POST\" action=\"index.php?section=$section&page=$page&option=contribute&newsletter_id=1\">
							<input type =\"hidden\" value=\"$contribute_text\" name=\"contribute_text\">
							<input type =\"hidden\" value=\"$user_email\" name=\"user_email\">";
			print "<p><input type=\"submit\" value=\"" . $sas_lang['newsletter_module_back_to_form'] . "\"></form>";
		}
		else
		{
			print "<p><b>" . $contribute_thankyou . "</b><br>";
			print "<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"parent.location='index.php?section=$section&page=$page'\" class=\"backbutton\">";

		}
		break;

	case "register" :
		$query1 = "SELECT title, register_text, extra_info FROM $table_newsletters WHERE name='$pagetitle'"; // do we have a newsletter with this pagename
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		$title = mysql_result($result1, 0, 0);
		$welcome_text = mysql_result($result1, 0, 1);
		$extra_info = mysql_result($result1, 0, 2);
		print $title . "<p><b>$welcome_text</b><p>";
		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?section=$section&page=$page&option=send_registration&newsletter_id=$newsletter_id\">
			 <table border=\"0\" cellpadding=\"5\">";
		print "<tr><td><span class=text>" . $sas_lang['newsletter_module_surename'] . ": </span></td>
			 <td><input type=\"text\" name=\"surename\" size=\"40\" class=\"textfield\"></td></tr>";
		print "<tr><td><span class=text>" . $sas_lang['newsletter_module_name'] . ": (*)</span></td>
			 <td><input type=\"text\" name=\"fullname\" size=\"40\" class=\"textfield\"></td></tr>";
		print "<tr><td><span class=text>" . $sas_lang['newsletter_module_email'] . ": (*)</span></td>
			 <td><input type=\"text\" name=\"user_email\" size=\"40\" class=\"textfield\"></td></tr>";
		print "<tr><td><span class=text>$extra_info :</span></td>
			 <td><input type=\"text\" name=\"remark\" size=\"40\" class=\"textfield\"></td></tr>";
		print "<tr><td>&nbsp;</td><td><span class=text>" . $sas_lang['newsletter_module_required_fields'] . "</td></tr>";
		print "</table>";
		print "<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"$sas_lang[send]\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";
		break;

	case "decline" :
		$query1 = "SELECT title, decline_text FROM $table_newsletters WHERE name='$pagetitle'"; // do we have a newsletter with this pagename
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		$title = mysql_result($result1, 0, 0);
		$welcome_text = mysql_result($result1, 0, 1);
		print $title . "<p><b>$welcome_text</b><p>";
		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?section=$section&page=$page&option=send_decline&newsletter_id=$newsletter_id\">
			 <table border=\"0\" cellpadding=\"5\">";
		print "<tr><td><span class=text>" . $sas_lang['newsletter_module_email'] . ": (*)</span></td>
			 <td><input type=\"text\" name=\"user_email\" size=\"40\" class=\"textfield\"></td></tr>";
		print "<tr><td>&nbsp;</td><td><span class=text>" . $sas_lang['required_fields'] . "</td></tr>";
		print "</table>";
		print "\n<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"$sas_lang[send]\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";
		break;

	case "contribute" :
		$query1 = "SELECT title, contribute_text FROM $table_newsletters WHERE name='$pagetitle'"; // do we have a newsletter with this pagename
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		$title = mysql_result($result1, 0, 0);
		$welcome_text = mysql_result($result1, 0, 1);
		print $title . "<p>$welcome_text<p>";
		print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?section=$section&page=$page&option=send_contribution&newsletter_id=$newsletter_id\">
			 <table border=\"0\" cellpadding=\"5\">";
		print "<tr><td><span class=text>" . $sas_lang['newsletter_module_email'] . ": (*)</span></td>
			 <td><input type=\"text\" name=\"user_email\" size=\"40\" value=\"$user_email\" class=\"textfield\"></td></tr>";
		print "<tr><td><span class=text>" . $sas_lang['text'] . ": (*)</span></td>
			 <td><textarea name=\"contribute_text\" ROWS=20 COLS=55>$contribute_text</textarea></td></tr>";
		print "<tr><td>&nbsp;</td><td><span class=text>" . $sas_lang['newsletter_module_required_fields'] . "</td></tr>";
		print "</table>";
		print "\n<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
		print "&nbsp;<input type=\"submit\" value=\"$sas_lang[send]\" name=\"savebutton\" class=\"savebutton\">";
		print "</form>";
		break;

	case "view" :
		$query = "SELECT header,content,footer,title FROM $table_newsletter_content WHERE id = '$newsletter_id'"; // getting the newsletter content
		$result = mysql_query($query) or die_script($query, mysql_error());
		$header = mysql_result($result, 0, 0);
		$content = mysql_result($result, 0, 1);
		$footer = mysql_result($result, 0, 2);
		$newsletter_title = mysql_result($result, 0, 3);
		unset ($query);
		unset ($result);
		print $header . $content . $footer;
		//print "<b><i>".$newsletter_title."</i></b><p>".$header.$content.$footer;  
		print "\n<br><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" class=\"backbutton\">"; //show the content of newsletter
		break;

	case "list" :

		$query1 = "SELECT id, title, contribute FROM $table_newsletters WHERE name='$pagetitle'"; // do we have a newsletter with this pagename
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		$RecordCount = mysql_num_rows($result1);
		if ($RecordCount == 1)
		{
			$category_id = mysql_result($result1, 0, 0);
			$title = mysql_result($result1, 0, 1);
			$contribute = mysql_result($result1, 0, 2);
			unset ($query1);
			unset ($result1);
			print "<b>$title</b> <p>";
			$query3 = "SELECT id, title FROM $table_newsletter_content WHERE newsletter_id='$category_id' AND archive='0' AND published='1' ORDER BY date DESC";
			$result3 = mysql_query($query3) or die_script($query3, mysql_error());
			print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">";
			print "<tr><td align=\"right\">
				<input type=\"button\" value=\"" . $sas_lang['newsletter_module_subscribe'] . "\" onClick=\"javascript:parent.location='index.php?section=$section&page=$page&option=register&newsletter_id=$category_id'\">
				&nbsp;&nbsp;<input type=\"button\" value=\"" . $sas_lang['newsletter_module_unsubscribe'] . "\" onClick=\"javascript:parent.location='index.php?section=$section&page=$page&option=decline&newsletter_id=$category_id'\">";
			if ($contribute == 1)
			{
				print "&nbsp;&nbsp;<input type=\"button\" value=\"" . $sas_lang['newsletter_module_contribute'] . "\" onClick=\"javascript:parent.location='index.php?section=$section&page=$page&option=contribute&newsletter_id=$category_id'\">";
			}
			print "&nbsp;&nbsp;<input type=\"button\" value=\"" . $sas_lang['newsletter_module_archive'] . "\" onClick=\"javascript:parent.location='index.php?section=$section&page=$page&option=archive&newsletter_id=$category_id'\">";
			print "</td></tr>";
			while ($newsletter_list = mysql_fetch_array($result3))
			{
				print "<tr>
							   <td><a href=\"index.php?section=$section&page=$page&option=view&newsletter_id=$newsletter_list[0]\"><b>$newsletter_list[1]</b></a></td>
							   </tr>";
			}
			print "</table><p>";
		}
		else
		{
			print $sas_lang['newsletter_module_page_error'];
		}
		break;

	case "archive" :
		$query1 = "SELECT id, title, contribute FROM $table_newsletters WHERE name='$pagetitle'"; // do we have a newsletter with this pagename
		$result1 = mysql_query($query1) or die_script($query1, mysql_error());
		$RecordCount = mysql_num_rows($result1);
		if ($RecordCount == 1)
		{
			$category_id = mysql_result($result1, 0, 0);
			$title = mysql_result($result1, 0, 1);
			$contribute = mysql_result($result1, 0, 2);
			unset ($query1);
			unset ($result1);
			print "<b>" . $sas_lang['newsletter_module_archive'] . ":</b><p><b>$title</b><br>";
			$query3 = "SELECT id, title FROM $table_newsletter_content WHERE newsletter_id='$category_id' AND archive='1' AND published='1' ORDER BY date DESC";
			$result3 = mysql_query($query3) or die_script($query3, mysql_error());
			print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">";
			print "<tr><td align=\"right\">";
			print "<input type=\"button\" value=\"" . $sas_lang['newsletter_module_newsletters'] . "\" onClick=\"javascript:parent.location='index.php?section=$section&page=$page&newsletter_id=$category_id'\">";
			print "</td></tr>";
			while ($newsletter_list = mysql_fetch_array($result3))
			{
				print "<tr>
				               <td><a href=\"index.php?section=$section&page=$page&option=view&newsletter_id=$newsletter_list[0]\"><b>$newsletter_list[1]</b></a></td>
				               </tr>";
			}
			print "</table><p>";
		}
		else
		{
			print $sas_lang['newsletter_module_page_error'];
		}
		break;
}
?>