// inverting items checked flag, depending on passed 'checkedFlag' and
// filling selectedItems string with values of the selected items,
// that name will be passed as parameter 'elementsName';
// for example, if we will have selected items with values 1 and 2,
// then 'selectedItems' string will contain '1,2';
// this function returns'selectedItems' string, amount of selected items
// and whole amount of proceed items.
// P.S.: if you don't want to change items selection, then pass undefined value in 'checkedFlag' parameter.
function invertSelection( checkedFlag, elementsName ) {

    var selectedItems = '';
    var selectedItemsAmount = 0;

    var elementsList = document.getElementsByName( elementsName );
    for( var i = 0; i < elementsList.length; i++ )
    {
        // invert selection, only if passed defined checkedFlag
        if ( checkedFlag != undefined ) { elementsList[i].checked = checkedFlag; }

        if ( elementsList[i].checked ) {
            if ( selectedItems ) { selectedItems = selectedItems + ','; }
            selectedItems = selectedItems + elementsList[i].value;
            selectedItemsAmount++;
        }

    };

    return [ selectedItems, selectedItemsAmount, elementsList.length ];

};

function ajaxRequest( responseElementId, ajaxUrl, ajaxType, ajaxData, successFunction ) {
    $.ajax( {
        url: ajaxUrl,
        data: ajaxData,
        type: ajaxType,
        success: function ( response ) {
        response = eval( '(' + response + ')' );
            if ( typeof( response ) == 'object' ) {
                if ( response['error'] ) {
                    if ( response['error'] == 'Your session has expired or otherwise invalid. You need to re-login.' ) {
                        var okFunction = function () { $( location ).attr( "href", "/?rm=logout" ); };
                        showDialog( undefined, 'Error', response['error'], okFunction );
                    } else {
                        showAlert( undefined, 'Error', response['error'] );
                    }
                } else {
                    if ( successFunction ) { successFunction( response ); }
                    if ( response['html'] && responseElementId ) { $( "#" + responseElementId ).html( response['html'] ); }
                }
            } else {
                if ( successFunction ) { successFunction( response ); }
                if ( responseElementId ) { $( "#" + responseElementId ).html( response ); }
            }
        },
        error: function () {
        },
        complete: function () {
        },
    } );
};

function showAlert( ownerId, alertTitleHtml, alerMessagetHtml, okFunction ) {
    $( "#alertMessage" ).html( "<p>" + alerMessagetHtml + "</p>" );
    $( "#alertMessage" ).dialog( {
        modal: true,
        title: alertTitleHtml,
        closeOnEscape: true,
        beforeClose: function( event, ui ) { $( '#' + ownerId ).blur(); },
        buttons: {
            Ok: function() {
                $( this ).dialog( "close" );
                if ( okFunction ) { okFunction(); }
            }
        }
    } );
};

function showDialog( ownerId, dialogTitleHtml, dialogMessageHtml, okFunction, cancelFunction ) {
    $( "#alertMessage" ).html( "<p>" + dialogMessageHtml + "</p>" );
    $( "#alertMessage" ).dialog( {
        modal: true,
        title: dialogTitleHtml,
        closeOnEscape: true,
        beforeClose: function( event, ui ) { $( '#' + ownerId ).blur(); },
        buttons: [
            {
                text: "Ok",
                click: function() {
                    $( this ).dialog( "close" );
                    if ( okFunction ) { okFunction(); }
                }
            },
            {
                text: "Cancel",
                click: function() {
                    $( this ).dialog( "close" );
                    if ( cancelFunction ) { cancelFunction(); }
                }
            }
        ]
    } );
};

function showConfirmation( ownerId, confirmationTitleHtml, confirmationMessageHtml, yesFunction, noFunction ) {
    $( "#alertMessage" ).html( "<p>" + confirmationMessageHtml + "</p>" );
    $( "#alertMessage" ).dialog( {
        modal: true,
        title: confirmationTitleHtml,
        closeOnEscape: true,
        beforeClose: function( event, ui ) { $( '#' + ownerId ).blur(); },
        buttons: [
            {
                text: "Yes",
                click: function() {
                    $( this ).dialog( "close" );
                    if ( yesFunction ) { yesFunction(); }
                }
            },
            {
                text: "No",
                click: function() {
                    $( this ).dialog( "close" );
                    if ( noFunction ) { noFunction(); }
                }
            }
        ]
    } );
    $('.ui-dialog :button').blur();
    $('.ui-dialog :button:eq(1)').focus();
};

var freeowShowed = 0;

function showFreeow( title, message ) {
    $("#freeow").show();
    var opts = {};
    opts.classes = [ "gray", "backround" ];
    $("#freeow").freeow( title, message, opts );
    freeowShowed = 1;
};

function delete_wbl_entry( id, contact, url ) {

    var ajaxData = url.substring( 2, url.length );

    var yesFunction = function () {
        var successFunction = function ( response ) {
            showFreeow( 'Success', 'WBL entry <b>' + contact + '</b> successfully deleted.' )
            setTimeout( "location.reload()", 2000 );
        };
        ajaxRequest( undefined, '/', 'POST', ajaxData, successFunction );
    };

    showConfirmation( undefined, 'Attention', 'WBL entry <b>' + contact + '</b> will be deleted.', yesFunction );

};

