﻿
var ErrorMessageArchived = 0;

function LogError(function_name, error_status, error_message, additional_info) {

    if (ErrorMessageArchived < 4) {
        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/LogError",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + ", function_name: '" + CleanForJSON(function_name) +
            "', error_status: '" + CleanForJSON(error_status) +
        "', error_message: '" + CleanForJSON(error_message) +
        "', additional_info: '" + CleanForJSON(additional_info) + "'}",
            success: function(data) {
                ErrorMessageArchived += 1;
            },
            error: function(xhr, err) {
            if (error_message != null && error_message != "") {
                    LogError(function_name, error_status, "", additional_info);
                }
            }
        });
    }
}

function PrettyUpText(s, callback) {

   
    if (s != null && s.Length != 0) {
    
       $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/PrettyUpText",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{input: '" + CleanForJSON(s) + "'}",
            success: function(data) {
                s = data.d;
                callback(s);
             }
           
         });
    } else
        callback(s);
   
}


function Fill_My_Groups(parent_div_id, group_type, page_type) {

    var s = TurnIntoIDSelector(parent_div_id, false);
    var selected_value = 0;
    var ddl_id = $(s + " .my_groups_ddl").attr("id");
    if (ddl_id != null && ddl_id != "" && $(s + " .my_groups_ddl option").size() == 0) {

        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/My_Groups",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + ", group_type: " + group_type + ", page_type: " + page_type + "}",
            success: function(data) {
                if (data.d != null && data.d.length > 0) {
                    selected_value = Fill_DLL(ddl_id, data.d);
                }
            }
        });
    }

}
function Fill_Sites(parent_div_id) {

    var s = TurnIntoIDSelector(parent_div_id, false);
    var ddl_id = $(s + " .sites_ddl").attr("id");

    
    if (ddl_id != null && ddl_id != "" && $(s + " .sites option").size() == 0) {
        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/GetSites",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + "}",
            success: function(data) {
                if (data.d != null && data.d.length > 0) {
                    selected_value = Fill_DLL(ddl_id, data.d);
                }
            }
        });
    }

}
function SubscribeToNewsletter() {

    var newsletter = $('#ef_chk_newsletter').attr('checked');
    var seminars = $('#ef_chk_seminars').attr('checked');
    var various = $('#ef_chk_Various').attr('checked');

    var full_name = $('#ef_txt_Name').val();
    var email = $('#ef_txt_EmailAddress').val();
    var phone = $('#ef_txt_Phone_Number').val();

    $.ajax({
        type: "POST",
        url: "/misc/CommonWebServices.asmx/SubscribeToNewsletter",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{my_member_id: " + My_Member_ID + ", my_site_id: " + My_Site_ID + ", full_name: '" + CleanForJSON(full_name) +
        "', email: '" + CleanForJSON(email) +
        "', phone: '" + CleanForJSON(phone) +
        "', newsletter: " + newsletter + ", seminars: " + seminars + ", various: " + various + "}",
        success: function(data) {

            if (data.d.HaveError) {
                $('#div_ef_error').show();
                $('#div_ef_error').html('<b>Error:</b> ' + data.d.Message);
            } else {

                $('#div_ef_form').hide();
                $('#div_ef_success').show();

            }
        },
        error: function(xhr, err) {
            $('#div_ef_error').show();
            $('#div_ef_error').html('<b>Sorry, there was a Server Error.</b><br />Please wait 30 seconds and try again. <!-- ' + xhr.responseText + ' -->');
            LogError('SubscribeToNewsletter', xhr.statusText, xhr.responseText, '');
        }
    });

}
function CombinedDivName(content_id, reply_to_comment_id) {
    return content_id + "_" + reply_to_comment_id;
}

function SubmitComment(content_id, reply_to_comment_id, content_type) {

    var c = CombinedDivName(content_id, reply_to_comment_id);

    var post_public = AlwaysReturnBool($('#chk_Post_Public_' + c).attr('checked'));
    var comment = $('#txt_Comment_' + c).val();   // $(".orderInfo[id='" + tabTest + "'"]);
    var subscribe = $('#chk_Subscribe_' + c).attr('checked');
    var reply_to_member_id = AlwaysReturnInt($('#hdn_Reply_To_Member_ID_' + c).val());
    
    var send_copy_to_participant = AlwaysReturnBool($('#chk_assessment_send_email_' + c).attr('checked')); // (1 == AlwaysReturnInt($("input[name='rbl_assessment_" + c + "']:checked").val()));
    var assessment_client_status = AlwaysReturnInt($('#ddl_assessment_status_' + c).val()); 
  
    if ((reply_to_member_id == 0 || reply_to_member_id == null) && DivIsBlock('div_Reply_To_Choice_' + c))
    {
    
        reply_to_member_id = AlwaysReturnInt($("input[name='rb_Reply_To_Member_ID_" + c + "']:checked").val());
        if (reply_to_member_id == 0 && post_public == false)
            {
              $('#div_comment_error_' + c).show();
              $('#div_comment_error_' + c).html('Please select who should receive your private comment'); 
                return false;
            }
    }


    $.ajax({
        type: "POST",
        url: "/misc/CommonWebServices.asmx/SubmitComment",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{" + WS_JSon + ", content_id: " + content_id +
        ", reply_to_comment_id: " + reply_to_comment_id +
        ", reply_to_member_id: " + reply_to_member_id +
        ", content_type: " + content_type + ", comnent: '" + CleanForJSON(comment) + "', public_comment: " + post_public + 
        ", send_copy_to_participant: " + send_copy_to_participant + ", updated_status: " + assessment_client_status + "}",
        success: function(data) {

            if (data.d.HaveError) {
                $('#div_comment_error_' + c).show();
                $('#div_comment_error_' + c).html('<b>Error:</b> ' + data.d.Message);
            } else {
                if (!post_public) {
                    comment += ' &nbsp; <b><span class="MediumText">(private comment)</span></b>';
                }
                if (subscribe) {
                    Subscribe(content_id, content_type, true);
                    comment += '<br /><br />(you have subscribed to this forum)';
                }
                $('#div_comment_form_' + c).html('<span class="ProfileLink"><a href="/profile.aspx?Member_ID=' + My_Member_ID + '">' + My_Username + '</a></span> ' + comment);
                if (!post_public)
                    $('#div_comment_box_' + c).removeClass('CommentBox').addClass('CommentBoxPrivate');

            }
        },
        error: function(xhr, err) {
            $('#div_comment_error_' + c).show();
            $('#div_comment_error_' + c).html('<b>Sorry, there was a Server Error.</b><br />Please wait 30 seconds and try again. <!-- ' + xhr.responseText + ' -->');
            LogError('SubmitComment', xhr.statusText, xhr.responseText, 'content_id: ' + content_id + ' reply_to_comment_id: ' + reply_to_comment_id + ' content_type: ' + content_type + ' comment: ' + comment);
        }
    });

}



function DeleteContent(content_id, content_type) {

    if (confirm("Are you sure you want to delete this?")) {
        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/DeleteContent",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + ", content_id: " + content_id + ", content_type: " + content_type + "}",
            success: function(data) {
                if (data.d.Success) {
                    $('#div_content_' + content_type + '_' + content_id).hide();
                }
                $('#div_content_divider_' + content_type + '_' + content_id).html("<b class=\"ImportantText\">" + data.d.Message + "</b><br /><br />");
            }
        });
    }
}

