$(document).ready(function(){
    
    applyFormElementCss();

    $('.ItemsChecker').bind('click', toggleItemCheckers);
    
    /*
    * Apply hover effect on table rows.
    * This supposes that table use CSS class 'hover' to highlight hoverring row
    */
    function applyRowHoverEffect(tableId){
        $("#"+tableId+" tr").mouseover(function() {
            $(this).addClass("hover");
            }).mouseout(function() {
                $(this).removeClass("hover");
            });
    }


    function applyFormElementCss(){
        //Form title
        $('FIELDSET > H3').addClass('FormTitle');
        //Fieldname
        $('FIELDSET .Input > label').each(function(i,elm){
            if (!$(elm).attr('class'))
                $(elm).addClass('Fieldname');
        });
        $('FIELDSET .Input > textarea').addClass('Fieldname');
        $('FIELDSET .Input > select').addClass('Fieldname');

        //Text and Password fields
        $('FIELDSET .Input input[type=text]').each(function(i,elm){
            if (!$(elm).attr('class'))
                $(elm).addClass('Text');
        });
        $('FIELDSET .Input input[type=password]').each(function(i,elm){
            if (!$(elm).attr('class'))
                $(elm).addClass('ShortText');
        });
        //Button
        $('FIELDSET .Action input[type=submit]').each(function(i,elm){
            if (!$(elm).attr('class'))
                $(elm).addClass('Button');
        });

        //advanced search form - label
        $('FIELDSET[id=pnlAdvancedSearch] label').each(function(i,elm){
            $(elm).removeClass();
            $(elm).addClass('Fieldname');
        });
        //advanced search form - text fields
        $('FIELDSET[id=pnlAdvancedSearch] .Input input[type=text]').each(function(i,elm){
            $(elm).removeClass();
            $(elm).addClass('Text');
        });
        //advanced search form - button
        $('FIELDSET[id=pnlAdvancedSearch] .output input[type=submit]').each(function(i,elm){
            $(elm).removeClass();
            $(elm).addClass('btn');
        });
    }

    // Toggle checkboxes which are used to select a row in a table
    function toggleItemCheckers(){
        var name = $(this).val();
        $(':checkbox[name='+name+'[]'+']').each(function(i, elm){
            $(elm).checked = !$(elm).checked;
        })
    }

    // Get list of IDs of selected items
    function getCheckedItemIds(className){
        var ids = Array();
        $(":checkbox." + className).each(function() {
            if(this.checked) {
               ids.push(this.value);
            }
        });
        return ids;
    }

    /**
      Set the service ID and page alias for form's post action
      @param Form frm the form object
      @param string sid Service ID
      @param string alias Page's alias or Widget ID
    */
    function setFormAction(frm, sid, alias){
        //Set post service ID
        if (frm.POST_SERVICE != undefined)
            frm.POST_SERVICE.value = sid;
        else
            $(frm).append('<input type="hidden" value="'+sid+'" name="POST_SERVICE">');
        
        //Set post success page alias
        if (alias != ''){
            if (frm.POST_SUCCESS_PAGE != undefined)
                frm.POST_SUCCESS_PAGE.value = alias;
            else
                $(frm).append('<input type="hidden" value="'+alias+'" name="POST_SUCCESS_PAGE">');
        }
    }
})

