﻿//review functions

function newReview(CG, mapID, title, notifyMe)  //also including the map item title so it can be passed back and the MyMap reviews updated
{
    if ((myUser.logged) || (CG == "PD"))   // allow users who receive playdate to reply without being logged in
    {
        $("reviewPaneCG").value = CG
        $("reviewPaneMapID").value = mapID
        $("reviewPaneMapIDTitle").value = unescape(title)
        $("reviewPaneNotifyMe").value = notifyMe
        //check that user hasn't already reviewed the item 
        //PageMethods.newReviewCheck(myUser.memberID, CG, mapID, onNewReviewCheckResult, onNewReviewCheckError);
        cmtID = ""  //in this case we don't know the cmtID yet
        PageMethods.loadReview(myUser.memberID, CG, mapID, cmtID, onLoadReviewResult, onLoadReviewError)        
    }
    else {showLoginJoin()}
}

        function onLoadReviewResult(resultTable)     //returna datatable including: CG, mapID, cmtID, cmtTitle, cmtDescription, cmtRating, cmtNotify, display, email  
        {
            if (resultTable.rows)  //a review was found, we load it up 
            {
                //msgBox("Oops!", "You already entered a review for this item. Please click on the MyMap button to find this review and edit it further.", 300);
                        
                //fill comment fields that are temporarily used to store cmt title, description and rating 
                cmtRef.CG = resultTable.rows[0].CG;        
                cmtRef.id = resultTable.rows[0].cmtID;
                cmtRef.mapID = resultTable.rows[0].mapID;
                
                cmtRef.rating = resultTable.rows[0].cmtRating;     
                rater.Value = resultTable.rows[0].cmtRating ;
                CastleRater.Refresh(rater, "rater")   //set rater value
              
                cmtRef.title = resultTable.rows[0].cmtTitle;
                $("reviewPaneTitle").value = dec(resultTable.rows[0].cmtTitle);
                
                cmtRef.description = resultTable.rows[0].cmtDescription;
                $("reviewPaneDesc").value = dec(resultTable.rows[0].cmtDescription);   
                
                cmtRef.notify = resultTable.rows[0].cmtNotify;
                if (resultTable.rows[0].cmtNotify) 
                {   $("reviewNotifyCB").checked = true;} else { $("reviewNotifyCB").checked = false;}                
                
                //change the submit functionality from new to edit mode
                $("reviewSubmitBut").href = "javascript:submitEditReview();";
                //$("reviewSubmitBut").onclick = function(){submitEditReview();}; 
                
                //case of playdate 
                if (resultTable.rows[0].CG == "PD")  
                {
                    enablePDreview()
                    //if (resultTable.rows[0].display
                    $("reviewPDtitle").innerHTML = "Reply from <span class='postStep'>"+resultTable.rows[0].display+" ("+resultTable.rows[0].email+")</span>";
                    //set previous reply
                    if (resultTable.rows[0].cmtRating != 0)
                    {
                        arr = document.getElementsByName("reviewPDradio")  //select all reply radio options
                        for (i=0;i<arr.length;i++)
                        {
                            if (arr[i].value == resultTable.rows[0].cmtRating)
                            {
                                arr[i].checked = true;
                                break;
                            }
                        }
                    }
                }
                    
                $("reviewPane").style.display = "block";
                
            }
            else    //this is a new review, we display the review pane
            {
                if ($("reviewPaneCG").value == "PD") 
                {
                    enablePDreview()
                }

                $("reviewPane").style.display = "block";
                $("reviewPaneDesc").focus();
            }
        }
        
        function onLoadReviewError()            
        {
            msgBox("Oops!", "Could not load prayer form.", 300);
        }