function DeleteLinkedContent(delete_id, content_id, content_type) {

    if (confirm("Are you sure you want to delete this?")) {
        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/DeleteContent",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + ", content_id: " + delete_id + ", content_type: " + content_type + "}",
            success: function(data) {
                if (data.d.Success) {
                    $('#div_content_' + content_type + '_' + content_id).hide();
                }
                $('#div_content_divider_' + content_type + '_' + content_id).html("<b class=\"ImportantText\">" + data.d.Message + "</b><br /><br />");
            }
        });
    }
}


function DeleteComment(comment_id, content_type) {

    if (confirm("Are you sure you want to delete this comment?")) {
        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/DeleteComment",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + ", comment_id: " + comment_id + ", content_type: " + content_type + "}",
            success: function(data) {
                if (data.d.Success) {
                    $('#div_comment_' + comment_id).hide();
                } else
                    $('#div_comment_info_' + comment_id).append("<br /><br /><b class=\"ImportantText\">" + data.d.Message + "</b><br /><br />");
            }
        });
    }
}



function DeleteMilestone(goal_id, milestone_id) {

    if (confirm("Are you sure you want to delete this milestone?")) {
        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/DeleteMilestone",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + ", goal_id: " + goal_id + ", milestone_id: " + milestone_id + "}",
            success: function(data) {
                if (data.d.Success) {
                    $('#div_milestone_' + goal_id + '_' + milestone_id).hide();
                }
            }
        });
    }
}

