$(document).ready(function ()
{
    var $f = $('div.app\\/container-form form');
    var f = $f.get(0);
    
    ///////////////////////////////////////////////////////
    // mirroring from consignee to institution
    ///////////////////////////////////////////////////////
    
    var f_mirror_consignee = f.elements['s2[s2_s1][user]'];
    var mirror_consignee = false;
    function fix_mirror_consignee()
    {
        if ($(f_mirror_consignee).filter(':checked').val() == 'yes')
        {
            mirror_consignee = true;
            copy_consignee();
        }
        else
        {
            mirror_consignee = false;
            uncopy_consignee();
        }
    }
    function do_mirror_consignee_event()
    {
        do_mirror_consignee($(this));
    }
    function do_mirror_consignee(which)
    {
        if (!mirror_consignee)
        {
            return;
        }
        // find the mirror field
        var this_name = $(which).attr('name');
        var this_elem = $(which).get(0);
        if (!this_name)
        {
            return;
        }
        var that_name = this_name.replace(/s2/g, "s3");
        if (f.elements[that_name])
        {
            var that_elem = f.elements[that_name];
            if ($(that_elem).val() == '')
            {
                $(that_elem).val($(this_elem).val());
                $(that_elem).change();
            }
        }
    }
    function do_unmirror_consignee(which)
    {
        if (mirror_consignee)
        {
            return;
        }
        // find the mirror field
        var this_name = $(which).attr('name');
        var this_elem = $(which).get(0);
        if (!this_name)
        {
            return;
        }
        var that_name = this_name.replace(/s2/g, "s3");
        if (f.elements[that_name])
        {
            var that_elem = f.elements[that_name];
            if ($(that_elem).val() == $(this_elem).val())
            {
                $(that_elem).val('');
                $(that_elem).change();
            }
        }
    }
    function copy_consignee()
    {
        $('#s2_s1_fieldset').find(':input').each(function ()
        {
            do_mirror_consignee($(this));
        });
    }
    function uncopy_consignee()
    {
        $('#s2_s1_fieldset').find(':input').each(function ()
        {
            do_unmirror_consignee($(this));
        });
    }
    $('#s2_s1_fieldset').find(':input').change(do_mirror_consignee_event);
    $(f_mirror_consignee).closest('div.field').click(fix_mirror_consignee);
    fix_mirror_consignee();

    
    ///////////////////////////////////////////////////////
    // mirroring from institution to funding
    ///////////////////////////////////////////////////////
    
    var f_mirror_institution = f.elements['funding[funding_by_institution]'];
    var mirror_institution = false;
    function fix_mirror_institution()
    {
        if ($(f_mirror_institution).filter(':checked').val() == 'yes')
        {
            mirror_institution = true;
            copy_institution();
        }
        else
        {
            mirror_institution = false;
            uncopy_institution();
        }
    }
    function do_mirror_institution_event()
    {
        do_mirror_institution($(this));
    }
    function do_mirror_institution(which)
    {
        if (!mirror_institution)
        {
            return;
        }
        // find the mirror field
        var this_name = $(which).attr('name');
        var this_elem = $(which).get(0);
        if (!this_name)
        {
            return;
        }
        var that_name = this_name.replace('s3[s3_s1]', "funding");
        if (f.elements[that_name])
        {
            var that_elem = f.elements[that_name];
            if ($(that_elem).val() == '')
            {
                $(that_elem).val($(this_elem).val());
                $(that_elem).change();
            }
        }
    }
    function do_unmirror_institution(which)
    {
        if (mirror_institution)
        {
            return;
        }
        // find the mirror field
        var this_name = $(which).attr('name');
        var this_elem = $(which).get(0);
        if (!this_name)
        {
            return;
        }
        var that_name = this_name.replace('s3[s3_s1]', "funding");
        if (f.elements[that_name])
        {
            var that_elem = f.elements[that_name];
            if ($(that_elem).val() == $(this_elem).val())
            {
                $(that_elem).val('');
                $(that_elem).change();
            }
        }
    }
    function copy_institution()
    {
        $('#s3_s1_fieldset').find(':input').each(function ()
        {
            do_mirror_institution($(this));
        });
    }
    function uncopy_institution()
    {
        $('#s3_s1_fieldset').find(':input').each(function ()
        {
            do_unmirror_institution($(this));
        });
    }
    $('#s3_s1_fieldset').find(':input').change(do_mirror_institution_event);
    $(f_mirror_institution).closest('div.field').click(fix_mirror_institution);
    fix_mirror_institution();

    
    ///////////////////////////////////////////////////////
    // facilities dropdown
    ///////////////////////////////////////////////////////
    
    var f_fields = $f.find('#facilities_fieldset fieldset');
    var f_num = $(f.elements['s3[num_facilities]']);
    var f_num_val = parseInt(f_num.val());
    function rehide_facilities()
    {
        var f_num_val = parseInt(f_num.val());
        for (var i = 0; i < f_fields.length; i++)
        {
            if (f_num_val >= (i+1))
            {
                $(f_fields[i]).show();
            }
            else
            {
                $(f_fields[i]).hide();
            }
        }
    }
    rehide_facilities();
    f_num.click(rehide_facilities);
    f_num.keypress(rehide_facilities);
    f_num.change(rehide_facilities);

    
    ///////////////////////////////////////////////////////
    // months field show/hide
    ///////////////////////////////////////////////////////

    var months = $f.find('div.field.s2\\[specify_months\\]');
    months.hide();
    var months_input = months.find('input');
    months.addClass('required').find('label').prepend('<img alt="(Required)" src="/styles/repose/required-field.gif" class="required"/>');
    var expires = f.elements['s2[laws][]'][0];
    var expires_label = $(expires).closest('li').find('label');
    var orig_expires_label = expires_label.text();
    var new_expires_label = orig_expires_label.replace(/__/, '<span><span>__</span></span>');
    expires_label.html(new_expires_label);
    var expires_span = expires_label.children('span');
    months_input.css({width:(expires_span.children('span').width())+'px'});
    months_input.hide();
    expires_span.append(months_input);
    function rehide_months()
    {
        if (expires.checked)
        {
            show_months();
        }
        else
        {
            hide_months();
        }
    }
    function show_months()
    {
        expires_span.children('span').hide();
        months_input.show().focus().select();
    }
    function hide_months()
    {
        months_input.blur().hide();
        expires_span.children('span').show();
    }
    $(expires).click(rehide_months);
    $(expires).keypress(rehide_months);
    $(expires).change(rehide_months);
    months_input.click(function (e) { e.preventDefault();$(this).focus().select(); });
    if (expires.checked)
    {
        expires_span.children('span').hide();
        months_input.show();
    }

    
    ///////////////////////////////////////////////////////
    // recipient agreement fields
    ///////////////////////////////////////////////////////

    var f_notify_name = $(f.elements['s2[s2_s2][name]']);
    var f_agree_name = $(f.elements['agreement[contact]']);
    f_notify_name.change(function ()
    {
        f_agree_name.val(f_notify_name.val());
    });
    var f_institution_name = $(f.elements['s3[s3_s1][organization]']);
    var f_agree_institution = $(f.elements['agreement[institution]']);
    f_institution_name.change(function ()
    {
        f_agree_institution.val(f_institution_name.val());
    });

    
    ///////////////////////////////////////////////////////
    // "other" fields
    ///////////////////////////////////////////////////////
    
    var other_names = {
        'field_s2_s2_s1_status-4' : 'field_s2_s2_s1_status_other',
        'field_s2_laws_item_other' : 'field_s2_other_laws',
        'field_s3_legal_item_other' : 'field_s3_other_legal',
        'field_s3_funding' : 'field_s3_funding_other',
        'field_s3_sterilized-3' : 'field_s3_sterilized_other',
        'field_s3_services_item_other' : 'field_s3_other_services',
        'field_equipment_item_other' : 'field_equipment_other'
    };
    function check_other_vis(which)
    {
        if (!which.get(0).other_field)
        {
            return;
        }
        if (which.filter('select').size())
        {
            if (which.val() == 'Other')
            {
                show_other(which);
            }
            else
            {
                hide_other(which);
            }
        }
        else
        {
            if (which.get(0).checked)
            {
                show_other(which);
            }
            else
            {
                hide_other(which);
            }
        }
    }
    function check_other_vis_radio(which)
    {
        if (!which.get(0).other_field)
        {
            return;
        }
        var choices = which.find('input:radio:checked');
        if (choices.val() == 'Other')
        {
            show_other(which);
        }
        else
        {
            hide_other(which);
        }
    }
    function show_other(which)
    {
        var other_id = which.get(0).other_field;
        $(other_id).closest('div.field').show();
    }
    function hide_other(which)
    {
        var other_id = which.get(0).other_field;
        $(other_id).closest('div.field').hide();
    }
    function check_other_vis_event()
    {
        check_other_vis($(this));
    }
    function check_other_vis_radio_event()
    {
        check_other_vis_radio($(this));
    }
    for (var input_id in other_names)
    {
        var other_field = $('#'+input_id);
        var other_id = other_names[input_id];
        var other_text = $('#'+other_id);
        if (other_field.filter('select').size())
        {
            other_field.click(check_other_vis_event);
            other_field.keypress(check_other_vis_event);
            other_field.change(check_other_vis_event);
        }
        else if (other_field.filter(':radio').size())
        {
            var full_field = other_field.closest('div.field');
            full_field.click(check_other_vis_radio_event);
            full_field.get(0).other_field = '#'+other_id;
        }
        else
        {
            other_field.click(check_other_vis_event);
            other_field.focus(check_other_vis_event);
            other_field.change(check_other_vis_event);
        }
        other_field.get(0).other_field = '#'+other_id;
        check_other_vis(other_field);
    }

    
    ///////////////////////////////////////////////////////
    // yes/no fields with specify
    ///////////////////////////////////////////////////////
    
    var yesno_names = {
        's3\\[internet\\]' : 's3\\[internet_speed\\]'
    };
    function check_yesno_vis(which)
    {
        if (!which.get(0).yesno_field)
        {
            return;
        }
        var choices = which.find('input:radio:checked');
        if (choices.val() == 'yes')
        {
            show_yesno(which);
        }
        else
        {
            hide_yesno(which);
        }
    }
    function show_yesno(which)
    {
        var yesno_id = which.get(0).yesno_field;
        $(yesno_id).show();
    }
    function hide_yesno(which)
    {
        var yesno_id = which.get(0).yesno_field;
        $(yesno_id).hide();
    }
    function check_yesno_vis_event()
    {
        check_yesno_vis($(this));
    }
    for (var input_class in yesno_names)
    {
        var yesno_field = $('div.field.'+input_class);
        var yesno_class = yesno_names[input_class];
        var yesno_text = $('div.field.'+yesno_class);
        
        yesno_field.click(check_yesno_vis_event);
        yesno_field.get(0).yesno_field = 'div.field.'+yesno_class;
        
        check_yesno_vis(yesno_field);
    }
});