function echo( value )
{
    document.write( value );
}

function decor_form1_open( width, height )
{
    echo_width = width ? ' width="' + width + '" ' : ' width="206" ';
    echo_height = height ? ' height="' + height + '" ' : '';
    
    echo( '<table class="form1" cellspacing="0" ' + ' ' + echo_width + ' ' + echo_height + '>' );
    echo( '<tr>');
    echo( '    <td class="form1cell1">&nbsp;</td>');
    echo( '    <td class="form1cell2">&nbsp;</td>');
    echo( '    <td class="form1cell3">&nbsp;</td>');
    echo( '</tr>');
    echo( '<tr>');
    echo( '    <td class="form1cell4">&nbsp;</td>');
    echo( '    <td class="form1cell5">');
}

function decor_form1_close()
{
    echo( '</td>');
    echo( '    <td class="form1cell6">&nbsp;</td>');
    echo( '</tr>');
    echo( '<tr>');
    echo( '    <td class="form1cell7">&nbsp;</td>');
    echo( '    <td class="form1cell8">&nbsp;</td>');
    echo( '    <td class="form1cell9">&nbsp;</td>');
    echo( '</tr>');
    echo( '</table>' );
}

function groupaction_confirm_enable()
{
    if( confirm('Вы уверено что хотите включить выбранные элементы?' ) == false )
    {
        return false;
    }
    
    document.forms['FormFields']['action'].value = 'enable';
    
    return true;
}

function decor_bar3d( lang_name, lang_id )
{
    echo( '<div class="bar3d"><div class="leftside"><div class="rightside"><div class="content">' );
    
    if( lang_id )
    {
        echo( '<div class="langbar lang_' + lang_id + '" title="' + lang_name+ '">' + lang_name + '</div>' );
    }
    else
    {
        echo( lang_name );
    }

    echo( '</div></div></div></div>' );
    
    return true;
}

function groupaction_confirm_disable()
{
    if( confirm('Вы уверено что хотите выключить выбранные элементы?' ) == false )
    {
        return false;
    }
    
    document.forms['FormFields']['action'].value = 'disable';
    
    return true;
}

function groupaction_confirm_delete()
{
    if( confirm('Вы уверено что хотите удалить выбранные элементы?' ) == false )
    {
        return false;
    }
    
    document.forms['FormFields']['action'].value = 'delete';
    
    return true;
}

function content_price_highline( value, tr_line_id )
{
    if( value )
    {
        document.getElementById(tr_line_id).className = '';
    }
    else
    {
        document.getElementById(tr_line_id).className = 'disabled';
    }

    return true;
}

function currency_select_changes( select_object )
{
    if( select_object.value == '' || select_object.value == '.' )
    {
        select_object.className = 'select price_currency empty';
    }
    else
    {
        select_object.className = 'select price_currency ' + select_object.value;
    }

    return true;
}

function statistic_in_manual__addblock( block_id )
{
    var data = $("div#container_"+block_id+" div.etalon").html();
    $("div#container_"+block_id+" div.pointer").before( '<div class="statistic_in_manual__additional_block">' + data + '</div>' );
}

function excel_line_clicked( lineid )
{
    var checkbox = document.forms['FormFields']['sheetline['+lineid+']'];
    checkbox.checked = ! checkbox.checked;
    
    document.getElementById("excel_line_"+lineid).className = checkbox.checked ? 'selected' : '';
    
    return true;
}

var excel_table_columns_count = 0;
var excel_process_rows_count = 0;