function addBookmark(CG, mapID, title)
{
    if (myUser.logged)
    {
        title1 = unescape(title)
        //check that user hasn't already reviewed the item 
        PageMethods.addNewBookmark(myUser.memberID, CG, mapID, title1, onAddNewBookmarkResult, onAddNewBookmarkError);
    }
    else {showLoginJoin()}   
}

        function onAddNewBookmarkResult(table)              //returns, CG, mapID, title, CGdesc
        {
            if (table.rows[0].mapID == -1)
            {
                msgBox("Oops!", "You already bookmarked this item. To see the bookmark for this item, please click on the 'My Faithmap' button.", 300);
            }
            else    
            {
                msgBox("Done!", "Bookmark added. To see it, click on the 'My Faithmap' button.", 300);
                
                myUser.favoriteCount += 1;
                if ((App.myMap) && ($("myMapNavBut").className == "activelink"))            //if the myMap code has been downloaded (ie user has already clicked on myMap button and the button is active (this guarantees that it's the user's profile and not another's user
                {
                    try{$("tabberDiv").getElementsByTagName("ul")[0].getElementsByTagName("a")[0].firstChild.nodeValue = "Bookmarks"// ("+myUser.favoriteCount+")"
                    }catch(err){}  //updating review tab title         
                    //getReviews()          // I replaced this with a client update of the myMap page based on the post and review returned by this function to avoid querying the server again

                    //newBookmarkDiv = document.createElement("<div id='divFavorites"+table.rows[0].CG+table.rows[0].mapID+"'>")  not supported by Firefox
                    newBookmarkDiv = document.createElement("div")
                    newBookmarkDiv.id = "divFavorites" + table.rows[0].CG+table.rows[0].mapID
                    
                    str1 = "";   //using str1 instead of str because addMyMapMenu also uses str and the browser gets confused
                    str1 += "<input type='checkbox' name='CBFavorites' id='"+table.rows[0].CG+table.rows[0].mapID+"'/>";
                    str1 += table.rows[0].title+" ("+table.rows[0].CGdesc+")";
                                                          
                    if (($("myMapFavorites").innerHTML == null) || ($("myMapFavorites").innerHTML == ""))   // we also need to add the menu
                    {
                                    addMyMapMenu(myUser.memberID, "myMapFavoritesMenu", "Favorites");
                    }
                    
                    $("myMapFavorites").appendChild(newBookmarkDiv);
                    newBookmarkDiv.innerHTML = str1;  
                      
                }
            }
        }
        
        function onAddNewBookmarkError()            
        {
            msgBox("Oops!", "Could not verify if this item was already bookmarked.", 300);
        }



