var myModel = user;
myModel.entity_name = 'unset';
// bind the model's 'name' attribute to the input textbox 
// identified by it's id (CSS selector '#name') 
// and react on DOM 'keyup' and 'change' events
// then bind again the model's 'name' attribute to the feedback 
// label named 'nameEcho'
$(myModel)
.dataBind({modelAttribute:'firstName', selector:'#firstName', eventToBind:'keyup change'})
.dataBind({modelAttribute:'firstName', selector:'#nameEcho'})

.dataBind({modelAttribute:'client_type', selector:'#client_type', eventToBind:'change'})

.dataBind({modelAttribute:'entity_name', selector:'#entity_name'})
;

$(myModel).on('change.client_type', function(event){
    if(event.newValue!==event.oldValue){
	if(event.newValue == 1){
            $('#person_billing').show();
            $('#company_billing').hide();
            $('#person_bank').show();
            $('#company_bank').hide();
            $(myModel).attr('entity_name', myModel.firstName);
        }else if(event.newValue == 2){
            $('#person_billing').hide();
            $('#company_billing').show();
            $('#person_bank').hide();
            $('#company_bank').show();
            $(myModel).attr('entity_name', myModel.company_legal_name);
        }
    }
    
});

$('#company_billing').hide(); 
$('#company_bank').hide();
$('#person_billing').hide();
$('#person_bank').hide();
    
if(myModel.client_type == 1){
    $('#person_billing').show();
    $('#company_billing').hide();   
    $('#person_bank').show();
    $('#company_bank').hide();
    $(myModel).attr('entity_name', myModel.firstName);
}else if(myModel.client_type == 2){
    $('#person_billing').hide();
    $('#company_billing').show();
    $('#person_bank').hide();
    $('#company_bank').show();
     $(myModel).attr('entity_name', myModel.company_legal_name);
}

$(myModel).on('change.firstName', function(event){
    $(myModel).attr('entity_name', myModel.firstName);
});
$(myModel).on('change.company_legal_name', function(event){
    $(myModel).attr('entity_name', myModel.company_legal_name);
});