<?php
include("includes/site_info.php");
include('session_handler.inc');
include_once __INCLUDE_CLASS_PATH."/class.Picture.php";
	
	switch ($_REQUEST["Submit"]) {
	case "addMarketComment"; 
		addMarketComment();
		break;
	case "deleteMarketComment"; 
		deleteMarketComment();
		break;
	default; 
		echo "";
		break;
	}

function addMarketComment() {
global $SITE_MAIN_WWW_ROOT, $CONST_LINK_ROOT;
$picture = new Picture();

	$comment = htmlspecialchars($_REQUEST["comment"], ENT_QUOTES);
	$postID = $_REQUEST["postID"];
	$userID = $_REQUEST["userID"];
	$userName = $_REQUEST["userName"];
	
	$sql="Insert into market_Post_comment set postID='$postID', com_memid='$userID', comment='$comment', com_mem_username='$userName', com_approved='1'";
	$squery=mysql_query($sql);
	$commentID=mysql_insert_id();
	
	$short_msg_text=strip_tags(stripslashes(substr($comment,0,100)));
	if (strlen (strip_tags($comment))>100) $short_msg_text.="...";
	
	$POST_query=mysql_query("SELECT case when char_length(title) > 100 then concat(LEFT(title,100), '...') else title end as title, mem_username, mem_userid, mem_profileid FROM market_Post LEFT JOIN members USING (mem_userid) WHERE postID = '$postID'") or die(mysql_error());
	$POST = mysql_fetch_object($POST_query);
	
	if ($POST->mem_profileid != null) { $male_name= $POST->mem_profileid; }
	else { $male_name= $POST->mem_username; }
	
	$activityText = "say, \"$short_msg_text\" on <a href=\"$SITE_MAIN_WWW_ROOT/$male_name\">".ucwords($POST->mem_username)."</a>\'s post <a href=\"$CONST_LINK_ROOT/home.php#mshowPost?tab=categories&postID=$postID&uID=$POST->mem_userid\" >".ucwords($POST->title)."</a>.";
	
	$query2="INSERT INTO activities_user (userID, activityID, activityText) values ('$userID', '16', '$activityText')";
	$result2=mysql_query($query2) or die(mysql_error());
	
	if($userID!=$POST->mem_userid){
	$current_query=mysql_query("SELECT mem_username, mem_userid, mem_profileid FROM members WHERE mem_userid = '$userID'") or die(mysql_error());
	$current = mysql_fetch_object($current_query);
	
	if ($current->mem_profileid != null) { $male_name2= $current->mem_profileid; }
	else { $male_name2= $current->mem_username; }
	
	$activityText2 = "got a message from <a href=\"$SITE_MAIN_WWW_ROOT/$male_name2\">".ucwords($current->mem_username)."</a>\ for post <a href=\"$CONST_LINK_ROOT/MKT$postID\" >".ucwords($POST->title)."</a>.";
	
	$query22="INSERT INTO activities_user (userID, activityID, activityText) values ('$POST->mem_userid', '16', '$activityText2')";
	$result22=mysql_query($query22) or die(mysql_error());
	}
	
	if (!$squery) {
		echo "Database error!";
		die;
	} else {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$userID' and pic_default='Y'") or die(mysql_error()); 
$pics_count = mysql_num_rows($photo_query);
$aPicture = mysql_fetch_object($photo_query);
if ($pics_count > 0) {
	$medium = $aPicture->pic_picture."_ps.".$aPicture->ext;
	$male1_picture1 = "$SITE_MAIN_WWW_ROOT/members/$userName/picture/$medium";
             }else{
$male1_picture1=$CONST_LINK_ROOT."/".$picture->GetProfilePicture($userID);
}
	?>
						<div id="commentBox<?=$commentID?>" class="comment_reply"><div class="left w65_h58"><a href="<?=$CONST_LINK_ROOT?>/<?=$male1_name1?>" <?php /*?>onclick="javascript: callprofileInfo(<?=$userID?>);"<?php */?>><img src="<?=$male1_picture1?>" border="0" title="Name:&nbsp;<?=ucwords($userName)?>" class="img_b_p3_white"/></a></div><div class="comment_reply_text"><div class="delete"><a href="javascript:;" onclick="javascript: deleteComment(<?=$commentID?>, <?=$postID?>);">x</a></div><span id="contentBox<?=$commentID?>"><?php 
				$short_msg_text=strip_tags(stripslashes(substr($comment,0,300)));
				echo $short_msg_text;
				if (strlen (strip_tags($comment))>300) echo "<span id=\"more".$commentID."\">... <a href=\"javascript:;\" onclick=\"javascript: showmore(".$commentID.");\">more</a></span><span id=\"Seemore".$commentID."\" class=\"none\">".strip_tags(stripslashes(substr($comment,300)))."</span>";
				?></span></div></div>
	<?php
	}
	die;
}

function deleteMarketComment() {
	$comment_id = $_REQUEST["comment_id"];
	$postID = $_REQUEST["postID"];
	$VUserID = $_SESSION['Sess_UserId'];
//	print_r($_POST);
//	die;
	
//	echo "SELECT * FROM market_Post_comment WHERE comment_id = '$comment_id' and postID = '$postID' and com_memid = '$userID' limit 1";

	$comment_query=mysql_query("SELECT * FROM market_Post_comment WHERE comment_id = '$comment_id' and postID = '$postID'");
	$comment_TOTAL = mysql_num_rows($comment_query);
	if ($comment_TOTAL >0) {
	$commentUser_query=mysql_query("SELECT com_memid FROM market_Post_comment WHERE comment_id = '$comment_id' and postID = '$postID' and com_memid='$VUserID' limit 1");
	$commentUser_TOTAL = mysql_num_rows($commentUser_query);
	if ($commentUser_TOTAL >0) {
		$query="DELETE FROM market_Post_comment WHERE comment_id = '$comment_id' and postID = '$postID' and com_memid='$VUserID'";
		$result=mysql_query($query) or die(mysql_error());
		if ($result) {
			$return['status'] = '1';
			$return["result"] = "<font color=\"green\">Delete success!</font> ";
		} else {
			$return['status'] = '0';
			$return["result"] = "<font color=\"red\">Failed to delete the content! Please try again later!</font> ";
		}
	} else {
	$blog_query=mysql_query("SELECT mem_userid FROM market_Post WHERE mem_userid = '$VUserID' and postID = '$postID' limit 1") or die(mysql_error());
	$blog_TOTAL = mysql_num_rows($blog_query);
	if ($blog_TOTAL >0) {
		$query1="DELETE FROM market_Post_comment WHERE comment_id = '$comment_id' and postID = '$postID'";
		$result1=mysql_query($query1) or die(mysql_error());
		if ($result1) {
			$return['status'] = '1';
			$return["result"] = "<font color=\"green\">Delete success!</font> ";
		} else {
			$return['status'] = '0';
			$return["result"] = "<font color=\"red\">Failed to delete the content! Please try again later!</font> ";
		}
	} else {
	$return['status'] = '0';
	$return["result"] = "<font color=\"red\">You don't have the authority to delete this comment!</font> ";
	}
	}
	} else {
	$return['status'] = '1';
	$return["result"] = "<font color=\"red\">The comment was deleted!</font> ";
	}
	echo json_encode($return);
	die;
}

?>