function submitNewReview()
{
    //perform checks
    if (!validateReviewForm()){return}
    
    disableBut("reviewSubmitBut")
    var boo = $("reviewPaneNotifyMe").value
    PageMethods.newReview(myUser.memberID, myUser.display, $("reviewPaneCG").value, $("reviewPaneMapID").value, $("reviewPaneMapIDTitle").value, $("reviewPaneNotifyMe").value, $("reviewPaneTitle").value.trims(), $("reviewPaneDesc").value.trims(), rater.Value, $("reviewNotifyCB").checked, onNewReviewResult, onNewReviewError)

}

    function onNewReviewResult(resultTable)              //callback function that processes the page method call return value.
    {   
  
        if ($("reviewPaneCG").value == "PD")  //case of playdate
        {
            msgBox("Thank you", "Your reply has been successfully posted! <br/>You can see it by mousing over the map pin.", 300);
        }
        else
        {
            msgBox("Thank you", "Your prayer has been added. <br/>Thank you for praying.<br/>God bless you", 300);                
            myUser.reviewCount += 1;
            if ((App.myMap) && ($("myMapNavBut").className == "activelink"))            //if the myMap code has been downloaded (ie user has already clicked on myMap button and the button is active (this guarantees that it's the user's profile and not another's user
            {
                try{$("tabberDiv").getElementsByTagName("ul")[0].getElementsByTagName("a")[1].firstChild.nodeValue = "Prayer Responses"// ("+myUser.reviewCount+")"
                }catch(err){}  //updating review tab title         
                getReviews(myUser.memberID)          // I should replaced this with a client update of the myMap page based on the post and review returned by this function to avoid querying the server again
                /*
                //newReviewDiv = document.createElement("<div id='divReviews"+resultTable.rows[0].CG+resultTable.rows[0].cmtID+"'>")  not supported by Firefox
                newReviewDiv = document.createElement("div")
                newReviewDiv.id = "divReviews" + resultTable.rows[0].CG+resultTable.rows[0].cmtID
                
                str1 = "";   //using str1 instead of str because addMyMapMenu also uses str and the browser gets confused
                //str += "<div id='divReviews"+resultTable.rows[0].CG+resultTable.rows[0].cmtID+"'>"
                str1 += "<div><input type='checkbox' name='CBReviews' id='"+resultTable.rows[0].CG+resultTable.rows[0].cmtID+"' value='"+resultTable.rows[0].CG+resultTable.rows[0].mapID+"'/>";  //I'm using the value attribute to store the mapID based id (instead of the cmtID based id), ie "PG7" where seven is the mapID, this will be used later for showing the item on the map
                str1 += "My review of "+resultTable.rows[0].title+" ("+resultTable.rows[0].CGdesc+"):</div>";
                str1 += "<div class='cttReview'><div class='mbrBit' id=''></div><div class='rvwBlock'><span id='rvwRating"+i+"'>"+createRating2(1,resultTable.rows[0].cmtRating)+"</span>";
                str1 += "<input id='"+resultTable.rows[0].CG+resultTable.rows[0].cmtID+"rating' style='display:none' value='"+resultTable.rows[0].CG+resultTable.rows[0].cmtRating+"'/>";
                str1 += "<span style='display:none' class='rvwTitle'>"+resultTable.rows[0].cmtTitle+"</span><span class='rvwDesc'>"+resultTable.rows[0].cmtDescription+"</div></div><div class='rvwDate'>"+resultTable.rows[0].cmtDate.format("MMM d, yyyy")+"</span></div>"
                //str += "</div>"

                if (($("myMapReviews").innerHTML == null) || ($("myMapReviews").innerHTML == ""))   // we also need to add the menu
                {
                       addMyMapMenu(myUser.memberID, "myMapReviewsMenu", "Reviews");
                }
                
                $("myMapReviews").appendChild(newReviewDiv);
                newReviewDiv.innerHTML = str1;    */ 
            }
        }
        //need to clear the infobox so it requeries the server and show the prayer response
        var id = $("reviewPaneCG").value + $("reviewPaneMapID").value
        clearInfoBox(id);
         // redisplay the infobox so user can see their prayer response
        var myShape = getShapeByLinkID(id);
        displayInfoBox(myShape);
        
        addCandleImageToPrayerList(id)
        
        enableBut("reviewSubmitBut")
        cancelReview();     //this instruction needs to come last, otherwise the if condition abaove ca not be done since reviewPaneCG will have been cleared.  
      
        //zoom out
        
    }
     
     
    function onNewReviewError()
    {
        enableBut("reviewSubmitBut")
        msgBox("Oops!", "Could not add prayer response.", 300);
    }

