﻿// JScript File

var StoredCommentText;
var StoredVote;
var SuppressFade = false;
var CommentTempID;

function PostComment()
{
    var oXmlHttp = createXMLHttpRequest();
    oXmlHttp.open("POST","ajax_post_comment.aspx",true);
    oXmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    var vals = "CommentBody="+encodeURIComponent(StoredCommentText);
    vals = vals + "&CriticVote=" + StoredVote;
    var SongID = document.getElementById("SongID").value;
    vals = vals + "&SongID=" + SongID;
    
    var ContextID = document.getElementById("ContextID").value;
    vals = vals + "&ContextID=" + ContextID;
        
    vals = vals + "&CommentTempID=" + CommentTempID;
    oXmlHttp.send(vals);    
 
    oXmlHttp.onreadystatechange = function () 
    {
        if (oXmlHttp.readyState == 4) 
        {
            // results are in
            //alert(oXmlHttp.responseText);
            var responsearray = oXmlHttp.responseText.split("|");
            if (responsearray[0] == "ok")
            {
                if (StoredVote > 0)
                {
                    var voted = document.getElementById("AlreadyVoted");
                    voted.value = "true";
                }
                ShowStartCommentButton();
                SuppressFade = true;            
                startCommentRequest()
            }
            else if (responsearray[0] == "error")
            {
                SetErrorText(responsearray[1]);
            }
        }
    }
}

function cmdStartComment_onclick() {
    StoredCommentText = "";
    StoredVote = 0;
    ShowCommentInput();
}

function ShowCommentInput() {
    // set the area to input mode
    var inputArea = document.getElementById("inputSpan");
    var html = "";

    // show the comment area
    html = html + "<div class='inPageCommentArea'>";

    html = html+ "<span id='errorMsgs'></span>";
    
    if (AlreadyVoted() == false)
    {
        // vote area
        var Selected00 = "";
        var Selected05 = "";
        var Selected10 = "";
        var Selected15 = "";
        var Selected20 = "";
        var Selected25 = "";
        var Selected30 = "";
        var Selected35 = "";
        var Selected40 = "";
        var Selected45 = "";
        var Selected50 = "";
        
        if (StoredVote == 0) Selected00="selected='selected'";
        if (StoredVote == 0.5) Selected05="selected='selected'";
        if (StoredVote == 1.0) Selected10="selected='selected'";
        if (StoredVote == 1.5) Selected15="selected='selected'";
        if (StoredVote == 2.0) Selected20="selected='selected'";
        if (StoredVote == 2.5) Selected25="selected='selected'";
        if (StoredVote == 3.0) Selected30="selected='selected'";
        if (StoredVote == 3.5) Selected35="selected='selected'";
        if (StoredVote == 4.0) Selected40="selected='selected'";
        if (StoredVote == 4.5) Selected45="selected='selected'";
        if (StoredVote == 5.0) Selected50="selected='selected'";
        
        // votes area
        html = html + "<div class='formRow'>";
        html = html + "<B>Your Rating</B> ";
        html = html + "<select name='lstVotes' id='lstVotes'><option " + Selected00 + " value='0'>No Vote Yet</option><option " + Selected05 + " value='0.5'>0.5 Stars</option><option " + Selected10 + " value='1'>1 Stars</option><option " + Selected15 + " value='1.5'>1.5 Stars</option><option " + Selected20 + " value='2'>2 Stars</option><option " + Selected25 + " value='2.5'>2.5 Stars</option><option " + Selected30 + " value='3'>3 Stars</option><option " + Selected35 + " value='3.5'>3.5 Stars</option><option " + Selected40 + " value='4'>4 Stars</option><option " + Selected45 + " value='4.5'>4.5 Stars</option><option " + Selected50 + " value='5'>5 Stars</option></select>";
        html = html + "</div>";
    }
    
    // text area
    html = html + "<div class='simpleFormElement'>";
    html = html + "<B>Your Comment</B>";
    
    // show big input regularly, smaller for contest
    var ContestID = document.getElementById("ContestID").value;
    if (ContestID == 0)
    {
        html = html + "<textarea name='txtComment' rows='2' cols='20' id='txtComment' style='width:500px;'>" + StoredCommentText + "</textarea>";
    }
    else
    {
        html = html + "<textarea name='txtComment' rows='2' cols='20' id='txtComment' style='width:300px;'>" + StoredCommentText + "</textarea>";
    }

    html = html + "</div>";

    html = html + "<div class='simpleFormBtn'>";
    html = html+ getButton("Discard", "ShowStartCommentButton", "btn1");    
    html = html+ getButton("Preview", "PreviewComment", "btn1");
    html = html+ getButton("Post Comment", "SubmitFromEntry", "btn2");
    html = html + "</div>";

    
    // end the page
    html = html + "</div>";
    inputArea.innerHTML = html;
    
    // set focus to textbox
    var commentTxtBox = document.getElementById("txtComment");
    commentTxtBox.focus();
}

