﻿$(function()
{
    ////////////////////////////
    $('#edit_description').keyup(function(){
        //$('#edit_description').wordCount({size: '4000', counter:'charcounter' });
        limitChars('edit_description', 2000, 'charcounter');
    });
    $('#add_description').keyup(function(){
        //$('#edit_description').wordCount({size: '4000', counter:'charcounter' });
        limitChars('add_description', 2000, 'charcounter');
    });
    ////////////////////////////
    var auth = $("#userId").val();
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  DISPLAY ADD TAG FORM
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('a.AddTags').click(function()
    {
       if(auth != "")
       {
            var id = $(this).attr('id');  
            var tagId = id.substr(4);
            $('#TagForm' + tagId).slideToggle('slow');
            return false;
       }
       else
       {
            alert('You have to be logged in to add tags');
       }
    });

    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  ADD THE TAG ENTERED  - REPLY WITH SUCCESS/FAIL MESSAGE!
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('input.AddPearlTag').click(function()
    {   
        gid = $(this).attr("id");
        id = gid.substr(5);
        var tags = $('#tags' + id).val();
        if(tags != "")
        {
            var data = $("#fmPearlTag"  + id).serialize();
            
            
             if (data == null || data =="")
            {
                var newdata = $("#frmPearls").serialize();
                data = newdata; 
            }
            
              //alert(data);
            
            
            $.ajax
            ({
                type: "POST",
                url: "AddTags.publisha",
                data: data,
                async: false,
            
                success: function(msg)
                {  
                  alert('Your tag was added successfully ');
                  $('#TagForm' + 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;
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  ADD THANK YOU TO MENTOR THANK YOU BANK
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('a.lnkThankU').click(function() 
    {
        var isAuth = 'True';
        if(isAuth == 'True')
        {
            var fullId = $(this).attr("id"); 
            var id = fullId.substr(6);
            var data = $("#fmThank"  + id).serialize(); 
            
            $.ajax
            ({
                type: "POST",
                url: "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;
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  EDIT STORY - DO VALIDATE FORM BEFORE SUBMITTING TEXT
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('#UpdatePearl').click(function() 
    {
        //alert('here');
        var title = $('#edit_userId').val();
        var storyText = $('#edit_description').val();
        var lifeEdit = $('input[@name=lifeEdit]:checked').size()
        var workEdit = $('input[@name=workEdit]:checked').size()
        var learnEdit = $('input[@name=learnEdit]:checked').size()
        if(title != "" && storyText!="")// != "" && tags != "")
        {
            if(lifeEdit!=0 || workEdit!=0 || learnEdit!=0)
                $('#fmEditPearl').submit();
            else
                alert('Please select from Life, Work or Learning');
        }
        else
        {
            alert('All field are mandatory!');
        }
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  EDIT STORY - DO VALIDATE FORM BEFORE SUBMITTING TEXT
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('#AddPearl').click(function()
    {
        var title = $('#add_title').val();
        var storyText = $('#add_description').val();
        var lifeAdd = $('input[@name=lifeAdd]:checked').size()
        var workAdd = $('input[@name=workAdd]:checked').size()
        var learnAdd = $('input[@name=learnAdd]:checked').size()
        if(title != "" && storyText!="")// != "" && tags != "")
        {
           if(lifeAdd!=0 || workAdd!=0 || learnAdd!=0)
                $('#fmAddPearl').submit();
            else
                alert('Please select from Life, Work or Learning');
        }
        else
        {
            alert('All field are mandatory!');
        }
    });
});


/*
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  EDIT STORY - DO VALIDATE FORM BEFORE SUBMITTING TEXT
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('#UpdateStory').click(function()
    {
        var title = $('#editTitle').val();
        var storyText = $('#RichText').val();
        var tags = $('#editTags').val();
        
        if(title != "" && storyText != "" && tags != "")
        {
           $('#fmEditStory').submit();
        }
        else
        {
            alert('All field are mandatory!');
        }
    });
    
    $('#AddNewStory').click(function()
    {
        var title = $('#newTitle').val();
        var storyText = $('#RichText').val();
        var tags = $('#newTags').val();
        
        if(title != "" && storyText != "" && tags != "")
        {
           $('#fmAddStory').submit();
        }
        else
        {
            alert('All field are mandatory!');
        }
    });

*/
function limitChars(textid, limit, infodiv)
{
    var text = $('#'+textid).val();
    var textlength = text.length;
    if(textlength > limit)
    {
        $('#' + infodiv).html('all '+limit+' characters used');
        $('#'+textid).val(text.substr(0,limit));/// <reference path="spell.js" />
        return false;
    }
    else
    {
        $('#' + infodiv).html((limit - textlength) +' characters left.');
        return true;
    }
}
