//
// Displays form for editing ad form
// configuration. Called by onclicking
// block title.
//
function display_edit_ad_form(block_id)
{
    set_form_field('form_ad', FLD_BLOCK_ID, block_id);
    set_form_field('form_ad', 'action', 'ajax_update_block_ad');
    set_form_field('form_ad', 'title', $('#mblock_title_'+block_id).html());
                    
    $('#button_del_ad').show();
}

//
// Send ad form.  
// Used for adding / editing ad form.
//
function send_edit_ad_form()
{
    var title = get_form_field('form_ad', 'title');
    var block_id = get_form_field('form_ad', FLD_BLOCK_ID);
    
    if (title == '')
    {
        my_alert('all field are must');
        return false;
    }

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

    ajax(data, code, "", "button_ad", "saving_ad");

    return false;
}

//
// Deletes ad block.
//
function del_block_ad()
{
    var title = $('#mblock_title_'+block_id).html();
    
    if (confirm(translate('should I delete the form') + ' ' + title))
    {
        document.forms['form_ad'].action.value = 'ajax_del_block_ad';
        var block_id = get_form_field('form_ad', FLD_BLOCK_ID);
        var data = $('#form_ad').serialize();
        var code = "hide_block("+block_id+");";
        code += "close_modals();"
        ajax(data, code);
    }

    return false;
}

// **********************************************************
//
//   The following functions relate to the behaviour of 
//   ad form, and not the the block itself.
//
// **********************************************************


//
// Generates contact form HTML.
//
function gen_html_ad_form(block_id, config)
{
    var html = '';
    
    html += '<form id="form_'+block_id+'" name="form_'+block_id+'" class="block_contact" action="'+get_url_action()+'" method="post" enctype="multipart/form-data">'
    
    html += '<input type="hidden" name="action" value="unid_add_ad" />';
    html += '<input type="hidden" name="'+FLD_PAGE_ID+'" value="'+PAGE_ID+'" />';
    
    html += '<div class="form_item">'; 
    html += '<span class="label">'+translate('name')+'</span>'; 
    html += '<input type="text" name="name" alt="must" onfocus="$(\'#msg_'+block_id+'\').html(\'\')" />'; 
    html += '</div>'; 
    
    html += '<div class="form_item">'; 
    html += '<span class="label">'+translate('email')+'</span>'; 
    html += '<input type="text" name="email" class="ltr" alt="must" onfocus="$(\'#msg_'+block_id+'\').html(\'\')" />'; 
    html += '</div>'; 
    
    html += '<div class="form_item">'; 
    html += '<span class="label">'+translate('phones')+'</span>'; 
    html += '<input type="text" name="phone" class="ltr" alt="must" onfocus="$(\'#msg_'+block_id+'\').html(\'\')" />'; 
    html += '</div>'; 
    
    html += '<div class="form_item">'; 
    html += '<span class="label">'+translate('category')+'</span>'; 
    html += '<select name="category" alt="must" onfocus="$(\'#msg_'+block_id+'\').html(\'\')">'; 
        html += '<option value="">--- '+translate('select category')+' ---</option>'; 
        html += '<option value="'+translate('second hand')+'">'+translate('second hand')+'</option>'; 
        html += '<option value="'+translate('jobs')+'">'+translate('jobs')+'</option>'; 
        html += '<option value="'+translate('houses for rent')+'">'+translate('houses for rent')+'</option>'; 
        html += '<option value="'+translate('houses for sale')+'">'+translate('houses for sale')+'</option>'; 
        html += '<option value="'+translate('rent house')+'">'+translate('rent house')+'</option>'; 
        html += '<option value="'+translate('buy house')+'">'+translate('buy house')+'</option>'; 
        html += '<option value="'+translate('vehicles')+'">'+translate('vehicles')+'</option>'; 
        html += '<option value="'+translate('service providers')+'">'+translate('service providers')+'</option>'; 
        html += '<option value="'+translate('other')+'">'+translate('other')+'</option>'; 
    html += '</select>'; 
    html += '</div>'; 
    
    html += '<div class="form_item">'; 
    html += '<span class="label">'+translate('title')+'</span>'; 
    html += '<input type="text" name="title" alt="must" onfocus="$(\'#msg_'+block_id+'\').html(\'\')" />'; 
    html += '</div>'; 
    
    html += '<div class="form_item">'; 
    html += '<span class="label">'+translate('content')+'</span>'; 
    html += '<textarea name="text" alt="must" onfocus="$(\'#msg_'+block_id+'\').html(\'\')"></textarea>'; 
    html += '</div>'; 
    
    html += '<div class="form_item">'; 
    html += '<span class="label">'+translate('file')+'</span>'; 
    html += '<input type="file" name="file" />'; 
    html += '</div>'; 
    
    html += '<div class="form_item big">'; 
    html += '<span class="label">&nbsp;</span>'; 
    html += '<button id="button_'+block_id+'" onclick="send_bform('+block_id+'); return false;">'+translate('send')+'</button>'; 
    html += '<img id="saving_'+block_id+'" class="image_saving hidden" src="'+CSS_IMAGE_SAVING+'" />'; 
    html += '<p id="msg_'+block_id+'"></p>'; 
    html += '</div>'; 
    
    html += '</form>';
    
    return html; 
}

//
// Send form for adding a forum message.
//
function send_add_ad()
{
    //
    // Parse form data.
    //
    var f = document.forms['form_add_ad'];
    
    var name = f.name.value;
    var email = f.email.value;
    var phone = f.phone.value;
    var category = f.category.value;
    var title = f.title.value;
    var text = f.text.value;
    var file = f.file.value;
    
    //
    // Error cases.
    //
    if (name == '' || email == '' || phone == '' || category == '' || title == '' || text == '')
    {
        my_alert('fill in all must fields');
        return false;
    }

    if (file)
    {             
        var file_ext = parse_file_ext(file);
        var allowed_file_types = ALLOWED_FILE_EXTENTIONS.split(',');
          
        if ($.inArray(file_ext, allowed_file_types) == -1)
        {
            my_alert('file type not allowed');
            return false;
        }
    }
    
    if (text.length > MAX_POST_MSG_LENGTH)
    {                   
        my_alert('message too long');
        return false;
    }
    
    if (text != strip_tags(text))
    {                   
        my_alert('html not allowed');
        return false;
    }
    
    //
    // Submit add forum message form.
    //
    $('#button_add_ad').hide();
    $('#saving_add_ad').show();
    
    f.submit();

    return true;
}

