var $v = "";

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) {
    $.ajax({
		type: "POST",
		url: "/Support/SetRating.aspx",
		data: "tipid=" + $t + "&value=" + $r
    });
    
    //for ($i = 1; $i <= 5; $i++)  document.getElementById($t + "_" + $i).src = ($i <= $r) ? "/images/stars/star-hover.gif" : "/images/stars/star-nohover.gif";
    
    $v += ";" + $t + ";";
        
    return false;
}

function rA($t, $r) {
    $.ajax({
		type: "POST",
		url: "/Support/SetRating.aspx",
		data: "t=4&ArticleId=" + $t + "&Value=" + $r,
		success: function(results){
			eval(results);
			
			$('.Details .Rating .Stars').fadeOut(300, function(){ $(this).html(rating.image).fadeIn(); });
		}
    });
    
    //for ($i = 1; $i <= 5; $i++) document.getElementById($t + "_" + $i).src = ($i <= $r) ? "/images/stars/star-hover.gif" : "/images/stars/star-nohover.gif";
    
    var star = document.getElementById('RatingCount');
    star.innerHTML = parseInt(star.innerHTML) + 1;
    
    $v += ";" + $t + ";";
     
    return false;
}

function ratingsOver($a, $n) { 
	if ($v.indexOf(";" + $a + ";") >=0) return;
	
	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) { 
if ($v.indexOf(";" + $a + ";") >=0) return;

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&comment=' + $("#MemberComment").val() + '&articleID=' + self.articleID, function(response) {      
						
				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>");
					
				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");
				}
				
				document.getElementById("MemberComment").value = String.empty;                        
				if($("#postToFacebook:checked").val() != null)
				{                                                                  
					var images = [];
					
					if(response.ItemThumbnailUrl != undefined && response.ItemThumbnailUrl != "")
						images = [{"src":response.ArticleThumbnailUrl, "href":document.location.href}]
						
					var variables = {
						'Title' : response.Title,
						'URL' : document.location.href,
						'Excerpt' : response.ArticleIntroduction,
						'Comment_Body' : response.UserComment,
						'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;    
    }
}