function excel_submit_handler( formobj )
{
    if( formobj['step'].value == 'step3' ) // 'step' is next step value
    {
        if( formobj['currency_value'].value == '' )
        {
            alert( 'Пожалуйста, выберите валюту.' );
            return false;
        }
        
        var set_column_name  = 0;
        var set_column_price = 0;
        var set_column_count = 0;
        var set_column_wap_traffic = 0;
        var index;

        for( index=0; index<excel_table_columns_count; index++ )
        {
            switch( formobj['column_type['+index+']'].value )
            {
                case 'name':
                {
                    set_column_name++;
                    break;
                }
                
                case 'price':
                {
                    set_column_price++;
                    break;
                }
                
                case 'count':
                {
                    set_column_count++;
                    break;
                }
                
                case 'wap_traffic':
                {
                    set_column_wap_traffic++;
                    break;
                }
            }
        }
        
        if( set_column_name != 1 )
        {
            alert( 'Укажите одну колонку с наименование контента.' );
            return false;
        }

        if( formobj['report_type'].value == 'std' )
        if( set_column_count != 1 )
        {
            alert( 'Укажите одну колонку с кол-вом продаж контента.' );
            return false;
        }

        if( formobj['report_type'].value == 'wap' )
        if( set_column_wap_traffic != 1 )
        {
            alert( 'Укажите одну колонку с объемом трафика.' );
            return false;
        }
        
        if( get_checked_value( formobj['price_method'], 'fromfile' ) == true && set_column_price != 1 )
        {
            alert( 'Укажите одну колонку с ценой контента или укажите фиксированную цену.' );
            return false;
        }
        
        if( get_checked_value( formobj['price_method'], 'const' ) == true && set_column_price > 0 )
        {
            alert( 'Вы указали фиксированную цену и выбрали колонку с ценой. Выберите только один конкретный вариант.' );
            return false;
        }

        if( formobj['update_method'].value == 'replace' )
        if( confirm( 'Вы выбрали метод вставки статистики, при котором все предыдущие данные за данный отчетный период будут удалены. Вы уверены в своем выборе?' ) === false )
        {
            return false;
        }
    }

    if( formobj['step'].value == 'step4' ) // 'step' is next step value
    {
        // Проверить, чтобы все поля были заполнены
        
        var index;
        
        for( index=0; index<excel_process_rows_count; index++ )
        {
            if( formobj['data['+index+'][cnt]'].value == '' )
            {
                return confirm( 'Определены не все соответствия между входящими именами и элементами из базы Nomoc.\nЭлементы без указанного имени контента будут проигнорированы при вставке статистики.\nВы уверены что хотите продолжит?' )
                     ? true
                     : false;
            }
        }
    }
    
    return true;
}

function get_checked_value( list_radio_objects, source_radio_value )
{
    var index;

    for( index=0; index<list_radio_objects.length; index++ )
    {
        if( list_radio_objects[index].value == source_radio_value )
        {
            return list_radio_objects[index].checked;
        }
    }
    
    return false;
}

function stat_content_highline( value, tr_line_id )
{
    if( value )
    {
        document.getElementById(tr_line_id).className = '';
    }
    else
    {
        document.getElementById(tr_line_id).className = 'disabled';
    }

    return true;
}

var rasysMouseCursorPositionX = 0;
var rasysMouseCursorPositionY = 0;

function rasysMouseGetCursorPosition( e )
{
    rasysMouseCursorPositionX = 0;
    rasysMouseCursorPositionY = 0;

    var ev = ( ! e ) ? window.event : e; // IE : Moz

    if( ev.pageX )
    {
        //Moz
        rasysMouseCursorPositionX = ev.pageX; // + window.pageXOffset;
        rasysMouseCursorPositionY = ev.pageY; // + window.pageYOffset;
    }
    else if( ev.clientX )
    {   //IE
        rasysMouseCursorPositionX = ev.clientX + document.body.scrollLeft;
        rasysMouseCursorPositionY = ev.clientY + document.body.scrollTop;
    }
    else
    {
        return false; //old browsers
    }
}

document.onmousemove = rasysMouseGetCursorPosition;

function show_exchange_hint( year, month )
{
    var hint_id = year + '' + ( month < 10 ? '0' : '' ) + month;
    
    hide_exchange_hint();

    $("div#exchange_hint_"+hint_id).show();
    
    return true;
}

function move_exchange_hint( year, month )
{
    var hint_id = year + '' + ( month < 10 ? '0' : '' ) + month;

    $("div#exchange_hint_"+hint_id).css( 'left', rasysMouseCursorPositionX + 10 );
    $("div#exchange_hint_"+hint_id).css( 'top',  rasysMouseCursorPositionY + 10 );

    return true;
}

function hide_exchange_hint()
{
    $("div.exchange_hint").hide();

    return true;
}

function content_media_show_delete_box( checkvalue, div_id )
{
    if( checkvalue )
    {
        document.getElementById( div_id ).className = 'delete_box active';
    }
    else
    {
        document.getElementById( div_id ).className = 'delete_box';
    }
}

function content_allowlangs_mark( value, cell_id )
{
    var cellobj = document.getElementById( cell_id );
    cellobj.className = value ? "checked" : "";
    return true;
}

function content_allowlangs_markgroup( value, language_id )
{
    var i;
    for( i=1; i<=allowlangs_baseitems_id.length; i++ )
    {
        var baseindex = allowlangs_baseitems_id[i];
        
        if( document.forms['FormFields']['allowlangs['+baseindex+']['+language_id+']'] )
        {
            document.forms['FormFields']['allowlangs['+baseindex+']['+language_id+']'].checked = value;
            content_allowlangs_mark( value, 'cell_'+baseindex+'_'+language_id );
        }
    }
    
    return true;
}

function GetContent_ShowScreen( group_class_id, show_screen_index )
{
    $("div.screengroup_"+group_class_id).hide();
    $("div#screen_"+group_class_id+'_'+show_screen_index).show();
}