var affiliate_field_name = 'Join_Affiliate_Program[0]';
var affiliate_field = null;
var base_url = 'http://dolphin.tybit.com';

$(document).ready( function() {
    if (document.join_form) {
        affiliate_field = document.join_form.elements[affiliate_field_name];
    } else if (document.edit_form) {
        affiliate_field = document.edit_form.elements[affiliate_field_name];
    }

    if (affiliate_field) {
        affiliate_field.onchange = function() { toggleAffiliateForm(affiliate_field); };
        toggleAffiliateForm(affiliate_field);
    }
});

function toggleAffiliateForm(dropdown) {
	if ((dropdown.selectedIndex) && (dropdown.selectedIndex == 1)) {
		//$('tr[type="affiliate_field"]').show();
        $('input[name^="aff_"]').each(function() {
            if ($(this).attr('disabled') == true) {
                $(this).removeAttr('disabled');
            }
            if ($(this).hasClass('input_text_readonly')) {
                $(this).removeClass('input_text_readonly');
            }
        });

        $('select[name^="aff_"]').each(function() {
          if ($(this).attr('disabled') == true) {
            $(this).removeAttr('disabled')
          }
        });
	} else {
		//$('tr[type="affiliate_field"]').hide();
        $('input[name^="aff_"]').each(function() {
            $(this).attr('disabled', 'true');
            if (!$(this).hasClass('input_text_readonly')) {
                $(this).addClass('input_text_readonly');
            }
        });

        $('select[name^="aff_"]').each(function() {
          $(this).attr('disabled', 'true');
        });
	}
}