$(document).ready(function(){
	
  $('#select-team').live('change', function() {
	$('#select-requester').empty();
	$('#select-requester').html('<option value="">..... Select one .....</option>');

    if($(this).val().length != 0) {
	  team = $(this).val();
		
      $.getJSON('http://localhost/tmx-es/webroot/users/get_groups_ajax',{idteam:team}, function(groups) {
                    if(groups !== null) {
                      populateCustTeamsGrpList(groups);
                    }
        });		
      }
    });
	
  $('#select-group').live('change', function() {
    if($(this).val().length != 0) {
		team = $('#select-team').val();
		group = $(this).val();
		
      $.getJSON('http://localhost/tmx-es/webroot/users/get_requesters_ajax',{idteam:team,idgroup:group}, function(requesters) {
                    if(requesters !== null) {
                      populateCustTeamsReqList(requesters);
                    }
        });		
      }
    });	

	$('#update-passw').click(function() {
		if($('#update-passw').is(':checked'))
		{				
			$('#divupdatepassw').show();		
		}
		else
		{
			$('#divupdatepassw').hide();
		}
	});	
	
  $('#submitUserAdd').click(function() {
//		$('#select-customer').removeAttr('disabled'); 
		$('#select-team').prop('disabled', false); 
		$('#UserAddForm').submit();
  });

  $('#submitUserEdit').click(function() {
	newpass = $('#UserNewpassword').val().length;
	if($('#update-passw').is(':checked') && newpass == 0)
	{
		alert('Please enter a New Password.');
		return false
	}
	else
	{
		$('#select-requester').prop('disabled', false); 
		$('#select-group').prop('disabled', false); 
		$('#UserEditForm').submit();	
	}
  });  
 
});

 
function populateCustTeamsGrpList(groups) {
  var options = '';
  options += '<option value="">..... Select one .....</option>'
 
  $.each(groups, function(index, group) {
    options += '<option value="' + index + '">' + group + '</option>';
  });
  $('#select-group').html(options);
}

function populateCustTeamsReqList(requesters) {
  var options = '';
 
  options += '<option value="">..... Select one .....</option>'
 
  $.each(requesters, function(index, requester) {
    options += '<option value="' + index + '">' + requester + '</option>';
  });
  $('#select-requester').html(options);
}