//
// Displays form for adding / editing forums_list form.
//
function display_add_block_forums_list()
{
    set_form_field('form_forums_list', FLD_BLOCK_ID, '');
    set_form_field('form_forums_list', 'action', 'ajax_add_block_forums_list');
                    
    $('#button_del_forums_list').hide();
}

function display_edit_block_forums_list(block_id)
{
    set_form_field('form_forums_list', FLD_BLOCK_ID, block_id);
    set_form_field('form_forums_list', 'action', 'ajax_update_block_forums_list');
    set_form_field('form_forums_list', 'title', $('#mblock_title_'+block_id).html());
                    
    $('#button_del_forums_list').show();
}

function send_edit_block_forums_list()
{
    var title = get_form_field('form_forums_list', 'title');
    var block_id = get_form_field('form_forums_list', FLD_BLOCK_ID);
    
    if (title == '')
    {
        my_alert('all field are must');
        return false;
    }

    var is_add_action = get_form_field('form_forums_list', 'action') == 'ajax_add_block_forums_list';
    
    var data = $("#form_forums_list").serialize();
    
    var code = is_add_action ? "display_media_blocks_sw_mode(%%%);" : "update_block_title("+block_id+", %%%.title);";
    code += "close_modals();"
    code += "$('#am_btn_forums_list').hide();";

    ajax(data, code, "", "button_forums_list", "saving_forums_list");

    return false;
}

//
// Deletes forums_list block.
//
function del_block_forums_list()
{
    document.forms['form_forums_list'].action.value = 'ajax_del_block_forums_list';
    var block_id = get_form_field('form_forums_list', FLD_BLOCK_ID);
    var data = $('#form_forums_list').serialize();
    var code = "hide_block("+block_id+");";
    code += "close_modals();"
    code += "$('#am_btn_forums_list').show();";
    ajax(data, code, '', 'button_del_forums_list', 'saving_del_forums_list');

    return false;
}

