<html>
<head>
<title>
<? 

echo"Email A Friend  About Product";?></title>
	  <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
	  <link rel='stylesheet' href='../style.css' type='text/css'>
</head>
<body>
<?
/***************************************************************************
 *			email-me.php	                                   *
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~	                   *
 *   File Info:		  Email a friend                                   *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
***************************************************************************/
include("./config.php");
include("./tests.php");

$pid = getenv("QUERY_STRING");
$done = "NO";
if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $act= addslashes($_POST["act"]);
        $yourname = addslashes($_POST["yourname"]);
        $youremail = addslashes($_POST["youremail"]);
        $frndname = addslashes($_POST["frndname"]);
       $frndemail = addslashes($_POST["frndemail"]);
       $pids = addslashes($_POST["pid"]);
}

$table= "stocks";
$conn = @mysql_connect($server,$user,$password)
	or die("Cannot connect to database");
$rs = @mysql_select_db($database,$conn) 
	or die("Unable to view product image");
if (empty($pids))
	$pid = getenv("QUERY_STRING");
else
	$pid = $pids;
$sql = "SELECT * from $table where pid = '$pid'";
$result = mysql_query($sql,$conn)
	or die("Cannot execute SQL query");
$nrow = 0;
while ($row = mysql_fetch_array($result))
{
	$pidme = $row["pid"];
	if ($pidme == $pid)
	{
		$images = $row["spic"];
		$scat = $row["scat"];
		$item = $row["sitem"];
		$stock = $row["stock"];
		$nrow = 1;
	}
}

if ($nrow == 0)
{
	if ($scat == NULL)
	$scat = "n/a";
	if ($item == NULL)
	$item = "n/a";
	if ($stock == 0 or $stock == NULL)
	$stock = "n/a";
	$images = "/images/nopic.jpg";
}
if (strlen($images) >= 0) {
echo "<table align='center' width='100%'><tr><td align='center' valign='middle'><img src=$images><p>$image</p><p><a href = javascript:window.close('acePopup')>Close Window</a></p></td></tr></table>";
echo("<center><br>Item: $item<br>Category: $scat<br>Stock: $stock<br></center>");

# code for validating email addresses etc

if ($act == "email")
{
	if ($yourname == NULL and $youremail == NULL and $frndname == NULL and $frndemail == NULL)
	{
		echo "<hr><br><center><font face = verdana color = $highlight>Opps...data fields must contain data...please try again!!!</font></center>";
		echo "<br><center><a href=javascript:history.back()><b>Click to go back</b></a></center>";
		exit();
	}
	if ($yourname != NULL && $youremail != NULL && $frndname != NULL && $frndemail != NULL)
	{

	}
	else
	{
		echo "<hr><br><center><font face = verdana color = $highlight>Opps...data fields must contain data...please try again!!!</font></center>";
		echo "<br><center><a href=javascript:history.back()><b>Click to go back</b></a></center>";
		exit();
	}
	$bad = isBadEmail($youremail);
	if ($bad)
	{
		echo "<hr><br><center><font face = verdana color = $highlight>Opps...Your email field should contain valid characters a-z, A-Z, 0-9,'@' !!!</font></center>";
		echo "<br><center><a href=javascript:history.back()><b>Click to go back</b></a></center>";
		exit();
	}
	$bad1 = isBadEmail($frndemail);
	if ($bad1)
	{
		echo "<hr><br><center><font face = verdana color = $highlight>Opps...Your friend's email field should contain valid characters a-z, A-Z, 0-9,'@' !!!</font></center>";
		echo "<br><center><a href=javascript:history.back()><b>Click to go back</b></a></center>";
		exit();
	}

	# do the sending

		$from = "$yourname <$youremaill>";
		$to = "$frndname <$frndemail>";
		$extraheader = "From: $yourname<$youremail>\r\n";
    		$extraheader.="Reply-To: $youremail\r\n";
    		$extraheader.="X-Mailer: $yourname [$youremail]";			
		$send= "<center><img src =./images/email.gif>Sent email to '$to' completed.....</center>";
		echo "<hr>";
		echo($send);
		if ($usedata == "YES")
			$mail = "Hi\n\nI found a great product at $sitecompany I like to share with you.\n\nTo view the product, go to main site using this url link. Look for product '$item' or \n$prod_url$pid\n$site_url.\n\nBest regards.";
		else
			$mail = "Hi\n\nI found a great product at $site_company I like to share with you.\n\nTo view the product, go to main site using this url link. Look for product '$item'  or \n$prod_url$pid\n$site_url.\n\nBest regards.";
	
		@mail($to,$subject,$mail,$extraheader);	
		echo("<br><center><a href = javascript:window.close('acePopup')>Close Window</a></center>");

}

# code and function for emailing a friend #

if ($act == NULL)
{
	echo "<hr><br><center><h2><font face = verdana color = $color>Email a friend about this product</font></h2></center><br>";
	$form.="<form action = email-me.php method = post>";
	$form.="<input type = hidden name = act value = 'email'>";
	$form.="<input type = hidden name = pid value = $pid>";
	$form.= "<table width=90% cellpadding=2 cellspacing=0 border=0 bgcolor=$backcolor align = center>";
	$form.="<tr><td>Your name:</td><td><input type = text name =yourname maxlength = 30></td></tr>";
	$form.="<tr><td>Your email:</td><td><input type = text name =youremail size = 35 maxlength = 50></td></tr>";
	$form.="<tr><td>Friend's name:</td><td><input type = text name = frndname maxlength = 30></td></tr>";
	$form.="<tr><td>Friend's email:</td><td><input type = text name = frndemail size = 35 maxlength = 50></td></tr>";
	$form.="<tr><td>Subject:</td></td><td><input type = text name = subject value = 'Great product at $site_company!!!' size = 35 maxlength = 50 readonly></td></tr>";
	$form.="<tr><td><input type = submit value = 'Email my friend now'></td><td><input type = reset value = 'Clear contents'>";
	$form.="</table></form>";
	echo $form;
	if ($usedata == "YES")
		$display = "<b>Message content of email:</b><br>Hi,<br><br>I found a great product at $sitecompany I like to share with you.<br><br>To view the product, go to main web site using this url link.<br><a href = $site_url>$site_url.</a><br><br>Best regards.<br><br>";
	else
		$display = "<b>Message content of email:</b><br>Hi,<br><br>I found a great product at $site_company I like to share with you.<br><br>To view the product, go to main web site using this url link.<br><a href = $site_url>$site_url.</a><br><br>Best regards.<br><br>";
	
	echo $display;
	echo "<br><br><hr>";
}

}
 function isBadEmail($str)
 {
	$atsign = 0;
	$dot = 0;
	$x = 0;
      if(strlen($str)>=4&&strlen($str)<=50){
      $valid_chars = " 0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.@";
      for($i=0;$i<strlen($str);$i++){
        if(strchr($valid_chars,substr($str,$i,1))==false) $x++;
      }
	 for($i=0;$i<strlen($str);$i++){
        if(strchr("@",substr($str,$i,1))==true) $atsign++;
      }
 	for($i=0;$i<strlen($str);$i++){
        if(strchr(".",substr($str,$i,1))==true) $dot++;
      }
       if ($atsign == 0) return true;
      if($x>0 and $atsign == 1 and $dot >= 1) return true;
    }else{
      return true;
    }
}
?>

</body>
</html>