/**
 * Specific javascript routines for the dhz rma form
 *
 * @author David Hazel
 * @copyright 2009 Voyager Components Inc.
 * @notes: Uses the jQuery javascript library
 */

$(document).ready(function(){
    /// increase the usability of various links
    //  (terms_and_conditions and notice_and_guidelines)
    // use hidden legalese page to pull content from
    $('#form_title_div').append('<div id="hidden_notice_content"></div>');
    $('#hidden_notice_content').hide();
    $('#hidden_notice_content').prepend('<img src="images/netlife_spinner3-black.gif"></img>');

    // dialog for notice-and-guidelines
    $('#notice_and_guidelines_link').attr('href','#');
    $('#notice_and_guidelines_link').click(function() {
        // add spinner
        $('#hidden_notice_content').empty();
        $('#hidden_notice_content').prepend('<img src="images/netlife_spinner3-black.gif"></img>');

        // generate dialog
        $('#hidden_notice_content').dialog({
            title: 'RMA Notice and Guidelines', 
            width: '75%', 
            draggable: true, 
            modal: false, 
            autoOpen: true, 
            buttons: {
                'Okay': function() {
                    $(this).dialog('close');
                }
            }
        });
        // load contents into notice div
        $('#hidden_notice_content').load(
            location.pathname+'?view=legalese #rma_notice_and_guidelines'
            ,function() {
                // modify css
                $('#hidden_notice_content').css('padding','1em');
                $('#hidden_notice_content p').css('margin-top','1em');
                $('#hidden_notice_content h2').css('font-weight','bold');
                $('#hidden_notice_content ul').css(
                    'list-style-type','disc');
                $('#hidden_notice_content h2').css('margin-top','1em');
                $('#hidden_notice_content ul').css('margin-top','1em');
                $('#hidden_notice_content ul').css('margin-left','1em');
                $('#hidden_notice_content ol').css('margin-top','1em');
                $('#hidden_notice_content ol').css('margin-left','1.5em');

            }
        );
    });

    // dialog for terms and conditions
    $('.terms_and_conditions_link').attr('href','#');
    $('.terms_and_conditions_link').click(function() {
        // add spinner
        $('#hidden_notice_content').empty();
        $('#hidden_notice_content').prepend('<img src="images/netlife_spinner3-black.gif"></img>');

        // generate dialog
        $('#hidden_notice_content').dialog({
            title: 'RMA Terms and Conditions', 
            width: '75%', 
            draggable: true, 
            modal: true, 
            autoOpen: true, 
            buttons: {
                'Okay': function() {
                    $(this).dialog('close');
                }
            }
        });

        // load contents into notice div
        $('#hidden_notice_content').load(
            location.pathname+'?view=legalese #legalese_terms_and_conditions'
            ,function() {
                // modify css
                $('#hidden_notice_content').css('padding','1em');
                $('#hidden_notice_content p').css('margin-top','1em');
                $('#hidden_notice_content h2').css('font-weight','bold');
                $('#hidden_notice_content h2').css('margin-top','1em');
                $('#hidden_notice_content ul').css(
                    'list-style-type','disc');
                $('#hidden_notice_content ul').css('margin-top','1em');
                $('#hidden_notice_content ul').css('margin-left','1em');

            }
        );
    });
    
    // if in Return Information section, make formstep repeatable
    if($('#current_form_step .form_step_legend:contains("Return Information")').length){
        // add 'add new part' button to the bottom of the formstep
        // create the button
        button1 = $('<button class="part_add_button" type="button" style="font-size:small;">Add A Part</button>').button();
        // add the button click event handler
        button1.bind('click',function(){
            addNewPart();
        });
        // attach the button
        $('#current_form_step hr').before(button1);
        
        // add 'remove part' button to each part section
        if($('#current_form_step fieldset > div').length > 1){
            $('#current_form_step fieldset > div').each(function(){
                new RemovalButton($(this));
            });
        }
    }
});


//-----------------------------------------------------------------------------
// functions below
//-----------------------------------------------------------------------------


/**
 * Adds a part to the Return Information formstep
 */
function addNewPart(){
    // copy formstep node
    new_entry = $('#current_form_step fieldset > div:last').clone();
    new_entry.hide();
    $('#current_form_step fieldset > div:last').after(new_entry);
    new_entry_input_nodes = $('div :input', new_entry);

    // calculate new index value
    new_index = parseInt($('div :input', new_entry).attr('meta:index')) + 1;

    // modify new formstep values and attributes
    new_entry_input_nodes.attr('name',function(){
        return $(this).attr('name').split('[')[0]+'['+new_index+']';
    });
    // modify new formstep meta indices to new value
    new_entry_input_nodes.attr('meta:index', new_index);
    // clear out any values that may be entered
    new_entry_input_nodes.val('');
    // remove all buttons
    $('button', new_entry).remove()

    // update div class toggle
    new_entry.toggleClass('form_odd');

    // add 'remove' button
    new RemovalButton(new_entry);

    // display the new entry
    new_entry.slideDown();

    // set the focus
    $('div :input', new_entry)[0].focus();
}

/**
 * Constructor function. Inserts a 'remove part' button at the end of the node.
 *
 * @param jQuery_object node
 *  The XML node within which to append the button
 */
function RemovalButton(node){
    var _node = node;

    // create the button (jquery themed)
    this.button = $('<button class="part_removal_button" type="button">Remove This Part</button>').button();

    // add the button click event handler
    this.button.bind('click',function(){
        // delete the part node
        _node.fadeOut().slideUp('slow',function(){
            // remove the node
            $(this).remove()

            // clear out the toggle classes for re-calculation
            $('#current_form_step fieldset > div').removeClass('form_odd');

            // re-calculate toggle classes for part display
            $('#current_form_step fieldset > div').each(function(index){
                if(! isEven(index)){
                    $(this).addClass('form_odd');
                }
            });

            // if only one part remains, remove 'remove part' button
            if($('#current_form_step fieldset > div').length < 2){
                $('.part_removal_button').remove();
            }
        });
    });

    // append the button
    this.button.appendTo($('p:first',_node));
}

/**
 * Checks for whether a number is odd or even
 *
 * @param bool some_number
 *  The number to check
 * @return
 *  The result true/false
 */
function isEven(someNumber){
    return ((someNumber % 2) == 0) ? true : false;
}