//
// Generates HTML of forums list block.
//
function gen_html_block_forums_list(block_id, forums_json)
{
    var html = '';
    var ancestor_id, ancestor_title, page_id, page_lib_name, forum_id, forum_title, is_locked, page_url;
    
    var former_ancestor_id = 0;
    var former_is_locked = -1;
    var forums = is_json(forums_json) ? jQuery.parseJSON(forums_json) : '';
    
    if (forums == null)
    {
        return '<p>'+translate('There are not yet any public forums in this site')+'.</p>';
    }
    
    var num_forums = forums.length;
    var do_disp_titles = num_forums > 8; 
    
    html += '<form class="no_labels" id="form_'+block_id+'" name="form_'+block_id+'" method="post" action="'+INDEX_URL+'">'
    
    html += '<input type="hidden" name="'+FLD_ACTION+'" value="unid_self_join_forum_mls" />';
    html += '<input type="hidden" name="'+FLD_TOKEN+'" value="'+get_current_token()+'" />';
    html += '<input type="hidden" name="'+FLD_PAGE_ID+'" value="'+PAGE_ID+'" />';
    html += '<input type="hidden" name="locali_page_pwd" value="" />';
    
    html += '<div style="clear:both; float: none;">';

        html += '<p>'+translate('You could receive a copy of every new message. Mark the desired forums / bulletin boards and fill in this form')+':</p>';
        
        html += '<div class="form_item">'; 
        html += '<p class="field">'; 
        html += '<input type="text" name="name" value="'+translate('name')+'" alt="must" onfocus="$(\'#msg_'+block_id+'\').html(\'\'); clear_field(this, \''+translate('name')+'\');" onblur="reset_field(this, \''+translate('name')+'\');" />';
        html += '</p>'; 
        html += '</div>'; 
        
        html += '<div class="form_item">'; 
        html += '<p class="field">'; 
        html += '<input type="email" name="email" value="'+translate('email')+'" class="ltr" alt="must" onfocus="$(\'#msg_'+block_id+'\').html(\'\'); clear_field(this, \''+translate('email')+'\');" onblur="reset_field(this, \''+translate('email')+'\');" />'; 
        html += '</p>';
        html += '</div>'; 

        html += '<p>'; 
        html += '<button id="button_'+block_id+'" onclick="join_forum_mls('+block_id+'); return false;">'+translate('Send')+'</button>'; 
        html += '<img id="saving_'+block_id+'" class="image_saving hidden" src="'+CSS_IMAGE_SAVING+'" />';
        html += '</p>'; 
        
        html += '&nbsp;'; 

    html += '</div>'; 

    //html += '<p>'+translate('To switch to a bulletin board, click its title')+':</p>'; 

    html += '<div style="clear: both; padding: 0 10px 30px 10px;" id="forums_list">';
     
        /*html += '<p>'; 
        html += '<input type="checkbox" class="cb" name="main_ml" id="fml_0" value="1" onclick="highlight_selected_forum_mls('+block_id+');" />';
        html += '<label for="fml_0"><span class="title">'+translate('Main mailing list of the site')+'</spab></label>';
        html += '</p>';*/ 


        for (var i=0; i<forums.length; i++)
        {
            forum = forums[i];
            
            ancestor_id = forum['ancestor_id'];
            ancestor_title = decode_from_json(forum['ancestor_title']);
            page_id = forum['page_public_id'];
            page_lib_name = forum['page_lib_name']; 
            forum_id = forum['forum_id'];
            is_locked = forum['is_locked'];
            forum_title = decode_from_json(forum['forum_title']);
            
            forum_title = forum_title.replace(translate('forum'), "");
            forum_title = forum_title.replace(translate('messages'), "");
            forum_title = $.trim(forum_title);
            
            page_url = IS_CLIENT ? get_page_url(page_id) : location.href + page_lib_name+'/#forum';
            
            /*if (former_is_locked == 1 && is_locked == 0)
            {
                html += '<p>'+translate('To switch to a forum, click its title')+':</p>'; 
            }*/
            
            former_is_locked = is_locked;
            
            if (do_disp_titles && former_ancestor_id != ancestor_id)
            {
                html += '<p style="clear: both; padding-top: 10px;">'+ancestor_title+':</p>';
                former_ancestor_id = ancestor_id;
            }
            
            html += '<p style="float: right; width: 160px;">'; 
            html += '<input type="checkbox" class="cb" name="fml_'+forum_id+'" id="fml_'+forum_id+'" value="1" onclick="highlight_selected_forum_mls('+block_id+');" />';
            html += '<label for="fml_'+forum_id+'"><a href="'+page_url+'" class="title">'+forum_title+'</a></label>';
            //html += '<span class="small">'+(is_locked == 1 ? 'עדכונים רשמיים' : 'פורום')+'</span>'; 
            html += '</p>'; 
            
        }


    html += '</div>'; 

    html += '</form>';
    
    html += '<script>';
    html += 'var page_pwd = read_cookie("LOCALI_PAGE_PWD");';
    html += 'if (page_pwd !== null) { $("form[name=form_'+block_id+'] input[name=locali_page_pwd]").val(page_pwd); }';
    html += '</script>';
    
    return html; 
}

function highlight_selected_forum_mls(block_id)
{
    $('#form_'+block_id).find('input:checkbox').each(
        function()
        {
            $(this).is(':checked') ?
                $("label[for='"+$(this).attr('id')+"']").addClass("highlight"):
                $("label[for='"+$(this).attr('id')+"']").removeClass("highlight");
        }
    );
}

function join_forum_mls(block_id)
{
    var f = document.forms['form_'+block_id];
    var name = $.trim(f.name.value);
    var email = $.trim(f.email.value);
    
    var num_mls_checked = $('#form_'+block_id+' :checked').length;
    
    if (num_mls_checked == 0)
    {
        my_alert('You should select at least one forum');
        return false;
    }
    
    if (name == '' || name == translate('name') || email == '' || email == translate('email'))
    {
        my_alert('You should fill in your name and email address');
        return false;
    }
    
    if ( ! is_valid_email_syntax(email))
    {
        my_alert('Invalid email syntax');
        return false;
    }
    
    $('#button_'+block_id).hide();
    $('#saving_'+block_id).show();
    
    document.forms['form_'+block_id].submit();
    
    return false;
}
