<?php
include_once("../../corebot/_config_.php");
include_once("../../corebot/errorHandler.php");

$conf = new config();

// get previous all thumb votes
function getAllVotes($id)  
 {  
 /** 
 Returns an array whose first element is votes_up and the second one is votes_down 
 **/  
 $votes = array();  
 $q = "SELECT uithm_upb, uithm_dwnb FROM copn_cpnbnks WHERE _frgn_ky = '".$id."'";  
 $r = mysql_query($q);  
 if(mysql_num_rows($r)==1)//id found in the table  
  {  
  $row = mysql_fetch_array($r);  
  $votes[0] = $row['uithm_upb'];  
  $votes[1] = $row['uithm_dwnb'];  
  }  
 return $votes;  
 }  
 
//// calculate thumb vote
function getEffectiveVotes($id)  
 {  
 /** 
 Returns an integer 
 **/  
 $votes = getAllVotes($id);  
 $effectiveVote = $votes[0] - $votes[1];  
 return $effectiveVote;  
 }
 
/// get input
$id = $_POST['id'];  
$action = $_POST['action']; 
//get the current votes  
$cur_votes = getAllVotes($id);  
  
//ok, now update the votes  
  
if($action=='vote_up') //voting up  
{  
 $votes_up = $cur_votes[0]+1;  
 $q = "UPDATE copn_cpnbnks SET uithm_upb = '".$votes_up."' WHERE _frgn_ky = '".$id."'";  
}  
elseif($action=='vote_down') //voting down  
{  
 $votes_down = $cur_votes[1]+1;  
 $q = "UPDATE copn_cpnbnks SET uithm_dwnb = '".$votes_down."' WHERE _frgn_ky = '".$id."'";  
}  

$r = mysql_query($q);  
if($r) //voting done  
 {  
 $effectiveVote = getEffectiveVotes($id);  
 //echo $effectiveVote." Votes"; 
 echo "<span style='font-size:12px; font-size:normal; color:#e5e5e5;'>Thanks</span>"; 
 }  
elseif(!$r) //voting failed  
 {  
 echo "Failed!";  
 }  

?>