$(document).ready(function(){
	$("#txtDirection").autogrow({lineHeight:18,minHeight:100});
	$(".pagingUl>li>a").unbind("href");
	$(".pagingUl>li>a").click(function(){Page($(this));});
	var isLoggedIn = $("#isLoggedIn").val();
	if(isLoggedIn == 0){
		addSpamQuestion();
	}	
	$("#addcommentonProfile").click(function(){
							if(($("#isLogin").length)){
								if($("#isLogin").val()!="1"){
									alert("Please login or register to add comment on this profile.");
									return false;
								}
							}
							$("#commentdiv").show();
							$("#txtDirection").focus();
							});
	$("#addcomment").toggle(function(){
								//if($("#Type").val() != 2)
								{
									
									if(isLoggedIn == 0){
										addSpamQuestion();
										$("#visitorCommentPopup").show();
									}else{
										$("#commentdiv").show();
										$("#txtDirection").focus();
									}
								}
							},
							function(){
								//if($("#Type").val() != 2)
								{
									var isLoggedIn = $("#isLoggedIn").val();
									if(isLoggedIn == 0){
										$("#visitorCommentPopup").hide();
									}else{
										$("#commentdiv").hide();
										$("#txtDirection").val("");
									}
								}
							});

	$("#btnCancel").click(Cancel);
	$(".ReplyCancel").click(Cancel);

	function Cancel(){

		if($(this).attr("name") == 'btnCancel')
		{
			//comment box
			$("#commentdiv").hide();
			$(this).parent().siblings("textarea").val("");
		}
		else
		{
			//reply box
			$(this).parent().parent().siblings("textarea").val("");
			$(this).parent().parent().parent().hide();
		}
	}
	$("#saveComment").click(SaveComment);

	$(".ReplySub").click(ReplySubFunc);



	$("#btnVSubmit").click(function(){
		if($("#Type").val() != 2 && $("#Type").val() != 0)
		{
			var e = $(this);
			if($("#visitor_form").valid()){
				$(this).attr('disabled',1);
				if($("#toCommentId").val() != 0)
					ReplySubFunc();
				else
					SaveComment();

				$(this).attr('disabled',0);
			}else{
				setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
			}
		}
	});
	jQuery.validator.addMethod("validName",function(value,element,param){ var firstnameRe=/^[a-zA-Z_][a-zA-Z_0-9]{0,14}$/;if(firstnameRe.test(value)){return true;}return false;});
	jQuery.validator.addMethod("answerCheck",function(value,element,param){ var answer = $("#spamQuestion").attr("answer"); if(value == answer ) {return true;}return false;});
	$("#visitor_form").validate({
			errorContainer : "#errorDiv",
			errorClass: "txtfldErr",
			errorElement : "p",
			errorElementClass : "marTop2px errorMsgs fontArial12px",
			rules: {
				txtVName:{ required:true},
				txtVEmail : { required:true,email:true},
				txtVSpamAnswer : {answerCheck:true}
			},
			messages: {
				txtVName:{required:'Please input your name.',validName:'You name can only contain characters a-z A-z 0-9 _.'},
				txtVEmail : {required:'Please enter a valid email.',email : 'Please enter a valid email.'},
				txtVSpamAnswer : {answerCheck:'Please enter a valid answer.'}
			}

		});

	$(".padRight5px.wid30px.floatRight").find("li").click(ReplyFunc);
	$(".padRight5px.wid30px.floatRight").find("h6:eq(0)").click(doExpertCritique);
	$(".wid480px.overFauto.floatLeft").find("a").click(ShowHide);

	$(".bg17brdb1d").hover(function(){
										if($("#Type").val() != 0){
											var thischildiv2=$(this).children("div")[2];
											var itschildul = $(thischildiv2).children("ul");
											$(itschildul).show(100);
											$(thischildiv2).children("h6").show(100);
										}
									},function(){
										if($("#Type").val() != 0){
											var thischildiv2=$(this).children("div")[2];
											var itschildul=$(thischildiv2).children("ul");
											$(itschildul).hide(100);
											if($(thischildiv2).children("h6").children("img").attr("operation") == 1)
												$(thischildiv2).children("h6").hide(100);
										}
									});
	function ShowHide(){

		var  id = $(this).parent().parent().attr("id");
		var name = $(this).attr("name");

		if( name == "Hide"){
			$(this).attr("name","Show");
			$(this).find("span:first").html("Show");
			$("#Reply"+id).hide();
		}else if( name == "Show"){
			$(this).attr("name","Hide");
			$(this).find("span:first").html("Hide");
			$("#Reply"+id).show();
		}
	}



	function SaveComment(){


		var e = $(this);

		if($.trim($("#txtDirection").val()) == ""){

			alert("Please enter comment.");
			setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
			$("#txtDirection").focus();
			return false;
		}
		if($("#Type").val() == 0 && $("#txtDirection").val().length < 140){

			alert("To encourage insightful and constructive critiques please enter atleast 140 characters.");
			setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
			$("#txtDirection").focus();
			return false;
		}else{

			e.unbind("click");
			//if(isLoggedIn == 1)
				//setTimeout(function(){e.bind("click",SaveComment);},3000);

			var name= "";
			var email= "";
			var website= "";
			var isLoggedIn = $("#isLoggedIn").val();

			if(isLoggedIn == 0){
				name = $("#txtVName").val();
				email = $("#txtVEmail").val();
				website = $("#txtVWebsite").val();


				var rExp = /http:\/\//i;
				var result = website.search("http://");

				if(result < 0){
					alert("Please enter the valid URL");
					setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
					return;
				}

				var eTest = email.search('@');

				if(eTest <= 0){
					alert("Please enter the valid Email");
					setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
					return;
				}




			}

			$.post(/*gBaseUrl+*/'/comment/SaveComment.html',
								{ "ContentId":$("#CommentConnectId").val(),
								  "Content":$.trim($("#txtDirection").val()),
								  "Type":$("#Type").val(),
								  "name":name,
								  "email":email,
								  "website":website,
								  "isLoggedIn":	isLoggedIn
								},
								function(data){

				e.bind("click",SaveComment);

				eval("commentData = "+data);
				{
					var hover = 'display:none';
					if($("#Type").val() == 0)
						hover = '';
				if(commentData.success == 1){
					alert("Unable to save comment");
					$("#txtDirection").val(commentData.content)	;
					setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
					return;
				}
				var obj = $('<div class="padBot5px padTop10px marTop1px bg17brdb1d"  id="'+commentData.CommentId+'">\
								<div class="wid50px floatLeft txtAlignCenter" title="'+commentData.show_name+'">\
									<a href="'+commentData.GetUserProfileUrl+'"><img alt="img" src="'+commentData.userImg+'" width="30" height="30"/></a>\
								</div>\
								<div class="wid480px overFauto floatLeft">\
									<ul class="padLeft5px">\
									</ul>\
									<div class="lineHeight18px padTop2px padLeft5px">'+commentData.comment+'</div>\
									<a class="marLeft5px cursorHand fontArial11px" name="Show" style="display:none"><span>Show</span> <span id="count'+commentData.CommentId+'">0</span> Replies</a>\
								</div>\
								<div class="padRight5px wid30px floatRight">\
									<input type="hidden" value="'+commentData.userId+'">\
									<ul class="padRight5px padLeft5px"  style="'+hover+'" id="Ul'+commentData.CommentId+'">\
									</ul>\
								</div>\
								<div class="ie6clearFix">&nbsp;</div>\
							</div>\
							<div id="ReplyBox'+commentData.CommentId+'" style="display:none" class="padding15px box_black16_brd1f">\
								<h4 class="clearBoth padBot10px brdBottomGray2">Add Reply</h4>\
								<textarea id="'+commentData.CommentId+'" class="overFauto txtfld1" onblur="txtBoxHighlight(this,0)" onfocus="txtBoxHighlight(this,1)"\
									style="border: 1px solid rgb(83, 82, 82); width: 540px; background-color: rgb(59, 59, 59); font-size: 12px" rows="5" cols="10" name="txtDirection"></textarea>\
								<div class="wid170px padTop15px mar0Auto overFauto clearBoth" id="'+commentData.CommentId+'">\
									<a class="floatLeft inpBtnBlackprim" href="javascript:void(0);"><span class="ReplySub">Submit</span></a>\
									<a class="floatLeft marLeft10px inpBtnBlacksec" href="javascript:void(0);"><span class="ReplyCancel">Cancel</span></a>\
								</div>\
							</div>\
							<div class="overFauto" id="Reply'+commentData.CommentId+'" style="display:none">\
							</div>');
				}
					if(isLoggedIn == 0){
						$("#txtVName").val("");
						$("#txtVEmail").val("");
						$("#txtVWebsite").val("http://");
						$("#txtVSpamAnswer").val("");
						$("#visitorCommentPopup").hide();
						$("#toCommentId").val("");
					}
					$("#txtDirection").val("");
					$("#commentList").prepend(obj);

					var optionsDiv1 = $(".bg17brdb1d").children("div")[1];
					var memberchildul=$(optionsDiv1).children("ul");

					if(commentData.GetUserProfileUrl != "http://")
						$(memberchildul[0]).append('<li class="fontCgray45"><a href="'+commentData.GetUserProfileUrl+'"><strong>'+commentData.show_name+'</strong></a><span class="marRight2px marLeft2px fontArial10px brdRightGray0">&nbsp;</span>  <span>'+commentData.time+'</span></li>');
					else
						$(memberchildul[0]).append('<li class="fontCgray45"><strong>'+commentData.show_name+'</strong><span class="marRight2px marLeft2px fontArial10px brdRightGray0">&nbsp;</span>  <span>'+commentData.time+'</span></li>');

					//append delete option ti right list for admin user;
					var optionsDiv = $(".bg17brdb1d").children("div")[2];
					var itschildul=$(optionsDiv).children("ul");

					if($Type = 0 ){
						if(($("#contentOwnerId").val() == $CurrentUserId)){
							if(commentData.expertCritique)
								$(optionsDiv).prepend('<h6 name="ExpertCritique" class="padRight5px padLeft5px txtAlignRight"><img src="'+commentData.siteUrl+'public/images/red_star.gif"  alt="*"  operation ="0" tempvalue="'+ commentData.UserId+'" class ="cursorHand"/></h6>');
							else
								$(optionsDiv).prepend('<h6 name="ExpertCritique" class="padRight5px padLeft5px txtAlignRight"><img src="'+commentData.siteUrl+'public/images/star.gif"  alt="*"  operation ="1" tempvalue="'+ commentData.UserId+'" class ="cursorHand"/></h6>');
						}else if(commentData.expertCritique){
							$(optionsDiv).prepend('<h6 name="ExpertCritique" class="padRight5px padLeft5px txtAlignRight"><img src="'+commentData.siteUrl+'public/images/star.gif"  alt="*" /></h6>');
						}
					}
					$(itschildul[0]).append('<li  class="padTop2px txtAlignRight" name="Reply"><img src="'+commentData.siteUrl+'public/images/member_reply.gif"  alt="+" class = "cursorHand"/></li>');
					if($("#isAdminUser").val() == 1)
						$(itschildul[0]).append('<li  class="padTop2px txtAlignRight" name="Delete"><img src="'+commentData.siteUrl+'public/images/admin_delete.gif"  alt="+" class = "cursorHand"/></li>');


					$("#commentTotal").html(parseInt($("#commentTotal").html())+1);
					if($("#Type").val() != 7)
						$("#commentdiv").hide();
					$(".floatRight").find("li").unbind("click");
					$(".ReplySub").unbind("click");

					$(".padRight5px.wid30px.floatRight").find("li").click(ReplyFunc);
					$(".ReplySub").click(ReplySubFunc);

					//old $(".floatLeft.inpBtnBlacksec").unbind("click");
					//old $(".floatLeft.inpBtnBlacksec").click(Cancel);
					$(".bg17brdb1d").unbind("hover");
					$(".bg17brdb1d").hover(function(){
										if($("#Type").val() != 0){
											var thischildiv2=$(this).children("div")[2];
											var itschildul = $(thischildiv2).children("ul");
											$(itschildul).show(100);
										}
									},function(){
										if($("#Type").val() != 0){
											var thischildiv2=$(this).children("div")[2];
											var itschildul=$(thischildiv2).children("ul");
											$(itschildul).hide(100);
										}
									});

				setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);

			});
		}
		return;
	}

	function ReplySubFunc(){

		var ContentId = $("#CommentConnectId").val();
		var Type = $("#Type").val();
		var CommentId = "";
		var Content = "";
		var name= "";
		var email= "";
		var website= "";
		var e = $(this);
		var isLoggedIn = $("#isLoggedIn").val();
		if(isLoggedIn == 0){

			CommentId = $("#toCommentId").val();
			Content = $("#txtDirection").val();
			name = $("#txtVName").val();
			email = $("#txtVEmail").val();
			website = $("#txtVWebsite").val();
		}else{

			if($.trim($(this).parents("div").find("textarea").val()) == ""){
				alert("Please input critique.");
				$(this).parents("ul").find("textarea").focus();
				setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
				return;
			}else{

				CommentId = $(this).parents("div").find("textarea").attr("id");
				//myCommentId = $(this).parent().parent().attr("id");
				Content = $(this).parents("div").find("textarea").val();
				$(this).parents("div").find("textarea").val("");
			}
		}
			$.post('/comment/SaveReply.html',
								{ "ContentId":ContentId,
								  "CommentId":CommentId,
								  "Content":Content,"Type":Type,
								  "name":name,
								  "email":email,
								  "website":website,
								  "isLoggedIn":	isLoggedIn
								},
								function(data){
				setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
				eval("Reply = "+data);
				var totalreplies = ($("#Reply"+CommentId).children().length) /2;
				var colourClass="";

				if(totalreplies % 2 == 0) colourClass="bgColor1e";
				else colourClass = "bgColor17";
				var obj = $('<div class="brdBottomDotted2 padBot5px padTop12px '+colourClass+'" id="'+CommentId+'">\
								<div class="wid30px floatLeft">&nbsp;</div>\
								<div class="wid30px floatLeft txtAlignCenter" title="'+Reply.UserName+'">\
									<a href="'+Reply.GetUserProfileUrl+'"><img alt="img" src="'+Reply.userImg+'" width="30" height="30"/></a>\
								</div>\
								<div class="wid480px overFauto floatLeft padLeft8px">\
									<ul class="padLeft5px" id="optionUl'+Reply.ContentCommentId+'">\
									</ul>\
									<input type="hidden" value="'+Reply.userId+'">\
									<div class="lineHeight18px padTop2px padLeft5px">'+Reply.Comment+'</div>\
								</div>\
								<div class="padRight5px wid30px floatRight">\
									<ul class="padRight5px padLeft5px" id="Ul'+Reply.ContentCommentId+'">\
										<li  class="padTop2px txtAlignRight" name="Reply"><img src="'+gBaseUrl+'public/images/member_reply.gif" alt="+" class ="cursorHand" /></li>\
									</ul>\
								</div>\
								<div class="clearBoth fontArial5px">&nbsp;</div>\
								</div>\
								<div id="ReplyBox'+Reply.ContentCommentId+'" style="display:none" class="padding15px box_black16_brd1f">\
									<h4 class="clearBoth padBot10px brdBottomGray2">Add Reply</h4>\
									<textarea id ="'+CommentId+'" class="overFauto txtfld1" onblur="txtBoxHighlight(this,0)" onfocus="txtBoxHighlight(this,1)" style="border: 1px solid rgb(83, 82, 82); width: 540px; background-color: rgb(59, 59, 59); font-size: 12px" rows="5" cols="10" name="txtDirection"></textarea>\
									<div class="wid170px padTop15px mar0Auto overFauto clearBoth" id="'+Reply.ContentCommentId+'">\
									<a class="floatLeft inpBtnBlackprim" href="javascript:void(0);"><span class="ReplySub">Submit</span></a>\
									<a class="floatLeft marLeft10px inpBtnBlacksec" href="javascript:void(0);"><span class="ReplyCancel">Cancel</span></a>\
								</div>\
							</div>');

				$("#Reply"+CommentId).append(obj);

				if(Reply.GetUserProfileUrl != "http://")
					$("#optionUl"+Reply.ContentCommentId).append('<li class="fontCgray45"><a href="'+Reply.GetUserProfileUrl+'"><strong>'+Reply.UserName+'</strong></a><span class="marRight5px marLeft5px fontArial10px brdRightGray0"> </span> <span>'+Reply.createdOn+'</span></li>');
				else
					$("#optionUl"+Reply.ContentCommentId).append('<li class="fontCgray45"><strong>'+Reply.UserName+'</strong><span class="marRight5px marLeft5px fontArial10px brdRightGray0"> </span> <span>'+Reply.createdOn+'</span></li>');



				if(isLoggedIn == 0){
					$("#txtVName").val("");
					$("#txtVEmail").val("");
					$("#txtVWebsite").val("http://");
					$("#txtDirection").val("");
					$("#txtVSpamAnswer").val("");
					$("#visitorCommentPopup").hide();
					$("#toCommentId").val("");
				}



				$(".wid480px.overFauto.floatLeft").find("a").click(ShowHide);
				$(".padRight5px.wid30px.floatRight").find("li").click(ReplyFunc);

				$(".ReplySub").click(ReplySubFunc);
				$(".ReplyCancel").click(Cancel);
				$(".padLeft5px").find("a").click(DelReply);

				$("#ReplyBox"+CommentId).hide();

				$("#count"+CommentId).html(parseInt($("#count"+CommentId).html())+1);
				$("#count"+CommentId).parent().show();

			});


	}



	$(".padLeft5px").find("a").click(DelReply);

	function DelReply(){
		if($(this).attr("name") == "replyDelete"){
			if(confirm("Are you sure you want to delete this reply ?")){
				var id = $(this).parent().parent().attr("id");
				var e = $(this);
				$.post(/*gBaseUrl+*/'/comment/DelComment.html',{"CommentId":id,"flag":""},function(data){
					if(data == "1")
						e.parents(".black_box_brdBottom").eq(0).slideUp("slow",function(){$(this).remove()});
					});
			}
		}
	}
	function doExpertCritique(){
				var e = $(this);
				var touser_id = $(this).children().attr("tempvalue");
				var operation = $(this).children().attr("operation");
				var  id = e.parent().parent().attr("id");//comment id
				if(touser_id == $("#CurrentUserId").attr("value")){
					alert("Sorry! This is yourself Critiques");
					return false;
				}

				$.post(/*gBaseUrl + */'/ajaxhandler/AddToFavorite.html', {objId:id,objType:$("#Type").val(),touser_id:touser_id,category_id:$("#category").val(),operation:operation},function(data){
    				eval('ret = ' + data);

    				if(ret.ret == 0){

						if(operation == 1){

							e.children("img").attr("src",gBaseUrl+ "public/images/red_star.gif");
							e.children("img").attr("operation","0");
						}
						else if(operation == 0){
							e.children("img").attr("src",gBaseUrl+ "public/images/star.gif");
							e.children("img").attr("operation","1");
						}
    				}
    			});
	}
	function ReplyFunc(){
		var e = $(this);

		var  id = e.parent().parent().parent().attr("id");//comment id

		switch($(this).attr("name")){
			case "Reply":
				//if(isLogin == "false"){
				//	alert("Please login to continue!");
				//	return ;
				//}
				var isLoggedIn = $("#isLoggedIn").val();
//PZ TODO
				//e.unbind("click");

				if(isLoggedIn == 0){
					if($("#Type").val() != 0 && $("#Type").val() != 2){
						addSpamQuestion();
						$("#visitorCommentPopup").show();
						$("#toCommentId").val(id);
					}else{
						alert("Please login to reply to this comment!");
					}
				}else{
					$("#ReplyBox"+id).show();
					e.toggle(function(){$("#ReplyBox"+id).show()},function(){$("#ReplyBox"+id).hide()});
				}
			break;
			/*case "ReplyToReply":
				if(isLogin == "false"){
					alert("Sorry ! You need Login!");
					return ;
				}
				e.unbind("click");

				$("#ReplyToReplyBox"+id).show();
				e.toggle(function(){$("#ReplyToReplyBox"+id).show()},function(){$("#ReplyToReplyBox"+id).hide()});
			break;*/
			case "Delete":
				if($("#isAdminUser").val() == 1){

					if(confirm("Are you sure you want to delete this comment ?")){
						$.post(/*gBaseUrl+*/'/comment/DelComment.html',{"CommentId":id,"flag":e.attr("name")},function(data){
							if(data == "1"){
								if(parseInt($("#commentTotal").html())-1 == 0)
									$("#commentTotal").html("0");
								else
									$("#commentTotal").html(parseInt($("#commentTotal").html())-1);
								e.parent().parent().parent().slideUp("slow",function(){$(this).remove()});
							}
						});
					}
				}
			break;
		}
	}

	$("[act='report']").click(function(){

		if(($("#isLogin").length)){
			if($("#isLogin").val()!="1"){
				alert("Please login or register to report this user.");
				return false;
			}
		}
		$("#btnReport").attr("action","Report");
		$("#reportpopupTitle").html("Report");
		$("#btnReport").val("Report");
		if(($("#btnSkip").length))
			$("#btnSkip").parent().hide();
		rate = getCenterPosition([418,242]);
		$.blockUI({overlayCSS:{ backgroundColor:'#000',opacity:'0.68'},css:{top:rate[0],left:rate[1],border:0,width:'415px',textAlign:'left'},message:$("#reportpopup")});
		//alert('Damian. Please provide the popup design.');
	});

	$("#btnSkip").click(function(){
		$("#addConnection").attr("skip",0);
		$("#btnReport").trigger("click");
	});

	$("#btnReport").click(function(){

		var msg = $.trim($("#txtEligible").val());
		var appType= $("#btnReport").attr("action");


		var obj = $(this);

		if(appType == "Report") {

			if(msg.length == 0){
					alert('Please input your cause.');
					return;
			}

			obj.attr('disabled',1);


			var	from = $("#reportFrom").val();
			var type = 0;
			if($("#contentType").length)
				type = $("#contentType").val();

			if(from)
				msg += '<br/><br/> From <br/><br/>'+from;
			var to;
			if(type == 1)
				to = "design_reports@designerscouch.org";
			else
				to = "member_reports@designerscouch.org";
			//we are making JSon-p request not normal post.
			// As profile page URl 	is remapped.
			$.getJSON(gBaseUrl + 'ajaxhandler/sendReport.html?from='+from+'&to='+to+'&msg='+msg+'&jsoncallback=?',
				function(ret){

						obj.attr('disabled',0);
						//eval('var ret = ' + data);
						if(ret.ret == 0){
							$("#txtEligible").val("");
							setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
							$.unblockUI();
							 var box = $('<div class="messageBox" >\
											<div class="messageTop">\
											<img src="'+gBaseUrl+'public/images/mesCloseBtn.gif" width="18" height="18" alt="Close" id="closeAlert"/>\
											</div>\
											<div class="messageCon" style="width:580px;">\
												Thanks for your report, we appreciate it. A member of our staff will look into this.\
											</div>\
										 </div>');

							$("#container").prepend(box);
							rate = getCenterPosition([596,73]);
							$(".messageBox").css({left:rate[1]});
							$("#closeAlert").click(function(){
								$(this).parents(".messageBox").hide();
							});
						}
					});
		}else{		//connection start

			var e=$("#addConnection");
			var friend_id = $("#addConnection").attr("user_id");
			var sendMail = $("#addConnection").attr("skip");
			var type = $("#addConnection").attr("Type");
			if(sendMail == 1 && msg.length == 0){
				alert('Please input your message.');
				return;
			}
			{
				if($("#addConnection").attr("operation")==1){
					$.getJSON(gBaseUrl+'connection/AddConnection.html?type='+type+'&friend_id='+friend_id+'&msg='+msg+'&sendMail='+sendMail+'&jsoncallback=?',
							function(data)
							{
								setTimeout(function() { $(".inpBtnBlackprim").animate({opacity: 1});}, 300);
								$.unblockUI();
								if(data=="2")
									alert("You have added!");
								else if(data=="3")
									alert("You need to be an approved member to add a connection.");
								else {
									if( type == 4 )
										var text='<img src="'+gBaseUrl+'public/images/add_connection.gif" alt=">" class="marBotMin3px" />Following';
									else
										var text='<img src="'+gBaseUrl+'public/images/add_connection.gif" alt=">" class="marBotMin3px" />Connection Pending';
								}
								e.removeClass("redLink");
								e.addClass("grayLink");
								e.attr("style","text-decoration:none;cursor:default;");
								e.html(text);

							});
				}
			}
		}

   	});
	function Page(e){

		if(e.attr("href") == "#")
			return ;
		if(e.attr("title") == "")
			e.attr("title",e.attr("href"));

		e.attr("href","javascript:void(0);");
		e.parent().siblings().find("a").removeClass();
		e.addClass("active");

		$.post(/*gBaseUrl+*/"/comment/CommentPage.html",{ContentId:$("#CommentConnectId").val(),"Page":e.attr("title")},function(data){
			eval("Comments = "+data);
			$("#commentList").html("");


			for(var i=0; i<(Comments.data.length); i++){
				var style = 'style="cursor:pointer"';
				var replies = 'style="display:none"';
				var totalReplies=0;
				var hover = 'display:none';
				if($("#Type").val() == 0)
					hover = '';

				if(Comments.Reply[Comments.data[i].CommentId] )
					totalReplies = Comments.Reply[Comments.data[i].CommentId].length;
				if(totalReplies != 0 )
					replies = 'style="display:inline"';


					var temp = $('<div class="padBot5px padTop10px marTop1px bg17brdb1d"  id="'+Comments.data[i].CommentId+'">\
									<div class="wid50px floatLeft txtAlignCenter" title="'+Comments.data[i].UserName+'">\
										<a href="'+Comments.data[i].GetUserProfileUrl+'"><img alt="img" src="'+Comments.data[i].GetUserAvatarUrl+'" width="30" height="30"/></a>\
									</div>\
									<div class="wid480px overFauto floatLeft">\
										<ul class="padLeft5px" id="optionUl'+Comments.data[i].CommentId+'" >\
										</ul>\
										<div class="lineHeight18px padTop2px padLeft5px">'+Comments.data[i].Comment+'</div>\
										<a class="marLeft5px cursorHand fontArial11px" name="Show" '+replies +'"><span>Show</span> <span id="count'+Comments.data[i].CommentId+'">'+totalReplies+'</span> Replies</a>\
									</div>\
									<div class="padRight5px wid30px floatRight">\
										<input type="hidden" value="'+Comments.data[i].UserId+'">\
										<ul class="padRight5px padLeft5px"  style="'+hover+'" id ="Ul'+Comments.data[i].CommentId+'">\
										</ul>\
									</div>\
									<div class="ie6clearFix">&nbsp;</div>\
								</div>\
								<div id="ReplyBox'+Comments.data[i].CommentId+'" style="display:none" class="padding15px box_black16_brd1f">\
									<h4 class="clearBoth padBot10px brdBottomGray2">Add Reply</h4>\
									<textarea id="'+Comments.data[i].CommentId+'" class="overFauto txtfld1" onblur="txtBoxHighlight(this,0)" onfocus="txtBoxHighlight(this,1)"\
										style="border: 1px solid rgb(83, 82, 82); width: 540px; background-color: rgb(59, 59, 59); font-size: 12px" rows="5" cols="10" name="txtDirection"></textarea>\
									<div class="wid170px padTop15px mar0Auto overFauto clearBoth" id="'+Comments.data[i].CommentId+'">\
										<a class="floatLeft inpBtnBlackprim" href="javascript:void(0);"><span class="ReplySub">Submit</span></a>\
										<a class="floatLeft marLeft10px inpBtnBlacksec" href="javascript:void(0);"><span class="ReplyCancel">Cancel</span></a>\
									</div>\
								</div>');


				$("#commentList").append(temp);

				var memberchildul=$("#optionUl"+Comments.data[i].CommentId);

				if(Comments.data[i].GetUserProfileUrl != "http://")
					$(memberchildul).append('<li class="fontCgray45"><a href="'+Comments.data[i].GetUserProfileUrl+'"><strong>'+Comments.data[i].UserName+'</strong></a><span class="marRight2px marLeft2px fontArial10px brdRightGray0">&nbsp;</span>  <span>'+Comments.data[i].createdOn+'</span></li>');
				else
					$(memberchildul).append('<li class="fontCgray45"><strong>'+Comments.data[i].UserName+'</strong><span class="marRight2px marLeft2px fontArial10px brdRightGray0">&nbsp;</span>  <span>'+Comments.data[i].createdOn+'</span></li>');


				if($Type = 0 && ($("#contentOwnerId").val() == $CurrentUserId)){
					if(Comments.data[i].expertCritique)
						$("#Ul"+Comments.data[i].CommentId).parent().prepend('<h6 name="ExpertCritique" class="padRight5px padLeft5px txtAlignRight"><img src="'+Comments.siteUrl+'public/images/red_star.gif"  alt="*"  operation ="0" tempvalue="'+Comments.data[i].UserId+'" class ="cursorHand"/></h6>');
					else
						$("#Ul"+Comments.data[i].CommentId).parent().prepend('<h6 name="ExpertCritique" class="padRight5px padLeft5px txtAlignRight"><img src="'+Comments.siteUrl+'public/images/red_star.gif"  alt="*"  operation ="1" tempvalue="'+Comments.data[i].UserId+'" class ="cursorHand"/></h6>');
				}else if(Comments.data[i].expertCritique){

					$("#Ul"+Comments.data[i].CommentId).parent().prepend('<h6 class="padRight5px padLeft5px txtAlignRight"><img src="'+Comments.siteUrl+'public/images/red_star.gif"  alt="*"  operation ="0" tempvalue="'+Comments.data[i].UserId+'"/></h6>');
				}
				$("#Ul"+Comments.data[i].CommentId).append('<li  class="padTop2px txtAlignRight" name="Reply"><img src="'+Comments.siteUrl+'public/images/member_reply.gif"  alt="+" class = "cursorHand"/></li>');
				//append delete option ti right list for admin user;
				if($("#isAdminUser").val() == 1)
					$("#Ul"+Comments.data[i].CommentId).append('<li  class="padTop2px txtAlignRight" name="Delete"><img src="'+Comments.siteUrl+'public/images/admin_delete.gif"  alt="+" /></li>');


				//var stringReply = showReplies(Comments.Reply[Comments.data[i].CommentId], Comments.data[i].CommentId);
				Replytemp = '<div class="overFauto" style="display:none" id="Reply'+Comments.data[i].CommentId+'">';
				if(Comments.Reply[Comments.data[i].CommentId]){

					var colourClass="";
					var replyIndex = 0;
					for(var j = 0; j < totalReplies; j++){


						 if(replyIndex % 2 == 0) colourClass="bgColor1e";
						 else colourClass = "bgColor17";
						 replyIndex++;

						 Replytemp += ('<div class="brdBottomDotted2 padBot5px padTop12px '+colourClass+'" id="'+Comments.data[i].CommentId+'">\
											<div class="wid30px floatLeft">&nbsp;</div>\
											<div class="wid30px floatLeft txtAlignCenter" title="'+Comments.Reply[Comments.data[i].CommentId][j].UserName+'">\
												<a href="'+Comments.Reply[Comments.data[i].CommentId][j].GetUserProfileUrl+'"><img alt="img" src="'+Comments.Reply[Comments.data[i].CommentId][j].GetUserAvatarUrl+'" width="30" height="30"/></a>\
											</div>\
											<div class="wid480px overFauto floatLeft padLeft8px">\
												<ul class="padLeft5px">\
													<li class="fontCgray45"><a href="'+Comments.Reply[Comments.data[i].CommentId][j].GetUserProfileUrl+'"><strong>'+Comments.Reply[Comments.data[i].CommentId][j].UserName+'</strong></a><span class="marRight5px marLeft5px fontArial10px brdRightGray0"> </span> <span>'+Comments.Reply[Comments.data[i].CommentId][j].jscreatedOn+'</span></li>\
												</ul>\
												<input type="hidden" value="'+Comments.Reply[Comments.data[i].CommentId][j].UserId+'">\
												<div class="lineHeight18px padTop2px padLeft5px">'+Comments.Reply[Comments.data[i].CommentId][j].Comment+'</div>\
											</div>\
											<div class="padRight5px wid30px floatRight">\
												<ul class="padRight5px padLeft5px" id="Ul'+Comments.Reply[Comments.data[i].CommentId][j].ContentCommentId+'">\
													<li  class="padTop2px txtAlignRight" name="Reply"><img src="'+gBaseUrl+'public/images/member_reply.gif" alt="+" class ="cursorHand" /></li>\
												</ul>\
											</div>\
											<div class="clearBoth fontArial5px">&nbsp;</div>\
									    </div>\
										<div id="ReplyBox'+Comments.Reply[Comments.data[i].CommentId][j].ContentCommentId+'" style="display:none" class="padding15px box_black16_brd1f">\
											<h4 class="clearBoth padBot10px brdBottomGray2">Add Reply</h4>\
											<textarea id ="'+Comments.data[i].CommentId+'" class="overFauto txtfld1" onblur="txtBoxHighlight(this,0)" onfocus="txtBoxHighlight(this,1)" style="border: 1px solid rgb(83, 82, 82); width: 540px; background-color: rgb(59, 59, 59); font-size: 12px" rows="5" cols="10" name="txtDirection"></textarea>\
											<div class="wid170px padTop15px mar0Auto overFauto clearBoth"   id="'+Comments.Reply[Comments.data[i].CommentId][j].ContentCommentId+'">\
												<a class="floatLeft inpBtnBlackprim" href="javascript:void(0);"><span class="ReplySub">Submit</span></a>\
												<a class="floatLeft marLeft10px inpBtnBlacksec" href="javascript:void(0);"><span class="ReplyCancel">Cancel</span></a>\
											</div>\
										</div>');

						//$("#Reply"+replyTo).append(Replytemp);//append reply to reply
						$(".ReplySub").click(ReplySubFunc);

						$(".padRight5px.wid30px.floatRight").find("li").click(ReplyFunc);
						$(".wid480px.overFauto.floatLeft").find("a").click(ShowHide);
					}
				}
				Replytemp += '</div>';

				$("#commentList").append(Replytemp);
			}



			$("#pagination").html(Comments.pageurl);
			$("#pagination").append('<div class="font0px clearBoth padding1px"> </div>');
			$(".pagingUl > li > a").unbind("href");
			$(".pagingUl > li > a").click(function(){Page($(this));});

			$(".ReplySub").click(ReplySubFunc);


			$(".padRight5px.wid30px.floatRight").find("li").click(ReplyFunc);
			$(".wid480px.overFauto.floatLeft").find("a").click(ShowHide);

			$(".bg17brdb1d").hover(function(){
									if($("#Type").val() != 0){
										var thischildiv2=$(this).children("div")[2];
										var itschildul=$(thischildiv2).children("ul");
										$(itschildul[0]).show(100);
									}
								},function(){
									if($("#Type").val() != 0){
										var thischildiv2=$(this).children("div")[2];
										var itschildul=$(thischildiv2).children("ul");
										$(itschildul[0]).hide(100);
									}
								});
		});
	}
/*	function showReplies(Reply, replyTo)
	{

			var Replytemp = '';
				// add this div so that reply can be appended to it
			Replytemp = '<div class="overFauto" style="display:none" id="Reply'+replyTo+'">';
			if(Reply){

				for(var j = 0; j < Reply.length; j++){

					var replyId = Reply[j].ContentCommentId;
					var totalRepliesToreply = Reply[j].totalReplies;
					var toshowReplyornot = "" ;
					if( totalRepliesToreply == 0 ) toshowReplyornot = 'style="display:none;"';


					 Replytemp += ('<div class="brdBottomDotted2 padBot5px padTop10px marTop1px" id="'+Reply[j].ContentCommentId+'">\
										<div class="wid30px floatLeft">&nbsp;</div>\
										<div class="wid50px floatLeft txtAlignCenter" title="'+Reply[j].UserName+'">\
											<a href="'+Reply[j].GetUserProfileUrl+'"><img alt="img" src="'+Reply[j].GetUserAvatarUrl+'" width="30" height="30"/></a>\
										</div>\
										<div class="wid480px overFauto floatLeft">\
											<ul class="padLeft5px">\
												<li class="fontCgray45"><a href="'+Reply[j].GetUserProfileUrl+'"><strong>'+Reply[j].UserName+'</strong></a><span class="marRight5px marLeft5px fontArial10px brdRightGray0"> </span> <span>'+Reply[j].jscreatedOn+'</span></li>\
											</ul>\
											<input type="hidden" value="'+Reply[j].UserId+'">\
											<div class="lineHeight18px padTop2px padLeft5px">'+Reply[j].Comment+'</div>\
										</div>\
										<div class="padRight5px wid30px floatRight">\
											<ul class="padRight5px padLeft5px" id="Ul'+Reply[j].ContentCommentId+'">\
												<li  class="padTop2px txtAlignRight" name="Reply"><img src="'+gBaseUrl+'public/images/member_reply.gif" alt="+" class ="cursorHand" /></li>\
											</ul>\
										</div>\
										<div class="ie6clearFix">&nbsp;</div>\
								    </div>\
									<div id="ReplyBox'+Reply[j].ContentCommentId+'" style="display:none" class="padding15px box_black16_brd1f">\
										<h4 class="clearBoth padBot10px brdBottomGray2">Add Reply</h4>\
										<textarea id ="'+Reply[j].ContentCommentId+'" class="overFauto txtfld1" onblur="txtBoxHighlight(this,0)" onfocus="txtBoxHighlight(this,1)" style="border: 1px solid rgb(83, 82, 82); width: 540px; background-color: rgb(59, 59, 59); font-size: 12px" rows="5" cols="10" name="txtDirection"></textarea>\
										<div class="wid170px padTop15px mar0Auto overFauto clearBoth"  id="'+Reply[j].ContentCommentId+'">\
											<a class="floatLeft inpBtnBlackprim" href="javascript:void(0);"><span class="ReplySub">Submit</span></a>\
											<a class="floatLeft marLeft10px inpBtnBlacksec" href="javascript:void(0);"><span class="ReplyCancel">Cancel</span></a>\
										</div>\
									</div>');

					//$("#Reply"+replyTo).append(Replytemp);//append reply to reply
					$(".ReplySub").click(ReplySubFunc);

					$(".padRight5px.wid30px.floatRight").find("li").click(ReplyFunc);
					$(".wid480px.overFauto.floatLeft").find("a").click(ShowHide);
					var str = showReplies(Reply[j].Reply, Reply[j].ContentCommentId)
					Replytemp += str;
				}
			}
			Replytemp += '</div>';
			return (Replytemp);

	}

*/
	$("#sendMultipleMail").click(function(){

			var from = $("#txtFrom").val();
			var randomStr = $("#randomStr").val();
			var type = $(this).attr("otype");
			var count = 1;
			var to="";
			var allTo = "";
			do
			{
				to = $("#txtTo"+count).val();
				if(allTo != "") allTo = allTo + ', ';
				allTo =  allTo + '<span>'+ to +'</span>';

				$.post(/*gBaseUrl + */'/ajaxhandler/sendMail.html', {from:from,to:to,type:type,randomStr:randomStr},
				function(data){

					eval('var ret = ' + data);
					if(ret.ret == 0){

					}
				});

				$("#txtTo"+count).val("");
				$("#txtFrom").val("");
				count++;

			}while( ($("#txtTo"+count).length > 0) && from != "")


			 var box = $('<div class="messageBox">\
							<div class="messageTop">\
							<img src="'+gBaseUrl+'public/images/mesCloseBtn.gif" width="18" height="18" alt="Close" id="closeAlert"/>\
							</div>\
							<div class="messageCon">\
								Your email to '+ allTo +' was sent successfully.\
							</div>\
						 </div>');

			$("#container").prepend(box);
			rate = getCenterPosition([596,73]);
			$(".messageBox").css({left:rate[1]});
			$("#closeAlert").click(function(){
				$(this).parents(".messageBox").hide();
			});

	});

	function addSpamQuestion()
	{
		var num1 = Math.floor(Math.random()*11);
		var num2 = Math.floor(Math.random()*11);
		var answer = num1 +num2;
		$("#spamQuestion").html(num1 + ' + ' + num2 + ' = ?');
		$("#spamQuestion").attr("answer",answer);
	}


});