function SetErrorText(ErrorText)
{
    var errorsArea = document.getElementById("errorMsgs");

    html = "<div id='valsumCommentEnter' class='errorText' style='color:Red;'>";
    html = html + "<ul><li>" + ErrorText + "</li></ul>";
    html = html + "</div>";

    errorsArea.innerHTML = html; 
}

function CheckForErrors()
{
    var ErrorsFound = false;
    var txtComment = document.getElementById("txtComment");
    
    // make sure there's something in there
    var NewVote = 0;
    if (AlreadyVoted() == false)
    {
        var lstVotes = document.getElementById("lstVotes");
        NewVote = lstVotes.value;
    }
    if (txtComment.value == "" && NewVote == 0)
    {
        SetErrorText("Please enter a vote or a comment!");
        ErrorsFound = true;   
    }
    else if (txtComment.value.indexOf('<') != -1 || txtComment.value.indexOf('>') != -1)
    {
        SetErrorText("HTML isn't allowed.  Line breaks and HTML links will be added for you.");
        ErrorsFound = true;
    }
    
    return ErrorsFound;
}

function getImageHTML(Stars)
{
    var baseURL = "images/";
    var ImageURL;
    var AltText;

    if (Stars > 4.75)
    {
        ImageURL = baseURL + "stars5.gif";
        AltText = "5 stars";
    }
    else if (Stars > 4.25)
    {
        ImageURL = baseURL + "stars4.5.gif";
        AltText = "4.5 stars";
    }
    else if (Stars > 3.75)
    {
        ImageURL = baseURL + "stars4.gif";
        AltText = "4 stars";
    }
    else if (Stars > 3.25)
    {
        ImageURL = baseURL + "stars3.5.gif";
        AltText = "3.5 stars";
    }
    else if (Stars > 2.75)
    {
        ImageURL = baseURL + "stars3.gif";
        AltText = "3 stars";
    }
    else if (Stars > 2.25)
    {
        ImageURL = baseURL + "stars2.5.gif";
        AltText = "2.5 stars";
    }
    else if (Stars > 1.75)
    {
        ImageURL = baseURL + "stars2.gif";
        AltText = "2 stars";
    }
    else if (Stars > 1.25)
    {
        ImageURL = baseURL + "stars1.5.gif";
        AltText = "1.5 stars";
    }
    else if (Stars > 0.75)
    {
        ImageURL = baseURL + "stars1.gif";
        AltText = "1 star";
    }
    else if (Stars > 0.25)
    {
        ImageURL = baseURL + "stars0.5.gif";
        AltText = "0.5 stars";
    }
    else
    {
        ImageURL = baseURL + "stars0.gif";
        AltText = "0 stars";
    }
    return "<img id='imgRating' border='0' src='" + ImageURL + "' alt='" + AltText + "' style='border-width:0px;' />";
}

function PreviewComment() {
    if (CheckForErrors() == false)
    {
        // errors not found
        var inputArea = document.getElementById("inputSpan");
        var txtComment = document.getElementById("txtComment");
        StoredCommentText = txtComment.value;
        var html = "";
     
        // show the comment area
        html = html + "<div class='inPageCommentArea'>";
        
        // put preview in box
        html = html + "<div class='previewComment'>";
        
        if (AlreadyVoted() == false)
        {
            // get the vote info
            var lstVotes = document.getElementById("lstVotes");
            StoredVote = lstVotes.value;
            
            // show vote preview
            if (StoredVote > 0)
            {
                html = html + "<div class='formRow'>";
                html = html + "<b>Your Rating:</b> ";
                html = html + getImageHTML(StoredVote);
                html = html + "</div>";
            }
        }
        
        // show comment preview
        html = html + "<div class='simpleFormElement'><b>Your Comment</b>";
        var PostButtonLabel;
        if (StoredCommentText == "")
        {
            html = html + "(none)";
            PostButtonLabel = "Rate Song";
        }
        else
        {
            html = html + AddLineBreaks(StoredCommentText);
            PostButtonLabel = "Post Comment";
        }
        html = html + "</div>";
        
        // end preview box
        html = html + "</div>";
        
        html = html + "<div class='simpleFormBtn'>";
        html = html + getButton("Discard", "ShowStartCommentButton", "btn1");
        html = html + getButton("Edit", "ShowCommentInput", "btn1");
        html = html + getButton(PostButtonLabel, "SubmitComment", "btn2");
        html = html + "</div>";        

        // show the comment area
        html = html + "</div>";
        inputArea.innerHTML = html;
    }
}

