var containerCount = 0;

function batchIsolate(groupNumber){
	var tableRowCount = document.getElementById('productTable').rows.length;
	var batchCount = document.getElementById('batchCount').value;
	var rowPos = document.getElementById('batch'+groupNumber).rowIndex;
	var end = 0;
	if (groupNumber == batchCount){
		end = tableRowCount - 1;
	} else if (groupNumber < batchCount) {
		nextGroupNumber = groupNumber + 1;
		end = document.getElementById('batch'+nextGroupNumber).rowIndex - 1;
	}
	for(var i=rowPos+1;i<end+1;i++){
		if(document.getElementById('productTable').rows[i].style.display == ''){
			document.getElementById('productTable').rows[i].style.display = 'none';
			document.getElementById('batchLink'+groupNumber).innerHTML = '[ + ]';
		} else {
			document.getElementById('productTable').rows[i].style.display = '';
			document.getElementById('batchLink'+groupNumber).innerHTML = '[ - ]';			
		}
	}
	document.getElementById('batchLink'+groupNumber).blur();
}

function checkContainers() {
  $('#checkAllContainers').each(function() {
    if (this.checked) {  // check all container checkboxes
      $('.containerCheckBox').each(function() {
        this.checked = true;
      });
    } else {  // uncheck all container checkboxes
      $('.containerCheckBox').each(function() {
        this.checked = false;
      });
    }
  });
}

function showClearDialog() {
    if ($('input[name=containerID]:checked').length > 0) {
	    var fieldNames = '';
	    if ($('#clearWharfPickupDate').is(':checked')) {
	        fieldNames = fieldNames + "<tr><td>Wharf Pickup Date</td></tr>";
	    }
	    if ($('#clearDemurrageCommencementDate').is(':checked')) {
	        fieldNames = fieldNames + "<tr><td>Demurrage Commencement Date</td></tr>";
	    }
	    if ($('#clearDeliveredDate').is(':checked')) {
	        fieldNames = fieldNames + "<tr><td>Delivered Date</td></tr>";
	    }
	    if ($('#clearUnpackedDate').is(':checked')) {
	        fieldNames = fieldNames + "<tr><td>Unpacked Date</td></tr>"; 
	    }
	    if ($('#clearDehiredDate').is(':checked')) {
	        fieldNames = fieldNames + "<tr><td>Dehired Date</td></tr>"; 
	    }
	    if ($('#clearLeftUnpackSiteDate').is(':checked')) {
	        fieldNames = fieldNames + "<tr><td>Left Unpack Site Date</td></tr>"; 
	    }
	    if (fieldNames.length > 0) {
		    $('body').append('<div id="warningDialog" style="ui-corner-all"></div>');
		    $('#warningDialog').dialog({
		      autoOpen: true,
			  bgiframe: true,
			  height: 200,
			  width: 455,
			  resizable: false,
			  draggable: false,
			  modal: true,
			  closeOnEscape: false,
			  title: 'Are you sure you want to clear Date(s)?',
			  zIndex: 9999 ,
			  buttons: {
				'Cancel': function() {
				  $(this).dialog('close');
				  $(this).remove();
				},
				'Clear': function() {
				  $(this).dialog('close');
				  $(this).remove();
				  submitUpdateContainers();
				}
			  }
			});
			var message = '<table cellspacing="0" cellpadding="0" border="0" summary="">';
			message += fieldNames;
			message += '</table>';
			$('#warningDialog').html(message);
		    $('#warningDialog').dialog('open');
		    return;
		} else {
		  submitUpdateContainers();
		}
	} else {
	  alert("Please select containers to update");
	}
}

function submitUpdateContainers() {
  $('#attemptMultiUpdate').val('true');
  $('#attemptExport').val('false');
  $('#search').submit();
}

function getContainerUpdateHistory(containerID, containerNumber) {
  // alert('cID: ' + containerID + ", number: " + containerNumber);
  setPosBy('moreDisplay','historyImg'+containerID,18,5);
  $('#moreDisplay').show();
  loaderHeight('/images/loader.gif', 'loading container history', 'moreDisplay', 20);
  $('#historyLink'+containerID).blur();  
  $.get('/operations/container/getUpdateHistory.jsp?containerID='+containerID+'&containerNumber='+containerNumber, function(data) {
    $('#moreDisplay').html($.trim(data));
    setPosBy('moreDisplay','historyImg'+containerID,18,5);
    $('#moreDisplay').show();
  });
}

  function getContainerNotes(containerID) {
    //alert("containerID:- "+ containerID);
    loader('/images/loader.gif','loading notes','containerNoteList');
    var dataSource = 'getContainerNote.jsp?containerID=' + containerID;
    $.get(dataSource, function(data) {
      $('#containerNoteList').html($.trim(data));
    });
  }
  
  function saveContainerNote(containerID) {
    var note = $('#note').val();
    if (note != null && note.trim() != '') {
      $.get('containerNoteSave.jsp?containerID='+containerID+'&description='+note, function(data){
        if ($.trim(data) == 0) {
          $('#note').val('');
          getContainerNotes(containerID);
          //$('#send').removeAttr('disabled');
        } else {
          alert('An Error has occured when adding the note')
        }
      });
    } else {
      alert('You must enter a note before clicking save.')
    }
  }
  
  function removeContainerNote(id, note) {
    $('#trashcan'+id).blur();
    $('body').append('<div id="warningDialog" style="ui-corner-all"></div>');
    $('#warningDialog').dialog({
      autoOpen: true,
	  bgiframe: true,
	  height: 190,
	  width: 455,
	  resizable: false,
	  draggable: false,
	  modal: true,
	  closeOnEscape: false,
	  title: 'Are you sure you want to delete this Note?',
	  zIndex: 9999 ,
	  buttons: {
		'Cancel': function() {
		  $(this).dialog('close');
		  $(this).remove();
		},
		'DELETE NOW': function() {
		  $(this).dialog('close');
		  $(this).remove();
		  execRemoveContainerNote(id);
		}
	  }
	});
	var message = '<table cellspacing="0" cellpadding="0" border="0" summary="">';
	message += '<tr><td>';
	message += note;
	message += '</td></tr>';
	message += '</table>';
	$('#warningDialog').html(message);
    $('#warningDialog').dialog('open');
    return;
  }
  
  this.execRemoveContainerNote = function(id) {
    loaderImageOnly('/images/loader.gif','trash'+id);
    $.get('containerNoteRemove.jsp?containerNoteID='+id, function(data){
      if ($.trim(data) == '0') {
        $('#note_'+id).fadeOut('slow', function(){
          $('#note_'+id).remove();
          formatTableRows('containerNoteListTable');
          var testRows = $('#containerNoteListTable tr').length;
          if(testRows <= 1){
            $('#containerNoteListTable').fadeOut('slow', function(){
              $('#containerNoteListTable').remove();
            });
          }
        });
      } else {
        $('#trash'+id).html($.trim(data));
      }
    });  
  }
