﻿
$(function() 
{  
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  HIDE/SHOW ADD TAG FORM - First check if user is auth
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::         
    $('#showtaggingfrm').click(function() 
    {
          var isAuth = $('#isAuth').val();
          if(isAuth == 'True')
          {
            $("#fmaddtag").slideToggle('slow');
            return false;
          }
          else
          {          
            alert('You need to be logged in to add tags');      
          }
          return false;
    });
    
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  ADD PROFILE TAG - MUST BE LOGGED IN!!!
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('#AddNewTag').click(function()
    {   
        var tags = $("#tags").val(); 
        var reload = $("#reload").val();
        
        if(tags != "")
        {
            var data = $("#fmTagMentor").serialize(); 
           //salert(data);
            $.ajax
            ({
                type: "POST",
                url: "AddTags.publisha",
                data: data,
                async: false,
                success: function(msg)
                {  
                  alert('Your tag was added successfully');
                  
                  if (reload=="true") location.reload(true);
                  
                  $("#fmaddtag").slideToggle('slow');       
                },
                error: function(msg)
                {
                    alert('something unexpected happened, please try again');
                    return $.jqURL.url();
                }
            });
        }
        else
        {
            alert('You have not entered a tag!');
        }
        
        return false;
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  ADD MENTOR AS A CONTACT - MUST BE LOGGED IN
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('#AddContact').click(function() 
    {
        var isAuth = $('#isAuth').val();
        if(isAuth == 'True')
        {
            var data = $("#fmAddContactMentor").serialize(); 
            
            $.ajax
            ({
                type: "POST",
                url: "AddContact.publisha",
                data: data,
                async: false,
                success: function(msg)
                {  
                   alert('The mentor has been added to your contacts');       
                },
                error: function(msg)
                {
                    alert('Oops - an error has occured!');
                }
            });
        } 
        else
        {
            alert('Please log in to add mentor to contacts!');
        }
        return false;
    });
    
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  CONTACT MENTOR - MUST BE LOGGED IN
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    $('#ContactMentor').click(function() 
    {       

        var isAuth = $('#isAuth').val();
        var menId = $("#menId").val();
        if(isAuth == 'True')
        {
            window.location = '/publisha.content/messages/sendhi.publisha?uid=' + menId; 
        } 
        else
        {
            alert('Please log in to contact the mentor!');
        }
        return false;  
    });  
    
    $('#aInviteMentorToGroup').click(function() 
    {       
        var isAuth = $('#isAuth').val();
        var isMentor = $("#isMentor").val();
       
        if(isAuth == 'True')
        { 
             if(isMentor == 'True')
             {
                 var data = $("#fmInviteMentor").serialize();
          
                $.ajax
                ({
                    type: "POST",
                    url: "/publisha.content/groupList.publisha",
                    data: data,
                    async: false,
                    success: function(msg)
                    {  
                        $('#mentorGroups').html(msg).css('oveflow', 'auto');
                        $('#mentorGroupList').slideToggle('slow');
                    },
                    error: function(msg)
                    {
                        
                    }
                });
            }
            else
            {
                 alert('Only mentors can invite members to their groups!');
            }
        } 
        else
        {
            alert('You have to be logged in to invite the mentor to your group!');
        }
        return false;  
    });  
    
    
    $('#closeButton').click(function() {
      $('#mentorGroupList').slideToggle('slow');
    });


    $('#inviteToGroups').click(function() 
    {
        var data = $("#tab2form").serialize();
        $.ajax
        ({
            type: "POST",
            url: "/publisha.content/groupList.publisha",
            data: data,
            async: false,
            success: function(msg)
            {  
                $('#mentorGroupList').slideToggle('slow');
            },
            error: function(msg)
            {
                
            }
        });
       
    });
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    //  VALIDATE "HI" MESSAGE AND SEND IF PASS VALIDATION
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*$('#sendHiMessage').click(function() 
    {        
        var subject = $('#MessageSubject').val();
        var text = $('#RichText').val();
        
        if(text != "" && subject != null)
        {
            var data = $("#sendHi").serialize();
            
            $.ajax
            ({
                type: "POST",
                url: "send.publisha",
                data: data,
                async: false,
                success: function(msg)
                {  
                   
                },
                error: function(msg)
                {
                    window.location = '/publisha.content/errors/404.publisha';
                }
            });              
        }
        else
        {
            alert('All fields should be completed');
        }
        return false;
    });*/     
});

var bCancel = false;

function processForm(oForm) 
{
    alert('processForm');
    var urlReferrer = document.getElementById("UrlReferrer");
    alert(urlReferrer);
	if (bCancel) {
		window.location = urlReferrer;
		return false;
	}
	return true;
}

function validate_required(field,alerttxt)
{
    with (field)
    {
        if (value==null||value=="")
        {
            alert(alerttxt);return false
        }
        else 
        {
            return true
        }
    }
}

function validate_form(thisform)
{
    with (thisform)
    {
        if (validate_required(MessageSubject,"Subject must be filled out!")==false)
        {
            MessageSubject.focus();
            return false
        }
        if (validate_required(MessageText,"Message must be filled out!")==false)
        {
            MessageText.focus();
            return false
        }
    }
}
