<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";
include "includes/htmltop.php";

if($_SESSION["sess_userid"]!="")
{
	$pid = ($_GET['pid']!='')?$_GET['pid']:$_POST['pid'];

	//checking already review exists or not
	$psql=mysql_query("select vDes from ".$tableprefix."Review where nUserId='".$_SESSION["sess_userid"]."' and nProdId='".$pid."'") or die(mysql_error());
	if(mysql_num_rows($psql)>0)
	{
		$txtReview=mysql_result($psql,0,'vDes');
		$btnVal='Edit';
	}//end if
	else
	{
		$btnVal='Add';
	}//end else
	
	   $sql_2 = "SELECT vReview FROM ".$tableprefix."settings";
		$res_2 = mysql_query($sql_2);
		if(mysql_num_rows($res_2)!= 0){
			$rw_2 = mysql_fetch_array($res_2);
			$reviewapproval=$rw_2["vReview"];
		}
		
	
	
	//insert review
	if(isset($_POST['btnGo']) && $_POST['btnGoVal']=='Add Review')
	{
		switch(getSettingsValue('vReview'))
		{
			case "automatic":
				$aActive='1';
			break;
			
			default:
				$aActive='0';
			break;
		}//end switch
		
		mysql_query("insert into ".$tableprefix."Review (nUserId,nProdId,vDes,vActive) values ('".$_SESSION["sess_userid"]."',
						'".$_POST["pid"]."','".$_POST["txtReview"]."','".$aActive."')") or die(mysql_error());
		$msg='Review added successfully';
		
		if($reviewapproval=="admin" || $reviewapproval=="vendors")
		{
		/************* Get Product details *************/
		$p_sql  = "select * from ".$tableprefix."products where product_id = \"".$_POST["pid"]."\"";
		$p_rs   = mysql_query($p_sql) or die(mysql_error());
		$p_row	= mysql_fetch_array($p_rs);
		
		/************* Get email of the seller ***********************/
		if($reviewapproval=="vendors")
		{
		  $seller_sql  = "select email from ".$tableprefix."artists where artist_id = ".$p_row["product_artist_id"];
		  $seller_rs   = mysql_query($seller_sql);
		  $seller_rw   = mysql_fetch_array($seller_rs);
		  $seller_email= $seller_rw["email"];
		}
		/***************** End fetching product details **********/
		// Send mail to admin
			
		/*---------Multicart 2.0 Upgradation------------*/		
		$subject = "New review is posted in ".stripslashes(SITE_NAME);
		$mailcontent = readEmailTemplate('product_review');

		if($reviewapproval=="admin")
		$to      = SITE_EMAIL;
		elseif($reviewapproval=="vendors")
		{
		 $to = $seller_email;
		 $mailcontent = str_replace('Administrator',"Seller",$mailcontent);	
		}
		
		$link    = "<a href='".SITE_URL."/productdetails.php?productid=".$_POST["pid"]."' target='_blank'>Click here</a>";
		
		/*-------Email Template Reading Fuction Call--------*/				
				
				
		$mailcontent = str_replace('[site_name]',SITE_NAME,$mailcontent);					
		$mailcontent = str_replace('[product_name]',$p_row["product_name"],$mailcontent);		
		$mailcontent = str_replace('[link]',$link,$mailcontent);			
		
		/*--------------- End email template section-------------------*/			
		
		/******* Send Email ************************************************/
		
		$headers  = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Reply-To: ".SITE_NAME."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Return-Path: ".SITE_NAME."<noreply@".SITE_EMAIL.">" . "\r\n"; 
		//$headers .= "Organization: ".SITE_NAME."\r\n"; 
		
		
		$mailcontent = nl2br($mailcontent);
				

		//$mail_send_details = "<html><head><link href='".SITE_URL."/style/template1-admin.css' rel='stylesheet' type='text/css'></head><body><center><div align='left' id='layout'><div align='left' class='header'><div align='left' class='logo'><img src=".$logofolder."/".$logoimage." width='125' height='39'/></div></div><div>".$mailcontent."</div></div><div align='left' class='clear_float'><img src=".$imagefolder."/clear.gif width=1 height=1/></div></div></div></center></body></html>";
		$mail_send_details = '<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Online reservation</title>
  <link href="./themes/'. $active_template[4].' rel="stylesheet" type="text/css">
</head>
<body>

<table width="103%" cellspacing="10" cellpadding="0" bgcolor="#EAEAEA">
   <tr>
     <td align="center" valign="top">
         
         <table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#EAEAEA" bgcolor="#FFFFFF" class="manage_table">
            <tr>
               <td align="right" >
              </td>
            </tr>
            <tr>
               <td valign="middle" align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                 <tr>
                   <td  bgcolor="#EAEAEA" > <img src='.SITE_URL.'/banners/'.getSettingsValue('site_logo').' width="125" height="60"/></td>
                 </tr>
               </table></td>
            </tr>
            <tr>
              <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td align="left"><p>'.$mailcontent.'</p>
                  </td>
                </tr>
              </table></td>
            </tr>
            <tr>
               <td align="center" bgcolor="#EAEAEA" class="footer">
                  <p>&nbsp;</p>
              </td>
            </tr>
       </table>

     </td>
   </tr>
</table>


</body>
</html>';
		
		$mailsent = mail($to, $subject, $mail_send_details, $headers);

/*********************** End email section ***********************************/
		}
	
	}//end if
	
	//edit review
	if(isset($_POST['btnGo']) && $_POST['btnGoVal']=='Edit Review')
	{
		switch(getSettingsValue('vReview'))
		{
			case "automatic":
				$aActive='1';
			break;
			
			default:
				$aActive='0';
			break;
		}//end switch
		
		mysql_query("update ".$tableprefix."Review set vDes='".$_POST["txtReview"]."',vActive='".$aActive."'
								where nUserId='".$_SESSION["sess_userid"]."' and nProdId='".$_POST["pid"]."'") or die(mysql_error());
		$msg= REVIEW_EDIT_SUCCESS_TEXT;
		
		
	}//end if
?>		
		
		<style>
		.button{
				 color:#FFFFFF;
				 font-family:Tahoma,Arial,Verdana;
				 font-size:11px;
				 font-weight:600;
				 background-color:#98C932;;
				 border-width:2px;
				 border-style:outset;
				 border-color:#98C932;
				 height:25px;
				 vertical-align:middle;
				}
		</style>
	<script language="javascript">
	function validate()
	{
		var txtreview=document.frmReview.txtReview.value;
		if(txtreview=='')
		{
			alert('<?php echo ENTER_REVIEW_TEXT;?>');
			return false;
		}
		else
		{
			return true;
		}
	}
	</script>
		<form action="" method="post" name="frmReview">
		<input type="hidden" name="pid" value="<?php echo $pid;?>" />
		<table cellpadding="0" cellspacing="0" border="0" width="100%" class="innertextCopy1">
		<tr>
			<td><b><?php echo PRODUCT_REVIEW_TEXT;?></b></td>
		</tr>
		<?php
			if(isset($msg) && $msg!='')
			{
		?>
		<tr>
		  <td>&nbsp;</td>
		  </tr>
		<tr>
		  <td><font color="#FF0000"><?php echo $msg;?></font></td>
		  </tr>
		<tr>
		  <td>&nbsp;</td>
		  </tr>
		  <?php }//end if
		  else
		  {
		?>
		<tr>
		  <td><textarea name="txtReview" cols="45" rows="8" class="textbox"><?php echo $txtReview;?></textarea></td>
		  </tr>
		<tr>
		  <td>&nbsp;</td>
		  </tr>
		<tr>
		  <td>
		  <input type="hidden" name="btnGoVal" value="<?php echo $btnVal.' Review';?>" />
		  <input type="submit" name="btnGo" value="<?php if ($btnVal == 'Edit') {echo EDIT_REVIEW_TEXT;} else {echo ADD_REVIEW_TEXT;} ?>" class="button" onClick="return validate();"/>
		  </td>
		  </tr>
		  <?php
		  	}//end else
		?>
		<tr>
		  <td align="center"><input type="button" class="button" name="close" value="<?php echo CLOSE_BUTTON;?>" onClick="Javascript:window.close();"></td>
		  </tr>
		</table>
		</form>
<?php		
}//end if
?>