function submitEditReview()
{
    //perform checks
    if (!validateReviewForm()){return}

    //detect changes
    
    var changed = false
    var main = Array()
    var mainRef = Array()
    var ratingDelta = 0
    if ($("reviewPaneTitle").value.trims() != cmtRef.title)
    {
        changed = true; 
        mainRef.push("cmtTitle")
        main.push($("reviewPaneTitle").value.trims())
    }
    if ($("reviewPaneDesc").value.trims() != cmtRef.description)
    {
        changed = true; 
        mainRef.push("cmtDescription")
        main.push($("reviewPaneDesc").value.trims())
    }    
    if (rater.Value != cmtRef.rating)
    {
        changed = true; 
        mainRef.push("cmtRating")
        main.push(rater.Value)
        ratingDelta = rater.Value - cmtRef.rating 
    }
    if ($("reviewNotifyCB").checked != cmtRef.notify)
    {
        changed = true; 
        mainRef.push("cmtNotify")
        main.push($("reviewNotifyCB").checked)
    }
        
    if (changed) {PageMethods.updateReview(cmtRef.CG, cmtRef.mapID, cmtRef.id, main, mainRef, ratingDelta, onEditReviewResult, onEditReviewError) }
    else {msgBox("Oops", "No changes have been detected. To close this window, click the close 'X' button.", 300)}
}

        function onEditReviewResult(resultTable)              //callback function that processes the page method call return value.
        {

            if ($("reviewPaneCG").value != "PD")
            {
                msgBox("Thank you", "Your prayer response has been been updated.", 300);
                if ((App.myMap) && ($("myMapNavBut").className == "activelink"))            //if the myMap code has been downloaded (ie user has already clicked on myMap button and the button is active (this guarantees that it's the user's profile and not another's user
                {
                    getReviews(myUser.memberID)          // I should replace this with a client update of the myMap page based on the post and review returned by this function to avoid querying the server again
                    
                    /*     this is used in the onNewReviewResult function
                    newReviewDiv = document.createElement("<div id='divReviews"+resultTable.rows[0].CG+resultTable.rows[0].cmtID+"' class='UIText'>")
                    
                    str = "";
                    str += "<div id='divReviews"+resultTable.rows[0].CG+resultTable.rows[0].cmtID+"' class='UIText'>"
                    str += "<div><input type='checkbox' name='CBReviews' id='"+resultTable.rows[0].CG+resultTable.rows[0].cmtID+"' value='"+resultTable.rows[0].CG+resultTable.rows[0].mapID+"'/>";  //I'm using the value attribute to store the mapID based id (instead of the cmtID based id), ie "PG7" where seven is the mapID, this will be used later for showing the item on the map
                    str += "My review of "+resultTable.rows[0].title+" ("+resultTable.rows[0].CGdesc+"):</div>";
                    str += "<div class='cttReview'><div class='mbrBit' id=''>---></div><div class='rvwBlock'><span id='rvwRating"+i+"'>"+createRating2(1,resultTable.rows[0].cmtRating)+"</span><span class='rvwTitle'>"+resultTable.rows[0].cmtTitle+"</span><div class='rvwDesc'>"+resultTable.rows[0].cmtDescription+"</div></div><div class='rvwDate'>"+resultTable.rows[0].cmtDate+"</div></div>"

                    $("myMapReviews").appendChild(newReviewDiv);
                    newReviewDiv.innerHTML = str;     
                    */
                }
            }
            else  //case of playdate
            {
            msgBox("Done!", "Your reply has been successfully posted! <br/>You can see it by mousing over the map pin.", 300);
            }
            
            //need to clear the infobox so it requeries the server and show the prayer response
            var id = $("reviewPaneCG").value + $("reviewPaneMapID").value
            clearInfoBox(id);
            // redisplay the infobox so user can see their prayer response
            var myShape = getShapeByLinkID(id);
            displayInfoBox(myShape);
            // add a candle to the prayer list item if there isn't one already
            
            
            cancelReview();     //this instruction needs to come last, otherwise the if condition abaove ca not be done since reviewPaneCG will have been cleared.  
            //zoom out
            
        }
         
         
        function onEditReviewError()
        {
            msgBox("Oops!", "Could not update prayer response.", 300);
        }   



function validateReviewForm()
{
    if ($("reviewPaneCG").value == "PD") //case of playdate
    {
        if (rater.Value == 0)
        {
            msgBox("Oops!", "Please check a reply.", 300);
            return false
        }        
    }
    else
    {
        if (rater.Value == 0)
        {
            msgBox("Oops!", "Please light candles by moving your mouse over them.", 300);
            return false
        } 
        
        if (isEmpty($("reviewPaneDesc")))
        {
            msgBox("Oops!", "Please pray and enter supporting words for this prayer request.", 300);
            return false
        }
    }
    return true;
}


function clearReview()
{
    rater.Value = 0
    CastleRater.Refresh(rater, "rater")   //empty rater

    $("reviewPaneTitle").value = "";
    $("reviewPaneDesc").value = "";
    $("reviewPaneCG").value = ""
    $("reviewPaneMapID").value = ""
    $("reviewPaneMapIDTitle").value = ""
    $("reviewPaneNotifyMe").value = ""
    
    $("reviewNotifyCB").checked = false;
    
    $("reviewSubmitBut").href = "javascript:submitNewReview();"; 
    //$("reviewSubmitBut").onclick = function(){submitNewReview();}; 


    $("reviewPane").style.display = "none";
}

function cancelReview()
{

    if ($("reviewPaneCG").value == "PD") //case of playdate
    {
            disablePDreview();   //morph form back from reply mode to review mode
    }  
    
    clearReview()
}

function clearInfoBox(id)
{
    var myShape = getShapeByLinkID(id)
    if (myShape) 
    {
        CustomHideInfoBox(myShape)        
        myShape.SetDescription("")
    }   
}