function MilestoneComplete(goal_id, milestone_id) {
    if (confirm("Are you sure you want to set this milestone to complete?")) {
        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/MilestoneComplete",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + ", goal_id: " + goal_id + ", milestone_id: " + milestone_id + "}",
            success: function(data) {
                if (data.d) {
                    $('#div_m_status_' + goal_id + '_' + milestone_id).html('Completed Today');
                    $('#img_m_status_' + goal_id + '_' + milestone_id).hide();
                }
            }
        });
    }
}

function SubmitVote(content_id, content_type, isComment, give_vote) {

    $.ajax({
        type: "POST",
        url: "/misc/CommonWebServices.asmx/SubmitVote",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{my_member_id: " + My_Member_ID + ", content_id: " + content_id + ", content_type: " + content_type + ", is_comment: " + isComment + ", give_vote: " + give_vote + "}",
        success: function(data) {
            var s = "";
            if (!give_vote)
                s = "<a href=\"javascript:SubmitVote(" + content_id + "," + content_type + ", " + isComment + ", true);\">" + ((isComment) ? "Agree" : "Favorite") + "</a>";
            else
                s = "<a href=\"javascript:SubmitVote(" + content_id + "," + content_type + ", " + isComment + ", false);\">" + ((isComment) ? "Un-Agree" : "Un-Favorite") + "</a>";

            if (data.d > 0)
                s += " <span class=\"VoteCount\">(" + data.d + ")</span> ";


            $('#div_vote_' + content_type + '_' + content_id + '_' + ((isComment) ? '1' : '0')).html(s);


        }
    });
}

function Subscribe(content_id, content_type, give_subscription) {
   
    $.ajax({
        type: "POST",
        url: "/misc/CommonWebServices.asmx/Subscribe",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{my_member_id: " + My_Member_ID + ", content_id: " + content_id + ", content_type: " + content_type + ", give_subscription: " + give_subscription + "}",
        success: function(data) {

            if (data.d) {
                if (give_subscription)
                    $('#div_subscribe_' + content_id).html('<b>Subscribed!</b>');
                else
                    $('#div_subscribe_' + content_id).html('<b>Un-Subscribed!</b>');
            }
        }
    });

}

function ModifyGroupAlerts(div_parent_id, group_type, group_id) {

    $("#" + div_parent_id + " .notify_message").hide();

    notify_schedule = AlwaysReturnInt($("#" + div_parent_id + " .notify_schedule:checked").val());
    notify_destination = AlwaysReturnInt($("#" + div_parent_id + " .notify_destination:checked").val());
    if ((My_Member_ID != 0 && group_id != 0 && group_type != 0) && (notify_schedule > 0 || notify_destination > 0)) {
        $.ajax({
            type: "POST",
            url: "/misc/CommonWebServices.asmx/ModifyGroupAlerts",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{" + WS_JSon + ", group_type: " + group_type + ", group_id: " + group_id + ", notify_schedule: " + notify_schedule + ", notify_destination: " + notify_destination + "}",
            success: function(data) {
                $("#" + div_parent_id + " .notify_message").show();
            }
        });
    }


}
function StarEmail(content_type, conversation_id) {


    is_starred = $("#star_" + conversation_id + " img").hasClass("Starred");
    $.ajax({
        type: "POST",
        url: "/misc/Email.asmx/Starred",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{" + WS_JSon + ", content_type: " + content_type + ", conversation_id: " + conversation_id + ", starred: " + (!is_starred) + "}",
        success: function(data) {
            if (is_starred) {
                $("#star_" + conversation_id + " img").removeClass("Starred");
                $("#star_" + conversation_id + " img").addClass("UnStarred");
            } else {
                $("#star_" + conversation_id + " img").removeClass("UnStarred");
                $("#star_" + conversation_id + " img").addClass("Starred");
            }
        }
    });
}