function SubmitFromEntry()
{
    if (CheckForErrors() == false)
    {
        var txtComment = document.getElementById("txtComment");
        if (AlreadyVoted() == false)
        {
            var lstVotes = document.getElementById("lstVotes");
            StoredVote = lstVotes.value;
        }
        StoredCommentText = txtComment.value;
        SubmitComment();
    }
}

function SubmitComment()
{
    //alert("submit to server, comment='" + StoredCommentText + "', vote='" + StoredVote + "'");
    PostComment()
}

function AlreadyVoted()
{
    var voted = document.getElementById("AlreadyVoted");
    if (voted.value == "true")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function ShowStartCommentButton()
{
    // set the unique ID
    // this is used by server to prevent comment re-submit
    var ran_unrounded=Math.random()*10000000;
    CommentTempID=Math.round(ran_unrounded);  

    var inputArea = document.getElementById("inputSpan");
    
    if (LoggedIn() == false)
    {
        inputArea.innerHTML = "<div class='commentBtnRow'><div class='btn4BoxC'><a href='logreg.aspx' class='btn4'>Post Comment / Rate Song</a></div></div>";
    }
    else
    {
        var Label;
        var BtnClass;
        if (AlreadyVoted() == true)
        {
            Label = "Post Comment";
            BtnClass = "btn2";
        }
        else
        {
            Label = "Post Comment / Rate Song";
            BtnClass = "btn4";
        }
        inputArea.innerHTML = "<div class='commentBtnRow'><div class='" + BtnClass + "BoxC'><a href='javascript:cmdStartComment_onclick()' class='" + BtnClass + "'>" + Label + "</a></div></div>";
    }
    StoredCommentText = "";
    StoredVote = 0;
}

function getButton(Label, Function, BtnClass)
{
    return "<div class='" + BtnClass + "Box'><a class='" + BtnClass + "' href='javascript:" + Function + "()'>" + Label + "</a></div> ";
}
function cmdShowHTML_onclick() {
    var inputArea = document.getElementById("inputSpan");
    alert(inputArea.innerHTML);
}

function setTimer() {
     setTimeout("checkForComments()", 10000); 
}

function checkForComments()
{
    startCommentRequest();
    setTimeout("checkForComments()", 10000);  
}

function startCommentRequest() {
    var oXmlHttp = createXMLHttpRequest();
    var SongID = document.getElementById("SongID").value;
    var ran_unrounded=Math.random()*10000000;
    var ran_number=Math.round(ran_unrounded);  
    var ContextID = document.getElementById("ContextID").value;
    var SponsorMode = document.getElementById("SponsorMode").value;
    oXmlHttp.open("get", "ajax_get_new_comments.aspx?songid=" + SongID + "&rand=" + ran_number + "&SponsorMode=" + SponsorMode + "&contextid=" + ContextID, true);
    oXmlHttp.onreadystatechange = function () 
    {
        if (oXmlHttp.readyState == 4) {
            var RsltElem = document.getElementById("comments_span");
            if (oXmlHttp.responseText.length > 10)
            {
                //alert(oXmlHttp.responseText);
                var responsearray = oXmlHttp.responseText.split("|");
                if (responsearray[0] == "ok")
                {
                    // add the new comments
                    var element = document.createElement('div');
                    element.innerHTML = responsearray[1];
                    if (SuppressFade == false)
                    {
                        element.id = "fadethis";
                    }
                    
                    var RsltElem = document.getElementById("comments_span");
                    if (RsltElem.hasChildNodes() == true)
                    {
                        RsltElem.insertBefore(element, RsltElem.firstChild);
                    }
                    else
                    {
                        RsltElem.appendChild(element);
                    }
                    
                    //element.style.opacity = (0 / 100);
                    //element.style.MozOpacity = (0 / 100);
                    //element.style.KhtmlOpacity = (0 / 100);
                    //element.style.filter = "alpha(opacity=" + 0 + ")";
                    
                    if (SuppressFade == false)
                    {
                        //DoFade(7, "fadethis");
                    }
                    else
                    {
                        // for next time
                        SuppressFade = false;
                    }    
                    //var myEffect = new fx.Opacity(element, {duration: 3000});
                    //myEffect.setOpacity(0);
                    //opacity("fadethis", 0, 100, 2000)
                                        
                    // see if the song stats are new
                    var OldVoteCount = document.getElementById("VoteCount");
                    if (parseInt(OldVoteCount.value) != parseInt(responsearray[3]))
                    {
                        // stats are new
                        var stats = document.getElementById("SongRatingSpan");
                        //alert("before: " + stats.innerHTML);
                        //alert("after: " + responsearray[2]);
                        stats.innerHTML = responsearray[2];
                       // DoFade(7, "SongRatingSpan");
                        
                        // update the old votecount value with the new value
                        OldVoteCount.value = responsearray[3];
                    }
                }
            }
        }
    };
    oXmlHttp.send(null);
}
