<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

// +----------------------------------------------------------------------+

// | This source file is a part of iScripts.com Visual Caster v1.0        |

// +----------------------------------------------------------------------+

// | Copyrights Armia Systems, Inc and iScripts.com © 2007                |

// | 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.                                                       |

// +----------------------------------------------------------------------+

header("Cache-Control: no-cache");

header("Pragma: nocache");

include_once("includes/config.php");// get the db connection info

include "includes/session.php";







//getting the values

$vote_sent = preg_replace("/[^0-9]/","",$_REQUEST['j']);

$id_sent = preg_replace("/[^0-9a-zA-Z]/","",$_REQUEST['q']);

$ip_num = preg_replace("/[^0-9\.]/","",$_REQUEST['t']);

$units = preg_replace("/[^0-9]/","",$_REQUEST['c']);

$ip = $_SERVER['REMOTE_ADDR'];

$rating_unitwidth     = 30;

$video_rating_id = preg_replace("/[^0-9a-zA-Z]/","",$_REQUEST['rating']);

$type=$_REQUEST['type'];


if ($vote_sent > $units) die("Sorry, vote appears to be invalid."); // kill the script because normal users will never see this.



$select_rating = "SELECT ntotal_ratings FROM ".$tableprefix."record_ratings WHERE nrseller_id = '$id_sent' AND vType='".$type."'
												AND nruser_id = ".$_SESSION["sess_userid"];



$result_rating = mysql_query($select_rating);



if(mysql_num_rows($result_rating) == 0)

{



	$total_count = 1;

	$record_rating = "INSERT INTO ".$tableprefix."record_ratings(nrating_id,nruser_id,nrseller_id,ntotal_ratings,vType)

				      VALUES(".$video_rating_id.",".$_SESSION["sess_userid"].",".$id_sent.",1,'".$type."')";

					  

	//echo $record_rating; 				  

	

}

/*else

{



	while($rating_row = mysql_fetch_array($result_rating))

	{

	

		$total_count = $total_count + 1;

		$record_rating = "UPDATE ".$tableprefix."record_ratings SET ntotal_ratings = ntotal_ratings + ".$total_count." WHERE nrproduct_id = '$id_sent' AND nruser_id = ".$_SESSION['SESS_userid'];

		//$result = mysql_query($update);	

		

	}



}*/



mysql_query($record_rating) or die(mysql_error());



$select_rating_details = mysql_query("SELECT nrating_id FROM ".$tableprefix."record_ratings WHERE nrseller_id  = '$id_sent' 
											AND vType='".$type."' AND nruser_id = ".$_SESSION["sess_userid"]);



//echo "SELECT nrating_id FROM ".$tableprefix."record_ratings WHERE nrproduct_id = '$id_sent' AND nruser_id = ".$_SESSION['SESS_userid'];



$result_rating_details = mysql_fetch_assoc($select_rating_details);



$select_rating_count = mysql_query("SELECT ROUND(SUM(nrating_id)/COUNT(nrating_id)) AS average_rating FROM ".$tableprefix."record_ratings WHERE 
										vType='".$type."' AND nrseller_id = ".$id_sent);

$rating_count = mysql_fetch_assoc($select_rating_count);



// $new_back is what gets 'drawn' on your page after a successful 'AJAX/Javascript' vote



$rater.='>( Average Rating :'.$rating_type['vrating'].')';



$new_back = array();



$new_back[] .= '<ul class="unit-rating" style="width:'.$units*$rating_unitwidth.'px;">';

$new_back[] .= '<li class="current-rating" style="width:'.$rating_width = @number_format($result_rating_details['nrating_id'])*$rating_unitwidth.'px;">Current rating.</li>';

/*$new_back[] .= '<li class="r1-unit">1</li>';

$new_back[] .= '<li class="r2-unit">2</li>';

$new_back[] .= '<li class="r3-unit">3</li>';

$new_back[] .= '<li class="r4-unit">4</li>';

$new_back[] .= '<li class="r5-unit">5</li>';

$new_back[] .= '<li class="r6-unit">6</li>';

$new_back[] .= '<li class="r7-unit">7</li>';

$new_back[] .= '<li class="r8-unit">8</li>';

$new_back[] .= '<li class="r9-unit">9</li>';

$new_back[] .= '<li class="r10-unit">10</li>';*/

$new_back[] .= '</ul>';

$new_back[] .= '<p class="voted">( Average Rating :'.$rating_type['vrating'].' )' ;

$new_back[] .= '<span class="thanks">Thanks for rating!</span></p>';



$allnewback = join("\n", $new_back);



// ========================



//name of the div id to be updated | the html that needs to be changed

$output = "unit_long$id_sent|$allnewback";

echo $output;

?>