
function FlagTip($t)
{
    if (!confirm('This comment will be flagged as offensive.  Are you sure?'))
        return;
        
    var requester;
    try { requester = new XMLHttpRequest(); }
    catch (error) {
        try { requester = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (error) { return false; }
    }
    
    requester.open("GET", "/Support/Flag.aspx?tipid=" + $t);
    requester.send(null);
    
    //var y = document.getElementById("spanTipFlag" + $t);
    //y.style.visibility = "hidden";
    //y.style.display = "none";
    
    //Make the href invisible.
    y = document.getElementById("spanTipFlagHref" + $t);
    y.visible = false;
    y.style.display = "none";
    
    y = document.getElementById("spanTipFlagged" + $t);
    //y.style.visibility = "visible";
    y.style.display = "inline";
    y.textContent = "This comment has been flagged.";
}

function r($t, $r) {
    $.getJSON("/Support/SetRating.aspx", { TipID: $t, Value: $r }, function(data) {            
        $('#' + $t + '_' + $r).parent().fadeOut(300, function(){ $(this).html(data.image).fadeIn(); });		    
    });    
            
    return false;        
}

function rA($t, $r) {
    $.getJSON("/Support/SetRating.aspx", { t: 4, ArticleID: $t, Value: $r }, function(data) {         
        $('#' + $t + '_' + $r).parent().fadeOut(300, function(){ $(this).html(data.image).fadeIn(); });
		    var star = document.getElementById('RatingCount');
            star.innerHTML = data.count;
    });    
                
    return false;
}

function ratingsOver($a, $n) { 		
	for ($i = 1; $i < 6; $i++)
		document.getElementById($a + "_" + $i).src = ($i <= $n) ? "/images/stars/star-hover.gif" : "/images/stars/star-nohover.gif";
}


function ratingsOut($a, $p1, $p2, $p3, $p4, $p5) { 
    var y = document.getElementById($a + "_1");
    y.src = "/images/stars/star-1-" + $p1 + ".gif";
    y = document.getElementById($a+"_2");
    y.src = "/images/stars/star-1-" + $p2 + ".gif";
    y = document.getElementById($a+"_3");
    y.src = "/images/stars/star-1-" + $p3 + ".gif";
    y = document.getElementById($a+"_4");
    y.src = "/images/stars/star-1-" + $p4 + ".gif";
    y = document.getElementById($a+"_5");
    y.src = "/images/stars/star-1-" + $p5 + ".gif";
}

function ismaxlength(obj)
{
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength);
}

function bindPostCommentLink(articleID)
{
    var self = this;
	this.articleID = articleID;
	
	$("#postComment").bind("click",
        function(){
			$(this).unbind("click");
			if ($(this).hasClass("Button2a")) {
				$(this).removeClass("Button2a")
					.addClass("Button1a")
					.find(".text").html("Posting Comment");
			} else if ($(this).hasClass("Button2Size1")) {
				$(this).removeClass("Button2Size1")
					.addClass("Button1Size1")
					.html("Posting Comment");
			}
			self.postComment($(this));
        }   
    )
	
	this.postComment = function(button) {
		if(validateComment()) {
			$.getJSON('ajax/Common.aspx', { 
			    method: "PostComment", 
			    articleID: self.articleID, 
			    comment: $("#MemberComment").val(),
			    captchaInput: $("#captchaInput").val(),
			    captchaText: $("#captchaText").val()
		}, function(response) {      
				if (response.Html) {
                    document.getElementById("MemberComment").value = String.empty;
					var totalCount = $("#TotalComments");
					if (totalCount)
					{
						totalCount = parseInt(totalCount.html());
						$("#TotalComments").html(totalCount + 1)
					}
											 
					if ($(".Comment").length > 0)
						$(".Comment:first").before(response.Html);
					else
						$(".Main.PostComment.Section").before("<div id=\"comments\" class=\"Main Comments Section\">" + response.Html + "</div>");

		                $("#captchaText").attr("value", "");
		                $("#captchaImage").attr("src", "/ui/images/misc/pixel.gif").hide();
		                $("#WordVerification").slideUp("fast");
				} else if (response.error) {
					$("#MemberComment").after('<div class="error-msg"></div>');
					$(".PostComment .error-msg").hide().html(response.error).fadeIn();
                    if(response.captcha==true) {
                        $("#captchaText").attr("value", response.captchaText);
                        $("#captchaImage").attr("src", response.captchaImage).show();
                        $("#WordVerification").slideDown();
                    } else {
                        document.getElementById("MemberComment").value = String.empty;
                    }
				}
						
				button.bind("click",
					function(){
						button.unbind("click")
							.removeClass("Button2a")
							.addClass("Button1a");
						self.postComment(button);
					} 
				);
				if (button.hasClass("Button1a")) {
					button.removeClass("Button1a")
						.addClass("Button2a")
						.find(".text").html("Post a Comment");
				} else if (button.hasClass("Button1Size1")) {
					button.removeClass("Button1Size1")
					.addClass("Button2Size1")
					.html("Post a Comment");
				}
				
				                        
				if($("#postToFacebook:checked").val() != null)
				{                                                                  
					var images = [];

					if (response.ItemThumbnailUrl != null && response.ItemThumbnailUrl != undefined && response.ItemThumbnailUrl != "")
						images = [{"src":response.ItemThumbnailUrl, "href":document.location.href}]
						
					var variables = {
						'Title' : response.Title,
						'URL' : document.location.href,
						'Excerpt' : response.ArticleIntroduction,
						'Comment_Body' : response.Text,
						'images' : images
					};
								
					FB.Connect.showFeedDialog("63205862906", variables, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);                        
				}
			})// -- end getJSON
		}// -- end validateComment
	}
}

function validateComment()
{
    if ($("#MemberComment").val() == "")
    {
        if($(".error-msg").length == 0)
            $("#MemberComment").before("<span class=\"error-msg\">There was an error in your request, please try again.</span>");
        return false;
    }
    else
    {
        $(".error-msg").remove();
        return true;    
    }
}