function DeleteEmail(content_type, conversation_id, current_folder) {
    $.ajax({
        type: "POST",
        url: "/misc/Email.asmx/DeleteEmail",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{" + WS_JSon + ", content_type: " + content_type + ", conversation_id: " + conversation_id + ", current_folder: " + current_folder + "}",
        success: function(data) {
            if (data.d) {
                $('#div_Email_' + conversation_id).hide();
            }
        }
    });

}

function DeleteMedia(photo_type, member_id, media_id) {
    $.ajax({
        type: "POST",
        url: "/misc/CommonWebServices.asmx/DeleteMedia",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{" + WS_JSon + 
        ", photo_type: " + photo_type + ", member_id: " + member_id + ", media_id: " + media_id + "}",
        success: function(data) {
            if (data.d) {
                $('#div_Media_' + photo_type + '_' + member_id + '_' + media_id).hide();
            }
        }
    });

}


var _autosave_id = 0;
var _autosave_txt_client_id = "";


function AutoSave(content_type, is_comment, custom_id, from_user) {

    if (_autosave_txt_client_id != "") {

        var content = $('#' + _autosave_txt_client_id).val();


        if (content != null && content != "") {


            $.ajax({
                type: "POST",
                url: "/misc/CommonWebServices.asmx/AutoSave",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: "{autosave_id: " + _autosave_id +
        ", my_member_id: " + My_Member_ID +
        ", content_type: " + content_type +
        ", is_comment: " + is_comment +
        ", custom_id: " + custom_id +
        ", text: '" + CleanForJSON(content) + "'}",
                success: function(data) {

                    if (data.d > 0) {
                        _autosave_id = data.d;
                        var s = '#div_autosave_' + content_type + '_' + BooleanToInteger(is_comment) + '_' + custom_id;
                        $(s + " .UpdateTime").show();
                        $(s + " .UpdateTime").html('last save on ' + dateFormat("mediumTime") + '');
                    }
                }
            });
        }
        if (from_user == false)
            var s1 = setTimeout("AutoSave(" + content_type + "," + is_comment + "," + custom_id + ")", 30 * 1000);
    }
}

function DeleteAutoSave(content_type, is_comment, custom_id, autosave) {


    $.ajax({
        type: "POST",
        url: "/misc/CommonWebServices.asmx/DeleteAutoSave",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{autosave_id: " + _autosave_id +
        ", my_member_id: " + My_Member_ID +
        ", content_type: " + content_type +
        ", is_comment: " + is_comment +
        ", custom_id: " + custom_id + "}",
        success: function(data) {

            $('#' + _autosave_txt_client_id).val('');


            // _autosave_id = 0;
            var s = '#div_autosave_' + content_type + '_' + BooleanToInteger(is_comment) + '_' + custom_id;
            $(s + " .Recovered").hide();

            if (autosave == _autosave_id)
                _autosave_id = 0;
        }
    });

}

var current_goal_id = 0;

function ShowMilestoneFormTime() {
    $('#div_New_Milestone_' + current_goal_id + ' .div_Milestone_Time_Sensitive').toggle();
    // $("#div_New_Milestone_" + current_goal_id + ' .txt_Milestone_Completion_Date').datepicker({ dateFormat: 'DD, d MM, yy' });

}

function SubmitMilestone() {



    var s = '#div_New_Milestone_' + current_goal_id + ' ';



    var milestone_id = AlwaysReturnInt($(s + ".milestone_form_id").val());
    var complete_milestone = AlwaysReturnBool($(s + ".chk_Milestone_Completed").is(':checked'));

    var description = $(s + ".txt_Milestone_Description").val();
    var time_sensitive = AlwaysReturnBool($(s + ".chk_Milestone_Time_Sensitive").is(':checked'));
    var completion_date = $(s + ".txt_Milestone_Completion_Date").val();
    var email_alert_in_day_1 = 0;




    if (AlwaysReturnBool($(s + ".chk_Milestone_Email_Alert_1").is(':checked'))) {
        email_alert_in_day_1 = AlwaysReturnInt($(s + ".ddl_Email_Alert_1").val());
    }


    $(s + '.WarningMessage').hide();



    $.ajax({
        type: "POST",
        url: "/misc/Journals.asmx/SubmitMilestone",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{" + WS_JSon + ", goal_id: " + current_goal_id +
", milestone_id: " + milestone_id +
", description: '" + CleanForJSON(description) + "'" +
", completed: " + complete_milestone +
", time_sensitive: " + time_sensitive +
", completion_date: '" + CleanForJSON(completion_date) + "'" +
", email_alert_in_day_1: " + email_alert_in_day_1 + " }",
        success: function(data) {

            if (data.d.HaveError) {
                $(s + '.WarningMessage').show();
                $(s + '.WarningMessage').html('<b>Error:</b> ' + data.d.Message);
            } else {


                $("#div_New_Milestone_Button_" + current_goal_id).show();
                $("#div_New_Milestone_" + current_goal_id).html('');
                $("#div_Milestones_List_" + current_goal_id).show();

                var row_inner = '<td></td><td nowrap="nowrap" valign="top" class="Inner"><a href="javascript:SetUpMilestone(' +
                current_goal_id + ',' + data.d.Custom_ID + ');\">edit</a></td><td valign="top" width="100%" class="Inner">' +
                description + '</td><td></td><td></td><td nowrap="nowrap" class="Inner"><a href="javascript:DeleteMilestone(' + current_goal_id + ',' + data.d.Custom_ID + ');"><img alt="Delete" class="Delete" src="/d.gif"></a></td>';

                if (milestone_id != 0) {
                    $("#div_milestone_" + current_goal_id + "_" + milestone_id).html(row_inner);
                } else {
                    $("#table_Milestones_List_" + current_goal_id).append('<tr id="div_milestone_' + current_goal_id + '_' + data.d.Custom_ID + '">' + row_inner + '</tr>');
                }
                current_goal_id = 0;
            }
        },
        error: function(xhr, err) {
            $(s + '.WarningMessage').show();
            $(s + '.WarningMessage').html('<b>Sorry, there was a Server Error.</b><br />Please wait 30 seconds and try again. <!-- ' + xhr.responseText + ' -->');
            LogError('SubmitMilestone', xhr.statusText, xhr.responseText, 'goal_id: ' + current_goal_id + ', milestone_id: ' + milestone_id + ', description: ' + description);
        }
    });



}
function Assessment_SendMessage(survey_taken_id) {

if (survey_taken_id > 0) {
      $.ajax({
                type: "POST",
                url: "/misc/email.asmx/Assessment_SendMessage",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: "{" + WS_JSon + ", survey_taken_id: " + survey_taken_id + "}",
                success: function(data) {

                    if (data.d != null && data.d != "") {
                       document.location.href = data.d;
                    }
                }
            });
}

}

function SetUpMilestone(goal_id, milestone_id) {

    if (goal_id > 0) {

        if (current_goal_id != 0) {
            $("#div_New_Milestone_Button_" + current_goal_id).show();
            $("#div_New_Milestone_" + current_goal_id).html('');
        }

        current_goal_id = goal_id;

        var s = '#div_New_Milestone_' + goal_id + ' ';

        $("#div_New_Milestone_Button_" + goal_id).hide();
        $("#div_New_Milestone_" + goal_id).html('');
        $("#div_New_Milestone_" + goal_id).append($('#div_milestone_form_container').html());


        $(s + ".milestone_form_goal_id").val(goal_id);

        if (milestone_id > 0) {

            $.ajax({
                type: "POST",
                url: "/misc/Journals.asmx/GetMilestone",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: "{" + WS_JSon + ", goal_id: " + goal_id +
", milestone_id: " + milestone_id + "}",
                success: function(data) {

                    if (data.d.Milestone_ID > 0) {
                        $(s + ".milestone_form_id").val(milestone_id);
                        $(s + ".div_milestone_complete").show();
                        $(s + ".chk_Milestone_Completed").attr('checked', data.d.Completed);

                        $(s + ".txt_Milestone_Description").val(data.d.Description);
                        $(s + ".chk_Milestone_Time_Sensitive").attr('checked', data.d.Time_Sensitive);
                        if (data.d.Time_Sensitive)
                            $(s + ".div_Milestone_Time_Sensitive").show();
                        else
                            $(s + ".div_Milestone_Time_Sensitive").hide();

                        $(s + ".txt_Milestone_Completion_Date").val(data.d.Completion_Date);


                        $(s + ".chk_Milestone_Email_Alert_1").attr('checked', (data.d.Email_Alert_in_Day_1 > 0));
                        $(s + ".ddl_Email_Alert_1").val(data.d.Email_Alert_in_Day_1);

                    } else
                        milestone_id = 0;
                },
                error: function(xhr, err) {
                LogError('SetUpMilestone', xhr.statusText, xhr.responseText, 'goal_id: ' + goal_id + ', milestone_id: ' + milestone_id);
                }                
            });

        }
        $(s + ".txt_Milestone_Completion_Date").attr('id', 'mcd' + Math.random());
        $(s + ".txt_Milestone_Completion_Date").datepicker({ dateFormat: 'DD, d MM, yy' });

        if (milestone_id == 0) {
            $(s + ".milestone_form_id").val(0);
            $(s + ".div_milestone_complete").hide();
            $(s + ".chk_Milestone_Completed").attr('checked', false);

            $(s + ".txt_Milestone_Description").val("");
            $(s + ".chk_Milestone_Time_Sensitive").attr('checked', false);
            $(s + ".div_Milestone_Time_Sensitive").hide();

            $(s + ".txt_Milestone_Completion_Date").val('');
            $(s + ".chk_Milestone_Email_Alert_1").attr('checked', true);
            $(s + ".ddl_Email_Alert_1").val(5);
        }

    } else
        current_goal_id = 0;
}













function Set_Up_Page(parent_div_name) {


    var s = TurnIntoIDSelector(parent_div_name, true);
    if (parent_div_name != null && parent_div_name.length > 2) {
        JS_Already_Loaded = true;
        tb_init(s + 'a.thickbox'); //pass where to apply thickbox

    }


    Set_Up_Comment_Form(parent_div_name);
    Set_Up_Content_Form(parent_div_name);
    Set_Up_LookUpMember(parent_div_name);
    Set_Up_PopUpLinks(parent_div_name);
}
function Set_Up_Comment_Form(parent_div_name) {

    var s = TurnIntoIDSelector(parent_div_name, true);
    $(s + 'textarea.CommentBoxy').focus(function() {

        s = $(this).attr('id').replace("txt_Comment_", "").split("_");
        // s[0] = content_id, s[1] = reply_to_comment_id
        Show_Comment_Submit(s[0], s[1]); // $(this).attr('title'));
        $(this).height(80);

    });
    //$(s + 'textarea.CommentBoxy').autoResize();

}


function Set_Up_Content_Form(parent_div_name) {

    var s = TurnIntoIDSelector(parent_div_name, true);

    $(s + 'textarea.ContentBoxy').focus(function() {
        $(s + ' .div_content_form').show();
        $(this).height(150);
    });

    // $(s + 'textarea.ContentBoxy').autoResize();

}

function Set_Up_PopUpLinks(parent_div_name) {

    if (isPopUp) {
        var s = TurnIntoIDSelector(parent_div_name, true);
        // GET ALL THE LINKS THAT ARE NOT TO A MEMBER
        // OPEN THEM IN FRESH WINDOWS

        $(s + "a:not(.thickbox)").not("[href^='/profile.aspx']").not("[href*='/ajax/']").not("[href*='javascript']").attr('target', '_blank');

        // ADD A POP TO ALL THE MEMBER LINKS
        $(s + "a[href^='/profile.aspx']").attr('href', function() {
            return this.href.replace("/profile.aspx", "/pop/profile.aspx");
        });


    }
}


function Set_Up_LookUpMember(parent_div_name) {
   

    var s = TurnIntoIDSelector(parent_div_name, true);

    $(s + 'input.LookUpMember').autocomplete({
        source: function(request, response) {
        
            $.ajax({
                type: "POST",
                url: "/misc/CommonWebServices.asmx/GetMembers",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: "{" + WS_JSon + ", filter: '" + CleanForJSON(request.term) + "'}",
                success: function(data) {

                    response($.map(data.d, function(item) {

                        return {
                            label: item.Last_Name ? (item.Username + " <span class=\"SmallLightText\">(" + item.First_Name + " " + item.Last_Name + ")</span>") : item.Username,
                            value: item.Username
                        }
                    }))
                }
            })
        },
        minLength: 2,
        select: function(event, ui) {
            // log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected, input was " + this.value);
        },
        open: function() {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function() {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });
  

}

/*
function Set_Up_LookUpGroup(parent_div_name) {


    var s = TurnIntoIDSelector(parent_div_name, true);

    $(s + 'input.LookUpGroup').autocomplete({
        source: function(request, response) {
            $.ajax({
                type: "POST",
                url: "/misc/CommonWebServices.asmx/GetGroups",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: "{" + WS_JSon + ", filter: '" + CleanForJSON(request.term) + "'}",
                success: function(data) {

                    response($.map(data.d, function(item) {

                        return {
                            label: item.GroupName,
                            value: item.GroupName
                        }
                    }))
                }
            })
        },
        minLength: 2,
        select: function(event, ui) {
            // log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected, input was " + this.value);
        },
        open: function() {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function() {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });


}
*/


$(document).ready(function() {

$('.MemberMenu TD, .PageMenu TD').mouseover(function() {
        $(this).addClass('Activated');
        $(this).css('cursor', 'pointer');
        var mdiv = $(this).attr('id');
        if (mdiv != null) { $('#d_' + mdiv).addClass('Activated'); }

    });

    $('.MemberMenu TD, .PageMenu TD').click(function() {
        var mdiv = $(this).attr('id');
        if (mdiv != null) {
            var href = $('#' + mdiv + ' a').attr('href');
            if (href != null) {

                var target_win = $('#' + mdiv + ' a').attr('target');
                if (target_win != null && target_win != "") {
                    popTab(href);
                } else
                    self.document.location.href = href;

            }
        }
    });
    $('.MemberMenu TD, .PageMenu TD').mouseout(function() {
        $(this).removeClass('Activated');
        var mdiv = $(this).attr('id');
        if (mdiv != null) { $('#d_' + mdiv).removeClass('Activated'); }
    });

});


function Set_Selected_Menu(menu_id) {

    var parent_td = $('.MemberMenu a.[rel=' + menu_id + ']').parent();
    var parent_td2 = $('.PageMenu a.[rel=' + menu_id + ']').parent();
    
    if (parent_td != null) {
        parent_td.addClass('Current');
        var mdiv = $(parent_td).attr('id');
        if (mdiv != null) { $('#d_' + mdiv).addClass('Current'); }
    }
    if (parent_td2 != null) {
        parent_td2.addClass('Current');
        var mdiv = $(parent_td2).attr('id');
        if (mdiv != null) { $('#d_' + mdiv).addClass('Current'); }
    }
    
}
