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

	submitCounter = 0;

	$('#submitbuttonorder').click(function(event) {
		submitCounter ++;
		empemail = $('#select-emailemp').val();
		commutype = $('#select-commu').val();
		strmsg = 'The Employee does not have an Email.\nPlease correct it or change the Type of Communication.';
		if($('#currentLang').text() == 'fre'){ strmsg = "L'employé n'a pas de courriel.\nS'il vous plaît corriger ou de modifier le type de communication."; }	
		
		if( empemail == "" && commutype == 1 ) {
			alert(strmsg);
			return false
		} else {		
			$('#select-requester').prop('disabled', false);
			$('#select-customer').prop('disabled', false);

			if(submitCounter > 1) {
				alert("Your order is being created and will be ready soon.");
				event.preventDefault();
			} else {
				$('#OrderAddForm').submit();
			}

		} 
		});


	// $('#submitbuttonorder').click(function() {
	// 	empemail = $('#select-emailemp').val();
	// 	commutype = $('#select-commu').val();
	// 	strmsg = 'The Employee does not have an Email.\nPlease correct it or change the Type of Communication.';
	// 	if($('#currentLang').text() == 'fre'){ strmsg = "L'employé n'a pas de courriel.\nS'il vous plaît corriger ou de modifier le type de communication."; }	
		
	// 	if( empemail == "" && commutype == 1 ) {
	// 		alert(strmsg);
	// 		return false
	// 	} else {		
	// 		$('#select-requester').prop('disabled', false);
	// 		$('#select-customer').prop('disabled', false);
	// 		$('#OrderAddForm').submit();
	// 	} 
	// 	});

	$('#select-statusorder').live('change', function() {
	statust = $('#select-statusorder').val();
	if( statust == 2 || statust == 3 ) {
		$('#divcomment').show();
	} else {
		$('#divcomment').hide();
	}	
    });
});

    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);
    }