$(document).ready(function(){

	// Administrator
	if( $('#sessionGroup').text() == '1'){
		if($('#EvaluationStatusId').val() == 8) {
			$('#dynamicDiv0').show();
		}
		if($('#EvaluationStatusId').val() == 17) {
			$('#dynamicDiv1').show();
		}		
	} else{
		//2	Through Mail
		if($('#select-commu_edit').val() == 2) {
			$("#EvaluationStatusId option[value='9']").remove();
		} else if($('#select-commu_edit').val() == 1) {
		//1	Through email			
			if($('#currentLang').text() == 'fre') { strmsg = 'En attente du quest.';}
			if($('#currentLang').text() == 'eng') { strmsg = 'Pend.to compl.quest.';}	
			$("#EvaluationStatusId").append('<option value="9">strmsg</option>');
		}
	}	

	// Any profil
	if($('#EvaluationStatusId').val() == 9 && $('#select-commu_edit').val() == 1) {
		$('#second-nav02a').show();
	}else {
		$('#second-nav02a').show();
	}
		
  $('#select-team').live('change', function() {
    if($(this).val().length != 0) {
	  team = $(this).val();		
      $.getJSON('http://localhost/tmx-es/webroot/evaluations/get_requesters_ajax',{idteam:team}, function(requesters) {
			if(requesters !== null) {
			  populateCustTeamsReqList(requesters);
			}
        });
      }
    });	
	
  $('#EvaluationStatusId').live('change', function() {
		statustest = $(this).val();		
		//8	Pend. to send out Quest. /  17	Missing answers
		if( statustest == 8 || statustest == 17){
			//2	Through Mail			
			$('#select-commu_edit').val(2);
			if( $('#sessionGroup').text() == '1' && statustest == 8){
				$('#dynamicDiv0').show();
				$('#dynamicDiv1').hide();
			}
			if( $('#sessionGroup').text() == '1' && statustest == 17){
				$('#dynamicDiv1').show();
				$('#dynamicDiv0').hide();
			}
			$('#second-nav02a').hide();			
		}
//		else{		
//			//9	Pend.to complete Quest.
//			if( statustest == 9){	
//				//1	Through the online system-Email
//				$('#select-commu_edit').val(1);
//				$('#second-nav02a').show();
//			}else{
//				if (!$(this).val()) {
//					$('#select-commu_edit').val('');
//				}
//				$('#second-nav02a').hide();
//			}			
//			$('#dynamicDiv0').hide();
//			$('#dynamicDiv1').hide();			
//		}
    });

  $('#select-commu_edit').live('change', function() {
		commu_edit = $(this).val();
		empemail = $('#select-emailemp').val();
		//2	Through Mail		
		if( commu_edit == 2){
			//8	Pend. to send out Quest.
			if($('#EvaluationStatusId').val() == 8 && $('#sessionGroup').text() == '1'){
				$('#dynamicDiv0').show();
			}
			$('#second-nav02a').hide();			
		}else{
			//1	Through Email
			if( commu_edit == 1){	
				if(empemail == "") {
					$('#second-nav02a').hide();
				} else {
					$('#second-nav02a').show();
				}
				if($('#EvaluationStatusId').val() == 8) { $('#EvaluationStatusId').val(9); }				
			}else{
				if (!$(this).val()) {
					$('#EvaluationStatusId').val('');
				}
				$('#second-nav02a').hide();
			}
			$('#dynamicDiv0').hide();
		}
    });		
	
  $('#submitbuttoneval').click(function() {
	empemail = $('#select-emailemp').val();
	commutype = $('#select-commu').val();	
	//alert($('#currentLang').text());	
	strmsg = 'The Employee does not have an Email. Please correct it or change the Type of Communication.';
	if($('#currentLang').text() == 'fre'){ strmsg = "L'employé n'a pas de courriel. S'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);
		$('#EvaluationAddForm').submit();
	} 
  }); 	

  $('#submitbuttonevaledit').click(function() {
	empemail = $('#select-emailemp').val();
	commutype = $('#select-commu_edit').val();		
	//alert($('#currentLang').text());	
	strmsg = 'The Employee does not have an Email. Please correct it or change the Type of Communication.';
	if($('#currentLang').text() == 'fre'){ strmsg = "L'employé n'a pas de courriel. S'il vous plaît corriger ou de modifier le type de communication."; }
	
	if( empemail == "" && commutype == 1 ) {
		$('#second-nav02a').hide();
		alert(strmsg);
		return false
	}else{
		$('#EvaluationEditForm').submit();
	} 
  }); 	  
  
});


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