﻿
$(function()
{   
    ////////////////////////////
    $('textarea.charcounter').keyup(function(){limitCharsComment(this, 1000, 'charcounter')});
    ////////////////////////////
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   //   CHECK WHETHER THE USER IS LOGGED IN BEFORE REQUESTING MEMBERSHIP
   //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    $('#ReqMem').click(function() 
    {
        var isAuth = $('#isAuth').val();
        if(isAuth == 'True')
        {
                
        }
        else
        {
            alert('You have to be logged in to request membership!')
        }
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //   CHECK WHETHER THE USER IS LOGGED IN BEFORE REQUESTING MEMBERSHIP
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    $('a.AddTags').click(function() 
    {
        var isAuth = $('#isAuth').val();
        var isMember = $('#isMember').val();
        var linkId = $(this).attr("id");   
        var id = linkId.substr(2);
          
        if(isAuth == 'True')
        {
            if(isMember == 'True')
            {
                $('#PGTags' + id).slideToggle(500);
            }
            else
            {
                alert('You have to be a member first before you can add tags');
            }
        }
        else
        {
            alert('You have to be logged in to add tags!');
        }
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  ADD THE TAG ENTERED  - REPLY WITH SUCCESS/FAIL MESSAGE!
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('input.AddPGTag').click(function()
    {   
        var gid = $(this).attr("id");
        var id = gid.substr(6);
        var tags = $('#tags' + id).val();
        
        if(tags != "")
        {
            var data = $("#fmPubGrpTag"  + id).serialize();
            
            $.ajax
            ({
                type: "POST",
                url: "/publisha.content/groups/AddTags.publisha",
                data: data,
                async: false,
            
                success: function(msg)
                {  
                  $('#PGTags' + id).slideToggle('slow');     
                },
                error: function(msg)
                {
                    alert('Oops - your tag was either blocked or a duplicate!');
                }
            });
        }
        else
        {
            alert('Please enter the tag you wish to add!');
        }        
        return false;
    });
    
    $('#ViewPublicGroups').click(function() 
    {
         OSOYouAlert('HORSESMOUTH Message', 'This is a test button that was clicked!'); 
         return false;
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  MENTEE - REQUEST MEMBERSHIP TO A MENTOR GROUP
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('a.request').click(function()
    {
        var auth = $("#isAuth").val();
        if(auth == "True")
        {
            gid = $(this).attr("id"); 
            id = gid.substr(3);
            var data = $("#ReqMembership"  + id ).serialize();           
            $.ajax
            ({
                type: "POST",
                url: "/publisha.content/groups/requestMembership.publisha?postRequest=t",
                data: data,
                async: false,
                success: function(msg)
                {  
                   alert('Your request for membership was sent.');
                },
                error: function(msg)
                {
                   alert('error: ' + msg);
                }
            });
        }
        else
        {
            alert('You have to be logged in to request membership.');
        }
        
        return false;
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    // MENTEE -  TOGGLE ADD COMMENT BOX
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('a.nopaddingleft').click(function()
    {   
       
        groupId = $(this).attr("id"); 
        var  id = groupId.substr(7);
         $('#postComment' + id).slideToggle('slow');
        return false;
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  MENTEE - ADD COMMENT TO GROUP
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('a.commentPost').click(function()
    {   
        gid = $(this).attr("id"); 
        id = gid.substr(2);
        var commentTitle = $('#titleGrp' + id).val();       
        var commentText = $('#commentgrp' + id).val();        

        if(commentTitle != "" || commentText != "")
        {
            var data = $("#fmPostComment"  + id).serialize(); 

            $.ajax
            ({
                type: "POST",
                url: "/publisha.content/groups/addComment.publisha",
                data: data,
                async: false,
                success: function(msg)
                {  
                   $('#postComment' + id).slideToggle();
                   
                   
                   var UpdateStatus=($(msg).text());
                       
                         if(UpdateStatus != "ok")
                       {
                          alert("Your comment was rejected because \n"+ UpdateStatus +"\nPlease try again");
                       }
                       else
                       {
                            alert("Thank you for adding your comment");
                            
                            location.reload(true);
                       }
                            
                        
                        return false;
                   
                   

                },
                error: function(msg)
                {
                   alert('Oops - you comment didnt pass our filters!');
                }
            });
        }
        else
        {
            alert('title and comment are both mandatory!');
        }      
        return false;
    });
    
    $('a.ThMentor').click(function() 
    {
        var isAuth = $('#isAuth').val();
        alert(isAuth);
        
        if(isAuth == 'True')
        {
            var fullId = $(this).attr("id"); 
            
            var id = fullId.substr(5);
           
            var data = $("#fmThank"+id).serialize(); 
          
            
            $.ajax
            ({
                type: "POST",
                url: "/publisha.content/profile/ThankMentor.publisha",
                data: data,
                async: false,
                success: function(msg)
                {  
                    alert('Your thank you has been added to the mentor "thank you bank"');
                },
                error: function(msg)
                {
                   alert('Ooops! Your thank you was not recorded! Please try again!');
                }
            });
        }
        else
        {
            alert("You have to be logged in to thank a mentor.");
        }

      return false;
    });


//function to delete commments

        $('.dellink>a').click(function()
        {
            confirmed = confirm("are you sure, you want to delete this comment?");
    
            if (confirmed)
            {
                return true;
            }
            
            return false;
            
        });
    
});



//function to edit comments

function editcomment(gid,mid)
{
     var mesageid = mid;
     var groupid = gid;
     var containerCommentTitle = $('#'+mid).find('.historysubject');
     var containerCommentMessage = $('#'+mid).find('.historymessage');
         
     var OriginalcontainerCommentTitle   = containerCommentTitle.text();
     var OriginalcontainerCommentMessage = containerCommentMessage.text();
     
     var editform = $('#editcomment'+mesageid);
     
     containerCommentTitle.hide();
     containerCommentMessage.hide();
     
     editform.show();  
     
    
   
}


function canceledit(mid)
{
     var mesageid = mid;
     var containerCommentTitle = $('#'+mid).find('.historysubject');
     var containerCommentMessage = $('#'+mid).find('.historymessage');
     var editform = $('#editcomment'+mesageid);
     
     containerCommentTitle.show();
     containerCommentMessage.show();
     
     editform.hide();  
        
}


function saveedit(mid,gid,uid)
{
    var newtitle = $('#editcomenttitle'+mid).val();
    var newbody  = $('#editcommentbody'+mid).val();
    var editform = $('#editcomment'+mid);
    var containerCommentTitle = $('#'+mid).find('.historysubject');
    var containerCommentMessage = $('#'+mid).find('.historymessage');
    
    var data = "cid="+mid+"&gid="+gid+"&uid="+uid+"&title="+newtitle+"&comment="+newbody+"&visible=true&ispublic=true";
     
    
     $.ajax   ({
                    type: "POST",
                    url: "/publisha.content/groups/UpdateComment.publisha",
                    data: data ,
                    async: false,
                    success: function(msg)
                    {  
                       var UpdateStatus=($(msg).text());
                       
                         if(UpdateStatus != "ok")
                       {
                          alert("Your comment was rejected because \n"+ UpdateStatus +"\nPlease try again");
                       }
                       else
                       {
                            alert("Thank you for updating your comment");
                            containerCommentTitle.text(newtitle);
                            containerCommentMessage.text(newbody);
                            editform.hide();
                            containerCommentTitle.show();   
                            containerCommentMessage.show();
                       }
                            
                        
                        return false;
                    },
                    error: function(msg)
                    {
                     alert('Something unexpected happened, please try again.');
                    }
                });
    
    
}

function limitCharsComment(objtextbox, limit, infodiv)
{
    var groupid=objtextbox.id.replace('commentgrp','');
    var text = $('#'+objtextbox.id).val();
    var textlength = text.length;
    if(textlength > limit)
    {
        $('#' + infodiv+groupid).html('all '+limit+' characters used');
        $('#'+textid).val(text.substr(0,limit));/// <reference path="spell.js" />
        return false;
    }
    else
    {
        $('#' + infodiv+groupid).html((limit - textlength) +' characters left.');
        return true;
    }
}